Commit 7e0d92f0 authored by Vasily Gorbik's avatar Vasily Gorbik Committed by Martin Schwidefsky

s390/kasan: improve string/memory functions checks

Avoid using arch specific implementations of string/memory functions
with KASAN since gcc cannot instrument asm code memory accesses and
many bugs could be missed.
Acked-by: default avatarHeiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: default avatarVasily Gorbik <gor@linux.ibm.com>
Signed-off-by: default avatarMartin Schwidefsky <schwidefsky@de.ibm.com>
parent 32b77252
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <linux/ctype.h> #include <linux/ctype.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/errno.h> #include <linux/errno.h>
#undef CONFIG_KASAN
#include "../lib/string.c" #include "../lib/string.c"
int strncmp(const char *cs, const char *ct, size_t count) int strncmp(const char *cs, const char *ct, size_t count)
......
...@@ -12,15 +12,21 @@ ...@@ -12,15 +12,21 @@
#include <linux/types.h> #include <linux/types.h>
#endif #endif
#define __HAVE_ARCH_MEMCHR /* inline & arch function */
#define __HAVE_ARCH_MEMCMP /* arch function */
#define __HAVE_ARCH_MEMCPY /* gcc builtin & arch function */ #define __HAVE_ARCH_MEMCPY /* gcc builtin & arch function */
#define __HAVE_ARCH_MEMMOVE /* gcc builtin & arch function */ #define __HAVE_ARCH_MEMMOVE /* gcc builtin & arch function */
#define __HAVE_ARCH_MEMSCAN /* inline & arch function */
#define __HAVE_ARCH_MEMSET /* gcc builtin & arch function */ #define __HAVE_ARCH_MEMSET /* gcc builtin & arch function */
#define __HAVE_ARCH_MEMSET16 /* arch function */ #define __HAVE_ARCH_MEMSET16 /* arch function */
#define __HAVE_ARCH_MEMSET32 /* arch function */ #define __HAVE_ARCH_MEMSET32 /* arch function */
#define __HAVE_ARCH_MEMSET64 /* arch function */ #define __HAVE_ARCH_MEMSET64 /* arch function */
void *memcpy(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
void *memmove(void *dest, const void *src, size_t n);
#ifndef CONFIG_KASAN
#define __HAVE_ARCH_MEMCHR /* inline & arch function */
#define __HAVE_ARCH_MEMCMP /* arch function */
#define __HAVE_ARCH_MEMSCAN /* inline & arch function */
#define __HAVE_ARCH_STRCAT /* inline & arch function */ #define __HAVE_ARCH_STRCAT /* inline & arch function */
#define __HAVE_ARCH_STRCMP /* arch function */ #define __HAVE_ARCH_STRCMP /* arch function */
#define __HAVE_ARCH_STRCPY /* inline & arch function */ #define __HAVE_ARCH_STRCPY /* inline & arch function */
...@@ -35,9 +41,6 @@ ...@@ -35,9 +41,6 @@
/* Prototypes for non-inlined arch strings functions. */ /* Prototypes for non-inlined arch strings functions. */
int memcmp(const void *s1, const void *s2, size_t n); int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
void *memmove(void *dest, const void *src, size_t n);
int strcmp(const char *s1, const char *s2); int strcmp(const char *s1, const char *s2);
size_t strlcat(char *dest, const char *src, size_t n); size_t strlcat(char *dest, const char *src, size_t n);
size_t strlcpy(char *dest, const char *src, size_t size); size_t strlcpy(char *dest, const char *src, size_t size);
...@@ -45,6 +48,7 @@ char *strncat(char *dest, const char *src, size_t n); ...@@ -45,6 +48,7 @@ char *strncat(char *dest, const char *src, size_t n);
char *strncpy(char *dest, const char *src, size_t n); char *strncpy(char *dest, const char *src, size_t n);
char *strrchr(const char *s, int c); char *strrchr(const char *s, int c);
char *strstr(const char *s1, const char *s2); char *strstr(const char *s1, const char *s2);
#endif /* !CONFIG_KASAN */
#undef __HAVE_ARCH_STRCHR #undef __HAVE_ARCH_STRCHR
#undef __HAVE_ARCH_STRNCHR #undef __HAVE_ARCH_STRNCHR
...@@ -95,6 +99,7 @@ static inline void *memset64(uint64_t *s, uint64_t v, size_t count) ...@@ -95,6 +99,7 @@ static inline void *memset64(uint64_t *s, uint64_t v, size_t count)
#if !defined(IN_ARCH_STRING_C) && (!defined(CONFIG_FORTIFY_SOURCE) || defined(__NO_FORTIFY)) #if !defined(IN_ARCH_STRING_C) && (!defined(CONFIG_FORTIFY_SOURCE) || defined(__NO_FORTIFY))
#ifdef __HAVE_ARCH_MEMCHR
static inline void *memchr(const void * s, int c, size_t n) static inline void *memchr(const void * s, int c, size_t n)
{ {
register int r0 asm("0") = (char) c; register int r0 asm("0") = (char) c;
...@@ -109,7 +114,9 @@ static inline void *memchr(const void * s, int c, size_t n) ...@@ -109,7 +114,9 @@ static inline void *memchr(const void * s, int c, size_t n)
: "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory"); : "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory");
return (void *) ret; return (void *) ret;
} }
#endif
#ifdef __HAVE_ARCH_MEMSCAN
static inline void *memscan(void *s, int c, size_t n) static inline void *memscan(void *s, int c, size_t n)
{ {
register int r0 asm("0") = (char) c; register int r0 asm("0") = (char) c;
...@@ -121,7 +128,9 @@ static inline void *memscan(void *s, int c, size_t n) ...@@ -121,7 +128,9 @@ static inline void *memscan(void *s, int c, size_t n)
: "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory"); : "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory");
return (void *) ret; return (void *) ret;
} }
#endif
#ifdef __HAVE_ARCH_STRCAT
static inline char *strcat(char *dst, const char *src) static inline char *strcat(char *dst, const char *src)
{ {
register int r0 asm("0") = 0; register int r0 asm("0") = 0;
...@@ -137,7 +146,9 @@ static inline char *strcat(char *dst, const char *src) ...@@ -137,7 +146,9 @@ static inline char *strcat(char *dst, const char *src)
: "d" (r0), "0" (0) : "cc", "memory" ); : "d" (r0), "0" (0) : "cc", "memory" );
return ret; return ret;
} }
#endif
#ifdef __HAVE_ARCH_STRCPY
static inline char *strcpy(char *dst, const char *src) static inline char *strcpy(char *dst, const char *src)
{ {
register int r0 asm("0") = 0; register int r0 asm("0") = 0;
...@@ -150,7 +161,9 @@ static inline char *strcpy(char *dst, const char *src) ...@@ -150,7 +161,9 @@ static inline char *strcpy(char *dst, const char *src)
: "cc", "memory"); : "cc", "memory");
return ret; return ret;
} }
#endif
#ifdef __HAVE_ARCH_STRLEN
static inline size_t strlen(const char *s) static inline size_t strlen(const char *s)
{ {
register unsigned long r0 asm("0") = 0; register unsigned long r0 asm("0") = 0;
...@@ -162,7 +175,9 @@ static inline size_t strlen(const char *s) ...@@ -162,7 +175,9 @@ static inline size_t strlen(const char *s)
: "+d" (r0), "+a" (tmp) : : "cc", "memory"); : "+d" (r0), "+a" (tmp) : : "cc", "memory");
return r0 - (unsigned long) s; return r0 - (unsigned long) s;
} }
#endif
#ifdef __HAVE_ARCH_STRNLEN
static inline size_t strnlen(const char * s, size_t n) static inline size_t strnlen(const char * s, size_t n)
{ {
register int r0 asm("0") = 0; register int r0 asm("0") = 0;
...@@ -175,6 +190,7 @@ static inline size_t strnlen(const char * s, size_t n) ...@@ -175,6 +190,7 @@ static inline size_t strnlen(const char * s, size_t n)
: "+a" (end), "+a" (tmp) : "d" (r0) : "cc", "memory"); : "+a" (end), "+a" (tmp) : "d" (r0) : "cc", "memory");
return end - s; return end - s;
} }
#endif
#else /* IN_ARCH_STRING_C */ #else /* IN_ARCH_STRING_C */
void *memchr(const void * s, int c, size_t n); void *memchr(const void * s, int c, size_t n);
void *memscan(void *s, int c, size_t n); void *memscan(void *s, int c, size_t n);
......
...@@ -43,11 +43,13 @@ static inline char *__strnend(const char *s, size_t n) ...@@ -43,11 +43,13 @@ static inline char *__strnend(const char *s, size_t n)
* *
* returns the length of @s * returns the length of @s
*/ */
#ifdef __HAVE_ARCH_STRLEN
size_t strlen(const char *s) size_t strlen(const char *s)
{ {
return __strend(s) - s; return __strend(s) - s;
} }
EXPORT_SYMBOL(strlen); EXPORT_SYMBOL(strlen);
#endif
/** /**
* strnlen - Find the length of a length-limited string * strnlen - Find the length of a length-limited string
...@@ -56,11 +58,13 @@ EXPORT_SYMBOL(strlen); ...@@ -56,11 +58,13 @@ EXPORT_SYMBOL(strlen);
* *
* returns the minimum of the length of @s and @n * returns the minimum of the length of @s and @n
*/ */
#ifdef __HAVE_ARCH_STRNLEN
size_t strnlen(const char *s, size_t n) size_t strnlen(const char *s, size_t n)
{ {
return __strnend(s, n) - s; return __strnend(s, n) - s;
} }
EXPORT_SYMBOL(strnlen); EXPORT_SYMBOL(strnlen);
#endif
/** /**
* strcpy - Copy a %NUL terminated string * strcpy - Copy a %NUL terminated string
...@@ -69,6 +73,7 @@ EXPORT_SYMBOL(strnlen); ...@@ -69,6 +73,7 @@ EXPORT_SYMBOL(strnlen);
* *
* returns a pointer to @dest * returns a pointer to @dest
*/ */
#ifdef __HAVE_ARCH_STRCPY
char *strcpy(char *dest, const char *src) char *strcpy(char *dest, const char *src)
{ {
register int r0 asm("0") = 0; register int r0 asm("0") = 0;
...@@ -81,6 +86,7 @@ char *strcpy(char *dest, const char *src) ...@@ -81,6 +86,7 @@ char *strcpy(char *dest, const char *src)
return ret; return ret;
} }
EXPORT_SYMBOL(strcpy); EXPORT_SYMBOL(strcpy);
#endif
/** /**
* strlcpy - Copy a %NUL terminated string into a sized buffer * strlcpy - Copy a %NUL terminated string into a sized buffer
...@@ -93,6 +99,7 @@ EXPORT_SYMBOL(strcpy); ...@@ -93,6 +99,7 @@ EXPORT_SYMBOL(strcpy);
* of course, the buffer size is zero). It does not pad * of course, the buffer size is zero). It does not pad
* out the result like strncpy() does. * out the result like strncpy() does.
*/ */
#ifdef __HAVE_ARCH_STRLCPY
size_t strlcpy(char *dest, const char *src, size_t size) size_t strlcpy(char *dest, const char *src, size_t size)
{ {
size_t ret = __strend(src) - src; size_t ret = __strend(src) - src;
...@@ -105,6 +112,7 @@ size_t strlcpy(char *dest, const char *src, size_t size) ...@@ -105,6 +112,7 @@ size_t strlcpy(char *dest, const char *src, size_t size)
return ret; return ret;
} }
EXPORT_SYMBOL(strlcpy); EXPORT_SYMBOL(strlcpy);
#endif
/** /**
* strncpy - Copy a length-limited, %NUL-terminated string * strncpy - Copy a length-limited, %NUL-terminated string
...@@ -115,6 +123,7 @@ EXPORT_SYMBOL(strlcpy); ...@@ -115,6 +123,7 @@ EXPORT_SYMBOL(strlcpy);
* The result is not %NUL-terminated if the source exceeds * The result is not %NUL-terminated if the source exceeds
* @n bytes. * @n bytes.
*/ */
#ifdef __HAVE_ARCH_STRNCPY
char *strncpy(char *dest, const char *src, size_t n) char *strncpy(char *dest, const char *src, size_t n)
{ {
size_t len = __strnend(src, n) - src; size_t len = __strnend(src, n) - src;
...@@ -123,6 +132,7 @@ char *strncpy(char *dest, const char *src, size_t n) ...@@ -123,6 +132,7 @@ char *strncpy(char *dest, const char *src, size_t n)
return dest; return dest;
} }
EXPORT_SYMBOL(strncpy); EXPORT_SYMBOL(strncpy);
#endif
/** /**
* strcat - Append one %NUL-terminated string to another * strcat - Append one %NUL-terminated string to another
...@@ -131,6 +141,7 @@ EXPORT_SYMBOL(strncpy); ...@@ -131,6 +141,7 @@ EXPORT_SYMBOL(strncpy);
* *
* returns a pointer to @dest * returns a pointer to @dest
*/ */
#ifdef __HAVE_ARCH_STRCAT
char *strcat(char *dest, const char *src) char *strcat(char *dest, const char *src)
{ {
register int r0 asm("0") = 0; register int r0 asm("0") = 0;
...@@ -146,6 +157,7 @@ char *strcat(char *dest, const char *src) ...@@ -146,6 +157,7 @@ char *strcat(char *dest, const char *src)
return ret; return ret;
} }
EXPORT_SYMBOL(strcat); EXPORT_SYMBOL(strcat);
#endif
/** /**
* strlcat - Append a length-limited, %NUL-terminated string to another * strlcat - Append a length-limited, %NUL-terminated string to another
...@@ -153,6 +165,7 @@ EXPORT_SYMBOL(strcat); ...@@ -153,6 +165,7 @@ EXPORT_SYMBOL(strcat);
* @src: The string to append to it * @src: The string to append to it
* @n: The size of the destination buffer. * @n: The size of the destination buffer.
*/ */
#ifdef __HAVE_ARCH_STRLCAT
size_t strlcat(char *dest, const char *src, size_t n) size_t strlcat(char *dest, const char *src, size_t n)
{ {
size_t dsize = __strend(dest) - dest; size_t dsize = __strend(dest) - dest;
...@@ -170,6 +183,7 @@ size_t strlcat(char *dest, const char *src, size_t n) ...@@ -170,6 +183,7 @@ size_t strlcat(char *dest, const char *src, size_t n)
return res; return res;
} }
EXPORT_SYMBOL(strlcat); EXPORT_SYMBOL(strlcat);
#endif
/** /**
* strncat - Append a length-limited, %NUL-terminated string to another * strncat - Append a length-limited, %NUL-terminated string to another
...@@ -182,6 +196,7 @@ EXPORT_SYMBOL(strlcat); ...@@ -182,6 +196,7 @@ EXPORT_SYMBOL(strlcat);
* Note that in contrast to strncpy, strncat ensures the result is * Note that in contrast to strncpy, strncat ensures the result is
* terminated. * terminated.
*/ */
#ifdef __HAVE_ARCH_STRNCAT
char *strncat(char *dest, const char *src, size_t n) char *strncat(char *dest, const char *src, size_t n)
{ {
size_t len = __strnend(src, n) - src; size_t len = __strnend(src, n) - src;
...@@ -192,6 +207,7 @@ char *strncat(char *dest, const char *src, size_t n) ...@@ -192,6 +207,7 @@ char *strncat(char *dest, const char *src, size_t n)
return dest; return dest;
} }
EXPORT_SYMBOL(strncat); EXPORT_SYMBOL(strncat);
#endif
/** /**
* strcmp - Compare two strings * strcmp - Compare two strings
...@@ -202,6 +218,7 @@ EXPORT_SYMBOL(strncat); ...@@ -202,6 +218,7 @@ EXPORT_SYMBOL(strncat);
* < 0 if @s1 is less than @s2 * < 0 if @s1 is less than @s2
* > 0 if @s1 is greater than @s2 * > 0 if @s1 is greater than @s2
*/ */
#ifdef __HAVE_ARCH_STRCMP
int strcmp(const char *s1, const char *s2) int strcmp(const char *s1, const char *s2)
{ {
register int r0 asm("0") = 0; register int r0 asm("0") = 0;
...@@ -219,12 +236,14 @@ int strcmp(const char *s1, const char *s2) ...@@ -219,12 +236,14 @@ int strcmp(const char *s1, const char *s2)
return ret; return ret;
} }
EXPORT_SYMBOL(strcmp); EXPORT_SYMBOL(strcmp);
#endif
/** /**
* strrchr - Find the last occurrence of a character in a string * strrchr - Find the last occurrence of a character in a string
* @s: The string to be searched * @s: The string to be searched
* @c: The character to search for * @c: The character to search for
*/ */
#ifdef __HAVE_ARCH_STRRCHR
char *strrchr(const char *s, int c) char *strrchr(const char *s, int c)
{ {
size_t len = __strend(s) - s; size_t len = __strend(s) - s;
...@@ -237,6 +256,7 @@ char *strrchr(const char *s, int c) ...@@ -237,6 +256,7 @@ char *strrchr(const char *s, int c)
return NULL; return NULL;
} }
EXPORT_SYMBOL(strrchr); EXPORT_SYMBOL(strrchr);
#endif
static inline int clcle(const char *s1, unsigned long l1, static inline int clcle(const char *s1, unsigned long l1,
const char *s2, unsigned long l2) const char *s2, unsigned long l2)
...@@ -261,6 +281,7 @@ static inline int clcle(const char *s1, unsigned long l1, ...@@ -261,6 +281,7 @@ static inline int clcle(const char *s1, unsigned long l1,
* @s1: The string to be searched * @s1: The string to be searched
* @s2: The string to search for * @s2: The string to search for
*/ */
#ifdef __HAVE_ARCH_STRSTR
char *strstr(const char *s1, const char *s2) char *strstr(const char *s1, const char *s2)
{ {
int l1, l2; int l1, l2;
...@@ -280,6 +301,7 @@ char *strstr(const char *s1, const char *s2) ...@@ -280,6 +301,7 @@ char *strstr(const char *s1, const char *s2)
return NULL; return NULL;
} }
EXPORT_SYMBOL(strstr); EXPORT_SYMBOL(strstr);
#endif
/** /**
* memchr - Find a character in an area of memory. * memchr - Find a character in an area of memory.
...@@ -290,6 +312,7 @@ EXPORT_SYMBOL(strstr); ...@@ -290,6 +312,7 @@ EXPORT_SYMBOL(strstr);
* returns the address of the first occurrence of @c, or %NULL * returns the address of the first occurrence of @c, or %NULL
* if @c is not found * if @c is not found
*/ */
#ifdef __HAVE_ARCH_MEMCHR
void *memchr(const void *s, int c, size_t n) void *memchr(const void *s, int c, size_t n)
{ {
register int r0 asm("0") = (char) c; register int r0 asm("0") = (char) c;
...@@ -304,6 +327,7 @@ void *memchr(const void *s, int c, size_t n) ...@@ -304,6 +327,7 @@ void *memchr(const void *s, int c, size_t n)
return (void *) ret; return (void *) ret;
} }
EXPORT_SYMBOL(memchr); EXPORT_SYMBOL(memchr);
#endif
/** /**
* memcmp - Compare two areas of memory * memcmp - Compare two areas of memory
...@@ -311,6 +335,7 @@ EXPORT_SYMBOL(memchr); ...@@ -311,6 +335,7 @@ EXPORT_SYMBOL(memchr);
* @s2: Another area of memory * @s2: Another area of memory
* @count: The size of the area. * @count: The size of the area.
*/ */
#ifdef __HAVE_ARCH_MEMCMP
int memcmp(const void *s1, const void *s2, size_t n) int memcmp(const void *s1, const void *s2, size_t n)
{ {
int ret; int ret;
...@@ -321,6 +346,7 @@ int memcmp(const void *s1, const void *s2, size_t n) ...@@ -321,6 +346,7 @@ int memcmp(const void *s1, const void *s2, size_t n)
return ret; return ret;
} }
EXPORT_SYMBOL(memcmp); EXPORT_SYMBOL(memcmp);
#endif
/** /**
* memscan - Find a character in an area of memory. * memscan - Find a character in an area of memory.
...@@ -331,6 +357,7 @@ EXPORT_SYMBOL(memcmp); ...@@ -331,6 +357,7 @@ EXPORT_SYMBOL(memcmp);
* returns the address of the first occurrence of @c, or 1 byte past * returns the address of the first occurrence of @c, or 1 byte past
* the area if @c is not found * the area if @c is not found
*/ */
#ifdef __HAVE_ARCH_MEMSCAN
void *memscan(void *s, int c, size_t n) void *memscan(void *s, int c, size_t n)
{ {
register int r0 asm("0") = (char) c; register int r0 asm("0") = (char) c;
...@@ -342,3 +369,4 @@ void *memscan(void *s, int c, size_t n) ...@@ -342,3 +369,4 @@ void *memscan(void *s, int c, size_t n)
return (void *) ret; return (void *) ret;
} }
EXPORT_SYMBOL(memscan); EXPORT_SYMBOL(memscan);
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment