]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgcreate: fix null pointer dereference in create_systemd_scope()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Wed, 21 Jun 2023 04:16:20 +0000 (09:46 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 21 Jun 2023 16:21:46 +0000 (10:21 -0600)
Fix a NULL pointer dereference, reported by the Coverity tool:

CID 321267 (#1 of 1): Dereference null return value (NULL_RETURNS)6.
dereference: Dereferencing a pointer that might be NULL scope when
calling strlen.

check for the return value from strstr(), before dereferencing it.

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

index 0314e84c3fc82be2441a2190d5b0638d1ee47572..589d72afd80189573d7f083030c6160dffc750c3 100644 (file)
@@ -71,6 +71,12 @@ static int create_systemd_scope(struct cgroup * const cg, const char * const pro
        ret = cgroup_create_scope2(cg, 0, &opts);
        if (!ret && set_default) {
                scope = strstr(cg->name, "/");
+               if (!scope) {
+                       err("%s: Invalid scope name %s, expected <slice>/<scope>\n",
+                           prog_name, cg->name);
+                       ret = ECGINVAL;
+                       goto err;
+               }
                len = strlen(cg->name) - strlen(scope);
                strncpy(slice, cg->name, len);
                slice[len] = '\0';