Skip to content

Commit 7f3c090

Browse files
fennerfxlb
authored andcommitted
OSPF6: Fix an undefined behavior
Handle ls_length shorter than sizeof(lsa_hdr) in the same way as OSPF. Use a u_int32 to hold a loop variable initialized with GET_BE_U_4. print-ospf6.c:815:46: runtime error: signed integer overflow: -2147483648 - 1 cannot be represented in type 'int' SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior print-ospf6.c:817:46 [Part of the PR #1012]
1 parent de0389e commit 7f3c090

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

print-ospf6.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,23 @@ static int
388388
ospf6_print_lshdr(netdissect_options *ndo,
389389
const struct lsa6_hdr *lshp, const u_char *dataend)
390390
{
391+
u_int ls_length;
392+
391393
if ((const u_char *)(lshp + 1) > dataend)
392394
goto trunc;
393395

396+
ls_length = GET_BE_U_2(lshp->ls_length);
397+
if (ls_length < sizeof(struct lsa_hdr)) {
398+
ND_PRINT("\n\t Bogus length %u < header (%zu)", ls_length,
399+
sizeof(struct lsa_hdr));
400+
goto trunc;
401+
}
402+
394403
ND_PRINT("\n\t Advertising Router %s, seq 0x%08x, age %us, length %zu",
395404
GET_IPADDR_STRING(lshp->ls_router),
396405
GET_BE_U_4(lshp->ls_seq),
397406
GET_BE_U_2(lshp->ls_age),
398-
GET_BE_U_2(lshp->ls_length)-sizeof(struct lsa6_hdr));
407+
ls_length-sizeof(struct lsa6_hdr));
399408

400409
ospf6_print_ls_type(ndo, GET_BE_U_2(lshp->ls_type),
401410
&lshp->ls_stateid);
@@ -734,7 +743,7 @@ ospf6_decode_v3(netdissect_options *ndo,
734743
const struct lsr6 *lsrp;
735744
const struct lsa6_hdr *lshp;
736745
const struct lsa6 *lsap;
737-
int i;
746+
uint32_t i;
738747

739748
switch (GET_U_1(op->ospf6_type)) {
740749

tests/TESTLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,3 +922,4 @@ pb_nhrp_1 pb_nhrp_1.pcap pb_nhrp_1.out -v
922922
ip-snmp-leftshift-unsigned ip-snmp-leftshift-unsigned.pcap ip-snmp-leftshift-unsigned.out
923923
ip6-snmp-oid-unsigned ip6-snmp-oid-unsigned.pcap ip6-snmp-oid-unsigned.out
924924
lwres-pointer-arithmetic-ub lwres-pointer-arithmetic-ub.pcap lwres-pointer-arithmetic-ub.out
925+
ospf-signed-integer-ubsan ospf-signed-integer-ubsan.pcap ospf-signed-integer-ubsan.out -vv

tests/ospf-signed-integer-ubsan.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 15:39:26.444985 IP6 (class 0xe0, hlim 1, next-header AH (51) payload length: 532) fe80::1 > fe80::2: AH(length=4(24-bytes),spi=0x00000100,seq=0x1e,icv=0x0a6ab0b271917e05f7a01c58): OSPFv3, LS-Update, length 508
2+
Router-ID 1.1.1.108, Area 11.234.210.1, Instance 1
3+
Bogus length 0 < header (20) [|ospf3]

tests/ospf-signed-integer-ubsan.pcap

724 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)