chiark / gitweb /
nss-myhostname: copy first result to preallocated buffer
authorEelco Dolstra <eelco.dolstra@logicblox.com>
Mon, 4 Feb 2013 14:56:26 +0000 (15:56 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Feb 2013 06:02:33 +0000 (01:02 -0500)
Fixes a segfault in nscd when using nss-myhostname.

Nscd expects that an NSS module's gethostbyname4_r function returns
its first result in the pre-allocated gaih_addrtuple denoted by **pat.
(See nscd/aicache.c in the Glibc sources.)  However, nss-myhostname
doesn't fill in **pat but allocates the first result in ‘buffer’, then
sets *pat.  So nscd crashes (e.g. when running ‘getent ahosts
my-machine’).

Hard to tell if this is a bug in nscd, since there doesn't seem to be
a proper API spec for gethostbyname4_r.  But in any case, this patch
fixes the crash by copying the first result to **pat.

src/nss-myhostname/nss-myhostname.c

index 834a80690fdff0338f87701497889606aec8a0a4..b0fb8323ad0db8d472bde63ea9700f7ad51afe1a 100644 (file)
@@ -176,7 +176,11 @@ enum nss_status _nss_myhostname_gethostbyname4_r(
         /* Verify the size matches */
         assert(idx == ms);
 
         /* Verify the size matches */
         assert(idx == ms);
 
-        *pat = r_tuple_prev;
+        /* Nscd expects us to store the first record in **pat. */
+        if (*pat)
+                **pat = *r_tuple_prev;
+        else
+                *pat = r_tuple_prev;
 
         if (ttlp)
                 *ttlp = 0;
 
         if (ttlp)
                 *ttlp = 0;