Skip to contentSkip to conversation

nfp: bpf: prevent integer overflow in nfp_bpf_event_output()

PATCH Applied Dan Carpenter 3 participants 7 messages Jan 13, 2025 – Jan 15, 2025

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfp: bpf: prevent integer overflow in nfp_bpf_event_output()

The "sizeof(struct cmsg_bpf_event) + pkt_size + data_size" math could potentially have an integer wrapping bug on 32bit systems. Check for this and return an error.

Fixes 9816dd35ecec ("nfp: bpf: perf event output helpers support")
---
 drivers/net/ethernet/netronome/nfp/bpf/offload.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/netronome/nfp/bpf/offload.c +2 -1
index 9d97cd281f18..c03558adda91 100644
@@ -458,7 +458,8 @@ int nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,
 	map_id_full = be64_to_cpu(cbe->map_ptr);
 	map_id = map_id_full;
 
-	if (len < sizeof(struct cmsg_bpf_event) + pkt_size + data_size)
+	if (size_add(pkt_size, data_size) > INT_MAX ||
+	    len < sizeof(struct cmsg_bpf_event) + pkt_size + data_size)
 		return -EINVAL;
 	if (cbe->hdr.ver != NFP_CCM_ABI_VERSION)
 		return -EINVAL;
Signature · 1 line
2.45.2
#1 lore raw

From: Dan Carpenter <[email protected]>
Date: Mon, 13 Jan 2025 09:18:39 +0300

Quoted 2 lines

The "sizeof(struct cmsg_bpf_event) + pkt_size + data_size" math could potentially have an integer wrapping bug on 32bit systems. Check for

Not in practice I suppose? Do we need to fix "never" bugs?

Quoted 4 lines

this and return an error.

Fixes 9816dd35ecec ("nfp: bpf: perf event output helpers support")

Thanks,
Olek

#2 lore raw

[ I tried to send this email yesterday but apparently gmail blocked
it for security reasons? So weird. - dan ]

Quoted 8 lines · Alexander Lobakin

From: Dan Carpenter <[email protected]>
Date: Mon, 13 Jan 2025 09:18:39 +0300

Quoted 2 lines

The "sizeof(struct cmsg_bpf_event) + pkt_size + data_size" math could potentially have an integer wrapping bug on 32bit systems. Check for

Not in practice I suppose? Do we need to fix "never" bugs?

No, this is from static analysis. We don't need to fix never bugs.

This is called from nfp_bpf_ctrl_msg_rx() and nfp_bpf_ctrl_msg_rx_raw() and I assumed that since pkt_size and data_size come from skb->data on the rx path then they couldn't be trusted.

Where is the bounds checking?

regards,
dan carpenter

#3 lore raw

From: Dan Carpenter <[email protected]>
Date: Tue, 14 Jan 2025 13:45:04 +0300

Quoted 10 lines

[ I tried to send this email yesterday but apparently gmail blocked
it for security reasons? So weird. - dan ]

Quoted 6 lines · Alexander Lobakin

From: Dan Carpenter <[email protected]>
Date: Mon, 13 Jan 2025 09:18:39 +0300

Quoted 2 lines

The "sizeof(struct cmsg_bpf_event) + pkt_size + data_size" math could potentially have an integer wrapping bug on 32bit systems. Check for

Quoted 8 lines
Quoted 2 lines

Not in practice I suppose? Do we need to fix "never" bugs?

No, this is from static analysis. We don't need to fix never bugs.

This is called from nfp_bpf_ctrl_msg_rx() and nfp_bpf_ctrl_msg_rx_raw() and I assumed that since pkt_size and data_size come from skb->data on the rx path then they couldn't be trusted.

skbs are always valid and skb->len could never cross INT_MAX to provoke an overflow.

Quoted 5 lines

Where is the bounds checking?

regards,
dan carpenter

Thanks,
Olek

#4 lore raw
Quoted 16 lines · Alexander Lobakin

From: Dan Carpenter <[email protected]>
Date: Tue, 14 Jan 2025 13:45:04 +0300

Quoted 13 lines

[ I tried to send this email yesterday but apparently gmail blocked
it for security reasons? So weird. - dan ]

Quoted 8 lines · Alexander Lobakin

From: Dan Carpenter <[email protected]>
Date: Mon, 13 Jan 2025 09:18:39 +0300

Quoted 2 lines

The "sizeof(struct cmsg_bpf_event) + pkt_size + data_size" math could potentially have an integer wrapping bug on 32bit systems. Check for

Not in practice I suppose? Do we need to fix "never" bugs?

Quoted 9 lines
Quoted 5 lines

No, this is from static analysis. We don't need to fix never bugs.

This is called from nfp_bpf_ctrl_msg_rx() and nfp_bpf_ctrl_msg_rx_raw() and I assumed that since pkt_size and data_size come from skb->data on the rx path then they couldn't be trusted.

skbs are always valid and skb->len could never cross INT_MAX to provoke an overflow.

True but unrelated. I think you are looking at the wrong code...

drivers/net/ethernet/netronome/nfp/bpf/offload.c
445 int nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,

                                                                      ^^^^

This code comes from the network so it cannot be trusted.

446 unsigned int len)
447 {

   448          struct cmsg_bpf_event *cbe = (void *)data;
                                       ^^^^^^^^^^^^^^^^^^^

It is cast to a struct here.

   449          struct nfp_bpf_neutral_map *record;
   450          u32 pkt_size, data_size, map_id;
   451          u64 map_id_full;

452
453 if (len < sizeof(struct cmsg_bpf_event))
454 return -EINVAL;
455
456 pkt_size = be32_to_cpu(cbe->pkt_size);
457 data_size = be32_to_cpu(cbe->data_size);

pkt_size and data_size are u32 values which are controlled from over the network.

458 map_id_full = be64_to_cpu(cbe->map_ptr);
459 map_id = map_id_full;
460
461 if (len < sizeof(struct cmsg_bpf_event) + pkt_size + data_size)

                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

On a 32bit system, then this math can overflow. The pkt_size and data_size are too high and we should return -EINVAL but this check doesn't work because of integer wrapping.

462 return -EINVAL;
63 if (cbe->hdr.ver != NFP_CCM_ABI_VERSION)
464 return -EINVAL;
465
466 rcu_read_lock();
467 record = rhashtable_lookup(&bpf->maps_neutral, &map_id,
468 nfp_bpf_maps_neutral_params);
469 if (!record || map_id_full > U32_MAX) {
470 rcu_read_unlock();
471 cmsg_warn(bpf, "perf event: map id %lld (0x%llx) not recognized, dropping event\n",
472 map_id_full, map_id_full);
473 return -EINVAL;
474 }
475
476 bpf_event_output(record->ptr, be32_to_cpu(cbe->cpu_id),
477 &cbe->data[round_up(pkt_size, 4)], data_size,

                                            ^^^^^^^^^^^^^^^^^^^^^

Here we are way out of bounds with regards to the cbe->data[] array.

regards,
dan carpenter

   478                           cbe->data, pkt_size, nfp_bpf_perf_event_copy);
   479          rcu_read_unlock();
   480  
   481          return 0;
   482  }
Reviewed-by
#5 lore raw

From: Dan Carpenter <[email protected]>
Date: Tue, 14 Jan 2025 21:43:26 +0300

Quoted 79 lines · Alexander Lobakin
Quoted 16 lines

From: Dan Carpenter <[email protected]>
Date: Tue, 14 Jan 2025 13:45:04 +0300

Quoted 13 lines

[ I tried to send this email yesterday but apparently gmail blocked
it for security reasons? So weird. - dan ]

Quoted 8 lines · Alexander Lobakin

From: Dan Carpenter <[email protected]>
Date: Mon, 13 Jan 2025 09:18:39 +0300

Quoted 2 lines

The "sizeof(struct cmsg_bpf_event) + pkt_size + data_size" math could potentially have an integer wrapping bug on 32bit systems. Check for

Not in practice I suppose? Do we need to fix "never" bugs?

Quoted 9 lines
Quoted 5 lines

No, this is from static analysis. We don't need to fix never bugs.

This is called from nfp_bpf_ctrl_msg_rx() and nfp_bpf_ctrl_msg_rx_raw() and I assumed that since pkt_size and data_size come from skb->data on the rx path then they couldn't be trusted.

skbs are always valid and skb->len could never cross INT_MAX to provoke an overflow.

True but unrelated. I think you are looking at the wrong code...

drivers/net/ethernet/netronome/nfp/bpf/offload.c
445 int nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,

                                                                      ^^^^

This code comes from the network so it cannot be trusted.

   446                           unsigned int len)
   447  {
   448          struct cmsg_bpf_event *cbe = (void *)data;
                                       ^^^^^^^^^^^^^^^^^^^

It is cast to a struct here.

   449          struct nfp_bpf_neutral_map *record;
   450          u32 pkt_size, data_size, map_id;
   451          u64 map_id_full;
   452  
   453          if (len < sizeof(struct cmsg_bpf_event))
   454                  return -EINVAL;
   455  
   456          pkt_size = be32_to_cpu(cbe->pkt_size);
   457          data_size = be32_to_cpu(cbe->data_size);

pkt_size and data_size are u32 values which are controlled from over the network.

   458          map_id_full = be64_to_cpu(cbe->map_ptr);
   459          map_id = map_id_full;
   460  
   461          if (len < sizeof(struct cmsg_bpf_event) + pkt_size + data_size)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

On a 32bit system, then this math can overflow. The pkt_size and data_size are too high and we should return -EINVAL but this check doesn't work because of integer wrapping.

   462                  return -EINVAL;
   63          if (cbe->hdr.ver != NFP_CCM_ABI_VERSION)
   464                  return -EINVAL;
   465  
   466          rcu_read_lock();
   467          record = rhashtable_lookup(&bpf->maps_neutral, &map_id,
   468                                     nfp_bpf_maps_neutral_params);
   469          if (!record || map_id_full > U32_MAX) {
   470                  rcu_read_unlock();
   471                  cmsg_warn(bpf, "perf event: map id %lld (0x%llx) not recognized, dropping event\n",
   472                            map_id_full, map_id_full);
   473                  return -EINVAL;
   474          }
   475  
   476          bpf_event_output(record->ptr, be32_to_cpu(cbe->cpu_id),
   477                           &cbe->data[round_up(pkt_size, 4)], data_size,
                                            ^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line

Here we are way out of bounds with regards to the cbe->data[] array.

Aaah okay. Thanks for the detailed explanation! My note was "generic", not regarding precisely this case.

I think a couple checks here wouldn't hurt, not only for an overflow, but functional ones whether we have sane values. It's an ask more for the Netronome guys tho.

Regarding your particular patch, I think it's now pretty clear for me:

Quoted 3 lines

regards,
dan carpenter

Thanks,
Olek

#6 lore raw
In reply to Dan Carpenter
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Mon, 13 Jan 2025 09:18:39 +0300 you wrote:

The "sizeof(struct cmsg_bpf_event) + pkt_size + data_size" math could potentially have an integer wrapping bug on 32bit systems. Check for this and return an error.

Fixes 9816dd35ecec ("nfp: bpf: perf event output helpers support")

[...]

Here is the summary with links:
- [net] nfp: bpf: prevent integer overflow in nfp_bpf_event_output()

    https://git.kernel.org/netdev/net/c/16ebb6f5b629

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html