Skip to contentSkip to conversation

pidfs: detect refcount bugs

PATCH In review Christian Brauner 3 participants 4 messages May 6, 2025 – May 9, 2025 VFS

lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

PATCH 1/1 pidfs: detect refcount bugs

Now that we have pidfs_{get,register}_pid() that needs to be paired with pidfs_put_pid() it's possible that someone pairs them with put_pid(). Thus freeing struct pid while it's still used by pidfs. Notice when that happens. I'll also add a scheme to detect invalid uses of pidfs_get_pid() and pidfs_put_pid() later.

---
 kernel/pid.c | 1 +
 1 file changed, 1 insertion(+)
kernel/pid.c +1 -0
index 26f1e136f017..8317bcbc7cf7 100644
@@ -100,6 +100,7 @@ void put_pid(struct pid *pid)
 
 	ns = pid->numbers[pid->level].ns;
 	if (refcount_dec_and_test(&pid->count)) {
+		WARN_ON_ONCE(pid->stashed);
 		kmem_cache_free(ns->pid_cachep, pid);
 		put_pid_ns(ns);
 	}
Signature · 1 line
2.47.2
#1 lore raw

I am traveling until May 15, can't read the code, but FWIW this change looks good to me.

Oleg.

Quoted 27 lines · Christian Brauner

Now that we have pidfs_{get,register}_pid() that needs to be paired with pidfs_put_pid() it's possible that someone pairs them with put_pid(). Thus freeing struct pid while it's still used by pidfs. Notice when that happens. I'll also add a scheme to detect invalid uses of pidfs_get_pid() and pidfs_put_pid() later.

---
 kernel/pid.c | 1 +
 1 file changed, 1 insertion(+)
kernel/pid.c +1 -0
index 26f1e136f017..8317bcbc7cf7 100644
@@ -100,6 +100,7 @@ void put_pid(struct pid *pid)
 
 	ns = pid->numbers[pid->level].ns;
 	if (refcount_dec_and_test(&pid->count)) {
+		WARN_ON_ONCE(pid->stashed);
 		kmem_cache_free(ns->pid_cachep, pid);
 		put_pid_ns(ns);
 	}
Signature · 1 line
2.47.2
#2 lore raw
In reply to Christian Brauner
Quoted 26 lines · Christian Brauner

Now that we have pidfs_{get,register}_pid() that needs to be paired with pidfs_put_pid() it's possible that someone pairs them with put_pid(). Thus freeing struct pid while it's still used by pidfs. Notice when that happens. I'll also add a scheme to detect invalid uses of pidfs_get_pid() and pidfs_put_pid() later.

---
 kernel/pid.c | 1 +
 1 file changed, 1 insertion(+)
kernel/pid.c +1 -0
index 26f1e136f017..8317bcbc7cf7 100644
@@ -100,6 +100,7 @@ void put_pid(struct pid *pid)
 
 	ns = pid->numbers[pid->level].ns;
 	if (refcount_dec_and_test(&pid->count)) {
+		WARN_ON_ONCE(pid->stashed);
 		kmem_cache_free(ns->pid_cachep, pid);
 		put_pid_ns(ns);
 	}
Signature · 1 line
2.47.2

With the patch as proposed you are only catching the misuse if this is the last ref though.

iow, the check should be hoisted above unrefing?

author maintainer
#3 lore raw
Quoted 31 lines · Mateusz Guzik
Quoted 26 lines · Christian Brauner

Now that we have pidfs_{get,register}_pid() that needs to be paired with pidfs_put_pid() it's possible that someone pairs them with put_pid(). Thus freeing struct pid while it's still used by pidfs. Notice when that happens. I'll also add a scheme to detect invalid uses of pidfs_get_pid() and pidfs_put_pid() later.

---
 kernel/pid.c | 1 +
 1 file changed, 1 insertion(+)
kernel/pid.c +1 -0
index 26f1e136f017..8317bcbc7cf7 100644
@@ -100,6 +100,7 @@ void put_pid(struct pid *pid)
 
 	ns = pid->numbers[pid->level].ns;
 	if (refcount_dec_and_test(&pid->count)) {
+		WARN_ON_ONCE(pid->stashed);
 		kmem_cache_free(ns->pid_cachep, pid);
 		put_pid_ns(ns);
 	}
Signature · 1 line
2.47.2

With the patch as proposed you are only catching the misuse if this is the last ref though.

Quoted 1 line

iow, the check should be hoisted above unrefing?

No, not really. If there's more than one reference then pid->stashed can be legimitately != NULL.