]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Bug 476780 - Extend strlcat and strlcpy wrappers to GNU libc
authorPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 12 Nov 2023 17:07:52 +0000 (18:07 +0100)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 12 Nov 2023 17:07:52 +0000 (18:07 +0100)
Will add Linux regtest soon

.gitignore
NEWS
configure.ac
memcheck/tests/freebsd/Makefile.am
memcheck/tests/freebsd/strlcat_strlcpy.c [new file with mode: 0644]
memcheck/tests/freebsd/strlcat_strlcpy.stderr.exp [new file with mode: 0644]
memcheck/tests/freebsd/strlcat_strlcpy.vgtest [new file with mode: 0644]
shared/vg_replace_strmem.c

index 8fc5bfd774e67b9866b0a75e5dadc47f29add615..b48622c5b919e98ee2754b779e3aeb26dc991d76 100644 (file)
 /memcheck/tests/freebsd/stat
 /memcheck/tests/freebsd/statfs
 /memcheck/tests/freebsd/static_allocs
+/memcheck/tests/freebsd/strlcat_strlcpy
 /memcheck/tests/freebsd/timerfd
 /memcheck/tests/freebsd/timing_safe
 /memcheck/tests/freebsd/utimens
diff --git a/NEWS b/NEWS
index 79482f20765b0fffd54291f6da7207f460d81040..5578caf4df7b8dcba8529a0b3657624987e6b45f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,7 @@ are not entered into bugzilla tend to get forgotten about or ignored.
 475498  Add reallocarray wrapper
 476320  Build failure with GCC
 476535  Difference in allocation size for massif/tests/overloaded-new between clang++/libc++ and g++/libstdc++
+476780  Extend strlcat and strlcpy wrappers to GNU libc
 476787  Build of Valgrind 3.21.0 fails when SOLARIS_PT_SUNDWTRACE_THRP is defined
 
 To see details of a given bug, visit
index 62d83371c1e333a58d19d05c0d9e14aaf018767e..5d491b9c3bc4a0599fda8b55f9cd47caff61ff4e 100755 (executable)
@@ -4923,7 +4923,7 @@ AC_CHECK_FUNCS([     \
         pwritev2     \
         rawmemchr    \
         readlinkat   \
-        reallocarray   \
+        reallocarray \
         semtimedop   \
         setcontext   \
         signalfd     \
@@ -4937,6 +4937,8 @@ AC_CHECK_FUNCS([     \
         syscall      \
         utimensat    \
         mempcpy      \
+        strlcat      \
+        strlcpy      \
         stpncpy      \
         strchrnul    \
         memrchr      \
@@ -4976,6 +4978,10 @@ AM_CONDITIONAL([HAVE_REALLOCARRAY],
                [test x$ac_cv_func_reallocarray = xyes])
 AM_CONDITIONAL([HAVE_WCSNCPY],
                [test x$ac_cv_func_wcsncpy = xyes])
+AM_CONDITIONAL([HAVE_STRLCAT],
+               [test x$ac_cv_func_strlcat = xyes])
+AM_CONDITIONAL([HAVE_STRLCPY],
+               [test x$ac_cv_func_strlcpy = xyes])
 
 if test x$VGCONF_PLATFORM_PRI_CAPS = xMIPS32_LINUX \
      -o x$VGCONF_PLATFORM_PRI_CAPS = xMIPS64_LINUX \
index 0e45aeab61b6c99bfa33f52fc1af4dd4739b5f83..fb1149f98b44302d9c816bc56b1274e51352c47b 100644 (file)
@@ -115,6 +115,8 @@ EXTRA_DIST = \
        statfs.stderr.exp \
        static_allocs.vgtest \
        static_allocs.stderr.exp \
+       strlcat_strlcpy.vgtest \
+       strlcat_strlcpy.stderr.exp \
        supponlyobj.vgtest \
        supponlyobj.stderr.exp \
        supponlyobj.supp \
@@ -148,6 +150,7 @@ check_PROGRAMS = \
        stat \
        statfs \
        static_allocs \
+       strlcat_strlcpy \
        timing_safe \
        utimens \
        utimes
diff --git a/memcheck/tests/freebsd/strlcat_strlcpy.c b/memcheck/tests/freebsd/strlcat_strlcpy.c
new file mode 100644 (file)
index 0000000..4d07b5a
--- /dev/null
@@ -0,0 +1,47 @@
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdio.h>
+
+int main(void)
+{
+   const size_t dstsize = 100U;
+   char *dst = malloc(dstsize);
+   // normal use
+   strlcpy(dst, "test1", dstsize);
+   assert(!strcmp(dst, "test1"));
+   strcat(dst, "test2");
+   // overlap, source starts within dst string
+   strlcpy(dst+4, dst+9, dstsize-4);
+   sprintf(dst, "test1test2");
+   // overlap, dst starts within src string
+   strlcpy(dst+9, dst+4, dstsize-9);
+   sprintf(dst, "test1");
+   // overlap, dst points to nul terminator of src
+   strlcpy(dst+5, dst+4, dstsize-5);
+   sprintf(dst, "test1");
+   // as above but incorrect length (1 too long)
+   // since src nul is overwritten this will
+   // keep reading from src until the length limit
+   // is reached
+   // since the length is wrong this will result
+   // in an invalid read and write 1 byte
+   // beyond the end of the buffer
+   strlcpy(dst+5, dst+4, dstsize-4);
+   
+   sprintf(dst, "test1");
+   strlcat(dst, "test2", dstsize);
+   assert(!strcmp(dst, "test1test2"));
+   
+   strlcat(dst+5, dst+7, dstsize-5);
+   sprintf(dst, "test1test2");
+   // we can't really control 'dst' since
+   // the destination id the end of the string
+   strlcat(dst+7, dst+5, dstsize-7);
+   
+   // again wrong dstsize
+   sprintf(dst, "test1");
+   strlcpy(dst+3, dst+4, dstsize-2);
+   free(dst);
+}
+
diff --git a/memcheck/tests/freebsd/strlcat_strlcpy.stderr.exp b/memcheck/tests/freebsd/strlcat_strlcpy.stderr.exp
new file mode 100644 (file)
index 0000000..0737f7b
--- /dev/null
@@ -0,0 +1,42 @@
+Source and destination overlap in strlcpy(0x........, 0x........, 96)
+   at 0x........: strlcpy (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:15)
+
+Source and destination overlap in strlcpy(0x........, 0x........, 91)
+   at 0x........: strlcpy (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:18)
+
+Source and destination overlap in strlcpy(0x........, 0x........, 95)
+   at 0x........: strlcpy (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:21)
+
+Source and destination overlap in strlcpy(0x........, 0x........, 96)
+   at 0x........: strlcpy (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:30)
+
+Invalid write of size 1
+   at 0x........: strlcpy (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:30)
+ Address 0x........ is 0 bytes after a block of size 100 alloc'd
+   at 0x........: malloc (vg_replace_malloc.c:...)
+   by 0x........: main (strlcat_strlcpy.c:9)
+
+Invalid read of size 1
+   at 0x........: strlcpy (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:30)
+ Address 0x........ is 0 bytes after a block of size 100 alloc'd
+   at 0x........: malloc (vg_replace_malloc.c:...)
+   by 0x........: main (strlcat_strlcpy.c:9)
+
+Source and destination overlap in strlcat(0x........, 0x........, 95)
+   at 0x........: strlcat (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:36)
+
+Source and destination overlap in strlcat(0x........, 0x........, 93)
+   at 0x........: strlcat (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:40)
+
+Source and destination overlap in strlcpy(0x........, 0x........, 98)
+   at 0x........: strlcpy (vg_replace_strmem.c:...)
+   by 0x........: main (strlcat_strlcpy.c:44)
+
diff --git a/memcheck/tests/freebsd/strlcat_strlcpy.vgtest b/memcheck/tests/freebsd/strlcat_strlcpy.vgtest
new file mode 100644 (file)
index 0000000..d29afe0
--- /dev/null
@@ -0,0 +1,3 @@
+prog: strlcat_strlcpy
+vgopts: -q
+
index 747b903fefd93530779fe33115ee26c234d4c635..9a2eda67379929eb58a70ab2f05f81bb33957a89 100644 (file)
@@ -431,6 +431,7 @@ static inline void my_exit ( int x )
    }
 
 #if defined(VGO_linux)
+ STRLCAT(VG_Z_LIBC_SONAME,   strlcat)
 
 #elif defined(VGO_freebsd)
  STRLCAT(VG_Z_LD_ELF_SO_1,   strlcat)
@@ -660,11 +661,8 @@ static inline void my_exit ( int x )
 
 #if defined(VGO_linux)
 
-#if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
-    || defined(VGPV_mips32_linux_android)
  #define STRLCPY_CHECK_FOR_DSTSIZE_ZERO
  STRLCPY(VG_Z_LIBC_SONAME, strlcpy);
-#endif
 
 #elif defined(VGO_freebsd)
  #define STRLCPY_CHECK_FOR_DSTSIZE_ZERO