ovl: add override_creds mount option
Subsystems (3)
Christian Brauner
maintainercover ovl: add override_creds mount option
Hey,
Currently overlayfs uses the mounter's credentials for it's override_creds() calls. That provides a consistent permission model.
This patches allows a caller to instruct overlayfs to use its credentials instead. The caller must be located in the same user namespace as the user namespace the overlayfs instance will be mounted in. This provides a consistent and simple security model.
With this it is possible to e.g., mount an overlayfs instance where the mounter must have CAP_SYS_ADMIN but the credentials used for override_creds() have dropped CAP_SYS_ADMIN. It also allows the usage of custom fs{g,u}id different from the callers and other tweaks.
I'm marking this as RFC since I've written this down pretty quickly and I'm not sure I've thought enough about all the possible pitfalls. I think overall the concept is sound but there might be additional changes needed in ovl_fill_super(). Right now I'm just calling override_creds() when creating the index and work directories.
Thanks!
Christian
---
Christian Brauner (2):
ovl: allow to specify override credentials selftests/ovl: add selftests for "override_creds"
fs/overlayfs/ovl_entry.h | 1 + fs/overlayfs/params.c | 25 +++++ fs/overlayfs/super.c | 13 ++- .../filesystems/overlayfs/set_layers_via_fds.c | 109 +++++++++++++++++++++ 4 files changed, 147 insertions(+), 1 deletion(-)
---
base-commit: 7a54947e727b6df840780a66c970395ed9734ebe
change-id: 20250214-work-overlayfs-dfcfc4cd7ebd
Christian Brauner
maintainerRFC PATCH 1/2 ovl: allow to specify override credentials
Currently overlayfs uses the mounter's credentials for it's override_creds() calls. That provides a consistent permission model.
This patches allows a caller to instruct overlayfs to use its credentials instead. The caller must be located in the same user namespace as the user namespace the overlayfs instance will be mounted in. This provides a consistent and simple security model.
With this it is possible to e.g., mount an overlayfs instance where the mounter must have CAP_SYS_ADMIN but the credentials used for override_creds() have dropped CAP_SYS_ADMIN. It also allows the usage of custom fs{g,u}id different from the callers and other tweaks.
--- fs/overlayfs/ovl_entry.h | 1 + fs/overlayfs/params.c | 25 +++++++++++++++++++++++++ fs/overlayfs/super.c | 13 ++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-)
fs/overlayfs/ovl_entry.h +1 -0
@@ -19,6 +19,7 @@ struct ovl_config {bool metacopy;bool userxattr;bool ovl_volatile; +bool ovl_credentials; };struct ovl_sb {
fs/overlayfs/params.c +25 -0
@@ -59,6 +59,7 @@ enum ovl_opt { Opt_metacopy, Opt_verity, Opt_volatile, + Opt_override_creds, }; static const struct constant_table ovl_parameter_bool[] = {
@@ -155,6 +156,7 @@ const struct fs_parameter_spec ovl_parameter_spec[] = {fsparam_enum ("metacopy" , Opt_metacopy, ovl_parameter_bool),fsparam_enum ("verity" , Opt_verity, ovl_parameter_verity),fsparam_flag ("volatile" , Opt_volatile), + fsparam_flag_no("override_creds", Opt_override_creds), {} };
@@ -662,6 +664,27 @@ static int ovl_parse_param(struct fs_context *fc, struct fs_parameter *param)case Opt_userxattr: config->userxattr = true;break ; +case Opt_override_creds: { +const struct cred *cred = ofs->creator_cred; + +if (!result.negated) { +if (fc->user_ns !=current_user_ns ()) { + err = -EINVAL ; +break ; + } + + ofs->creator_cred =prepare_creds (); +if (!ofs->creator_cred) + err = -EINVAL ; +else + config->ovl_credentials = true; + }else { + ofs->creator_cred =NULL ; + config->ovl_credentials = false; + } +put_cred (cred); +break ; + }default :pr_err ("unrecognized mount option \"%s\" or missing value\n" , param->key);
@@ -1071,5 +1094,7 @@ int ovl_show_options(struct seq_file *m, struct dentry *dentry)if (ofs->config.verity_mode !=ovl_verity_mode_def ())seq_printf (m,",verity=%s" ,ovl_verity_mode (&ofs->config)); +if (ofs->config.ovl_credentials) +seq_puts (m,",override_creds" );return 0 ; }
fs/overlayfs/super.c +12 -1
@@ -654,6 +654,7 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, const struct path *workpath) {struct vfsmount *mnt =ovl_upper_mnt (ofs); +const struct cred *old_cred;struct dentry *workdir;struct file *tmpfile;bool rename_whiteout;
@@ -665,6 +666,8 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,if (err)return err; + old_cred =ovl_override_creds (sb); + workdir =ovl_workdir_create (ofs,OVL_WORKDIR_NAME , false); err =PTR_ERR (workdir);if (IS_ERR_OR_NULL (workdir))
@@ -788,6 +791,7 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, ofs->config.nfs_export = false; } out: +ovl_revert_creds (old_cred);mnt_drop_write (mnt);return err; }
@@ -830,6 +834,7 @@ static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, struct ovl_entry *oe, const struct path *upperpath) { struct vfsmount *mnt = ovl_upper_mnt(ofs); +const struct cred *old_cred;struct dentry *indexdir;struct dentry *origin =ovl_lowerstack (oe)->dentry;const struct ovl_fh *fh;
@@ -843,6 +848,8 @@ static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,if (err) goto out_free_fh; + old_cred =ovl_override_creds (sb); +/* Verify lower root is upper root origin */ err =ovl_verify_origin_fh (ofs, upperpath->dentry, fh, true);if (err) {
@@ -893,6 +900,7 @@ static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,pr_warn ("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n" ); out: +ovl_revert_creds (old_cred);mnt_drop_write (mnt); out_free_fh:kfree (fh);
@@ -1318,7 +1326,10 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_d_op = &ovl_dentry_operations; err = -ENOMEM ; - ofs->creator_cred = cred =prepare_creds (); +if (!ofs->creator_cred) + ofs->creator_cred = cred =prepare_creds (); +else + cred = (struct cred *)ofs->creator_cred;if (!cred) goto out_err;
Signature · 1 line
2.47.2
Amir Goldstein
maintainerRe: [PATCH RFC 1/2] ovl: allow to specify override credentials
Quoted 26 lines · Christian Brauner
Currently overlayfs uses the mounter's credentials for it's
override_creds()calls. That provides a consistent permission model.This patches allows a caller to instruct overlayfs to use its credentials instead. The caller must be located in the same user namespace as the user namespace the overlayfs instance will be mounted in. This provides a consistent and simple security model.
With this it is possible to e.g., mount an overlayfs instance where the mounter must have
CAP_SYS_ADMINbut the credentials used foroverride_creds()have droppedCAP_SYS_ADMIN. It also allows the usage of custom fs{g,u}id different from the callers and other tweaks.Signed-off-by Christian Brauner <[email protected]>--- fs/overlayfs/ovl_entry.h | 1 + fs/overlayfs/params.c | 25 +++++++++++++++++++++++++ fs/overlayfs/super.c | 13 ++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-)fs/overlayfs/ovl_entry.h +0
-0
fs/overlayfs/ovl_entry.h struct ovl_config { · line 22
Quoted 4 lines
fs/overlayfs/ovl_entry.h +1
-0@@ -19,6 +19,7 @@ struct ovl_config {bool metacopy;bool userxattr;bool ovl_volatile; +bool ovl_credentials;
IMO this is not a configuration...
Quoted 52 lines
fs/overlayfs/ovl_entry.h +0
-0};struct ovl_sb {fs/overlayfs/params.c +23
-0@@ -59,6 +59,7 @@ enum ovl_opt { Opt_metacopy, Opt_verity, Opt_volatile, + Opt_override_creds, }; static const struct constant_table ovl_parameter_bool[] = {@@ -155,6 +156,7 @@ const struct fs_parameter_spec ovl_parameter_spec[] = {fsparam_enum ("metacopy" , Opt_metacopy, ovl_parameter_bool),fsparam_enum ("verity" , Opt_verity, ovl_parameter_verity),fsparam_flag ("volatile" , Opt_volatile), + fsparam_flag_no("override_creds", Opt_override_creds), {} };@@ -662,6 +664,27 @@ static int ovl_parse_param(struct fs_context *fc, struct fs_parameter *param)case Opt_userxattr: config->userxattr = true;break ; +case Opt_override_creds: { +const struct cred *cred = ofs->creator_cred; + +if (!result.negated) { +if (fc->user_ns !=current_user_ns ()) { + err = -EINVAL ; +break ; + } + + ofs->creator_cred =prepare_creds (); +if (!ofs->creator_cred) + err = -EINVAL ; +else + config->ovl_credentials = true; + }else { + ofs->creator_cred =NULL ; + config->ovl_credentials = false; + } +put_cred (cred); +break ; + }default :pr_err ("unrecognized mount option \"%s\" or missing value\n" , param->key);
fs/overlayfs/params.c int ovl_show_options(struct seq_file *m, struct dentry *dentry) · line 1098
Quoted 5 lines
fs/overlayfs/params.c +2
-0@@ -1071,5 +1094,7 @@ int ovl_show_options(struct seq_file *m, struct dentry *dentry)if (ofs->config.verity_mode !=ovl_verity_mode_def ())seq_printf (m,",verity=%s" ,ovl_verity_mode (&ofs->config)); +if (ofs->config.ovl_credentials) +seq_puts (m,",override_creds" );
...and there is no meaning to showing it in mount options, because this is not a replayable configuration.
If you strap ",override_creds" to a normal shell command line mount it means nothing and showing this option does not carry any information about how (in which context) it was executed.
I am willing to be convinced otherwise.
Quoted 58 lines
fs/overlayfs/params.c +0
-0return 0 ; }fs/overlayfs/super.c +8
-0@@ -654,6 +654,7 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, const struct path *workpath) {struct vfsmount *mnt =ovl_upper_mnt (ofs); +const struct cred *old_cred;struct dentry *workdir;struct file *tmpfile;bool rename_whiteout;@@ -665,6 +666,8 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,if (err)return err; + old_cred =ovl_override_creds (sb); + workdir =ovl_workdir_create (ofs,OVL_WORKDIR_NAME , false); err =PTR_ERR (workdir);if (IS_ERR_OR_NULL (workdir))@@ -788,6 +791,7 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, ofs->config.nfs_export = false; } out: +ovl_revert_creds (old_cred);mnt_drop_write (mnt);return err; }@@ -830,6 +834,7 @@ static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, struct ovl_entry *oe, const struct path *upperpath) { struct vfsmount *mnt = ovl_upper_mnt(ofs); +const struct cred *old_cred;struct dentry *indexdir;struct dentry *origin =ovl_lowerstack (oe)->dentry;const struct ovl_fh *fh;@@ -843,6 +848,8 @@ static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,if (err) goto out_free_fh; + old_cred =ovl_override_creds (sb); +/* Verify lower root is upper root origin */ err =ovl_verify_origin_fh (ofs, upperpath->dentry, fh, true);if (err) {@@ -893,6 +900,7 @@ static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,pr_warn ("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n" ); out: +ovl_revert_creds (old_cred);mnt_drop_write (mnt); out_free_fh:kfree (fh);@@ -1318,7 +1326,10 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_d_op = &ovl_dentry_operations;
fs/overlayfs/super.c int ovl_fill_super(struct super_block *sb, struct fs_context *fc) · line 1335
Quoted 10 lines
fs/overlayfs/super.c +4
-1err = -ENOMEM ; - ofs->creator_cred = cred =prepare_creds (); +if (!ofs->creator_cred) + ofs->creator_cred = cred =prepare_creds (); +else + cred = (struct cred *)ofs->creator_cred;if (!cred) goto out_err;
Is there any reason not to scope the rest of ovl_fill_super() with the alternative override_creds instead of scoping helper functions?
If there is a reason then I do not see what it is.
My opinion is that it does not change the permission model if we switch one mounter_creds with another, as long as we execute all operations on underlying layers with a consistent set of creds (give or take CAP_SYS_RESOURCE).
If we scope the entire ovl_fill_super() with the override_creds used later on, then we get this consistent property.
I should note that before the new mount API, it was obvious that all operations on underlying layers are performed with "mounter creds". with new API, "mounter creds" are really "fsconfig setup creds", and kern_path() doing lookup when parsing string *dir= mount options may be executed with different creds than fsconfig setup creds.
So the way I see it, the override_creds option is more of a command, which allows better control over the single consistent set of ovl creds.
Thanks,
Amir.
Christian Brauner
maintainerRFC PATCH 2/2 selftests/ovl: add selftests for "override_creds"
In reply to Christian BraunerAdd a simple test to verify that the new "override_creds" option works.
--- .../filesystems/overlayfs/set_layers_via_fds.c | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+)
tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c +109 -0
@@ -6,11 +6,13 @@#include <sched.h> #include <stdio.h> #include <string.h> +#include <sys/fsuid.h> #include <sys/stat.h> #include <sys/mount.h> #include <unistd.h> #include "../../kselftest_harness.h" +#include "../../pidfd/pidfd.h" #include "log.h" #include "wrappers.h"
@@ -409,4 +411,111 @@ TEST_F(set_layers_via_fds, set_layers_via_detached_mount_fds)ASSERT_EQ (fclose (f_mountinfo),0 ); } +TEST_F(set_layers_via_fds, set_override_creds) +{ +int fd_context, fd_tmpfs, fd_overlay; +int layer_fds[] = { [0 ...3 ] = -EBADF }; +bool found = false;; +size_t len =0 ; +char *line =NULL ; +FILE *f_mountinfo; +pid_t pid; +int pidfd; + +ASSERT_EQ (unshare (CLONE_NEWNS ),0 ); +ASSERT_EQ (sys_mount (NULL ,"/" ,NULL ,MS_SLAVE |MS_REC ,NULL ),0 ); + + fd_context =sys_fsopen ("tmpfs" ,0 ); +ASSERT_GE (fd_context,0 ); + +ASSERT_EQ (sys_fsconfig (fd_context,FSCONFIG_CMD_CREATE ,NULL ,NULL ,0 ),0 ); + fd_tmpfs =sys_fsmount (fd_context,0 ,0 ); +ASSERT_GE (fd_tmpfs,0 ); +ASSERT_EQ (close (fd_context),0 ); + +ASSERT_EQ (mkdirat (fd_tmpfs,"w" ,0755 ),0 ); +ASSERT_EQ (mkdirat (fd_tmpfs,"u" ,0755 ),0 ); +ASSERT_EQ (mkdirat (fd_tmpfs,"l1" ,0755 ),0 ); +ASSERT_EQ (mkdirat (fd_tmpfs,"l2" ,0755 ),0 ); + + layer_fds[0 ] =openat (fd_tmpfs,"w" ,O_DIRECTORY ); +ASSERT_GE (layer_fds[0 ],0 ); + + layer_fds[1 ] =openat (fd_tmpfs,"u" ,O_DIRECTORY ); +ASSERT_GE (layer_fds[1 ],0 ); + + layer_fds[2 ] =openat (fd_tmpfs,"l1" ,O_DIRECTORY ); +ASSERT_GE (layer_fds[2 ],0 ); + + layer_fds[3 ] =openat (fd_tmpfs,"l2" ,O_DIRECTORY ); +ASSERT_GE (layer_fds[3 ],0 ); + +ASSERT_EQ (sys_move_mount (fd_tmpfs,"" , -EBADF ,"/tmp" ,MOVE_MOUNT_F_EMPTY_PATH ),0 ); +ASSERT_EQ (close (fd_tmpfs),0 ); + + fd_context =sys_fsopen ("overlay" ,0 ); +ASSERT_GE (fd_context,0 ); + +ASSERT_NE (sys_fsconfig (fd_context,FSCONFIG_SET_FD ,"lowerdir" ,NULL , layer_fds[2 ]),0 ); + +ASSERT_EQ (sys_fsconfig (fd_context,FSCONFIG_SET_FD ,"workdir" ,NULL , layer_fds[0 ]),0 ); +ASSERT_EQ (sys_fsconfig (fd_context,FSCONFIG_SET_FD ,"upperdir" ,NULL , layer_fds[1 ]),0 ); +ASSERT_EQ (sys_fsconfig (fd_context,FSCONFIG_SET_FD ,"lowerdir+" ,NULL , layer_fds[2 ]),0 ); +ASSERT_EQ (sys_fsconfig (fd_context,FSCONFIG_SET_FD ,"lowerdir+" ,NULL , layer_fds[3 ]),0 ); + +ASSERT_EQ (sys_fsconfig (fd_context,FSCONFIG_SET_STRING ,"metacopy" ,"on" ,0 ),0 ); + + pid =create_child (&pidfd,CLONE_NEWUSER ); +EXPECT_GE (pid,0 ); +if (pid ==0 ) { +if (!sys_fsconfig (fd_context,FSCONFIG_SET_FLAG ,"override_creds" ,NULL ,0 )) { +TH_LOG ("sys_fsconfig should have failed" ); +_exit (EXIT_FAILURE ); + } + +_exit (EXIT_SUCCESS ); + } +EXPECT_EQ (sys_waitid (P_PID , pid,NULL ,WEXITED ),0 ); +EXPECT_EQ (close (pidfd),0 ); + + pid =create_child (&pidfd,0 ); +EXPECT_GE (pid,0 ); +if (pid ==0 ) { +if (sys_fsconfig (fd_context,FSCONFIG_SET_FLAG ,"override_creds" ,NULL ,0 )) { +TH_LOG ("sys_fsconfig should have succeeded" ); +_exit (EXIT_FAILURE ); + } + +_exit (EXIT_SUCCESS ); + } +EXPECT_EQ (sys_waitid (P_PID , pid,NULL ,WEXITED ),0 ); +EXPECT_EQ (close (pidfd),0 ); + +ASSERT_EQ (sys_fsconfig (fd_context,FSCONFIG_CMD_CREATE ,NULL ,NULL ,0 ),0 ); + + fd_overlay =sys_fsmount (fd_context,0 ,0 ); +ASSERT_GE (fd_overlay,0 ); + +ASSERT_EQ (sys_move_mount (fd_overlay,"" , -EBADF ,"/set_layers_via_fds" ,MOVE_MOUNT_F_EMPTY_PATH ),0 ); + + f_mountinfo =fopen ("/proc/self/mountinfo" ,"r" ); +ASSERT_NE (f_mountinfo,NULL ); + +while (getline (&line, &len, f_mountinfo) !=-1 ) { +char *haystack = line; + +if (strstr (haystack,"override_creds" )) { + found = true; +break ; + } + } +free (line); + +ASSERT_EQ (found, true); + +ASSERT_EQ (close (fd_context),0 ); +ASSERT_EQ (close (fd_overlay),0 ); +ASSERT_EQ (fclose (f_mountinfo),0 ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.47.2