Skip to contentSkip to conversation

overlayfs: clean error handling

PATCH NAK wenjun93 2 participants 2 messages May 6, 2023 – Jun 6, 2023 1 message not in the archive Overlay filesystem

lore
Share & tools
lore

wenjun93

author
#1 lore raw

PATCH v2 1/1 overlayfs: clean error handling

In reply to a message not in the archive

Remove PTR_ERR from unused code path and assign error value where it exactly happens. This cleans the code and also helps to reduce the possibility of incorrect err settings when it's set globally and is forgotten to be overwrite in custom development.

---
 fs/overlayfs/copy_up.c |  20 ++++----
 fs/overlayfs/dir.c     | 101 ++++++++++++++++++++++++-----------------
 fs/overlayfs/export.c  |  22 +++++----
 fs/overlayfs/namei.c   |  34 ++++++++------
 fs/overlayfs/readdir.c |   5 +-
 fs/overlayfs/super.c   |  80 ++++++++++++++++++--------------
 fs/overlayfs/util.c    |   2 +-
 7 files changed, 154 insertions(+), 110 deletions(-)
fs/overlayfs/copy_up.c +12 -8
index f658cc8ea492..2ec25bcacf8f 100644
@@ -507,9 +507,10 @@ static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
 		return err;
 
 	temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0));
-	err = PTR_ERR(temp);
-	if (IS_ERR(temp))
+	if (IS_ERR(temp)) {
+		err = PTR_ERR(temp);
 		goto free_name;
+	}
 
 	err = ovl_set_upper_fh(ofs, upper, temp);
 	if (err)
@@ -705,9 +706,10 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
 	};
 
 	/* workdir and destdir could be the same when copying up to indexdir */
-	err = -EIO;
-	if (lock_rename(c->workdir, c->destdir) != NULL)
+	if (lock_rename(c->workdir, c->destdir) != NULL) {
+		err = -EIO;
 		goto unlock;
+	}
 
 	err = ovl_prep_cu_creds(c->dentry, &cc);
 	if (err)
@@ -716,9 +718,10 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
 	temp = ovl_create_temp(ofs, c->workdir, &cattr);
 	ovl_revert_cu_creds(&cc);
 
-	err = PTR_ERR(temp);
-	if (IS_ERR(temp))
+	if (IS_ERR(temp)) {
+		err = PTR_ERR(temp);
 		goto unlock;
+	}
 
 	/*
 	 * Copy up data first and then xattrs. Writing data after
@@ -741,9 +744,10 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
 
 	upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
 				 c->destname.len);
-	err = PTR_ERR(upper);
-	if (IS_ERR(upper))
+	if (IS_ERR(upper)) {
+		err = PTR_ERR(upper);
 		goto cleanup;
+	}
 
 	err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0);
 	dput(upper);
fs/overlayfs/dir.c +59 -42

223 lines not on this page · raw message

fs/overlayfs/export.c +13 -9
index defd4e231ad2..00183ebf8d28 100644
@@ -379,10 +379,11 @@ static struct dentry *ovl_lookup_real_one(struct dentry *connected,
 	 * connected real path from the top.
 	 */
 	inode_lock_nested(dir, I_MUTEX_PARENT);
-	err = -ECHILD;
 	parent = dget_parent(real);
-	if (ovl_dentry_real_at(connected, layer->idx) != parent)
+	if (ovl_dentry_real_at(connected, layer->idx) != parent) {
+		err = -ECHILD;
 		goto fail;
+	}
 
 	/*
 	 * We also need to take a snapshot of real dentry name to protect us
@@ -716,9 +717,10 @@ static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
 	if (!d_is_dir(origin.dentry) ||
 	    !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
 		inode = ovl_lookup_inode(sb, origin.dentry, false);
-		err = PTR_ERR(inode);
-		if (IS_ERR(inode))
+		if (IS_ERR(inode)) {
+			err = PTR_ERR(inode);
 			goto out_err;
+		}
 		if (inode) {
 			dentry = d_find_any_alias(inode);
 			iput(inode);
@@ -730,8 +732,8 @@ static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
 	/* Then lookup indexed upper/whiteout by origin fh */
 	if (ofs->indexdir) {
 		index = ovl_get_index_fh(ofs, fh);
-		err = PTR_ERR(index);
 		if (IS_ERR(index)) {
+			err = PTR_ERR(index);
 			index = NULL;
 			goto out_err;
 		}
@@ -741,9 +743,10 @@ static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
 	if (index && d_is_dir(index)) {
 		struct dentry *upper = ovl_index_upper(ofs, index, true);
 
-		err = PTR_ERR(upper);
-		if (IS_ERR_OR_NULL(upper))
+		if (IS_ERR_OR_NULL(upper)) {
+			err = PTR_ERR(upper);
 			goto out_err;
+		}
 
 		dentry = ovl_get_dentry(sb, upper, NULL, NULL);
 		dput(upper);
@@ -810,9 +813,10 @@ static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
 	int err;
 
 	fh = ovl_fid_to_fh(fid, len, fh_type);
-	err = PTR_ERR(fh);
-	if (IS_ERR(fh))
+	if (IS_ERR(fh)) {
+		err = PTR_ERR(fh);
 		goto out_err;
+	}
 
 	err = ovl_check_fh_len(fh, len);
 	if (err)
fs/overlayfs/namei.c +20 -14
index cfb3420b7df0..665e9f19d8de 100644
@@ -462,8 +462,8 @@ int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
 	int err;
 
 	fh = ovl_encode_real_fh(ofs, real, is_upper);
-	err = PTR_ERR(fh);
 	if (IS_ERR(fh)) {
+		err = PTR_ERR(fh);
 		fh = NULL;
 		goto fail;
 	}
@@ -533,19 +533,22 @@ int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
 	if (!d_inode(index))
 		return 0;
 
-	err = -EINVAL;
-	if (index->d_name.len < sizeof(struct ovl_fb)*2)
+	if (index->d_name.len < sizeof(struct ovl_fb)*2) {
+		err = -EINVAL;
 		goto fail;
+	}
 
-	err = -ENOMEM;
 	len = index->d_name.len / 2;
 	fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
-	if (!fh)
+	if (!fh) {
+		err = -ENOMEM;
 		goto fail;
+	}
 
-	err = -EINVAL;
-	if (hex2bin(fh->buf, index->d_name.name, len))
+	if (hex2bin(fh->buf, index->d_name.name, len)) {
+		err = -EINVAL;
 		goto fail;
+	}
 
 	err = ovl_check_fb_len(&fh->fb, len);
 	if (err)
@@ -905,11 +908,12 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
 	}
 
 	if (!d.stop && poe->numlower) {
-		err = -ENOMEM;
 		stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
 				GFP_KERNEL);
-		if (!stack)
+		if (!stack) {
+			err = -ENOMEM;
 			goto out_put_upper;
+		}
 	}
 
 	for (i = 0; !d.stop && i < poe->numlower; i++) {
@@ -994,10 +998,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
 		 * Only following redirects when redirects are enabled disables
 		 * this attack vector when not necessary.
 		 */
-		err = -EPERM;
 		if (d.redirect && !ofs->config.redirect_follow) {
 			pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
 					    dentry);
+			err = -EPERM;
 			goto out_put;
 		}
 
@@ -1068,9 +1072,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
 	}
 
 	oe = ovl_alloc_entry(ctr);
-	err = -ENOMEM;
-	if (!oe)
+	if (!oe) {
+		err = -ENOMEM;
 		goto out_put;
+	}
 
 	memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
 	dentry->d_fsdata = oe;
@@ -1115,9 +1120,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
 		};
 
 		inode = ovl_get_inode(dentry->d_sb, &oip);
-		err = PTR_ERR(inode);
-		if (IS_ERR(inode))
+		if (IS_ERR(inode)) {
+			err = PTR_ERR(inode);
 			goto out_free_oe;
+		}
 		if (upperdentry && !uppermetacopy)
 			ovl_set_flag(OVL_UPPERDATA, inode);
 	}
fs/overlayfs/readdir.c +3 -2
index b6952b21a7ee..ad5363ac3a68 100644
@@ -764,9 +764,10 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)
 		struct ovl_dir_cache *cache;
 
 		cache = ovl_cache_get(dentry);
-		err = PTR_ERR(cache);
-		if (IS_ERR(cache))
+		if (IS_ERR(cache)) {
+			err = PTR_ERR(cache);
 			goto out;
+		}
 
 		od->cache = cache;
 		ovl_seek_cursor(od, ctx->pos);
fs/overlayfs/super.c +46 -34

218 lines not on this page · raw message

fs/overlayfs/util.c +1 -1
index 923d66d131c1..4b9f09a274ae 100644
@@ -851,8 +851,8 @@ static void ovl_cleanup_index(struct dentry *dentry)
 
 	inode_lock_nested(dir, I_MUTEX_PARENT);
 	index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
-	err = PTR_ERR(index);
 	if (IS_ERR(index)) {
+		err = PTR_ERR(index);
 		index = NULL;
 	} else if (ovl_index_all(dentry->d_sb)) {
 		/* Whiteout orphan index to block future open by handle */
Signature · 1 line
2.27.0
maintainer NAK
#2 lore raw
Quoted 9 lines · On Sat, 6 May 2023 at 17:09, wenjun93 <[email protected]> wrote:

Remove PTR_ERR from unused code path and assign error value where it exactly happens. This cleans the code and also helps to reduce the possibility of incorrect err settings when it's set globally and is forgotten to be overwrite in custom development.

Nack.

This is a regularly used pattern across the kernel to unclutter error handling.

If you find a concrete bug, please submit a fix for that.

Thanks,
Miklos