From mboxrd@z Thu Jan 1 00:00:00 1970 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 72ECCC433F5 for ; Wed, 3 Nov 2021 16:34:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 52C106103B for ; Wed, 3 Nov 2021 16:34:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232870AbhKCQgl (ORCPT ); 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-Google-Smtp-Source: ABdhPJySZdnjD6oZ6jsH9vBRx+o0Q6ebKk/YPLXkYbMgxUsRQJ4Dv6pdhVl4g0C3Ex4PS3dI0dvFew== 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