Skip to content

Commit e5c5f35

Browse files
Erick ArcherPaolo Abeni
authored andcommitted
sctp: prefer struct_size over open coded arithmetic
This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "ids" variable is a pointer to "struct sctp_assoc_ids" and this structure ends in a flexible array: struct sctp_assoc_ids { [...] sctp_assoc_t gaids_assoc_id[]; }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + size * count" in the kmalloc() function. Also, refactor the code adding the "ids_size" variable to avoid sizing twice. This way, the code is more readable and safer. This code was detected with the help of Coccinelle, and audited and modified manually. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: KSPP/linux#160 [2] Signed-off-by: Erick Archer <[email protected]> Acked-by: Xin Long <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/PAXPR02MB724871DB78375AB06B5171C88B152@PAXPR02MB7248.eurprd02.prod.outlook.com Signed-off-by: Paolo Abeni <[email protected]>
1 parent 9f02bb6 commit e5c5f35

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

net/sctp/socket.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7119,6 +7119,7 @@ static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
71197119
struct sctp_sock *sp = sctp_sk(sk);
71207120
struct sctp_association *asoc;
71217121
struct sctp_assoc_ids *ids;
7122+
size_t ids_size;
71227123
u32 num = 0;
71237124

71247125
if (sctp_style(sk, TCP))
@@ -7131,11 +7132,11 @@ static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
71317132
num++;
71327133
}
71337134

7134-
if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7135+
ids_size = struct_size(ids, gaids_assoc_id, num);
7136+
if (len < ids_size)
71357137
return -EINVAL;
71367138

7137-
len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7138-
7139+
len = ids_size;
71397140
ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
71407141
if (unlikely(!ids))
71417142
return -ENOMEM;

0 commit comments

Comments
 (0)