This is an archive of the discontinued LLVM Phabricator instance.

[sanitizer] Allow getsockname with NULL addrlen
ClosedPublic

Authored by tamird on Nov 2 2021, 2:13 PM.

Details

Summary

This is already permitted in getpeername, and returns EFAULT on Linux
(does not crash the program).

Fixes https://github.com/google/sanitizers/issues/1451.

Diff Detail

Event Timeline

tamird requested review of this revision.Nov 2 2021, 2:13 PM
tamird created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptNov 2 2021, 2:13 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
2718–2722

it was a pointer to the int int addr_sz;

3234

can you please add some test, e.g. clone compiler-rt/test/sanitizer_common/TestCases/Linux/recv_msg_trunc.cc and remove unnecessary stuff
in test/sanitizer_common we usually only check that intercepted call does not crash and behaves reasonably.

tamird updated this revision to Diff 384269.Nov 2 2021, 4:17 PM
tamird marked an inline comment as done.

Add test

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
2718–2722

I don't follow. Are you asking for a change to be made here?

vitalybuka accepted this revision.Nov 2 2021, 4:40 PM

Thanks

This revision is now accepted and ready to land.Nov 2 2021, 4:40 PM
vitalybuka added inline comments.Nov 2 2021, 4:42 PM
compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
2718–2722

yes
because is addrlen is "int *"
I assume getsockname will fail for negative number anyway

tamird updated this revision to Diff 384439.Nov 3 2021, 7:57 AM

Fix getsockname prototype

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
2718–2722

The function prototype here is wrong; addrlen is actually socklen_t which is a typedef for unsigned. Fixed.

tamird updated this revision to Diff 384442.Nov 3 2021, 8:01 AM

Stricter test

tamird added a comment.Nov 3 2021, 8:28 AM

Could someone land this for me? I don't have commit access.

This revision was automatically updated to reflect the committed changes.

Crashes on Android https://lab.llvm.org/buildbot/#/builders/77/builds/11119/steps/16/logs/stdio

FAIL: SanitizerCommon-asan-i386-Android :: Linux/get_sock_peer_name.cpp (807 of 1325)
******************** TEST 'SanitizerCommon-asan-i386-Android :: Linux/get_sock_peer_name.cpp' FAILED ********************
Script:
--
: 'RUN: at line 4';     /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/sanitizer_common/android_commands/android_compile.py  /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm_build64/bin/clang  --driver-mode=g++ -gline-tables-only -fsanitize=address  --target=i686-linux-android24 --sysroot=/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot --gcc-toolchain=/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/android_ndk/toolchains/llvm/prebuilt/linux-x86_64  -B/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/android_ndk/toolchains/llvm/prebuilt/linux-x86_64 -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -stdlib=libc++ -fuse-ld=lld  -ldl /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/sanitizer_common/TestCases/Linux/get_sock_peer_name.cpp -o /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/compiler_rt_build_android_i686/test/sanitizer_common/asan-i386-Android/Linux/Output/get_sock_peer_name.cpp.tmp &&  /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/compiler_rt_build_android_i686/test/sanitizer_common/asan-i386-Android/Linux/Output/get_sock_peer_name.cpp.tmp 2>&1
--
Exit Code: 134
Command Output (stdout):
--
/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/sanitizer_common/TestCases/Linux/get_sock_peer_name.cpp:24: int main(): assertion "connect(fd, reinterpret_cast<const sockaddr *>(&sin), sizeof(sin)) == 0" failed
Aborted 
--
Command Output (stderr):
--
+ : 'RUN: at line 4'
+ /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/sanitizer_common/android_commands/android_compile.py /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm_build64/bin/clang --driver-mode=g++ -gline-tables-only -fsanitize=address --target=i686-linux-android24 --sysroot=/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot --gcc-toolchain=/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/android_ndk/toolchains/llvm/prebuilt/linux-x86_64 -B/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/android_ndk/toolchains/llvm/prebuilt/linux-x86_64 -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -stdlib=libc++ -fuse-ld=lld -ldl /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/llvm-project/compiler-rt/test/sanitizer_common/TestCases/Linux/get_sock_peer_name.cpp -o /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/compiler_rt_build_android_i686/test/sanitizer_common/asan-i386-Android/Linux/Output/get_sock_peer_name.cpp.tmp
+ /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/compiler_rt_build_android_i686/test/sanitizer_common/asan-i386-Android/Linux/Output/get_sock_peer_name.cpp.tmp
/var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/compiler_rt_build_android_i686/test/sanitizer_common/asan-i386-Android/Linux/Output/get_sock_peer_name.cpp.script: line 1: 14248 Aborted                 (core dumped) /var/lib/buildbot/sanitizer-buildbot6/sanitizer-x86_64-linux-android/build/compiler_rt_build_android_i686/test/sanitizer_common/asan-i386-Android/Linux/Output/get_sock_peer_name.cpp.tmp 2>&1
--
********************

Thanks for the fix!