Skip to content

Commit 4a7ba45

Browse files
htejunakpm00
authored andcommitted
memcg: fix possible use-after-free in memcg_write_event_control()
memcg_write_event_control() accesses the dentry->d_name of the specified control fd to route the write call. As a cgroup interface file can't be renamed, it's safe to access d_name as long as the specified file is a regular cgroup file. Also, as these cgroup interface files can't be removed before the directory, it's safe to access the parent too. Prior to 347c4a8 ("memcg: remove cgroup_event->cft"), there was a call to __file_cft() which verified that the specified file is a regular cgroupfs file before further accesses. The cftype pointer returned from __file_cft() was no longer necessary and the commit inadvertently dropped the file type check with it allowing any file to slip through. With the invarients broken, the d_name and parent accesses can now race against renames and removals of arbitrary files and cause use-after-free's. Fix the bug by resurrecting the file type check in __file_cft(). Now that cgroupfs is implemented through kernfs, checking the file operations needs to go through a layer of indirection. Instead, let's check the superblock and dentry type. Link: https://lkml.kernel.org/r/Y5FRm/cfcKPGzWwl@slm.duckdns.org Fixes: 347c4a8 ("memcg: remove cgroup_event->cft") Signed-off-by: Tejun Heo <tj@kernel.org> Reported-by: Jann Horn <jannh@google.com> Acked-by: Roman Gushchin <roman.gushchin@linux.dev> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Muchun Song <songmuchun@bytedance.com> Cc: Shakeel Butt <shakeelb@google.com> Cc: <stable@vger.kernel.org> [3.14+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent a501788 commit 4a7ba45

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

include/linux/cgroup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct css_task_iter {
6868
struct list_head iters_node; /* css_set->task_iters */
6969
};
7070

71+
extern struct file_system_type cgroup_fs_type;
7172
extern struct cgroup_root cgrp_dfl_root;
7273
extern struct css_set init_css_set;
7374

kernel/cgroup/cgroup-internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ struct cgroup_mgctx {
167167
extern spinlock_t css_set_lock;
168168
extern struct cgroup_subsys *cgroup_subsys[];
169169
extern struct list_head cgroup_roots;
170-
extern struct file_system_type cgroup_fs_type;
171170

172171
/* iterate across the hierarchies */
173172
#define for_each_root(root) \

mm/memcontrol.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4832,6 +4832,7 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
48324832
unsigned int efd, cfd;
48334833
struct fd efile;
48344834
struct fd cfile;
4835+
struct dentry *cdentry;
48354836
const char *name;
48364837
char *endp;
48374838
int ret;
@@ -4885,6 +4886,16 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
48854886
if (ret < 0)
48864887
goto out_put_cfile;
48874888

4889+
/*
4890+
* The control file must be a regular cgroup1 file. As a regular cgroup
4891+
* file can't be renamed, it's safe to access its name afterwards.
4892+
*/
4893+
cdentry = cfile.file->f_path.dentry;
4894+
if (cdentry->d_sb->s_type != &cgroup_fs_type || !d_is_reg(cdentry)) {
4895+
ret = -EINVAL;
4896+
goto out_put_cfile;
4897+
}
4898+
48884899
/*
48894900
* Determine the event callbacks and set them in @event. This used
48904901
* to be done via struct cftype but cgroup core no longer knows
@@ -4893,7 +4904,7 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
48934904
*
48944905
* DO NOT ADD NEW FILES.
48954906
*/
4896-
name = cfile.file->f_path.dentry->d_name.name;
4907+
name = cdentry->d_name.name;
48974908

48984909
if (!strcmp(name, "memory.usage_in_bytes")) {
48994910
event->register_event = mem_cgroup_usage_register_event;
@@ -4917,7 +4928,7 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
49174928
* automatically removed on cgroup destruction but the removal is
49184929
* asynchronous, so take an extra ref on @css.
49194930
*/
4920-
cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
4931+
cfile_css = css_tryget_online_from_dir(cdentry->d_parent,
49214932
&memory_cgrp_subsys);
49224933
ret = -EINVAL;
49234934
if (IS_ERR(cfile_css))

0 commit comments

Comments
 (0)