summaryrefslogtreecommitdiffstats
path: root/fs/notify/inotify
diff options
context:
space:
mode:
authorNikolay Borisov <n.borisov.lkml@gmail.com>2016-12-14 08:56:33 -0500
committerEric W. Biederman <ebiederm@xmission.com>2017-01-23 18:03:07 -0500
commit1cce1eea0aff51201753fcaca421df825b0813b6 (patch)
tree9717a36b5968a179942e2b2f62d21c3c05cc39c6 /fs/notify/inotify
parent880a38547ff08715ce4f1daf9a4bb30c87676e68 (diff)
inotify: Convert to using per-namespace limits
This patchset converts inotify to using the newly introduced per-userns sysctl infrastructure. Currently the inotify instances/watches are being accounted in the user_struct structure. This means that in setups where multiple users in unprivileged containers map to the same underlying real user (i.e. pointing to the same user_struct) the inotify limits are going to be shared as well, allowing one user(or application) to exhaust all others limits. Fix this by switching the inotify sysctls to using the per-namespace/per-user limits. This will allow the server admin to set sensible global limits, which can further be tuned inside every individual user namespace. Additionally, in order to preserve the sysctl ABI make the existing inotify instances/watches sysctls modify the values of the initial user namespace. Signed-off-by: Nikolay Borisov <n.borisov.lkml@gmail.com> Acked-by: Jan Kara <jack@suse.cz> Acked-by: Serge Hallyn <serge@hallyn.com> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Diffstat (limited to 'fs/notify/inotify')
-rw-r--r--fs/notify/inotify/inotify.h17
-rw-r--r--fs/notify/inotify/inotify_fsnotify.c6
-rw-r--r--fs/notify/inotify/inotify_user.c34
3 files changed, 36 insertions, 21 deletions
diff --git a/fs/notify/inotify/inotify.h b/fs/notify/inotify/inotify.h
index a6f5907a3fee..7c461fd49c4c 100644
--- a/fs/notify/inotify/inotify.h
+++ b/fs/notify/inotify/inotify.h
@@ -30,3 +30,20 @@ extern int inotify_handle_event(struct fsnotify_group *group,
30 const unsigned char *file_name, u32 cookie); 30 const unsigned char *file_name, u32 cookie);
31 31
32extern const struct fsnotify_ops inotify_fsnotify_ops; 32extern const struct fsnotify_ops inotify_fsnotify_ops;
33
34#ifdef CONFIG_INOTIFY_USER
35static inline void dec_inotify_instances(struct ucounts *ucounts)
36{
37 dec_ucount(ucounts, UCOUNT_INOTIFY_INSTANCES);
38}
39
40static inline struct ucounts *inc_inotify_watches(struct ucounts *ucounts)
41{
42 return inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_INOTIFY_WATCHES);
43}
44
45static inline void dec_inotify_watches(struct ucounts *ucounts)
46{
47 dec_ucount(ucounts, UCOUNT_INOTIFY_WATCHES);
48}
49#endif
diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
index 19e7ec109a75..f36c29398de3 100644
--- a/fs/notify/inotify/inotify_fsnotify.c
+++ b/fs/notify/inotify/inotify_fsnotify.c
@@ -165,10 +165,8 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
165 /* ideally the idr is empty and we won't hit the BUG in the callback */ 165 /* ideally the idr is empty and we won't hit the BUG in the callback */
166 idr_for_each(&group->inotify_data.idr, idr_callback, group); 166 idr_for_each(&group->inotify_data.idr, idr_callback, group);
167 idr_destroy(&group->inotify_data.idr); 167 idr_destroy(&group->inotify_data.idr);
168 if (group->inotify_data.user) { 168 if (group->inotify_data.ucounts)
169 atomic_dec(&group->inotify_data.user->inotify_devs); 169 dec_inotify_instances(group->inotify_data.ucounts);
170 free_uid(group->inotify_data.user);
171 }
172} 170}
173 171
174static void inotify_free_event(struct fsnotify_event *fsn_event) 172static void inotify_free_event(struct fsnotify_event *fsn_event)
diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c
index 69d1ea3d292a..1cf41c623be1 100644
--- a/fs/notify/inotify/inotify_user.c
+++ b/fs/notify/inotify/inotify_user.c
@@ -44,10 +44,8 @@
44 44
45#include <asm/ioctls.h> 45#include <asm/ioctls.h>
46 46
47/* these are configurable via /proc/sys/fs/inotify/ */ 47/* configurable via /proc/sys/fs/inotify/ */
48static int inotify_max_user_instances __read_mostly;
49static int inotify_max_queued_events __read_mostly; 48static int inotify_max_queued_events __read_mostly;
50static int inotify_max_user_watches __read_mostly;
51 49
52static struct kmem_cache *inotify_inode_mark_cachep __read_mostly; 50static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
53 51
@@ -60,7 +58,7 @@ static int zero;
60struct ctl_table inotify_table[] = { 58struct ctl_table inotify_table[] = {
61 { 59 {
62 .procname = "max_user_instances", 60 .procname = "max_user_instances",
63 .data = &inotify_max_user_instances, 61 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
64 .maxlen = sizeof(int), 62 .maxlen = sizeof(int),
65 .mode = 0644, 63 .mode = 0644,
66 .proc_handler = proc_dointvec_minmax, 64 .proc_handler = proc_dointvec_minmax,
@@ -68,7 +66,7 @@ struct ctl_table inotify_table[] = {
68 }, 66 },
69 { 67 {
70 .procname = "max_user_watches", 68 .procname = "max_user_watches",
71 .data = &inotify_max_user_watches, 69 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
72 .maxlen = sizeof(int), 70 .maxlen = sizeof(int),
73 .mode = 0644, 71 .mode = 0644,
74 .proc_handler = proc_dointvec_minmax, 72 .proc_handler = proc_dointvec_minmax,
@@ -500,7 +498,7 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
500 /* remove this mark from the idr */ 498 /* remove this mark from the idr */
501 inotify_remove_from_idr(group, i_mark); 499 inotify_remove_from_idr(group, i_mark);
502 500
503 atomic_dec(&group->inotify_data.user->inotify_watches); 501 dec_inotify_watches(group->inotify_data.ucounts);
504} 502}
505 503
506/* ding dong the mark is dead */ 504/* ding dong the mark is dead */
@@ -584,14 +582,17 @@ static int inotify_new_watch(struct fsnotify_group *group,
584 tmp_i_mark->fsn_mark.mask = mask; 582 tmp_i_mark->fsn_mark.mask = mask;
585 tmp_i_mark->wd = -1; 583 tmp_i_mark->wd = -1;
586 584
587 ret = -ENOSPC;
588 if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
589 goto out_err;
590
591 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark); 585 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
592 if (ret) 586 if (ret)
593 goto out_err; 587 goto out_err;
594 588
589 /* increment the number of watches the user has */
590 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
591 inotify_remove_from_idr(group, tmp_i_mark);
592 ret = -ENOSPC;
593 goto out_err;
594 }
595
595 /* we are on the idr, now get on the inode */ 596 /* we are on the idr, now get on the inode */
596 ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, group, inode, 597 ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, group, inode,
597 NULL, 0); 598 NULL, 0);
@@ -601,8 +602,6 @@ static int inotify_new_watch(struct fsnotify_group *group,
601 goto out_err; 602 goto out_err;
602 } 603 }
603 604
604 /* increment the number of watches the user has */
605 atomic_inc(&group->inotify_data.user->inotify_watches);
606 605
607 /* return the watch descriptor for this new mark */ 606 /* return the watch descriptor for this new mark */
608 ret = tmp_i_mark->wd; 607 ret = tmp_i_mark->wd;
@@ -653,10 +652,11 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events)
653 652
654 spin_lock_init(&group->inotify_data.idr_lock); 653 spin_lock_init(&group->inotify_data.idr_lock);
655 idr_init(&group->inotify_data.idr); 654 idr_init(&group->inotify_data.idr);
656 group->inotify_data.user = get_current_user(); 655 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
656 current_euid(),
657 UCOUNT_INOTIFY_INSTANCES);
657 658
658 if (atomic_inc_return(&group->inotify_data.user->inotify_devs) > 659 if (!group->inotify_data.ucounts) {
659 inotify_max_user_instances) {
660 fsnotify_destroy_group(group); 660 fsnotify_destroy_group(group);
661 return ERR_PTR(-EMFILE); 661 return ERR_PTR(-EMFILE);
662 } 662 }
@@ -819,8 +819,8 @@ static int __init inotify_user_setup(void)
819 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC); 819 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
820 820
821 inotify_max_queued_events = 16384; 821 inotify_max_queued_events = 16384;
822 inotify_max_user_instances = 128; 822 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
823 inotify_max_user_watches = 8192; 823 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
824 824
825 return 0; 825 return 0;
826} 826}