Re: [PATCH v2] cred: annotate data race on non_rcu when copying a cred

From: Paul Moore

Date: Fri Nov 20 2026 - 17:38:13 EST


On Fri, Nov 20, 2026 at 2:33 PM Serge E. Hallyn <serge@xxxxxxxxxx> wrote:
> On Sat, Nov 14, 2026 at 05:29:00AM +0000, Babanpreet Singh wrote:
> > prepare_creds() copies the current cred while another task can be in
> > get_cred() on it, clearing non_rcu. KCSAN reports:
> >
> > BUG: KCSAN: data-race in io_prep_async_work / prepare_creds
> >
> > write to 0xffff8881046cea68 of 4 bytes by task 3646 on cpu 1:
> > get_cred_many include/linux/cred.h:222 [inline]
> > get_cred include/linux/cred.h:238 [inline]
> > io_prep_async_work+0x9b/0x3c0 io_uring/io_uring.c:367
> >
> > read to 0xffff8881046ce9c0 of 184 bytes by task 3645 on cpu 0:
> > prepare_creds+0x71/0x550 kernel/cred.c:192
> >
> > The race is harmless. non_rcu and usage are the only fields that change
> > on a published cred, and both are reset on the new cred right after the
> > copy.
> >
> > Annotate the copy with data_race(). prepare_kernel_cred() makes the
> > same copy, so annotate it too and switch it to memcpy() to match.
> >
> > Reported-by: syzbot+ee58b040032d8bed63d9@xxxxxxxxxxxxxxxxxxxxxxxxx
> > Closes: https://syzkaller.appspot.com/bug?extid=ee58b040032d8bed63d9
> > Reported-by: syzbot+557fdc985d6ac3831e14@xxxxxxxxxxxxxxxxxxxxxxxxx
> > Closes: https://syzkaller.appspot.com/bug?extid=557fdc985d6ac3831e14
> > Signed-off-by: Babanpreet Singh <bbnpreetsingh@xxxxxxxxx>
>
> Reviewed-by: Serge Hallyn <serge@xxxxxxxxxx>
>
> At the moment this seems correct.
>
> I do worry that if struct cred changes/evolves, then this solution
> may mask newly introduced regressions. But I don't have a better
> idea. I mean, I guess we could keep usage at front and nonrcu at
> back, and memcpy just the parts in between... In a clearly
> named new helper function that wouldn't be so bad, e.g.
>
> static inline void cred_memcpy(new, old), and let cred_mempy do
> the offset and size calculation.

The __randomize_layout annotation will likely make that tricky.

Alternatively we could have a private dup_creds() function which would
copy over the individual non-pointer fields through direct assignment
as opposed to a memcpy(), skipping the rcu and usage fields of course.
It's in that awkward number of fields where it is somewhat practical,
but it would be fairly ugly. I do wonder what the difference would be
from a performance / compiled-code perspective; if it is slower it is
probably a non-starter.

--
paul-moore.com