From patchwork Mon Oct 25 23:24:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sujit Kautkar X-Patchwork-Id: 12583491 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B8FAC433EF for ; Mon, 25 Oct 2021 23:25:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7E98B60ED4 for ; Mon, 25 Oct 2021 23:25:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234621AbhJYX1i (ORCPT ); Mon, 25 Oct 2021 19:27:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234559AbhJYX1h (ORCPT ); Mon, 25 Oct 2021 19:27:37 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B801C061348 for ; Mon, 25 Oct 2021 16:25:14 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id c4so5088212plg.13 for ; Mon, 25 Oct 2021 16:25:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oTG8QvPNWzFgpKy7CqYXTp73RNb8SI/uyXnU4fGa3S8=; b=fTWBELNVu9D1RTOFOigK/cvOBFjDksmRbwT77WHyIQiqX0o6wq9X5lrZVVuKiGNh2j Astj3ci5BkgQ2TSXR+6+MG2OqurA+1TULr/5VzSVfwLad4xOMViFzQ2b5lUwySj4YKLR e11qgu7zqr3q+4FNLAW77cHXvr9LlFIMWqIcU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oTG8QvPNWzFgpKy7CqYXTp73RNb8SI/uyXnU4fGa3S8=; b=z8j1vwcgux4ZsxffL+O58yqEdnnuZLrgzLDJD5ugKm2bzR5EHf8r99wuqBkXJ6+got yRD06jW9/BoHCAkCvrMIomtfnNqsE+0t4v1caIgSrEVgrJZaA/KqqFUVI6hXaiPEFn6U mmm0sUarmlHK5PkWj+iU8cSpujClEFmeTZ9TEcp8NPzwHRs5FUIu8SHE8pWaOF40hNQW pYON3gA6RHIpz03ME7PWbXnE9c25r/Iuj1JuVbxBOY7TlpxGLGfZICmMRSqU8cFCY07a LsuTOzzipZ7rS2OjzdjxFneZ6CP0pAYm+EZ1b9f5EvxhmfaYfbMvbWrd0H5KTQtvIPa3 +aBA== X-Gm-Message-State: AOAM532aDffZyuZ5Ikawkus/VhSD0oiMppQ8p27GWSjim1ipkZ/TD3cZ 4qfkvkPhuSi4Qz0r7oTMbsQC3A== X-Google-Smtp-Source: ABdhPJxhiLnaU2FxzdZRMbo5t3eqX5j9UbbaVda6lptnK0ywpOAMdzqVvoau8aVgr0KaiBtNGm6qnA== X-Received: by 2002:a17:90b:3a81:: with SMTP id om1mr39312152pjb.184.1635204314186; Mon, 25 Oct 2021 16:25:14 -0700 (PDT) Received: from sujitka-glaptop.hsd1.ca.comcast.net ([2601:646:8e00:b2f0:eac2:13a5:2214:747b]) by smtp.gmail.com with ESMTPSA id ob5sm20891535pjb.30.2021.10.25.16.25.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 16:25:13 -0700 (PDT) From: Sujit Kautkar To: Andy Gross , Ohad Ben-Cohen Cc: Sibi Sankar , Bjorn Andersson , Stephen Boyd , Sujit Kautkar , Sujit Kautkar , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-remoteproc@vger.kernel.org Subject: [PATCH 1/2] rpmsg: glink: Fix use-after-free in qcom_glink_rpdev_release() Date: Mon, 25 Oct 2021 16:24:59 -0700 Message-Id: <20211025162452.1.Id19324ea36b4cf89faf98bf520bc6b6f01240433@changeid> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20211025232500.1775231-1-sujitka@chromium.org> References: <20211025232500.1775231-1-sujitka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-remoteproc@vger.kernel.org From: Sujit Kautkar qcom_glink_rpdev_release() sets channel->rpdev to NULL. However, with debug enabled kernel, qcom_glink_rpdev_release() gets delayed due to delayed kobject release and channel gets released by that time and triggers below kernel warning. To avoid this use-after-free, add a condition to checks if channel was already released. | BUG: KASAN: use-after-free in qcom_glink_rpdev_release+0x54/0x70 | Write of size 8 at addr ffffffaba438e8d0 by task kworker/6:1/54 | | CPU: 6 PID: 54 Comm: kworker/6:1 Not tainted 5.4.109-lockdep #16 | Hardware name: Google Lazor (rev3+) with KB Backlight (DT) | Workqueue: events kobject_delayed_cleanup | Call trace: | dump_backtrace+0x0/0x284 | show_stack+0x20/0x2c | dump_stack+0xd4/0x170 | print_address_description+0x3c/0x4a8 | __kasan_report+0x144/0x168 | kasan_report+0x10/0x18 | __asan_report_store8_noabort+0x1c/0x24 | qcom_glink_rpdev_release+0x54/0x70 | device_release+0x68/0x14c | kobject_delayed_cleanup+0x158/0x2cc | process_one_work+0x7cc/0x10a4 | worker_thread+0x80c/0xcec | kthread+0x2a8/0x314 | ret_from_fork+0x10/0x18 Signed-off-by: Sujit Kautkar --- drivers/rpmsg/qcom_glink_native.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/rpmsg/qcom_glink_native.c b/drivers/rpmsg/qcom_glink_native.c index e1444fefdd1c0..cc3556a9385a9 100644 --- a/drivers/rpmsg/qcom_glink_native.c +++ b/drivers/rpmsg/qcom_glink_native.c @@ -270,6 +270,7 @@ static void qcom_glink_channel_release(struct kref *ref) spin_unlock_irqrestore(&channel->intent_lock, flags); kfree(channel->name); + channel = NULL; kfree(channel); } @@ -1372,8 +1373,10 @@ static void qcom_glink_rpdev_release(struct device *dev) { struct rpmsg_device *rpdev = to_rpmsg_device(dev); struct glink_channel *channel = to_glink_channel(rpdev->ept); + if (channel) { + channel->rpdev = NULL; + } - channel->rpdev = NULL; kfree(rpdev); } From patchwork Mon Oct 25 23:25:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sujit Kautkar X-Patchwork-Id: 12583493 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D9B6C433EF for ; Mon, 25 Oct 2021 23:25:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7E7F960D07 for ; Mon, 25 Oct 2021 23:25:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234982AbhJYX1l (ORCPT ); Mon, 25 Oct 2021 19:27:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234652AbhJYX1i (ORCPT ); Mon, 25 Oct 2021 19:27:38 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E41A5C061745 for ; Mon, 25 Oct 2021 16:25:15 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id oa4so9476662pjb.2 for ; Mon, 25 Oct 2021 16:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=F+2km2Ms41Ov0dS1z/A4rzJgD2Yh00Ba8bMPWR6hat0=; b=kTQtPmmOOzD87pssCi7DNb0cDtnb/JN0oFnGUiY+UEwc67S/D/2KuE+mBYUabmMWIO MPtnc1gbQKZMGALHzWk9Pfl0Z/66WUi1O3YpUxgPwakncSQpg/OedJXKbHi5fVjoHuyM mBBtCgwkH02DH0CsvIiHx3tgCogxypPdnsjaQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=F+2km2Ms41Ov0dS1z/A4rzJgD2Yh00Ba8bMPWR6hat0=; b=qybPnDxj8Lbw9VKBBwyGDTu5GeL1IbD4BuHPrMbnhMDeSGV0hRmWuSGmlhguR7cb+0 reKWf1NcIODiMG5quyDCNSAlYGh6BdGtvAB5M07Tqe38Qo+0A5jpucR/YmTD/en/ZjFU iGZXP1e7tQz80py3K/cGJJu+esPNTlXR7fdk7A3ynlD9Wgu4aErw0jmdGbCKlDsfqz19 /2+TD9uq5Q3Hx3Ti099FzoFY7LNL+pT5wtEd6wOmvUruDAOLeA+fVEcHKax7NS4UXerr uK3dCm2SOY75UX2oXHKOomnN3f6vmt0PExAnkouEqyQF7J3GrE1NWXsWmrFpswe3AgP/ gbqA== X-Gm-Message-State: AOAM5336q7KC79yX3ElUlxcM5sJxkBpjKJckOvuhUhWPMFnswsqfGF6J d60/zs+WOO3Ro4aUuVUANBiUFQ== X-Google-Smtp-Source: ABdhPJzixDA3xdtSIj2DXOgZ8A9jy/aeErGASKZUzBhRyIuRTPFjAvfw5LcEibiNnj02WzRsHKFZ3A== X-Received: by 2002:a17:902:aa82:b0:140:4655:b211 with SMTP id d2-20020a170902aa8200b001404655b211mr11419249plr.38.1635204315466; Mon, 25 Oct 2021 16:25:15 -0700 (PDT) Received: from sujitka-glaptop.hsd1.ca.comcast.net ([2601:646:8e00:b2f0:eac2:13a5:2214:747b]) by smtp.gmail.com with ESMTPSA id ob5sm20891535pjb.30.2021.10.25.16.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 16:25:15 -0700 (PDT) From: Sujit Kautkar To: Andy Gross , Ohad Ben-Cohen Cc: Sibi Sankar , Bjorn Andersson , Stephen Boyd , Sujit Kautkar , Sujit Kautkar , linux-kernel@vger.kernel.org, linux-remoteproc@vger.kernel.org Subject: [PATCH 2/2] rpmsg: glink: Update cdev add/del API in rpmsg_ctrldev_release_device() Date: Mon, 25 Oct 2021 16:25:00 -0700 Message-Id: <20211025162452.2.I507c5cea0cf97db4cedfa0e47029e711e7edd0df@changeid> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20211025232500.1775231-1-sujitka@chromium.org> References: <20211025232500.1775231-1-sujitka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-remoteproc@vger.kernel.org From: Sujit Kautkar Replace cdev add/del APIs with cdev_device_add/cdev_device_del to avoid below kernel warning. This correctly takes a reference to the parent device so the parent will not get released until all references to the cdev are released. | ODEBUG: free active (active state 0) object type: timer_list hint: delayed_work_timer_fn+0x0/0x7c | WARNING: CPU: 7 PID: 19892 at lib/debugobjects.c:488 debug_print_object+0x13c/0x1b0 | CPU: 7 PID: 19892 Comm: kworker/7:4 Tainted: G W 5.4.147-lockdep #1 | ================================================================== | Hardware name: Google CoachZ (rev1 - 2) with LTE (DT) | Workqueue: events kobject_delayed_cleanup | pstate: 60c00009 (nZCv daif +PAN +UAO) | pc : debug_print_object+0x13c/0x1b0 | lr : debug_print_object+0x13c/0x1b0 | sp : ffffff83b2ec7970 | x29: ffffff83b2ec7970 x28: dfffffd000000000 | x27: ffffff83d674f000 x26: dfffffd000000000 | x25: ffffffd06b8fa660 x24: dfffffd000000000 | x23: 0000000000000000 x22: ffffffd06b7c5108 | x21: ffffffd06d597860 x20: ffffffd06e2c21c0 | x19: ffffffd06d5974c0 x18: 000000000001dad8 | x17: 0000000000000000 x16: dfffffd000000000 | BUG: KASAN: use-after-free in qcom_glink_rpdev_release+0x54/0x70 | x15: ffffffffffffffff x14: 79616c6564203a74 | x13: 0000000000000000 x12: 0000000000000080 | Write of size 8 at addr ffffff83d95768d0 by task kworker/3:1/150 | x11: 0000000000000001 x10: 0000000000000000 | x9 : fc9e8edec0ad0300 x8 : fc9e8edec0ad0300 | | x7 : 0000000000000000 x6 : 0000000000000000 | x5 : 0000000000000080 x4 : 0000000000000000 | CPU: 3 PID: 150 Comm: kworker/3:1 Tainted: G W 5.4.147-lockdep #1 | x3 : ffffffd06c149574 x2 : ffffff83f77f7498 | x1 : ffffffd06d596f60 x0 : 0000000000000061 | Hardware name: Google CoachZ (rev1 - 2) with LTE (DT) | Call trace: | debug_print_object+0x13c/0x1b0 | Workqueue: events kobject_delayed_cleanup | __debug_check_no_obj_freed+0x25c/0x3c0 | debug_check_no_obj_freed+0x18/0x20 | Call trace: | slab_free_freelist_hook+0xb4/0x1bc | kfree+0xe8/0x2d8 | dump_backtrace+0x0/0x27c | rpmsg_ctrldev_release_device+0x78/0xb8 | device_release+0x68/0x14c | show_stack+0x20/0x2c | kobject_cleanup+0x12c/0x298 | kobject_delayed_cleanup+0x10/0x18 | dump_stack+0xe0/0x19c | process_one_work+0x578/0x92c | worker_thread+0x804/0xcf8 | print_address_description+0x3c/0x4a8 | kthread+0x2a8/0x314 | ret_from_fork+0x10/0x18 | __kasan_report+0x100/0x124 Signed-off-by: Sujit Kautkar --- drivers/rpmsg/rpmsg_char.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index 876ce43df732b..b63a5c396da57 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -458,7 +458,7 @@ static void rpmsg_ctrldev_release_device(struct device *dev) ida_simple_remove(&rpmsg_ctrl_ida, dev->id); ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt)); - cdev_del(&ctrldev->cdev); + cdev_device_del(&ctrldev->cdev, &ctrldev->dev); kfree(ctrldev); } @@ -493,14 +493,13 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) dev->id = ret; dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret); - ret = cdev_add(&ctrldev->cdev, dev->devt, 1); + ret = cdev_device_add(&ctrldev->cdev, &ctrldev->dev); if (ret) goto free_ctrl_ida; /* We can now rely on the release function for cleanup */ dev->release = rpmsg_ctrldev_release_device; - ret = device_add(dev); if (ret) { dev_err(&rpdev->dev, "device_add failed: %d\n", ret); put_device(dev);