History

afs: Fix delayed allocation of a cell's anonymous key

The allocation of a cell's anonymous key is done in a background thread along with other cell setup such as doing a DNS upcall. In the reported bug, this is triggered by afs_parse_source() parsing the device name given to mount() and calling afs_lookup_cell() with the name of the cell. The normal key lookup then tries to use the key description on the anonymous authentication key as the reference for request_key() - but it may not yet be set and so an oops can happen. This has been made more likely to happen by the fix for dynamic lookup failure. Fix this by firstly allocating a reference name and attaching it to the afs_cell record when the record is created. It can share the memory allocation with the cell name (unfortunately it can't just overlap the cell name by prepending it with "afs@" as the cell name already has a '.' prepended for other purposes). This reference name is then passed to request_key(). Secondly, the anon key is now allocated on demand at the point a key is requested in afs_request_key() if it is not already allocated. A mutex is used to prevent multiple allocation for a cell. Thirdly, make afs_request_key_rcu() return NULL if the anonymous key isn't yet allocated (if we need it) and then the caller can return -ECHILD to drop out of RCU-mode and afs_request_key() can be called. Note that the anonymous key is kind of necessary to make the key lookup cache work as that doesn't currently cache a negative lookup, but it's probably worth some investigation to see if NULL can be used instead.

David Howells committed 11 months ago

Commit d27c7121 parent e9c70083 files · +49 −43Browse files

Unified · Split

3 files
  1. fs/afs/cell.c +8 −35
  2. fs/afs/internal.h +1 −0
  3. fs/afs/security.c +40 −8
fs/afs/cell.c+8−35
@@ -140,7 +140,9 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net, · 139 unmodified lines
140 return ERR_PTR(-ENOMEM);140 return ERR_PTR(-ENOMEM);
141 }141 }
142 142
143 cell->name = kmalloc(1 + namelen + 1, GFP_KERNEL);143 /* Allocate the cell name and the key name in one go. */
144 cell->name = kmalloc(1 + namelen + 1 +
145 4 + namelen + 1, GFP_KERNEL);
144 if (!cell->name) {146 if (!cell->name) {
145 kfree(cell);147 kfree(cell);
146 return ERR_PTR(-ENOMEM);148 return ERR_PTR(-ENOMEM);
@@ -151,7 +153,11 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net, · 4 unmodified lines
151 cell->name_len = namelen;153 cell->name_len = namelen;
152 for (i = 0; i < namelen; i++)154 for (i = 0; i < namelen; i++)
153 cell->name[i] = tolower(name[i]);155 cell->name[i] = tolower(name[i]);
154 cell->name[i] = 0;156 cell->name[i++] = 0;
157
158 cell->key_desc = cell->name + i;
159 memcpy(cell->key_desc, "afs@", 4);
160 memcpy(cell->key_desc + 4, cell->name, cell->name_len + 1);
155 161
156 cell->net = net;162 cell->net = net;
157 refcount_set(&cell->ref, 1);163 refcount_set(&cell->ref, 1);
@@ -710,33 +716,6 @@ void afs_set_cell_timer(struct afs_cell *cell, unsigned int delay_secs) · 552 unmodified lines
710 timer_reduce(&cell->management_timer, jiffies + delay_secs * HZ);716 timer_reduce(&cell->management_timer, jiffies + delay_secs * HZ);
711}717}
712 718
713/*
714 * Allocate a key to use as a placeholder for anonymous user security.
715 */
716static int afs_alloc_anon_key(struct afs_cell *cell)
717{
718 struct key *key;
719 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
720
721 /* Create a key to represent an anonymous user. */
722 memcpy(keyname, "afs@", 4);
723 dp = keyname + 4;
724 cp = cell->name;
725 do {
726 *dp++ = tolower(*cp);
727 } while (*cp++);
728
729 key = rxrpc_get_null_key(keyname);
730 if (IS_ERR(key))
731 return PTR_ERR(key);
732
733 cell->anonymous_key = key;
734
735 _debug("anon key %p{%x}",
736 cell->anonymous_key, key_serial(cell->anonymous_key));
737 return 0;
738}
739
740/*719/*
741 * Activate a cell.720 * Activate a cell.
742 */721 */
@@ -746,12 +725,6 @@ static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell) · 3 unmodified lines
746 struct afs_cell *pcell;725 struct afs_cell *pcell;
747 int ret;726 int ret;
748 727
749 if (!cell->anonymous_key) {
750 ret = afs_alloc_anon_key(cell);
751 if (ret < 0)
752 return ret;
753 }
754
755 ret = afs_proc_cell_setup(cell);728 ret = afs_proc_cell_setup(cell);
756 if (ret < 0)729 if (ret < 0)
757 return ret;730 return ret;
fs/afs/internal.h+1−0
@@ -413,6 +413,7 @@ struct afs_cell { · 412 unmodified lines
413 413
414 u8 name_len; /* Length of name */414 u8 name_len; /* Length of name */
415 char *name; /* Cell name, case-flattened and NUL-padded */415 char *name; /* Cell name, case-flattened and NUL-padded */
416 char *key_desc; /* Authentication key description */
416};417};
417 418
418/*419/*
fs/afs/security.c+40−8
@@ -16,6 +16,30 @@ · 15 unmodified lines
16 16
17static DEFINE_HASHTABLE(afs_permits_cache, 10);17static DEFINE_HASHTABLE(afs_permits_cache, 10);
18static DEFINE_SPINLOCK(afs_permits_lock);18static DEFINE_SPINLOCK(afs_permits_lock);
19static DEFINE_MUTEX(afs_key_lock);
20
21/*
22 * Allocate a key to use as a placeholder for anonymous user security.
23 */
24static int afs_alloc_anon_key(struct afs_cell *cell)
25{
26 struct key *key;
27
28 mutex_lock(&afs_key_lock);
29 if (!cell->anonymous_key) {
30 key = rxrpc_get_null_key(cell->key_desc);
31 if (!IS_ERR(key))
32 cell->anonymous_key = key;
33 }
34 mutex_unlock(&afs_key_lock);
35
36 if (IS_ERR(key))
37 return PTR_ERR(key);
38
39 _debug("anon key %p{%x}",
40 cell->anonymous_key, key_serial(cell->anonymous_key));
41 return 0;
42}
19 43
20/*44/*
21 * get a key45 * get a key
@@ -23,11 +47,12 @@ static DEFINE_SPINLOCK(afs_permits_lock); · 1 unmodified line
23struct key *afs_request_key(struct afs_cell *cell)47struct key *afs_request_key(struct afs_cell *cell)
24{48{
25 struct key *key;49 struct key *key;
50 int ret;
26 51
27 _enter("{%x}", key_serial(cell->anonymous_key));52 _enter("{%s}", cell->key_desc);
28 53
29 _debug("key %s", cell->anonymous_key->description);54 _debug("key %s", cell->key_desc);
30 key = request_key_net(&key_type_rxrpc, cell->anonymous_key->description,55 key = request_key_net(&key_type_rxrpc, cell->key_desc,
31 cell->net->net, NULL);56 cell->net->net, NULL);
32 if (IS_ERR(key)) {57 if (IS_ERR(key)) {
33 if (PTR_ERR(key) != -ENOKEY) {58 if (PTR_ERR(key) != -ENOKEY) {
@@ -35,6 +60,12 @@ struct key *afs_request_key(struct afs_cell *cell) · 1 unmodified line
35 return key;60 return key;
36 }61 }
37 62
63 if (!cell->anonymous_key) {
64 ret = afs_alloc_anon_key(cell);
65 if (ret < 0)
66 return ERR_PTR(ret);
67 }
68
38 /* act as anonymous user */69 /* act as anonymous user */
39 _leave(" = {%x} [anon]", key_serial(cell->anonymous_key));70 _leave(" = {%x} [anon]", key_serial(cell->anonymous_key));
40 return key_get(cell->anonymous_key);71 return key_get(cell->anonymous_key);
@@ -52,11 +83,10 @@ struct key *afs_request_key_rcu(struct afs_cell *cell) · 11 unmodified lines
52{83{
53 struct key *key;84 struct key *key;
54 85
55 _enter("{%x}", key_serial(cell->anonymous_key));86 _enter("{%s}", cell->key_desc);
56 87
57 _debug("key %s", cell->anonymous_key->description);88 _debug("key %s", cell->key_desc);
58 key = request_key_net_rcu(&key_type_rxrpc,89 key = request_key_net_rcu(&key_type_rxrpc, cell->key_desc,
59 cell->anonymous_key->description,
60 cell->net->net);90 cell->net->net);
61 if (IS_ERR(key)) {91 if (IS_ERR(key)) {
62 if (PTR_ERR(key) != -ENOKEY) {92 if (PTR_ERR(key) != -ENOKEY) {
@@ -65,6 +95,8 @@ struct key *afs_request_key_rcu(struct afs_cell *cell) · 2 unmodified lines
65 }95 }
66 96
67 /* act as anonymous user */97 /* act as anonymous user */
98 if (!cell->anonymous_key)
99 return NULL; /* Need to allocate */
68 _leave(" = {%x} [anon]", key_serial(cell->anonymous_key));100 _leave(" = {%x} [anon]", key_serial(cell->anonymous_key));
69 return key_get(cell->anonymous_key);101 return key_get(cell->anonymous_key);
70 } else {102 } else {
@@ -408,7 +440,7 @@ int afs_permission(struct mnt_idmap *idmap, struct inode *inode, · 337 unmodified lines
408 440
409 if (mask & MAY_NOT_BLOCK) {441 if (mask & MAY_NOT_BLOCK) {
410 key = afs_request_key_rcu(vnode->volume->cell);442 key = afs_request_key_rcu(vnode->volume->cell);
411 if (IS_ERR(key))443 if (IS_ERR_OR_NULL(key))
412 return -ECHILD;444 return -ECHILD;
413 445
414 ret = -ECHILD;446 ret = -ECHILD;