Skip to contentSkip to conversation

audit: annotate the lockless read of the audit control lock owner

PATCH Superseded Babanpreet Singh 4 participants 8 messages Oct 2 – Nov 3 Audit

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 audit: annotate the lockless read of the audit control lock owner

audit_ctl_owner_current() reads the lock owner without taking the lock and KCSAN complains about it. Only the current task can store itself as owner, so the check can't give a wrong answer. Mark the accesses with READ_ONCE() and WRITE_ONCE().

---
Paul, you looked at this race in 2022 and asked whether READ_ONCE() was worth it and what it would cost:
https://lore.kernel.org/all/CAHC9VhTXNPWBDRoPcz-Jw=f+NNAEhxbh-ySc56CUd-ZbuboW5w@mail.gmail.com/
Compile tested only (gcc and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.

 kernel/audit.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/audit.c +8 -3
index 631d6d4a23cfd..f8c43ac6d5b82 100644
@@ -247,7 +247,7 @@ int auditd_test_task(struct task_struct *task)
 void audit_ctl_lock(void)
 {
 	mutex_lock(&audit_cmd_mutex.lock);
-	audit_cmd_mutex.owner = current;
+	WRITE_ONCE(audit_cmd_mutex.owner, current);
 }
 
 /**
@@ -255,7 +255,7 @@ void audit_ctl_lock(void)
  */
 void audit_ctl_unlock(void)
 {
-	audit_cmd_mutex.owner = NULL;
+	WRITE_ONCE(audit_cmd_mutex.owner, NULL);
 	mutex_unlock(&audit_cmd_mutex.lock);
 }
 
@@ -268,7 +268,12 @@ void audit_ctl_unlock(void)
  */
 static bool audit_ctl_owner_current(void)
 {
-	return (current == audit_cmd_mutex.owner);
+	/*
+	 * Lockless read: the owner can only equal current if current set
+	 * it, so another task changing the owner concurrently can never
+	 * make this return the wrong answer.
+	 */
+	return (current == READ_ONCE(audit_cmd_mutex.owner));
 }
 
 /**

base-commit: a8bdcf944504980635c2069b4a4cfcf677b09bbb

Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

audit_ctl_owner_current() reads the lock owner without taking the lock and KCSAN complains about it. Only the current task can store itself as owner, so the check can't give a wrong answer. Mark the accesses with READ_ONCE() and WRITE_ONCE().

Sashiko has reviewed this patch and found no issues. It looks great!

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

audit_ctl_owner_current() reads the lock owner without taking the lock and KCSAN complains about it. Only the current task can store itself as owner, so the check can't give a wrong answer. Mark the accesses with READ_ONCE() and WRITE_ONCE().

---
Paul, you looked at this race in 2022 and asked whether READ_ONCE() was worth it and what it would cost:
https://lore.kernel.org/all/CAHC9VhTXNPWBDRoPcz-Jw=f+NNAEhxbh-ySc56CUd-ZbuboW5w@mail.gmail.com/
Compile tested only (gcc and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.

Quoted 2 lines
 kernel/audit.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Thanks for the patch.

I stand by my previous comments, but given the current state of LLM assisted reporting and submissions it's probably worth making the change. I would like to see one small change, see below ...

Quoted 5 lines
kernel/audit.c +0 -0
index 631d6d4a23cfd..f8c43ac6d5b82 100644

kernel/audit.c void audit_ctl_unlock(void) · line 275

Quoted 9 lines
kernel/audit.c +5 -1
@@ -268,7 +268,12 @@ void audit_ctl_unlock(void)
  */
 static bool audit_ctl_owner_current(void)
 {
-       return (current == audit_cmd_mutex.owner);
+       /*
+        * Lockless read: the owner can only equal current if current set
+        * it, so another task changing the owner concurrently can never
+        * make this return the wrong answer.
+        */

This code is fairly easy to understand so I don't believe this comment is necessary or adds any value, please drop this commment and resubmit.

Quoted 2 lines
kernel/audit.c +1 -0
+       return (current == READ_ONCE(audit_cmd_mutex.owner));
 }
Signature · 1 line
paul-moore.com
#3 lore raw

PATCH v2 1/1 audit: annotate the lockless read of the audit control lock owner

audit_ctl_owner_current() reads the lock owner without taking the lock and KCSAN complains about it. Only the current task can store itself as owner, so the check can't give a wrong answer. Mark the accesses with READ_ONCE() and WRITE_ONCE().

---
v2: drop the comment in audit_ctl_owner_current(). The KCSAN build of audit.o disassembles the same as v1.
v1: https://lore.kernel.org/all/[email protected]/
Compile tested only (gcc W=1 and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.

 kernel/audit.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/audit.c +3 -3
index 631d6d4a23cfd..d0c2036bde759 100644
@@ -247,7 +247,7 @@ int auditd_test_task(struct task_struct *task)
 void audit_ctl_lock(void)
 {
 	mutex_lock(&audit_cmd_mutex.lock);
-	audit_cmd_mutex.owner = current;
+	WRITE_ONCE(audit_cmd_mutex.owner, current);
 }
 
 /**
@@ -255,7 +255,7 @@ void audit_ctl_lock(void)
  */
 void audit_ctl_unlock(void)
 {
-	audit_cmd_mutex.owner = NULL;
+	WRITE_ONCE(audit_cmd_mutex.owner, NULL);
 	mutex_unlock(&audit_cmd_mutex.lock);
 }
 
@@ -268,7 +268,7 @@ void audit_ctl_unlock(void)
  */
 static bool audit_ctl_owner_current(void)
 {
-	return (current == audit_cmd_mutex.owner);
+	return (current == READ_ONCE(audit_cmd_mutex.owner));
 }
 
 /**

base-commit: a8bdcf944504980635c2069b4a4cfcf677b09bbb

Signature · 1 line
2.43.0
#4 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

audit_ctl_owner_current() reads the lock owner without taking the lock and KCSAN complains about it. Only the current task can store itself as owner, so the check can't give a wrong answer. Mark the accesses with READ_ONCE() and WRITE_ONCE().

Sashiko has reviewed this patch and found no issues. It looks great!

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

audit_ctl_owner_current() reads the lock owner without taking the lock and KCSAN complains about it. Only the current task can store itself as owner, so the check can't give a wrong answer. Mark the accesses with READ_ONCE() and WRITE_ONCE().

Reported-by [email protected]
Assisted-by Claude:claude-opus-5-5
Quoted 34 lines

---
v2: drop the comment in audit_ctl_owner_current(). The KCSAN build of audit.o disassembles the same as v1.
v1: https://lore.kernel.org/all/[email protected]/
Compile tested only (gcc W=1 and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.

kernel/audit.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
kernel/audit.c +2 -2
index 631d6d4a23cfd..d0c2036bde759 100644
@@ -247,7 +247,7 @@ int auditd_test_task(struct task_struct *task)
 void audit_ctl_lock(void)
 {
 	mutex_lock(&audit_cmd_mutex.lock);
-	audit_cmd_mutex.owner = current;
+	WRITE_ONCE(audit_cmd_mutex.owner, current);
 }
 
 /**
@@ -255,7 +255,7 @@ void audit_ctl_lock(void)
 */
 void audit_ctl_unlock(void)
 {
-	audit_cmd_mutex.owner = NULL;
+	WRITE_ONCE(audit_cmd_mutex.owner, NULL);
 	mutex_unlock(&audit_cmd_mutex.lock);
 }
 

kernel/audit.c void audit_ctl_unlock(void) · line 274

Quoted 8 lines
kernel/audit.c +1 -1
@@ -268,7 +268,7 @@ void audit_ctl_unlock(void)
 */
 static bool audit_ctl_owner_current(void)
 {
-	return (current == audit_cmd_mutex.owner);
+	return (current == READ_ONCE(audit_cmd_mutex.owner));
 }
 
 /**

Clean!

Quoted 3 lines

base-commit: a8bdcf944504980635c2069b4a4cfcf677b09bbb

--- Thanks!
"I'm not a very positive person" - Linus torvalds

#6 lore raw
In reply to Babanpreet Singh

Hi Paul,

Gentle ping on this v2. It has been three weeks, Bradley gave it a Reviewed-by and Tested-by, and I don't see it in audit/dev or audit/next yet. If you want something else changed, tell me and I will respin.

Thanks,
Babanpreet

maintainer Applied
#7 lore raw
In reply to Babanpreet Singh
Quoted 19 lines · On Oct 7, 2026 Babanpreet Singh <[email protected]> wrote:

audit_ctl_owner_current() reads the lock owner without taking the lock and KCSAN complains about it. Only the current task can store itself as owner, so the check can't give a wrong answer. Mark the accesses with READ_ONCE() and WRITE_ONCE().

---
v2: drop the comment in audit_ctl_owner_current(). The KCSAN build of audit.o disassembles the same as v1.
v1: https://lore.kernel.org/all/[email protected]/
Compile tested only (gcc W=1 and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.

Quoted 2 lines
 kernel/audit.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Merged into audit/dev, thanks!

--
paul-moore.com