]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/: Fortify source of strtcpy(), stpecpy(), and zustr2stp()
authorAlejandro Colomar <alx@kernel.org>
Fri, 24 Nov 2023 22:56:17 +0000 (23:56 +0100)
committerSerge Hallyn <serge@hallyn.com>
Mon, 4 Dec 2023 04:24:29 +0000 (22:24 -0600)
By writing the terminating null byte via stpcpy(3), we take advantage of
_FORTIFY_SOURCE for the last byte, which was unprotected before this
commit.

Reported-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/string/stpecpy.h
lib/string/strtcpy.h
lib/string/zustr2stp.h

index c4a60680f3d40cdd48a9e1ce87becb443031d27a..640d19c2e35f8e5799aeea9a0a59ce8c6a23426b 100644 (file)
@@ -66,7 +66,6 @@ inline char *
 stpecpy(char *dst, char *end, const char *restrict src)
 {
        bool    trunc;
-       char    *p;
        size_t  dsize, dlen, slen;
 
        if (dst == end)
@@ -79,10 +78,7 @@ stpecpy(char *dst, char *end, const char *restrict src)
        trunc = (slen == dsize);
        dlen = slen - trunc;
 
-       p = mempcpy(dst, src, dlen);
-       *p = '\0';
-
-       return p + trunc;
+       return stpcpy(mempcpy(dst, src, dlen), "") + trunc;
 }
 
 
index 25255493a75e3146191b7efb2151c7865dbc0e50..4e6534476bb1bdd9571ba8bfb49fc677304ab576 100644 (file)
@@ -55,7 +55,6 @@ inline ssize_t
 strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
 {
        bool    trunc;
-       char    *p;
        size_t  dlen, slen;
 
        if (dsize == 0)
@@ -65,8 +64,7 @@ strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
        trunc = (slen == dsize);
        dlen = slen - trunc;
 
-       p = mempcpy(dst, src, dlen);
-       *p = '\0';
+       stpcpy(mempcpy(dst, src, dlen), "");
 
        return trunc ? -1 : slen;
 }
index 436f47bcbbbe05b8e302cbbdb2533d0b1e29f9b2..5ed424950638fd1df134a886baa1b6645eab58fc 100644 (file)
@@ -72,12 +72,7 @@ inline char *zustr2stp(char *restrict dst, const char *restrict src, size_t sz);
 inline char *
 zustr2stp(char *restrict dst, const char *restrict src, size_t sz)
 {
-       char  *p;
-
-       p = mempcpy(dst, src, strnlen(src, sz));
-       *p = '\0';
-
-       return p;
+       return stpcpy(mempcpy(dst, src, strnlen(src, sz)), "");
 }