History

Add strncat and strrchr to bounds checking

herman ten brugge committed 3 years ago

Commit 28fa4d31 parent f48efee6 files · +89 −7Browse files

Showing the changes to lib (1 of 6 files). Show all

Unified · Split

lib/bcheck.c+42−0
@@ -292,7 +292,9 @@ DLL_EXPORT char *__bound_strncpy(char *dst, const char *src, size_t n); · 291 unmodified lines
292DLL_EXPORT int __bound_strcmp(const char *s1, const char *s2);292DLL_EXPORT int __bound_strcmp(const char *s1, const char *s2);
293DLL_EXPORT int __bound_strncmp(const char *s1, const char *s2, size_t n);293DLL_EXPORT int __bound_strncmp(const char *s1, const char *s2, size_t n);
294DLL_EXPORT char *__bound_strcat(char *dest, const char *src);294DLL_EXPORT char *__bound_strcat(char *dest, const char *src);
295DLL_EXPORT char *__bound_strncat(char *dest, const char *src, size_t n);
295DLL_EXPORT char *__bound_strchr(const char *string, int ch);296DLL_EXPORT char *__bound_strchr(const char *string, int ch);
297DLL_EXPORT char *__bound_strrchr(const char *string, int ch);
296DLL_EXPORT char *__bound_strdup(const char *s);298DLL_EXPORT char *__bound_strdup(const char *s);
297 299
298#if defined(__arm__) && defined(__ARM_EABI__)300#if defined(__arm__) && defined(__ARM_EABI__)
@@ -424,7 +426,9 @@ static unsigned long long bound_strncpy_count; · 125 unmodified lines
424static unsigned long long bound_strcmp_count;426static unsigned long long bound_strcmp_count;
425static unsigned long long bound_strncmp_count;427static unsigned long long bound_strncmp_count;
426static unsigned long long bound_strcat_count;428static unsigned long long bound_strcat_count;
429static unsigned long long bound_strncat_count;
427static unsigned long long bound_strchr_count;430static unsigned long long bound_strchr_count;
431static unsigned long long bound_strrchr_count;
428static unsigned long long bound_strdup_count;432static unsigned long long bound_strdup_count;
429static unsigned long long bound_not_found;433static unsigned long long bound_not_found;
430#define INCR_COUNT(x) ++x434#define INCR_COUNT(x) ++x
@@ -1259,7 +1263,9 @@ void __attribute__((destructor)) __bound_exit(void) · 828 unmodified lines
1259 fprintf (stderr, "bound_strcmp_count %llu\n", bound_strcmp_count);1263 fprintf (stderr, "bound_strcmp_count %llu\n", bound_strcmp_count);
1260 fprintf (stderr, "bound_strncmp_count %llu\n", bound_strncmp_count);1264 fprintf (stderr, "bound_strncmp_count %llu\n", bound_strncmp_count);
1261 fprintf (stderr, "bound_strcat_count %llu\n", bound_strcat_count);1265 fprintf (stderr, "bound_strcat_count %llu\n", bound_strcat_count);
1266 fprintf (stderr, "bound_strncat_count %llu\n", bound_strncat_count);
1262 fprintf (stderr, "bound_strchr_count %llu\n", bound_strchr_count);1267 fprintf (stderr, "bound_strchr_count %llu\n", bound_strchr_count);
1268 fprintf (stderr, "bound_strrchr_count %llu\n", bound_strrchr_count);
1263 fprintf (stderr, "bound_strdup_count %llu\n", bound_strdup_count);1269 fprintf (stderr, "bound_strdup_count %llu\n", bound_strdup_count);
1264 fprintf (stderr, "bound_not_found %llu\n", bound_not_found);1270 fprintf (stderr, "bound_not_found %llu\n", bound_not_found);
1265#endif1271#endif
@@ -1940,6 +1946,24 @@ char *__bound_strcat(char *dest, const char *src) · 674 unmodified lines
1940 return strcat(r, s);1946 return strcat(r, s);
1941}1947}
1942 1948
1949char *__bound_strncat(char *dest, const char *src, size_t n)
1950{
1951 char *r = dest;
1952 const char *s = src;
1953 size_t len = n;
1954
1955 dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n",
1956 __FILE__, __FUNCTION__, dest, src, (unsigned long)n);
1957 INCR_COUNT(bound_strncat_count);
1958 while (*dest++);
1959 while (len-- && *src++);
1960 __bound_check(r, (dest - r) + (src - s) - 1, "strncat dest");
1961 __bound_check(s, src - s, "strncat src");
1962 if (check_overlap(r, (dest - r) + (src - s) - 1, s, src - s, "strncat"))
1963 return dest;
1964 return strncat(r, s, n);
1965}
1966
1943char *__bound_strchr(const char *s, int c)1967char *__bound_strchr(const char *s, int c)
1944{1968{
1945 const unsigned char *str = (const unsigned char *) s;1969 const unsigned char *str = (const unsigned char *) s;
@@ -1957,6 +1981,24 @@ char *__bound_strchr(const char *s, int c) · 11 unmodified lines
1957 return *str == ch ? (char *) str : NULL;1981 return *str == ch ? (char *) str : NULL;
1958}1982}
1959 1983
1984char *__bound_strrchr(const char *s, int c)
1985{
1986 const unsigned char *str = (const unsigned char *) s;
1987 unsigned char ch = c;
1988
1989 dprintf(stderr, "%s, %s(): %p, %d\n",
1990 __FILE__, __FUNCTION__, s, ch);
1991 INCR_COUNT(bound_strrchr_count);
1992 while (*str++);
1993 __bound_check(s, (const char *)str - s, "strrchr");
1994 while (str != (const unsigned char *)s) {
1995 if (*--str == ch)
1996 break;
1997 }
1998 __bound_check(s, (const char *)str - s, "strrchr");
1999 return *str == ch ? (char *) str : NULL;
2000}
2001
1960char *__bound_strdup(const char *s)2002char *__bound_strdup(const char *s)
1961{2003{
1962 const char *p = s;2004 const char *p = s;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t