* [PATCH net-next] sctp: improve memory allocation patterns
@ 2026-12-01 5:39 Jamie Bainbridge
2026-12-01 21:27 ` Xin Long
2026-12-04 5:00 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Jamie Bainbridge @ 2026-12-01 5:39 UTC (permalink / raw)
To: Marcelo Ricardo Leitner, Xin Long, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
Kees Cook, Gustavo A. R. Silva
Cc: Jamie Bainbridge, linux-sctp, netdev, linux-hardening
Much of SCTP was written before helpers such as kmalloc_flex() for
safely allocating structs with Flexible Array Members.
There are also some instances of allocating an object using the object's
literal type as the size, rather than dereferencing the object type.
Use modern alloc wrappers and dereference types where appropriate.
Annotate sctp_auth_bytes.data[] with __counted_by() to enable checking.
Use a macro instead of magic number in endpoint hashtable creation.
Only set the global hashsize if allocating the hashtable succeeded.
Tested under KASAN with tests confirmed by gcov to exercise this code.
Signed-off-by: Jamie Bainbridge <jamie.bainbridge@gmail.com>
---
include/net/sctp/auth.h | 2 +-
include/net/sctp/structs.h | 2 ++
net/sctp/auth.c | 8 ++++----
net/sctp/chunk.c | 2 +-
net/sctp/protocol.c | 4 ++--
net/sctp/stream.c | 2 +-
6 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/include/net/sctp/auth.h b/include/net/sctp/auth.h
index 85efa65d3912280b0e93ea7fb34bc32dc96ccc48..84e0a9f3405b1f11014c7587a297bd7979626cd4 100644
--- a/include/net/sctp/auth.h
+++ b/include/net/sctp/auth.h
@@ -37,7 +37,7 @@ struct sctp_hmac {
struct sctp_auth_bytes {
refcount_t refcnt;
__u32 len;
- __u8 data[];
+ __u8 data[] __counted_by(len);
};
/* Definition for a shared key, weather endpoint or association */
diff --git a/include/net/sctp/structs.h b/include/net/sctp/structs.h
index b21f23b736fd20acef0bc8d79b1ee3821565f235..ba966eeee97b348ee8ecd970da1dd5397416fdda 100644
--- a/include/net/sctp/structs.h
+++ b/include/net/sctp/structs.h
@@ -138,6 +138,8 @@ extern struct sctp_globals {
#define sctp_transport_hashtable (sctp_globals.transport_hashtable)
#define sctp_checksum_disable (sctp_globals.checksum_disable)
+#define SCTP_EP_INITIAL_HASHSIZE 64
+
/* SCTP Socket type: UDP or TCP style. */
enum sctp_socket_type {
SCTP_SOCKET_UDP = 0,
diff --git a/net/sctp/auth.c b/net/sctp/auth.c
index cbeb8a87a17a7333986b9b168b89915012f20a84..2b9b3ca6b67406286787870e71d176b39970a853 100644
--- a/net/sctp/auth.c
+++ b/net/sctp/auth.c
@@ -65,7 +65,7 @@ static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
return NULL;
/* Allocate the shared key */
- key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
+ key = kmalloc_flex(*key, data, key_len, gfp);
if (!key)
return NULL;
@@ -82,7 +82,7 @@ struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
struct sctp_shared_key *new;
/* Allocate the shared key container */
- new = kzalloc_obj(struct sctp_shared_key, gfp);
+ new = kzalloc_obj(*new, gfp);
if (!new)
return NULL;
@@ -1027,8 +1027,8 @@ int sctp_auth_init(struct sctp_endpoint *ep, gfp_t gfp)
if (!ep->auth_chunk_list) {
struct sctp_chunks_param *auth_chunks;
- auth_chunks = kzalloc(sizeof(*auth_chunks) +
- SCTP_NUM_CHUNK_TYPES, gfp);
+ auth_chunks = kzalloc_flex(*auth_chunks, chunks,
+ SCTP_NUM_CHUNK_TYPES, gfp);
if (!auth_chunks)
goto nomem;
/* Initialize the CHUNKS parameter */
diff --git a/net/sctp/chunk.c b/net/sctp/chunk.c
index 5b889e89e9f2b6dbc089476b3f115bb99f10b229..65f38644ae0fb8803a7a226920711df819d4ef20 100644
--- a/net/sctp/chunk.c
+++ b/net/sctp/chunk.c
@@ -47,7 +47,7 @@ static void sctp_datamsg_init(struct sctp_datamsg *msg)
static struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
{
struct sctp_datamsg *msg;
- msg = kmalloc_obj(struct sctp_datamsg, gfp);
+ msg = kmalloc_obj(*msg, gfp);
if (msg) {
sctp_datamsg_init(msg);
SCTP_DBG_OBJCNT_INC(datamsg);
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index 27c26e12f95d82fc56f8ea4e0dd70f4218f8119a..6c9d32e9fcd8ff8f39d1a2bddc3ee8cb466d5ca8 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -1550,14 +1550,14 @@ static __init int sctp_init(void)
order = min(order, max_entry_order);
/* Allocate and initialize the endpoint hash table. */
- sctp_ep_hashsize = 64;
sctp_ep_hashtable =
- kmalloc_objs(struct sctp_hashbucket, 64);
+ kmalloc_objs(*sctp_ep_hashtable, SCTP_EP_INITIAL_HASHSIZE);
if (!sctp_ep_hashtable) {
pr_err("Failed endpoint_hash alloc\n");
status = -ENOMEM;
goto err_ehash_alloc;
}
+ sctp_ep_hashsize = SCTP_EP_INITIAL_HASHSIZE;
for (i = 0; i < sctp_ep_hashsize; i++) {
rwlock_init(&sctp_ep_hashtable[i].lock);
INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
diff --git a/net/sctp/stream.c b/net/sctp/stream.c
index 30037d0ca160ec63ba1154ef6b142e589e2a2e84..19f10e4e099df650c4162b82c7a1670ce93cb930 100644
--- a/net/sctp/stream.c
+++ b/net/sctp/stream.c
@@ -326,7 +326,7 @@ int sctp_send_reset_streams(struct sctp_association *asoc,
goto out;
}
- nstr_list = kzalloc_objs(__be16, str_nums);
+ nstr_list = kzalloc_objs(*nstr_list, str_nums);
if (!nstr_list) {
retval = -ENOMEM;
goto out;
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net-next] sctp: improve memory allocation patterns
2026-12-01 5:39 [PATCH net-next] sctp: improve memory allocation patterns Jamie Bainbridge
@ 2026-12-01 21:27 ` Xin Long
2026-12-04 5:00 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Xin Long @ 2026-12-01 21:27 UTC (permalink / raw)
To: Jamie Bainbridge
Cc: Marcelo Ricardo Leitner, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
Kees Cook, Gustavo A. R. Silva, linux-sctp, netdev,
linux-hardening
On Tue, Dec 1, 2026 at 12:42 AM Jamie Bainbridge
<jamie.bainbridge@gmail.com> wrote:
>
> Much of SCTP was written before helpers such as kmalloc_flex() for
> safely allocating structs with Flexible Array Members.
>
> There are also some instances of allocating an object using the object's
> literal type as the size, rather than dereferencing the object type.
>
> Use modern alloc wrappers and dereference types where appropriate.
>
> Annotate sctp_auth_bytes.data[] with __counted_by() to enable checking.
>
> Use a macro instead of magic number in endpoint hashtable creation.
> Only set the global hashsize if allocating the hashtable succeeded.
>
> Tested under KASAN with tests confirmed by gcov to exercise this code.
>
> Signed-off-by: Jamie Bainbridge <jamie.bainbridge@gmail.com>
Acked-by: Xin Long <lucien.xin@gmail.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net-next] sctp: improve memory allocation patterns
2026-12-01 5:39 [PATCH net-next] sctp: improve memory allocation patterns Jamie Bainbridge
2026-12-01 21:27 ` Xin Long
@ 2026-12-04 5:00 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-12-04 5:00 UTC (permalink / raw)
To: Jamie Bainbridge
Cc: marcelo.leitner, lucien.xin, davem, edumazet, kuba, pabeni,
willemb, horms, kees, gustavoars, linux-sctp, netdev,
linux-hardening
Hello:
This patch was applied to netdev/net-next.git (main)
by Willem de Bruijn <willemb@google.com>:
On Tue, 1 Dec 2026 16:39:37 +1100 you wrote:
> Much of SCTP was written before helpers such as kmalloc_flex() for
> safely allocating structs with Flexible Array Members.
>
> There are also some instances of allocating an object using the object's
> literal type as the size, rather than dereferencing the object type.
>
> Use modern alloc wrappers and dereference types where appropriate.
>
> [...]
Here is the summary with links:
- [net-next] sctp: improve memory allocation patterns
https://git.kernel.org/netdev/net-next/c/2bd980c0dac3
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 5:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 5:39 [PATCH net-next] sctp: improve memory allocation patterns Jamie Bainbridge
2026-12-01 21:27 ` Xin Long
2026-12-04 5:00 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox