[libc] inet_addr incorrectly accepts binary strings
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.
@llvm/issue-subscribers-libc
Author: Alexey Samsonov (vonosmas)
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
0bprefix).
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.
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.
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.