netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Question] integer overflow in function __qdisc_calculate_pkt_len()
@ 2023-06-02  2:50 luwei (O)
  2023-06-05 23:19 ` Jakub Kicinski
  0 siblings, 1 reply; 5+ messages in thread
From: luwei (O) @ 2023-06-02  2:50 UTC (permalink / raw)
  To: Networking

Hi list,

     I found an integer overflow issue in function 
__qdisc_calculate_pkt_len(), the root cause is overhead and cell_align 
in stab is not checked.

For example, if overhead is set to -2147483559 and cell_align is set to 
-32767 (tc tool limit it to 0 and -1, but other values can be set with 
netlink api),

the integer overflow occurs:

  568 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
  569                                const struct qdisc_size_table *stab)
  570 {
  571         int pkt_len, slot;
  572
  573         pkt_len = skb->len + stab->szopts.overhead; (1)
  574         if (unlikely(!stab->szopts.tsize))
  575                 goto out;
  576
  577         slot = pkt_len + stab->szopts.cell_align;   (2)
  578         if (unlikely(slot < 0))
  579                 slot = 0;

if skb->len is 66, slot will be 66 + (-2147483559) + (-32767) = 
2147451036, and pkt_len will be 2147451040 finally.  I think the value 
of overhead and cell_align

should be limited, but not sure to which values they should be limited, 
can any one give me some suggestions?








-- 
Best Regards,
Lu Wei


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

* Re: [Question] integer overflow in function __qdisc_calculate_pkt_len()
  2023-06-02  2:50 [Question] integer overflow in function __qdisc_calculate_pkt_len() luwei (O)
@ 2023-06-05 23:19 ` Jakub Kicinski
  2023-06-06 12:54   ` luwei (O)
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2023-06-05 23:19 UTC (permalink / raw)
  To: luwei (O); +Cc: Networking

On Fri, 2 Jun 2023 10:50:44 +0800 luwei (O) wrote:
>      I found an integer overflow issue in function 
> __qdisc_calculate_pkt_len(), the root cause is overhead and cell_align 
> in stab is not checked.
> 
> For example, if overhead is set to -2147483559 and cell_align is set to 
> -32767 (tc tool limit it to 0 and -1, but other values can be set with 
> netlink api),
> 
> the integer overflow occurs:
> 
>   568 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
>   569                                const struct qdisc_size_table *stab)
>   570 {
>   571         int pkt_len, slot;
>   572
>   573         pkt_len = skb->len + stab->szopts.overhead; (1)
>   574         if (unlikely(!stab->szopts.tsize))
>   575                 goto out;
>   576
>   577         slot = pkt_len + stab->szopts.cell_align;   (2)
>   578         if (unlikely(slot < 0))
>   579                 slot = 0;
> 
> if skb->len is 66, slot will be 66 + (-2147483559) + (-32767) = 
> 2147451036, and pkt_len will be 2147451040 finally.  I think the value 
> of overhead and cell_align
> 
> should be limited, but not sure to which values they should be limited, 
> can any one give me some suggestions?

on a quick look limiting the cell_align to S16_MIN at the netlink level
(NLA_POLICY_MIN()) seems reasonable, feel free to send a patch.

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

* Re: [Question] integer overflow in function __qdisc_calculate_pkt_len()
  2023-06-05 23:19 ` Jakub Kicinski
@ 2023-06-06 12:54   ` luwei (O)
  2023-06-06 15:49     ` Jakub Kicinski
  0 siblings, 1 reply; 5+ messages in thread
From: luwei (O) @ 2023-06-06 12:54 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: Networking


在 2023/6/6 7:19 AM, Jakub Kicinski 写道:
> On Fri, 2 Jun 2023 10:50:44 +0800 luwei (O) wrote:
>>       I found an integer overflow issue in function
>> __qdisc_calculate_pkt_len(), the root cause is overhead and cell_align
>> in stab is not checked.
>>
>> For example, if overhead is set to -2147483559 and cell_align is set to
>> -32767 (tc tool limit it to 0 and -1, but other values can be set with
>> netlink api),
>>
>> the integer overflow occurs:
>>
>>    568 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
>>    569                                const struct qdisc_size_table *stab)
>>    570 {
>>    571         int pkt_len, slot;
>>    572
>>    573         pkt_len = skb->len + stab->szopts.overhead; (1)
>>    574         if (unlikely(!stab->szopts.tsize))
>>    575                 goto out;
>>    576
>>    577         slot = pkt_len + stab->szopts.cell_align;   (2)
>>    578         if (unlikely(slot < 0))
>>    579                 slot = 0;
>>
>> if skb->len is 66, slot will be 66 + (-2147483559) + (-32767) =
>> 2147451036, and pkt_len will be 2147451040 finally.  I think the value
>> of overhead and cell_align
>>
>> should be limited, but not sure to which values they should be limited,
>> can any one give me some suggestions?
> on a quick look limiting the cell_align to S16_MIN at the netlink level
> (NLA_POLICY_MIN()) seems reasonable, feel free to send a patch.
> .

Thanks for your reply, but do your mean cell_align or overhead? It seems 
limit cell_align to

S16_MIN(-32768) can still cause the overflow:

     66 + (-2147483559) + (-32767) = 2147451036

   skb->len = 66
   stab->szopts.overhead = -2147483559
   stab->szopts.cell_align = -32767

-- 
Best Regards,
Lu Wei


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

* Re: [Question] integer overflow in function __qdisc_calculate_pkt_len()
  2023-06-06 12:54   ` luwei (O)
@ 2023-06-06 15:49     ` Jakub Kicinski
  2023-06-25  2:19       ` luwei (O)
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2023-06-06 15:49 UTC (permalink / raw)
  To: luwei (O); +Cc: Networking

On Tue, 6 Jun 2023 20:54:47 +0800 luwei (O) wrote:
> > on a quick look limiting the cell_align to S16_MIN at the netlink level
> > (NLA_POLICY_MIN()) seems reasonable, feel free to send a patch.
> > .  
> 
> Thanks for your reply, but do your mean cell_align or overhead? It seems 
> limit cell_align to
> 
> S16_MIN(-32768) can still cause the overflow:
> 
>      66 + (-2147483559) + (-32767) = 2147451036
> 
>    skb->len = 66
>    stab->szopts.overhead = -2147483559
>    stab->szopts.cell_align = -32767

Could you explain what the problem caused by the overflow will be?

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

* Re: [Question] integer overflow in function __qdisc_calculate_pkt_len()
  2023-06-06 15:49     ` Jakub Kicinski
@ 2023-06-25  2:19       ` luwei (O)
  0 siblings, 0 replies; 5+ messages in thread
From: luwei (O) @ 2023-06-25  2:19 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: Networking


在 2023/6/6 11:49 PM, Jakub Kicinski 写道:
> On Tue, 6 Jun 2023 20:54:47 +0800 luwei (O) wrote:
>>> on a quick look limiting the cell_align to S16_MIN at the netlink level
>>> (NLA_POLICY_MIN()) seems reasonable, feel free to send a patch.
>>> .
>> Thanks for your reply, but do your mean cell_align or overhead? It seems
>> limit cell_align to
>>
>> S16_MIN(-32768) can still cause the overflow:
>>
>>       66 + (-2147483559) + (-32767) = 2147451036
>>
>>     skb->len = 66
>>     stab->szopts.overhead = -2147483559
>>     stab->szopts.cell_align = -32767
> Could you explain what the problem caused by the overflow will be?

yes, it affects the final result of pkt_len in 
__qdisc_calculate_pkt_len().  In the previous example, the final pkt_len 
will be 2147451040

which is very different from the origin skb->len 66 and it will reduce 
traffic control accuracy heavily.

it is calculated as follows:

void __qdisc_calculate_pkt_len(struct sk_buff *skb,
                                const struct qdisc_size_table *stab)
{
         int pkt_len, slot;

         pkt_len = skb->len + stab->szopts.overhead;     // pkt_len = 66 
+ (-2147483559) = -2147483493
         if (unlikely(!stab->szopts.tsize))
                 goto out;

         slot = pkt_len + stab->szopts.cell_align;            // slot  = 
-2147483493 + (-32767) = 2147451036
         if (unlikely(slot < 0))
                 slot = 0;

         slot >>= stab->szopts.cell_log;          slot  =  2147451036 
 >>  2 =  536862759
         if (likely(slot < stab->szopts.tsize))
                 pkt_len = stab->data[slot];
         else
                 pkt_len = stab->data[stab->szopts.tsize - 1] *
                                 (slot / stab->szopts.tsize) +
                                 stab->data[slot % 
stab->szopts.tsize];    // pkt_len  = 2048 * (536862759 / 512) + 160 = 
2147451040

         pkt_len <<= stab->szopts.size_log;
out:
         if (unlikely(pkt_len < 1))
                 pkt_len = 1;
         qdisc_skb_cb(skb)->pkt_len = pkt_len;
}
EXPORT_SYMBOL(__qdisc_calculate_pkt_len);
> .

-- 
Best Regards,
Lu Wei


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

end of thread, other threads:[~2023-06-25  2:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-02  2:50 [Question] integer overflow in function __qdisc_calculate_pkt_len() luwei (O)
2023-06-05 23:19 ` Jakub Kicinski
2023-06-06 12:54   ` luwei (O)
2023-06-06 15:49     ` Jakub Kicinski
2023-06-25  2:19       ` luwei (O)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).