Skip to content

Commit a1386af

Browse files
Vadim Fedorenkoaabc
authored andcommitted
tcp options: fix possible shift-out-of-bounds
Calculation of tcp option bit is done before actual check and could lead to shift-out-of-bounds error tracked by UBSAN. Fix it by checking for zero value before the calculation. While here also fix bit calculation because it should be 31-based instead of 32-based. Signed-off-by: Vadim Fedorenko <[email protected]>
1 parent 40fefb2 commit a1386af

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

ipt_NETFLOW.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4821,7 +4821,7 @@ static inline __u32 ip4_options(const u_int8_t *p, const unsigned int optsize)
48214821
*
48224822
* Set proper bit for htonl later. */
48234823
if (ip4_opt_table[op])
4824-
ret |= 1 << (32 - ip4_opt_table[op]);
4824+
ret |= 1 << (31 - ip4_opt_table[op]);
48254825
}
48264826
if (likely(i >= optsize || op == 0))
48274827
break;
@@ -4842,26 +4842,24 @@ static inline __u32 tcp_options(const struct sk_buff *skb, const unsigned int pt
48424842
const unsigned int optsize = th->doff * 4 - sizeof(struct tcphdr);
48434843
__u8 _opt[TCPHDR_MAXSIZE];
48444844
const u_int8_t *p;
4845-
__u32 ret;
4845+
__u32 ret = 0;
48464846
unsigned int i;
48474847

48484848
p = skb_header_pointer(skb, ptr + sizeof(struct tcphdr), optsize, _opt);
48494849
if (unlikely(!p))
4850-
return 0;
4851-
ret = 0;
4850+
return ret;
4851+
48524852
for (i = 0; likely(i < optsize); ) {
48534853
u_int8_t opt = p[i++];
48544854

4855+
if (likely(opt == 0) || unlikely(p[i] < 2))
4856+
break;
48554857
if (likely(opt < 32)) {
48564858
/* IANA doc is messed up, see above. */
4857-
ret |= 1 << (32 - opt);
4859+
ret |= 1 << (31 - opt);
48584860
}
4859-
if (likely(i >= optsize || opt == 0))
4860-
break;
4861-
else if (unlikely(opt == 1))
4861+
if (unlikely(opt == 1))
48624862
continue;
4863-
else if (unlikely(p[i] < 2)) /* "silly options" */
4864-
break;
48654863
else
48664864
i += p[i] - 1;
48674865
}

0 commit comments

Comments
 (0)