Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp385878rdb; Fri, 8 Sep 2023 04:04:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrLweShl/kKnHpG5GcVMAvYOSs5UmvkI1qKTvORFO9vknTtDWwVes3UUFruAhRO+7YPQiW X-Received: by 2002:a05:6a00:2191:b0:68a:42d0:6b9b with SMTP id h17-20020a056a00219100b0068a42d06b9bmr1967633pfi.3.1694171067452; Fri, 08 Sep 2023 04:04:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694171067; cv=none; d=google.com; s=arc-20160816; b=LdeIwYHL2nUF2MK1mrLyQuXzlawlxodth4+IugloBvF62X+oPdDeEkKBcYhWdTp3bN xfmGDk2bqvIdW/Muuivz969uBpaEPMr2N037vjG5MQIj6BCDsSXZswvFVXAgiAmdGXYy lxangpo3ulwuBGxxx/ra7a/1MvSFugE1I3g4yPnzmJWHJsPzJtA4mvdm3k1N9SjUKDuy HXxgjTCsYSid9VmdhdjeXy5U+PO/OHcxFLaU2/67ZATPqJzmYCWkRt2QHb3zAcbj4LG7 9zBoQd3ihcnjJyFjLvSn3v2gNYwnbJhV2ZwumGru3znel6V27hxQG7fGv+lkPb+l/2Tk JrpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:references:in-reply-to:date:cc:to:from :subject:message-id; bh=afl8JdugAXnHrNhrkucQf2dXPq0pP1DfdWc/PlYQ2BA=; fh=uG8VQDk5sSyVuEC2vVQQ8mDlcEDnXpqHZa98lRI7IRc=; b=f2FEHtnncuOmgsiGGYQA5s5jdG/SApXbcu3Mitt5hM0cCFx3iisSpQOACk6/XV1Is5 S0y95prlv+MttvM0MH4SVhUhWjgD6fk3drBZBXqfJR1Cn4dtDsBB0CQc/H1XZdpz0Qkh bDFO/IOQN7ba9WbU3t5COjTOksB20Bh2OhhseClLRUUZcea/jRht6t4cgL7aZX4Auh9Q mSFVCXSKH3zjGdBr+e0qmrUDNLRoQE34FGPWvkVpep9/XqgpYoP+pqbvc94uql5wAToj ZMuAldpJkvekPOKYibRYqozFTbluOnfgVyoxmfGyGGPJNpHoL2+Rp/E+IFAoiqRMk9nk foXA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bx12-20020a056a00428c00b0068e285eaa31si1373628pfb.259.2023.09.08.04.04.01; Fri, 08 Sep 2023 04:04:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234915AbjIHJdc convert rfc822-to-8bit (ORCPT + 99 others); Fri, 8 Sep 2023 05:33:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229644AbjIHJdc (ORCPT ); Fri, 8 Sep 2023 05:33:32 -0400 Received: from outpost1.zedat.fu-berlin.de (outpost1.zedat.fu-berlin.de [130.133.4.66]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BD9F11B; Fri, 8 Sep 2023 02:33:28 -0700 (PDT) Received: from inpost2.zedat.fu-berlin.de ([130.133.4.69]) by outpost.zedat.fu-berlin.de (Exim 4.95) with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (envelope-from ) id 1qeXrN-000Qdx-SS; Fri, 08 Sep 2023 11:33:05 +0200 Received: from p5b13a40a.dip0.t-ipconnect.de ([91.19.164.10] helo=[192.168.178.81]) by inpost2.zedat.fu-berlin.de (Exim 4.95) with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (envelope-from ) id 1qeXrN-001RDz-Kp; Fri, 08 Sep 2023 11:33:05 +0200 Message-ID: Subject: Re: [PATCH] sh: push-switch: reorder cleanup operations to avoid UAF bug From: John Paul Adrian Glaubitz To: Duoming Zhou , ysato@users.sourceforge.jp Cc: dalias@libc.org, kvalo@kernel.org, pavel@ucw.cz, pabeni@redhat.com, rostedt@goodmis.org, linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org Date: Fri, 08 Sep 2023 11:33:04 +0200 In-Reply-To: <20230802033737.9738-1-duoming@zju.edu.cn> References: <20230802033737.9738-1-duoming@zju.edu.cn> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT User-Agent: Evolution 3.48.4 MIME-Version: 1.0 X-Original-Sender: glaubitz@physik.fu-berlin.de X-Originating-IP: 91.19.164.10 X-ZEDAT-Hint: PO X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2023-08-02 at 11:37 +0800, Duoming Zhou wrote: > The original code puts flush_work() before timer_shutdown_sync() > in switch_drv_remove(). Although we use flush_work() to stop > the worker, it could be re-scheduled in switch_timer. As a result, > the UAF bug will happen. The detail is shown below: > > (cpu 0) | (cpu 1) > switch_drv_remove() | > flush_work() | > ... | switch_timer //timer > | schedule_work(&psw->work) > timer_shutdown_sync() | > ... | switch_work_handler //worker > kfree(psw) //free | > | psw->state = 0 //use > > This patch puts timer_shutdown_sync() before flush_work() to > mitigate the bugs. As a result, the worker and timer could > be stopped safely before the deallocate operations. > > Fixes: 9f5e8eee5cfe ("sh: generic push-switch framework.") > Signed-off-by: Duoming Zhou > --- > arch/sh/drivers/push-switch.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/arch/sh/drivers/push-switch.c b/arch/sh/drivers/push-switch.c > index c95f48ff3f6..6ecba5f521e 100644 > --- a/arch/sh/drivers/push-switch.c > +++ b/arch/sh/drivers/push-switch.c > @@ -101,8 +101,8 @@ static int switch_drv_remove(struct platform_device *pdev) > device_remove_file(&pdev->dev, &dev_attr_switch); > > platform_set_drvdata(pdev, NULL); > - flush_work(&psw->work); > timer_shutdown_sync(&psw->debounce); > + flush_work(&psw->work); > free_irq(irq, pdev); > > kfree(psw); Reviewed-by: John Paul Adrian Glaubitz -- .''`. John Paul Adrian Glaubitz : :' : Debian Developer `. `' Physicist `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913