mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Serge E. Hallyn" <serge@hallyn.com>
To: Babanpreet Singh <bbnpreetsingh@gmail.com>
Cc: Paul Moore <paul@paul-moore.com>,
	Serge Hallyn <sergeh@kernel.org>,
	Bradley Morgan <brads@mainlining.org>,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	syzbot+ee58b040032d8bed63d9@syzkaller.appspotmail.com,
	syzbot+557fdc985d6ac3831e14@syzkaller.appspotmail.com
Subject: Re: [PATCH v2] cred: annotate data race on non_rcu when copying a cred
Date: Fri, 20 Nov 2026 13:33:32 -0600	[thread overview]
Message-ID: <awCgjAHgzJi5Wn4+@hallyn.com> (raw)
In-Reply-To: <20261114052900.7-1-bbnpreetsingh@gmail.com>

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@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=ee58b040032d8bed63d9
> Reported-by: syzbot+557fdc985d6ac3831e14@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=557fdc985d6ac3831e14
> Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>

Reviewed-by: Serge Hallyn <serge@hallyn.com>

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.

> ---
> Drafted with Claude, an LLM coding assistant, from the syzbot reports.
> 
>  kernel/cred.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/cred.c b/kernel/cred.c
> index d01df40a9a386..d9ab1cb4230a8 100644
> --- a/kernel/cred.c
> +++ b/kernel/cred.c
> @@ -189,7 +189,12 @@ struct cred *prepare_creds(void)
>  	kdebug("prepare_creds() alloc %p", new);
>  
>  	old = task->cred;
> -	memcpy(new, old, sizeof(struct cred));
> +	/*
> +	 * Another task can be in get_cred() on the old cred, clearing
> +	 * non_rcu. That and usage are the only fields that change on a
> +	 * published cred, and both are set fresh below.
> +	 */
> +	data_race(memcpy(new, old, sizeof(struct cred)));
>  
>  	new->non_rcu = 0;
>  	atomic_long_set(&new->usage, 1);
> @@ -573,7 +578,8 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)
>  
>  	old = get_task_cred(daemon);
>  
> -	*new = *old;
> +	/* Same race as in prepare_creds(). */
> +	data_race(memcpy(new, old, sizeof(struct cred)));
>  	new->non_rcu = 0;
>  	atomic_long_set(&new->usage, 1);
>  	get_uid(new->user);
> 
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> -- 
> 2.54.0 (Apple Git-157)

      parent reply	other threads:[~2026-11-20 19:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-14  5:29 Babanpreet Singh
2026-11-14 12:46 ` Bradley Morgan
2026-11-20 19:33 ` Serge E. Hallyn [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awCgjAHgzJi5Wn4+@hallyn.com \
    --to=serge@hallyn.com \
    --cc=bbnpreetsingh@gmail.com \
    --cc=brads@mainlining.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=sergeh@kernel.org \
    --cc=syzbot+557fdc985d6ac3831e14@syzkaller.appspotmail.com \
    --cc=syzbot+ee58b040032d8bed63d9@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®