Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgroup_utils: explicitly check for cgroup2 FDs in cgroup_walkup_to_root #633

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading