Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp623508pxb; Wed, 3 Nov 2021 09:36:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwy+zY2veeOd2q+x2f+Fz+NDhbfYzyyw4WUIiuuwK4fPngVWOo3ApPGn5xZgon+eiQDfbLU X-Received: by 2002:a05:6e02:1b85:: with SMTP id h5mr1520760ili.112.1635957412700; Wed, 03 Nov 2021 09:36:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635957412; cv=none; d=google.com; s=arc-20160816; b=mM1eqMKYGQ3JptU8oca9+jLwtbNBvZmZJLrM9cDvIszYuTVI/Lvvje5HknogvaSWg0 28ci5sgATDlhrrOPk/tQ0hFxje9bj+qd0KoKcA2ygCBXG813okk9KCoGk4YtGR9XbjqM vkz0BrZglymJBHgHNr27EZXbHY97G4BoNVa/jWrIdsbe8oa0uPWYCCwbD3hOURcEL9YY Av6HOGCC0UisV4oSJ75ZKzzIn6iuOctWeOO0o/3PN4EZd2/55/ledoUf2zJUEXv98uMB TPgM544O3ZAFVf/84bQVOU0XZ6WMShz6ntx1M4ctsP8Xal0rYKIMVkER8qvrx4WMXPpy oYOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=slIukMnGdEjFvYLnsgPyMFQpuRCWCSMtQookMpWIfvo=; b=j5MCEanHT7RDG29CcJvTvUNCaDqnm0i3tJPzc6m0fyj9M90aLTRnHG1ZVS9Yt2koSE AdNCI9DjOPXmbDnPqMjVqO8TCpXtvWPBRpIIV7MENiqW2dK3+YBKSyKFbgIaUbH2h3m6 FmzV1wjyiCnr4ihn5C+vLaPC3N0wXdCOEt+7uszFR0TQn8tXhdlPwOx1EkCaFHdu79dg sWi7Rirb2V0g2hCHxTh5anE4xI+OuBWvM1WqLCo0K8CeCq96SV5luhgfNP6pyWDH6Sg2 5BY9HtA0GJv7n9KkISzEWeEdsinZ4RpwX3yIt6cEH09aSuZJa5gAHdXfyojP3Z7+8TNa 24lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=el2xkxJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k8si4884357iow.17.2021.11.03.09.36.39; Wed, 03 Nov 2021 09:36:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=el2xkxJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232806AbhKCQgl (ORCPT + 99 others); Wed, 3 Nov 2021 12:36:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229652AbhKCQgk (ORCPT ); Wed, 3 Nov 2021 12:36:40 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD142C061714 for ; Wed, 3 Nov 2021 09:34:03 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id u33so2904382pfg.8 for ; Wed, 03 Nov 2021 09:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=slIukMnGdEjFvYLnsgPyMFQpuRCWCSMtQookMpWIfvo=; b=el2xkxJfACIM7oCJT65CebNCQ8WmNxVQ2DB/EionnHAb40T4ku3HmRR9z64gnxsfll vaX6PXIKcm+L7nuNIE7UwoFKA3Rc+r5Rfm/Ltum2UK48CXz+uWGDl3EXFCMu8N0E+nuF CmbqdqbzF6EXQH+J+Yt0mTHpkN7aUekgRdoPI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=slIukMnGdEjFvYLnsgPyMFQpuRCWCSMtQookMpWIfvo=; b=rFh3R9M9iYRl6JDua2rLjYI+kr5FhyeJuhdmZ/6SfzXzT3UFE0jGCpElaqrUUkfK1r 19TPu00ETu6+sEgMWB372W06MFhPkIOGZwazhO2smdTsXbpwBEYuJrAUdEY/9HtEHayT A0gHecd7MTVhH9FjuZs4rlo9o3C28Au9NxIqy2s+v0G/6C5jf3+sxACLdrExY4EVaasF kSiM8TjdJXzTeUG+SSZp/1tZmyA4WIV3bkke6ey390UNDOgcBcHbB/fYu0koOI6VZxX8 a0aAoltkVd5TTlvxBjE2gt4pXpdBXAyCo5aToN2DVFxf21Obc+jQvuX/DZ/5AR462lY0 z3iQ== X-Gm-Message-State: AOAM532XJ7P6qyue1Gc0OA7y3LwLJCBTs2bEJR2ip+sVkAU8AZT857/Q jrfyE/NxO+1WSai7DDMwRkyufg== X-Received: by 2002:a63:7516:: with SMTP id q22mr19387601pgc.24.1635957243446; Wed, 03 Nov 2021 09:34:03 -0700 (PDT) Received: from localhost ([2620:15c:202:201:b5cc:11fb:70c8:786f]) by smtp.gmail.com with UTF8SMTPSA id e8sm2967610pfv.183.2021.11.03.09.34.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Nov 2021 09:34:02 -0700 (PDT) Date: Wed, 3 Nov 2021 09:34:01 -0700 From: Matthias Kaehlcke To: Sujit Kautkar Cc: Andy Gross , Ohad Ben-Cohen , Bjorn Andersson , Sibi Sankar , Stephen Boyd , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-remoteproc@vger.kernel.org Subject: Re: [PATCH v3 1/2] rpmsg: glink: Fix use-after-free in qcom_glink_rpdev_release() Message-ID: References: <20211102235147.872921-1-sujitka@chromium.org> <20211102165137.v3.1.I2858f54e737295d746ea67e1dc0068fe63913ae5@changeid> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20211102165137.v3.1.I2858f54e737295d746ea67e1dc0068fe63913ae5@changeid> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Sujit, On Tue, Nov 02, 2021 at 04:51:49PM -0700, Sujit Kautkar wrote: > 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, clear ept > pointers during ept destroy and channel release and add a new condition > in qcom_glink_rpdev_release() to access channel > > | 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 > --- > Changes in v3: > - Clear ept pointers and add extra conditions > > Changes in v2: > - Fix typo in commit message > > drivers/rpmsg/qcom_glink_native.c | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/drivers/rpmsg/qcom_glink_native.c b/drivers/rpmsg/qcom_glink_native.c > index e1444fefdd1c0..0c64a6f7a4f09 100644 > --- a/drivers/rpmsg/qcom_glink_native.c > +++ b/drivers/rpmsg/qcom_glink_native.c > @@ -269,6 +269,9 @@ static void qcom_glink_channel_release(struct kref *ref) > idr_destroy(&channel->riids); > spin_unlock_irqrestore(&channel->intent_lock, flags); > > + if (channel->rpdev) > + channel->rpdev->ept = NULL; > + > kfree(channel->name); > kfree(channel); > } > @@ -1214,6 +1217,8 @@ static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept) > channel->ept.cb = NULL; > spin_unlock_irqrestore(&channel->recv_lock, flags); > > + channel->rpdev->ept = NULL; > + > /* Decouple the potential rpdev from the channel */ > channel->rpdev = NULL; > > @@ -1371,9 +1376,12 @@ static const struct rpmsg_endpoint_ops glink_endpoint_ops = { > 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); > + struct glink_channel *channel = NULL; no need to initialize the pointer, it is assigned in the path that uses it. > > - channel->rpdev = NULL; > + if (rpdev->ept) { > + channel = to_glink_channel(rpdev->ept); > + channel->rpdev = NULL; > + } > kfree(rpdev); > } Looks like this is already fixed in -next by: commit 343ba27b6f9d473ec3e602cc648300eb03a7fa05 Author: Chris Lew Date: Thu Jul 30 10:48:15 2020 +0530 rpmsg: glink: Remove channel decouple from rpdev release If a channel is being rapidly restarting and the kobj release worker is busy, there is a chance the rpdev_release function will run after the channel struct itself has been released. There should not be a need to decouple the channel from rpdev in the rpdev release since that should only happen from the close commands. Signed-off-by: Chris Lew Signed-off-by: Deepak Kumar Singh Signed-off-by: Bjorn Andersson Link: https://lore.kernel.org/r/1596086296-28529-6-git-send-email-deesin@codeaurora.org