]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
replace incorrect strscpy use in FORTIFY_SOURCE
authorDaniel Micay <danielmicay@gmail.com>
Fri, 14 Jul 2017 21:28:12 +0000 (17:28 -0400)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 15 Jul 2017 04:56:31 +0000 (21:56 -0700)
Using strscpy was wrong because FORTIFY_SOURCE is passing the maximum
possible size of the outermost object, but strscpy defines the count
parameter as the exact buffer size, so this could copy past the end of
the source.  This would still be wrong with the planned usage of
__builtin_object_size(p, 1) for intra-object overflow checks since it's
the maximum possible size of the specified object with no guarantee of
it being that large.

Reuse of the fortified functions like this currently makes the runtime
error reporting less precise but that can be improved later on.

Noticed by Dave Jones and KASAN.

Signed-off-by: Daniel Micay <danielmicay@gmail.com>
Acked-by: Kees Cook <keescook@chromium.org>
Reported-by: Dave Jones <davej@codemonkey.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/string.h

index 96f5a5fd037747cb22ec6e815d090449eb3d9900..049866760e8bb604358fed5355442d0b097597d0 100644 (file)
@@ -202,17 +202,6 @@ void __read_overflow2(void) __compiletime_error("detected read beyond size of ob
 void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
 
 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
-__FORTIFY_INLINE char *strcpy(char *p, const char *q)
-{
-       size_t p_size = __builtin_object_size(p, 0);
-       size_t q_size = __builtin_object_size(q, 0);
-       if (p_size == (size_t)-1 && q_size == (size_t)-1)
-               return __builtin_strcpy(p, q);
-       if (strscpy(p, q, p_size < q_size ? p_size : q_size) < 0)
-               fortify_panic(__func__);
-       return p;
-}
-
 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
 {
        size_t p_size = __builtin_object_size(p, 0);
@@ -391,6 +380,18 @@ __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
                fortify_panic(__func__);
        return __real_kmemdup(p, size, gfp);
 }
+
+/* defined after fortified strlen and memcpy to reuse them */
+__FORTIFY_INLINE char *strcpy(char *p, const char *q)
+{
+       size_t p_size = __builtin_object_size(p, 0);
+       size_t q_size = __builtin_object_size(q, 0);
+       if (p_size == (size_t)-1 && q_size == (size_t)-1)
+               return __builtin_strcpy(p, q);
+       memcpy(p, q, strlen(q) + 1);
+       return p;
+}
+
 #endif
 
 #endif /* _LINUX_STRING_H_ */