Message ID | 20220902204351.2521805-4-keescook@chromium.org (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | Fix FORTIFY=y UBSAN_LOCAL_BOUNDS=y | expand |
On Fri, Sep 02, 2022 at 01:43:51PM -0700, Kees Cook wrote: > Clean up uses of "(size_t)-1" in favor of SIZE_MAX. > > Cc: linux-hardening@vger.kernel.org > Suggested-by: Nick Desaulniers <ndesaulniers@google.com> > Signed-off-by: Kees Cook <keescook@chromium.org> > --- > include/linux/fortify-string.h | 28 ++++++++++++++-------------- > 1 file changed, 14 insertions(+), 14 deletions(-) *brown paper bag* This needs: #include <linux/limits.h> I will fix it locally...
Hi Kees, I love your patch! Yet something to improve: [auto build test ERROR on akpm-mm/mm-everything] [also build test ERROR on linus/master kees/for-next/pstore v6.0-rc3 next-20220901] [cannot apply to kees/for-next/hardening] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Kees-Cook/Fix-FORTIFY-y-UBSAN_LOCAL_BOUNDS-y/20220903-044622 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything config: powerpc-allyesconfig (https://download.01.org/0day-ci/archive/20220903/202209030625.wdoOhjAW-lkp@intel.com/config) compiler: powerpc-linux-gcc (GCC) 12.1.0 reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/intel-lab-lkp/linux/commit/6c991b3511a531e44e856fdf2b64020b70fd7b22 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Kees-Cook/Fix-FORTIFY-y-UBSAN_LOCAL_BOUNDS-y/20220903-044622 git checkout 6c991b3511a531e44e856fdf2b64020b70fd7b22 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=powerpc prepare If you fix the issue, kindly add following tag where applicable Reported-by: kernel test robot <lkp@intel.com> All errors (new ones prefixed by >>): scripts/genksyms/parse.y: warning: 9 shift/reduce conflicts [-Wconflicts-sr] scripts/genksyms/parse.y: warning: 5 reduce/reduce conflicts [-Wconflicts-rr] scripts/genksyms/parse.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples In file included from include/linux/string.h:253, from include/linux/uuid.h:12, from include/linux/mod_devicetable.h:13, from scripts/mod/devicetable-offsets.c:3: include/linux/fortify-string.h: In function 'strcat': >> include/linux/fortify-string.h:98:23: error: 'SIZE_MAX' undeclared (first use in this function) 98 | if (p_size == SIZE_MAX) | ^~~~~~~~ include/linux/fortify-string.h:6:1: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? 5 | #include <linux/const.h> +++ |+#include <stdint.h> 6 | include/linux/fortify-string.h:98:23: note: each undeclared identifier is reported only once for each function it appears in 98 | if (p_size == SIZE_MAX) | ^~~~~~~~ include/linux/fortify-string.h: In function 'strnlen': include/linux/fortify-string.h:20:24: error: 'SIZE_MAX' undeclared (first use in this function) 20 | size_t __ret = SIZE_MAX; \ | ^~~~~~~~ include/linux/fortify-string.h:109:24: note: in expansion of macro '__compiletime_strlen' 109 | size_t p_len = __compiletime_strlen(p); | ^~~~~~~~~~~~~~~~~~~~ include/linux/fortify-string.h:20:24: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? 20 | size_t __ret = SIZE_MAX; \ | ^~~~~~~~ include/linux/fortify-string.h:109:24: note: in expansion of macro '__compiletime_strlen' 109 | size_t p_len = __compiletime_strlen(p); | ^~~~~~~~~~~~~~~~~~~~ include/linux/fortify-string.h: In function '__fortify_strlen': include/linux/fortify-string.h:141:23: error: 'SIZE_MAX' undeclared (first use in this function) 141 | if (p_size == SIZE_MAX) | ^~~~~~~~ include/linux/fortify-string.h:141:23: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? include/linux/fortify-string.h: In function 'strlcpy': include/linux/fortify-string.h:158:23: error: 'SIZE_MAX' undeclared (first use in this function) 158 | if (p_size == SIZE_MAX && q_size == SIZE_MAX) | ^~~~~~~~ include/linux/fortify-string.h:158:23: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? include/linux/fortify-string.h: In function 'strscpy': include/linux/fortify-string.h:186:23: error: 'SIZE_MAX' undeclared (first use in this function) 186 | if (p_size == SIZE_MAX && q_size == SIZE_MAX) | ^~~~~~~~ include/linux/fortify-string.h:186:23: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? include/linux/fortify-string.h: In function 'strncat': include/linux/fortify-string.h:231:23: error: 'SIZE_MAX' undeclared (first use in this function) 231 | if (p_size == SIZE_MAX && q_size == SIZE_MAX) | ^~~~~~~~ include/linux/fortify-string.h:231:23: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? include/linux/fortify-string.h: In function 'fortify_memset_chk': include/linux/fortify-string.h:275:23: error: 'SIZE_MAX' undeclared (first use in this function) 275 | if (p_size != SIZE_MAX && p_size < size) | ^~~~~~~~ include/linux/fortify-string.h:275:23: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? include/linux/fortify-string.h: In function 'fortify_memcpy_chk': include/linux/fortify-string.h:369:24: error: 'SIZE_MAX' undeclared (first use in this function) 369 | if ((p_size != SIZE_MAX && p_size < size) || | ^~~~~~~~ include/linux/fortify-string.h:369:24: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? include/linux/fortify-string.h: In function 'strcpy': include/linux/fortify-string.h:469:23: error: 'SIZE_MAX' undeclared (first use in this function) 469 | if (p_size == SIZE_MAX && q_size == SIZE_MAX) | ^~~~~~~~ include/linux/fortify-string.h:469:23: note: 'SIZE_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'? make[2]: *** [scripts/Makefile.build:117: scripts/mod/devicetable-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors. make[1]: *** [Makefile:1204: prepare0] Error 2 make[1]: Target 'prepare' not remade because of errors. make: *** [Makefile:222: __sub-make] Error 2 make: Target 'prepare' not remade because of errors. vim +/SIZE_MAX +98 include/linux/fortify-string.h 92 93 __FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2) 94 char *strcat(char * const POS p, const char *q) 95 { 96 size_t p_size = __builtin_object_size(p, 1); 97 > 98 if (p_size == SIZE_MAX) 99 return __underlying_strcat(p, q); 100 if (strlcat(p, q, p_size) >= p_size) 101 fortify_panic(__func__); 102 return p; 103 } 104
diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 07d5d1921eff..8f2b6b1cb848 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -17,9 +17,9 @@ void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning(" #define __compiletime_strlen(p) \ ({ \ unsigned char *__p = (unsigned char *)(p); \ - size_t __ret = (size_t)-1; \ + size_t __ret = SIZE_MAX; \ size_t __p_size = __builtin_object_size(p, 1); \ - if (__p_size != (size_t)-1 && \ + if (__p_size != SIZE_MAX && \ __builtin_constant_p(*__p)) { \ size_t __p_len = __p_size - 1; \ if (__builtin_constant_p(__p[__p_len]) && \ @@ -125,7 +125,7 @@ char *strcat(char * const POS p, const char *q) { size_t p_size = __builtin_object_size(p, 1); - if (p_size == (size_t)-1) + if (p_size == SIZE_MAX) return __underlying_strcat(p, q); if (strlcat(p, q, p_size) >= p_size) fortify_panic(__func__); @@ -140,7 +140,7 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size size_t ret; /* We can take compile-time actions when maxlen is const. */ - if (__builtin_constant_p(maxlen) && p_len != (size_t)-1) { + if (__builtin_constant_p(maxlen) && p_len != SIZE_MAX) { /* If p is const, we can use its compile-time-known len. */ if (maxlen >= p_size) return p_len; @@ -168,7 +168,7 @@ __kernel_size_t __fortify_strlen(const char * const POS p) size_t p_size = __builtin_object_size(p, 1); /* Give up if we don't know how large p is. */ - if (p_size == (size_t)-1) + if (p_size == SIZE_MAX) return __underlying_strlen(p); ret = strnlen(p, p_size); if (p_size <= ret) @@ -185,7 +185,7 @@ __FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, si size_t q_len; /* Full count of source string length. */ size_t len; /* Count of characters going into destination. */ - if (p_size == (size_t)-1 && q_size == (size_t)-1) + if (p_size == SIZE_MAX && q_size == SIZE_MAX) return __real_strlcpy(p, q, size); q_len = strlen(q); len = (q_len >= size) ? size - 1 : q_len; @@ -213,7 +213,7 @@ __FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, s size_t q_size = __builtin_object_size(q, 1); /* If we cannot get size of p and q default to call strscpy. */ - if (p_size == (size_t) -1 && q_size == (size_t) -1) + if (p_size == SIZE_MAX && q_size == SIZE_MAX) return __real_strscpy(p, q, size); /* @@ -258,7 +258,7 @@ char *strncat(char * const POS p, const char * const POS q, __kernel_size_t coun size_t p_size = __builtin_object_size(p, 1); size_t q_size = __builtin_object_size(q, 1); - if (p_size == (size_t)-1 && q_size == (size_t)-1) + if (p_size == SIZE_MAX && q_size == SIZE_MAX) return __underlying_strncat(p, q, count); p_len = strlen(p); copy_len = strnlen(q, count); @@ -299,10 +299,10 @@ __FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size, /* * Always stop accesses beyond the struct that contains the * field, when the buffer's remaining size is known. - * (The -1 test is to optimize away checks where the buffer + * (The SIZE_MAX test is to optimize away checks where the buffer * lengths are unknown.) */ - if (p_size != (size_t)(-1) && p_size < size) + if (p_size != SIZE_MAX && p_size < size) fortify_panic("memset"); } @@ -393,11 +393,11 @@ __FORTIFY_INLINE void fortify_memcpy_chk(__kernel_size_t size, /* * Always stop accesses beyond the struct that contains the * field, when the buffer's remaining size is known. - * (The -1 test is to optimize away checks where the buffer + * (The SIZE_MAX test is to optimize away checks where the buffer * lengths are unknown.) */ - if ((p_size != (size_t)(-1) && p_size < size) || - (q_size != (size_t)(-1) && q_size < size)) + if ((p_size != SIZE_MAX && p_size < size) || + (q_size != SIZE_MAX && q_size < size)) fortify_panic(func); } @@ -496,7 +496,7 @@ char *strcpy(char * const POS p, const char * const POS q) size_t size; /* If neither buffer size is known, immediately give up. */ - if (p_size == (size_t)-1 && q_size == (size_t)-1) + if (p_size == SIZE_MAX && q_size == SIZE_MAX) return __underlying_strcpy(p, q); size = strlen(q) + 1; /* Compile-time check for const size overflow. */
Clean up uses of "(size_t)-1" in favor of SIZE_MAX. Cc: linux-hardening@vger.kernel.org Suggested-by: Nick Desaulniers <ndesaulniers@google.com> Signed-off-by: Kees Cook <keescook@chromium.org> --- include/linux/fortify-string.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)