llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[libc] inet_addr incorrectly accepts binary strings

Open vonosmas opened this issue 3 months ago • 4 comments

According to POSIX, inet_addr should accept integers in decimal, octal, or hexadecimal format: https://pubs.opengroup.org/onlinepubs/9799919799/

Note that it does not allow the binary format (recently standardized in C23 strings with 0b prefix). However, because the implementation delegates to internal::strtointeger implementation: https://github.com/llvm/llvm-project/blob/ff04512fef692af512a790158d82e34d9b35efd3/libc/src/__support/net/address.cpp#L36-L41

it incorrectly accepts 0B0 as a valid representation of 0.0.0.0 address. Note that since we already validate that the first character must be a digit in our implementation, we might as well strip out the binary format special-case.

vonosmas avatar Aug 27 '26 07:08 vonosmas

@llvm/issue-subscribers-libc

Author: Alexey Samsonov (vonosmas)

According to POSIX, `inet_addr` should accept integers in decimal, octal, or hexadecimal format: https://pubs.opengroup.org/onlinepubs/9799919799/

Note that it does not allow the binary format (recently standardized in C23 strings with 0b prefix). However, because the implementation delegates to internal::strtointeger implementation: https://github.com/llvm/llvm-project/blob/ff04512fef692af512a790158d82e34d9b35efd3/libc/src/__support/net/address.cpp#L36-L41

it incorrectly accepts 0B0 as a valid representation of 0.0.0.0 address. Note that since we already validate that the first character must be a digit in our implementation, we might as well strip out the binary format special-case.

Note that it does not allow the binary format (recently standardized in C23 strings with 0b prefix).

This is possibly just because of POSIX still referring to C17. But I'm curious about whether POSIX is rebased onto C23 in the future, the behavior should change for binary format.

frederick-vs-ja avatar Aug 27 '26 07:08 frederick-vs-ja

Right, to quote the POSIX docs:

All numbers supplied as parts in IPv4 dotted decimal notation may be decimal, octal, or hexadecimal, as specified in the ISO C standard

so I agree that would likely include binary format in the future edition of POSIX... however, this function is already marked as obsolescent, and I wonder if it will be removed entirely soon. I still think we probably need to explicitly disable 0b support, at least for now.

vonosmas avatar Aug 27 '26 17:08 vonosmas

This is an interesting find, and I'm glad I wrote the fuzzer for this. It seems that glibc explicitly chose not to support this the 0b prefix in inet_addr: https://sourceware.org/git/?p=glibc.git;a=commit;h=64924422a99690d147a166b4de3103f3bf3eaf6c. Musl and bionic seem to accept the 0b prefix, though this choice appears to be accidental -- a side-effect of inet_addr being implemented in terms of strtoul. I think it makes sense to follow glibc here.

labath avatar Sep 02 '26 09:09 labath