Re: [PATCH v2 07/12] tracing: Don't read user memory without an mm

From: James Houghton

Date: Wed Dec 02 2026 - 13:14:58 EST


On Wed, Dec 2, 2026 at 12:29 PM David Laight
<david.laight.linux@xxxxxxxxx> wrote:
>
> On Wed, 2 Dec 2026 08:32:59 -0500
> Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
>
> > On Wed, 2 Dec 2026 02:39:14 +0000
> > James Houghton <jthoughton@xxxxxxxxxx> wrote:
> >
> > > #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
> >
> > Instead of adding all these crazy checks, and then we need to play
> > whack-a-mole when we add a new one that triggers the warning, can't you add
> > a helper function for this?
> >
> > strncpy_from_user_trace_nofault()?
> >
> > Where it can be used by places that do not care if there's an current->mm or not.
> >
> > If you are reading user space for tracing purposes, it may be done from a
> > kernel thread too, and that's not a bug. It should simply return -EFAULT.
>
> Is there anywhere where it is valid to call strncpy_from_user_nofault()
> when current->mm is NULL?

Yes, tracing is one such case. BPF is another, see the previous patch[1].

> So the check could be moved inside.

It can be moved inside; I'll do that in v3 (same as v1). In this case,
it will reject the uaccess, but it *won't* WARN_ON_ONCE() given the
legitimate callers.

> Unlike access_ok() (and friends) this is a slow function and no one will
> notice the extra check.

Indeed.

> Thinks more...
>
> I'd also have thought that kernel threads won't have anything mapped for
> user addresses, so the access will fault, the fault handler will see the
> flag that 'nofault' sets (or itself check current->mm) and return -EFAULT
> instead of trying to fault-in the missing page.
> So the extra check only affects threads that have mapped user addresses but
> don't have current->mm.

With lazy TLB, kernel threads *do* have valid user page tables. The
user page tables that are present are those of the last process that
ran on the core. We can't really get rid of lazy TLB (evaluated
somewhat in the cover letter[2]), so we have to deal with the fact
that these accesses are not guaranteed to fault.

[1] https://lore.kernel.org/kvm/20261202023920.595207-7-jthoughton@xxxxxxxxxx/
[2] https://lore.kernel.org/linux-mm/20261202023920.595207-1-jthoughton@xxxxxxxxxx/
Please see the "For comparison, I also measured making the x86 lazy
TLB mode eager" bit

Thanks for all your feedback, David!