From fb84603f8c45fdafdaa750490785fc1b15541386 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Fri, 2 Jan 2004 23:34:13 +0000 Subject: Peter S. Mazinger writes: Hello Erik! I have made some cosmetical changes to the files, removed the added SCRT=-fPIC option from building the crt0.S file (but it is a requirement to build them with -fPIC), and changed some comments. I have left the ldso.c patch with PIE_SUPPORT ifdefs, but consider applying it w/o them (see some earlier comment from PaX Team on this issue, as it is considered a bug). To have it work correctly, you'll also need removing COMPLETELY_PIC. One thing is missing: PIE_SUPPORT should be usable only for i386 (for now). Also added the support for propolice protection (that works for me and catches memcpy/strcpy attacks (but needs a special gcc version). Thanks, Peter --- libc/misc/internals/__uClibc_main.c | 7 +++ libc/sysdeps/linux/common/Makefile | 3 ++ libc/sysdeps/linux/common/ssp.c | 97 +++++++++++++++++++++++++++++++++++++ libc/sysdeps/linux/i386/Makefile | 10 ++++ libc/sysdeps/linux/i386/crt0.S | 25 +++++++++- 5 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 libc/sysdeps/linux/common/ssp.c (limited to 'libc') diff --git a/libc/misc/internals/__uClibc_main.c b/libc/misc/internals/__uClibc_main.c index 5f85b5928..842e99500 100644 --- a/libc/misc/internals/__uClibc_main.c +++ b/libc/misc/internals/__uClibc_main.c @@ -15,6 +15,9 @@ #include #include #include +#ifdef __UCLIBC_PROPOLICE__ +extern void __guard_setup(void); +#endif /* @@ -139,6 +142,10 @@ __uClibc_start_main(int argc, char **argv, char **envp, } #endif +#ifdef __UCLIBC_PROPOLICE__ + __guard_setup (); +#endif + /* Note: It is possible that any initialization done above could * have resulted in errno being set nonzero, so set it to 0 before * we call main. diff --git a/libc/sysdeps/linux/common/Makefile b/libc/sysdeps/linux/common/Makefile index cfd95300f..5b5034095 100644 --- a/libc/sysdeps/linux/common/Makefile +++ b/libc/sysdeps/linux/common/Makefile @@ -33,6 +33,9 @@ endif ifeq ($(strip $(UCLIBC_PROFILING)),y) CSRC+=gmon.c endif +ifeq ($(strip $(UCLIBC_PROPOLICE)),y) +CSRC+=ssp.c +endif COBJS=$(patsubst %.c,%.o, $(CSRC)) MSRC=syscalls.c diff --git a/libc/sysdeps/linux/common/ssp.c b/libc/sysdeps/linux/common/ssp.c new file mode 100644 index 000000000..135892c37 --- /dev/null +++ b/libc/sysdeps/linux/common/ssp.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +#ifdef _POSIX_SOURCE +#include +#endif + +#if defined(HAVE_SYSLOG) +#include +#include +#include + +#include +#ifndef _PATH_LOG +#define _PATH_LOG "/dev/log" +#endif +#endif + +long __guard[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + +void __guard_setup (void) +{ + int fd; + if (__guard[0]!=0) return; + fd = open ("/dev/urandom", 0); + if (fd != -1) { + ssize_t size = read (fd, (char*)&__guard, sizeof(__guard)); + close (fd) ; + if (size == sizeof(__guard)) return; + } + /* If a random generator can't be used, the protector switches the guard + to the "terminator canary" */ + ((char*)__guard)[0] = 0; ((char*)__guard)[1] = 0; + ((char*)__guard)[2] = '\n'; ((char*)__guard)[3] = 255; +} + +void __stack_smash_handler (char func[], int damaged) +{ +#if defined (__GNU_LIBRARY__) + extern char * __progname; +#endif + const char message[] = ": stack smashing attack in function "; + int bufsz = 512, len; + char buf[bufsz]; +#if defined(HAVE_SYSLOG) + int LogFile; + struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ +#endif +#ifdef _POSIX_SOURCE + { + sigset_t mask; + sigfillset(&mask); + sigdelset(&mask, SIGABRT); /* Block all signal handlers */ + sigprocmask(SIG_BLOCK, &mask, NULL); /* except SIGABRT */ + } +#endif + + strcpy(buf, "<2>"); len=3; /* send LOG_CRIT */ +#if defined (__GNU_LIBRARY__) + strncat(buf, __progname, bufsz-len-1); len = strlen(buf); +#endif + if (bufsz>len) {strncat(buf, message, bufsz-len-1); len = strlen(buf);} + if (bufsz>len) {strncat(buf, func, bufsz-len-1); len = strlen(buf);} + /* print error message */ + write (STDERR_FILENO, buf+3, len-3); +#if defined(HAVE_SYSLOG) + if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) != -1) { + + /* + * Send "found" message to the "/dev/log" path + */ + SyslogAddr.sun_family = AF_UNIX; + (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, + sizeof(SyslogAddr.sun_path) - 1); + SyslogAddr.sun_path[sizeof(SyslogAddr.sun_path) - 1] = '\0'; + sendto(LogFile, buf, len, 0, (struct sockaddr *)&SyslogAddr, + sizeof(SyslogAddr)); + } +#endif + +#ifdef _POSIX_SOURCE + { /* Make sure the default handler is associated with SIGABRT */ + struct sigaction sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sigfillset(&sa.sa_mask); /* Block all signals */ + sa.sa_flags = 0; + sa.sa_handler = SIG_DFL; + sigaction(SIGABRT, &sa, NULL); + (void)kill(getpid(), SIGABRT); + } +#endif + _exit(127); +} + diff --git a/libc/sysdeps/linux/i386/Makefile b/libc/sysdeps/linux/i386/Makefile index 9ba60f59e..c7f88b681 100644 --- a/libc/sysdeps/linux/i386/Makefile +++ b/libc/sysdeps/linux/i386/Makefile @@ -22,6 +22,9 @@ ASFLAGS=$(CFLAGS) CRT0_SRC = crt0.S CRT0_OBJ = crt0.o crt1.o gcrt1.o +ifeq ($(strip $(UCLIBC_PIE_SUPPORT)),y) +CRT0_OBJ += Scrt0.o Scrt1.o +endif CRT0_DEPS=gmon-start.S CTOR_TARGETS=$(TOPDIR)lib/crti.o $(TOPDIR)lib/crtn.o @@ -44,6 +47,13 @@ $(LIBC): ar-target ar-target: $(OBJS) $(CRT0_OBJ) $(CTOR_TARGETS) $(AR) $(ARFLAGS) $(LIBC) $(OBJS) cp $(CRT0_OBJ) $(TOPDIR)lib/ +ifeq ($(strip $(UCLIBC_PIE_SUPPORT)),y) +ifeq ($(strip $(UCLIBC_CTOR_DTOR)),y) + $(RM) $(TOPDIR)lib/Scrt0.o +else + mv $(TOPDIR)lib/Scrt0.o $(TOPDIR)lib/Scrt1.o +endif +endif $(CRT0_OBJ): $(CRT0_SRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o diff --git a/libc/sysdeps/linux/i386/crt0.S b/libc/sysdeps/linux/i386/crt0.S index a488d6508..e7df6992b 100644 --- a/libc/sysdeps/linux/i386/crt0.S +++ b/libc/sysdeps/linux/i386/crt0.S @@ -18,6 +18,7 @@ Cambridge, MA 02139, USA. */ /* Based on the code from GNU libc, but hacked up by John Beppu and Erik Andersen */ +/* adapted by PaX Team for ET_DYN/PIE binaries */ /* When we enter this piece of code, the program stack looks like this: @@ -37,7 +38,7 @@ Cambridge, MA 02139, USA. */ .global _start .type _start,%function -#if defined L_crt0 || ! defined __UCLIBC_CTOR_DTOR__ +#if defined L_crt0 || defined L_Scrt0 || ! defined __UCLIBC_CTOR_DTOR__ .type __uClibc_main,%function #else .weak _init @@ -74,10 +75,22 @@ _start: pushl %ebp /* callers %ebp (frame pointer) */ movl %esp,%ebp /* mark callers stack frame as invalid */ -#if (defined L_crt1 || defined L_gcrt1 ) && defined __UCLIBC_CTOR_DTOR__ +#if defined L_Scrt0 || defined L_Scrt1 + call .L0 +.L0: + pop %edx + addl $_GLOBAL_OFFSET_TABLE_+[.-.L0],%edx +#endif + +#if (defined L_crt1 || defined L_Scrt1 || defined L_gcrt1 ) && defined __UCLIBC_CTOR_DTOR__ /* Push .init and .fini arguments to __uClibc_start_main() on the stack */ +#ifdef L_Scrt1 + pushl _fini@GOT(%edx) + pushl _init@GOT(%edx) +#else pushl $_fini pushl $_init +#endif /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */ pushl %eax /* Environment pointer */ @@ -85,14 +98,22 @@ _start: pushl %ecx /* And the argument count */ /* Ok, now run uClibc's main() -- shouldn't return */ +#ifdef L_Scrt1 + call *__uClibc_start_main@GOT(%edx) +#else call __uClibc_start_main +#endif #else /* Push envp, argc, and argc arguments to __uClibc_start_main() on the stack */ pushl %eax /* Environment pointer */ pushl %ebx /* Argument pointer */ pushl %ecx /* And the argument count */ +#ifdef L_Scrt0 + call *__uClibc_main@GOT(%edx) +#else call __uClibc_main +#endif #endif /* Crash if somehow `exit' returns anyways. */ -- cgit v1.2.3