diff options
| author | jvoisin | 2023-08-20 18:15:24 +0200 |
|---|---|---|
| committer | jvoisin | 2023-08-20 20:51:06 +0200 |
| commit | e182dd0138d8040c44481a48277cab4098ccca0a (patch) | |
| tree | d92c3884bbe496c7619bfc9d9e508ab7c921354a | |
| parent | e441ae8c30b35ad7602ab428753afb4a335b34bf (diff) | |
Add hardening for strrchr
| -rw-r--r-- | include/string.h | 11 | ||||
| -rw-r--r-- | tests/Makefile | 2 | ||||
| -rw-r--r-- | tests/test_strrchr_dynamic_read.c | 17 | ||||
| -rw-r--r-- | tests/test_strrchr_static_read.c | 17 |
4 files changed, 47 insertions, 0 deletions
diff --git a/include/string.h b/include/string.h index 1fba9d9..4a3426a 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -102,6 +102,17 @@ _FORTIFY_FN(strchr) char *strchr(const char * _FORTIFY_POS0 __s, int __c) | |||
| 102 | return __r; | 102 | return __r; |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | __access(read_only, 1, 2) | ||
| 106 | _FORTIFY_FN(strrchr) char *strrchr(const char * _FORTIFY_POS0 __s, int __c) | ||
| 107 | { | ||
| 108 | size_t __b = __bos(__s, 0); | ||
| 109 | |||
| 110 | char* __r = __builtin_strrchr(__s, __c); | ||
| 111 | if (__r - __s > __b) | ||
| 112 | __builtin_trap(); | ||
| 113 | return __r; | ||
| 114 | } | ||
| 115 | |||
| 105 | #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ | 116 | #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ |
| 106 | || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ | 117 | || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ |
| 107 | || defined(_BSD_SOURCE) | 118 | || defined(_BSD_SOURCE) |
diff --git a/tests/Makefile b/tests/Makefile index 1ea610b..c2bc378 100644 --- a/tests/Makefile +++ b/tests/Makefile | |||
| @@ -84,6 +84,8 @@ TARGETS= \ | |||
| 84 | test_strncpy_overwrite_over \ | 84 | test_strncpy_overwrite_over \ |
| 85 | test_strncpy_overwrite_under \ | 85 | test_strncpy_overwrite_under \ |
| 86 | test_strncpy_static_write \ | 86 | test_strncpy_static_write \ |
| 87 | test_strrchr_dynamic_read \ | ||
| 88 | test_strrchr_static_read \ | ||
| 87 | test_ttyname_r_dynamic \ | 89 | test_ttyname_r_dynamic \ |
| 88 | test_ttyname_r_static \ | 90 | test_ttyname_r_static \ |
| 89 | test_vsnprintf_dynamic \ | 91 | test_vsnprintf_dynamic \ |
diff --git a/tests/test_strrchr_dynamic_read.c b/tests/test_strrchr_dynamic_read.c new file mode 100644 index 0000000..3b192c8 --- /dev/null +++ b/tests/test_strrchr_dynamic_read.c | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[] = {'1', '2', '3', '4', '5'}; | ||
| 7 | const char* padding = "ABCDEFGHIJKLMN"; | ||
| 8 | strrchr(buffer, (int)'4'); | ||
| 9 | puts(buffer); | ||
| 10 | |||
| 11 | CHK_FAIL_START | ||
| 12 | strrchr(buffer, (int)'A'); | ||
| 13 | CHK_FAIL_END | ||
| 14 | |||
| 15 | puts(buffer); | ||
| 16 | return ret; | ||
| 17 | } | ||
diff --git a/tests/test_strrchr_static_read.c b/tests/test_strrchr_static_read.c new file mode 100644 index 0000000..3b192c8 --- /dev/null +++ b/tests/test_strrchr_static_read.c | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[] = {'1', '2', '3', '4', '5'}; | ||
| 7 | const char* padding = "ABCDEFGHIJKLMN"; | ||
| 8 | strrchr(buffer, (int)'4'); | ||
| 9 | puts(buffer); | ||
| 10 | |||
| 11 | CHK_FAIL_START | ||
| 12 | strrchr(buffer, (int)'A'); | ||
| 13 | CHK_FAIL_END | ||
| 14 | |||
| 15 | puts(buffer); | ||
| 16 | return ret; | ||
| 17 | } | ||
