Netdev List
 help / color / mirror / Atom feed
* less size_t please (was Re: [PATCH net] xfrm: fix integer overflow in xfrm_replay_state_esn_len())
@ 2025-01-30 13:44 Alexey Dobriyan
  2025-01-30 16:15 ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Alexey Dobriyan @ 2025-01-30 13:44 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, netdev, linux-kernel,
	kernel-janitors

> -static inline unsigned int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
> +static inline size_t xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
>  {
> -	return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
> +	return size_add(sizeof(*replay_esn), size_mul(replay_esn->bmp_len, sizeof(__u32)));

Please don't do this.

You can (and should!) make calculations and check for overflow at the
same time. It's very efficient.

> 1) Use size_add() and size_mul().  This change is necessary for 32bit systems.

This bloats code on 32-bit.

	int len;
	if (__builtin_mul_overflow(replay_esn->bmp_len, 4, &len)) {
		return true;
	}
	if (__builtin_add_overflow(len, sizeof(*replay_esn), &len)) {
		return true;
	}
	*plen = len;
	return false;

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-03-07 13:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-30 13:44 less size_t please (was Re: [PATCH net] xfrm: fix integer overflow in xfrm_replay_state_esn_len()) Alexey Dobriyan
2025-01-30 16:15 ` Dan Carpenter
2025-02-06 17:06   ` Alexey Dobriyan
2025-02-07  7:46     ` Dan Carpenter
2025-03-07 13:43     ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox