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 X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89209C433E7 for ; Wed, 14 Oct 2020 07:51:51 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0CBC7214D8 for ; Wed, 14 Oct 2020 07:51:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="H/unE1y9"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=mg.codeaurora.org header.i=@mg.codeaurora.org header.b="cS3icK/5" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0CBC7214D8 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Message-ID:References:In-Reply-To:Subject:To:From: Date:MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=BAyj5O8w48E6gW77sRPbuioSO1Aa/gUB4AfLZYjRZ3A=; b=H/unE1y9zqwKX+nuAoU2KvrRj t24zYGpBjZrWuNTyAzDP894ysFOQFRPZgvd4OF/MXlQOq5K7MNX3jAZXcPxuvH44cCg9ZCeDN6Kx9 dEY38y+4BepXJbZPxMjbYIT4t4Gc51BxbPWWsGd3nvHDMB5HF+Dmp1wrwbp/8O5ijZ+Zczmq3iuO8 0Bd/zXaw6iqD4SySvm2KKZto8cEbLVfvpqultTCPJQQ8R3i3COs9ZKF6C5HLiP03jy2b1jXIeHs/D EqWk0na8+4ZJELRq4s1skpl9u7FCMFtcAQelaJw258DLFggW7CSW/8IBTnbjyGuLz36Wge9i55w21 J2NHCNEwg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kSbYC-00068n-0g; Wed, 14 Oct 2020 07:50:20 +0000 Received: from z5.mailgun.us ([104.130.96.5]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kSbY7-00068R-4I for linux-arm-kernel@lists.infradead.org; Wed, 14 Oct 2020 07:50:17 +0000 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1602661816; h=Message-ID: References: In-Reply-To: Subject: Cc: To: From: Date: Content-Transfer-Encoding: Content-Type: MIME-Version: Sender; bh=ZDa3vOpmEZ8eDLlwS42JxZqhi1NArG+4awI4WGrgrN8=; b=cS3icK/5mScVeVDRp9+Up56wq6BQmzzs4Glh1mnSKZ/2JDmDJNjiTB14ExVgoiekT9wuTl8y 4KEpfprveE+NNWK65rpfwNRiZjXn55db50b6SdEygPDYwCjJ7YnHA0oMxXLDdE8zihY/etlM lzotEqcghpQRJNT1IV99CnNIWqE= X-Mailgun-Sending-Ip: 104.130.96.5 X-Mailgun-Sid: WyJiYzAxZiIsICJsaW51eC1hcm0ta2VybmVsQGxpc3RzLmluZnJhZGVhZC5vcmciLCAiYmU5ZTRhIl0= Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n02.prod.us-west-2.postgun.com with SMTP id 5f86adaead37af35ecb4508f (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Wed, 14 Oct 2020 07:50:06 GMT Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 75BCBC43382; Wed, 14 Oct 2020 07:50:06 +0000 (UTC) Received: from mail.codeaurora.org (localhost.localdomain [127.0.0.1]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: saiprakash.ranjan) by smtp.codeaurora.org (Postfix) with ESMTPSA id 7E0A6C433C9; Wed, 14 Oct 2020 07:50:05 +0000 (UTC) MIME-Version: 1.0 Date: Wed, 14 Oct 2020 13:20:05 +0530 From: Sai Prakash Ranjan To: Suzuki K Poulose Subject: Re: [PATCH 1/2] coresight: tmc-etf: Fix NULL ptr dereference in tmc_enable_etf_sink_perf() In-Reply-To: <5bbb2d35-3e56-56d7-4722-bf34c5efa2fb@arm.com> References: <5bbb2d35-3e56-56d7-4722-bf34c5efa2fb@arm.com> Message-ID: <7ef182f5ca9ff473c7a643ae3b7a7e75@codeaurora.org> X-Sender: saiprakash.ranjan@codeaurora.org User-Agent: Roundcube Webmail/1.3.9 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201014_035016_201111_089E1E8E X-CRM114-Status: GOOD ( 23.22 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mathieu.poirier@linaro.org, peterz@infradead.org, linux-arm-msm@vger.kernel.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org, swboyd@chromium.org, denik@google.com, leo.yan@linaro.org, linux-arm-kernel@lists.infradead.org, mike.leach@linaro.org Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Suzuki, On 2020-10-13 22:05, Suzuki K Poulose wrote: > On 10/07/2020 02:00 PM, Sai Prakash Ranjan wrote: >> There was a report of NULL pointer dereference in ETF enable >> path for perf CS mode with PID monitoring. It is almost 100% >> reproducible when the process to monitor is something very >> active such as chrome and with ETF as the sink and not ETR. >> Currently in a bid to find the pid, the owner is dereferenced >> via task_pid_nr() call in tmc_enable_etf_sink_perf() and with >> owner being NULL, we get a NULL pointer dereference. >> >> Looking at the ETR and other places in the kernel, ETF and the >> ETB are the only places trying to dereference the task(owner) >> in tmc_enable_etf_sink_perf() which is also called from the >> sched_in path as in the call trace. Owner(task) is NULL even >> in the case of ETR in tmc_enable_etr_sink_perf(), but since we >> cache the PID in alloc_buffer() callback and it is done as part >> of etm_setup_aux() when allocating buffer for ETR sink, we never >> dereference this NULL pointer and we are safe. So lets do the > > The patch is necessary to fix some of the issues. But I feel it is > not complete. Why is it safe earlier and not later ? I believe we are > simply reducing the chances of hitting the issue, by doing this earlier > than > later. I did stress it for a long time with this patch and did not face any issues but I guess it doesn't hurt to have the check as you suggested. > I would say we better fix all instances to make sure that the > event->owner is valid. (e.g, I can see that the for kernel events > event->owner == -1 ?) > > struct task_struct *tsk = READ_ONCE(event->owner); > > if (!tsk || is_kernel_event(event)) > /* skip ? */ > So to confirm my understanding, I will add the above checks on top of this patch for ETR, ETB and ETF something like below? diff --git a/drivers/hwtracing/coresight/coresight-tmc-etf.c b/drivers/hwtracing/coresight/coresight-tmc-etf.c index 989d965f3d90..86ff0dda0444 100644 --- a/drivers/hwtracing/coresight/coresight-tmc-etf.c +++ b/drivers/hwtracing/coresight/coresight-tmc-etf.c @@ -392,6 +392,10 @@ static void *tmc_alloc_etf_buffer(struct coresight_device *csdev, { int node; struct cs_buffers *buf; + struct task_struct *task = READ_ONCE(event->owner); + + if (!task || is_kernel_event(event)) + return NULL; node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu); @@ -400,7 +404,7 @@ static void *tmc_alloc_etf_buffer(struct coresight_device *csdev, if (!buf) return NULL; - buf->pid = task_pid_nr(event->owner); + buf->pid = task_pid_nr(task); buf->snapshot = overwrite; buf->nr_pages = nr_pages; buf->data_pages = pages; Thanks, Sai -- QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel