Skip to contentSkip to conversation

psi: Fix uaf issue when psi trigger is destroyed while being polled

PATCH Superseded Suren Baghdasaryan 3 participants 5 messages Jan 11, 2022

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Suren Baghdasaryan

author maintainer
#0 lore raw

PATCH 1/1 psi: Fix uaf issue when psi trigger is destroyed while being polled

With write operation on psi files replacing old trigger with a new one, the lifetime of its waitqueue is totally arbitrary. Overwriting an existing trigger causes its waitqueue to be freed and pending poll() will stumble on trigger->event_wait which was destroyed.
Fix this by disallowing to redefine an existing psi trigger. If a write operation is used on a file descriptor with an already existing psi trigger, the operation will fail with EBUSY error.
Also bypass a check for psi_disabled in the psi_trigger_destroy as the flag can be flipped after the trigger is created, leading to a memory leak.

---
 Documentation/accounting/psi.rst |  3 +-
 include/linux/psi.h              |  2 +-
 include/linux/psi_types.h        |  3 --
 kernel/cgroup/cgroup.c           | 11 ++++--
 kernel/sched/psi.c               | 68 +++++++++++++++-----------------
 5 files changed, 42 insertions(+), 45 deletions(-)
Documentation/accounting/psi.rst +2 -1
index f2b3439edcc2..860fe651d645 100644
@@ -92,7 +92,8 @@ Triggers can be set on more than one psi metric and more than one trigger
 for the same psi metric can be specified. However for each trigger a separate
 file descriptor is required to be able to poll it separately from others,
 therefore for each trigger a separate open() syscall should be made even
-when opening the same psi interface file.
+when opening the same psi interface file. Write operations to a file descriptor
+with an already existing psi trigger will fail with EBUSY.
 
 Monitors activate only when system enters stall state for the monitored
 psi metric and deactivates upon exit from the stall state. While system is
include/linux/psi.h +1 -1
index 65eb1476ac70..370707902345 100644
@@ -32,7 +32,7 @@ void cgroup_move_task(struct task_struct *p, struct css_set *to);
 
 struct psi_trigger *psi_trigger_create(struct psi_group *group,
 			char *buf, size_t nbytes, enum psi_res res);
-void psi_trigger_replace(void **trigger_ptr, struct psi_trigger *t);
+void psi_trigger_destroy(void **trigger_ptr);
 
 __poll_t psi_trigger_poll(void **trigger_ptr, struct file *file,
 			poll_table *wait);
include/linux/psi_types.h +0 -3
index 0a23300d49af..6537d0c92825 100644
@@ -129,9 +129,6 @@ struct psi_trigger {
 	 * events to one per window
 	 */
 	u64 last_event_time;
-
-	/* Refcounting to prevent premature destruction */
-	struct kref refcount;
 };
 
 struct psi_group {
kernel/cgroup/cgroup.c +8 -3
index cafb8c114a21..e6878238fb19 100644
@@ -3642,6 +3642,12 @@ static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf,
 	cgroup_get(cgrp);
 	cgroup_kn_unlock(of->kn);
 
+	/* Allow only one trigger per file descriptor */
+	if (READ_ONCE(ctx->psi.trigger)) {
+		cgroup_put(cgrp);
+		return -EBUSY;
+	}
+
 	psi = cgroup_ino(cgrp) == 1 ? &psi_system : &cgrp->psi;
 	new = psi_trigger_create(psi, buf, nbytes, res);
 	if (IS_ERR(new)) {
@@ -3649,8 +3655,7 @@ static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf,
 		return PTR_ERR(new);
 	}
 
-	psi_trigger_replace(&ctx->psi.trigger, new);
-
+	WRITE_ONCE(ctx->psi.trigger, new);
 	cgroup_put(cgrp);
 
 	return nbytes;
@@ -3689,7 +3694,7 @@ static void cgroup_pressure_release(struct kernfs_open_file *of)
 {
 	struct cgroup_file_ctx *ctx = of->priv;
 
-	psi_trigger_replace(&ctx->psi.trigger, NULL);
+	psi_trigger_destroy(&ctx->psi.trigger);
 }
 
 bool cgroup_psi_enabled(void)
kernel/sched/psi.c +31 -37

125 lines not on this page · raw message

Signature · 1 line
2.34.1.575.g55b058a8bb-goog
#1 lore raw
Quoted 4 lines · Suren Baghdasaryan

Fix this by disallowing to redefine an existing psi trigger. If a write operation is used on a file descriptor with an already existing psi trigger, the operation will fail with EBUSY error.

Looks fine to me. Eric?

I assume I'll get it through the usual channels unless there are issues,

Linus

#2 lore raw
In reply to Suren Baghdasaryan
Quoted 11 lines · Suren Baghdasaryan

With write operation on psi files replacing old trigger with a new one, the lifetime of its waitqueue is totally arbitrary. Overwriting an existing trigger causes its waitqueue to be freed and pending poll() will stumble on trigger->event_wait which was destroyed.
Fix this by disallowing to redefine an existing psi trigger. If a write operation is used on a file descriptor with an already existing psi trigger, the operation will fail with EBUSY error.
Also bypass a check for psi_disabled in the psi_trigger_destroy as the flag can be flipped after the trigger is created, leading to a memory leak.

Quoted 4 lines

Please include Fixes and Cc stable tags.

Quoted 5 lines
kernel/cgroup/cgroup.c +0 -0
index cafb8c114a21..e6878238fb19 100644

kernel/cgroup/cgroup.c static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf, · line 3650

Quoted 9 lines
kernel/cgroup/cgroup.c +6 -0
@@ -3642,6 +3642,12 @@ static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf,
 	cgroup_get(cgrp);
 	cgroup_kn_unlock(of->kn);
 
+	/* Allow only one trigger per file descriptor */
+	if (READ_ONCE(ctx->psi.trigger)) {
+		cgroup_put(cgrp);
+		return -EBUSY;
+	}
+

Doesn't the task have exclusive access to the file at this point? READ_ONCE() is only needed instead of a plain load when the field can be concurrently changed by another thread.

Quoted 23 lines
kernel/sched/psi.c +3 -4
index 1652f2bb54b7..882bf62cc247 100644
@@ -1151,7 +1151,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
 	t->event = 0;
 	t->last_event_time = 0;
 	init_waitqueue_head(&t->event_wait);
-	kref_init(&t->refcount);
 
 	mutex_lock(&group->trigger_lock);
 
@@ -1180,15 +1179,21 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
 	return t;
 }
 
-static void psi_trigger_destroy(struct kref *ref)
+void psi_trigger_destroy(void **trigger_ptr)
 {
-	struct psi_trigger *t = container_of(ref, struct psi_trigger, refcount);
-	struct psi_group *group = t->group;
+	struct psi_trigger *t;
+	struct psi_group *group;

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1194

Quoted 10 lines
kernel/sched/psi.c +6 -1
 	struct task_struct *task_to_destroy = NULL;
 
-	if (static_branch_likely(&psi_disabled))
+	/*
+	 * We do not check psi_disabled since it might have been disabled after
+	 * the trigger got created.
+	 */
+	t = xchg(trigger_ptr, NULL);
+	if (!t)
 		return;

Likewise, doesn't the task have exclusive access to the file at this point? This is only called during ->release().

And why does this take a pointer to a pointer instead of just the pointer?

Quoted 9 lines
kernel/sched/psi.c +0 -4
@@ -1305,14 +1289,24 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
 
 	buf[buf_size - 1] = '\0';
 
-	new = psi_trigger_create(&psi_system, buf, nbytes, res);
-	if (IS_ERR(new))
-		return PTR_ERR(new);
-
 	seq = file->private_data;

kernel/sched/psi.c static ssize_t psi_write(struct file *file, const char __user *user_buf, · line 1301

Quoted 10 lines
kernel/sched/psi.c +7 -1
+
 	/* Take seq->lock to protect seq->private from concurrent writes */
 	mutex_lock(&seq->lock);
-	psi_trigger_replace(&seq->private, new);
+
+	/* Allow only one trigger per file descriptor */
+	if (READ_ONCE(seq->private)) {
+		mutex_unlock(&seq->lock);
+		return -EBUSY;
+	}

Likewise, what does this race against that would require the use of READ_ONCE()?

- Eric

author maintainer
#3 lore raw
Quoted 13 lines · Eric Biggers
Quoted 11 lines · Suren Baghdasaryan

With write operation on psi files replacing old trigger with a new one, the lifetime of its waitqueue is totally arbitrary. Overwriting an existing trigger causes its waitqueue to be freed and pending poll() will stumble on trigger->event_wait which was destroyed.
Fix this by disallowing to redefine an existing psi trigger. If a write operation is used on a file descriptor with an already existing psi trigger, the operation will fail with EBUSY error.
Also bypass a check for psi_disabled in the psi_trigger_destroy as the flag can be flipped after the trigger is created, leading to a memory leak.

Quoted 6 lines
Quoted 4 lines

Please include Fixes and Cc stable tags.

Ack.

Quoted 9 lines
Quoted 9 lines
kernel/cgroup/cgroup.c +0 -0
index cafb8c114a21..e6878238fb19 100644
@@ -3642,6 +3642,12 @@ static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf,
      cgroup_get(cgrp);
      cgroup_kn_unlock(of->kn);
 
Quoted 10 lines

kernel/cgroup/cgroup.c static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf, · line 3650

Quoted 6 lines
kernel/cgroup/cgroup.c +6 -0
+     /* Allow only one trigger per file descriptor */
+     if (READ_ONCE(ctx->psi.trigger)) {
+             cgroup_put(cgrp);
+             return -EBUSY;
+     }
+

Doesn't the task have exclusive access to the file at this point? READ_ONCE() is only needed instead of a plain load when the field can be concurrently changed by another thread.

Yeah, you are right. Concurrent writes are serialized by of->mutex and kernfs_release_file documents "@of is guaranteed to have no other file operations in flight", so ->release() can't race with ->write(). Will fix.

Quoted 26 lines
Quoted 26 lines
kernel/sched/psi.c +3 -4
index 1652f2bb54b7..882bf62cc247 100644
@@ -1151,7 +1151,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
      t->event = 0;
      t->last_event_time = 0;
      init_waitqueue_head(&t->event_wait);
-     kref_init(&t->refcount);
 
      mutex_lock(&group->trigger_lock);
 
@@ -1180,15 +1179,21 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
      return t;
 }
 
-static void psi_trigger_destroy(struct kref *ref)
+void psi_trigger_destroy(void **trigger_ptr)
 {
-     struct psi_trigger *t = container_of(ref, struct psi_trigger, refcount);
-     struct psi_group *group = t->group;
+     struct psi_trigger *t;
+     struct psi_group *group;
      struct task_struct *task_to_destroy = NULL;
 
Quoted 11 lines

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1194

Quoted 8 lines
kernel/sched/psi.c +6 -1
-     if (static_branch_likely(&psi_disabled))
+     /*
+      * We do not check psi_disabled since it might have been disabled after
+      * the trigger got created.
+      */
+     t = xchg(trigger_ptr, NULL);
+     if (!t)
              return;

Likewise, doesn't the task have exclusive access to the file at this point? This is only called during ->release().

Yes, will fix.

Quoted 2 lines

And why does this take a pointer to a pointer instead of just the pointer?

That was done to do atomic xchg, but as you mentioned, it's not needed here. Will change.

Quoted 14 lines
Quoted 14 lines
kernel/sched/psi.c +1 -5
@@ -1305,14 +1289,24 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
 
      buf[buf_size - 1] = '\0';
 
-     new = psi_trigger_create(&psi_system, buf, nbytes, res);
-     if (IS_ERR(new))
-             return PTR_ERR(new);
-
      seq = file->private_data;
+
      /* Take seq->lock to protect seq->private from concurrent writes */
      mutex_lock(&seq->lock);
-     psi_trigger_replace(&seq->private, new);
Quoted 8 lines

kernel/sched/psi.c static ssize_t psi_write(struct file *file, const char __user *user_buf, · line 1301

Quoted 6 lines
kernel/sched/psi.c +6 -0
+
+     /* Allow only one trigger per file descriptor */
+     if (READ_ONCE(seq->private)) {
+             mutex_unlock(&seq->lock);
+             return -EBUSY;
+     }

Likewise, what does this race against that would require the use of READ_ONCE()?

Will fix.
Thanks!

Quoted 2 lines

- Eric

author maintainer
#4 lore raw
Quoted 118 lines · Suren Baghdasaryan
Quoted 13 lines · Eric Biggers
Quoted 11 lines · Suren Baghdasaryan

With write operation on psi files replacing old trigger with a new one, the lifetime of its waitqueue is totally arbitrary. Overwriting an existing trigger causes its waitqueue to be freed and pending poll() will stumble on trigger->event_wait which was destroyed.
Fix this by disallowing to redefine an existing psi trigger. If a write operation is used on a file descriptor with an already existing psi trigger, the operation will fail with EBUSY error.
Also bypass a check for psi_disabled in the psi_trigger_destroy as the flag can be flipped after the trigger is created, leading to a memory leak.

Quoted 6 lines
Quoted 4 lines

Please include Fixes and Cc stable tags.

Ack.

Quoted 9 lines
Quoted 9 lines
kernel/cgroup/cgroup.c +0 -0
index cafb8c114a21..e6878238fb19 100644
@@ -3642,6 +3642,12 @@ static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf,
      cgroup_get(cgrp);
      cgroup_kn_unlock(of->kn);
 
Quoted 10 lines

kernel/cgroup/cgroup.c static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, char *buf, · line 3650

Quoted 6 lines
kernel/cgroup/cgroup.c +6 -0
+     /* Allow only one trigger per file descriptor */
+     if (READ_ONCE(ctx->psi.trigger)) {
+             cgroup_put(cgrp);
+             return -EBUSY;
+     }
+

Doesn't the task have exclusive access to the file at this point? READ_ONCE() is only needed instead of a plain load when the field can be concurrently changed by another thread.

Yeah, you are right. Concurrent writes are serialized by of->mutex and kernfs_release_file documents "@of is guaranteed to have no other file operations in flight", so ->release() can't race with ->write(). Will fix.

Quoted 26 lines
Quoted 26 lines
kernel/sched/psi.c +3 -4
index 1652f2bb54b7..882bf62cc247 100644
@@ -1151,7 +1151,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
      t->event = 0;
      t->last_event_time = 0;
      init_waitqueue_head(&t->event_wait);
-     kref_init(&t->refcount);
 
      mutex_lock(&group->trigger_lock);
 
@@ -1180,15 +1179,21 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
      return t;
 }
 
-static void psi_trigger_destroy(struct kref *ref)
+void psi_trigger_destroy(void **trigger_ptr)
 {
-     struct psi_trigger *t = container_of(ref, struct psi_trigger, refcount);
-     struct psi_group *group = t->group;
+     struct psi_trigger *t;
+     struct psi_group *group;
      struct task_struct *task_to_destroy = NULL;
 
Quoted 11 lines

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1194

Quoted 8 lines
kernel/sched/psi.c +6 -1
-     if (static_branch_likely(&psi_disabled))
+     /*
+      * We do not check psi_disabled since it might have been disabled after
+      * the trigger got created.
+      */
+     t = xchg(trigger_ptr, NULL);
+     if (!t)
              return;

Likewise, doesn't the task have exclusive access to the file at this point? This is only called during ->release().

Yes, will fix.

Quoted 2 lines

And why does this take a pointer to a pointer instead of just the pointer?

That was done to do atomic xchg, but as you mentioned, it's not needed here. Will change.

Quoted 14 lines
Quoted 14 lines
kernel/sched/psi.c +1 -5
@@ -1305,14 +1289,24 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
 
      buf[buf_size - 1] = '\0';
 
-     new = psi_trigger_create(&psi_system, buf, nbytes, res);
-     if (IS_ERR(new))
-             return PTR_ERR(new);
-
      seq = file->private_data;
+
      /* Take seq->lock to protect seq->private from concurrent writes */
      mutex_lock(&seq->lock);
-     psi_trigger_replace(&seq->private, new);
Quoted 8 lines

kernel/sched/psi.c static ssize_t psi_write(struct file *file, const char __user *user_buf, · line 1301

Quoted 6 lines
kernel/sched/psi.c +6 -0
+
+     /* Allow only one trigger per file descriptor */
+     if (READ_ONCE(seq->private)) {
+             mutex_unlock(&seq->lock);
+             return -EBUSY;
+     }

Likewise, what does this race against that would require the use of READ_ONCE()?

Quoted 2 lines

Will fix.
Thanks!

Posted v2 at https://lore.kernel.org/all/20220111071212.1210124-1-surenb-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org

Quoted 3 lines
Quoted 3 lines

- Eric