Skip to content

Commit

Permalink
cgroup_utils: explicitly check for cgroup2 FDs in cgroup_walkup_to_root
Browse files Browse the repository at this point in the history
See:
lxc#617 (comment)

Suggested-by: Christian Brauner (Microsoft) <[email protected]>
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
  • Loading branch information
mihalicyn committed Mar 27, 2024
1 parent 532c61f commit e4d5736
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/cgroups/cgroup_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ bool is_cgroup_fd(int fd)
return false;
}

bool is_cgroup2_fd(int fd)
{
int ret;
struct statfs fs;

ret = fstatfs(fd, &fs);
if (ret)
return false;

if (is_fs_type(&fs, CGROUP2_SUPER_MAGIC))
return true;

return false;
}

void *must_realloc(void *orig, size_t sz)
{
void *ret;
Expand Down Expand Up @@ -766,11 +781,14 @@ int cgroup_walkup_to_root(int cgroup2_root_fd, int hierarchy_fd,
return 0;
}

if (!is_cgroup2_fd(dir_fd))
return -EINVAL;

/*
* Legacy cgroup hierarchies should always show a valid value in the
* file of the cgroup. So no need to do this upwards walking crap.
*/
if (cgroup2_root_fd < 0)
if (cgroup2_root_fd < 0 || !is_cgroup2_fd(cgroup2_root_fd))
return -EINVAL;
else if (same_file(cgroup2_root_fd, dir_fd))
return 1;
Expand All @@ -791,6 +809,9 @@ int cgroup_walkup_to_root(int cgroup2_root_fd, int hierarchy_fd,
if (dir_fd < 0)
return -errno;

if (!is_cgroup2_fd(dir_fd))
return log_error_errno(-ELOOP, ELOOP, "Found non-cgroup2 directory during cgroup2 tree walkup. Terminating walk");

/*
* We're at the root of the cgroup2 tree so stop walking
* upwards.
Expand Down

0 comments on commit e4d5736

Please sign in to comment.