Handle notifications on overlayfs fake path files
Subsystems (4)
Amir Goldstein
maintainerv4 cover Handle notifications on overlayfs fake path files
Christain,
A little while ago, Jan and I realized that an unprivileged overlayfs mount could be used to avert fanotify permission events that were requested for an inode or sb on the underlying fs.
The [v1] patch set was an attempt to implement Miklos' suggestion (opt-in to query the fake path) which turned out to affet the vfs in many places, so Miklos and I agreed on a solution that will be less intrusive for vfs (opt-in to query the real path).
The [v2] patch set took the less intrusive approach to vfs, but it also tried a different approach of extending the d_real() interface, which Miklos did not like.
The [v3] patch goes back to the less intrusive approach to vfs without complicating d_real() interface, that Miklso and I agreed on during the [v1] patch set review, so hopefully everyone can be happy with it.
This v4 patch set addresses review comments from yourself and from Christoph on [v3].
Since the patches are 95% vfs, I think it is better if they are merged through the vfs tree.
I am hoping to solicit an ACK from Jan and Miklos on the minor changes in ovl and fsnotify in the 2nd patch.
Thanks,
Amir.
Changes since [v3]:
- Rename struct file_fake to backing_file
- Rename helpers to open_backing_file(), backing_file_real_path()
- Rename FMODE_FAKE_PATH to FMODE_BACKING
- Separate flag from FMODE_NOACCOUNT
- inline the fast-path branch of f_real_path()
Changes since [v2]:
- Restore the file_fake container (Miklos)
- Re-arrange the v1 helpers (Christian)
Changes since [v1]:
- Drop the file_fake container
- Leave f_path fake and special case only fsnotify
[v3] https://lore.kernel.org/linux-unionfs/[email protected]/
[v2] https://lore.kernel.org/linux-unionfs/[email protected]/
[v1] https://lore.kernel.org/linux-unionfs/[email protected]/
Amir Goldstein (2):
fs: use backing_file container for internal files with "fake" f_path
ovl: enable fsnotify events on underlying real files
fs/cachefiles/namei.c | 4 +-- fs/file_table.c | 74 +++++++++++++++++++++++++++++++++++----- fs/internal.h | 5 +-- fs/open.c | 30 +++++++++------- fs/overlayfs/file.c | 8 ++--- include/linux/fs.h | 24 ++++++++++--- include/linux/fsnotify.h | 3 +- 7 files changed, 113 insertions(+), 35 deletions(-)
Signature · 1 line
2.34.1
Amir Goldstein
maintainerPATCH v4 1/2 fs: use backing_file container for internal files with "fake" f_path
Overlayfs and cachefiles use open_with_fake_path() to allocate internal files, where overlayfs also puts a "fake" path in f_path - a path which is not on the same fs as f_inode.
Allocate a container struct backing_file for those internal files, that is used to hold the "fake" ovl path along with the real path.
--- fs/cachefiles/namei.c | 4 +-- fs/file_table.c | 74 +++++++++++++++++++++++++++++++++++++------ fs/internal.h | 5 +-- fs/open.c | 30 +++++++++++------- fs/overlayfs/file.c | 4 +-- include/linux/fs.h | 24 +++++++++++--- 6 files changed, 109 insertions(+), 32 deletions(-)
fs/cachefiles/namei.c +2 -2
@@ -560,8 +560,8 @@ static bool cachefiles_open_file(struct cachefiles_object *object,*/ path.mnt = cache->mnt; path.dentry = dentry; - file =open_with_fake_path (&path,O_RDWR |O_LARGEFILE |O_DIRECT , -d_backing_inode (dentry), cache->cache_cred); + file =open_backing_file (&path,O_RDWR |O_LARGEFILE |O_DIRECT , + &path, cache->cache_cred);if (IS_ERR (file)) { trace_cachefiles_vfs_error(object, d_backing_inode(dentry), PTR_ERR(file),
fs/file_table.c +65 -9
115 lines not on this page · raw message
fs/internal.h +3 -2
@@ -97,8 +97,9 @@ extern void chroot_fs_refs(const struct path *, const struct path *);/* * file_table.c */ -extern struct file *alloc_empty_file (int ,const struct cred *); -extern struct file *alloc_empty_file_noaccount (int ,const struct cred *); +struct file *alloc_empty_file (int flags,const struct cred *cred); +struct file *alloc_empty_file_noaccount (int flags,const struct cred *cred); +struct file *alloc_empty_backing_file (int flags,const struct cred *cred);static inline void put_file_access (struct file *file) {
fs/open.c +18 -12
@@ -1121,23 +1121,29 @@ struct file *dentry_create(const struct path *path, int flags, umode_t mode, }EXPORT_SYMBOL (dentry_create); -struct file *open_with_fake_path (const struct path *path,int flags, -struct inode *inode,const struct cred *cred) +struct file *open_backing_file (const struct path *path,int flags, +const struct path *real_path, +const struct cred *cred) { -struct file *f =alloc_empty_file_noaccount (flags, cred); -if (!IS_ERR (f)) { -int error; +struct file *f; +int error; - f->f_path = *path; - error =do_dentry_open (f, inode,NULL ); -if (error) { -fput (f); - f =ERR_PTR (error); - } + f =alloc_empty_backing_file (flags, cred); +if (IS_ERR (f)) +return f; + + f->f_path = *path; +path_get (real_path); + *backing_file_real_path (f) = *real_path; + error =do_dentry_open (f,d_inode (real_path->dentry),NULL ); +if (error) { +fput (f); + f =ERR_PTR (error); } +return f; } -EXPORT_SYMBOL (open_with_fake_path); +EXPORT_SYMBOL (open_backing_file);#define WILL_CREATE (flags) (flags & (O_CREAT | __O_TMPFILE))#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
fs/overlayfs/file.c +2 -2
@@ -61,8 +61,8 @@ static struct file *ovl_open_realfile(const struct file *file,if (!inode_owner_or_capable (real_idmap, realinode)) flags &= ~O_NOATIME ; - realfile =open_with_fake_path (&file->f_path, flags, realinode, -current_cred ()); + realfile =open_backing_file (&file->f_path, flags, realpath, +current_cred ()); }revert_creds (old_cred);
include/linux/fs.h +19 -5
@@ -171,6 +171,9 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,/* File supports non-exclusive O_DIRECT writes from multiple threads */ #define FMODE_DIO_PARALLEL_WRITE ((__force fmode_t)0x1000000) +/* File is embedded in backing_file object */ +#define FMODE_BACKING ((__force fmode_t)0x2000000) +/* File was opened by fanotify and shouldn't generate fanotify events */ #define FMODE_NONOTIFY ((__force fmode_t)0x4000000)
@@ -2349,11 +2352,22 @@ static inline struct file *file_open_root_mnt(struct vfsmount *mnt,return file_open_root (&(struct path ){.mnt = mnt, .dentry = mnt->mnt_root}, name, flags, mode); } -extern struct file *dentry_open (const struct path *,int ,const struct cred *); -extern struct file *dentry_create (const struct path *path,int flags, -umode_t mode,const struct cred *cred); -extern struct file *open_with_fake_path (const struct path *,int , -struct inode *,const struct cred *); +struct file *dentry_open (const struct path *path,int flags, +const struct cred *creds); +struct file *dentry_create (const struct path *path,int flags,umode_t mode, +const struct cred *cred); +struct file *open_backing_file (const struct path *path,int flags, +const struct path *real_path, +const struct cred *cred); +struct path *backing_file_real_path (struct file *f); +static inline const struct path *f_real_path (struct file *f) +{ +if (unlikely (f->f_mode &FMODE_BACKING )) +return backing_file_real_path (f); +else +return &f->f_path; +} +static inline struct file *file_clone_open (struct file *file) {return dentry_open (&file->f_path, file->f_flags, file->f_cred);
Signature · 1 line
2.34.1
Christian Brauner
maintainerRe: [PATCH v4 1/2] fs: use backing_file container for internal files with "fake" f_path
Quoted 67 lines · Amir Goldstein
Overlayfs and cachefiles use
open_with_fake_path()to allocate internal files, where overlayfs also puts a "fake" path inf_path- a path which is not on the same fs asf_inode.Allocate a container struct
backing_filefor those internal files, that is used to hold the "fake" ovl path along with the real path.Signed-off-by Amir Goldstein <[email protected]>--- fs/cachefiles/namei.c | 4 +-- fs/file_table.c | 74 +++++++++++++++++++++++++++++++++++++------ fs/internal.h | 5 +-- fs/open.c | 30 +++++++++++------- fs/overlayfs/file.c | 4 +-- include/linux/fs.h | 24 +++++++++++--- 6 files changed, 109 insertions(+), 32 deletions(-)fs/cachefiles/namei.c +2
-2@@ -560,8 +560,8 @@ static bool cachefiles_open_file(struct cachefiles_object *object,*/ path.mnt = cache->mnt; path.dentry = dentry; - file =open_with_fake_path (&path,O_RDWR |O_LARGEFILE |O_DIRECT , -d_backing_inode (dentry), cache->cache_cred); + file =open_backing_file (&path,O_RDWR |O_LARGEFILE |O_DIRECT , + &path, cache->cache_cred);if (IS_ERR (file)) { trace_cachefiles_vfs_error(object, d_backing_inode(dentry), PTR_ERR(file),fs/file_table.c +21
-1@@ -44,18 +44,40 @@ static struct kmem_cache *filp_cachep __read_mostly; static struct percpu_counter nr_files __cacheline_aligned_in_smp; +/* Container for backing file with optional real path */ +struct backing_file { +struct file file; +struct path real_path; +}; + +static inline struct backing_file *backing_file (struct file *f) +{ + return container_of(f, struct backing_file, file); +} + +struct path *backing_file_real_path (struct file *f) +{ +return &backing_file (f)->real_path; +} +EXPORT_SYMBOL_GPL (backing_file_real_path); +static void file_free_rcu (struct rcu_head *head) { struct file *f = container_of(head, struct file, f_rcuhead);put_cred (f->f_cred); -kmem_cache_free (filp_cachep, f); +if (unlikely (f->f_mode &FMODE_BACKING )) +kfree (backing_file (f)); +else +kmem_cache_free (filp_cachep, f);
fs/file_table.c static struct kmem_cache *filp_cachep __read_mostly; · line 81
Quoted 10 lines
fs/file_table.c +3
-1}static inline void file_free (struct file *f) {security_file_free (f); -if (!(f->f_mode &FMODE_NOACCOUNT )) +if (unlikely (f->f_mode &FMODE_BACKING )) +path_put (backing_file_real_path (f)); +else percpu_counter_dec (&nr_files);
I think this needs to be:
if (unlikely(f->f_mode & FMODE_BACKING))
path_put(backing_file_real_path(f));
if (likely(!(f->f_mode & FMODE_NOACCOUNT)))
percpu_counter_dec(&nr_files);
as we do have FMODE_NOACCOUNT without FMODE_BACKING.
No need to resend though.
Amir Goldstein
maintainerRe: [PATCH v4 1/2] fs: use backing_file container for internal files with "fake" f_path
Quoted 82 lines · Christian Brauner
Quoted 67 lines · Amir Goldstein
Overlayfs and cachefiles use
open_with_fake_path()to allocate internal files, where overlayfs also puts a "fake" path inf_path- a path which is not on the same fs asf_inode.Allocate a container struct
backing_filefor those internal files, that is used to hold the "fake" ovl path along with the real path.Signed-off-by Amir Goldstein <[email protected]>--- fs/cachefiles/namei.c | 4 +-- fs/file_table.c | 74 +++++++++++++++++++++++++++++++++++++------ fs/internal.h | 5 +-- fs/open.c | 30 +++++++++++------- fs/overlayfs/file.c | 4 +-- include/linux/fs.h | 24 +++++++++++--- 6 files changed, 109 insertions(+), 32 deletions(-)fs/cachefiles/namei.c +2
-2@@ -560,8 +560,8 @@ static bool cachefiles_open_file(struct cachefiles_object *object,*/ path.mnt = cache->mnt; path.dentry = dentry; - file =open_with_fake_path (&path,O_RDWR |O_LARGEFILE |O_DIRECT , -d_backing_inode (dentry), cache->cache_cred); + file =open_backing_file (&path,O_RDWR |O_LARGEFILE |O_DIRECT , + &path, cache->cache_cred);if (IS_ERR (file)) { trace_cachefiles_vfs_error(object, d_backing_inode(dentry), PTR_ERR(file),fs/file_table.c +21
-1@@ -44,18 +44,40 @@ static struct kmem_cache *filp_cachep __read_mostly; static struct percpu_counter nr_files __cacheline_aligned_in_smp; +/* Container for backing file with optional real path */ +struct backing_file { +struct file file; +struct path real_path; +}; + +static inline struct backing_file *backing_file (struct file *f) +{ + return container_of(f, struct backing_file, file); +} + +struct path *backing_file_real_path (struct file *f) +{ +return &backing_file (f)->real_path; +} +EXPORT_SYMBOL_GPL (backing_file_real_path); +static void file_free_rcu (struct rcu_head *head) { struct file *f = container_of(head, struct file, f_rcuhead);put_cred (f->f_cred); -kmem_cache_free (filp_cachep, f); +if (unlikely (f->f_mode &FMODE_BACKING )) +kfree (backing_file (f)); +else +kmem_cache_free (filp_cachep, f);fs/file_table.c static struct kmem_cache *filp_cachep __read_mostly; · line 81
Quoted 10 lines
fs/file_table.c +3
-1}static inline void file_free (struct file *f) {security_file_free (f); -if (!(f->f_mode &FMODE_NOACCOUNT )) +if (unlikely (f->f_mode &FMODE_BACKING )) +path_put (backing_file_real_path (f)); +else percpu_counter_dec (&nr_files);I think this needs to be:
Quoted 8 lines
if (
unlikely(f->f_mode & FMODE_BACKING))path_put(backing_file_real_path(f));if (
likely(!(f->f_mode & FMODE_NOACCOUNT)))percpu_counter_dec(&nr_files);as we do have
FMODE_NOACCOUNTwithoutFMODE_BACKING.
Ouch! good catch!
Thanks,
Amir.
Amir Goldstein
maintainerRe: [PATCH v4 1/2] fs: use backing_file container for internal files with "fake" f_path
In reply to Christian BraunerQuoted 83 lines · Christian Brauner
Quoted 67 lines · Amir Goldstein
Overlayfs and cachefiles use
open_with_fake_path()to allocate internal files, where overlayfs also puts a "fake" path inf_path- a path which is not on the same fs asf_inode.Allocate a container struct
backing_filefor those internal files, that is used to hold the "fake" ovl path along with the real path.Signed-off-by Amir Goldstein <[email protected]>--- fs/cachefiles/namei.c | 4 +-- fs/file_table.c | 74 +++++++++++++++++++++++++++++++++++++------ fs/internal.h | 5 +-- fs/open.c | 30 +++++++++++------- fs/overlayfs/file.c | 4 +-- include/linux/fs.h | 24 +++++++++++--- 6 files changed, 109 insertions(+), 32 deletions(-)fs/cachefiles/namei.c +2
-2@@ -560,8 +560,8 @@ static bool cachefiles_open_file(struct cachefiles_object *object,*/ path.mnt = cache->mnt; path.dentry = dentry; - file =open_with_fake_path (&path,O_RDWR |O_LARGEFILE |O_DIRECT , -d_backing_inode (dentry), cache->cache_cred); + file =open_backing_file (&path,O_RDWR |O_LARGEFILE |O_DIRECT , + &path, cache->cache_cred);if (IS_ERR (file)) { trace_cachefiles_vfs_error(object, d_backing_inode(dentry), PTR_ERR(file),fs/file_table.c +21
-1@@ -44,18 +44,40 @@ static struct kmem_cache *filp_cachep __read_mostly; static struct percpu_counter nr_files __cacheline_aligned_in_smp; +/* Container for backing file with optional real path */ +struct backing_file { +struct file file; +struct path real_path; +}; + +static inline struct backing_file *backing_file (struct file *f) +{ + return container_of(f, struct backing_file, file); +} + +struct path *backing_file_real_path (struct file *f) +{ +return &backing_file (f)->real_path; +} +EXPORT_SYMBOL_GPL (backing_file_real_path); +static void file_free_rcu (struct rcu_head *head) { struct file *f = container_of(head, struct file, f_rcuhead);put_cred (f->f_cred); -kmem_cache_free (filp_cachep, f); +if (unlikely (f->f_mode &FMODE_BACKING )) +kfree (backing_file (f)); +else +kmem_cache_free (filp_cachep, f);fs/file_table.c static struct kmem_cache *filp_cachep __read_mostly; · line 81
Quoted 10 lines
fs/file_table.c +3
-1}static inline void file_free (struct file *f) {security_file_free (f); -if (!(f->f_mode &FMODE_NOACCOUNT )) +if (unlikely (f->f_mode &FMODE_BACKING )) +path_put (backing_file_real_path (f)); +else percpu_counter_dec (&nr_files);I think this needs to be:
if (
unlikely(f->f_mode & FMODE_BACKING))
Quoted 8 lines
path_put(backing_file_real_path(f));if (
likely(!(f->f_mode & FMODE_NOACCOUNT)))percpu_counter_dec(&nr_files);as we do have
FMODE_NOACCOUNTwithoutFMODE_BACKING.No need to resend though.
Ay! forgot to include this in v5.
Please fix it for me again.
Thanks,
Amir.
Re: [PATCH v4 1/2] fs: use backing_file container for internal files with "fake" f_path
In reply to Amir GoldsteinQuoted 4 lines · Amir Goldstein
fs/file_table.c +4
-0+static struct file *__alloc_file (int flags,const struct cred *cred) +{ +struct file *f; +int error;
fs/file_table.c static struct file *__alloc_file(int flags, const struct cred *cred) · line 191
Quoted 10 lines
fs/file_table.c +9
-0+ + f =kmem_cache_zalloc (filp_cachep,GFP_KERNEL ); +if (unlikely (!f)) +return ERR_PTR (-ENOMEM ); + + error =init_file (f, flags, cred); +if (unlikely (error)) +return ERR_PTR (error); +return f;
Nit: is there much of a point in keeping this now very trivial helper instead of open coding it in the two callers?
Quoted 5 lines
+/*
+ * Variant ofalloc_empty_file()that allocates abacking_filecontainer
+ * and doesn't check and modifynr_files.
+ *
+ * Should not be used unless there's a very good reason to do so.
I'm not sure this comment is all that helpful.. I'd rather explain when it should be used (i.e. only from open_backing_file) then when it should not..
fs/open.c struct file *dentry_create(const struct path *path, int flags, umode_t mode, · line 1126
Quoted 5 lines
fs/open.c +3
-2-struct file *open_with_fake_path (const struct path *path,int flags, -struct inode *inode,const struct cred *cred) +struct file *open_backing_file (const struct path *path,int flags, +const struct path *real_path, +const struct cred *cred)
Please write a big fat comment on where this function should and should not be used and how it works.
include/linux/fs.h static inline struct file *file_open_root_mnt(struct vfsmount *mnt, · line 2369
Quoted 8 lines
include/linux/fs.h +8
-0+struct path *backing_file_real_path (struct file *f); +static inline const struct path *f_real_path (struct file *f) +{ +if (unlikely (f->f_mode &FMODE_BACKING )) +return backing_file_real_path (f); +else +return &f->f_path; +}
Nit: no need for the else here.
Amir Goldstein
maintainerPATCH v4 2/2 ovl: enable fsnotify events on underlying real files
In reply to Amir GoldsteinOverlayfs creates the real underlying files with fake f_path, whose f_inode is on the underlying fs and f_path on overlayfs.
Those real files were open with FMODE_NONOTIFY, because fsnotify code was not prapared to handle fsnotify hooks on files with fake path correctly and fanotify would report unexpected event->fd with fake overlayfs path, when the underlying fs was being watched.
Teach fsnotify to handle events on the real files, and do not set real files to FMODE_NONOTIFY to allow operations on real file (e.g. open, access, modify, close) to generate async and permission events.
Because fsnotify does not have notifications on address space operations, we do not need to worry about ->vm_file not reporting events to a watched overlayfs when users are accessing a mapped overlayfs file.
--- fs/overlayfs/file.c | 4 ++-- include/linux/fsnotify.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-)
fs/overlayfs/file.c +2 -2
@@ -34,8 +34,8 @@ static char ovl_whatisit(struct inode *inode, struct inode *realinode)return 'm' ; } -/* No atime modification nor notify on underlying */ -#define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY) +/* No atime modification on underlying */ +#define OVL_OPEN_FLAGS (O_NOATIME)static struct file *ovl_open_realfile (const struct file *file,const struct path *realpath)
include/linux/fsnotify.h +2 -1
@@ -91,7 +91,8 @@ static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)static inline int fsnotify_file (struct file *file,__u32 mask) { -const struct path *path = &file->f_path; +/* Overlayfs internal files have fake f_path */ +const struct path *path =f_real_path (file);if (file->f_mode &FMODE_NONOTIFY )return 0 ;
Signature · 1 line
2.34.1
Re: [PATCH v4 2/2] ovl: enable fsnotify events on underlying real files
Quoted 12 lines · Amir Goldstein
Overlayfs creates the real underlying files with fake
f_path, whosef_inodeis on the underlying fs andf_pathon overlayfs.Those real files were open with
FMODE_NONOTIFY, because fsnotify code was not prapared to handle fsnotify hooks on files with fake path correctly and fanotify would report unexpectedevent->fdwith fake overlayfs path, when the underlying fs was being watched.Teach fsnotify to handle events on the real files, and do not set real files to
FMODE_NONOTIFYto allow operations on real file (e.g. open, access, modify, close) to generate async and permission events.
Quoted 6 lines
Because fsnotify does not have notifications on address space operations, we do not need to worry about ->vm_file not reporting events to a watched overlayfs when users are accessing a mapped overlayfs file.
Signed-off-by Amir Goldstein <[email protected]>
Looks good to me. Feel free to add:
Honza
Quoted 37 lines
--- fs/overlayfs/file.c | 4 ++-- include/linux/fsnotify.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-)fs/overlayfs/file.c +2
-2@@ -34,8 +34,8 @@ static char ovl_whatisit(struct inode *inode, struct inode *realinode)return 'm' ; } -/* No atime modification nor notify on underlying */ -#define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY) +/* No atime modification on underlying */ +#define OVL_OPEN_FLAGS (O_NOATIME)static struct file *ovl_open_realfile (const struct file *file,const struct path *realpath)include/linux/fsnotify.h +2
-1@@ -91,7 +91,8 @@ static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)static inline int fsnotify_file (struct file *file,__u32 mask) { -const struct path *path = &file->f_path; +/* Overlayfs internal files have fake f_path */ +const struct path *path =f_real_path (file);if (file->f_mode &FMODE_NONOTIFY )return 0 ;Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR