From: Kamalesh Babulal Date: Wed, 21 Jun 2023 04:16:20 +0000 (+0530) Subject: cgcreate: fix null pointer dereference in create_systemd_scope() X-Git-Tag: v3.1.0~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f740b9bf8898b25992c5b9acbb880e1e78a665b6;p=thirdparty%2Flibcgroup.git cgcreate: fix null pointer dereference in create_systemd_scope() 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 Signed-off-by: Tom Hromatka --- diff --git a/src/tools/cgcreate.c b/src/tools/cgcreate.c index 0314e84c..589d72af 100644 --- a/src/tools/cgcreate.c +++ b/src/tools/cgcreate.c @@ -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 /\n", + prog_name, cg->name); + ret = ECGINVAL; + goto err; + } len = strlen(cg->name) - strlen(scope); strncpy(slice, cg->name, len); slice[len] = '\0';