Skip to content

Commit a26ff37

Browse files
Thadeu Lima de Souza CascardoPaolo Abeni
authored andcommitted
net: fix out-of-bounds access in ops_init
net_alloc_generic is called by net_alloc, which is called without any locking. It reads max_gen_ptrs, which is changed under pernet_ops_rwsem. It is read twice, first to allocate an array, then to set s.len, which is later used to limit the bounds of the array access. It is possible that the array is allocated and another thread is registering a new pernet ops, increments max_gen_ptrs, which is then used to set s.len with a larger than allocated length for the variable array. Fix it by reading max_gen_ptrs only once in net_alloc_generic. If max_gen_ptrs is later incremented, it will be caught in net_assign_generic. Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]> Fixes: 073862b ("netns: fix net_alloc_generic()") Reviewed-by: Eric Dumazet <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent fa870b4 commit a26ff37

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

net/core/net_namespace.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ DEFINE_COOKIE(net_cookie);
6969

7070
static struct net_generic *net_alloc_generic(void)
7171
{
72+
unsigned int gen_ptrs = READ_ONCE(max_gen_ptrs);
73+
unsigned int generic_size;
7274
struct net_generic *ng;
73-
unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
75+
76+
generic_size = offsetof(struct net_generic, ptr[gen_ptrs]);
7477

7578
ng = kzalloc(generic_size, GFP_KERNEL);
7679
if (ng)
77-
ng->s.len = max_gen_ptrs;
80+
ng->s.len = gen_ptrs;
7881

7982
return ng;
8083
}
@@ -1307,7 +1310,11 @@ static int register_pernet_operations(struct list_head *list,
13071310
if (error < 0)
13081311
return error;
13091312
*ops->id = error;
1310-
max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
1313+
/* This does not require READ_ONCE as writers already hold
1314+
* pernet_ops_rwsem. But WRITE_ONCE is needed to protect
1315+
* net_alloc_generic.
1316+
*/
1317+
WRITE_ONCE(max_gen_ptrs, max(max_gen_ptrs, *ops->id + 1));
13111318
}
13121319
error = __register_pernet_operations(list, ops);
13131320
if (error) {

0 commit comments

Comments
 (0)