Skip to content

Commit f3b1733

Browse files
committed
Merge tag 'vfs-6.18-rc8.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
Pull vfs fixes from Christian Brauner: - 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. The normal key lookup tries to use the key description on the anonymous authentication key as the reference for request_key() - but it may not yet be set, causing an oops - ovl: fail ovl_lock_rename_workdir() if either target is unhashed As well as checking that the parent hasn't changed after getting the lock, the code needs to check that the dentry hasn't been unhashed. Otherwise overlayfs might try to rename something that has been removed - namespace: fix a reference leak in grab_requested_mnt_ns lookup_mnt_ns() already takes a reference on mnt_ns, and so grab_requested_mnt_ns() doesn't need to take an extra reference * tag 'vfs-6.18-rc8.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: afs: Fix delayed allocation of a cell's anonymous key ovl: fail ovl_lock_rename_workdir() if either target is unhashed fs/namespace: fix reference leak in grab_requested_mnt_ns
2 parents 7fa0d77 + d27c712 commit f3b1733

5 files changed

Lines changed: 55 additions & 48 deletions

File tree

fs/afs/cell.c

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net,
140140
return ERR_PTR(-ENOMEM);
141141
}
142142

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);
144146
if (!cell->name) {
145147
kfree(cell);
146148
return ERR_PTR(-ENOMEM);
@@ -151,7 +153,11 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net,
151153
cell->name_len = namelen;
152154
for (i = 0; i < namelen; i++)
153155
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);
155161

156162
cell->net = net;
157163
refcount_set(&cell->ref, 1);
@@ -710,33 +716,6 @@ void afs_set_cell_timer(struct afs_cell *cell, unsigned int delay_secs)
710716
timer_reduce(&cell->management_timer, jiffies + delay_secs * HZ);
711717
}
712718

713-
/*
714-
* Allocate a key to use as a placeholder for anonymous user security.
715-
*/
716-
static 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-
740719
/*
741720
* Activate a cell.
742721
*/
@@ -746,12 +725,6 @@ static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
746725
struct afs_cell *pcell;
747726
int ret;
748727

749-
if (!cell->anonymous_key) {
750-
ret = afs_alloc_anon_key(cell);
751-
if (ret < 0)
752-
return ret;
753-
}
754-
755728
ret = afs_proc_cell_setup(cell);
756729
if (ret < 0)
757730
return ret;

fs/afs/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ struct afs_cell {
413413

414414
u8 name_len; /* Length of name */
415415
char *name; /* Cell name, case-flattened and NUL-padded */
416+
char *key_desc; /* Authentication key description */
416417
};
417418

418419
/*

fs/afs/security.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,56 @@
1616

1717
static DEFINE_HASHTABLE(afs_permits_cache, 10);
1818
static DEFINE_SPINLOCK(afs_permits_lock);
19+
static DEFINE_MUTEX(afs_key_lock);
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+
}
1943

2044
/*
2145
* get a key
2246
*/
2347
struct key *afs_request_key(struct afs_cell *cell)
2448
{
2549
struct key *key;
50+
int ret;
2651

27-
_enter("{%x}", key_serial(cell->anonymous_key));
52+
_enter("{%s}", cell->key_desc);
2853

29-
_debug("key %s", cell->anonymous_key->description);
30-
key = request_key_net(&key_type_rxrpc, cell->anonymous_key->description,
54+
_debug("key %s", cell->key_desc);
55+
key = request_key_net(&key_type_rxrpc, cell->key_desc,
3156
cell->net->net, NULL);
3257
if (IS_ERR(key)) {
3358
if (PTR_ERR(key) != -ENOKEY) {
3459
_leave(" = %ld", PTR_ERR(key));
3560
return key;
3661
}
3762

63+
if (!cell->anonymous_key) {
64+
ret = afs_alloc_anon_key(cell);
65+
if (ret < 0)
66+
return ERR_PTR(ret);
67+
}
68+
3869
/* act as anonymous user */
3970
_leave(" = {%x} [anon]", key_serial(cell->anonymous_key));
4071
return key_get(cell->anonymous_key);
@@ -52,11 +83,10 @@ struct key *afs_request_key_rcu(struct afs_cell *cell)
5283
{
5384
struct key *key;
5485

55-
_enter("{%x}", key_serial(cell->anonymous_key));
86+
_enter("{%s}", cell->key_desc);
5687

57-
_debug("key %s", cell->anonymous_key->description);
58-
key = request_key_net_rcu(&key_type_rxrpc,
59-
cell->anonymous_key->description,
88+
_debug("key %s", cell->key_desc);
89+
key = request_key_net_rcu(&key_type_rxrpc, cell->key_desc,
6090
cell->net->net);
6191
if (IS_ERR(key)) {
6292
if (PTR_ERR(key) != -ENOKEY) {
@@ -65,6 +95,8 @@ struct key *afs_request_key_rcu(struct afs_cell *cell)
6595
}
6696

6797
/* act as anonymous user */
98+
if (!cell->anonymous_key)
99+
return NULL; /* Need to allocate */
68100
_leave(" = {%x} [anon]", key_serial(cell->anonymous_key));
69101
return key_get(cell->anonymous_key);
70102
} else {
@@ -408,7 +440,7 @@ int afs_permission(struct mnt_idmap *idmap, struct inode *inode,
408440

409441
if (mask & MAY_NOT_BLOCK) {
410442
key = afs_request_key_rcu(vnode->volume->cell);
411-
if (IS_ERR(key))
443+
if (IS_ERR_OR_NULL(key))
412444
return -ECHILD;
413445

414446
ret = -ECHILD;

fs/namespace.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5746,6 +5746,8 @@ static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq
57465746

57475747
if (kreq->mnt_ns_id) {
57485748
mnt_ns = lookup_mnt_ns(kreq->mnt_ns_id);
5749+
if (!mnt_ns)
5750+
return ERR_PTR(-ENOENT);
57495751
} else if (kreq->mnt_ns_fd) {
57505752
struct ns_common *ns;
57515753

@@ -5761,13 +5763,12 @@ static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq
57615763
return ERR_PTR(-EINVAL);
57625764

57635765
mnt_ns = to_mnt_ns(ns);
5766+
refcount_inc(&mnt_ns->passive);
57645767
} else {
57655768
mnt_ns = current->nsproxy->mnt_ns;
5769+
refcount_inc(&mnt_ns->passive);
57665770
}
5767-
if (!mnt_ns)
5768-
return ERR_PTR(-ENOENT);
57695771

5770-
refcount_inc(&mnt_ns->passive);
57715772
return mnt_ns;
57725773
}
57735774

fs/overlayfs/util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,9 +1234,9 @@ int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *work,
12341234
goto err;
12351235
if (trap)
12361236
goto err_unlock;
1237-
if (work && work->d_parent != workdir)
1237+
if (work && (work->d_parent != workdir || d_unhashed(work)))
12381238
goto err_unlock;
1239-
if (upper && upper->d_parent != upperdir)
1239+
if (upper && (upper->d_parent != upperdir || d_unhashed(upper)))
12401240
goto err_unlock;
12411241

12421242
return 0;

0 commit comments

Comments
 (0)