[PATCHv2 3/4] coresight: etb10: Fix possible NULL ptr dereference in etb_enable_perf()

From: Sai Prakash Ranjan
Date: Thu Oct 22 2020 - 06:58:39 EST


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.

But code path shows that ETB has a similar path as ETF, so
there could be possible NULL pointer dereference crash in
ETB as well. Currently in a bid to find the pid, the owner
is dereferenced via task_pid_nr() call in etb_enable_perf()
and with owner being NULL, we can get a NULL pointer
dereference, so have a similar change as ETF where we cache
PID in alloc_buffer() callback which is called as the part of
etm_setup_aux(). This will reduce the task_pid_nr() function
call overheads as well. In addition to this, add a check to
validate event->owner before dereferencing it to fix any
possible NULL pointer dereference crashes and check for
kernel events.

Fixes: 75d7dbd38824 ("coresight: etb10: Add support for CPU-wide trace scenarios")
Suggested-by: Suzuki K Poulose <suzuki.poulose@xxxxxxx>
Signed-off-by: Sai Prakash Ranjan <saiprakash.ranjan@xxxxxxxxxxxxxx>
---
drivers/hwtracing/coresight/coresight-etb10.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/hwtracing/coresight/coresight-etb10.c b/drivers/hwtracing/coresight/coresight-etb10.c
index 248cc82c838e..9d2f1ab0e29e 100644
--- a/drivers/hwtracing/coresight/coresight-etb10.c
+++ b/drivers/hwtracing/coresight/coresight-etb10.c
@@ -176,6 +176,7 @@ static int etb_enable_perf(struct coresight_device *csdev, void *data)
unsigned long flags;
struct etb_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);

@@ -186,7 +187,7 @@ static int etb_enable_perf(struct coresight_device *csdev, void *data)
}

/* 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;
@@ -376,6 +377,10 @@ static void *etb_alloc_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);

@@ -383,6 +388,7 @@ static void *etb_alloc_buffer(struct coresight_device *csdev,
if (!buf)
return NULL;

+ buf->pid = task_pid_nr(task);
buf->snapshot = overwrite;
buf->nr_pages = nr_pages;
buf->data_pages = pages;
--
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation