useful_functions: Fix for buffer overflow in parse_ip6_mask()
Phil Sutter
reviewerPATCH 1/1 useful_functions: Fix for buffer overflow in parse_ip6_mask()
Quoted 6 lines · Ilia
I have found several global buffer overflows in
useful_functions.cThey both occur in the function
parse_ip6_mask()and are caused by unconditionally writing to p[16].
Quoted 6 lines
The first overflow occurs when bits is equal to 128,
which causes p[bits / 8] =0xff<< (8 - (bits & 7)); to write at p[16].The second overflow occurs when bits is equal to 8,
which causesmemset(p + (bits / 8) + 1, 0, (128 - bits) / 8); to write 15 bytes starting at p + 2, which leads to the same issue.
This patch contains a simplified version of his fix:
- The original code attempted to div_round_up() for the second memset(), to zero the fully empty mask bytes. It used the term '(bits / 8) + 1' which misbehaves if bits is equal to 8.
- Addressing p at offset 'bits / 8' is illegal if bits has the legal max value of 128. Also, zeroing this byte is needed only if bits is not divisible by 8.
--- useful_functions.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
useful_functions.c +3 -2
@@ -364,8 +364,9 @@ static struct in6_addr *parse_ip6_mask(char *mask)if (bits !=0 ) {char *p = (char *)&maskaddr;memset (p,0xff , bits /8 ); -memset (p + (bits /8 ) +1 ,0 , (128 - bits) /8 ); - p[bits /8 ] =0xff << (8 - (bits &7 )); +memset (p + (bits +7 ) /8 ,0 , (128 - bits) /8 ); +if (bits &7 ) + p[bits /8 ] =0xff << (8 - (bits &7 ));return &maskaddr; }
Signature · 1 line
2.51.0
Florian Westphal
maintainerQuoted 9 lines · Phil Sutter
This patch contains a simplified version of his fix:
- The original code attempted to
div_round_up()for the secondmemset(), to zero the fully empty mask bytes. It used the term '(bits / 8) + 1' which misbehaves if bits is equal to 8.- Addressing p at offset 'bits / 8' is illegal if bits has the legal max value of 128. Also, zeroing this byte is needed only if bits is not divisible by 8.
Thanks, feel free to push this out.
Phil Sutter
reviewerQuoted 7 lines · Florian Westphal
Quoted 6 lines · Phil Sutter
This patch contains a simplified version of his fix:
- The original code attempted to
div_round_up()for the secondmemset(), to zero the fully empty mask bytes. It used the term '(bits / 8) + 1' which misbehaves if bits is equal to 8.
Quoted 5 lines
Quoted 3 lines
- Addressing p at offset 'bits / 8' is illegal if bits has the legal max value of 128. Also, zeroing this byte is needed only if bits is not divisible by 8.
Thanks, feel free to push this out.
Patch applied.