Skip to contentSkip to conversation

ovl: pass an explicit reference of creators creds to callers

PATCH In review Amir Goldstein 2 participants 6 messages Nov 14, 2024

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

PATCH 1/1 ovl: pass an explicit reference of creators creds to callers

ovl_setup_cred_for_create() decrements one refcount of new creds and ovl_revert_creds() in callers decrements the last refcount.

In preparation to revert_creds_light() back to caller creds, pass an explicit reference of the creators creds to the callers and drop the refcount explicitly in the callers after ovl_revert_creds().

---

Miklos, Christian,

I was chasing a suspect memleak in revert_creds_light() patches. This fix is unrelated to memleak but I think it is needed for correctness anyway.

This applies in the middle of the series after adding the ovl_revert_creds() helper.

Thanks,
Amir.

 fs/overlayfs/dir.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
fs/overlayfs/dir.c +22 -12
index 09db5eb19242..4b0bb7a91d37 100644
@@ -553,15 +553,17 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
 	goto out_dput;
 }
 
-static int ovl_setup_cred_for_create(struct dentry *dentry, struct inode *inode,
-				     umode_t mode, const struct cred *old_cred)
+static const struct cred *ovl_setup_cred_for_create(struct dentry *dentry,
+						    struct inode *inode,
+						    umode_t mode,
+						    const struct cred *old_cred)
 {
 	int err;
 	struct cred *override_cred;
 
 	override_cred = prepare_creds();
 	if (!override_cred)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	override_cred->fsuid = inode->i_uid;
 	override_cred->fsgid = inode->i_gid;
@@ -569,19 +571,18 @@ static int ovl_setup_cred_for_create(struct dentry *dentry, struct inode *inode,
 					      old_cred, override_cred);
 	if (err) {
 		put_cred(override_cred);
-		return err;
+		return ERR_PTR(err);
 	}
 	put_cred(override_creds(override_cred));
-	put_cred(override_cred);
 
-	return 0;
+	return override_cred;
 }
 
 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 			      struct ovl_cattr *attr, bool origin)
 {
 	int err;
-	const struct cred *old_cred;
+	const struct cred *old_cred, *new_cred = NULL;
 	struct dentry *parent = dentry->d_parent;
 
 	old_cred = ovl_override_creds(dentry->d_sb);
@@ -610,9 +611,13 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 		 * create a new inode, so just use the ovl mounter's
 		 * fs{u,g}id.
 		 */
-		err = ovl_setup_cred_for_create(dentry, inode, attr->mode, old_cred);
-		if (err)
+		new_cred = ovl_setup_cred_for_create(dentry, inode, attr->mode,
+						     old_cred);
+		err = PTR_ERR(new_cred);
+		if (IS_ERR(new_cred)) {
+			new_cred = NULL;
 			goto out_revert_creds;
+		}
 	}
 
 	if (!ovl_dentry_is_whiteout(dentry))
@@ -622,6 +627,7 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 
 out_revert_creds:
 	ovl_revert_creds(old_cred);
+	put_cred(new_cred);
 	return err;
 }
 
@@ -1306,7 +1312,7 @@ static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir,
 static int ovl_create_tmpfile(struct file *file, struct dentry *dentry,
 			      struct inode *inode, umode_t mode)
 {
-	const struct cred *old_cred;
+	const struct cred *old_cred, *new_cred = NULL;
 	struct path realparentpath;
 	struct file *realfile;
 	struct dentry *newdentry;
@@ -1315,9 +1321,12 @@ static int ovl_create_tmpfile(struct file *file, struct dentry *dentry,
 	int err;
 
 	old_cred = ovl_override_creds(dentry->d_sb);
-	err = ovl_setup_cred_for_create(dentry, inode, mode, old_cred);
-	if (err)
+	new_cred = ovl_setup_cred_for_create(dentry, inode, mode, old_cred);
+	err = PTR_ERR(new_cred);
+	if (IS_ERR(new_cred)) {
+		new_cred = NULL;
 		goto out_revert_creds;
+	}
 
 	ovl_path_upper(dentry->d_parent, &realparentpath);
 	realfile = backing_tmpfile_open(&file->f_path, flags, &realparentpath,
@@ -1338,6 +1347,7 @@ static int ovl_create_tmpfile(struct file *file, struct dentry *dentry,
 	}
 out_revert_creds:
 	ovl_revert_creds(old_cred);
+	put_cred(new_cred);
 	return err;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 10 lines · Amir Goldstein

ovl_setup_cred_for_create() decrements one refcount of new creds and ovl_revert_creds() in callers decrements the last refcount.

In preparation to revert_creds_light() back to caller creds, pass an explicit reference of the creators creds to the callers and drop the refcount explicitly in the callers after ovl_revert_creds().

---

Quoted 8 lines

Miklos, Christian,

I was chasing a suspect memleak in revert_creds_light() patches. This fix is unrelated to memleak but I think it is needed for correctness anyway.

This applies in the middle of the series after adding the ovl_revert_creds() helper.

I'm going to try and reproduce the kmemleak with your ovl_creds branch as is and then retry with the series applied as is plus one small fix you correctly pointed out.

author maintainer
#2 lore raw
Quoted 18 lines · Christian Brauner
Quoted 16 lines · Amir Goldstein

ovl_setup_cred_for_create() decrements one refcount of new creds and ovl_revert_creds() in callers decrements the last refcount.

In preparation to revert_creds_light() back to caller creds, pass an explicit reference of the creators creds to the callers and drop the refcount explicitly in the callers after ovl_revert_creds().

---

Miklos, Christian,

I was chasing a suspect memleak in revert_creds_light() patches. This fix is unrelated to memleak but I think it is needed for correctness anyway.

Quoted 6 lines
Quoted 2 lines

This applies in the middle of the series after adding the ovl_revert_creds() helper.

I'm going to try and reproduce the kmemleak with your ovl_creds branch as is and then retry with the series applied as is plus one small fix you correctly pointed out.

Don't bother. The fix that Vinicius sent me was correct. I still want to use this patch, so would appreciate a review.

Thanks,
Amir.

#3 lore raw
Quoted 22 lines · Amir Goldstein
Quoted 21 lines · Christian Brauner
Quoted 18 lines · Amir Goldstein

ovl_setup_cred_for_create() decrements one refcount of new creds and ovl_revert_creds() in callers decrements the last refcount.

In preparation to revert_creds_light() back to caller creds, pass an explicit reference of the creators creds to the callers and drop the refcount explicitly in the callers after ovl_revert_creds().

---

Miklos, Christian,

I was chasing a suspect memleak in revert_creds_light() patches. This fix is unrelated to memleak but I think it is needed for correctness anyway.

This applies in the middle of the series after adding the ovl_revert_creds() helper.

Quoted 6 lines
Quoted 3 lines

I'm going to try and reproduce the kmemleak with your ovl_creds branch as is and then retry with the series applied as is plus one small fix you correctly pointed out.

Don't bother. The fix that Vinicius sent me was correct. I still want to use this patch, so would appreciate a review.

Ah, thanks for letting me know!

maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to Amir Goldstein
Quoted 10 lines · Amir Goldstein

ovl_setup_cred_for_create() decrements one refcount of new creds and ovl_revert_creds() in callers decrements the last refcount.

In preparation to revert_creds_light() back to caller creds, pass an explicit reference of the creators creds to the callers and drop the refcount explicitly in the callers after ovl_revert_creds().

---

Quoted 8 lines

Miklos, Christian,

I was chasing a suspect memleak in revert_creds_light() patches. This fix is unrelated to memleak but I think it is needed for correctness anyway.

This applies in the middle of the series after adding the ovl_revert_creds() helper.

Ok, so you're moving the put_cred() on the cred_for_create creds into the callers. Tbh, that patch alone here is very confusing because with the last patch in your tree at 07532f7f8450 you're calling

old_cred = override_creds(override_cred);

which made it buggy. But I see in 3756f22061c2 this is fixed and correctly uses

old_cred = override_creds_light(override_cred);

as expected. And together with that change your patch here makes perfect sense. I don't want to complain too much but it would've been nice if that was spelled out in the commit message. Would've spared me 30 minutes of staring at the code until I refreshed your branch. :)

author maintainer
#5 lore raw
Quoted 30 lines · Christian Brauner
Quoted 10 lines · Amir Goldstein

ovl_setup_cred_for_create() decrements one refcount of new creds and ovl_revert_creds() in callers decrements the last refcount.

In preparation to revert_creds_light() back to caller creds, pass an explicit reference of the creators creds to the callers and drop the refcount explicitly in the callers after ovl_revert_creds().

---

Quoted 8 lines

Miklos, Christian,

I was chasing a suspect memleak in revert_creds_light() patches. This fix is unrelated to memleak but I think it is needed for correctness anyway.

This applies in the middle of the series after adding the ovl_revert_creds() helper.

Ok, so you're moving the put_cred() on the cred_for_create creds into the callers. Tbh, that patch alone here is very confusing because with the last patch in your tree at 07532f7f8450 you're calling

old_cred = override_creds(override_cred);

which made it buggy. But I see in 3756f22061c2 this is fixed and correctly uses

Quoted 6 lines

old_cred = override_creds_light(override_cred);

as expected. And together with that change your patch here makes perfect sense. I don't want to complain too much but it would've been nice if that was spelled out in the commit message. Would've spared me 30 minutes of staring at the code until I refreshed your branch. :)

No, do feel free to complain :)
At the time of writing the commit message I still did not understand the bug. It just made sense to me that the reference should be passed explicitly, because I *thought* that ovl_setup_cred_for_create() was dropping the last reference after the last commit, but that wasn't going to explain a memleak (quite the opposite).

My point with *this* patch is that it was more clear to code readers who is responsible to drop the reference and after a while, this clarity also helped me to see the bug.

Quoted 2 lines

Thanks!
Amir.