Skip to content

AddressSanitizer:DEADLYSIGNAL in getaddrinfo() when compiled with clang-15 with "-nodefaultlibs" without "-lresolv" #59007

@mmatrosov

Description

@mmatrosov

The following code

#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

int main() {
  addrinfo hints = {};
  addrinfo* res;
  getaddrinfo("api.binance.com", nullptr, &hints, &res);
  printf("%s\n", inet_ntoa(reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr));
  freeaddrinfo(res);
  return 0;
}

compiled with

clang-15 -fsanitize=address test.cpp -nodefaultlibs -lgcc_eh -lc -lm -lpthread -ldl

produces the following error:

$ ./a.out
AddressSanitizer:DEADLYSIGNAL
=================================================================
==1010333==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000000000 bp 0x7ffd91eac050 sp 0x7ffd91eab808 T0)
==1010333==Hint: pc points to the zero page.
==1010333==The signal is caused by a READ memory access.
==1010333==Hint: address points to the zero page.
    #0 0x0  (<unknown module>)
    #1 0x7f5bab6b6805 in gaih_getanswer_slice /build/glibc-SzIz7B/glibc-2.31/resolv/nss_dns/dns-host.c:1147:8

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (<unknown module>)
==1010333==ABORTING

If -lresolv option is added, it works as expected:

$ clang-15 -fsanitize=address test.cpp -nodefaultlibs -lgcc_eh -lc -lm -lpthread -ldl -lresolv -o b.out && ./b.out
13.32.142.177

It also works as expected if I use clang-14 instead of clang-15.

This is a very practical issue, because it is reproduced when using python's requests module with dynamically loaded sanitizer runtime (necessary when using sanitized python extensions):

$ LD_PRELOAD=$(clang++-15 -print-file-name=libclang_rt.asan-x86_64.so) python -c 'import requests
print(requests.get("https://api.binance.com/"))'
AddressSanitizer:DEADLYSIGNAL
=================================================================
==919440==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000000000 bp 0x7ffe180677c0 sp 0x7ffe18066f78 T0)
==919440==Hint: pc points to the zero page.
==919440==The signal is caused by a READ memory access.
==919440==Hint: address points to the zero page.
    #0 0x0  (<unknown module>)
    #1 0x7f3048604805 in gaih_getanswer_slice /build/glibc-SzIz7B/glibc-2.31/resolv/nss_dns/dns-host.c:1147:8

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (<unknown module>)
==919440==ABORTING

More info:

$ ldd ./a.out # this one crashes
	linux-vdso.so.1 (0x00007ffc6537a000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f77efcbf000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f77efb70000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f77efb4d000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f77efb47000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f77f0930000)

$ ldd ./b.out # this one does not crash
	linux-vdso.so.1 (0x00007ffde1385000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f15e3c18000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f15e3ac9000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f15e3aa6000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f15e3aa0000)
	libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f15e3a84000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f15e488c000)

$ clang-15 --version
Ubuntu clang version 15.0.4-++20221031075612+08bd84e8a635-1~exp1~20221031075700.87
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

$ python --version
Python 3.8.10

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.5 LTS
Release:	20.04
Codename:	focal

I posted the same bug at google/sanitizers#1592, because I am not sure if it is related to sanitizers or clang-15.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions