Data-only layer mount time validations
PATCH 1/1 Data-only layer mount time validations
There seem to be a few scenarios where it is possible to successfully mount up an overlay filesystem including data-only layer(s), but in configurations where it will never be possible to read data successfully from the data-only layers. I think this should result in a mount-time error instead of the current behavior of being unable to read data from the files that should normally return data from a data-only layer.
Both cases were found by attempting to use data-only lower layers from a user namespace, a proposition that appears to be guaranteed to not end well since data-only lower layers requires use of trusted xattrs, but trusted xattrs can only be accessed in the initial user namespace.
Case 1: upper dirs in use but xattrs cannot be written to the filesystem containing workdir (for any reason, user namespace-related or not.) This triggers a fallback behavior of disabling metacopy after an existing validation in ovl_fs_params_verify ensured metacopy is on when data-only layers are present. This is now rechecked after possibly disabling metacopy.
Case 2: upper dirs are not in use, data-only layer(s) in use, mount initiated from a user namespace other than the initial one.
When the filesystem consists of only lower layers, the test of xattrs is not performed and so metacopy remains on, satisfying Case 1. Therefore it is also neceessary to explicitly check for data-only layers in a mount whose initiator lacks CAP_SYS_ADMIN in the initial user namespace.
--- fs/overlayfs/super.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
fs/overlayfs/super.c +13 -0
@@ -1394,6 +1394,19 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc)if (IS_ERR (oe)) goto out_err; +if (ofs->numdatalayer) { +if (!ofs->config.metacopy) { +pr_err ("lower data-only dirs require metacopy support.\n" ); + err = -EINVAL ; + goto out_err; + } +if (!capable (CAP_SYS_ADMIN )) { +pr_err ("lower data-only dirs require CAP_SYS_ADMIN in the initial user namespace.\n" ); + err = -EPERM ; + goto out_err; + } + } +/* If the upper fs is nonexistent, we mark overlayfs r/o too */ if (!ovl_upper_mnt (ofs)) sb->s_flags |=SB_RDONLY ;
Signature · 1 line
2.34.1
Amir Goldstein
maintainerQuoted 39 lines · Mike Baynton
There seem to be a few scenarios where it is possible to successfully mount up an overlay filesystem including data-only layer(s), but in configurations where it will never be possible to read data successfully from the data-only layers. I think this should result in a mount-time error instead of the current behavior of being unable to read data from the files that should normally return data from a data-only layer.
Both cases were found by attempting to use data-only lower layers from a user namespace, a proposition that appears to be guaranteed to not end well since data-only lower layers requires use of trusted xattrs, but trusted xattrs can only be accessed in the initial user namespace.
Case 1: upper dirs in use but xattrs cannot be written to the filesystem containing workdir (for any reason, user namespace-related or not.) This triggers a fallback behavior of disabling metacopy after an existing validation in
ovl_fs_params_verifyensured metacopy is on when data-only layers are present. This is now rechecked after possibly disabling metacopy.Case 2: upper dirs are not in use, data-only layer(s) in use, mount initiated from a user namespace other than the initial one.
When the filesystem consists of only lower layers, the test of xattrs is not performed and so metacopy remains on, satisfying Case 1. Therefore it is also neceessary to explicitly check for data-only layers in a mount whose initiator lacks
CAP_SYS_ADMINin the initial user namespace.Signed-off-by Mike Baynton <[email protected]>--- fs/overlayfs/super.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)fs/overlayfs/super.c +0
-0
fs/overlayfs/super.c int ovl_fill_super(struct super_block *sb, struct fs_context *fc) · line 1402
Quoted 9 lines
fs/overlayfs/super.c +6
-0@@ -1394,6 +1394,19 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc)if (IS_ERR (oe)) goto out_err; +if (ofs->numdatalayer) { +if (!ofs->config.metacopy) { +pr_err ("lower data-only dirs require metacopy support.\n" ); + err = -EINVAL ; + goto out_err; + }
Is that not already handled by?
int ovl_fs_params_verify(const struct ovl_fs_context *ctx,
struct ovl_config *config)
{
struct ovl_opt_set set = ctx->set;
if (ctx->nr_data > 0 && !config->metacopy) {
pr_err("lower data-only dirs require metacopy support.\n");
return -EINVAL;
}Probably because of:
ofs->config.metacopy = false;
pr_warn("...falling back to metacopy=off.\n");in xattr check, but it could also happen from:
/* Resolve userxattr -> !redirect && !metacopy && !verity dependency */
if (config->userxattr) {
...
/*
* Silently disable default setting of redirect and metacopy.
* This shall be the default in the future as well: these
* options must be explicitly enabled if used together with
* userxattr.
*/
config->redirect_mode = OVL_REDIRECT_NOFOLLOW;
config->metacopy = false;
}So maybe also the lowerdatadirs vs metacopy conflict should be moved to the end ofovl_fs_params_verify()?
fs/overlayfs/super.c int ovl_fill_super(struct super_block *sb, struct fs_context *fc) · line 1407
Quoted 5 lines
fs/overlayfs/super.c +5
-0+if (!capable (CAP_SYS_ADMIN )) { +pr_err ("lower data-only dirs require CAP_SYS_ADMIN in the initial user namespace.\n" ); + err = -EPERM ; + goto out_err; + }
This is too specific IMO.
If we really want to check CAP_SYS_ADMIN at mount time, we should error on any configuration that requires trusted xattrs and suggest that the user will use -o userxattr, and maybe disable the conflicting config if it was not explicitly specified in mount options.
Of course, userxattr conflicts with some other options including redirect_dir, metacopy and verity, but that just means that the errors will have to be smarter and the check for CAP_SYS_ADMIN should definitely be in ovl_fs_params_verify() if we add them.
Thanks,
Amir.
On 7/5/24 01:35, Amir Goldstein wrote>> ---
Quoted 30 lines
Quoted 8 lines
fs/overlayfs/super.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)fs/overlayfs/super.c +0
-0fs/overlayfs/super.c int ovl_fill_super(struct super_block *sb, struct fs_context *fc) · line 1402
Quoted 9 lines
fs/overlayfs/super.c +6
-0@@ -1394,6 +1394,19 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc)if (IS_ERR (oe)) goto out_err; +if (ofs->numdatalayer) { +if (!ofs->config.metacopy) { +pr_err ("lower data-only dirs require metacopy support.\n" ); + err = -EINVAL ; + goto out_err; + }Is that not already handled by?
int ovl_fs_params_verify(const struct
ovl_fs_context*ctx,
structovl_config*config){ struct ovl_opt_set set = ctx->set; if (ctx->nr_data > 0 && !config->metacopy) { pr_err("lower data-only dirs require metacopy support.\n"); return -EINVAL; }
Quoted 4 lines
Probably because of:
ofs->config.metacopy = false; pr_warn("...falling back to metacopy=off.\n");
Yes I think that's right.
Quoted 16 lines
in xattr check, but it could also happen from:
/* Resolve userxattr -> !redirect && !metacopy && !verity dependency */ if (config->userxattr) { ... /* * Silently disable default setting of redirect and metacopy. * This shall be the default in the future as well: these * options must be explicitly enabled if used together with * userxattr. */ config->redirect_mode = OVL_REDIRECT_NOFOLLOW; config->metacopy = false; }
Quoted 3 lines
So maybe also the lowerdatadirs vs metacopy conflict should be moved to the end of
ovl_fs_params_verify()?
Or possibly just remove it from ovl_fs_params_verify() completely and only check it once, after all adjustments to metacopy have occurred?
FWIW, I'm personally more interested in seeing the data-only layers from
a user namespace case fail at mount time than these various other
scenarios where metacopy can get switched off. I'd also be happy to confine a patch to detecting conflicts between no access to trusted.* xattrs and any of the features that needs them.
Quoted 14 lines
fs/overlayfs/super.c int ovl_fill_super(struct super_block *sb, struct fs_context *fc) · line 1407
Quoted 6 lines
fs/overlayfs/super.c +5
-0+if (!capable (CAP_SYS_ADMIN )) { +pr_err ("lower data-only dirs require CAP_SYS_ADMIN in the initial user namespace.\n" ); + err = -EPERM ; + goto out_err; + }This is too specific IMO.
If we really want to check
CAP_SYS_ADMINat mount time, we should error on any configuration that requires trusted xattrs and suggest that the user will use -o userxattr, and maybe disable the conflicting config if it was not explicitly specified in mount options.
Quoted 4 lines
Of course, userxattr conflicts with some other options including
redirect_dir, metacopy and verity, but that just means that the errors will have to be smarter and the check forCAP_SYS_ADMINshould definitely be inovl_fs_params_verify()if we add them.
This makes a lot of sense to me. Thanks for your review & suggestions.
!capable(CAP_SYS_ADMIN) is the proxy I chose for "no access to trusted.*
xattrs" but I think it's a solid one as that's how xattr_permission()
does it. I was thinking calling xattr_permission() directly on the
first lowerdir or something to test if we can access trusted.* would just complicate, but I could be wrong.
I'll take another stab at a patch in ovl_fs_params_verify() and focused on no trusted.* access + metacopy/redirect/verity in coming days.
Thanks,
Mike
PATCH v2 1/1 ovl: Fail if trusted xattrs are needed but caller lacks permission
Some overlayfs features require permission to read/write trusted.* xattrs. These include redirect_dir, verity, metacopy, and data-only layers. This patch adds additional validations at mount time to stop overlays from mounting in certain cases where the resulting mount would not function according to the user's expectations because they lack permission to access trusted.* xattrs (for example, not global root.)
Similar checks in ovl_make_workdir() that disable features instead of failing are still relevant and used in cases where the resulting mount can still work "reasonably well." Generally, if the feature was enabled through kernel config or module option, any mount that worked before will still work the same; this applies to redirect_dir and metacopy. The user must explicitly request these features in order to generate a mount failure. Verity and data-only layers on the other hand must be explictly requested and have no "reasonable" disabled or degraded alternative, so mounts attempting either always fail.
"lower data-only dirs require metacopy support" moved down in case userxattr is set, which disables metacopy.
---
v1 -> v2 not specific to data-only layers, punt on metacopy disable
due to xattr write errors creating a conflicting configuration when data-only layers are present.
fs/overlayfs/params.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)
fs/overlayfs/params.c +33 -6
@@ -782,11 +782,6 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, {struct ovl_opt_set set = ctx->set; -if (ctx->nr_data >0 && !config->metacopy) { -pr_err ("lower data-only dirs require metacopy support.\n" ); -return -EINVAL ; - } -/* Workdir/index are useless in non-upper mount */ if (!config->upperdir) {if (config->workdir) {
@@ -910,7 +905,6 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, } } -/* Resolve userxattr -> !redirect && !metacopy && !verity dependency */ if (config->userxattr) { if (set.redirect &&
@@ -938,6 +932,39 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, config->metacopy = false; } +/* +* Fail if we don't have trusted xattr capability and a feature was +* explicitly requested that requires them. +*/ +if (!config->userxattr && !capable (CAP_SYS_ADMIN )) { +if (set.redirect && + config->redirect_mode !=OVL_REDIRECT_NOFOLLOW ) { +pr_err ("redirect_dir requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (config->metacopy && set.metacopy) { +pr_err ("metacopy requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (config->verity_mode) { +pr_err ("verity requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (ctx->nr_data >0 ) { +pr_err ("lower data-only dirs require permission to access trusted xattrs\n" ); +return -EPERM ; + } +/* +* Other xattr-dependent features should be disabled without +* great disturbance to the user in ovl_make_workdir(). +*/ + } + +if (ctx->nr_data >0 && !config->metacopy) { +pr_err ("lower data-only dirs require metacopy support.\n" ); +return -EINVAL ; + } +return 0 ; }
Signature · 1 line
2.34.1
Re: [PATCH v2] ovl: Fail if trusted xattrs are needed but caller lacks permission
Quoted 18 lines · Mike Baynton
Some overlayfs features require permission to read/write trusted.* xattrs. These include
redirect_dir, verity, metacopy, and data-only layers. This patch adds additional validations at mount time to stop overlays from mounting in certain cases where the resulting mount would not function according to the user's expectations because they lack permission to access trusted.* xattrs (for example, not global root.)Similar checks in
ovl_make_workdir()that disable features instead of failing are still relevant and used in cases where the resulting mount can still work "reasonably well." Generally, if the feature was enabled through kernel config or module option, any mount that worked before will still work the same; this applies toredirect_dirand metacopy. The user must explicitly request these features in order to generate a mount failure. Verity and data-only layers on the other hand must be explictly requested and have no "reasonable" disabled or degraded alternative, so mounts attempting either always fail.
Quoted 4 lines
"lower data-only dirs require metacopy support" moved down in case userxattr is set, which disables metacopy.
Signed-off-by Mike Baynton <[email protected]>
Looks nice
Quoted 77 lines
---
v1 -> v2 not specific to data-only layers, punt on metacopy disable
due to xattr write errors creating a conflicting configuration when data-only layers are present.fs/overlayfs/params.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)fs/overlayfs/params.c +33
-7@@ -782,11 +782,6 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, {struct ovl_opt_set set = ctx->set; -if (ctx->nr_data >0 && !config->metacopy) { -pr_err ("lower data-only dirs require metacopy support.\n" ); -return -EINVAL ; - } -/* Workdir/index are useless in non-upper mount */ if (!config->upperdir) {if (config->workdir) {@@ -910,7 +905,6 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, } } -/* Resolve userxattr -> !redirect && !metacopy && !verity dependency */ if (config->userxattr) { if (set.redirect &&@@ -938,6 +932,39 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, config->metacopy = false; } +/* +* Fail if we don't have trusted xattr capability and a feature was +* explicitly requested that requires them. +*/ +if (!config->userxattr && !capable (CAP_SYS_ADMIN )) { +if (set.redirect && + config->redirect_mode !=OVL_REDIRECT_NOFOLLOW ) { +pr_err ("redirect_dir requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (config->metacopy && set.metacopy) { +pr_err ("metacopy requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (config->verity_mode) { +pr_err ("verity requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (ctx->nr_data >0 ) { +pr_err ("lower data-only dirs require permission to access trusted xattrs\n" ); +return -EPERM ; + } +/* +* Other xattr-dependent features should be disabled without +* great disturbance to the user in ovl_make_workdir(). +*/ + } + +if (ctx->nr_data >0 && !config->metacopy) { +pr_err ("lower data-only dirs require metacopy support.\n" ); +return -EINVAL ; + } +return 0 ; } --2.34.1
A message that is not in the archive (sent off-list or not yet received): [email protected]
Amir Goldstein
maintainerRe: [PATCH v2] ovl: Fail if trusted xattrs are needed but caller lacks permission
Quoted 4 lines · Mike Baynton
Hi Amir,
I apologize for my unfamiliarity with the process. Would you be so kind as to point me to the next steps for this patch?
Your next step would be to ping the maintainers ;)
Sorry, as both me and Miklos were on vacation during July, nobody picked up this patch.
I did skim over the mailing list for missed patches after my vacation, but I somehow missed it.
I will queue it up and designate it for stable v6.6+. v6.6 added overlayfs verity feature, but lower datadir was added already in v6.5.
However, 1. v6.5 is not an LTS kernel, 2. the params.c refactoring in v6.5 makes it hard to backport beyond v6.6.
Thanks,
Amir.
Quoted 117 lines
My team took a wrong turn building on data-only layers on account of my not vetting the feature inside user namespaces well enough -- I just checked "does it mount and enumerate files successfully." I'm hoping the most good that can come from that blunder is saving someone else from the same fate in future.
Regards
MikeQuoted 105 lines · Amir Goldstein
Quoted 18 lines · Mike Baynton
Some overlayfs features require permission to read/write trusted.* xattrs. These include
redirect_dir, verity, metacopy, and data-only layers. This patch adds additional validations at mount time to stop overlays from mounting in certain cases where the resulting mount would not function according to the user's expectations because they lack permission to access trusted.* xattrs (for example, not global root.)Similar checks in
ovl_make_workdir()that disable features instead of failing are still relevant and used in cases where the resulting mount can still work "reasonably well." Generally, if the feature was enabled through kernel config or module option, any mount that worked before will still work the same; this applies toredirect_dirand metacopy. The user must explicitly request these features in order to generate a mount failure. Verity and data-only layers on the other hand must be explictly requested and have no "reasonable" disabled or degraded alternative, so mounts attempting either always fail.Quoted 4 lines
"lower data-only dirs require metacopy support" moved down in case userxattr is set, which disables metacopy.
Signed-off-by Mike Baynton <[email protected]>Looks nice
Reviewed-by Amir Goldstein <[email protected]>Quoted 77 lines
---
v1 -> v2 not specific to data-only layers, punt on metacopy disable
due to xattr write errors creating a conflicting configuration when data-only layers are present.fs/overlayfs/params.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)fs/overlayfs/params.c +33
-7@@ -782,11 +782,6 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, {struct ovl_opt_set set = ctx->set; -if (ctx->nr_data >0 && !config->metacopy) { -pr_err ("lower data-only dirs require metacopy support.\n" ); -return -EINVAL ; - } -/* Workdir/index are useless in non-upper mount */ if (!config->upperdir) {if (config->workdir) {@@ -910,7 +905,6 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, } } -/* Resolve userxattr -> !redirect && !metacopy && !verity dependency */ if (config->userxattr) { if (set.redirect &&@@ -938,6 +932,39 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx, config->metacopy = false; } +/* +* Fail if we don't have trusted xattr capability and a feature was +* explicitly requested that requires them. +*/ +if (!config->userxattr && !capable (CAP_SYS_ADMIN )) { +if (set.redirect && + config->redirect_mode !=OVL_REDIRECT_NOFOLLOW ) { +pr_err ("redirect_dir requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (config->metacopy && set.metacopy) { +pr_err ("metacopy requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (config->verity_mode) { +pr_err ("verity requires permission to access trusted xattrs\n" ); +return -EPERM ; + } +if (ctx->nr_data >0 ) { +pr_err ("lower data-only dirs require permission to access trusted xattrs\n" ); +return -EPERM ; + } +/* +* Other xattr-dependent features should be disabled without +* great disturbance to the user in ovl_make_workdir(). +*/ + } + +if (ctx->nr_data >0 && !config->metacopy) { +pr_err ("lower data-only dirs require metacopy support.\n" ); +return -EINVAL ; + } +return 0 ; } --2.34.1