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
292292 DLL_EXPORT int __bound_strcmp(const char *s1, const char *s2);
293293 DLL_EXPORT int __bound_strncmp(const char *s1, const char *s2, size_t n);
294294 DLL_EXPORT char *__bound_strcat(char *dest, const char *src);
295+DLL_EXPORT char *__bound_strncat(char *dest, const char *src, size_t n);
295296 DLL_EXPORT char *__bound_strchr(const char *string, int ch);
297+DLL_EXPORT char *__bound_strrchr(const char *string, int ch);
296298 DLL_EXPORT char *__bound_strdup(const char *s);
297299
298300 #if defined(__arm__) && defined(__ARM_EABI__)
@@ -424,7 +426,9 @@ static unsigned long long bound_strncpy_count; · 125 unmodified lines
424426 static unsigned long long bound_strcmp_count;
425427 static unsigned long long bound_strncmp_count;
426428 static unsigned long long bound_strcat_count;
429+static unsigned long long bound_strncat_count;
427430 static unsigned long long bound_strchr_count;
431+static unsigned long long bound_strrchr_count;
428432 static unsigned long long bound_strdup_count;
429433 static unsigned long long bound_not_found;
430434 #define INCR_COUNT(x) ++x
@@ -1259,7 +1263,9 @@ void __attribute__((destructor)) __bound_exit(void) · 828 unmodified lines
12591263 fprintf (stderr, "bound_strcmp_count %llu\n", bound_strcmp_count);
12601264 fprintf (stderr, "bound_strncmp_count %llu\n", bound_strncmp_count);
12611265 fprintf (stderr, "bound_strcat_count %llu\n", bound_strcat_count);
1266+ fprintf (stderr, "bound_strncat_count %llu\n", bound_strncat_count);
12621267 fprintf (stderr, "bound_strchr_count %llu\n", bound_strchr_count);
1268+ fprintf (stderr, "bound_strrchr_count %llu\n", bound_strrchr_count);
12631269 fprintf (stderr, "bound_strdup_count %llu\n", bound_strdup_count);
12641270 fprintf (stderr, "bound_not_found %llu\n", bound_not_found);
12651271 #endif
@@ -1940,6 +1946,24 @@ char *__bound_strcat(char *dest, const char *src) · 674 unmodified lines
19401946 return strcat(r, s);
19411947 }
19421948
1949+char *__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+
19431967 char *__bound_strchr(const char *s, int c)
19441968 {
19451969 const unsigned char *str = (const unsigned char *) s;
@@ -1957,6 +1981,24 @@ char *__bound_strchr(const char *s, int c) · 11 unmodified lines
19571981 return *str == ch ? (char *) str : NULL;
19581982 }
19591983
1984+char *__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+
19602002 char *__bound_strdup(const char *s)
19612003 {
19622004 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