cifs: second pile of cleanups for cifsacl
Subsystems (2)
cover cifs: second pile of cleanups for cifsacl
This patchset is a second pile of cleanups for the cifsacl code. This should be applied on top of the set I sent yesterday, which in turn should go on top of the patch I sent yesterday to fix the buffer overruns in cifs. The meat of this set is in the 6th patch which is largely responsible for the awesome diffstat.
I've tested this set and it seems to work as expected. With the merge window closed, this is probably best suited for 3.8, but getting it into linux-next soon would be ideal.
Steve, all of these patches are in my cifs-next branch if you just want to pull from there...
Jeff Layton (7):
cifs: make cifs_copy_sid handle a source sid with variable size
subauth arrays
cifs: redefine NUM_SUBAUTH constant from 5 to 15
cifs: fix the format specifiers in sid_to_str
cifs: remove uneeded __KERNEL__ block from cifsacl.h
cifs: simplify id_to_sid and sid_to_id mapping code
cifs: avoid extra allocation for small cifs.idmap keys
cifs: don't override the uid/gid in getattr when cifsacl is enabled
fs/cifs/cifsacl.c | 569 +++++++++++----------------------------------------- fs/cifs/cifsacl.h | 44 ++-- fs/cifs/cifsfs.c | 1 - fs/cifs/cifsproto.h | 1 - fs/cifs/inode.c | 7 +- 5 files changed, 145 insertions(+), 477 deletions(-)
Signature · 1 line
1.7.11.7
A message that is not in the archive (sent off-list or not yet received): 1350497343-7155-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
PATCH 1/7 cifs: make cifs_copy_sid handle a source sid with variable size subauth arrays
...and lift the restriction in id_to_sid upcall that the size must be at least as big as a full cifs_sid.
--- fs/cifs/cifsacl.c | 10 ++++++++-- fs/cifs/cifsacl.h | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-)
fs/cifs/cifsacl.c +8 -2
@@ -277,8 +277,14 @@ compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid)static void cifs_copy_sid (struct cifs_sid *dst,const struct cifs_sid *src) { -memcpy (dst, src,sizeof (*dst)); +int i; + + dst->revision = src->revision; dst->num_subauth =min_t (u8, src->num_subauth,NUM_SUBAUTHS ); +for (i =0 ; i <NUM_AUTHS ; ++i) + dst->authority[i] = src->authority[i]; +for (i =0 ; i < dst->num_subauth; ++i) + dst->sub_auth[i] = src->sub_auth[i]; } static void
@@ -427,7 +433,7 @@ id_to_sid(unsigned long cid, uint sidtype, struct cifs_sid *ssid)if (IS_ERR (sidkey)) { rc = -EINVAL ;cFYI (1 ,"%s: Can't map and id to a SID" , __func__); - }else if (sidkey->datalen <sizeof (struct cifs_sid )) { + }else if (sidkey->datalen <CIFS_SID_BASE_SIZE ) { rc = -EIO ;cFYI (1 ,"%s: Downcall contained malformed key " "(datalen=%hu)" , __func__, sidkey->datalen);
fs/cifs/cifsacl.h +3 -0
@@ -64,6 +64,9 @@ struct cifs_sid {__le32 sub_auth[NUM_SUBAUTHS ];/* sub_auth[num_subauth] */ } __attribute__((packed)); +/* size of a struct cifs_sid, sans sub_auth array */ +#define CIFS_SID_BASE_SIZE (1 + 1 + NUM_AUTHS) +struct cifs_acl {__le16 revision;/* revision level */ __le16 size;
Signature · 1 line
1.7.11.7
PATCH 2/7 cifs: redefine NUM_SUBAUTH constant from 5 to 15
In reply to a message not in the archiveAccording to several places on the Internet and the samba winbind code, this is hard limited to 15 in windows, not 5. This does balloon out the allocation of each by 40 bytes, but I don't see any alternative.
Also, rename it to SID_MAX_SUB_AUTHORITIES to match the alleged name of this constant in the windows header files
Finally, rename SIDLEN to SID_STRING_MAX, fix the value to reflect the change to SID_MAX_SUB_AUTHORITIES and document how it was determined.
--- fs/cifs/cifsacl.c | 6 +++--- fs/cifs/cifsacl.h | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-)
fs/cifs/cifsacl.c +3 -3
@@ -280,7 +280,7 @@ cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)int i; dst->revision = src->revision; - dst->num_subauth =min_t (u8, src->num_subauth,NUM_SUBAUTHS ); + dst->num_subauth =min_t (u8, src->num_subauth,SID_MAX_SUB_AUTHORITIES );for (i =0 ; i <NUM_AUTHS ; ++i) dst->authority[i] = src->authority[i];for (i =0 ; i < dst->num_subauth; ++i)
@@ -383,7 +383,7 @@ id_to_sid(unsigned long cid, uint sidtype, struct cifs_sid *ssid)if (!npsidid)return -ENOMEM ; - npsidid->sidstr =kmalloc (SIDLEN ,GFP_KERNEL ); + npsidid->sidstr =kmalloc (SID_STRING_MAX ,GFP_KERNEL );if (!npsidid->sidstr) {kfree (npsidid);return -ENOMEM ;
@@ -500,7 +500,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,if (!npsidid)return -ENOMEM ; - npsidid->sidstr =kmalloc (SIDLEN ,GFP_KERNEL ); + npsidid->sidstr =kmalloc (SID_STRING_MAX ,GFP_KERNEL );if (!npsidid->sidstr) {kfree (npsidid);return -ENOMEM ;
fs/cifs/cifsacl.h +16 -3
@@ -24,7 +24,7 @@#define NUM_AUTHS 6/* number of authority fields */ -#define NUM_SUBAUTHS 5/* number of sub authority fields */ +#define SID_MAX_SUB_AUTHORITIES (15)/* max number of sub authority fields */ #define NUM_WK_SIDS 7/* number of well known sids */ #define SIDNAMELENGTH 20/* long enough for the ones we care about */ #define DEFSECDESCLEN 192/* sec desc len contaiting a dacl with three aces */
@@ -41,7 +41,20 @@#define SIDOWNER 1#define SIDGROUP 2 -#define SIDLEN 150/* S- 1 revision- 6 authorities- max 5 sub authorities */ + +/* +* Maximum size of a string representation of a SID: +* +* The fields are unsigned values in decimal. So: +* +* u8: max 3 bytes in decimal +* u32: max 10 bytes in decimal +* +* "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes +* per number + 1 for '-') + 11 bytes for each subauthority field (10 bytes +* per number + 1 for '-') + NULL terminator. +*/ +#define SID_STRING_MAX (195)#define SID_ID_MAPPED 0#define SID_ID_PENDING 1
@@ -61,7 +74,7 @@ struct cifs_sid {__u8 revision;/* revision level */ __u8 num_subauth;__u8 authority[NUM_AUTHS ]; -__le32 sub_auth[NUM_SUBAUTHS ];/* sub_auth[num_subauth] */ +__le32 sub_auth[SID_MAX_SUB_AUTHORITIES ];/* sub_auth[num_subauth] */ } __attribute__((packed));/* size of a struct cifs_sid, sans sub_auth array */
Signature · 1 line
1.7.11.7
PATCH 3/7 cifs: fix the format specifiers in sid_to_str
In reply to a message not in the archiveThe format specifiers are for signed values, but these are unsigned. Given that '-' is a delimiter between fields, I don't think you'd get what you'd expect if you got a value here that would overflow the sign bit.
The version and authority fields are 8 bit values so use a "hh" length modifier there. The subauths are 32 bit values, so there's no need to use a "l" length modifier there.
--- fs/cifs/cifsacl.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
fs/cifs/cifsacl.c +4 -7
@@ -199,27 +199,24 @@ static void sid_to_str(struct cifs_sid *sidptr, char *sidstr) { int i; -unsigned long saval; +unsigned int saval;char *strptr; strptr = sidstr; -sprintf (strptr,"%s" ,"S" ); - strptr = sidstr +strlen (sidstr); - -sprintf (strptr,"-%d" , sidptr->revision); +sprintf (strptr,"S-%hhu" , sidptr->revision); strptr = sidstr +strlen (sidstr);for (i =0 ; i <NUM_AUTHS ; ++i) {if (sidptr->authority[i]) { -sprintf (strptr,"-%d" , sidptr->authority[i]); +sprintf (strptr,"-%hhu" , sidptr->authority[i]); strptr = sidstr +strlen (sidstr); } }for (i =0 ; i < sidptr->num_subauth; ++i) { saval =le32_to_cpu (sidptr->sub_auth[i]); -sprintf (strptr,"-%ld" , saval); +sprintf (strptr,"-%u" , saval); strptr = sidstr +strlen (sidstr); } }
Signature · 1 line
1.7.11.7
PATCH 4/7 cifs: remove uneeded __KERNEL__ block from cifsacl.h
In reply to a message not in the archive...and make those symbols static in cifsacl.c. Nothing outside of that file refers to them.
--- fs/cifs/cifsacl.c | 4 ++-- fs/cifs/cifsacl.h | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-)
fs/cifs/cifsacl.c +2 -2
@@ -42,7 +42,7 @@ static const struct cifs_sid sid_authusers = {/* group users */ static const struct cifs_sid sid_user = {1 ,2 , {0 ,0 ,0 ,0 ,0 ,5 }, {} }; -const struct cred *root_cred; +static const struct cred *root_cred; static void shrink_idmap_tree(struct rb_root *root, int nr_to_scan, int *nr_rem,
@@ -187,7 +187,7 @@ cifs_idmap_key_destroy(struct key *key)kfree (key->payload.data); } -struct key_type cifs_idmap_key_type = { +static struct key_type cifs_idmap_key_type = { .name ="cifs.idmap" , .instantiate = cifs_idmap_key_instantiate, .destroy = cifs_idmap_key_destroy,
fs/cifs/cifsacl.h +0 -5
@@ -109,9 +109,4 @@ struct cifs_sid_id { struct cifs_sid sid; }; -#ifdef __KERNEL__ -extern struct key_type cifs_idmap_key_type; -extern const struct cred *root_cred; -#endif /* KERNEL */ -#endif /* _CIFSACL_H */
Signature · 1 line
1.7.11.7
PATCH 5/7 cifs: simplify id_to_sid and sid_to_id mapping code
In reply to a message not in the archiveThe cifs.idmap handling code currently causes the kernel to cache the data from userspace twice. It first looks in a rbtree to see if there is a matching entry for the given id. If there isn't then it calls request_key which then checks its cache and then calls out to userland if it doesn't have one. If the userland program establishes a mapping and downcalls with that info, it then gets cached in the keyring and in this rbtree.
Aside from the double memory usage and the performance penalty in doing all of these extra copies, there are some nasty bugs in here too. The code declares four rbtrees and spinlocks to protect them, but only seems to use two of them. The upshot is that the same tree is used to hold (eg) uid:sid and sid:uid mappings. The comparitors aren't equipped to deal with that.
I think we'd be best off to remove a layer of caching in this code. If this was originally done for performance reasons, then that really seems like a premature optimization.
This patch does that -- it removes the rbtrees and the locks that protect them and simply has the code do a request_key call on each call into sid_to_id and id_to_sid. This greatly simplifies this code and should roughly halve the memory utilization from using the idmapping code.
--- fs/cifs/cifsacl.c | 535 +++++++++------------------------------------------- fs/cifs/cifsacl.h | 23 +-- fs/cifs/cifsfs.c | 1 - fs/cifs/cifsproto.h | 1 - 4 files changed, 99 insertions(+), 461 deletions(-)
fs/cifs/cifsacl.c +94 -441
@@ -44,128 +44,6 @@ static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };static const struct cred *root_cred; -static void -shrink_idmap_tree (struct rb_root *root,int nr_to_scan,int *nr_rem, -int *nr_del) -{ -struct rb_node *node; -struct rb_node *tmp; -struct cifs_sid_id *psidid; - - node =rb_first (root); -while (node) { - tmp = node; - node =rb_next (tmp); - psidid = rb_entry(tmp, struct cifs_sid_id, rbnode); -if (nr_to_scan ==0 || *nr_del == nr_to_scan) - ++(*nr_rem); -else { -if (time_after (jiffies, psidid->time +SID_MAP_EXPIRE ) - && psidid->refcount ==0 ) { -rb_erase (tmp, root); - ++(*nr_del); - }else - ++(*nr_rem); - } - } -} - -/* -* Run idmap cache shrinker. -*/ -static int -cifs_idmap_shrinker (struct shrinker *shrink,struct shrink_control *sc) -{ -int nr_to_scan = sc->nr_to_scan; -int nr_del =0 ; -int nr_rem =0 ; -struct rb_root *root; - - root = &uidtree; -spin_lock (&siduidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&siduidlock); - - root = &gidtree; -spin_lock (&sidgidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&sidgidlock); - - root = &siduidtree; -spin_lock (&uidsidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&uidsidlock); - - root = &sidgidtree; -spin_lock (&gidsidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&gidsidlock); - -return nr_rem; -} - -static void -sid_rb_insert (struct rb_root *root,unsigned long cid, -struct cifs_sid_id **psidid,char *typestr) -{ -char *strptr; -struct rb_node *node = root->rb_node; -struct rb_node *parent =NULL ; -struct rb_node **linkto = &(root->rb_node); -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); - parent = node; -if (cid > lsidid->id) { - linkto = &(node->rb_left); - node = node->rb_left; - } -if (cid < lsidid->id) { - linkto = &(node->rb_right); - node = node->rb_right; - } - } - - (*psidid)->id = cid; - (*psidid)->time = jiffies - (SID_MAP_RETRY +1 ); - (*psidid)->refcount =0 ; - -sprintf ((*psidid)->sidstr,"%s" , typestr); - strptr = (*psidid)->sidstr +strlen ((*psidid)->sidstr); -sprintf (strptr,"%ld" , cid); - -clear_bit (SID_ID_PENDING , &(*psidid)->state); -clear_bit (SID_ID_MAPPED , &(*psidid)->state); - -rb_link_node (&(*psidid)->rbnode, parent, linkto); -rb_insert_color (&(*psidid)->rbnode, root); -} - -static struct cifs_sid_id * -sid_rb_search (struct rb_root *root,unsigned long cid) -{ -struct rb_node *node = root->rb_node; -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); -if (cid > lsidid->id) - node = node->rb_left; -else if (cid < lsidid->id) - node = node->rb_right; -else /* node found */ -return lsidid; - } - -return NULL ; -} - -static struct shrinker cifs_shrinker = { - .shrink = cifs_idmap_shrinker, - .seeks =DEFAULT_SEEKS , -}; -static int cifs_idmap_key_instantiate (struct key *key,struct key_preparsed_payload *prep) {
@@ -195,30 +73,39 @@ static struct key_type cifs_idmap_key_type = { .match = user_match, }; -static void -sid_to_str (struct cifs_sid *sidptr,char *sidstr) +static char * +sid_to_key_str (struct cifs_sid *sidptr,unsigned int type) { -int i; +int i, len;unsigned int saval; -char *strptr; +char *sidstr, *strptr; - strptr = sidstr; +/* 3 bytes for prefix */ + sidstr =kmalloc (3 +SID_STRING_BASE_SIZE + + (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth), +GFP_KERNEL ); +if (!sidstr) +return sidstr; -sprintf (strptr,"S-%hhu" , sidptr->revision); - strptr = sidstr +strlen (sidstr); + strptr = sidstr; + len =sprintf (strptr,"%cs:S-%hhu" , type ==SIDOWNER ?'o' :'g' , + sidptr->revision); + strptr += len;for (i =0 ; i <NUM_AUTHS ; ++i) {if (sidptr->authority[i]) { -sprintf (strptr,"-%hhu" , sidptr->authority[i]); - strptr = sidstr +strlen (sidstr); + len =sprintf (strptr,"-%hhu" , sidptr->authority[i]); + strptr += len; } }for (i =0 ; i < sidptr->num_subauth; ++i) { saval =le32_to_cpu (sidptr->sub_auth[i]); -sprintf (strptr,"-%u" , saval); - strptr = sidstr +strlen (sidstr); + len =sprintf (strptr,"-%u" , saval); + strptr += len; } + +return sidstr; }/*
@@ -284,184 +171,38 @@ cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src) dst->sub_auth[i] = src->sub_auth[i]; } -static void -id_rb_insert (struct rb_root *root,struct cifs_sid *sidptr, -struct cifs_sid_id **psidid,char *typestr) -{ -int rc; -char *strptr; -struct rb_node *node = root->rb_node; -struct rb_node *parent =NULL ; -struct rb_node **linkto = &(root->rb_node); -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); - parent = node; - rc =compare_sids (sidptr, &((lsidid)->sid)); -if (rc >0 ) { - linkto = &(node->rb_left); - node = node->rb_left; - }else if (rc <0 ) { - linkto = &(node->rb_right); - node = node->rb_right; - } - } - -cifs_copy_sid (&(*psidid)->sid, sidptr); - (*psidid)->time = jiffies - (SID_MAP_RETRY +1 ); - (*psidid)->refcount =0 ; - -sprintf ((*psidid)->sidstr,"%s" , typestr); - strptr = (*psidid)->sidstr +strlen ((*psidid)->sidstr); -sid_to_str (&(*psidid)->sid, strptr); - -clear_bit (SID_ID_PENDING , &(*psidid)->state); -clear_bit (SID_ID_MAPPED , &(*psidid)->state); - -rb_link_node (&(*psidid)->rbnode, parent, linkto); -rb_insert_color (&(*psidid)->rbnode, root); -} - -static struct cifs_sid_id * -id_rb_search (struct rb_root *root,struct cifs_sid *sidptr) -{ -int rc; -struct rb_node *node = root->rb_node; -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); - rc =compare_sids (sidptr, &((lsidid)->sid)); -if (rc >0 ) { - node = node->rb_left; - }else if (rc <0 ) { - node = node->rb_right; - }else /* node found */ -return lsidid; - } - -return NULL ; -} - -static int -sidid_pending_wait (void *unused) -{ -schedule (); -return signal_pending (current) ? -ERESTARTSYS :0 ; -} -static int -id_to_sid (unsigned long cid,uint sidtype,struct cifs_sid *ssid) +id_to_sid (unsigned int cid,uint sidtype,struct cifs_sid *ssid) { -int rc =0 ; +int rc;struct key *sidkey; +char desc[3 +10 +1 ];/* 3 byte prefix + 10 bytes for value + NULL */ const struct cred *saved_cred; -struct cifs_sid *lsid; -struct cifs_sid_id *psidid, *npsidid; -struct rb_root *cidtree; -spinlock_t *cidlock; - -if (sidtype ==SIDOWNER ) { - cidlock = &siduidlock; - cidtree = &uidtree; - }else if (sidtype ==SIDGROUP ) { - cidlock = &sidgidlock; - cidtree = &gidtree; - }else -return -EINVAL ; - -spin_lock (cidlock); - psidid =sid_rb_search (cidtree, cid); - -if (!psidid) {/* node does not exist, allocate one & attempt adding */ -spin_unlock (cidlock); - npsidid =kzalloc (sizeof (struct cifs_sid_id ),GFP_KERNEL ); -if (!npsidid) -return -ENOMEM ; - - npsidid->sidstr =kmalloc (SID_STRING_MAX ,GFP_KERNEL ); -if (!npsidid->sidstr) { -kfree (npsidid); -return -ENOMEM ; - } - -spin_lock (cidlock); - psidid =sid_rb_search (cidtree, cid); -if (psidid) {/* node happened to get inserted meanwhile */ - ++psidid->refcount; -spin_unlock (cidlock); -kfree (npsidid->sidstr); -kfree (npsidid); - }else { - psidid = npsidid; -sid_rb_insert (cidtree, cid, &psidid, - sidtype ==SIDOWNER ?"oi:" :"gi:" ); - ++psidid->refcount; -spin_unlock (cidlock); - } - }else { - ++psidid->refcount; -spin_unlock (cidlock); - } -/* -* If we are here, it is safe to access psidid and its fields -* since a reference was taken earlier while holding the spinlock. -* A reference on the node is put without holding the spinlock -* and it is OK to do so in this case, shrinker will not erase -* this node until all references are put and we do not access -* any fields of the node after a reference is put . -*/ -if (test_bit (SID_ID_MAPPED , &psidid->state)) { -cifs_copy_sid (ssid, &psidid->sid); - psidid->time = jiffies;/* update ts for accessing */ - goto id_sid_out; - } + rc =snprintf (desc,sizeof (desc),"%ci:%u" , + sidtype ==SIDOWNER ?'o' :'g' , cid); +if (rc >=sizeof (desc)) +return -EINVAL ; -if (time_after (psidid->time +SID_MAP_RETRY , jiffies)) { + rc =0 ; + saved_cred =override_creds (root_cred); + sidkey =request_key (&cifs_idmap_key_type, desc,"" ); +if (IS_ERR (sidkey)) { rc = -EINVAL ; - goto id_sid_out; - } - -if (!test_and_set_bit (SID_ID_PENDING , &psidid->state)) { - saved_cred =override_creds (root_cred); - sidkey =request_key (&cifs_idmap_key_type, psidid->sidstr,"" ); -if (IS_ERR (sidkey)) { - rc = -EINVAL ; -cFYI (1 ,"%s: Can't map and id to a SID" , __func__); - }else if (sidkey->datalen <CIFS_SID_BASE_SIZE ) { - rc = -EIO ; -cFYI (1 ,"%s: Downcall contained malformed key " -"(datalen=%hu)" , __func__, sidkey->datalen); - }else { - lsid = (struct cifs_sid *)sidkey->payload.data; -cifs_copy_sid (&psidid->sid, lsid); -cifs_copy_sid (ssid, &psidid->sid); -set_bit (SID_ID_MAPPED , &psidid->state); -key_put (sidkey); -kfree (psidid->sidstr); - } - psidid->time = jiffies;/* update ts for accessing */ -revert_creds (saved_cred); -clear_bit (SID_ID_PENDING , &psidid->state); -wake_up_bit (&psidid->state,SID_ID_PENDING ); - }else { - rc =wait_on_bit (&psidid->state,SID_ID_PENDING , - sidid_pending_wait,TASK_INTERRUPTIBLE ); -if (rc) { -cFYI (1 ,"%s: sidid_pending_wait interrupted %d" , - __func__, rc); - --psidid->refcount; -return rc; - } -if (test_bit (SID_ID_MAPPED , &psidid->state)) -cifs_copy_sid (ssid, &psidid->sid); -else - rc = -EINVAL ; - } -id_sid_out: - --psidid->refcount; +cFYI (1 ,"%s: Can't map %cid %u to a SID" , __func__, + sidtype ==SIDOWNER ?'u' :'g' , cid); + goto out_revert_creds; + }else if (sidkey->datalen <CIFS_SID_BASE_SIZE ) { + rc = -EIO ; +cFYI (1 ,"%s: Downcall contained malformed key " +"(datalen=%hu)" , __func__, sidkey->datalen); + goto out_key_put; + } +cifs_copy_sid (ssid, (struct cifs_sid *)sidkey->payload.data); +out_key_put: +key_put (sidkey); +out_revert_creds: +revert_creds (saved_cred);return rc; }
@@ -470,111 +211,66 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid, struct cifs_fattr *fattr, uint sidtype) { int rc; -unsigned long cid; -struct key *idkey; +struct key *sidkey; +char *sidstr;const struct cred *saved_cred; -struct cifs_sid_id *psidid, *npsidid; -struct rb_root *cidtree; -spinlock_t *cidlock; - -if (sidtype ==SIDOWNER ) { - cid = cifs_sb->mnt_uid;/* default uid, in case upcall fails */ - cidlock = &siduidlock; - cidtree = &uidtree; - }else if (sidtype ==SIDGROUP ) { - cid = cifs_sb->mnt_gid;/* default gid, in case upcall fails */ - cidlock = &sidgidlock; - cidtree = &gidtree; - }else -return -ENOENT ; - -spin_lock (cidlock); - psidid =id_rb_search (cidtree, psid); - -if (!psidid) {/* node does not exist, allocate one & attempt adding */ -spin_unlock (cidlock); - npsidid =kzalloc (sizeof (struct cifs_sid_id ),GFP_KERNEL ); -if (!npsidid) -return -ENOMEM ; - - npsidid->sidstr =kmalloc (SID_STRING_MAX ,GFP_KERNEL ); -if (!npsidid->sidstr) { -kfree (npsidid); -return -ENOMEM ; - } - -spin_lock (cidlock); - psidid =id_rb_search (cidtree, psid); -if (psidid) {/* node happened to get inserted meanwhile */ - ++psidid->refcount; -spin_unlock (cidlock); -kfree (npsidid->sidstr); -kfree (npsidid); - }else { - psidid = npsidid; -id_rb_insert (cidtree, psid, &psidid, - sidtype ==SIDOWNER ?"os:" :"gs:" ); - ++psidid->refcount; -spin_unlock (cidlock); - } - }else { - ++psidid->refcount; -spin_unlock (cidlock); - } +uid_t fuid = cifs_sb->mnt_uid; +gid_t fgid = cifs_sb->mnt_gid;/* -* If we are here, it is safe to access psidid and its fields -* since a reference was taken earlier while holding the spinlock. -* A reference on the node is put without holding the spinlock -* and it is OK to do so in this case, shrinker will not erase -* this node until all references are put and we do not access -* any fields of the node after a reference is put . +* If we have too many subauthorities, then something is really wrong. +* Just return an error. */ -if (test_bit (SID_ID_MAPPED , &psidid->state)) { - cid = psidid->id; - psidid->time = jiffies;/* update ts for accessing */ - goto sid_to_id_out; +if (unlikely (psid->num_subauth >SID_MAX_SUB_AUTHORITIES )) { +cFYI (1 ,"%s: %u subauthorities is too many!" , __func__, + psid->num_subauth); +return -EIO ; } -if (time_after (psidid->time +SID_MAP_RETRY , jiffies)) - goto sid_to_id_out; - -if (!test_and_set_bit (SID_ID_PENDING , &psidid->state)) { - saved_cred =override_creds (root_cred); - idkey =request_key (&cifs_idmap_key_type, psidid->sidstr,"" ); -if (IS_ERR (idkey)) -cFYI (1 ,"%s: Can't map SID to an id" , __func__); -else { - cid = *(unsigned long *)idkey->payload.value; - psidid->id = cid; -set_bit (SID_ID_MAPPED , &psidid->state); -key_put (idkey); -kfree (psidid->sidstr); - } -revert_creds (saved_cred); - psidid->time = jiffies;/* update ts for accessing */ -clear_bit (SID_ID_PENDING , &psidid->state); -wake_up_bit (&psidid->state,SID_ID_PENDING ); - }else { - rc =wait_on_bit (&psidid->state,SID_ID_PENDING , - sidid_pending_wait,TASK_INTERRUPTIBLE ); -if (rc) { -cFYI (1 ,"%s: sidid_pending_wait interrupted %d" , - __func__, rc); - --psidid->refcount;/* decremented without spinlock */ -return rc; - } -if (test_bit (SID_ID_MAPPED , &psidid->state)) - cid = psidid->id; + sidstr =sid_to_key_str (psid, sidtype); +if (!sidstr) +return -ENOMEM ; + + saved_cred =override_creds (root_cred); + sidkey =request_key (&cifs_idmap_key_type, sidstr,"" ); +if (IS_ERR (sidkey)) { + rc = -EINVAL ; +cFYI (1 ,"%s: Can't map SID %s to a %cid" , __func__, sidstr, + sidtype ==SIDOWNER ?'u' :'g' ); + goto out_revert_creds; + } + +/* +* FIXME: Here we assume that uid_t and gid_t are same size. It's +* probably a safe assumption but might be better to check based on +* sidtype. +*/ +if (sidkey->datalen <sizeof (uid_t)) { + rc = -EIO ; +cFYI (1 ,"%s: Downcall contained malformed key " +"(datalen=%hu)" , __func__, sidkey->datalen); + goto out_key_put; } -sid_to_id_out: - --psidid->refcount;/* decremented without spinlock */ if (sidtype ==SIDOWNER ) - fattr->cf_uid = cid; + fuid = *(uid_t *)sidkey->payload.value;else - fattr->cf_gid = cid; + fgid = *(gid_t *)sidkey->payload.value; +out_key_put: +key_put (sidkey); +out_revert_creds: +revert_creds (saved_cred); +kfree (sidstr); + +/* +* Note that we return 0 here unconditionally. If the mapping +* fails then we just fall back to using the mnt_uid/mnt_gid. +*/ +if (sidtype ==SIDOWNER ) + fattr->cf_uid = fuid; +else + fattr->cf_gid = fgid;return 0 ; }
@@ -621,17 +317,6 @@ init_cifs_idmap(void) cred->jit_keyring =KEY_REQKEY_DEFL_THREAD_KEYRING ; root_cred = cred; -spin_lock_init (&siduidlock); - uidtree =RB_ROOT ; -spin_lock_init (&sidgidlock); - gidtree =RB_ROOT ; - -spin_lock_init (&uidsidlock); - siduidtree =RB_ROOT ; -spin_lock_init (&gidsidlock); - sidgidtree =RB_ROOT ; -register_shrinker (&cifs_shrinker); -cFYI (1 ,"cifs idmap keyring: %d" ,key_serial (keyring));return 0 ;
@@ -648,41 +333,9 @@ exit_cifs_idmap(void)key_revoke (root_cred->thread_keyring);unregister_key_type (&cifs_idmap_key_type);put_cred (root_cred); -unregister_shrinker (&cifs_shrinker);cFYI (1 ,"Unregistered %s key type" , cifs_idmap_key_type.name); } -void -cifs_destroy_idmaptrees (void ) -{ -struct rb_root *root; -struct rb_node *node; - - root = &uidtree; -spin_lock (&siduidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&siduidlock); - - root = &gidtree; -spin_lock (&sidgidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&sidgidlock); - - root = &siduidtree; -spin_lock (&uidsidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&uidsidlock); - - root = &sidgidtree; -spin_lock (&gidsidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&gidsidlock); -} -/* copy ntsd, owner sid, and group sid from a security descriptor to another */ static void copy_sec_desc (const struct cifs_ntsd *pntsd,struct cifs_ntsd *pnntsd,__u32 sidsoffset)
fs/cifs/cifsacl.h +5 -18
@@ -23,7 +23,7 @@#define _CIFSACL_H -#define NUM_AUTHS 6/* number of authority fields */ +#define NUM_AUTHS (6)/* number of authority fields */ #define SID_MAX_SUB_AUTHORITIES (15)/* max number of sub authority fields */ #define NUM_WK_SIDS 7/* number of well known sids */ #define SIDNAMELENGTH 20/* long enough for the ones we care about */
@@ -51,15 +51,12 @@* u32: max 10 bytes in decimal * * "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes -* per number + 1 for '-') + 11 bytes for each subauthority field (10 bytes * per number + 1 for '-') + NULL terminator. +* +* Add 11 bytes for each subauthority field (10 bytes each + 1 for '-') */ -#define SID_STRING_MAX (195) - -#define SID_ID_MAPPED 0 -#define SID_ID_PENDING 1 -#define SID_MAP_EXPIRE (3600 * HZ)/* map entry expires after one hour */ -#define SID_MAP_RETRY (300 * HZ)/* wait 5 minutes for next attempt to map */ +#define SID_STRING_BASE_SIZE (2 + 3 + (4 * NUM_AUTHS) + 1) +#define SID_STRING_SUBAUTH_SIZE (11)/* size of a single subauth string */ struct cifs_ntsd {__le16 revision;/* revision level */
@@ -99,14 +96,4 @@ struct cifs_wksid {char sidname[SIDNAMELENGTH ]; } __attribute__((packed)); -struct cifs_sid_id { -unsigned int refcount;/* increment with spinlock, decrement without */ -unsigned long id; -unsigned long time; -unsigned long state; -char *sidstr; -struct rb_node rbnode; -struct cifs_sid sid; -}; -#endif /* _CIFSACL_H */
fs/cifs/cifsfs.c +0 -1
@@ -1205,7 +1205,6 @@ exit_cifs(void)unregister_filesystem (&cifs_fs_type);cifs_dfs_release_automount_timer ();#ifdef CONFIG_CIFS_ACL -cifs_destroy_idmaptrees ();exit_cifs_idmap ();#endif #ifdef CONFIG_CIFS_UPCALL
fs/cifs/cifsproto.h +0 -1
@@ -58,7 +58,6 @@ do { \ }while (0 ) extern int init_cifs_idmap(void);extern void exit_cifs_idmap (void ); -extern void cifs_destroy_idmaptrees (void );extern char *build_path_from_dentry (struct dentry *);extern char *build_wildcard_path_from_dentry (struct dentry *direntry); extern char *cifs_compose_mount_options(const char *sb_mountdata,
Signature · 1 line
1.7.11.7
A message that is not in the archive (sent off-list or not yet received): 1350497343-7155-6-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Re: [PATCH 5/7] cifs: simplify id_to_sid and sid_to_id mapping code
Quoted 397 lines · Jeff Layton
The cifs.idmap handling code currently causes the kernel to cache the data from userspace twice. It first looks in a rbtree to see if there is a matching entry for the given id. If there isn't then it calls
request_keywhich then checks its cache and then calls out to userland if it doesn't have one. If the userland program establishes a mapping and downcalls with that info, it then gets cached in the keyring and in this rbtree.Aside from the double memory usage and the performance penalty in doing all of these extra copies, there are some nasty bugs in here too. The code declares four rbtrees and spinlocks to protect them, but only seems to use two of them. The upshot is that the same tree is used to hold (eg) uid:sid and sid:uid mappings. The comparitors aren't equipped to deal with that.
I think we'd be best off to remove a layer of caching in this code. If this was originally done for performance reasons, then that really seems like a premature optimization.
This patch does that -- it removes the rbtrees and the locks that protect them and simply has the code do a
request_keycall on each call intosid_to_idandid_to_sid. This greatly simplifies this code and should roughly halve the memory utilization from using the idmapping code.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsacl.c | 535 +++++++++------------------------------------------- fs/cifs/cifsacl.h | 23 +-- fs/cifs/cifsfs.c | 1 - fs/cifs/cifsproto.h | 1 - 4 files changed, 99 insertions(+), 461 deletions(-)fs/cifs/cifsacl.c +0
-122@@ -44,128 +44,6 @@ static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };static const struct cred *root_cred; -static void -shrink_idmap_tree (struct rb_root *root,int nr_to_scan,int *nr_rem, -int *nr_del) -{ -struct rb_node *node; -struct rb_node *tmp; -struct cifs_sid_id *psidid; - - node =rb_first (root); -while (node) { - tmp = node; - node =rb_next (tmp); - psidid = rb_entry(tmp, struct cifs_sid_id, rbnode); -if (nr_to_scan ==0 || *nr_del == nr_to_scan) - ++(*nr_rem); -else { -if (time_after (jiffies, psidid->time + SID_MAP_EXPIRE) - && psidid->refcount ==0 ) { -rb_erase (tmp, root); - ++(*nr_del); - }else - ++(*nr_rem); - } - } -} - -/* -* Run idmap cache shrinker. -*/ -static int -cifs_idmap_shrinker (struct shrinker *shrink,struct shrink_control *sc) -{ -int nr_to_scan = sc->nr_to_scan; -int nr_del =0 ; -int nr_rem =0 ; -struct rb_root *root; - - root = &uidtree; -spin_lock (&siduidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&siduidlock); - - root = &gidtree; -spin_lock (&sidgidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&sidgidlock); - - root = &siduidtree; -spin_lock (&uidsidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&uidsidlock); - - root = &sidgidtree; -spin_lock (&gidsidlock); -shrink_idmap_tree (root, nr_to_scan, &nr_rem, &nr_del); -spin_unlock (&gidsidlock); - -return nr_rem; -} - -static void -sid_rb_insert (struct rb_root *root,unsigned long cid, -struct cifs_sid_id **psidid,char *typestr) -{ -char *strptr; -struct rb_node *node = root->rb_node; -struct rb_node *parent =NULL ; -struct rb_node **linkto = &(root->rb_node); -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); - parent = node; -if (cid > lsidid->id) { - linkto = &(node->rb_left); - node = node->rb_left; - } -if (cid < lsidid->id) { - linkto = &(node->rb_right); - node = node->rb_right; - } - } - - (*psidid)->id = cid; - (*psidid)->time = jiffies - (SID_MAP_RETRY +1 ); - (*psidid)->refcount =0 ; - -sprintf ((*psidid)->sidstr,"%s" , typestr); - strptr = (*psidid)->sidstr +strlen ((*psidid)->sidstr); -sprintf (strptr,"%ld" , cid); - -clear_bit (SID_ID_PENDING , &(*psidid)->state); -clear_bit (SID_ID_MAPPED , &(*psidid)->state); - -rb_link_node (&(*psidid)->rbnode, parent, linkto); -rb_insert_color (&(*psidid)->rbnode, root); -} - -static struct cifs_sid_id * -sid_rb_search (struct rb_root *root,unsigned long cid) -{ -struct rb_node *node = root->rb_node; -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); -if (cid > lsidid->id) - node = node->rb_left; -else if (cid < lsidid->id) - node = node->rb_right; -else /* node found */ -return lsidid; - } - -return NULL ; -} - -static struct shrinker cifs_shrinker = { - .shrink = cifs_idmap_shrinker, - .seeks =DEFAULT_SEEKS , -}; -static int
cifs_idmap_key_instantiate(struct key *key, structkey_preparsed_payload
*prep)
{fs/cifs/cifsacl.c +20
-11@@ -195,30 +73,39 @@ static struct key_type cifs_idmap_key_type = { .match = user_match, }; -static void -sid_to_str (struct cifs_sid *sidptr,char *sidstr) +static char * +sid_to_key_str (struct cifs_sid *sidptr,unsigned int type) { -int i; +int i, len;unsigned int saval; -char *strptr; +char *sidstr, *strptr; - strptr = sidstr; +/* 3 bytes for prefix */ + sidstr =kmalloc (3 +SID_STRING_BASE_SIZE + + (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth), +GFP_KERNEL ); +if (!sidstr) +return sidstr; -sprintf (strptr,"S-%hhu" , sidptr->revision); - strptr = sidstr +strlen (sidstr); + strptr = sidstr; + len =sprintf (strptr,"%cs:S-%hhu" , type ==SIDOWNER ?'o' :'g' , + sidptr->revision); + strptr += len;for (i =0 ; i <NUM_AUTHS ; ++i) {if (sidptr->authority[i]) { -sprintf (strptr,"-%hhu" , sidptr->authority[i]); - strptr = sidstr +strlen (sidstr); + len =sprintf (strptr,"-%hhu" , sidptr->authority[i]); + strptr += len; } }for (i =0 ; i < sidptr->num_subauth; ++i) { saval =le32_to_cpu (sidptr->sub_auth[i]); -sprintf (strptr,"-%u" , saval); - strptr = sidstr +strlen (sidstr); + len =sprintf (strptr,"-%u" , saval); + strptr += len; } + +return sidstr; }/*
fs/cifs/cifsacl.c +3
-69@@ -284,184 +171,38 @@ cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src) dst->sub_auth[i] = src->sub_auth[i]; } -static void -id_rb_insert (struct rb_root *root,struct cifs_sid *sidptr, -struct cifs_sid_id **psidid,char *typestr) -{ -int rc; -char *strptr; -struct rb_node *node = root->rb_node; -struct rb_node *parent =NULL ; -struct rb_node **linkto = &(root->rb_node); -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); - parent = node; - rc =compare_sids (sidptr, &((lsidid)->sid)); -if (rc >0 ) { - linkto = &(node->rb_left); - node = node->rb_left; - }else if (rc <0 ) { - linkto = &(node->rb_right); - node = node->rb_right; - } - } - -cifs_copy_sid (&(*psidid)->sid, sidptr); - (*psidid)->time = jiffies - (SID_MAP_RETRY +1 ); - (*psidid)->refcount =0 ; - -sprintf ((*psidid)->sidstr,"%s" , typestr); - strptr = (*psidid)->sidstr +strlen ((*psidid)->sidstr); -sid_to_str (&(*psidid)->sid, strptr); - -clear_bit (SID_ID_PENDING , &(*psidid)->state); -clear_bit (SID_ID_MAPPED , &(*psidid)->state); - -rb_link_node (&(*psidid)->rbnode, parent, linkto); -rb_insert_color (&(*psidid)->rbnode, root); -} - -static struct cifs_sid_id * -id_rb_search (struct rb_root *root,struct cifs_sid *sidptr) -{ -int rc; -struct rb_node *node = root->rb_node; -struct cifs_sid_id *lsidid; - -while (node) { - lsidid = rb_entry(node, struct cifs_sid_id, rbnode); - rc =compare_sids (sidptr, &((lsidid)->sid)); -if (rc >0 ) { - node = node->rb_left; - }else if (rc <0 ) { - node = node->rb_right; - }else /* node found */ -return lsidid; - } - -return NULL ; -} - -static int -sidid_pending_wait (void *unused) -{ -schedule (); -return signal_pending (current) ? -ERESTARTSYS :0 ; -} -static int -id_to_sid (unsigned long cid,uint sidtype,struct cifs_sid *ssid) +id_to_sid (unsigned int cid,uint sidtype,struct cifs_sid *ssid) { -int rc =0 ; +int rc;struct key *sidkey; +char desc[3 +10 +1 ];/* 3 byte prefix + 10 bytes for value + NULL */
const struct cred *saved_cred;fs/cifs/cifsacl.c +0
-18-struct cifs_sid *lsid; -struct cifs_sid_id *psidid, *npsidid; -struct rb_root *cidtree; -spinlock_t *cidlock; - -if (sidtype ==SIDOWNER ) { - cidlock = &siduidlock; - cidtree = &uidtree; - }else if (sidtype ==SIDGROUP ) { - cidlock = &sidgidlock; - cidtree = &gidtree; - }else -return -EINVAL ; - -spin_lock (cidlock); - psidid =sid_rb_search (cidtree, cid); - -if (!psidid) {/* node does not exist, allocate one & attempt adding */
fs/cifs/cifsacl.c +0
-14-spin_unlock (cidlock); - npsidid =kzalloc (sizeof (struct cifs_sid_id ),GFP_KERNEL ); -if (!npsidid) -return -ENOMEM ; - - npsidid->sidstr =kmalloc (SID_STRING_MAX ,GFP_KERNEL ); -if (!npsidid->sidstr) { -kfree (npsidid); -return -ENOMEM ; - } - -spin_lock (cidlock); - psidid =sid_rb_search (cidtree, cid); -if (psidid) {/* node happened to get inserted meanwhile */
fs/cifs/cifsacl.c +0
-8- ++psidid->refcount; -spin_unlock (cidlock); -kfree (npsidid->sidstr); -kfree (npsidid); - }else { - psidid = npsidid; - sid_rb_insert(cidtree, cid, &psidid, - sidtype == SIDOWNER ? "oi:" :"gi:");
fs/cifs/cifsacl.c +8
-27- ++psidid->refcount; -spin_unlock (cidlock); - } - }else { - ++psidid->refcount; -spin_unlock (cidlock); - } -/* -* If we are here, it is safe to access psidid and its fields -* since a reference was taken earlier while holding the spinlock. -* A reference on the node is put without holding the spinlock -* and it is OK to do so in this case, shrinker will not erase -* this node until all references are put and we do not access -* any fields of the node after a reference is put . -*/ -if (test_bit (SID_ID_MAPPED , &psidid->state)) { -cifs_copy_sid (ssid, &psidid->sid); - psidid->time = jiffies;/* update ts for accessing */ - goto id_sid_out; - } + rc =snprintf (desc,sizeof (desc),"%ci:%u" , + sidtype ==SIDOWNER ?'o' :'g' , cid); +if (rc >=sizeof (desc)) +return -EINVAL ; -if (time_after (psidid->time +SID_MAP_RETRY , jiffies)) { + rc =0 ; + saved_cred =override_creds (root_cred); + sidkey =request_key (&cifs_idmap_key_type, desc,"" ); +if (IS_ERR (sidkey)) { rc = -EINVAL ; - goto id_sid_out; - } - -if (!test_and_set_bit (SID_ID_PENDING , &psidid->state)) { - saved_cred =override_creds (root_cred); - sidkey = request_key(&cifs_idmap_key_type, psidid->sidstr,"");
fs/cifs/cifsacl.c +0
-3-if (IS_ERR (sidkey)) { - rc = -EINVAL ; - cFYI(1, "%s: Can't map and id to a SID",
__func__);fs/cifs/cifsacl.c +0
-4- }else if (sidkey->datalen <CIFS_SID_BASE_SIZE ) { - rc = -EIO ; - cFYI(1, "%s: Downcall contained malformed key " - "(datalen=%hu)", __func__,
sidkey->datalen);
fs/cifs/cifsacl.c cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src) · line 442
Quoted 6 lines
fs/cifs/cifsacl.c +0
-6- }else { - lsid = (struct cifs_sid *)sidkey->payload.data; -cifs_copy_sid (&psidid->sid, lsid); -cifs_copy_sid (ssid, &psidid->sid); -set_bit (SID_ID_MAPPED , &psidid->state); -key_put (sidkey);
All the patches look correct except this question, would not this key_put call end up purging the mapping from the cache. In the current code this is OK since cifsacl has its own cache but once it gone, we are relying on keys API cache to provide mapping.
Quoted 355 lines
fs/cifs/cifsacl.c +16
-30-kfree (psidid->sidstr); - } - psidid->time = jiffies;/* update ts for accessing */ -revert_creds (saved_cred); -clear_bit (SID_ID_PENDING , &psidid->state); -wake_up_bit (&psidid->state,SID_ID_PENDING ); - }else { - rc =wait_on_bit (&psidid->state,SID_ID_PENDING , - sidid_pending_wait,TASK_INTERRUPTIBLE ); -if (rc) { -cFYI (1 ,"%s: sidid_pending_wait interrupted %d" , - __func__, rc); - --psidid->refcount; -return rc; - } -if (test_bit (SID_ID_MAPPED , &psidid->state)) -cifs_copy_sid (ssid, &psidid->sid); -else - rc = -EINVAL ; - } -id_sid_out: - --psidid->refcount; +cFYI (1 ,"%s: Can't map %cid %u to a SID" , __func__, + sidtype ==SIDOWNER ?'u' :'g' , cid); + goto out_revert_creds; + }else if (sidkey->datalen <CIFS_SID_BASE_SIZE ) { + rc = -EIO ; +cFYI (1 ,"%s: Downcall contained malformed key " +"(datalen=%hu)" , __func__, sidkey->datalen); + goto out_key_put; + } +cifs_copy_sid (ssid, (struct cifs_sid *)sidkey->payload.data); +out_key_put: +key_put (sidkey); +out_revert_creds: +revert_creds (saved_cred);return rc; }@@ -470,111 +211,66 @@ sid_to_id(struct cifs_sb_info *cifs_sb, structcifs_sid *psid, struct cifs_fattr *fattr, uint sidtype) { int rc; -unsigned long cid; -struct key *idkey; +struct key *sidkey; +char *sidstr;const struct cred *saved_cred; -struct cifs_sid_id *psidid, *npsidid; -struct rb_root *cidtree; -spinlock_t *cidlock; - -if (sidtype ==SIDOWNER ) { - cid = cifs_sb->mnt_uid;/* default uid, in case upcall fails */
fs/cifs/cifsacl.c +0
-4- cidlock = &siduidlock; - cidtree = &uidtree; - }else if (sidtype ==SIDGROUP ) { - cid = cifs_sb->mnt_gid;/* default gid, in case upcall fails */
fs/cifs/cifsacl.c +0
-9- cidlock = &sidgidlock; - cidtree = &gidtree; - }else -return -ENOENT ; - -spin_lock (cidlock); - psidid =id_rb_search (cidtree, psid); - -if (!psidid) {/* node does not exist, allocate one & attempt adding */
fs/cifs/cifsacl.c +0
-14-spin_unlock (cidlock); - npsidid =kzalloc (sizeof (struct cifs_sid_id ),GFP_KERNEL ); -if (!npsidid) -return -ENOMEM ; - - npsidid->sidstr =kmalloc (SID_STRING_MAX ,GFP_KERNEL ); -if (!npsidid->sidstr) { -kfree (npsidid); -return -ENOMEM ; - } - -spin_lock (cidlock); - psidid =id_rb_search (cidtree, psid); -if (psidid) {/* node happened to get inserted meanwhile */
fs/cifs/cifsacl.c +0
-8- ++psidid->refcount; -spin_unlock (cidlock); -kfree (npsidid->sidstr); -kfree (npsidid); - }else { - psidid = npsidid; - id_rb_insert(cidtree, psid, &psidid, - sidtype == SIDOWNER ? "os:" :"gs:");
fs/cifs/cifsacl.c +3
-13- ++psidid->refcount; -spin_unlock (cidlock); - } - }else { - ++psidid->refcount; -spin_unlock (cidlock); - } +uid_t fuid = cifs_sb->mnt_uid; +gid_t fgid = cifs_sb->mnt_gid;/* -* If we are here, it is safe to access psidid and its fields -* since a reference was taken earlier while holding the spinlock. -* A reference on the node is put without holding the spinlock -* and it is OK to do so in this case, shrinker will not erase -* this node until all references are put and we do not access -* any fields of the node after a reference is put . +* If we have too many subauthorities, then something is really wrong.
fs/cifs/cifsacl.c +5
-10+* Just return an error. */ -if (test_bit (SID_ID_MAPPED , &psidid->state)) { - cid = psidid->id; - psidid->time = jiffies;/* update ts for accessing */ - goto sid_to_id_out; +if (unlikely (psid->num_subauth >SID_MAX_SUB_AUTHORITIES )) { +cFYI (1 ,"%s: %u subauthorities is too many!" , __func__, + psid->num_subauth); +return -EIO ; } -if (time_after (psidid->time +SID_MAP_RETRY , jiffies)) - goto sid_to_id_out; - -if (!test_and_set_bit (SID_ID_PENDING , &psidid->state)) { - saved_cred =override_creds (root_cred); - idkey = request_key(&cifs_idmap_key_type, psidid->sidstr,"");
fs/cifs/cifsacl.c +0
-20-if (IS_ERR (idkey)) -cFYI (1 ,"%s: Can't map SID to an id" , __func__); -else { - cid = *(unsigned long *)idkey->payload.value; - psidid->id = cid; -set_bit (SID_ID_MAPPED , &psidid->state); -key_put (idkey); -kfree (psidid->sidstr); - } -revert_creds (saved_cred); - psidid->time = jiffies;/* update ts for accessing */ -clear_bit (SID_ID_PENDING , &psidid->state); -wake_up_bit (&psidid->state,SID_ID_PENDING ); - }else { - rc =wait_on_bit (&psidid->state,SID_ID_PENDING , - sidid_pending_wait,TASK_INTERRUPTIBLE ); -if (rc) { -cFYI (1 ,"%s: sidid_pending_wait interrupted %d" , - __func__, rc); - --psidid->refcount;/* decremented without spinlock */
fs/cifs/cifsacl.c +9
-4-return rc; - } -if (test_bit (SID_ID_MAPPED , &psidid->state)) - cid = psidid->id; + sidstr =sid_to_key_str (psid, sidtype); +if (!sidstr) +return -ENOMEM ; + + saved_cred =override_creds (root_cred); + sidkey =request_key (&cifs_idmap_key_type, sidstr,"" ); +if (IS_ERR (sidkey)) { + rc = -EINVAL ; + cFYI(1, "%s: Can't map SID %s to a %cid", __func__,sidstr,
fs/cifs/cifsacl.c +7
-0+ sidtype == SIDOWNER ? 'u' : 'g'); + goto out_revert_creds; + } + +/* +* FIXME: Here we assume that uid_t and gid_t are same size. It's +* probably a safe assumption but might be better to check based on
fs/cifs/cifsacl.c +23
-47+* sidtype. +*/ +if (sidkey->datalen <sizeof (uid_t)) { + rc = -EIO ; +cFYI (1 ,"%s: Downcall contained malformed key " +"(datalen=%hu)" , __func__, sidkey->datalen); + goto out_key_put; } -sid_to_id_out: - --psidid->refcount;/* decremented without spinlock */ if (sidtype ==SIDOWNER ) - fattr->cf_uid = cid; + fuid = *(uid_t *)sidkey->payload.value;else - fattr->cf_gid = cid; + fgid = *(gid_t *)sidkey->payload.value; +out_key_put: +key_put (sidkey); +out_revert_creds: +revert_creds (saved_cred); +kfree (sidstr); + +/* +* Note that we return 0 here unconditionally. If the mapping +* fails then we just fall back to using the mnt_uid/mnt_gid. +*/ +if (sidtype ==SIDOWNER ) + fattr->cf_uid = fuid; +else + fattr->cf_gid = fgid;return 0 ; }@@ -621,17 +317,6 @@ init_cifs_idmap(void) cred->jit_keyring =KEY_REQKEY_DEFL_THREAD_KEYRING ; root_cred = cred; -spin_lock_init (&siduidlock); - uidtree =RB_ROOT ; -spin_lock_init (&sidgidlock); - gidtree =RB_ROOT ; - -spin_lock_init (&uidsidlock); - siduidtree =RB_ROOT ; -spin_lock_init (&gidsidlock); - sidgidtree =RB_ROOT ; -register_shrinker (&cifs_shrinker); -cFYI (1 ,"cifs idmap keyring: %d" ,key_serial (keyring));return 0 ;@@ -648,41 +333,9 @@ exit_cifs_idmap(void)key_revoke (root_cred->thread_keyring);unregister_key_type (&cifs_idmap_key_type);put_cred (root_cred); -unregister_shrinker (&cifs_shrinker);cFYI (1 ,"Unregistered %s key type" , cifs_idmap_key_type.name); } -void -cifs_destroy_idmaptrees (void ) -{ -struct rb_root *root; -struct rb_node *node; - - root = &uidtree; -spin_lock (&siduidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&siduidlock); - - root = &gidtree; -spin_lock (&sidgidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&sidgidlock); - - root = &siduidtree; -spin_lock (&uidsidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&uidsidlock); - - root = &sidgidtree; -spin_lock (&gidsidlock); -while ((node =rb_first (root))) -rb_erase (node, root); -spin_unlock (&gidsidlock); -} -/* copy ntsd, owner sid, and group sid from a security descriptor to another */
static void copy_sec_desc(const structcifs_ntsd*pntsd,
structcifs_ntsd*pnntsd,__u32
sidsoffset)fs/cifs/cifsacl.h +1
-1@@ -23,7 +23,7 @@#define _CIFSACL_H -#define NUM_AUTHS 6/* number of authority fields */ +#define NUM_AUTHS (6)/* number of authority fields */ #define SID_MAX_SUB_AUTHORITIES (15)/* max number of sub authority fields */
#defineNUM_WK_SIDS7 /* number of well known sids */ #define SIDNAMELENGTH 20 /* long enough for the ones we care about */fs/cifs/cifsacl.h +0
-0@@ -51,15 +51,12 @@* u32: max 10 bytes in decimal * * "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes
- * per number + 1 for '-') + 11 bytes for each subauthority field (10 bytes
* per number + 1 for '-') + NULL terminator.
+ *
+ * Add 11 bytes for each subauthority field (10 bytes each + 1 for '-') */fs/cifs/cifsacl.h +0
-6-#define SID_STRING_MAX (195) - -#define SID_ID_MAPPED 0 -#define SID_ID_PENDING 1 -#define SID_MAP_EXPIRE (3600 * HZ)/* map entry expires after one hour */ -#define SID_MAP_RETRY (300 * HZ)/* wait 5 minutes for next attempt to map */
fs/cifs/cifsacl.h +2
-0+#define SID_STRING_BASE_SIZE (2 + 3 + (4 * NUM_AUTHS) + 1) +#define SID_STRING_SUBAUTH_SIZE (11)/* size of a single subauth string */
struct
cifs_ntsd{
__le16revision; /* revision level */fs/cifs/cifsacl.h +0
-2@@ -99,14 +96,4 @@ struct cifs_wksid {char sidname[SIDNAMELENGTH ]; } __attribute__((packed)); -struct cifs_sid_id { -unsigned int refcount;/* increment with spinlock, decrement without */
fs/cifs/cifsacl.h +0
-8-unsigned long id; -unsigned long time; -unsigned long state; -char *sidstr; -struct rb_node rbnode; - struct cifs_sid sid; -}; -#endif /* _CIFSACL_H */ fs/cifs/cifsfs.c +0
-1@@ -1205,7 +1205,6 @@ exit_cifs(void)unregister_filesystem (&cifs_fs_type);cifs_dfs_release_automount_timer ();#ifdef CONFIG_CIFS_ACL -cifs_destroy_idmaptrees ();exit_cifs_idmap ();#endif #ifdef CONFIG_CIFS_UPCALL fs/cifs/cifsproto.h +0
-1@@ -58,7 +58,6 @@ do { \ }while (0 ) extern int init_cifs_idmap(void);extern void exit_cifs_idmap (void ); -extern void cifs_destroy_idmaptrees (void );extern char *build_path_from_dentry (struct dentry *);extern char *build_wildcard_path_from_dentry (struct dentry *direntry);extern char *cifs_compose_mount_options(const char
*sb_mountdata,
--
1.7.11.7
A message that is not in the archive (sent off-list or not yet received): CADT32eLyF-FGKYxdYJibm8jOq0ofO_cNOMChG9qQ1zNxpZJhVQ@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CADT32eLyF-FGKYxdYJibm8jOq0ofO_cNOMChG9qQ1zNxpZJhVQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [PATCH 5/7] cifs: simplify id_to_sid and sid_to_id mapping code
Quoted 2 lines · On Thu, 18 Oct 2012 14:51:03 -0500 Shirish Pargaonkar…
On Wed, Oct 17, 2012 at 1:09 PM, Jeff Layton <[email protected]> wrote:
[...]
Quoted 6 lines
All the patches look correct except this question, would not this
key_putcall end up purging the mapping from the cache. In the current code this is OK since cifsacl has its own cache but once it gone, we are relying on keys API cache to provide mapping.
No. The key (aka cache entry) will not garbage collected from the cache (aka the keyring) until it has expired and all references are put. Currently, I don't think we're setting any expiry on the key, which I think means that it'll live in the cache indefinitely.
That does bring up a good point though. We should probably allow these to eventually expire. That's fairly simple to implement though -- any time we do a request key, we can just reset the timeout to some point in the future. I'll plan to add that in.
Thanks for the review.
Signature · 1 line
Jeff Layton <[email protected]>
PATCH 6/7 cifs: avoid extra allocation for small cifs.idmap keys
In reply to a message not in the archiveThe cifs.idmap keytype always allocates memory to hold the payload from userspace. In the common case where we're translating a SID to a UID or GID, we're allocating memory to hold something that's less than or equal to the size of a pointer.
When the payload is the same size as a pointer or smaller, just store it in the payload.value union member instead. That saves us an extra allocation on the sid_to_id upcall.
Note that we have to take extra care to check the datalen when we go to dereference the .data pointer in the union, but the callers now check that as a matter of course anyway.
--- fs/cifs/cifsacl.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
fs/cifs/cifsacl.c +19 -4
@@ -49,6 +49,20 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep) {char *payload; +/* +* If the payload is less than or equal to the size of a pointer, then +* an allocation here is wasteful. Just copy the data directly to the +* payload.value union member instead. +* +* With this however, you must check the datalen before trying to +* dereference payload.data! +*/ +if (prep->datalen <=sizeof (unsigned long )) { + key->payload.value =0 ; +memcpy (&key->payload.value, prep->data, prep->datalen); + key->datalen = prep->datalen; +return 0 ; + } payload =kmalloc (prep->datalen,GFP_KERNEL );if (!payload)return -ENOMEM ;
@@ -62,7 +76,8 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)static inline void cifs_idmap_key_destroy (struct key *key) { -kfree (key->payload.data); +if (key->datalen >sizeof (unsigned long )) +kfree (key->payload.data); }static struct key_type cifs_idmap_key_type = {
@@ -245,7 +260,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,* probably a safe assumption but might be better to check based on * sidtype. */ -if (sidkey->datalen <sizeof (uid_t)) { +if (sidkey->datalen !=sizeof (uid_t)) { rc = -EIO ;cFYI (1 ,"%s: Downcall contained malformed key " "(datalen=%hu)" , __func__, sidkey->datalen);
@@ -253,9 +268,9 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid, }if (sidtype ==SIDOWNER ) - fuid = *(uid_t *)sidkey->payload.value; + fuid = (uid_t )sidkey->payload.value;else - fgid = *(gid_t *)sidkey->payload.value; + fgid = (gid_t )sidkey->payload.value; out_key_put:key_put (sidkey);
Signature · 1 line
1.7.11.7
PATCH 7/7 cifs: don't override the uid/gid in getattr when cifsacl is enabled
In reply to a message not in the archiveIf we're using cifsacl, then we don't want to override the uid/gid with the current uid/gid, since that would prevent you from being able to upcall for this info.
--- fs/cifs/inode.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/cifs/inode.c +4 -3
@@ -1791,11 +1791,12 @@ int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, stat->ino =CIFS_I (inode)->uniqueid;/* -* If on a multiuser mount without unix extensions, and the admin hasn't -* overridden them, set the ownership to the fsuid/fsgid of the current -* process. +* If on a multiuser mount without unix extensions or cifsacl being +* enabled, and the admin hasn't overridden them, set the ownership +* to the fsuid/fsgid of the current process. */ if ((cifs_sb->mnt_cifs_flags &CIFS_MOUNT_MULTIUSER ) && + !(cifs_sb->mnt_cifs_flags &CIFS_MOUNT_CIFS_ACL ) && !tcon->unix_ext) {if (!(cifs_sb->mnt_cifs_flags &CIFS_MOUNT_OVERR_UID )) stat->uid =current_fsuid ();
Signature · 1 line
1.7.11.7