summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
authorPeter S. Mazinger <ps.m@gmx.net>2005-09-23 19:52:33 +0000
committerPeter S. Mazinger <ps.m@gmx.net>2005-09-23 19:52:33 +0000
commit593d5a1557d4921624575fe2b4d4904d30414bb6 (patch)
tree9a9b64592f52ff6dc3866e77c3fd936a9789fc6a /libc
parentd886583eb07ed28c1ef0c1acb6417e1a69c0b565 (diff)
New ssp code using syscalls where possible.
Moved guard_setup to __uClibc_main.c, the only place where it is called. Removed SIGKILL option, not usable with sigaction.
Diffstat (limited to 'libc')
-rw-r--r--libc/misc/internals/__uClibc_main.c69
-rw-r--r--libc/sysdeps/linux/common/ssp.c137
2 files changed, 97 insertions, 109 deletions
diff --git a/libc/misc/internals/__uClibc_main.c b/libc/misc/internals/__uClibc_main.c
index 167b77a33..c88649def 100644
--- a/libc/misc/internals/__uClibc_main.c
+++ b/libc/misc/internals/__uClibc_main.c
@@ -26,9 +26,10 @@
#include <sys/stat.h>
#include <sys/sysmacros.h>
#ifdef __UCLIBC_HAS_SSP__
-extern void __guard_setup(void);
-#endif
+#include <ssp-internal.h>
+unsigned long __guard = 0UL;
+#endif
/*
* Prototypes.
@@ -105,6 +106,62 @@ static int __check_suid(void)
return 1;
}
+#ifdef __UCLIBC_HAS_SSP__
+static __always_inline void __guard_setup(void)
+{
+ if (__guard != 0UL)
+ return;
+
+#ifndef __SSP_QUICK_CANARY__
+
+ size_t size;
+
+# ifdef __SSP_USE_ERANDOM__
+ {
+ int mib[3];
+ /* Random is another depth in Linux, hence an array of 3. */
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_RANDOM;
+ mib[2] = RANDOM_ERANDOM;
+
+ size = sizeof(unsigned long);
+ if (SYSCTL(mib, 3, &__guard, &size, NULL, 0) != (-1))
+ if (__guard != 0UL)
+ return;
+ }
+# endif /* ifdef __SSP_USE_ERANDOM__ */
+ {
+ int fd;
+
+# ifdef __SSP_USE_ERANDOM__
+ /*
+ * Attempt to open kernel pseudo random device if one exists before
+ * opening urandom to avoid system entropy depletion.
+ */
+ if ((fd = OPEN("/dev/erandom", O_RDONLY)) == (-1))
+# endif
+ fd = OPEN("/dev/urandom", O_RDONLY);
+ if (fd != (-1)) {
+ size = READ(fd, (char *) &__guard, sizeof(__guard));
+ CLOSE(fd);
+ if (size == sizeof(__guard))
+ return;
+ }
+ }
+#endif /* ifndef __SSP_QUICK_CANARY__ */
+
+ /* Start with the "terminator canary". */
+ __guard = 0xFF0A0D00UL;
+
+ /* Everything failed? Or we are using a weakened model of the
+ * terminator canary */
+ {
+ struct timeval tv;
+ GETTIMEOFDAY(&tv, NULL);
+ __guard ^= tv.tv_usec ^ tv.tv_sec;
+ }
+}
+#endif /* __UCLIBC_HAS_SSP__ */
/* __uClibc_init completely initialize uClibc so it is ready to use.
*
@@ -140,6 +197,10 @@ void __uClibc_init(void)
__pthread_initialize_minimal();
#endif
+#ifdef __UCLIBC_HAS_SSP__
+ __guard_setup ();
+#endif
+
#ifdef __UCLIBC_HAS_LOCALE__
/* Initialize the global locale structure. */
if (likely(_locale_init!=NULL))
@@ -235,10 +296,6 @@ __uClibc_main(int (*main)(int, char **, char **), int argc,
}
#endif
-#ifdef __UCLIBC_HAS_SSP__
- __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/ssp.c b/libc/sysdeps/linux/common/ssp.c
index 31833cbb9..8d8d89653 100644
--- a/libc/sysdeps/linux/common/ssp.c
+++ b/libc/sysdeps/linux/common/ssp.c
@@ -20,126 +20,57 @@
# include <config.h>
#endif
-#ifdef __SSP__
-# error ssp.c has to be built w/ -fno-stack-protector
-#endif
-
-#include <stdio.h>
#include <string.h>
-#include <fcntl.h>
#include <unistd.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/un.h>
#include <sys/syslog.h>
-#include <sys/time.h>
-#ifdef __SSP_USE_ERANDOM__
-# include <sys/sysctl.h>
-#endif
-
-#ifdef __PROPOLICE_BLOCK_SEGV__
-# define SSP_SIGTYPE SIGSEGV
-#elif __PROPOLICE_BLOCK_KILL__
-# define SSP_SIGTYPE SIGKILL
-#else
-# define SSP_SIGTYPE SIGABRT
-#endif
-
-unsigned long __guard = 0UL;
-/* Use of __* functions from the rest of glibc here avoids
- * initialisation problems for executables preloaded with
- * libraries that overload the associated standard library
- * functions.
- */
-#ifdef __UCLIBC__
-extern int __libc_open(__const char *file, int flags, ...);
-extern ssize_t __libc_read(int fd, void *buf, size_t count);
-extern int __libc_close(int fd);
-#else
-# define __libc_open(file, flags) __open(file, flags)
-# define __libc_read(fd, buf, count) __read(fd, buf, count)
-# define __libc_close(fd) __close(fd)
-#endif
+#include <ssp-internal.h>
-void __guard_setup(void) __attribute__ ((constructor));
-void __guard_setup(void)
+static __always_inline void block_signals(void)
{
- size_t size;
-
- if (__guard != 0UL)
- return;
+ struct sigaction sa;
+ sigset_t mask;
- /* Start with the "terminator canary". */
- __guard = 0xFF0A0D00UL;
+ sigfillset(&mask);
-#ifndef __SSP_QUICK_CANARY__
-# ifdef __SSP_USE_ERANDOM__
- {
- int mib[3];
- /* Random is another depth in Linux, hence an array of 3. */
- mib[0] = CTL_KERN;
- mib[1] = KERN_RANDOM;
- mib[2] = RANDOM_ERANDOM;
+ sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
+ SIGPROCMASK(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
- size = sizeof(unsigned long);
- if (__sysctl(mib, 3, &__guard, &size, NULL, 0) != (-1))
- if (__guard != 0UL)
- return;
- }
-# endif /* ifdef __SSP_USE_ERANDOM__ */
- /*
- * Attempt to open kernel pseudo random device if one exists before
- * opening urandom to avoid system entropy depletion.
- */
- {
- int fd;
+ /* Make the default handler associated with the signal handler */
+ memset(&sa, 0, sizeof(struct sigaction));
+ sigfillset(&sa.sa_mask); /* Block all signals */
+ sa.sa_flags = 0;
+ sa.sa_handler = SIG_DFL;
+ SIGACTION(SSP_SIGTYPE, &sa, NULL);
+}
-# ifdef __SSP_USE_ERANDOM__
- if ((fd = __libc_open("/dev/erandom", O_RDONLY)) == (-1))
-# endif
- fd = __libc_open("/dev/urandom", O_RDONLY);
- if (fd != (-1)) {
- size = __libc_read(fd, (char *) &__guard, sizeof(__guard));
- __libc_close(fd);
- if (size == sizeof(__guard))
- return;
- }
- }
-#endif /* ifndef __SSP_QUICK_CANARY__ */
+static __always_inline void ssp_write(int fd, const char *msg1, const char *msg2, const char *msg3)
+{
+ WRITE(fd, msg1, strlen(msg1));
+ WRITE(fd, msg2, strlen(msg2));
+ WRITE(fd, msg3, strlen(msg3));
+ WRITE(fd, "()\n", 3);
+ openlog("ssp", LOG_CONS | LOG_PID, LOG_USER);
+ syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
+ closelog();
+}
- /* Everything failed? Or we are using a weakened model of the
- * terminator canary */
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- __guard ^= tv.tv_usec ^ tv.tv_sec;
- }
+static __always_inline void terminate(void)
+{
+ (void) KILL(GETPID(), SSP_SIGTYPE);
+ EXIT(127);
}
-void __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
-void __stack_smash_handler(char func[], int damaged)
+void __attribute__ ((noreturn)) __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
+void __attribute__ ((noreturn)) __stack_smash_handler(char func[], int damaged)
{
extern char *__progname;
const char message[] = ": stack smashing attack in function ";
- struct sigaction sa;
- sigset_t mask;
- sigfillset(&mask);
-
- sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
- sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
+ block_signals();
- /* Print error message to stderr and syslog */
- fprintf(stderr, "%s%s%s()\n", __progname, message, func);
- syslog(LOG_INFO, "%s%s%s()", __progname, message, func);
+ ssp_write(STDERR_FILENO, __progname, message, func);
- /* Make the default handler associated with the signal handler */
- memset(&sa, 0, sizeof(struct sigaction));
- sigfillset(&sa.sa_mask); /* Block all signals */
- sa.sa_flags = 0;
- sa.sa_handler = SIG_DFL;
- sigaction(SSP_SIGTYPE, &sa, NULL);
- (void) kill(getpid(), SSP_SIGTYPE);
- _exit(127);
+ while(1)
+ terminate();
}