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

* Re: less size_t please (was Re: [PATCH net] xfrm: fix integer overflow in xfrm_replay_state_esn_len())
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2025-01-30 16:15 UTC (permalink / raw)
  To: Alexey Dobriyan
  Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, netdev, linux-kernel,
	kernel-janitors

On Thu, Jan 30, 2025 at 04:44:42PM +0300, Alexey Dobriyan wrote:
> > -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.
> 

I'm not sure I understand.  On 32-bit systems a size_t and an unsigned
int are the same size.  Did you mean to say 64-bit?

Declaring sizes as u32 leads to integer overflows like this one.  If
you look at integer overflows with security implications there is a
5 to 1 ratio of bugs that only affect 32-bit vs bugs that affect
everything because it's just so much easier to overflow a 32-bit size.

aab98e2dbd64 ("ksmbd: fix integer overflows on 32 bit systems")
16ebb6f5b629 ("nfp: bpf: prevent integer overflow in nfp_bpf_event_output()")
09c4a6101532 ("rtc: tps6594: Fix integer overflow on 32bit systems")
55cf2f4b945f ("binfmt_flat: Fix integer overflow bug on 32 bit systems")
fbbd84af6ba7 ("chelsio/chtls: prevent potential integer overflow on 32bit")
bd96a3935e89 ("rdma/cxgb4: Prevent potential integer overflow on 32bit")
d0257e089d1b ("RDMA/uverbs: Prevent integer overflow issue")
3c63d8946e57 ("svcrdma: Address an integer overflow")
7f33b92e5b18 ("NFSD: Prevent a potential integer overflow")

> 	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;
> 	}

This is so ugly...  :/  I'd prefer to just do open code the check at
that point.

static inline int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
{
	if (replay_esn->bmp_len > (INT_MAX - sizeof(*replay_esn)) / sizeof(__u32))
		return -EINVAL;
	return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
}

regards,
dan carpenter


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

* Re: less size_t please (was Re: [PATCH net] xfrm: fix integer overflow in xfrm_replay_state_esn_len())
  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
  0 siblings, 2 replies; 5+ messages in thread
From: Alexey Dobriyan @ 2025-02-06 17:06 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

On Thu, Jan 30, 2025 at 07:15:15PM +0300, Dan Carpenter wrote:
> On Thu, Jan 30, 2025 at 04:44:42PM +0300, Alexey Dobriyan wrote:
> > > -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.
> > 
> 
> I'm not sure I understand.  On 32-bit systems a size_t and an unsigned
> int are the same size.  Did you mean to say 64-bit?

It looks like yes.

> Declaring sizes as u32 leads to integer overflows like this one.

No, the problem is unchecked C addition and mixing types which confuses
people (in the opposite direction too -- there were fake CVEs because
someone thought "size_t len" in write hooks could be big enough).

The answer is to use single type as much as possible and using checked
additions on-the-go at every binary operator if possible.

Of course one bug could be fixed in multiple ways.

> If you look at integer overflows with security implications there is a
> 5 to 1 ratio of bugs that only affect 32-bit vs bugs that affect
> everything because it's just so much easier to overflow a 32-bit size.
> 
> aab98e2dbd64 ("ksmbd: fix integer overflows on 32 bit systems")
> 16ebb6f5b629 ("nfp: bpf: prevent integer overflow in nfp_bpf_event_output()")
> 09c4a6101532 ("rtc: tps6594: Fix integer overflow on 32bit systems")
> 55cf2f4b945f ("binfmt_flat: Fix integer overflow bug on 32 bit systems")
> fbbd84af6ba7 ("chelsio/chtls: prevent potential integer overflow on 32bit")
> bd96a3935e89 ("rdma/cxgb4: Prevent potential integer overflow on 32bit")
> d0257e089d1b ("RDMA/uverbs: Prevent integer overflow issue")

This one is good demonstration why BAO is better:
https://godbolt.org/z/14ofdfvhc

> 3c63d8946e57 ("svcrdma: Address an integer overflow")
> 7f33b92e5b18 ("NFSD: Prevent a potential integer overflow")
> 
> > 	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;
> > 	}
> 
> This is so ugly...  :/  I'd prefer to just do open code the check at
> that point.
> 
> static inline int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
> {
> 	if (replay_esn->bmp_len > (INT_MAX - sizeof(*replay_esn)) / sizeof(__u32))
> 		return -EINVAL;
> 	return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
> }

You can't open code if you have something like this:

	X = a * b + c;

Second, the code is now effectively duplicated, once in overflow check,
second time in actual calculation.

BAO and BMO may look chatty but they're doing the right thing.

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

* Re: less size_t please (was Re: [PATCH net] xfrm: fix integer overflow in xfrm_replay_state_esn_len())
  2025-02-06 17:06   ` Alexey Dobriyan
@ 2025-02-07  7:46     ` Dan Carpenter
  2025-03-07 13:43     ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2025-02-07  7:46 UTC (permalink / raw)
  To: Alexey Dobriyan
  Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, netdev, linux-kernel,
	kernel-janitors

On Thu, Feb 06, 2025 at 08:06:55PM +0300, Alexey Dobriyan wrote:
> On Thu, Jan 30, 2025 at 07:15:15PM +0300, Dan Carpenter wrote:
> > On Thu, Jan 30, 2025 at 04:44:42PM +0300, Alexey Dobriyan wrote:
> > > > -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.
> > > 
> > 
> > I'm not sure I understand.  On 32-bit systems a size_t and an unsigned
> > int are the same size.  Did you mean to say 64-bit?
> 
> It looks like yes.
> 
> > Declaring sizes as u32 leads to integer overflows like this one.
> 
> No, the problem is unchecked C addition and mixing types which confuses
> people (in the opposite direction too -- there were fake CVEs because
> someone thought "size_t len" in write hooks could be big enough).
> 

What was the CVE number?

regards,
dan carpenter


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

* Re: less size_t please (was Re: [PATCH net] xfrm: fix integer overflow in xfrm_replay_state_esn_len())
  2025-02-06 17:06   ` Alexey Dobriyan
  2025-02-07  7:46     ` Dan Carpenter
@ 2025-03-07 13:43     ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2025-03-07 13:43 UTC (permalink / raw)
  To: Alexey Dobriyan
  Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, netdev, linux-kernel,
	kernel-janitors

On Thu, Feb 06, 2025 at 08:06:55PM +0300, Alexey Dobriyan wrote:
> On Thu, Jan 30, 2025 at 07:15:15PM +0300, Dan Carpenter wrote:
> > On Thu, Jan 30, 2025 at 04:44:42PM +0300, Alexey Dobriyan wrote:
> > > > -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.
> > > 
> > 
> > I'm not sure I understand.  On 32-bit systems a size_t and an unsigned
> > int are the same size.  Did you mean to say 64-bit?
> 
> It looks like yes.
> 
> > Declaring sizes as u32 leads to integer overflows like this one.
> 
> No, the problem is unchecked C addition and mixing types which confuses
> people (in the opposite direction too -- there were fake CVEs because
> someone thought "size_t len" in write hooks could be big enough).
> 
> The answer is to use single type as much as possible and using checked
> additions on-the-go at every binary operator if possible.

In the write_hooks examples, we fixed those by moving to size_t.

64bit types are safer because 2**64 is a superset of 2**32.  Anything
which can overflow 64bits can overflow 32bits.  So obviously 64bits is
safer.

But it's surprising the extent of it.  We avoid using ulong types in
UAPI because it's a headache for 32bit support.  So normally we get
an u32 number_items from the user.  That's 4 billion.  It's a small
number and it's actually pretty hard for it to lead to an integer
overflow on 64bit systems.  The struct_size() function is basically
not needed if you're on 64bit and you declare your length variables as
size_t.

The rest of the kernel has an assumption that sizes are saved in size_t.
The size_add() and struct_size() macros rely on it.  In networking there
are a number of functions like sock_kmalloc() which truncate the size
parameter to int and they just make me itch to look at.

regards,
dan carpenter


^ 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