summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc
diff options
context:
space:
mode:
authorotto <>2023-06-04 06:58:33 +0000
committerotto <>2023-06-04 06:58:33 +0000
commit359dbb8e7485c66a6707470ce9d5af0ddbbff9f8 (patch)
tree658d7037f67b98a6598726569c6933ca718927f6 /src/regress/lib/libc
parent2cbf6fb83a2b98d0b6ffbb161bcc85c81e5f2b59 (diff)
downloadopenbsd-359dbb8e7485c66a6707470ce9d5af0ddbbff9f8.tar.gz
openbsd-359dbb8e7485c66a6707470ce9d5af0ddbbff9f8.tar.bz2
openbsd-359dbb8e7485c66a6707470ce9d5af0ddbbff9f8.zip
More thorough write-afetr-free checks.
On free, chunks (the pieces of a pages used for smaller allocations) are junked and then validated after they leave the delayed free list. So after free, a chunk always contains junk bytes. This means that if we start with the right contents for a new page of chunks, we can *validate* instead of *write* junk bytes when (re)-using a chunk. With this, we can detect write-after-free when a chunk is recycled, not justy when a chunk is in the delayed free list. We do a little bit more work on initial allocation of a page of chunks and when re-using (as we validate now even on junk level 1). Also: some extra consistency checks for recallocaray(3) and fixes in error messages to make them more consistent, with man page bits. Plus regress additions.
Diffstat (limited to 'src/regress/lib/libc')
-rw-r--r--src/regress/lib/libc/malloc/malloc_errs/malloc_errs.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/regress/lib/libc/malloc/malloc_errs/malloc_errs.c b/src/regress/lib/libc/malloc/malloc_errs/malloc_errs.c
index e0efb6ebf3..6040590a65 100644
--- a/src/regress/lib/libc/malloc/malloc_errs/malloc_errs.c
+++ b/src/regress/lib/libc/malloc/malloc_errs/malloc_errs.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc_errs.c,v 1.2 2023/05/09 19:07:37 otto Exp $ */ 1/* $OpenBSD: malloc_errs.c,v 1.3 2023/06/04 06:58:33 otto Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2023 Otto Moerbeek <otto@drijf.net>
4 * 4 *
@@ -138,9 +138,7 @@ t8(void)
138void 138void
139t9(void) 139t9(void)
140{ 140{
141 char *p; 141 char *p = malloc(100);
142
143 p = malloc(100);
144 p[100] = 0; 142 p[100] = 0;
145 free(p); 143 free(p);
146} 144}
@@ -191,7 +189,6 @@ t15(void)
191void 189void
192t16(void) 190t16(void)
193{ 191{
194 abort(); /* not yet */
195 char *p = recallocarray(NULL, 0, 16, 1); 192 char *p = recallocarray(NULL, 0, 16, 1);
196 char *q = recallocarray(p, 2, 3, 16); 193 char *q = recallocarray(p, 2, 3, 16);
197} 194}
@@ -208,11 +205,27 @@ t17(void)
208void 205void
209t18(void) 206t18(void)
210{ 207{
211 abort(); /* not yet */
212 char *p = recallocarray(NULL, 0, 1, getpagesize()); 208 char *p = recallocarray(NULL, 0, 1, getpagesize());
213 char *q = recallocarray(p, 2, 3, getpagesize()); 209 char *q = recallocarray(p, 2, 3, getpagesize());
214} 210}
215 211
212/* recallocarray with wrong size, pages */
213void
214t19(void)
215{
216 char *p = recallocarray(NULL, 0, 1, 10 * getpagesize());
217 char *q = recallocarray(p, 1, 2, 4 * getpagesize());
218}
219
220/* canary check pages */
221void
222t20(void)
223{
224 char *p = malloc(2*getpagesize() - 100);
225 p[2*getpagesize() - 100] = 0;
226 free(p);
227}
228
216struct test { 229struct test {
217 void (*test)(void); 230 void (*test)(void);
218 const char *flags; 231 const char *flags;
@@ -238,6 +251,8 @@ struct test tests[] = {
238 { t16, "" }, 251 { t16, "" },
239 { t17, "C" }, 252 { t17, "C" },
240 { t18, "" }, 253 { t18, "" },
254 { t19, "" },
255 { t20, "C" },
241}; 256};
242 257
243int main(int argc, char *argv[]) 258int main(int argc, char *argv[])