From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179177; b=r7G6kHddSENzPDS8/dx1kUVmOf5zxR1TvaddQZV8znfwBu1jvgufl8Wu1TKKQqefPIm9 wqhP7KJyKqIsST5WYBLAfuPAVgEPEfmWKHjto9lzuFu2Cjyxewlo09xPni/+q2H/A84f0 u9Mf3iJw7yPGv7FnHlIpKinocN1m8zXy5NLZ7cpkfrqJ7b47TlEre7IUu+7qRpHDX6O3z yjfg4QuovdwH9a+GK3FJumMf5nr4nB4YWPg2wwYKIjqmgKkpTbsLaAFVzPuj+POp00a8W LNxTAMHD9OM7WGJXUItIcBBFXFJx3vXKQceKoFWBnq6G/bFFGsJNAZ4lDlocs7p+kbA== 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=0hZbDHVDpilYfKeiEJnFKEqewbtaNC4clHhQzu4akNUZ2YB1NotoK4nSJdATK2ufe1LR R2AgL0mOyJH/7HBd19vM7J1U3+r/Mab+h2vvblYZ40iwhPpxJ+VJl4Y8Nk5uyRL5gIzIl 10n0xKMExjSRWhmGxXcXtsY8xTXK3qio8eNYlbN2JlzNJGNdFRgg/uXhMuNFjC0IClXHi dFtfxgm3RG4uuxuIvlgMGhkrsz0C15OIsiZq8VrwSnIqrX8AsmCNiQtE1sGnPDobY+ked KdLr1w8xl29eK5D537MZZKIVP2XB1VqreFK7iVx/f5GF5HCNLG94fqgvO1sazrFzhCg== 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.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (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 BA227606CCEB for ; Wed, 2 Dec 2026 02:39:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BA227606CCEB 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-f199.google.com with SMTP id d9443c01a7336-2f074b87307so89586685ad.2 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=OwmPdpkSOaahevAzOKcXjSFtFdwCJx8AIJwvtYnhyqTVsUUyW82IA95NxXb4tCWj4/ RJ3V31z5MZ8Shf9UISmpfA2xRhpBOncK7ExL1YD1r1F+ICfw43MFQ9Bg/m01whLKx6R7 +qDBgn0VHXSzHSvahSgEhGq9YVkyXVrmSAF9eypMB5jOjvDPHbvUoN5vWCISrFbXlPtJ /edoHmf28p+66natzfqf5EKd9W5dKxyDRJsORe89wDk7Fjkl5xW2JCnfACtSvN/QtQKL PEOdNXQu+4UW7BqCVWhBYcHAl2zxQuG6bwbDyoU+HllQcZRsAjb1jPeKxdur48I9PnRb 8Gyg== X-Forwarded-Encrypted: i=1; AL+hErGBk0R4EuxfmlGxD3SNoOQdpzOrxpp5f1nUihkMGKxaqcaU32PrjN87f0XzLQH89PIS3cMYpxCcs9TZK+QR@vger.kernel.org X-Gm-Message-State: APuU4Z8ZeEwYR9b3JcGaLlGxDaFlfhlneJj9jLN8J3Dn0RJyHX4WlIDA g0xZyXzKBN3B7T32BpR1I/V/ZnlJ9F24d60rN5GSt4VEQBO7QGz4wCNbLKte+KrqtgWttepDHXq sBQNyuDZ5Ep2+DgjsVK+2eQ== 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-fsdevel@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