Skip to contentSkip to conversation

cred: annotate data race on non_rcu when copying a cred

PATCH In review Babanpreet Singh 5 participants 5 messages Nov 14 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cred: annotate data race on non_rcu when copying a cred

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.

---
Drafted with Claude, an LLM coding assistant, from the syzbot reports.

 kernel/cred.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cred.c +8 -2
index d01df40a9a386..d9ab1cb4230a8 100644
@@ -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

Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 17 lines

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.

Quoted 8 lines

Annotate the copy with data_race(). prepare_kernel_cred() makes the same copy, so annotate it too and switch it to memcpy() to match.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Babanpreet Singh
Quoted 13 lines · Babanpreet Singh

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

Quoted 6 lines

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.

Yes yes, good now.

Quoted 41 lines

---
Drafted with Claude, an LLM coding assistant, from the syzbot reports.

kernel/cred.c | 10 ++++++++--

1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cred.c +8 -2
index d01df40a9a386..d9ab1cb4230a8 100644
@@ -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);
Quoted 2 lines

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

--- Thanks!

maintainer Reviewed-by on v2 1/1
#3 lore raw
In reply to Babanpreet Singh
Quoted 17 lines · Babanpreet Singh

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.

Quoted 8 lines

Annotate the copy with data_race(). prepare_kernel_cred() makes the same copy, so annotate it too and switch it to memcpy() to match.

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.

Quoted 38 lines

---
Drafted with Claude, an LLM coding assistant, from the syzbot reports.

 kernel/cred.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cred.c +8 -2
index d01df40a9a386..d9ab1cb4230a8 100644
@@ -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

Signature · 1 line
2.54.0 (Apple Git-157)
maintainer
#4 lore raw
Quoted 31 lines · Serge E. Hallyn
Quoted 17 lines · Babanpreet Singh

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.

Quoted 8 lines

Annotate the copy with data_race(). prepare_kernel_cred() makes the same copy, so annotate it too and switch it to memcpy() to match.

At the moment this seems correct.

Quoted 8 lines

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.

Signature · 1 line
paul-moore.com