The vulnerability CVE-2023-4806 is a use-after-free in glibc's getaddrinfo function. The provided commit 973fe93a5675c42798b2161c6f29c01b0e243994 directly patches sysdeps/posix/getaddrinfo.c. The commit message explicitly states 'getaddrinfo: Fix use after free in getcanonname (CVE-2023-4806)'.
The analysis of the patch reveals that the core issue lies in how getaddrinfo handles the h_name field from struct hostent returned by NSS modules, particularly when a temporary buffer (tmpbuf) backing this h_name is reallocated or freed.
getaddrinfo is the main, user-facing function. The complex logic involving multiple calls to NSS modules (e.g., gethostbyname2_r, then getcanonname_r) and buffer management happens within its implementation. The vulnerability occurs because getaddrinfo's internal state management allowed a pointer to h_name (stored in res->at->name before the fix) to become dangling if tmpbuf was freed, and this dangling pointer was subsequently used.
- The internal static function
getcanonname was identified as the point where the stale pointer (previously at->name) was passed to the NSS _getcanonname_r hook. The patch changes getcanonname to use a safely copied version of h_name (res->h_name).
Other modified functions like convert_hostent_to_gaih_addrtuple and get_nss_addresses are part of the fix mechanism (e.g., copying h_name, passing the copied version) but getaddrinfo is the encompassing vulnerable function, and getcanonname is the internal function that directly used the stale pointer before passing it to the NSS hook. The new test files (nss_test_gai_hv2_canonname.c, tst-nss-gai-hv2-canonname.c) are for reproducing the bug and are not part of the vulnerable glibc code itself.