Skip to contentSkip to conversation

cgroup: introduce cgroup namespaces

PATCH In review Dan Carpenter 4 participants 9 messages Feb 17, 2016 – Feb 29, 2016 3 messages not in the archive cgroup

lore
Share & tools
lore
#0 lore raw

Hello Aditya Kali,

The patch a79a908fd2b0: "cgroup: introduce cgroup namespaces" from Jan 29, 2016, leads to the following static checker warning:

	kernel/cgroup.c:6091 copy_cgroup_ns()
	error: 'new_ns' dereferencing possible ERR_PTR()

kernel/cgroup.c
6085

  6086          err = -ENOMEM;
  6087          new_ns = alloc_cgroup_ns();
  6088          if (!new_ns)
                    ^^^^^^^

This should be an IS_ERR() check.

  6089                  goto err_out;
  6090  
  6091          new_ns->user_ns = get_user_ns(user_ns);

regards,
dan carpenter

Applied
#1 lore raw

PATCH 1/1 cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()

From d22025570e2ebfc68819b35c5d457e53d9337217 Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Thu, 18 Feb 2016 11:44:24 -0500

alloc_cgroup_ns() returns an ERR_PTR value on error but copy_cgroup_ns() was checking for NULL for error. Fix it.

---
Hello, Dan.

Applied to cgroup/for-4.5-fixes.

Thanks.

 kernel/cgroup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/cgroup.c +3 -2
index afb1205..d92d91a 100644
@@ -6083,10 +6083,11 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 	spin_unlock_bh(&css_set_lock);
 	mutex_unlock(&cgroup_mutex);
 
-	err = -ENOMEM;
 	new_ns = alloc_cgroup_ns();
-	if (!new_ns)
+	if (IS_ERR(new_ns)) {
+		err = PTR_ERR(new_ns);
 		goto err_out;
+	}
 
 	new_ns->user_ns = get_user_ns(user_ns);
 	new_ns->root_cset = cset;
Signature · 1 line
2.5.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

Acked-by
#3 lore raw

Re: [PATCH cgroup/for-4.5-fixes] cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()

Thanks for catching.

Quoted 9 lines · Tejun Heo

From d22025570e2ebfc68819b35c5d457e53d9337217 Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Thu, 18 Feb 2016 11:44:24 -0500

alloc_cgroup_ns() returns an ERR_PTR value on error but copy_cgroup_ns() was checking for NULL for error. Fix it.

Quoted 31 lines

---
Hello, Dan.

Applied to cgroup/for-4.5-fixes.

Thanks.

 kernel/cgroup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/cgroup.c +3 -3
index afb1205..d92d91a 100644
@@ -6083,10 +6083,11 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
        spin_unlock_bh(&css_set_lock);
        mutex_unlock(&cgroup_mutex);
 
-       err = -ENOMEM;
        new_ns = alloc_cgroup_ns();
-       if (!new_ns)
+       if (IS_ERR(new_ns)) {
+               err = PTR_ERR(new_ns);
                goto err_out;
+       }
 
        new_ns->user_ns = get_user_ns(user_ns);
        new_ns->root_cset = cset;
--

2.5.0

Signature · 1 line
Aditya
#4 lore raw

Re: [PATCH cgroup/for-4.5-fixes] cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()

In reply to a message not in the archive

Sorry, I was feeling like a jerk yesterday.

I obviously could have fixed this myself but I wanted to make a point about one error label, do-everything style "future proof" error handling. It is a trap for the unwary.

Now the code calls kfree(-ENOMEM). It's a very predictable mistake.

regards,
dan carpenter

#5 lore raw

Re: [PATCH cgroup/for-4.5-fixes] cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()

Quoted 7 lines · Dan Carpenter

Sorry, I was feeling like a jerk yesterday.

I obviously could have fixed this myself but I wanted to make a point about one error label, do-everything style "future proof" error handling. It is a trap for the unwary.

Now the code calls kfree(-ENOMEM). It's a very predictable mistake.

lol let's take the chance and convert the NULL check in kfree() to IS_ERR_OR_NULL().

Seriously tho, I don't think this is a good evidence against merged error handling. It's not like we don't make repeated mistakes with split error labels. Anyways, care to submit a proper patch?

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Re: [PATCH cgroup/for-4.5-fixes] cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()

I'm not sure what to write the patch against now. Can you just do this?

regards,
dan carpenter

#8 lore raw

PATCH 1/1 cgroup: fix and restructure error handling in copy_cgroup_ns()

copy_cgroup_ns()'s error handling was broken and the attempt to fix it d22025570e2e ("cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()") was broken too in that it ended up trying an ERR_PTR() value.

There's only one place where copy_cgroup_ns() needs to perform cleanup after failure. Simplify and fix the error handling by removing the goto's.

---
 kernel/cgroup.c |   18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
kernel/cgroup.c +5 -13
index d92d91a..2c88149 100644
@@ -6058,9 +6058,8 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 					struct user_namespace *user_ns,
 					struct cgroup_namespace *old_ns)
 {
-	struct cgroup_namespace *new_ns = NULL;
-	struct css_set *cset = NULL;
-	int err;
+	struct cgroup_namespace *new_ns;
+	struct css_set *cset;
 
 	BUG_ON(!old_ns);
 
@@ -6070,9 +6069,8 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 	}
 
 	/* Allow only sysadmin to create cgroup namespace. */
-	err = -EPERM;
 	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
-		goto err_out;
+		return ERR_PTR(-EPERM);
 
 	mutex_lock(&cgroup_mutex);
 	spin_lock_bh(&css_set_lock);
@@ -6085,20 +6083,14 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 
 	new_ns = alloc_cgroup_ns();
 	if (IS_ERR(new_ns)) {
-		err = PTR_ERR(new_ns);
-		goto err_out;
+		put_css_set(cset);
+		return new_ns;
 	}
 
 	new_ns->user_ns = get_user_ns(user_ns);
 	new_ns->root_cset = cset;
 
 	return new_ns;
-
-err_out:
-	if (cset)
-		put_css_set(cset);
-	kfree(new_ns);
-	return ERR_PTR(err);
 }
 
 static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)

A message that is not in the archive (sent off-list or not yet received): [email protected]

Acked-by
#10 lore raw

Re: [PATCH cgroup/for-4.6-ns] cgroup: fix and restructure error handling in copy_cgroup_ns()

Quoted 11 lines · Tejun Heo

copy_cgroup_ns()'s error handling was broken and the attempt to fix it d22025570e2e ("cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()") was broken too in that it ended up trying an ERR_PTR() value.

There's only one place where copy_cgroup_ns() needs to perform cleanup after failure. Simplify and fix the error handling by removing the goto's.

It certainly looks cleaner, thanks.

Quoted 54 lines
---
 kernel/cgroup.c |   18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
kernel/cgroup.c +5 -13
index d92d91a..2c88149 100644
@@ -6058,9 +6058,8 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 					struct user_namespace *user_ns,
 					struct cgroup_namespace *old_ns)
 {
-	struct cgroup_namespace *new_ns = NULL;
-	struct css_set *cset = NULL;
-	int err;
+	struct cgroup_namespace *new_ns;
+	struct css_set *cset;
 
 	BUG_ON(!old_ns);
 
@@ -6070,9 +6069,8 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 	}
 
 	/* Allow only sysadmin to create cgroup namespace. */
-	err = -EPERM;
 	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
-		goto err_out;
+		return ERR_PTR(-EPERM);
 
 	mutex_lock(&cgroup_mutex);
 	spin_lock_bh(&css_set_lock);
@@ -6085,20 +6083,14 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 
 	new_ns = alloc_cgroup_ns();
 	if (IS_ERR(new_ns)) {
-		err = PTR_ERR(new_ns);
-		goto err_out;
+		put_css_set(cset);
+		return new_ns;
 	}
 
 	new_ns->user_ns = get_user_ns(user_ns);
 	new_ns->root_cset = cset;
 
 	return new_ns;
-
-err_out:
-	if (cset)
-		put_css_set(cset);
-	kfree(new_ns);
-	return ERR_PTR(err);
 }
 
 static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
Applied
#11 lore raw

Re: [PATCH cgroup/for-4.6-ns] cgroup: fix and restructure error handling in copy_cgroup_ns()

Quoted 10 lines · Serge Hallyn
Quoted 9 lines · Tejun Heo

copy_cgroup_ns()'s error handling was broken and the attempt to fix it d22025570e2e ("cgroup: fix alloc_cgroup_ns() error handling in copy_cgroup_ns()") was broken too in that it ended up trying an ERR_PTR() value.

There's only one place where copy_cgroup_ns() needs to perform cleanup after failure. Simplify and fix the error handling by removing the goto's.

Quoted 6 lines
Quoted 2 lines

It certainly looks cleaner, thanks.

Applied to cgroup/for-4.6-ns.

Thanks.

Signature · 1 line
tejun