summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2023-08-22 19:16:49 +0200
committerjvoisin2023-08-22 19:16:49 +0200
commitacfa9f6ce8295b2493b4e21b73463b93ef3c4333 (patch)
tree0b4ad45ed42caf5bf407f05ec75deb9ccba46ff5
parent9231e0905829f1c33abe07bed9be35298c9bdcf5 (diff)
Add hardening for pwrite
-rw-r--r--include/unistd.h11
-rw-r--r--tests/Makefile2
-rw-r--r--tests/test_pwrite_dynamic.c14
-rw-r--r--tests/test_pwrite_static.c14
4 files changed, 41 insertions, 0 deletions
diff --git a/include/unistd.h b/include/unistd.h
index 1c79711..e91f922 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -180,6 +180,17 @@ _FORTIFY_FN(write) ssize_t write(int __f, const void * _FORTIFY_POS0 __s,
180 return __orig_write(__f, __s, __n); 180 return __orig_write(__f, __s, __n);
181} 181}
182 182
183__diagnose_as_builtin(__builtin_pwrite, 1, 2, 3, 4)
184_FORTIFY_FN(pwrite) ssize_t pwrite(int __f, const void * _FORTIFY_POS0 __s,
185 size_t __n, off_t __o)
186{
187 size_t __b = __bos(__s, 0);
188
189 if (__n > __b)
190 __builtin_trap();
191 return __orig_pwrite(__f, __s, __n, __o);
192}
193
183#ifdef __cplusplus 194#ifdef __cplusplus
184} 195}
185#endif 196#endif
diff --git a/tests/Makefile b/tests/Makefile
index 1f92b36..1c73c7e 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -63,6 +63,8 @@ TARGETS= \
63 test_ppoll_static \ 63 test_ppoll_static \
64 test_pread_dynamic \ 64 test_pread_dynamic \
65 test_pread_static \ 65 test_pread_static \
66 test_pwrite_dynamic \
67 test_pwrite_static \
66 test_read_dynamic \ 68 test_read_dynamic \
67 test_read_static \ 69 test_read_static \
68 test_readlink_dynamic \ 70 test_readlink_dynamic \
diff --git a/tests/test_pwrite_dynamic.c b/tests/test_pwrite_dynamic.c
new file mode 100644
index 0000000..8e132ed
--- /dev/null
+++ b/tests/test_pwrite_dynamic.c
@@ -0,0 +1,14 @@
1#include "common.h"
2
3#include <unistd.h>
4
5int main(int argc, char** argv) {
6 char buffer[8] = {0};
7
8 CHK_FAIL_START
9 pwrite(0, buffer, argc, 0);
10 CHK_FAIL_END
11
12 puts(buffer);
13 return ret;
14}
diff --git a/tests/test_pwrite_static.c b/tests/test_pwrite_static.c
new file mode 100644
index 0000000..6815fd4
--- /dev/null
+++ b/tests/test_pwrite_static.c
@@ -0,0 +1,14 @@
1#include "common.h"
2
3#include <unistd.h>
4
5int main(int argc, char** argv) {
6 char buffer[12] = {0};
7
8 CHK_FAIL_START
9 pwrite(0, buffer, 14, 0);
10 CHK_FAIL_END
11
12 puts(buffer);
13 return ret;
14}