From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179177; b=xrm7ip//WZCvhK4TYmBP2NQgpxpHv4GL9h08ZwObdVylEPf7E74b38OeoCoPKTw6ogNl ThC4bggImDgZSVCCcwwBPFnqa5fko4PKVSwPou44XvrXG0lf+AsXyQGKq8qSOeS/AEepF vQzTgwQd9GDpSZsqomXdbPXuOJzZySEiot1D/KfRIYHgvPta3fuEsm6kmkESef7JlmW4+ AhqvWu0yRk7qtUt5mNuo0KI921EuabDuvUUKpLQW1ANrejjWQDIgQDDEF1NfoMgpBmTa9 9FrtO1QBDEnsdvXFGecLy9aqPAerisFsuMAX8xf24zL8goanMKQjsG4cEuDG8bTIazQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179177; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=vGR2T8bmQigorWaWi0H/3t/L8Ok3QHm5tkAKSaLx8/0=; b=oWoP16T+zW3T7jlLg8IIbHtXIrf22G9IBCBs/8X1jqeYMUe2GsSAhln+ImFl4oH4v7bY J9ky6gdotV8EGfnP+pBZwKnEVLNoQJLJrGAqy4IKD9BbCS7iQUFhoGoKaJPOORYzxuUHe Ll28p8hPm96EjSDxw9HpB0ING+IXPB3AJU7+SxtXTMa/9s/WCu/Ah5nxLKeQg+Ti6I6v8 +N66a8BrvX6aeUNW4bGIfqfZDCFNuiiy6/dJuErUjFVU6Vh/wdVlc7zP2itGpzqEtQiGl uPfh0ll8VvuOH26UiJaLaFk1hGcOQN21XvqazIq1W5QKhAS/HCqb3dJ5qN2UqV3kJWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="bGXbJq/8"; arc=none smtp.remote-ip=209.85.214.200 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C723B606CCF0 for ; Wed, 2 Dec 2026 02:39:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C723B606CCF0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=bGXbJq/8 Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2f03a137f6aso106875165ad.3 for ; Tue, 01 Dec 2026 18:39:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179176; x=1796783976; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=vGR2T8bmQigorWaWi0H/3t/L8Ok3QHm5tkAKSaLx8/0=; b=bGXbJq/85xAGZ6xBcj/xI85G3M/MgfSYHGmhqkLlaTtThNCtB1vqJAXmJdGgzo2yy8 eupk88cUD2w6hpq1dlT3zv1QOFtagG2nGZuk8FXkBwLhV8ta0wkap6hl1N7vzMnrXUXa aoZ0m17v96qYmFduqx+XZKWwj0L9OkHK5IF4mi/+LrjbaGwkpf+G2PWrilzQeQ40YZ/a szJm7yRb+DSUX/V0chTzhI2tl+NlZnbNF9ZB7j9hZmTE8z8d3TsemFYbqC9D7l/aDYIX BC8XfN+Gf/pAGcRu8OJ4Hd1EX1IVwWoshw/MZeh7NBpBYdGks5ztP2iAr9ZIC32MQKjm KWZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179176; x=1796783976; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vGR2T8bmQigorWaWi0H/3t/L8Ok3QHm5tkAKSaLx8/0=; b=bzqnPvYI26Cy4nhfPRSd8aK+tBVZ21hgi8dSF3vtN2HOdk03LznqDkT/kC53JSYszt NAGW7O9hyYlQ9jLE6oIRoCbq/dXipiam6wJ0rmO+vHyglDb/dNaD140SFgCy/NLQ3lbI Eo2+oJPZusBmrKCe2eLvMXpGbhf20x7Ut/8CLqurErw1QVHvlw9jWnKHcjwlH0jR9/rb xjAqi5Lm0Suq2xaQsidu1qLywuwqk5mx9mwuis7MiCUkg/jDQN671AiQdyCxEjFXTn6r hIU/iV5YCPtY19lebuzaxLwslNw8xjKrK5msWgTTS8bEya9aFvHXgoLjwqHsjCIPB56b V6eA== X-Forwarded-Encrypted: i=1; AL+hErHzjSPDl9n2+5i5imyWfPQVMEYIPjEpr01Hi7033sWL9D0INYlA2jT1U6ac6mi01e6XauV7p7mybNFzUA==@vger.kernel.org X-Gm-Message-State: APuU4Z+f6nihpmh980E2TJgfBRrX5Vne7hQFA2O60d/pdY/Qte8s7POp oL+O6M8UYbgf4h6+aVFIb9XlIMVzcO8nRf/HPeIXubDGXh0FMlPWapau2XTn8j6X0EtqQMmNgMB ox2LhhB2cQVVhViKin7P8FA== X-Received: from plpw13.prod.google.com ([2002:a17:902:9a8d:b0:2f1:6dde:ca73]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2b0f:b0:2ef:d9b8:463 with SMTP id d9443c01a7336-2f03f186250mr409981955ad.35.1796179175681; Tue, 01 Dec 2026 18:39:35 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:14 +0000 In-Reply-To: <20261202023920.595207-1-jthoughton@google.com> X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202023920.595207-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202023920.595207-8-jthoughton@google.com> Subject: [PATCH v2 07/12] tracing: Don't read user memory without an mm From: James Houghton To: Andrew Morton , Alexander Viro , Arnd Bergmann Cc: Sean Christopherson , Paolo Bonzini , Jim Mattson , Jens Axboe , David Hildenbrand , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Madhavan Srinivasan , Chris Zankel , Max Filippov , Dinh Nguyen , Simon Schuster , David Sterba , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , KP Singh , Matt Bobrowski , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Steven Rostedt , Masami Hiramatsu , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, io-uring@vger.kernel.org, James Houghton Content-Type: text/plain; charset="UTF-8" Probe events (kprobe, eprobe and fprobe events) fetching user memory, event filters on user space strings (filter_pred_pchar_user()) and synthetic events with user space string fields can fire in any context, including kernel threads and tasks that have already gone through exit_mm(). There is no user memory to access there, so these accesses should simply fail. They currently call the user nofault helpers regardless. Without an mm, those don't reliably fail: the user half of the address space belongs to whatever mm happens to be lazily loaded, if any, so the event may record another task's memory. Check for a NULL current->mm before these accesses, and fail as if the access had faulted. A NULL mm here isn't a kernel bug, as these events don't choose the context they fire in, so this also keeps them from triggering the warning that a later patch in this series adds to the uaccess helpers. Each of these has read user memory without checking for an mm since it was added: user memory fetches in probe events in commit e65f7ae7f4da ("tracing/probe: Support user-space dereference") and commit 88903c464321 ("tracing/probe: Add ustring type for user-space string"), user space strings in event filters in commit 77360f9bbc7e ("tracing: Add test for user space strings when filtering on string pointers"), and user space strings in synthetic events in commit 0934ae9977c2 ("tracing: Fix reading strings from synthetic events"). Fixes: e65f7ae7f4da ("tracing/probe: Support user-space dereference") Fixes: 88903c464321 ("tracing/probe: Add ustring type for user-space string") Fixes: 77360f9bbc7e ("tracing: Add test for user space strings when filtering on string pointers") Fixes: 0934ae9977c2 ("tracing: Fix reading strings from synthetic events") Signed-off-by: James Houghton --- kernel/trace/trace_events_filter.c | 8 ++++++++ kernel/trace/trace_events_synth.c | 15 ++++++++++++--- kernel/trace/trace_probe_kernel.h | 16 +++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c index b3ac2519a62d..2161c78e0223 100644 --- a/kernel/trace/trace_events_filter.c +++ b/kernel/trace/trace_events_filter.c @@ -832,6 +832,14 @@ static __always_inline char *test_ustring(char *str) ubuf = this_cpu_ptr(ustring_per_cpu); kstr = ubuf->buffer; + /* + * Events may fire in any context, including kernel threads and exiting + * tasks, which have no user memory to read. That's not a kernel bug, + * so fail before the user copy helpers warn about it. + */ + if (unlikely(!current->mm)) + return NULL; + /* user space address? */ ustr = (char __user *)str; if (strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE) < 0) diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c index e2870212e58d..d40d0c30db54 100644 --- a/kernel/trace/trace_events_synth.c +++ b/kernel/trace/trace_events_synth.c @@ -449,9 +449,18 @@ static unsigned int trace_string(struct synth_trace_event *entry, str_field = (char *)&entry->fields[*n_u64].as_u64; #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE - if ((unsigned long)str_val < TASK_SIZE) - ret = strncpy_from_user_nofault(str_field, (const void __user *)str_val, STR_VAR_LEN_MAX); - else + if ((unsigned long)str_val < TASK_SIZE) { + /* + * Synthetic events may fire in any context, including + * kernel threads, which have no user memory to read. + */ + if (unlikely(!current->mm)) + ret = -EFAULT; + else + ret = strncpy_from_user_nofault(str_field, + (const void __user *)str_val, + STR_VAR_LEN_MAX); + } else #endif ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX); diff --git a/kernel/trace/trace_probe_kernel.h b/kernel/trace/trace_probe_kernel.h index 4eb4acfa09ba..b8ff2a275f92 100644 --- a/kernel/trace/trace_probe_kernel.h +++ b/kernel/trace/trace_probe_kernel.h @@ -14,6 +14,13 @@ fetch_store_strlen_user(unsigned long addr) { const void __user *uaddr = (__force const void __user *)addr; + /* + * Probes may fire in any context, including kernel threads and exiting + * tasks, which have no user memory to read. That's not a kernel bug, + * so fail (like a fault would) before the user copy helpers warn. + */ + if (unlikely(!current->mm)) + return 0; return strnlen_user_nofault(uaddr, MAX_STRING_SIZE); } @@ -61,7 +68,11 @@ fetch_store_string_user(unsigned long addr, void *dest, void *base) __dest = get_loc_data(dest, base); - ret = strncpy_from_user_nofault(__dest, uaddr, maxlen); + /* No user memory without an mm, see fetch_store_strlen_user(). */ + if (unlikely(!current->mm)) + ret = -EFAULT; + else + ret = strncpy_from_user_nofault(__dest, uaddr, maxlen); set_data_loc(ret, dest, __dest, base); return ret; @@ -103,6 +114,9 @@ probe_mem_read_user(void *dest, void *src, size_t size) { const void __user *uaddr = (__force const void __user *)src; + /* No user memory without an mm, see fetch_store_strlen_user(). */ + if (unlikely(!current->mm)) + return -EFAULT; return copy_from_user_nofault(dest, uaddr, size); } -- 2.56.1.911.g883d8bcb20-goog