Inotify limits per usernamespace
Subsystems (9)
v3 cover Inotify limits per usernamespace
Hello Eric,
Another day, another version...
So this is version 3 of the patches initially posted at [1]. Version 2 can be fount at [2]
So I reworked the way the state for namespaces are created and the code is indeed a lot cleaner and easier to understand. I've also moved from using uid and instead always work with kuids. Finally I reworked the hashtable to be a global one and indexed by the kuid value. With this I believe I have adressed all your points in your previous review.
I still haven't tackled the issue with the semantics of the sysctls. But judging from your last reply about being content with just setting the limit at the top-level I believe the way to fix this would be make a custom proc handler that writes to the variable in the current userns. Opinions?
[1] http://thread.gmane.org/gmane.linux.kernel/2232000
[2] https://lists.linuxfoundation.org/pipermail/containers/2016-June/037019.html
Nikolay Borisov (4):
hashtable: Add __HASHTABLE_INITIALIZER
misc: Rename the HASH_SIZE macro
userns/inotify: Initial implementation of inotify per-userns
inotify: Convert to using new userns infrastructure
fs/logfs/dir.c | 6 +- fs/notify/inotify/inotify.h | 2 + fs/notify/inotify/inotify_fsnotify.c | 14 +++- fs/notify/inotify/inotify_user.c | 136 +++++++++++++++++++++++++++---- include/linux/fsnotify_backend.h | 4 +- include/linux/hashtable.h | 3 + include/linux/sched.h | 4 - include/linux/user_namespace.h | 45 ++++++++++ kernel/user_namespace.c | 106 +++++++++++++++++++++++- net/ipv6/ip6_gre.c | 8 +- net/ipv6/ip6_tunnel.c | 10 +-- net/ipv6/ip6_vti.c | 10 +-- net/ipv6/sit.c | 10 +-- security/keys/encrypted-keys/encrypted.c | 32 ++++---- 14 files changed, 327 insertions(+), 63 deletions(-)
Signature · 1 line
2.5.0
A message that is not in the archive (sent off-list or not yet received): 1468412053-30130-1-git-send-email-kernel-6AxghH7DbtA@public.gmane.org
PATCH v3 1/4 hashtable: Add __HASHTABLE_INITIALIZER
This is used so that one can initialize a hashtbale declared in a struct.
--- include/linux/hashtable.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/hashtable.h +3 -0
@@ -23,6 +23,9 @@#define DECLARE_HASHTABLE (name, bits) \ struct hlist_head name[1 << (bits)] +#define __HASHTABLE_INITIALIZER (bits) \ + { [0 ... (( 1 << bits)) - 1] = HLIST_HEAD_INIT } +#define HASH_SIZE (name) (ARRAY_SIZE(name))#define HASH_BITS (name) ilog2(HASH_SIZE(name))
Signature · 1 line
2.5.0
PATCH v3 2/4 misc: Rename the HASH_SIZE macro
In reply to a message not in the archiveThis change is required since the inotify-per-namespace code added hashtable.h to the include list of sched.h. This in turn causes compiler warnings since HASH_SIZE is being defined in multiple locations
--- fs/logfs/dir.c | 6 +++--- net/ipv6/ip6_gre.c | 8 ++++---- net/ipv6/ip6_tunnel.c | 10 +++++----- net/ipv6/ip6_vti.c | 10 +++++----- net/ipv6/sit.c | 10 +++++----- security/keys/encrypted-keys/encrypted.c | 32 ++++++++++++++++---------------- 6 files changed, 38 insertions(+), 38 deletions(-)
fs/logfs/dir.c +3 -3
@@ -95,7 +95,7 @@ static int beyond_eof(struct inode *inode, loff_t bix)* of each character and pick a prime nearby, preferably a bit-sparse * one. */ -static u32 hash_32 (const char *s,int len,u32 seed) +static u32 logfs_hash_32 (const char *s,int len,u32 seed) {u32 hash = seed;int i;
@@ -159,7 +159,7 @@ static struct page *logfs_get_dd_page(struct inode *dir, struct dentry *dentry)struct qstr *name = &dentry->d_name;struct page *page;struct logfs_disk_dentry *dd; -u32 hash =hash_32 (name->name, name->len,0 ); +u32 hash =logfs_hash_32 (name->name, name->len,0 );pgoff_t index;int round;
@@ -370,7 +370,7 @@ static int logfs_write_dir(struct inode *dir, struct dentry *dentry, {struct page *page;struct logfs_disk_dentry *dd; -u32 hash =hash_32 (dentry->d_name.name, dentry->d_name.len,0 ); +u32 hash =logfs_hash_32 (dentry->d_name.name, dentry->d_name.len,0 );pgoff_t index;int round, err;
net/ipv6/ip6_gre.c +4 -4
@@ -62,11 +62,11 @@ module_param(log_ecn_error, bool, 0644);MODULE_PARM_DESC (log_ecn_error,"Log packets received with corrupted ECN" );#define HASH_SIZE_SHIFT 5 -#define HASH_SIZE (1 << HASH_SIZE_SHIFT) +#define IP6G_HASH_SIZE (1 << HASH_SIZE_SHIFT)static int ip6gre_net_id__read_mostly ;struct ip6gre_net { -struct ip6_tnl __rcu *tunnels[4 ][HASH_SIZE ]; +struct ip6_tnl __rcu *tunnels[4 ][IP6G_HASH_SIZE ];struct net_device *fb_tunnel_dev; };
@@ -96,7 +96,7 @@ static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);will match fallback tunnel. */ -#define HASH_KEY (key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1)) +#define HASH_KEY (key) (((__force u32)key^((__force u32)key>>4))&(IP6G_HASH_SIZE - 1))static u32 HASH_ADDR (const struct in6_addr *addr) {u32 hash =ipv6_addr_hash (addr);
@@ -1089,7 +1089,7 @@ static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)for (prio =0 ; prio <4 ; prio++) {int h; -for (h =0 ; h <HASH_SIZE ; h++) { +for (h =0 ; h <IP6G_HASH_SIZE ; h++) {struct ip6_tnl *t; t =rtnl_dereference (ign->tunnels[prio][h]);
net/ipv6/ip6_tunnel.c +5 -5
@@ -64,8 +64,8 @@ MODULE_LICENSE("GPL");MODULE_ALIAS_RTNL_LINK ("ip6tnl" );MODULE_ALIAS_NETDEV ("ip6tnl0" ); -#define HASH_SIZE_SHIFT 5 -#define HASH_SIZE (1 << HASH_SIZE_SHIFT) +#define IP6_HASH_SIZE_SHIFT 5 +#define IP6_HASH_SIZE (1 << IP6_HASH_SIZE_SHIFT)static bool log_ecn_error = true;module_param (log_ecn_error, bool,0644 );
@@ -75,7 +75,7 @@ static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2) {u32 hash =ipv6_addr_hash (addr1) ^ipv6_addr_hash (addr2); -return hash_32 (hash,HASH_SIZE_SHIFT ); +return hash_32 (hash,IP6_HASH_SIZE_SHIFT ); }static int ip6_tnl_dev_init (struct net_device *dev);
@@ -87,7 +87,7 @@ struct ip6_tnl_net {/* the IPv6 tunnel fallback device */ struct net_device *fb_tnl_dev;/* lists for storing tunnels in use */ -struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE ]; +struct ip6_tnl __rcu *tnls_r_l[IP6_HASH_SIZE ];struct ip6_tnl __rcu *tnls_wc[1 ];struct ip6_tnl __rcu **tnls[2 ]; };
@@ -2031,7 +2031,7 @@ static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)if (dev->rtnl_link_ops == &ip6_link_ops)unregister_netdevice_queue (dev, &list); -for (h =0 ; h <HASH_SIZE ; h++) { +for (h =0 ; h <IP6_HASH_SIZE ; h++) { t =rtnl_dereference (ip6n->tnls_r_l[h]);while (t) {/* If dev is in the same netns, it has already
net/ipv6/ip6_vti.c +5 -5
@@ -50,14 +50,14 @@#include <net/net_namespace.h> #include <net/netns/generic.h> -#define HASH_SIZE_SHIFT 5 -#define HASH_SIZE (1 << HASH_SIZE_SHIFT) +#define VTI_HASH_SIZE_SHIFT 5 +#define VTI_HASH_SIZE (1 << VTI_HASH_SIZE_SHIFT)static u32 HASH (const struct in6_addr *addr1,const struct in6_addr *addr2) {u32 hash =ipv6_addr_hash (addr1) ^ipv6_addr_hash (addr2); -return hash_32 (hash,HASH_SIZE_SHIFT ); +return hash_32 (hash,VTI_HASH_SIZE_SHIFT ); }static int vti6_dev_init (struct net_device *dev);
@@ -69,7 +69,7 @@ struct vti6_net {/* the vti6 tunnel fallback device */ struct net_device *fb_tnl_dev;/* lists for storing tunnels in use */ -struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE ]; +struct ip6_tnl __rcu *tnls_r_l[VTI_HASH_SIZE ];struct ip6_tnl __rcu *tnls_wc[1 ];struct ip6_tnl __rcu **tnls[2 ]; };
@@ -1040,7 +1040,7 @@ static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)struct ip6_tnl *t;LIST_HEAD (list); -for (h =0 ; h <HASH_SIZE ; h++) { +for (h =0 ; h <VTI_HASH_SIZE ; h++) { t =rtnl_dereference (ip6n->tnls_r_l[h]);while (t) {unregister_netdevice_queue (t->dev, &list);
net/ipv6/sit.c +5 -5
@@ -62,7 +62,7 @@For comments look at net/ipv4/ip_gre.c --ANK */ -#define HASH_SIZE 16 +#define SIT_HASH_SIZE 16#define HASH (addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)static bool log_ecn_error = true;
@@ -78,9 +78,9 @@ static struct rtnl_link_ops sit_link_ops __read_mostly;static int sit_net_id__read_mostly ;struct sit_net { -struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE ]; -struct ip_tunnel __rcu *tunnels_r[HASH_SIZE ]; -struct ip_tunnel __rcu *tunnels_l[HASH_SIZE ]; +struct ip_tunnel __rcu *tunnels_r_l[SIT_HASH_SIZE ]; +struct ip_tunnel __rcu *tunnels_r[SIT_HASH_SIZE ]; +struct ip_tunnel __rcu *tunnels_l[SIT_HASH_SIZE ];struct ip_tunnel __rcu *tunnels_wc[1 ];struct ip_tunnel __rcu **tunnels[4 ];
@@ -1773,7 +1773,7 @@ static void __net_exit sit_destroy_tunnels(struct net *net,for (prio =1 ; prio <4 ; prio++) {int h; -for (h =0 ; h <HASH_SIZE ; h++) { +for (h =0 ; h <SIT_HASH_SIZE ; h++) {struct ip_tunnel *t; t =rtnl_dereference (sitn->tunnels[prio][h]);
security/keys/encrypted-keys/encrypted.c +16 -16
@@ -49,7 +49,7 @@ static int blksize;#define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)#define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)#define KEY_ECRYPTFS_DESC_LEN 16 -#define HASH_SIZE SHA256_DIGEST_SIZE +#define E_HASH_SIZE SHA256_DIGEST_SIZE#define MAX_DATA_SIZE 4096#define MIN_DATA_SIZE 20
@@ -380,8 +380,8 @@ static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,int ret; derived_buf_len =strlen ("AUTH_KEY" ) +1 + master_keylen; -if (derived_buf_len <HASH_SIZE ) - derived_buf_len =HASH_SIZE ; +if (derived_buf_len <E_HASH_SIZE ) + derived_buf_len =E_HASH_SIZE ; derived_buf =kzalloc (derived_buf_len,GFP_KERNEL );if (!derived_buf) {
@@ -517,7 +517,7 @@ out:static int datablob_hmac_append (struct encrypted_key_payload *epayload,const u8 *master_key,size_t master_keylen) { -u8 derived_key[HASH_SIZE ]; +u8 derived_key[E_HASH_SIZE ];u8 *digest;int ret;
@@ -529,7 +529,7 @@ static int datablob_hmac_append(struct encrypted_key_payload *epayload, ret =calc_hmac (digest, derived_key,sizeof derived_key, epayload->format, epayload->datablob_len);if (!ret) -dump_hmac (NULL , digest,HASH_SIZE ); +dump_hmac (NULL , digest,E_HASH_SIZE ); out:return ret; }
@@ -539,8 +539,8 @@ static int datablob_hmac_verify(struct encrypted_key_payload *epayload, const u8 *format, const u8 *master_key, size_t master_keylen) { - u8 derived_key[HASH_SIZE]; -u8 digest[HASH_SIZE ]; + u8 derived_key[E_HASH_SIZE]; +u8 digest[E_HASH_SIZE ];int ret;char *p;unsigned short len;
@@ -565,8 +565,8 @@ static int datablob_hmac_verify(struct encrypted_key_payload *epayload, ret = -EINVAL ;dump_hmac ("datablob" , epayload->format + epayload->datablob_len, -HASH_SIZE ); -dump_hmac ("calc" , digest,HASH_SIZE ); +E_HASH_SIZE ); +dump_hmac ("calc" , digest,E_HASH_SIZE ); } out:return ret;
@@ -651,12 +651,12 @@ static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, +strlen (datalen) +1 + ivsize +1 + encrypted_datalen; ret =key_payload_reserve (key, payload_datalen + datablob_len - +HASH_SIZE +1 ); + +E_HASH_SIZE +1 );if (ret <0 )return ERR_PTR (ret); epayload =kzalloc (sizeof (*epayload) + payload_datalen + - datablob_len +HASH_SIZE +1 ,GFP_KERNEL ); + datablob_len +E_HASH_SIZE +1 ,GFP_KERNEL );if (!epayload)return ERR_PTR (-ENOMEM );
@@ -670,7 +670,7 @@ static int encrypted_key_decrypt(struct encrypted_key_payload *epayload, const char *format, const char *hex_encoded_iv) { struct key *mkey; -u8 derived_key[HASH_SIZE ]; +u8 derived_key[E_HASH_SIZE ];const u8 *master_key;u8 *hmac;const char *hex_encoded_data;
@@ -680,7 +680,7 @@ static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,int ret; encrypted_datalen =roundup (epayload->decrypted_datalen, blksize); - asciilen = (ivsize +1 + encrypted_datalen +HASH_SIZE ) *2 ; + asciilen = (ivsize +1 + encrypted_datalen +E_HASH_SIZE ) *2 ;if (strlen (hex_encoded_iv) != asciilen)return -EINVAL ;
@@ -695,7 +695,7 @@ static int encrypted_key_decrypt(struct encrypted_key_payload *epayload, hmac = epayload->format + epayload->datablob_len; ret =hex2bin (hmac, hex_encoded_data + (encrypted_datalen *2 ), -HASH_SIZE ); +E_HASH_SIZE );if (ret <0 )return -EINVAL ;
@@ -918,7 +918,7 @@ static long encrypted_read(const struct key *key, char __user *buffer,struct key *mkey;const u8 *master_key;size_t master_keylen; -char derived_key[HASH_SIZE ]; +char derived_key[E_HASH_SIZE ];char *ascii_buf;size_t asciiblob_len;int ret;
@@ -928,7 +928,7 @@ static long encrypted_read(const struct key *key, char __user *buffer,/* returns the hex encoded iv, encrypted-data, and hmac as ascii */ asciiblob_len = epayload->datablob_len + ivsize +1 +roundup (epayload->decrypted_datalen, blksize) - + (HASH_SIZE *2 ); + + (E_HASH_SIZE *2 );if (!buffer || buflen < asciiblob_len)return asciiblob_len;
Signature · 1 line
2.5.0
PATCH v3 3/4 userns/inotify: Initial implementation of inotify per-userns
In reply to a message not in the archiveSo here is the first version of the hierarchical inotify limits. Changes include:
* Added 2 new sysctls:
- inotify_reserved_user_instances and inotify_reserved_user_watches these essentially control the distribution of instances/watches down the hierarchy. For example if we have instances/watches limit of 1024/256 and reserved instances/watches are set to 128/32 then at every level of the hierarchy instances/watches are going to be reduced by 128/32, so at userns level of 1 (e.g. init_user_ns->level_1_user_ns) each user would have 896/224 respectively. Currently the defaults are calculated so that at least 8 levels of indirection are allowed. Those can be set only by global root user.
* Changed core userns code to support adding per-userns/per-user counters, this is happening in the nsuser_state structure.
* Add necessary functionality to inotify to make use of the newly added userns infrastructure.
* Moved the initialization of the inotify_max_user_instances/watches to user_namespaces_init so that it's initialised by the time inotify is bootstrapped.
--- fs/notify/inotify/inotify.h | 2 + fs/notify/inotify/inotify_user.c | 93 +++++++++++++++++++++++++++++++++- include/linux/fsnotify_backend.h | 3 ++ include/linux/user_namespace.h | 45 +++++++++++++++++ kernel/user_namespace.c | 106 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 246 insertions(+), 3 deletions(-)
fs/notify/inotify/inotify.h +2 -0
@@ -1,6 +1,8 @@#include <linux/fsnotify_backend.h> #include <linux/inotify.h> #include <linux/slab.h> /* struct kmem_cache */ +#include <linux/page_counter.h> +#include <linux/user_namespace.h> struct inotify_event_info {struct fsnotify_event fse;
fs/notify/inotify/inotify_user.c +91 -2
115 lines not on this page · raw message
include/linux/fsnotify_backend.h +3 -0
@@ -170,6 +170,9 @@ struct fsnotify_group {spinlock_t idr_lock;struct idr idr;struct user_struct *user; +struct user_namespace *userns; +kuid_t uid;/* id in the userns this group is + associated with */ } inotify_data;#endif #ifdef CONFIG_FANOTIFY
include/linux/user_namespace.h +45 -0
74 lines not on this page · raw message
kernel/user_namespace.c +105 -1
161 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH v3 4/4 inotify: Convert to using new userns infrastructure
In reply to a message not in the archiveModify the current inotify code to use the newly added perns/per user counters. Also change the max_inotify_user_instances/watches to apply only to the top level.
--- fs/notify/inotify/inotify_fsnotify.c | 14 +++++++++--- fs/notify/inotify/inotify_user.c | 43 ++++++++++++++++++++++++------------ include/linux/fsnotify_backend.h | 1 - include/linux/sched.h | 4 ---- 4 files changed, 40 insertions(+), 22 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +11 -3
@@ -30,6 +30,7 @@#include <linux/slab.h> /* kmem_* */ #include <linux/types.h> #include <linux/sched.h> +#include <linux/user_namespace.h> #include "inotify.h"
@@ -165,9 +166,16 @@ static void inotify_free_group_priv(struct fsnotify_group *group)/* ideally the idr is empty and we won't hit the BUG in the callback */ idr_for_each (&group->inotify_data.idr, idr_callback, group);idr_destroy (&group->inotify_data.idr); -if (group->inotify_data.user) { -atomic_dec (&group->inotify_data.user->inotify_devs); -free_uid (group->inotify_data.user); +if (group->inotify_data.userns) { +struct nsuser_state *state; + +spin_lock_bh (&nsuser_state_lock); + state =get_nsuser_state (group->inotify_data.userns, + group->inotify_data.uid); +spin_unlock_bh (&nsuser_state_lock); +BUG_ON (!state); + +page_counter_uncharge (&state->inotify_instances,1 ); } }
fs/notify/inotify/inotify_user.c +29 -14
106 lines not on this page · raw message
include/linux/fsnotify_backend.h +0 -1
@@ -169,7 +169,6 @@ struct fsnotify_group {struct inotify_group_private_data {spinlock_t idr_lock;struct idr idr; -struct user_struct *user;struct user_namespace *userns;kuid_t uid;/* id in the userns this group is associated with */
include/linux/sched.h +0 -4
@@ -838,10 +838,6 @@ struct user_struct {atomic_t __count;/* reference count */ atomic_t processes;/* How many processes does this user have? */ atomic_t sigpending;/* How many pending signals does this user have? */ -#ifdef CONFIG_INOTIFY_USER -atomic_t inotify_watches;/* How many inotify watches does this user have? */ -atomic_t inotify_devs;/* How many inotify devs does this user have opened? */ -#endif #ifdef CONFIG_FANOTIFY atomic_t fanotify_listeners;#endif
Signature · 1 line
2.5.0
Quoted 14 lines · Nikolay Borisov
Hello Eric,
Another day, another version...
So this is version 3 of the patches initially posted at [1]. Version 2 can be fount at [2]
So I reworked the way the state for namespaces are created and the code is indeed a lot cleaner and easier to understand. I've also moved from using uid and instead always work with kuids. Finally I reworked the hashtable to be a global one and indexed by the kuid value. With this I believe I have adressed all your points in your previous review.
Quoted 5 lines
I still haven't tackled the issue with the semantics of the sysctls. But judging from your last reply about being content with just setting the limit at the top-level I believe the way to fix this would be make a custom proc handler that writes to the variable in the current userns. Opinions?
So first let me say thank you for this. It was a very good starting point for my thinking on other sysctls.
I have been proceeding on another track I will post in just a minute that addresses some different sysctls. Those I know how to pick a reasonable sanity check limit on a per user system wide basis and not a
per user basis so I don't have any per user limits. Otherwise it is
pretty much the same case as here.
Please take a look at those patches on how to address creating per user namespace sysctls.
After those patches settle I will be willing to look at these more.
Eric
Quoted 24 lines
[1] http://thread.gmane.org/gmane.linux.kernel/2232000
[2] https://lists.linuxfoundation.org/pipermail/containers/2016-June/037019.htmlNikolay Borisov (4):
hashtable: Add__HASHTABLE_INITIALIZER
misc: Rename theHASH_SIZEmacro
userns/inotify: Initial implementation of inotify per-userns
inotify: Convert to using new userns infrastructurefs/logfs/dir.c | 6 +- fs/notify/inotify/inotify.h | 2 + fs/notify/inotify/inotify_fsnotify.c | 14 +++- fs/notify/inotify/inotify_user.c | 136 +++++++++++++++++++++++++++---- include/linux/fsnotify_backend.h | 4 +- include/linux/hashtable.h | 3 + include/linux/sched.h | 4 - include/linux/user_namespace.h | 45 ++++++++++ kernel/user_namespace.c | 106 +++++++++++++++++++++++- net/ipv6/ip6_gre.c | 8 +- net/ipv6/ip6_tunnel.c | 10 +-- net/ipv6/ip6_vti.c | 10 +-- net/ipv6/sit.c | 10 +-- security/keys/encrypted-keys/encrypted.c | 32 ++++---- 14 files changed, 327 insertions(+), 63 deletions(-)