Skip to contentSkip to conversation

mm/damon/sysfs-schemes: fix use-after-free for [memcg_]path

RFC PATCH In review SeongJae Park 2 participants 8 messages Apr 22 – Apr 23

Subsystems (3)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon/sysfs-schemes: fix use-after-free for [memcg_]path

Reads of 'path' and 'memcg_path' files in DAMON sysfs interface could race with their writes, results in use-after-free. Fix those.

SeongJae Park (2):
mm/damon/sysfs-schemes: protect memcg_path kfree() with
damon_sysfs_lock
mm/damon/sysfs-schemes: protect path kfree() with damon_sysfs_lock

 mm/damon/sysfs-schemes.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

base-commit: 0d45806f3a75bf53e59475b0e56be324f650ab09

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

RFC PATCH 1/2 mm/damon/sysfs-schemes: protect memcg_path kfree() with damon_sysfs_lock

damon_sysfs_scheme_filter->mmecg_path can be read and written by users, via DAMON sysfs memcg_path file. It can also be indirectly read, for the parameters {on,off}line committing to DAMON. The reads for parameters committing are protected by damon_sysfs_lock to avoid the sysfs files being destroyed while any of the parameters are being read. But the user-driven direct reads and writes are not protected by any lock, while the write is deallocating the memcg_path-pointing buffer. As a result, the readers could read the already freed buffer (user-after-free). Note that the user-reads don't race when the same open file is used by the writer, due to kernfs's open file locking. Nonetheless, doing the reads and writes with separate open files would be common. Fix it by protecting both the user-direct reads and writes with damon_sysfs_lock.

Fixes 4f489fe6afb3 ("mm/damon/sysfs-schemes: free old damon_sysfs_scheme_filter->memcg_path on write")
Cc <[email protected]> # 6.16.x
---
 mm/damon/sysfs-schemes.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
mm/damon/sysfs-schemes.c +11 -1
index 5186966dafb35..8d32a20531d49 100644
@@ -533,9 +533,14 @@ static ssize_t memcg_path_show(struct kobject *kobj,
 {
 	struct damon_sysfs_scheme_filter *filter = container_of(kobj,
 			struct damon_sysfs_scheme_filter, kobj);
+	int len;
 
-	return sysfs_emit(buf, "%s\n",
+	if (!mutex_trylock(&damon_sysfs_lock))
+		return -EBUSY;
+	len = sysfs_emit(buf, "%s\n",
 			filter->memcg_path ? filter->memcg_path : "");
+	mutex_unlock(&damon_sysfs_lock);
+	return len;
 }
 
 static ssize_t memcg_path_store(struct kobject *kobj,
@@ -550,8 +555,13 @@ static ssize_t memcg_path_store(struct kobject *kobj,
 		return -ENOMEM;
 
 	strscpy(path, buf, count + 1);
+	if (!mutex_trylock(&damon_sysfs_lock)) {
+		kfree(path);
+		return -EBUSY;
+	}
 	kfree(filter->memcg_path);
 	filter->memcg_path = path;
+	mutex_unlock(&damon_sysfs_lock);
 	return count;
 }
 
Signature · 1 line
2.47.3
#2 lore raw

Re: [RFC PATCH 1/2] mm/damon/sysfs-schemes: protect memcg_path kfree() with damon_sysfs_lock

Sashiko AI review found 1 potential issue(s): - [High] Breaking from `mem_cgroup_iter()` without calling `mem_cgroup_iter_break()` leaks a cgroup reference in `damon_sysfs_memcg_path_to_id()`. --

Sashiko AI review found 1 potential issue(s):
- [High] Breaking from mem_cgroup_iter() without calling mem_cgroup_iter_break() leaks a cgroup reference in damon_sysfs_memcg_path_to_id(). --

commit f19bff910905231adad168cfffc97ed67cb1fde3
Author: SeongJae Park <[email protected]>

mm/damon/sysfs-schemes: protect memcg_path kfree() with damon_sysfs_lock

This commit fixes a use-after-free vulnerability by using damon_sysfs_lock to protect concurrent user-direct reads and writes to the memcg_path sysfs file. The locking prevents readers from accessing the memory path buffer while it is being deallocated by a write operation.

Quoted 4 lines
mm/damon/sysfs-schemes.c +0 -0
index 5186966dafb35..8d32a20531d49 100644

[ ... ]

mm/damon/sysfs-schemes.c static ssize_t memcg_path_store(struct kobject *kobj, · line 566

Quoted 13 lines
mm/damon/sysfs-schemes.c +5 -0
@@ -550,8 +555,13 @@ static ssize_t memcg_path_store(struct kobject *kobj,
 		return -ENOMEM;
 
 	strscpy(path, buf, count + 1);
+	if (!mutex_trylock(&damon_sysfs_lock)) {
+		kfree(path);
+		return -EBUSY;
+	}
 	kfree(filter->memcg_path);
 	filter->memcg_path = path;
+	mutex_unlock(&damon_sysfs_lock);
 	return count;
 }

This isn't a bug introduced by this patch, but I noticed a pre-existing issue in this file.

Does damon_sysfs_memcg_path_to_id() leak a cgroup reference when breaking early from the hierarchy walk?

mm/damon/sysfs-schemes.c:damon_sysfs_memcg_path_to_id() {
	...
	for (memcg = mem_cgroup_iter(NULL, NULL, NULL); memcg;
			memcg = mem_cgroup_iter(NULL, memcg, NULL)) {
		/* skip offlined memcg */
		if (!mem_cgroup_online(memcg))
			continue;
		if (damon_sysfs_memcg_path_eq(memcg, path, memcg_path)) {
			*id = mem_cgroup_id(memcg);
			found = true;
			break;
		}
	}
	...
}

According to the API contract, if a hierarchy walk is aborted prematurely before a round-trip is complete, the caller must use mem_cgroup_iter_break(NULL, memcg) to drop the reference to the last visited cgroup.

Because this is missing, could every successful resolution of a memcg path permanently leak a css reference?

In environments where DAMON configurations are frequently updated (like dynamically profiling containers), this could lead to a buildup of "zombie" cgroups that cannot be destroyed, eventually pinning substantial kernel memory and causing system-wide memory exhaustion.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [RFC PATCH 1/2] mm/damon/sysfs-schemes: protect memcg_path kfree() with damon_sysfs_lock

Quoted 2 lines · On Wed, 22 Apr 2026 20:30:16 +0000 [email protected] wrote:

Sashiko AI review found 1 potential issue(s):
- [High] Breaking from mem_cgroup_iter() without calling mem_cgroup_iter_break() leaks a cgroup reference in damon_sysfs_memcg_path_to_id().

Nice finding, but not this patch's fault. I will separately work on this.

Quoted 60 lines

--

commit f19bff910905231adad168cfffc97ed67cb1fde3
Author: SeongJae Park <[email protected]>

mm/damon/sysfs-schemes: protect memcg_path kfree() with damon_sysfs_lock

This commit fixes a use-after-free vulnerability by using damon_sysfs_lock to protect concurrent user-direct reads and writes to the memcg_path sysfs file. The locking prevents readers from accessing the memory path buffer while it is being deallocated by a write operation.

Quoted 4 lines
mm/damon/sysfs-schemes.c +0 -0
index 5186966dafb35..8d32a20531d49 100644

[ ... ]

mm/damon/sysfs-schemes.c static ssize_t memcg_path_store(struct kobject *kobj, · line 566

Quoted 13 lines
mm/damon/sysfs-schemes.c +5 -0
@@ -550,8 +555,13 @@ static ssize_t memcg_path_store(struct kobject *kobj,
 		return -ENOMEM;
 
 	strscpy(path, buf, count + 1);
+	if (!mutex_trylock(&damon_sysfs_lock)) {
+		kfree(path);
+		return -EBUSY;
+	}
 	kfree(filter->memcg_path);
 	filter->memcg_path = path;
+	mutex_unlock(&damon_sysfs_lock);
 	return count;
 }

This isn't a bug introduced by this patch, but I noticed a pre-existing issue in this file.

Does damon_sysfs_memcg_path_to_id() leak a cgroup reference when breaking early from the hierarchy walk?

mm/damon/sysfs-schemes.c:damon_sysfs_memcg_path_to_id() {
	...
	for (memcg = mem_cgroup_iter(NULL, NULL, NULL); memcg;
			memcg = mem_cgroup_iter(NULL, memcg, NULL)) {
		/* skip offlined memcg */
		if (!mem_cgroup_online(memcg))
			continue;
		if (damon_sysfs_memcg_path_eq(memcg, path, memcg_path)) {
			*id = mem_cgroup_id(memcg);
			found = true;
			break;
		}
	}
	...
}

According to the API contract, if a hierarchy walk is aborted prematurely before a round-trip is complete, the caller must use mem_cgroup_iter_break(NULL, memcg) to drop the reference to the last visited cgroup.

Quoted 7 lines

Because this is missing, could every successful resolution of a memcg path permanently leak a css reference?

In environments where DAMON configurations are frequently updated (like dynamically profiling containers), this could lead to a buildup of "zombie" cgroups that cannot be destroyed, eventually pinning substantial kernel memory and causing system-wide memory exhaustion.

Nice finding. As Sashiko also mentions, not this patch's fault, though. I will separately work on this.

Quoted 4 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

Thanks,
SJ

author maintainer
#4 lore raw

RFC PATCH 2/2 mm/damon/sysfs-schemes: protect path kfree() with damon_sysfs_lock

In reply to SeongJae Park

damon_sysfs_quot_goal->path can be read and written by users, via DAMON sysfs memcg_path file. It can also be indirectly read, for the parameters {on,off}line committing to DAMON. The reads for parameters committing are protected by damon_sysfs_lock to avoid the sysfs files being destroyed while any of the parameters are being read. But the user-driven direct reads and writes are not protected by any lock, while the write is deallocating the path-pointing buffer. As a result, the readers could read the already freed buffer (user-after-free). Note that the user-reads don't race when the same open file is used by the writer, due to kernfs's open file locking. Nonetheless, doing the reads and writes with separate open files would be common. Fix it by protecting both the user-direct reads and writes with damon_sysfs_lock.

Fixes c41e253a411e ("mm/damon/sysfs-schemes: implement path file under quota goal directory")
Cc <[email protected]> # 6.19.x
---
 mm/damon/sysfs-schemes.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
mm/damon/sysfs-schemes.c +11 -1
index 8d32a20531d49..245d63808411a 100644
@@ -1197,8 +1197,13 @@ static ssize_t path_show(struct kobject *kobj,
 {
 	struct damos_sysfs_quota_goal *goal = container_of(kobj,
 			struct damos_sysfs_quota_goal, kobj);
+	int len;
 
-	return sysfs_emit(buf, "%s\n", goal->path ? goal->path : "");
+	if (!mutex_trylock(&damon_sysfs_lock))
+		return -EBUSY;
+	len = sysfs_emit(buf, "%s\n", goal->path ? goal->path : "");
+	mutex_unlock(&damon_sysfs_lock);
+	return len;
 }
 
 static ssize_t path_store(struct kobject *kobj,
@@ -1213,8 +1218,13 @@ static ssize_t path_store(struct kobject *kobj,
 		return -ENOMEM;
 
 	strscpy(path, buf, count + 1);
+	if (!mutex_trylock(&damon_sysfs_lock)) {
+		kfree(path);
+		return -EBUSY;
+	}
 	kfree(goal->path);
 	goal->path = path;
+	mutex_unlock(&damon_sysfs_lock);
 	return count;
 }
 
Signature · 1 line
2.47.3
#5 lore raw

Re: [RFC PATCH 2/2] mm/damon/sysfs-schemes: protect path kfree() with damon_sysfs_lock

Sashiko AI review found 1 potential issue(s): - [Low] The commit message incorrectly references the `memcg_path` file instead of the `path` file. --

Sashiko AI review found 1 potential issue(s):
- [Low] The commit message incorrectly references the memcg_path file instead of the path file. --

commit 9335b00570732d12b29034fc72a20506fd69fbdd
Author: SeongJae Park <[email protected]>

mm/damon/sysfs-schemes: protect path kfree() with damon_sysfs_lock

This commit adds damon_sysfs_lock protection to the path attribute's sysfs show and store functions. This prevents a use-after-free race condition during concurrent user-driven reads and writes.

Quoted 2 lines

damon_sysfs_quot_goal->path can be read and written by users, via DAMON sysfs memcg_path file. It can also be indirectly read, for the

This isn't a bug, but the commit message references the memcg_path sysfs file. Since the patch actually modifies path_show and path_store, which handle the path attribute for quota goals, should this say "via DAMON sysfs path file" instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#6 lore raw

Re: [RFC PATCH 2/2] mm/damon/sysfs-schemes: protect path kfree() with damon_sysfs_lock

Quoted 13 lines · On Wed, 22 Apr 2026 21:02:21 +0000 [email protected] wrote:

Sashiko AI review found 1 potential issue(s):
- [Low] The commit message incorrectly references the memcg_path file instead of the path file. --

commit 9335b00570732d12b29034fc72a20506fd69fbdd
Author: SeongJae Park <[email protected]>

mm/damon/sysfs-schemes: protect path kfree() with damon_sysfs_lock

This commit adds damon_sysfs_lock protection to the path attribute's sysfs show and store functions. This prevents a use-after-free race condition during concurrent user-driven reads and writes.

Quoted 7 lines
Quoted 2 lines

damon_sysfs_quot_goal->path can be read and written by users, via DAMON sysfs memcg_path file. It can also be indirectly read, for the

This isn't a bug, but the commit message references the memcg_path sysfs file. Since the patch actually modifies path_show and path_store, which handle the path attribute for quota goals, should this say "via DAMON sysfs path file" instead?

Good finding. I will fix this in the next revision.

Quoted 4 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

Thanks,
SJ

author maintainer
#7 lore raw
In reply to SeongJae Park
Quoted 2 lines · On Wed, 22 Apr 2026 07:34:59 -0700 SeongJae Park <[email protected]> wrote:

Reads of 'path' and 'memcg_path' files in DAMON sysfs interface could race with their writes, results in use-after-free. Fix those.

Forgot adding change log, sorry.

Changes from v2
- v2: https://lore.kernel.org/[email protected]
- Split patch for individual fixes commits.
- Hand-off authorship to SJ, give Co-developed-by: to Junxi.
- Use mutex_trylock() instead of mutex_lock().
- Add RFC tag for Sashiko review round.
- Wordsmith commit messages.
Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Protect not only user-writes but also user-reads.

Thanks,
SJ

Quoted 6 lines

SeongJae Park (2):
mm/damon/sysfs-schemes: protect memcg_path kfree() with
damon_sysfs_lock
mm/damon/sysfs-schemes: protect path kfree() with damon_sysfs_lock

Quoted 7 lines
 mm/damon/sysfs-schemes.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

base-commit: 0d45806f3a75bf53e59475b0e56be324f650ab09

Signature · 1 line
2.47.3

Sent using hkml (https://github.com/sjp38/hackermail)