Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5877594pxu; Wed, 23 Dec 2020 07:38:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwoULuWo/35/MLqeQGh1QBc8vsgOowF5mRTGeMzl9FA5MMMZYj6L/jHyygEqF/d1VbrFuiI X-Received: by 2002:a17:906:b56:: with SMTP id v22mr24491072ejg.145.1608737916037; Wed, 23 Dec 2020 07:38:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608737916; cv=none; d=google.com; s=arc-20160816; b=AKCeMX59f6OWcdQj3PFby8wG58Lfq4qXD8V9J3mJZ/24XDvMG181owljBd+dKHW2Bh FGA0mLV0fgFiGH7H0D1F82z2NbWQjPykBguPTC45o4q19kuRKnKbbxO9wd98MHZs/iWK yRcuJVfXMLrG+CvSssahG7TAQhZUp51sWXw0EOzaMAc/XsZX25d/1DdkPotTldnkT8we e1WEbsvXuxXSLCOmD2iIhKaSd2qE7lAxayp0vuM3fy/B4+Y3NILFYm+PPQu1AJVKfJQ6 LxQ7S6KWNVN9QR7eukQUNWQXEizPDJsYooV4PgaH77+w7nKUyLUzVmN0nptSopqy+9C1 nuaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IhtO+WOnN6zf8zAKVvsJKbn9h9LgpZdEaTHfrGvcC28=; b=zxe3c+9jOJNxCGhR7cS/Yln+wZ4fPXmHOoV3AV8tLrvoTJVtVt3RBr6eNrqex5R1S8 QS+tjCgb0G3Hs+ANXXiQErPBXYNmLR2zKgmuF9nY6v+cYOENlFFPRyiDy2RcfvrXFJZ6 nBvXMJGOhdJ9xah16JY8QyhZbfcpvhwynqVKot7qJ64NV/XJEROpsav0FgFVzh4m65fb mhHjd4LlqZ4pg6MoFH7XBsZ26nLw6Q+gHgP1cRZj+Fc8E949CIpQI7z0KrJP+59TVvmO FTprhIeDjF0JeYcXNy3UL4uRyqP4XS+y2BrGUQTiTAhGbtXCW4FFL2lcsasfTptd8g0Z /jZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LQTfr7Vt; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pw18si11901775ejb.163.2020.12.23.07.38.13; Wed, 23 Dec 2020 07:38:36 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=LQTfr7Vt; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728315AbgLWPeD (ORCPT + 99 others); Wed, 23 Dec 2020 10:34:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:44718 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728264AbgLWPeA (ORCPT ); Wed, 23 Dec 2020 10:34:00 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5019F23359; Wed, 23 Dec 2020 15:32:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1608737577; bh=qrSCJ4ABI+TtwQhR1J3g+DK6JBfJxLoqz1ZQ6N9mA7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LQTfr7VtWk9MNw8q2RthzJ6CHgrg+SiwVKp+/UAaDrpt+s+T/2gbMImCx/DgoKt+5 GW7SF6JkMaYZSyD3i3zvSgkGwbaS53ddnby3pPe+YJU60YCidtJ+PkYji3l6xvQLZv 59rv4wz/mlTgfHwTZ8I8hXjgqFaJfjoY6LaatlZ0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sai Prakash Ranjan , Mathieu Poirier Subject: [PATCH 5.10 15/40] coresight: tmc-etf: Fix NULL ptr dereference in tmc_enable_etf_sink_perf() Date: Wed, 23 Dec 2020 16:33:16 +0100 Message-Id: <20201223150516.292233870@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201223150515.553836647@linuxfoundation.org> References: <20201223150515.553836647@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Prakash Ranjan commit 868663dd5d69fef05bfb004f91da5c30e9b93461 upstream. 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 same thing with ETF and cache the PID to which the cs_buffer belongs in tmc_alloc_etf_buffer() as done for ETR. This will also remove the unnecessary function calls(task_pid_nr()) since we are caching the PID. Easily reproducible running below: perf record -e cs_etm/@tmc_etf0/ -N -p Unable to handle kernel NULL pointer dereference at virtual address 0000000000000548 Mem abort info: ESR = 0x96000006 EC = 0x25: DABT (current EL), IL = 32 bits SET = 0, FnV = 0 EA = 0, S1PTW = 0 Data abort info: ISV = 0, ISS = 0x00000006 CM = 0, WnR = 0 ... Call trace: tmc_enable_etf_sink+0xe4/0x280 coresight_enable_path+0x168/0x1fc etm_event_start+0x8c/0xf8 etm_event_add+0x38/0x54 event_sched_in+0x194/0x2ac group_sched_in+0x54/0x12c flexible_sched_in+0xd8/0x120 visit_groups_merge+0x100/0x16c ctx_flexible_sched_in+0x50/0x74 ctx_sched_in+0xa4/0xa8 perf_event_sched_in+0x60/0x6c perf_event_context_sched_in+0x98/0xe0 __perf_event_task_sched_in+0x5c/0xd8 finish_task_switch+0x184/0x1cc schedule_tail+0x20/0xec ret_from_fork+0x4/0x18 Fixes: 880af782c6e8 ("coresight: tmc-etf: Add support for CPU-wide trace scenarios") Cc: stable@vger.kernel.org Signed-off-by: Sai Prakash Ranjan Signed-off-by: Mathieu Poirier Link: https://lore.kernel.org/r/20201127175256.1092685-10-mathieu.poirier@linaro.org Signed-off-by: Greg Kroah-Hartman --- drivers/hwtracing/coresight/coresight-priv.h | 2 ++ drivers/hwtracing/coresight/coresight-tmc-etf.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) --- a/drivers/hwtracing/coresight/coresight-priv.h +++ b/drivers/hwtracing/coresight/coresight-priv.h @@ -87,6 +87,7 @@ enum cs_mode { * struct cs_buffer - keep track of a recording session' specifics * @cur: index of the current buffer * @nr_pages: max number of pages granted to us + * @pid: PID this cs_buffer belongs to * @offset: offset within the current buffer * @data_size: how much we collected in this run * @snapshot: is this run in snapshot mode @@ -95,6 +96,7 @@ enum cs_mode { struct cs_buffers { unsigned int cur; unsigned int nr_pages; + pid_t pid; unsigned long offset; local_t data_size; bool snapshot; --- a/drivers/hwtracing/coresight/coresight-tmc-etf.c +++ b/drivers/hwtracing/coresight/coresight-tmc-etf.c @@ -227,6 +227,7 @@ static int tmc_enable_etf_sink_perf(stru unsigned long flags; struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); struct perf_output_handle *handle = data; + struct cs_buffers *buf = etm_perf_sink_config(handle); spin_lock_irqsave(&drvdata->spinlock, flags); do { @@ -243,7 +244,7 @@ static int tmc_enable_etf_sink_perf(stru } /* Get a handle on the pid of the process to monitor */ - pid = task_pid_nr(handle->event->owner); + pid = buf->pid; if (drvdata->pid != -1 && drvdata->pid != pid) { ret = -EBUSY; @@ -399,6 +400,7 @@ static void *tmc_alloc_etf_buffer(struct if (!buf) return NULL; + buf->pid = task_pid_nr(event->owner); buf->snapshot = overwrite; buf->nr_pages = nr_pages; buf->data_pages = pages;