Commit a26ff37e authored by Thadeu Lima de Souza Cascardo's avatar Thadeu Lima de Souza Cascardo Committed by Paolo Abeni

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: default avatarThadeu Lima de Souza Cascardo <cascardo@igalia.com>
Fixes: 073862ba ("netns: fix net_alloc_generic()")
Reviewed-by: default avatarEric Dumazet <edumazet@google.com>
Reviewed-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20240502132006.3430840-1-cascardo@igalia.comSigned-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parent fa870b45
...@@ -69,12 +69,15 @@ DEFINE_COOKIE(net_cookie); ...@@ -69,12 +69,15 @@ DEFINE_COOKIE(net_cookie);
static struct net_generic *net_alloc_generic(void) static struct net_generic *net_alloc_generic(void)
{ {
unsigned int gen_ptrs = READ_ONCE(max_gen_ptrs);
unsigned int generic_size;
struct net_generic *ng; struct net_generic *ng;
unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
generic_size = offsetof(struct net_generic, ptr[gen_ptrs]);
ng = kzalloc(generic_size, GFP_KERNEL); ng = kzalloc(generic_size, GFP_KERNEL);
if (ng) if (ng)
ng->s.len = max_gen_ptrs; ng->s.len = gen_ptrs;
return ng; return ng;
} }
...@@ -1307,7 +1310,11 @@ static int register_pernet_operations(struct list_head *list, ...@@ -1307,7 +1310,11 @@ static int register_pernet_operations(struct list_head *list,
if (error < 0) if (error < 0)
return error; return error;
*ops->id = error; *ops->id = error;
max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1); /* This does not require READ_ONCE as writers already hold
* pernet_ops_rwsem. But WRITE_ONCE is needed to protect
* net_alloc_generic.
*/
WRITE_ONCE(max_gen_ptrs, max(max_gen_ptrs, *ops->id + 1));
} }
error = __register_pernet_operations(list, ops); error = __register_pernet_operations(list, ops);
if (error) { if (error) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment