Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp1908713lqp; Tue, 16 Apr 2024 01:03:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVcqjFsjcVp7bD3kGQKTotbic4A1HM7/itJYD4dkdK1uJIArlIc8xTKknzXQ+FY1au3nHLqDh6I3Awf8lQEPDNO7QRg3q14oNOX/Jfppg== X-Google-Smtp-Source: AGHT+IESiJ3+MBn7KH7hwTIs5KDFZVFzlNvNBXDNDtAo2TT0J/8ILio5ALMMaeVX+TO9MAYbICua X-Received: by 2002:a05:6358:838c:b0:186:249a:c8de with SMTP id c12-20020a056358838c00b00186249ac8demr15534197rwk.5.1713254636584; Tue, 16 Apr 2024 01:03:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713254636; cv=pass; d=google.com; s=arc-20160816; b=X9gmQJaoFCEzmdamH8hrgyQHrWqGO+930UpwDd4zDMCScClQEnIWZ4PRAgtCH1tXW7 l+DaTna43p7OwI/hZYFQikbrJIei0gD8JuNB0Qm0xLtbnUI+mItaOW2JBBNhpQj1oq1O vgKc7YLLYU0N7b8wj+kpXbPA4OwPwDTEm8QYmEdbAXD6Mc2ofU+I2xknrfMbzT5jjY7v kBPMJA3dOBD/pzAtDqDX27liFR8JMmOp9MS5uwdPIY7RSQ+7sqsoidl61lpDc0a/2UmS N0Z/LB/DLeEk0QRWOZlSAH1WHSktlYWdIKs1/Y35qiTOoVE9AcUGnTwlY+/DfpGxkprI EVTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=YtBq8yEZv097YxmGZO1uAqajLtUZ0qspENxuvlWUMkc=; fh=F0fdy0D7H2ipz+GGhAUhSQCC/8YB8/fl5ChyR+ZV59Y=; b=P7spvGJi+5qqVbEHQCTu66ZEknsnSK00c1LKpd6kigDGHa7UuE2UCPo2Jn+KVp9X66 wD2aENc5AJi6BARUHccFLpyep4HN9EXhxF01VAOaMBWW++Ha7zYyMULvsdbOQJblmT5Y HDhdadBjOQ6l3qyUys708X+NwnAlyZt+MXWaPsM109Rk+/Fj7RJzEig7C8K+eR5Iyxi1 6FKr4hoI7oi4hbqJir6AaWO3JIUdJwK8/yl7MzWuFd7Xl8YGcWmvd1b/qT1IidmSGnFC 6Ow1TSUcNI3BLU5YA6RsEQtiQXc3nzB3VZ2nvPbXFfxAwM422g5QNu+tbywJQj9DM/sX kA0Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=MyBil7y6; arc=pass (i=1 spf=pass spfdomain=163.com dkim=pass dkdomain=163.com dmarc=pass fromdomain=163.com); spf=pass (google.com: domain of linux-kernel+bounces-146458-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146458-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id j12-20020ac85f8c000000b00434a7b22841si12584728qta.103.2024.04.16.01.03.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 01:03:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-146458-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=MyBil7y6; arc=pass (i=1 spf=pass spfdomain=163.com dkim=pass dkdomain=163.com dmarc=pass fromdomain=163.com); spf=pass (google.com: domain of linux-kernel+bounces-146458-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146458-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4D7311C22372 for ; Tue, 16 Apr 2024 08:03:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 89AB412C472; Tue, 16 Apr 2024 08:03:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="MyBil7y6" Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.2]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5545C75808 for ; Tue, 16 Apr 2024 08:03:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.2 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713254629; cv=none; b=sfNVDRENuiImbddWep0i2f8caHDyZ8AZwAeqv6ScNWz+8KPaDuKNap7khQ35iytEGbSmbCIArAHzExsTlEkeP6L2xQpuLVrt5l5Zk8m4uvvUPoPzLcQsC+nH1r1SPTIviBU54sPCOEm/5fxEvgY1Hx+Mg4SBc68YEkYYDE2Ez4g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713254629; c=relaxed/simple; bh=kYNTupiv3TQw3fNvik9CrYQof7/tZvkNuRS5rjJflOw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=a7NPk4gcR5x/jDemordxmlvrxKIw2ElCz7b/5SjmlPu2F7Hx3lN4tWXQR3Dd0DosCit0IeIs1lGOd5FUMUArDudhTD6iFml+jDw3K7LcdHIdXYxnE6gX8Y6u0Ze5X+GdUkO3Ch5mNhSOaSNMPqHMY5LaUbjQrts586WE6Mf931k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=MyBil7y6; arc=none smtp.client-ip=220.197.31.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=YtBq8 yEZv097YxmGZO1uAqajLtUZ0qspENxuvlWUMkc=; b=MyBil7y69RfEJOQK3poij wtEPInopyvPyO8Boov1c831gSKz9YmwGkmmBpShXwENKg5C/31BczWlB9lGnGelL Go7awvruOkWKqw7AmI++8bGNQmJinP2BuR2lqRosiUEc+xTiySsbG1kyaRoHjpLA X7lkaGZXEo/jKAu1IoHPl4= Received: from flipped.. (unknown [159.226.94.118]) by gzga-smtp-mta-g2-3 (Coremail) with SMTP id _____wD3Xx_DMB5m5JBDBQ--.33062S2; Tue, 16 Apr 2024 16:03:16 +0800 (CST) From: Sicong Huang To: gregkh@linuxfoundation.org, johan@kernel.org, elder@kernel.org Cc: greybus-dev@lists.linaro.org, linux-kernel@vger.kernel.org, Sicong Huang Subject: [PATCH v1] greybus: Fix use-after-free bug in gb_interface_release due to race condition. Date: Tue, 16 Apr 2024 16:03:13 +0800 Message-Id: <20240416080313.92306-1-congei42@163.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wD3Xx_DMB5m5JBDBQ--.33062S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxJr1rtFWDuw1UXFy3WFWxZwb_yoW8GFW3pr WrGrWUGw4DJFs8tws8KF1UXFZYgw17KryakFWxCw4xGr98Ary3Zrn0yFZrAFZ3Cw1rJr17 Zr4UZ3sYvas8X3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07Ul-erUUUUU= X-CM-SenderInfo: 5frqwvrlusqiywtou0bp/1tbivgbC8mV4Ir559gAAsG In gb_interface_create, &intf->mode_switch_completion is bound with gb_interface_mode_switch_work. Then it will be started by gb_interface_request_mode_switch. Here is the relevant code. if (!queue_work(system_long_wq, &intf->mode_switch_work)) { ... } If we call gb_interface_release to make cleanup, there may be an unfinished work. This function will call kfree to free the object "intf". However, if gb_interface_mode_switch_work is scheduled to run after kfree, it may cause use-after-free error as gb_interface_mode_switch_work will use the object "intf". The possible execution flow that may lead to the issue is as follows: CPU0 CPU1 | gb_interface_create | gb_interface_request_mode_switch gb_interface_release | kfree(intf) (free) | | gb_interface_mode_switch_work | mutex_lock(&intf->mutex) (use) Fix it by canceling the work before kfree. Signed-off-by: Sicong Huang --- drivers/greybus/interface.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/greybus/interface.c b/drivers/greybus/interface.c index fd58a86b0888..d022bfb5e95d 100644 --- a/drivers/greybus/interface.c +++ b/drivers/greybus/interface.c @@ -693,6 +693,7 @@ static void gb_interface_release(struct device *dev) trace_gb_interface_release(intf); + cancel_work_sync(&intf->mode_switch_work); kfree(intf); } -- 2.34.1