Skip to content

Commit 101bf15

Browse files
committed
Merge patch series "ovl: convert copyup credential override to cred guard"
Christian Brauner <brauner@kernel.org> says: This simplifies the copyup specific credential override. The current code is centered around a helper struct ovl_cu_creds and is a bit convoluted. We can simplify this by using a cred guard. This will also allow us to remove the helper struct and associated functions. * patches from https://patch.msgid.link/20251114-work-ovl-cred-guard-copyup-v1-0-ea3fb15cf427@kernel.org: ovl: remove struct ovl_cu_creds and associated functions ovl: port ovl_copy_up_tmpfile() to cred guard ovl: mark *_cu_creds() as unused temporarily ovl: port ovl_copy_up_workdir() to cred guard ovl: add copy up credential guard Link: https://patch.msgid.link/20251114-work-ovl-cred-guard-copyup-v1-0-ea3fb15cf427@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents c0fb968 + 2c42b6c commit 101bf15

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

fs/overlayfs/copy_up.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -724,34 +724,33 @@ static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp)
724724
return err;
725725
}
726726

727-
struct ovl_cu_creds {
728-
const struct cred *old;
729-
struct cred *new;
730-
};
731-
732-
static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
727+
static const struct cred *ovl_prepare_copy_up_creds(struct dentry *dentry)
733728
{
729+
struct cred *copy_up_cred = NULL;
734730
int err;
735731

736-
cc->old = cc->new = NULL;
737-
err = security_inode_copy_up(dentry, &cc->new);
732+
err = security_inode_copy_up(dentry, &copy_up_cred);
738733
if (err < 0)
739-
return err;
734+
return ERR_PTR(err);
740735

741-
if (cc->new)
742-
cc->old = override_creds(cc->new);
736+
if (!copy_up_cred)
737+
return NULL;
743738

744-
return 0;
739+
return override_creds(copy_up_cred);
745740
}
746741

747-
static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
742+
static void ovl_revert_copy_up_creds(const struct cred *orig_cred)
748743
{
749-
if (cc->new) {
750-
revert_creds(cc->old);
751-
put_cred(cc->new);
752-
}
744+
const struct cred *copy_up_cred;
745+
746+
copy_up_cred = revert_creds(orig_cred);
747+
put_cred(copy_up_cred);
753748
}
754749

750+
DEFINE_CLASS(copy_up_creds, const struct cred *,
751+
if (!IS_ERR_OR_NULL(_T)) ovl_revert_copy_up_creds(_T),
752+
ovl_prepare_copy_up_creds(dentry), struct dentry *dentry)
753+
755754
/*
756755
* Copyup using workdir to prepare temp file. Used when copying up directories,
757756
* special files or when upper fs doesn't support O_TMPFILE.
@@ -763,7 +762,6 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
763762
struct path path = { .mnt = ovl_upper_mnt(ofs) };
764763
struct renamedata rd = {};
765764
struct dentry *temp;
766-
struct ovl_cu_creds cc;
767765
int err;
768766
struct ovl_cattr cattr = {
769767
/* Can't properly set mode on creation because of the umask */
@@ -772,14 +770,14 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
772770
.link = c->link
773771
};
774772

775-
err = ovl_prep_cu_creds(c->dentry, &cc);
776-
if (err)
777-
return err;
773+
scoped_class(copy_up_creds, copy_up_creds, c->dentry) {
774+
if (IS_ERR(copy_up_creds))
775+
return PTR_ERR(copy_up_creds);
778776

779-
ovl_start_write(c->dentry);
780-
temp = ovl_create_temp(ofs, c->workdir, &cattr);
781-
ovl_end_write(c->dentry);
782-
ovl_revert_cu_creds(&cc);
777+
ovl_start_write(c->dentry);
778+
temp = ovl_create_temp(ofs, c->workdir, &cattr);
779+
ovl_end_write(c->dentry);
780+
}
783781

784782
if (IS_ERR(temp))
785783
return PTR_ERR(temp);
@@ -857,17 +855,17 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
857855
struct inode *udir = d_inode(c->destdir);
858856
struct dentry *temp, *upper;
859857
struct file *tmpfile;
860-
struct ovl_cu_creds cc;
861858
int err;
862859

863-
err = ovl_prep_cu_creds(c->dentry, &cc);
864-
if (err)
865-
return err;
860+
scoped_class(copy_up_creds, copy_up_creds, c->dentry) {
861+
if (IS_ERR(copy_up_creds))
862+
return PTR_ERR(copy_up_creds);
863+
864+
ovl_start_write(c->dentry);
865+
tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode);
866+
ovl_end_write(c->dentry);
867+
}
866868

867-
ovl_start_write(c->dentry);
868-
tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode);
869-
ovl_end_write(c->dentry);
870-
ovl_revert_cu_creds(&cc);
871869
if (IS_ERR(tmpfile))
872870
return PTR_ERR(tmpfile);
873871

0 commit comments

Comments
 (0)