overlayfs upperdir/workdir verifications
Subsystems (3)
Amir Goldstein
maintainerv2 cover overlayfs upperdir/workdir verifications
Miklos,
This patch set is a prelude to the index dir feature, which requires origin verification w.r.t. upperdir. I will follow up with the posting of index dir feature soon.
This work prevents:
1. Concurrent overlay mounts with same upperdir/workdir
2. Reuse of upperdir with different lowerdir
3. Merge lower dir that doesn't match upper dir origin
For backward compat with existing setups that copy layers, the checks 2-3 above depend on a new mount option (-o verify_lower). The same mount option is going to determine whether an index dir that doesn't match upper dir is blown away or fails the mount.
I have another dir verification patch that decodes origin dir on failure to verify lower dir, which I am using for snapshots and is going to be needed for NFS export. Because those features are not proposed for v4.13 I left out the patch from this posting.
The verify_lower mount option can be tested with my unionmount-testsuite development branch [1]. I had to fix the mount cycling test (run --ov=N) not to reuse the same workdir with new rotated upperdir, because reusing workdir with different upperdir breaks the origin verification for the persistent index dir.
[1] https://github.com/amir73il/unionmount-testsuite/commits/overlayfs-devel
Amir Goldstein (5):
vfs: introduce inode 'inuse' lock
ovl: get exclusive ownership on upper/work dirs
ovl: add support for verify_lower mount option
ovl: verify lower root dir by file handle
ovl: document the 'verify_lower' feature
Documentation/filesystems/overlayfs.txt | 48 +++++++++++++ fs/inode.c | 50 ++++++++++++++ fs/overlayfs/copy_up.c | 16 +++-- fs/overlayfs/namei.c | 95 ++++++++++++++++++++++--- fs/overlayfs/overlayfs.h | 18 +++++ fs/overlayfs/ovl_entry.h | 4 ++ fs/overlayfs/super.c | 118 +++++++++++++++++++++++++++++++- fs/overlayfs/util.c | 7 ++ include/linux/fs.h | 15 ++++ 9 files changed, 351 insertions(+), 20 deletions(-)
Signature · 1 line
2.7.4
Amir Goldstein
maintainerPATCH v2 1/5 vfs: introduce inode 'inuse' lock
Added an i_state flag I_INUSE and helpers to set/clear/test it.
The 'inuse' lock is an 'advisory' inode lock, that can be used to extend exclusive create protection beyond parent->i_mutex lock among cooperating users.
This is going to be used by overlayfs to get exclusive ownership on upper and work dirs among overlayfs mounts.
--- fs/inode.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 15 +++++++++++++++ 2 files changed, 65 insertions(+)
fs/inode.c +50 -0
@@ -2120,3 +2120,53 @@ struct timespec current_time(struct inode *inode)return timespec_trunc (now, inode->i_sb->s_time_gran); }EXPORT_SYMBOL (current_time); + +/** +* inode_inuse_trylock - try to get an exclusive 'inuse' lock on inode +* @inode: inode being locked +* +* The 'inuse' lock is an 'advisory' lock that can be used to extend exclusive +* create protection beyond parent->i_mutex lock among cooperating users. +* Used by overlayfs to get exclusive ownership on upper and work dirs among +* overlayfs mounts. +* +* Caller must hold a reference to inode to prevent it from being freed while +* it is marked inuse. +* +* Return true if I_INUSE flag was set by this call. +*/ +bool inode_inuse_trylock (struct inode *inode) +{ +bool locked = false; + +spin_lock (&inode->i_lock); +if (!WARN_ON (!atomic_read (&inode->i_count)) && + !WARN_ON (inode->i_state & (I_NEW |I_FREEING |I_WILL_FREE )) && + !(inode->i_state &I_INUSE )) { + inode->i_state |=I_INUSE ; + locked = true; + } +spin_unlock (&inode->i_lock); +return locked; +} +EXPORT_SYMBOL (inode_inuse_trylock); + +/** +* inode_inuse_unlock - release exclusive 'inuse' lock +* @inode: inode inuse to unlock +* +* Clear the I_INUSE state and wake up any waiters. +* +* Caller must hold a reference to inode and must have successfully marked +* the inode 'inuse' prior to this call. +*/ +void inode_inuse_unlock (struct inode *inode) +{ +spin_lock (&inode->i_lock); +WARN_ON (!atomic_read (&inode->i_count)); +WARN_ON (inode->i_state & (I_NEW |I_FREEING |I_WILL_FREE )); +WARN_ON (!(inode->i_state &I_INUSE )); + inode->i_state &= ~I_INUSE ; +spin_unlock (&inode->i_lock); +} +EXPORT_SYMBOL (inode_inuse_unlock);
include/linux/fs.h +15 -0
@@ -1929,6 +1929,12 @@ static inline bool HAS_UNMAPPED_ID(struct inode *inode)* wb stat updates to grab mapping->tree_lock. See * inode_switch_wb_work_fn() for details. * +* I_INUSE An 'advisory' bit to get exclusive ownership on inode +* using inode_inuse_trylock(). It can be used to extend +* exclusive create protection beyond parent->i_mutex lock. +* Used by overlayfs to get exclusive ownership on upper +* and work dirs among overlayfs mounts. +* * Q: What is the difference between I_WILL_FREE and I_FREEING? */ #define I_DIRTY_SYNC (1 << 0)
@@ -1949,6 +1955,8 @@ static inline bool HAS_UNMAPPED_ID(struct inode *inode)#define __I_DIRTY_TIME_EXPIRED 12#define I_DIRTY_TIME_EXPIRED (1 << __I_DIRTY_TIME_EXPIRED)#define I_WB_SWITCH (1 << 13) +#define __I_INUSE 14 +#define I_INUSE (1 << __I_INUSE)#define I_DIRTY (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES)#define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
@@ -3258,5 +3266,12 @@ static inline bool dir_relax_shared(struct inode *inode)extern bool path_noexec (const struct path *path);extern void inode_nohighmem (struct inode *inode); +extern bool inode_inuse_trylock (struct inode *inode); +extern void inode_inuse_unlock (struct inode *inode); + +static inline bool inode_inuse (struct inode *inode) +{ +return inode->i_state &I_INUSE ; +}#endif /* _LINUX_FS_H */
Signature · 1 line
2.7.4
Amir Goldstein
maintainerPATCH v2 2/5 ovl: get exclusive ownership on upper/work dirs
In reply to Amir GoldsteinBad things can happen if several concurrent overlay mounts try to use the same upperdir/workdir path.
Try to get the 'inuse' advisory lock on upperdir and workdir. Fail mount if another overlay mount instance or another user holds the 'inuse' lock on these directories.
Note that this provides no protection for concurrent overlay mount that use overlapping (i.e. descendant) upper/work dirs.
--- fs/overlayfs/ovl_entry.h | 3 +++ fs/overlayfs/super.c | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-)
fs/overlayfs/ovl_entry.h +3 -0
@@ -21,6 +21,9 @@ struct ovl_fs {struct vfsmount *upper_mnt;unsigned numlower;struct vfsmount **lower_mnt; +/* workbasedir is the path at workdir= mount option */ +struct dentry *workbasedir; +/* workdir is the 'work' directory under workbasedir */ struct dentry *workdir;long namelen;/* pathnames of lower and upper dirs, for show_options */
fs/overlayfs/super.c +38 -3
92 lines not on this page · raw message
Signature · 1 line
2.7.4
Amir Goldstein
maintainerPATCH v2 3/5 ovl: add support for verify_lower mount option
In reply to Amir GoldsteinWhen overlayfs is mounted with option 'verify_lower', a directory inode found in lower layer by name or by redirect_dir is verified against the file handle of the copy up origin that is stored in the upper layer.
The 'verify_lower' option should not be used after copying layers, because the new lower directory inodes would fail verification.
Internally, 'verify_lower' is implemented as an alias to mount option 'verify_dir=<verify_mask>'. Currently, 'verify_mask' is a bitmask with a single defined flag (__OVL_VERIFY_MERGE). That bitmask is going to be extended with more flags for more directory inode verifications soon.
--- fs/overlayfs/copy_up.c | 16 +++++--- fs/overlayfs/namei.c | 95 ++++++++++++++++++++++++++++++++++++++++++------ fs/overlayfs/overlayfs.h | 15 ++++++++ fs/overlayfs/ovl_entry.h | 1 + fs/overlayfs/super.c | 34 +++++++++++++++++ fs/overlayfs/util.c | 7 ++++ 6 files changed, 151 insertions(+), 17 deletions(-)
fs/overlayfs/copy_up.c +10 -6
@@ -233,12 +233,19 @@ int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)return err; } -static struct ovl_fh *ovl_encode_fh (struct dentry *lower,uuid_be *uuid) +bool ovl_can_decode_fh (struct super_block *sb) +{ +return (sb->s_export_op && sb->s_export_op->fh_to_dentry && +uuid_be_cmp (*(uuid_be *) &sb->s_uuid,NULL_UUID_BE )); +} + +struct ovl_fh *ovl_encode_fh (struct dentry *lower) {struct ovl_fh *fh;int fh_type, fh_len, dwords;void *buf;int buflen =MAX_HANDLE_SZ ; +uuid_be *uuid = (uuid_be *) &lower->d_sb->s_uuid; buf =kmalloc (buflen,GFP_TEMPORARY );if (!buf)
@@ -283,8 +290,6 @@ static struct ovl_fh *ovl_encode_fh(struct dentry *lower, uuid_be *uuid)static int ovl_set_origin (struct dentry *dentry,struct dentry *lower,struct dentry *upper) { -struct super_block *sb = lower->d_sb; -uuid_be *uuid = (uuid_be *) &sb->s_uuid;const struct ovl_fh *fh =NULL ;int err;
@@ -293,9 +298,8 @@ static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,* so we can use the overlay.origin xattr to distignuish between a copy * up and a pure upper inode. */ -if (sb->s_export_op && sb->s_export_op->fh_to_dentry && -uuid_be_cmp (*uuid,NULL_UUID_BE )) { - fh =ovl_encode_fh (lower, uuid); +if (ovl_can_decode_fh (lower->d_sb)) { + fh =ovl_encode_fh (lower);if (IS_ERR (fh))return PTR_ERR (fh); }
fs/overlayfs/namei.c +84 -11
141 lines not on this page · raw message
fs/overlayfs/overlayfs.h +15 -0
@@ -20,6 +20,16 @@ enum ovl_path_type {#define OVL_TYPE_MERGE (type) ((type) & __OVL_PATH_MERGE)#define OVL_TYPE_ORIGIN (type) ((type) & __OVL_PATH_ORIGIN) +enum ovl_verify_dir { + __OVL_VERIFY_MERGE = (1 <<0 ), +}; + +/* Verify on lookup of merge dir that lower matches origin fh stored in upper */ +#define OVL_VERIFY_MERGE (v) ((v) & __OVL_VERIFY_MERGE) + +/* Verify flags for mount options 'verify_lower' */ +#define OVL_VERIFY_LOWER (__OVL_VERIFY_MERGE) +#define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."#define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque"#define OVL_XATTR_REDIRECT OVL_XATTR_PREFIX "redirect"
@@ -191,6 +201,7 @@ void ovl_drop_write(struct dentry *dentry);struct dentry *ovl_workdir (struct dentry *dentry);const struct cred *ovl_override_creds (struct super_block *sb);struct super_block *ovl_same_sb (struct super_block *sb); +unsigned int ovl_verify_dir (struct super_block *sb);struct ovl_entry *ovl_alloc_entry (unsigned int numlower);bool ovl_dentry_remote (struct dentry *dentry);bool ovl_dentry_weird (struct dentry *dentry);
@@ -233,6 +244,8 @@ static inline bool ovl_is_impuredir(struct dentry *dentry)/* namei.c */ +int ovl_verify_origin (struct dentry *dentry,struct vfsmount *mnt, +struct dentry *origin);int ovl_path_next (int idx,struct dentry *dentry,struct path *path,int *idxp);struct dentry *ovl_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags);bool ovl_lower_positive (struct dentry *dentry);
@@ -291,3 +304,5 @@ int ovl_copy_up(struct dentry *dentry);int ovl_copy_up_flags (struct dentry *dentry,int flags);int ovl_copy_xattr (struct dentry *old,struct dentry *new);int ovl_set_attr (struct dentry *upper,struct kstat *stat); +bool ovl_can_decode_fh (struct super_block *sb); +struct ovl_fh *ovl_encode_fh (struct dentry *lower);
fs/overlayfs/ovl_entry.h +1 -0
@@ -14,6 +14,7 @@ struct ovl_config {char *workdir;bool default_permissions;bool redirect_dir; +unsigned int verify_dir; };/* private information held for overlayfs's superblock */
fs/overlayfs/super.c +34 -0
@@ -265,6 +265,12 @@ static int ovl_show_options(struct seq_file *m, struct dentry *dentry)if (ufs->config.redirect_dir != ovl_redirect_dir_def)seq_printf (m,",redirect_dir=%s" , ufs->config.redirect_dir ?"on" :"off" ); +if (ufs->config.verify_dir) { +if (ufs->config.verify_dir ==OVL_VERIFY_LOWER ) +seq_puts (m,",verify_lower" ); +else +seq_printf (m,",verify_dir=%x" , ufs->config.verify_dir); + }return 0 ; }
@@ -294,6 +300,8 @@ enum {OPT_DEFAULT_PERMISSIONS ,OPT_REDIRECT_DIR_ON ,OPT_REDIRECT_DIR_OFF , +OPT_VERIFY_LOWER , +OPT_VERIFY_DIR , OPT_ERR, };
@@ -304,6 +312,8 @@ static const match_table_t ovl_tokens = { {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, {OPT_REDIRECT_DIR_ON, "redirect_dir=on"}, {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"}, + {OPT_VERIFY_LOWER, "verify_lower"}, + {OPT_VERIFY_DIR, "verify_dir=%u"}, {OPT_ERR, NULL} };
@@ -376,7 +386,17 @@ static int ovl_parse_opt(char *opt, struct ovl_config *config) config->redirect_dir = false;break ; +case OPT_VERIFY_LOWER : + config->verify_dir =OVL_VERIFY_LOWER ; +break ; + +case OPT_VERIFY_DIR : +if (match_hex (args, &config->verify_dir)) + goto parse_err; +break ; +default : +parse_err:pr_err ("overlayfs: unrecognized mount option \"%s\" or missing value\n" , p);return -EINVAL ; }
@@ -964,6 +984,20 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) ufs->same_sb = mnt->mnt_sb; else if (ufs->same_sb != mnt->mnt_sb) ufs->same_sb = NULL; + +/* +* The verify_lower feature is used to verify that lower dir +* found by path matches the stored copy up origin file handle. +* It requires that all layers support NFS export. +*/ +if (ufs->config.verify_dir) { + err = -EOPNOTSUPP ; +if (!ovl_can_decode_fh (mnt->mnt_sb)) { +pr_err ("overlayfs: option \"verify_lower\" not supported by lower fs.\n" ); + goto out_put_lower_mnt; + } + } + }/* If the upper fs is nonexistent, we mark overlayfs r/o too */
fs/overlayfs/util.c +7 -0
@@ -47,6 +47,13 @@ struct super_block *ovl_same_sb(struct super_block *sb)return ofs->same_sb; } +unsigned int ovl_verify_dir (struct super_block *sb) +{ +struct ovl_fs *ofs = sb->s_fs_info; + +return ofs->config.verify_dir; +} +struct ovl_entry *ovl_alloc_entry (unsigned int numlower) { size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
Signature · 1 line
2.7.4
Amir Goldstein
maintainerPATCH v2 4/5 ovl: verify lower root dir by file handle
In reply to Amir GoldsteinWith mount option 'verify_lower', verify that the file handle stored in upper root dir matches the lower root dir or fail to mount.
If upper root dir has no stored file handle, encode and store the lower root dir file handle in overlay.origin xattr.
--- fs/overlayfs/overlayfs.h | 5 ++++- fs/overlayfs/super.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-)
fs/overlayfs/overlayfs.h +4 -1
@@ -22,13 +22,16 @@ enum ovl_path_type {enum ovl_verify_dir { __OVL_VERIFY_MERGE = (1 <<0 ), + __OVL_VERIFY_ROOT = (1 <<1 ), };/* Verify on lookup of merge dir that lower matches origin fh stored in upper */ #define OVL_VERIFY_MERGE (v) ((v) & __OVL_VERIFY_MERGE) +/* Verify on mount that lower root matches origin fh stored in upper root */ +#define OVL_VERIFY_ROOT (v) ((v) & __OVL_VERIFY_ROOT)/* Verify flags for mount options 'verify_lower' */ -#define OVL_VERIFY_LOWER (__OVL_VERIFY_MERGE) +#define OVL_VERIFY_LOWER (__OVL_VERIFY_MERGE | __OVL_VERIFY_ROOT)#define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."#define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque"
fs/overlayfs/super.c +43 -0
55 lines not on this page · raw message
Signature · 1 line
2.7.4
Amir Goldstein
maintainerPATCH v2 5/5 ovl: document the 'verify_lower' feature
In reply to Amir GoldsteinThe 'verify_lower' feature provides the following:
1. Verify on mount that upper root origin matches lower root
2. Verify on lookup that upper dir origin matches lower dir
The feature is needed for upcoming overlayfs features:
- Prevent breaking hardlinks on copy up
- NFS export support
- Overlayfs snapshots
--- Documentation/filesystems/overlayfs.txt | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
Documentation/filesystems/overlayfs.txt +48 -0
@@ -201,6 +201,40 @@ rightmost one and going left. In the above example lower1 will be the top, lower2 the middle and lower3 the bottom layer. +Sharing and copying layers +-------------------------- + +Lower layers may be shared among several overlay mounts and that is indeed +a very common practice. An overlay mounts may use the same lower layer +path as another overlay mount and it may use a lower layer path that is +beneath or above the path of another overlay lower layer path. + +Using an upper layer path and/or a workdir path that are already used by +another overlay mount is not allowed and will fail with EBUSY. Using +partially overlapping paths is not allowed but will not fail with EBUSY. + +Mounting an overlay using an upper layer path, where the upper layer path +was previously used by another mounted overlay in combination with a +different lower layer path, is allowed, unless the "verify_lower" mount +option is used. + +With the "verify_lower" feature, on the first time mount, an NFS file +handle of the lower layer root directory, along with the UUID of the lower +filesystem, are encoded and stored in the "trusted.overlay.origin" extended +attribute on the upper layer root directory. On subsequent mount attempts, +the lower root directory file handle and lower filesystem UUID are compared +to the stored origin in upper root directory. On failure to verify the +lower root origin, mount will fail with ESTALE. A "verify_lower" mount +will fail with EOPNOTSUPP if the lower filesystem does not support NFS +export, lower filesystem does not have a valid UUID or if the upper +filesystem does not support extended attributes. + +It is quite a common practice to copy overlay layers to a different +directory tree on the same or different underlying filesystem, and even +to a different machine. With the "verify_lower" feature, trying to mount +the copied layers will fail the verification of the lower root file handle. + + Non-standard behavior ---------------------
@@ -228,6 +262,20 @@ filesystem are not allowed. If the underlying filesystem is changed, the behavior of the overlay is undefined, though it will not result in a crash or deadlock. +When the lower filesystem supports NFS export, overlay mount can be made +more resilient to offline and online changes of the underlying lower layer. +On every copy_up, an NFS file handle of the lower inode, along with the +UUID of the lower filesystem, are encoded and stored in an extended +attribute "trusted.overlay.origin" on the upper inode. + +With the "verify_lower" feature, a lookup of a merged directory, that found +a lower directory at the lookup path or at the path pointed to by the +"trusted.overlay.redirect" extended attribute, will verify that the found +lower directory file handle and lower filesystem UUID match the origin +that was stored at copy_up time. If a found lower directory does not match +the stored origin, that directory will be not be merged with the upper +directory. + Testsuite ---------
Signature · 1 line
2.7.4