summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordhartmei <>2002-08-30 07:58:08 +0000
committerdhartmei <>2002-08-30 07:58:08 +0000
commitb317bf50a10ff572164977749e56454ad595e904 (patch)
tree51557b2bbef0a3aa7dfeb77c6f8962aff1d6a489 /src
parent509a5b94525f34212cd48bccbf85481897ecd99f (diff)
downloadopenbsd-b317bf50a10ff572164977749e56454ad595e904.tar.gz
openbsd-b317bf50a10ff572164977749e56454ad595e904.tar.bz2
openbsd-b317bf50a10ff572164977749e56454ad595e904.zip
re-enable function pointer table protection, this time make sure that
malloc.c gets the first mmap() call (since it depends on that, for its sbrk(0) use). ok deraadt@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/atexit.c86
-rw-r--r--src/lib/libc/stdlib/atexit.h56
-rw-r--r--src/lib/libc/stdlib/exit.c21
-rw-r--r--src/regress/lib/libc/Makefile4
4 files changed, 94 insertions, 73 deletions
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c
index 449417b162..92c4884f28 100644
--- a/src/lib/libc/stdlib/atexit.c
+++ b/src/lib/libc/stdlib/atexit.c
@@ -1,46 +1,43 @@
1/*- 1/*
2 * Copyright (c) 1990 The Regents of the University of California. 2 * Copyright (c) 2002 Daniel Hartmeier
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
10 * are met: 7 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 * 8 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 9 * - Redistributions of source code must retain the above copyright
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 10 * notice, this list of conditions and the following disclaimer.
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 11 * - Redistributions in binary form must reproduce the above
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 12 * copyright notice, this list of conditions and the following
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 13 * disclaimer in the documentation and/or other materials provided
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 14 * with the distribution.
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 15 *
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 * SUCH DAMAGE. 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
35 */ 29 */
36 30
37#if defined(LIBC_SCCS) && !defined(lint) 31#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: atexit.c,v 1.4 2002/07/31 18:13:16 dhartmei Exp $"; 32static char *rcsid = "$OpenBSD: atexit.c,v 1.5 2002/08/30 07:58:07 dhartmei Exp $";
39#endif /* LIBC_SCCS and not lint */ 33#endif /* LIBC_SCCS and not lint */
40 34
35#include <sys/types.h>
36#include <sys/mman.h>
41#include <stdlib.h> 37#include <stdlib.h>
42#include "atexit.h" 38#include "atexit.h"
43 39
40int __atexit_invalid = 1;
44struct atexit *__atexit; 41struct atexit *__atexit;
45 42
46/* 43/*
@@ -50,18 +47,37 @@ int
50atexit(fn) 47atexit(fn)
51 void (*fn)(); 48 void (*fn)();
52{ 49{
53 static struct atexit __atexit0; /* one guaranteed table */ 50 register struct atexit *p = __atexit;
54 register struct atexit *p; 51 register int pgsize = getpagesize();
55 52
56 if ((p = __atexit) == NULL) 53 if (pgsize < sizeof(*p))
57 __atexit = p = &__atexit0; 54 return (-1);
58 else if (p->ind >= ATEXIT_SIZE) { 55 if (p != NULL) {
59 if ((p = malloc(sizeof(*p))) == NULL) 56 if (p->ind + 1 >= p->max)
57 p = NULL;
58 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
59 return (-1);
60 }
61 if (p == NULL) {
62 if (__atexit_invalid) {
63 /* malloc.c wants the first mmap() for sbrk()
64 games ('nice hack'), so enforce
65 malloc_init() with a dummy call. */
66 free(malloc(1));
67 __atexit_invalid = 0;
68 }
69 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
70 MAP_ANON | MAP_PRIVATE, -1, 0);
71 if (p == MAP_FAILED)
60 return (-1); 72 return (-1);
61 p->ind = 0; 73 p->ind = 0;
74 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
75 sizeof(p->fns[0]);
62 p->next = __atexit; 76 p->next = __atexit;
63 __atexit = p; 77 __atexit = p;
64 } 78 }
65 p->fns[p->ind++] = fn; 79 p->fns[p->ind++] = fn;
80 if (mprotect(p, pgsize, PROT_READ))
81 return (-1);
66 return (0); 82 return (0);
67} 83}
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h
index e7185e5559..28bf3a7f27 100644
--- a/src/lib/libc/stdlib/atexit.h
+++ b/src/lib/libc/stdlib/atexit.h
@@ -1,45 +1,41 @@
1/*- 1/* $OpenBSD: atexit.h,v 1.5 2002/08/30 07:58:07 dhartmei Exp $ */
2 * Copyright (c) 1990 The Regents of the University of California. 2
3/*
4 * Copyright (c) 2002 Daniel Hartmeier
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
7 * are met: 9 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 * 10 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 11 * - Redistributions of source code must retain the above copyright
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 12 * notice, this list of conditions and the following disclaimer.
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 13 * - Redistributions in binary form must reproduce the above
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 14 * copyright notice, this list of conditions and the following
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 15 * disclaimer in the documentation and/or other materials provided
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 16 * with the distribution.
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 17 *
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * SUCH DAMAGE. 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * 30 *
33 * $OpenBSD: atexit.h,v 1.4 2002/07/31 18:13:16 dhartmei Exp $
34 */ 31 */
35 32
36/* must be at least 32 to guarantee ANSI conformance */
37#define ATEXIT_SIZE 32
38
39struct atexit { 33struct atexit {
40 struct atexit *next; /* next in list */ 34 struct atexit *next; /* next in list */
41 int ind; /* next index in this table */ 35 int ind; /* next index in this table */
42 void (*fns[ATEXIT_SIZE])(); /* the table itself */ 36 int max; /* max entries >= ATEXIT_SIZE */
37 void (*fns[1])(); /* the table itself */
43}; 38};
44 39
40extern int __atexit_invalid;
45extern struct atexit *__atexit; /* points to head of LIFO stack */ 41extern struct atexit *__atexit; /* points to head of LIFO stack */
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c
index 0bf0d3a180..c69639125e 100644
--- a/src/lib/libc/stdlib/exit.c
+++ b/src/lib/libc/stdlib/exit.c
@@ -32,9 +32,11 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: exit.c,v 1.6 2002/07/31 18:13:16 dhartmei Exp $"; 35static char *rcsid = "$OpenBSD: exit.c,v 1.7 2002/08/30 07:58:07 dhartmei Exp $";
36#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
37 37
38#include <sys/types.h>
39#include <sys/mman.h>
38#include <stdlib.h> 40#include <stdlib.h>
39#include <unistd.h> 41#include <unistd.h>
40#include "atexit.h" 42#include "atexit.h"
@@ -58,12 +60,19 @@ void
58exit(status) 60exit(status)
59 int status; 61 int status;
60{ 62{
61 register struct atexit *p; 63 register struct atexit *p, *q;
62 register int n; 64 register int n, pgsize = getpagesize();
63 65
64 for (p = __atexit; p; p = p->next) 66 if (!__atexit_invalid) {
65 for (n = p->ind; --n >= 0;) 67 p = __atexit;
66 (*p->fns[n])(); 68 while (p != NULL) {
69 for (n = p->ind; --n >= 0;)
70 (*p->fns[n])();
71 q = p;
72 p = p->next;
73 munmap(q, pgsize);
74 }
75 }
67 if (__cleanup) 76 if (__cleanup)
68 (*__cleanup)(); 77 (*__cleanup)();
69 _exit(status); 78 _exit(status);
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile
index 70b2d57123..d383b58371 100644
--- a/src/regress/lib/libc/Makefile
+++ b/src/regress/lib/libc/Makefile
@@ -1,7 +1,7 @@
1# $OpenBSD: Makefile,v 1.8 2002/07/05 15:54:30 itojun Exp $ 1# $OpenBSD: Makefile,v 1.9 2002/08/30 07:58:08 dhartmei Exp $
2 2
3SUBDIR+= _setjmp db getaddrinfo regex setjmp sigsetjmp malloc sigreturn popen 3SUBDIR+= _setjmp db getaddrinfo regex setjmp sigsetjmp malloc sigreturn popen
4SUBDIR+= longjmp 4SUBDIR+= longjmp atexit
5.if (${MACHINE_ARCH} != "vax") 5.if (${MACHINE_ARCH} != "vax")
6SUBDIR+= ieeefp 6SUBDIR+= ieeefp
7.endif 7.endif