fanotify: support watching filesystems and mounts inside userns
Subsystems (4)
Amir Goldstein
reviewerRFC PATCH 1/1 fanotify: support watching filesystems and mounts inside userns
An unprivileged user is allowed to create an fanotify group and add inode marks, but not filesystem and mount marks.
Add limited support for setting up filesystem and mount marks by an unprivileged user under the following conditions:
1. User has CAP_SYS_ADMIN in the user ns where the group was created
2.a. User has CAP_SYS_ADMIN in the user ns where the filesystem was
mounted (implies FS_USERNS_MOUNT)
OR (in case setting up a mark mount)
2.b. User has CAP_SYS_ADMIN in the user ns attached to an idmapped mount
---
Jan,
This patch is from a WIP followup to unpriv fanotify two years ago [1].
I realize that I never posted it, partly because we wanted to let the dust settle on upriv fanotify first.
Reading back the thread, it mostly revolved around trying to leverage idmapped mounts to implement subtree watches, which didn't converge to a workable solution.
Besides that there was the minor detail that userns admin cannot use open_by_file_handle() to resolve the path from fids, but that's not realy a show stopper from using fanotify sb/mount marks within userns.
If there were any other concerns regarding this patch, I did not find them in the thread.
A recent request from Christian to watch (within user ns) when a mount is being unmounted brought this patch back to mind, but note that this patch is complementary to FAN_UNMOUNT patches [2] and would be useful regardless on FAN_UNMOUNT patches.
I've only tested it manually, because LTP does not (yet) have good helpers to setup userns with its own mount ns nor idmapped mounts.
Christian,
You can find this patch, along with FAN_UNMOUNT patches on my github [3]. Please confirm that this meets your needs for watching container mounts.
Thanks,
Amir.
[1] https://lore.kernel.org/linux-fsdevel/CAOQ4uxhfx012GtvXMfiaHSk1M7+gTqkz3LsT0i_cHLnZLMk8nw@mail.gmail.com/
[2] https://lore.kernel.org/linux-fsdevel/[email protected]/
[3] https://github.com/amir73il/linux/commits/fan_unmount
fs/notify/fanotify/fanotify.c | 1 + fs/notify/fanotify/fanotify_user.c | 58 +++++++++++++++++------------- include/linux/fsnotify_backend.h | 1 + 3 files changed, 36 insertions(+), 24 deletions(-)
fs/notify/fanotify/fanotify.c +1 -0
@@ -1040,6 +1040,7 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,static void fanotify_free_group_priv (struct fsnotify_group *group) { +put_user_ns (group->user_ns);kfree (group->fanotify_data.merge_hash);if (group->fanotify_data.ucounts) dec_ucount(group->fanotify_data.ucounts,
fs/notify/fanotify/fanotify_user.c +34 -24
114 lines not on this page · raw message
include/linux/fsnotify_backend.h +1 -0
@@ -230,6 +230,7 @@ struct fsnotify_group {* full */ struct mem_cgroup *memcg;/* memcg to charge allocations */ +struct user_namespace *user_ns;/* user ns where group was created */ /* groups can define private fields here or use the void *private */ union {
Signature · 1 line
2.34.1
Jan Kara
maintainerQuoted 6 lines · Amir Goldstein
An unprivileged user is allowed to create an fanotify group and add inode marks, but not filesystem and mount marks.
Add limited support for setting up filesystem and mount marks by an unprivileged user under the following conditions:
Quoted 7 lines
1. User has
CAP_SYS_ADMINin the user ns where the group was created
2.a. User hasCAP_SYS_ADMINin the user ns where the filesystem was
mounted (impliesFS_USERNS_MOUNT)
OR (in case setting up a mark mount)
2.b. User hasCAP_SYS_ADMINin the user ns attached to an idmapped mountSigned-off-by Amir Goldstein <[email protected]>
The patch looks good to me. Just two comments below.
Quoted 6 lines
Christian,
You can find this patch, along with
FAN_UNMOUNTpatches on my github [3]. Please confirm that this meets your needs for watching container mounts.
Yeah, it would be good to get ack from Christian that the model you propose works for him.
Quoted 12 lines
fs/notify/fanotify/fanotify_user.c +1
-0@@ -1238,6 +1238,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,* A group with FAN_UNLIMITED_MARKS does not contribute to mark count * in the limited groups account. */ +BUILD_BUG_ON (!(FANOTIFY_ADMIN_INIT_FLAGS &FAN_UNLIMITED_MARKS ));if (!FAN_GROUP_FLAG (group,FAN_UNLIMITED_MARKS ) && !inc_ucount (ucounts->ns, ucounts->uid,UCOUNT_FANOTIFY_MARKS ))return ERR_PTR (-ENOSPC );
...
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +1
-3@@ -1557,21 +1559,13 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) goto out_destroy_group; } +BUILD_BUG_ON (!(FANOTIFY_ADMIN_INIT_FLAGS &FAN_UNLIMITED_QUEUE ));if (flags &FAN_UNLIMITED_QUEUE ) { - fd = -EPERM ; -if (!capable (CAP_SYS_ADMIN )) - goto out_destroy_group; group->max_events =UINT_MAX ;
fs/notify/fanotify/fanotify_user.c SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) · line 1574
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +0
-6}else { group->max_events = fanotify_max_queued_events; } -if (flags &FAN_UNLIMITED_MARKS ) { - fd = -EPERM ; -if (!capable (CAP_SYS_ADMIN )) - goto out_destroy_group; - } -
Perhaps this hunk (plus the BUILD_BUG_ON hunk above) should go into a separate patch with a proper changelog? I was scratching my head over it for a while until I've realized it's unrelated cleanup of dead code.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerQuoted 55 lines · Jan Kara
Quoted 6 lines · Amir Goldstein
An unprivileged user is allowed to create an fanotify group and add inode marks, but not filesystem and mount marks.
Add limited support for setting up filesystem and mount marks by an unprivileged user under the following conditions:
Quoted 7 lines
1. User has
CAP_SYS_ADMINin the user ns where the group was created
2.a. User hasCAP_SYS_ADMINin the user ns where the filesystem was
mounted (impliesFS_USERNS_MOUNT)
OR (in case setting up a mark mount)
2.b. User hasCAP_SYS_ADMINin the user ns attached to an idmapped mountSigned-off-by Amir Goldstein <[email protected]>The patch looks good to me. Just two comments below.
Quoted 6 lines
Christian,
You can find this patch, along with
FAN_UNMOUNTpatches on my github [3]. Please confirm that this meets your needs for watching container mounts.Yeah, it would be good to get ack from Christian that the model you propose works for him.
Quoted 12 lines
fs/notify/fanotify/fanotify_user.c +1
-0@@ -1238,6 +1238,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,* A group with FAN_UNLIMITED_MARKS does not contribute to mark count * in the limited groups account. */ +BUILD_BUG_ON (!(FANOTIFY_ADMIN_INIT_FLAGS &FAN_UNLIMITED_MARKS ));if (!FAN_GROUP_FLAG (group,FAN_UNLIMITED_MARKS ) && !inc_ucount (ucounts->ns, ucounts->uid,UCOUNT_FANOTIFY_MARKS ))return ERR_PTR (-ENOSPC );...
Quoted 14 lines
fs/notify/fanotify/fanotify_user.c +1
-3@@ -1557,21 +1559,13 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) goto out_destroy_group; } +BUILD_BUG_ON (!(FANOTIFY_ADMIN_INIT_FLAGS &FAN_UNLIMITED_QUEUE ));if (flags &FAN_UNLIMITED_QUEUE ) { - fd = -EPERM ; -if (!capable (CAP_SYS_ADMIN )) - goto out_destroy_group; group->max_events =UINT_MAX ; }else { group->max_events = fanotify_max_queued_events; }
Quoted 11 lines
fs/notify/fanotify/fanotify_user.c SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) · line 1574
Quoted 6 lines
fs/notify/fanotify/fanotify_user.c +0
-6-if (flags &FAN_UNLIMITED_MARKS ) { - fd = -EPERM ; -if (!capable (CAP_SYS_ADMIN )) - goto out_destroy_group; - } -Perhaps this hunk (plus the
BUILD_BUG_ONhunk above) should go into a separate patch with a proper changelog? I was scratching my head over it for a while until I've realized it's unrelated cleanup of dead code.
Sure. It took me a while to understand it myself, when I re-read it after two years...
Thanks,
Amir.