Skip to content

Commit b318494

Browse files
committed
net: fix out-of-bounds access in ops_init
jira VULN-8240 cve CVE-2024-36883 commit-author Thadeu Lima de Souza Cascardo <cascardo@igalia.com> commit a26ff37 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 <cascardo@igalia.com> Fixes: 073862b ("netns: fix net_alloc_generic()") Reviewed-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20240502132006.3430840-1-cascardo@igalia.com Signed-off-by: Paolo Abeni <pabeni@redhat.com> (cherry picked from commit a26ff37) Signed-off-by: Brett Mastbergen <bmastbergen@ciq.com>
1 parent b97f8a6 commit b318494

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
@@ -68,12 +68,15 @@ DEFINE_COOKIE(net_cookie);
6868

6969
static struct net_generic *net_alloc_generic(void)
7070
{
71+
unsigned int gen_ptrs = READ_ONCE(max_gen_ptrs);
72+
unsigned int generic_size;
7173
struct net_generic *ng;
72-
unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
74+
75+
generic_size = offsetof(struct net_generic, ptr[gen_ptrs]);
7376

7477
ng = kzalloc(generic_size, GFP_KERNEL);
7578
if (ng)
76-
ng->s.len = max_gen_ptrs;
79+
ng->s.len = gen_ptrs;
7780

7881
return ng;
7982
}
@@ -1210,7 +1213,11 @@ static int register_pernet_operations(struct list_head *list,
12101213
if (error < 0)
12111214
return error;
12121215
*ops->id = error;
1213-
max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
1216+
/* This does not require READ_ONCE as writers already hold
1217+
* pernet_ops_rwsem. But WRITE_ONCE is needed to protect
1218+
* net_alloc_generic.
1219+
*/
1220+
WRITE_ONCE(max_gen_ptrs, max(max_gen_ptrs, *ops->id + 1));
12141221
}
12151222
error = __register_pernet_operations(list, ops);
12161223
if (error) {

0 commit comments

Comments
 (0)