ce426f
commit ea7d8b95e2fcb81f68b04ed7787a3dbda023991a
ce426f
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
ce426f
Date:   Thu Mar 27 19:48:15 2014 +0530
ce426f
ce426f
    Avoid overlapping addresses to stpcpy calls in nscd (BZ #16760)
ce426f
    
ce426f
    Calls to stpcpy from nscd netgroups code will have overlapping source
ce426f
    and destination when all three values in the returned triplet are
ce426f
    non-NULL and in the expected (host,user,domain) order.  This is seen
ce426f
    in valgrind as:
ce426f
    
ce426f
    ==3181== Source and destination overlap in stpcpy(0x19973b48, 0x19973b48)
ce426f
    ==3181==    at 0x4C2F30A: stpcpy (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
ce426f
    ==3181==    by 0x12567A: addgetnetgrentX (string3.h:111)
ce426f
    ==3181==    by 0x12722D: addgetnetgrent (netgroupcache.c:665)
ce426f
    ==3181==    by 0x11114C: nscd_run_worker (connections.c:1338)
ce426f
    ==3181==    by 0x4E3C102: start_thread (pthread_create.c:309)
ce426f
    ==3181==    by 0x59B81AC: clone (clone.S:111)
ce426f
    ==3181==
ce426f
    
ce426f
    Fix this by using memmove instead of stpcpy.
ce426f
ce426f
diff --git glibc-2.17-c758a686/nscd/netgroupcache.c glibc-2.17-c758a686/nscd/netgroupcache.c
ce426f
index 5d15aa4..820d823 100644
ce426f
--- glibc-2.17-c758a686/nscd/netgroupcache.c
ce426f
+++ glibc-2.17-c758a686/nscd/netgroupcache.c
ce426f
@@ -216,6 +216,10 @@ addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
ce426f
 			    const char *nuser = data.val.triple.user;
ce426f
 			    const char *ndomain = data.val.triple.domain;
ce426f
 
ce426f
+			    size_t hostlen = strlen (nhost ?: "") + 1;
ce426f
+			    size_t userlen = strlen (nuser ?: "") + 1;
ce426f
+			    size_t domainlen = strlen (ndomain ?: "") + 1;
ce426f
+
ce426f
 			    if (nhost == NULL || nuser == NULL || ndomain == NULL
ce426f
 				|| nhost > nuser || nuser > ndomain)
ce426f
 			      {
ce426f
@@ -233,9 +237,6 @@ addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
ce426f
 				     : last + strlen (last) + 1 - buffer);
ce426f
 
ce426f
 				/* We have to make temporary copies.  */
ce426f
-				size_t hostlen = strlen (nhost ?: "") + 1;
ce426f
-				size_t userlen = strlen (nuser ?: "") + 1;
ce426f
-				size_t domainlen = strlen (ndomain ?: "") + 1;
ce426f
 				size_t needed = hostlen + userlen + domainlen;
ce426f
 
ce426f
 				if (buflen - req->key_len - bufused < needed)
ce426f
@@ -269,9 +270,12 @@ addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
ce426f
 			      }
ce426f
 
ce426f
 			    char *wp = buffer + buffilled;
ce426f
-			    wp = stpcpy (wp, nhost) + 1;
ce426f
-			    wp = stpcpy (wp, nuser) + 1;
ce426f
-			    wp = stpcpy (wp, ndomain) + 1;
ce426f
+			    wp = memmove (wp, nhost ?: "", hostlen);
ce426f
+			    wp += hostlen;
ce426f
+			    wp = memmove (wp, nuser ?: "", userlen);
ce426f
+			    wp += userlen;
ce426f
+			    wp = memmove (wp, ndomain ?: "", domainlen);
ce426f
+			    wp += domainlen;
ce426f
 			    buffilled = wp - buffer;
ce426f
 			    ++nentries;
ce426f
 			  }