afs: Fix delayed allocation of a cell's anonymous key
Subsystems (2)
David Howells
maintainerPATCH v3 1/1 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.
---
Changes
=======
ver #3)
- Deactivated debugging statement.
ver #2)
- Allocated the anon key on demand to avoid race.
fs/afs/cell.c | 43 ++++++++----------------------------------- fs/afs/internal.h | 1 + fs/afs/security.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 43 deletions(-)
fs/afs/cell.c +8 -35
@@ -140,7 +140,9 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net,return ERR_PTR (-ENOMEM ); } - cell->name =kmalloc (1 + namelen +1 ,GFP_KERNEL ); +/* Allocate the cell name and the key name in one go. */ + cell->name =kmalloc (1 + namelen +1 + +4 + namelen +1 ,GFP_KERNEL );if (!cell->name) {kfree (cell);return ERR_PTR (-ENOMEM );
@@ -151,7 +153,11 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net, cell->name_len = namelen;for (i =0 ; i < namelen; i++) cell->name[i] =tolower (name[i]); - cell->name[i] =0 ; + cell->name[i++] =0 ; + + cell->key_desc = cell->name + i; +memcpy (cell->key_desc,"afs@" ,4 ); +memcpy (cell->key_desc +4 , cell->name, cell->name_len +1 ); cell->net = net;refcount_set (&cell->ref,1 );
@@ -710,33 +716,6 @@ void afs_set_cell_timer(struct afs_cell *cell, unsigned int delay_secs)timer_reduce (&cell->management_timer, jiffies + delay_secs *HZ ); } -/* -* Allocate a key to use as a placeholder for anonymous user security. -*/ -static int afs_alloc_anon_key (struct afs_cell *cell) -{ -struct key *key; -char keyname[4 +AFS_MAXCELLNAME +1 ], *cp, *dp; - -/* Create a key to represent an anonymous user. */ -memcpy (keyname,"afs@" ,4 ); - dp = keyname +4 ; - cp = cell->name; -do { - *dp++ =tolower (*cp); - }while (*cp++); - - key =rxrpc_get_null_key (keyname); -if (IS_ERR (key)) -return PTR_ERR (key); - - cell->anonymous_key = key; - -_debug ("anon key %p{%x}" , - cell->anonymous_key,key_serial (cell->anonymous_key)); -return 0 ; -} -/* * Activate a cell. */
@@ -746,12 +725,6 @@ static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)struct afs_cell *pcell;int ret; -if (!cell->anonymous_key) { - ret =afs_alloc_anon_key (cell); -if (ret <0 ) -return ret; - } - ret =afs_proc_cell_setup (cell);if (ret <0 )return ret;
fs/afs/internal.h +1 -0
@@ -413,6 +413,7 @@ struct afs_cell {u8 name_len;/* Length of name */ char *name;/* Cell name, case-flattened and NUL-padded */ +char *key_desc;/* Authentication key description */ };/*
fs/afs/security.c +40 -8
87 lines not on this page · raw message
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi David,
kernel test robot noticed the following build warnings:
[auto build test WARNING on linus/master]
[also build test WARNING on v6.18-rc7 next-20251128]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/David-Howells/afs-Fix-delayed-allocation-of-a-cell-s-anonymous-key/20251128-182016 base: linus/master patch link: https://lore.kernel.org/r/800328.1764325145%40warthog.procyon.org.uk patch subject: [PATCH v3] afs: Fix delayed allocation of a cell's anonymous key config: riscv-allmodconfig (https://download.01.org/0day-ci/archive/20251129/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project b3428bb966f1de8aa48375ffee0eba04ede133b7) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251129/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> fs/afs/security.c:29:6: warning: variable 'key' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
29 | if (!cell->anonymous_key) {
| ^~~~~~~~~~~~~~~~~~~~
fs/afs/security.c:36:13: note: uninitialized use occurs here
36 | if (IS_ERR(key))
| ^~~
fs/afs/security.c:29:2: note: remove the 'if' if its condition is always true
29 | if (!cell->anonymous_key) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~
fs/afs/security.c:26:17: note: initialize the variable 'key' to silence this warning
26 | struct key *key;
| ^
| = NULL1 warning generated.
vim +29 fs/afs/security.c
20
21 /*
22 * Allocate a key to use as a placeholder for anonymous user security.
23 */
24 static 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 }
43 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
David Howells
maintainerQuoted 1 line · kernel test robot
Quoted 1 line
Quoted 1 line
fs/afs/security.c:29:6: warning: variable 'key' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
This is already fixed by:
commit 9eef1d98eeda3745df35839190b7d4a4adea656 afs: Fix uninit var in afs_alloc_anon_key()
Thanks,
David