[PATCH v2 09/16] overlayfs/dir: Convert to cred_guard()

From: Vinicius Costa Gomes
Date: Wed Aug 21 2024 - 21:32:10 EST


Replace the override_creds_light()/revert_creds_light() pairs of
operations with cred_guard()/cred_scoped_guard().

In ovl_do_remove(), cred_scoped_guard() was used because mixing
cred_guard() with 'goto' can cause the cleanup part of the guard to
run with garbage values.

Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@xxxxxxxxx>
---
fs/overlayfs/dir.c | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/fs/overlayfs/dir.c b/fs/overlayfs/dir.c
index 52021e56b235..28ea6bc0a298 100644
--- a/fs/overlayfs/dir.c
+++ b/fs/overlayfs/dir.c
@@ -698,12 +698,10 @@ static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir,

static int ovl_set_link_redirect(struct dentry *dentry)
{
- const struct cred *old_cred;
int err;

- old_cred = ovl_override_creds_light(dentry->d_sb);
+ cred_guard(ovl_creds(dentry->d_sb));
err = ovl_set_redirect(dentry, false);
- revert_creds_light(old_cred);

return err;
}
@@ -889,7 +887,6 @@ static void ovl_drop_nlink(struct dentry *dentry)
static int ovl_do_remove(struct dentry *dentry, bool is_dir)
{
int err;
- const struct cred *old_cred;
bool lower_positive = ovl_lower_positive(dentry);
LIST_HEAD(list);

@@ -908,12 +905,12 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir)
if (err)
goto out;

- old_cred = ovl_override_creds_light(dentry->d_sb);
- if (!lower_positive)
- err = ovl_remove_upper(dentry, is_dir, &list);
- else
- err = ovl_remove_and_whiteout(dentry, &list);
- revert_creds_light(old_cred);
+ cred_scoped_guard(ovl_creds(dentry->d_sb)) {
+ if (!lower_positive)
+ err = ovl_remove_upper(dentry, is_dir, &list);
+ else
+ err = ovl_remove_and_whiteout(dentry, &list);
+ }
if (!err) {
if (is_dir)
clear_nlink(dentry->d_inode);
--
2.46.0