diff options
| author | tedu <> | 2005-05-24 16:39:05 +0000 |
|---|---|---|
| committer | tedu <> | 2005-05-24 16:39:05 +0000 |
| commit | b8fcb6ec3fa11ff1aef5cfb3719eaf0a3588812a (patch) | |
| tree | 989609b0ec278d4f6f69ff9eea3e3a874c147f5d /src/lib/libc/stdlib | |
| parent | 11614e58638e2a3775eabeb1a5021c5bd2ba40d2 (diff) | |
| download | openbsd-b8fcb6ec3fa11ff1aef5cfb3719eaf0a3588812a.tar.gz openbsd-b8fcb6ec3fa11ff1aef5cfb3719eaf0a3588812a.tar.bz2 openbsd-b8fcb6ec3fa11ff1aef5cfb3719eaf0a3588812a.zip | |
handle sizeof(void *) allocations specially when using malloc guard.
they get a whole page and go right at the end of it. ok deraadt tdeval
Diffstat (limited to 'src/lib/libc/stdlib')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.c | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index ae89f5d72b..9f7ceba080 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| 10 | #if defined(LIBC_SCCS) && !defined(lint) | 10 | #if defined(LIBC_SCCS) && !defined(lint) |
| 11 | static char rcsid[] = "$OpenBSD: malloc.c,v 1.72 2005/03/31 21:24:46 tdeval Exp $"; | 11 | static char rcsid[] = "$OpenBSD: malloc.c,v 1.73 2005/05/24 16:39:05 tedu Exp $"; |
| 12 | #endif /* LIBC_SCCS and not lint */ | 12 | #endif /* LIBC_SCCS and not lint */ |
| 13 | 13 | ||
| 14 | /* | 14 | /* |
| @@ -1061,6 +1061,13 @@ malloc_bytes(size_t size) | |||
| 1061 | } | 1061 | } |
| 1062 | 1062 | ||
| 1063 | /* | 1063 | /* |
| 1064 | * magic so that malloc(sizeof(ptr)) is near the end of the page. | ||
| 1065 | */ | ||
| 1066 | #define PTR_GAP (malloc_pagesize - sizeof(void *)) | ||
| 1067 | #define PTR_SIZE (sizeof(void *)) | ||
| 1068 | #define PTR_ALIGNED(p) (((unsigned long)p & malloc_pagemask) == PTR_GAP) | ||
| 1069 | |||
| 1070 | /* | ||
| 1064 | * Allocate a piece of memory | 1071 | * Allocate a piece of memory |
| 1065 | */ | 1072 | */ |
| 1066 | static void * | 1073 | static void * |
| @@ -1075,6 +1082,11 @@ imalloc(size_t size) | |||
| 1075 | if (suicide) | 1082 | if (suicide) |
| 1076 | abort(); | 1083 | abort(); |
| 1077 | 1084 | ||
| 1085 | if (malloc_guard && size == PTR_SIZE) { | ||
| 1086 | ptralloc = 1; | ||
| 1087 | size = malloc_pagesize; | ||
| 1088 | } | ||
| 1089 | |||
| 1078 | if ((size + malloc_pagesize) < size) { /* Check for overflow */ | 1090 | if ((size + malloc_pagesize) < size) { /* Check for overflow */ |
| 1079 | result = NULL; | 1091 | result = NULL; |
| 1080 | errno = ENOMEM; | 1092 | errno = ENOMEM; |
| @@ -1090,6 +1102,8 @@ imalloc(size_t size) | |||
| 1090 | if (malloc_zero && result != NULL) | 1102 | if (malloc_zero && result != NULL) |
| 1091 | memset(result, 0, size); | 1103 | memset(result, 0, size); |
| 1092 | 1104 | ||
| 1105 | if (result && ptralloc) | ||
| 1106 | return ((char *)result + PTR_GAP); | ||
| 1093 | return (result); | 1107 | return (result); |
| 1094 | } | 1108 | } |
| 1095 | 1109 | ||
| @@ -1114,6 +1128,19 @@ irealloc(void *ptr, size_t size) | |||
| 1114 | return (NULL); | 1128 | return (NULL); |
| 1115 | } | 1129 | } |
| 1116 | 1130 | ||
| 1131 | if (malloc_guard && PTR_ALIGNED(ptr)) { | ||
| 1132 | if (size <= PTR_SIZE) | ||
| 1133 | return (ptr); | ||
| 1134 | else { | ||
| 1135 | p = imalloc(size); | ||
| 1136 | if (p) | ||
| 1137 | memcpy(p, ptr, PTR_SIZE); | ||
| 1138 | ifree(ptr); | ||
| 1139 | return (p); | ||
| 1140 | } | ||
| 1141 | } | ||
| 1142 | |||
| 1143 | |||
| 1117 | index = ptr2index(ptr); | 1144 | index = ptr2index(ptr); |
| 1118 | 1145 | ||
| 1119 | if (index < malloc_pageshift) { | 1146 | if (index < malloc_pageshift) { |
| @@ -1575,6 +1602,9 @@ ifree(void *ptr) | |||
| 1575 | if (suicide) | 1602 | if (suicide) |
| 1576 | return; | 1603 | return; |
| 1577 | 1604 | ||
| 1605 | if (malloc_guard && PTR_ALIGNED(ptr)) | ||
| 1606 | ptr = (char *)ptr - PTR_GAP; | ||
| 1607 | |||
| 1578 | index = ptr2index(ptr); | 1608 | index = ptr2index(ptr); |
| 1579 | 1609 | ||
| 1580 | if (index < malloc_pageshift) { | 1610 | if (index < malloc_pageshift) { |
