]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api.c: fix segfault in cgroup_attach_task_pid()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Mon, 19 Dec 2022 16:25:52 +0000 (09:25 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 19 Dec 2022 16:25:56 +0000 (09:25 -0700)
When the user passes NULL in place of struct cgroup argument, the pid
passed as the second parameter is expected to be attached to root
hierarchies of the controllers in the case of legacy/hybrid modes and
to the root hierarchy in the case of unified mode.

The current code will segfault, while attempting to dereference a NULL
pointer, fix it by using the controller names, from cg_mount_table(),
that is already populated with the information and also remove the
controller_is_enabled check is specific to the cgroup v2 hierarchy.
With the root cgroup v2 hierarchy, all the controllers are enabled by
default, hence the check returns true always.

Reproducer:

int main(int argc, char *argv[]) {
        pid_t pid = atoi(argv[1]);
        int ret;

        ret = cgroup_init();
        if (ret) {
                printf("Failed to initialize: %s\n", cgroup_strerror(ret));
                exit(1);
        }

        ret = cgroup_attach_task_pid(NULL, pid);
        if (ret) {
                printf("Failed to attach %d to root cgroup(s): %s\n", pid, cgroup_strerror(ret));
                exit(1);
        }

        exit (0);
}

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/api.c

index 2c30ad3215efe230c3b5972d401a3d84823cbda7..bcea11c788bc1813e51ab664412b961fb7570824 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -1879,14 +1879,9 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid)
                pthread_rwlock_rdlock(&cg_mount_table_lock);
                for (i = 0; i < CG_CONTROLLER_MAX &&
                                cg_mount_table[i].name[0] != '\0'; i++) {
-                       ret = cgroupv2_controller_enabled(cgroup->name,
-                               cgroup->controller[i]->name);
-                       if (ret)
-                               return ret;
-
                        ret = cgroup_build_tasks_procs_path(path,
-                               sizeof(path), cgroup->name,
-                               cgroup->controller[i]->name);
+                               sizeof(path), NULL,
+                               cg_mount_table[i].name);
                        if (ret)
                                return ret;