|
|
| 1 |
/* Copyright (C) 2005, 2007 Free Software Foundation, Inc. |
1 |
/* Copyright (C) 2005 Free Software Foundation, Inc. |
| 2 |
This file is part of the GNU C Library. |
2 |
This file is part of the GNU C Library. |
| 3 |
|
3 |
|
| 4 |
The GNU C Library is free software; you can redistribute it and/or |
4 |
The GNU C Library is free software; you can redistribute it and/or |
|
|
| 16 |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
16 |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 17 |
02111-1307 USA. */ |
17 |
02111-1307 USA. */ |
| 18 |
|
18 |
|
| 19 |
#include <stdio.h> |
19 |
/* Copyright (C) 2006-2007 Gentoo Foundation Inc. |
|
|
20 |
* License terms as above. |
| 21 |
* |
| 22 |
* Hardened Gentoo SSP handler |
| 23 |
* |
| 24 |
* An SSP failure handler that does not use functions from the rest of |
| 25 |
* glibc; it uses the INTERNAL_SYSCALL methods directly. This ensures |
| 26 |
* no possibility of recursion into the handler. |
| 27 |
* |
| 28 |
* Direct all bug reports to http://bugs.gentoo.org/ |
| 29 |
* |
| 30 |
* Re-written from the glibc-2.3 Hardened Gentoo SSP handler |
| 31 |
* by Kevin F. Quinn - <kevquinn[@]gentoo.org> |
| 32 |
* |
| 33 |
* The following people contributed to the glibc-2.3 Hardened |
| 34 |
* Gentoo SSP handler, from which this implementation draws much: |
| 35 |
* |
| 36 |
* Ned Ludd - <solar[@]gentoo.org> |
| 37 |
* Alexander Gabert - <pappy[@]gentoo.org> |
| 38 |
* The PaX Team - <pageexec[@]freemail.hu> |
| 39 |
* Peter S. Mazinger - <ps.m[@]gmx.net> |
| 40 |
* Yoann Vandoorselaere - <yoann[@]prelude-ids.org> |
| 41 |
* Robert Connolly - <robert[@]linuxfromscratch.org> |
| 42 |
* Cory Visi <cory[@]visi.name> |
| 43 |
* Mike Frysinger <vapier[@]gentoo.org> |
| 44 |
*/ |
| 45 |
|
| 46 |
#include <errno.h> |
| 20 |
#include <stdlib.h> |
47 |
#include <stdlib.h> |
|
|
48 |
#include <unistd.h> |
| 49 |
#include <signal.h> |
| 50 |
|
| 51 |
#include <sys/types.h> |
| 52 |
|
| 53 |
#include <sysdep-cancel.h> |
| 54 |
#include <sys/syscall.h> |
| 55 |
#include <bp-checks.h> |
| 56 |
|
| 57 |
#include <kernel-features.h> |
| 58 |
|
| 59 |
#include <alloca.h> |
| 60 |
/* from sysdeps */ |
| 61 |
#include <socketcall.h> |
| 62 |
/* for the stuff in bits/socket.h */ |
| 63 |
#include <sys/socket.h> |
| 64 |
#include <sys/un.h> |
| 65 |
|
| 66 |
|
| 67 |
/* Sanity check on SYSCALL macro names - force compilation |
| 68 |
* failure if the names used here do not exist |
| 69 |
*/ |
| 70 |
#if !defined __NR_socketcall && !defined __NR_socket |
| 71 |
# error Cannot do syscall socket or socketcall |
| 72 |
#endif |
| 73 |
#if !defined __NR_socketcall && !defined __NR_connect |
| 74 |
# error Cannot do syscall connect or socketcall |
| 75 |
#endif |
| 76 |
#ifndef __NR_write |
| 77 |
# error Cannot do syscall write |
| 78 |
#endif |
| 79 |
#ifndef __NR_close |
| 80 |
# error Cannot do syscall close |
| 81 |
#endif |
| 82 |
#ifndef __NR_getpid |
| 83 |
# error Cannot do syscall getpid |
| 84 |
#endif |
| 85 |
#ifndef __NR_kill |
| 86 |
# error Cannot do syscall kill |
| 87 |
#endif |
| 88 |
#ifndef __NR_exit |
| 89 |
# error Cannot do syscall exit |
| 90 |
#endif |
| 91 |
#ifdef SSP_SMASH_DUMPS_CORE |
| 92 |
# define ENABLE_SSP_SMASH_DUMPS_CORE 1 |
| 93 |
# if !defined _KERNEL_NSIG && !defined _NSIG |
| 94 |
# error No _NSIG or _KERNEL_NSIG for rt_sigaction |
| 95 |
# endif |
| 96 |
# if !defined __NR_sigaction && !defined __NR_rt_sigaction |
| 97 |
# error Cannot do syscall sigaction or rt_sigaction |
| 98 |
# endif |
| 99 |
/* Although rt_sigaction expects sizeof(sigset_t) - it expects the size |
| 100 |
* of the _kernel_ sigset_t which is not the same as the user sigset_t. |
| 101 |
* Most arches have this as _NSIG bits - mips has _KERNEL_NSIG bits for |
| 102 |
* some reason. |
| 103 |
*/ |
| 104 |
# ifdef _KERNEL_NSIG |
| 105 |
# define _SSP_NSIG _KERNEL_NSIG |
| 106 |
# else |
| 107 |
# define _SSP_NSIG _NSIG |
| 108 |
# endif |
| 109 |
#else |
| 110 |
# define _SSP_NSIG 0 |
| 111 |
# define ENABLE_SSP_SMASH_DUMPS_CORE 0 |
| 112 |
#endif |
| 113 |
|
| 114 |
/* Define DO_SIGACTION - default to newer rt signal interface but |
| 115 |
* fallback to old as needed. |
| 116 |
*/ |
| 117 |
#ifdef __NR_rt_sigaction |
| 118 |
# define DO_SIGACTION(signum, act, oldact) \ |
| 119 |
INLINE_SYSCALL(rt_sigaction, 4, signum, act, oldact, _SSP_NSIG/8) |
| 120 |
#else |
| 121 |
# define DO_SIGACTION(signum, act, oldact) \ |
| 122 |
INLINE_SYSCALL(sigaction, 3, signum, act, oldact) |
| 123 |
#endif |
| 124 |
|
| 125 |
/* Define DO_SOCKET/DO_CONNECT functions to deal with socketcall vs socket/connect */ |
| 126 |
#if defined(__NR_socket) && defined(__NR_connect) |
| 127 |
# define USE_OLD_SOCKETCALL 0 |
| 128 |
#else |
| 129 |
# define USE_OLD_SOCKETCALL 1 |
| 130 |
#endif |
| 131 |
/* stub out the __NR_'s so we can let gcc optimize away dead code */ |
| 132 |
#ifndef __NR_socketcall |
| 133 |
# define __NR_socketcall 0 |
| 134 |
#endif |
| 135 |
#ifndef __NR_socket |
| 136 |
# define __NR_socket 0 |
| 137 |
#endif |
| 138 |
#ifndef __NR_connect |
| 139 |
# define __NR_connect 0 |
| 140 |
#endif |
| 141 |
#define DO_SOCKET(result, domain, type, protocol) \ |
| 142 |
do { \ |
| 143 |
if (USE_OLD_SOCKETCALL) { \ |
| 144 |
socketargs[0] = domain; \ |
| 145 |
socketargs[1] = type; \ |
| 146 |
socketargs[2] = protocol; \ |
| 147 |
socketargs[3] = 0; \ |
| 148 |
result = INLINE_SYSCALL(socketcall, 2, SOCKOP_socket, socketargs); \ |
| 149 |
} else \ |
| 150 |
result = INLINE_SYSCALL(socket, 3, domain, type, protocol); \ |
| 151 |
} while (0) |
| 152 |
#define DO_CONNECT(result, sockfd, serv_addr, addrlen) \ |
| 153 |
do { \ |
| 154 |
if (USE_OLD_SOCKETCALL) { \ |
| 155 |
socketargs[0] = sockfd; \ |
| 156 |
socketargs[1] = (unsigned long int)serv_addr; \ |
| 157 |
socketargs[2] = addrlen; \ |
| 158 |
socketargs[3] = 0; \ |
| 159 |
result = INLINE_SYSCALL(socketcall, 2, SOCKOP_connect, socketargs); \ |
| 160 |
} else \ |
| 161 |
result = INLINE_SYSCALL(connect, 3, sockfd, serv_addr, addrlen); \ |
| 162 |
} while (0) |
| 163 |
|
| 164 |
#ifndef _PATH_LOG |
| 165 |
# define _PATH_LOG "/dev/log" |
| 166 |
#endif |
| 167 |
|
| 168 |
static const char path_log[] = _PATH_LOG; |
| 169 |
|
| 170 |
/* For building glibc with SSP switched on, define __progname to a |
| 171 |
* constant if building for the run-time loader, to avoid pulling |
| 172 |
* in more of libc.so into ld.so |
| 173 |
*/ |
| 174 |
#ifdef IS_IN_rtld |
| 175 |
static char *__progname = "<rtld>"; |
| 176 |
#else |
| 177 |
extern char *__progname; |
| 178 |
#endif |
| 179 |
|
| 21 |
|
180 |
|
|
|
181 |
/* Common handler code, used by stack_chk_fail and __stack_smash_handler |
| 182 |
* Inlined to ensure no self-references to the handler within itself. |
| 183 |
* Data static to avoid putting more than necessary on the stack, |
| 184 |
* to aid core debugging. |
| 185 |
* The copy in rtld must be hidden to ensure that it gets no relocations |
| 186 |
* and thus does not crash if called during libc startup. |
| 187 |
*/ |
| 188 |
__attribute__ ((__noreturn__ , __always_inline__)) |
| 189 |
#ifdef IS_IN_rtld |
| 190 |
attribute_hidden |
| 191 |
#endif |
| 192 |
static inline void |
| 193 |
__hardened_gentoo_stack_chk_fail(char func[], int damaged) |
| 194 |
{ |
| 195 |
#define MESSAGE_BUFSIZ 256 |
| 196 |
static pid_t pid; |
| 197 |
static int plen, i; |
| 198 |
static char message[MESSAGE_BUFSIZ]; |
| 199 |
static const char msg_ssa[] = ": stack smashing attack"; |
| 200 |
static const char msg_inf[] = " in function "; |
| 201 |
static const char msg_ssd[] = "*** stack smashing detected ***: "; |
| 202 |
static const char msg_terminated[] = " - terminated\n"; |
| 203 |
static const char msg_unknown[] = "<unknown>"; |
| 204 |
static int log_socket, connect_result; |
| 205 |
static struct sockaddr_un sock; |
| 206 |
static unsigned long int socketargs[4]; |
| 207 |
|
| 208 |
/* Build socket address |
| 209 |
*/ |
| 210 |
sock.sun_family = AF_UNIX; |
| 211 |
i = 0; |
| 212 |
while ((path_log[i] != '\0') && (i<(sizeof(sock.sun_path)-1))) { |
| 213 |
sock.sun_path[i] = path_log[i]; |
| 214 |
i++; |
| 215 |
} |
| 216 |
sock.sun_path[i] = '\0'; |
| 217 |
|
| 218 |
/* Try SOCK_DGRAM connection to syslog */ |
| 219 |
connect_result = -1; |
| 220 |
DO_SOCKET(log_socket, AF_UNIX, SOCK_DGRAM, 0); |
| 221 |
if (log_socket != -1) |
| 222 |
DO_CONNECT(connect_result, log_socket, &sock, sizeof(sock)); |
| 223 |
if (connect_result == -1) { |
| 224 |
if (log_socket != -1) |
| 225 |
INLINE_SYSCALL(close, 1, log_socket); |
| 226 |
/* Try SOCK_STREAM connection to syslog */ |
| 227 |
DO_SOCKET(log_socket, AF_UNIX, SOCK_STREAM, 0); |
| 228 |
if (log_socket != -1) |
| 229 |
DO_CONNECT(connect_result, log_socket, &sock, sizeof(sock)); |
| 230 |
} |
| 231 |
|
| 232 |
/* Build message. Messages are generated both in the old style and new style, |
| 233 |
* so that log watchers that are configured for the old-style message continue |
| 234 |
* to work. |
| 235 |
*/ |
| 236 |
#define strconcat(str) \ |
| 237 |
{i=0; while ((str[i] != '\0') && ((i+plen)<(MESSAGE_BUFSIZ-1))) \ |
| 238 |
{\ |
| 239 |
message[plen+i]=str[i];\ |
| 240 |
i++;\ |
| 241 |
}\ |
| 242 |
plen+=i;} |
| 243 |
|
| 244 |
/* R.Henderson post-gcc-4 style message */ |
| 245 |
plen = 0; |
| 246 |
strconcat(msg_ssd); |
| 247 |
if (__progname != (char *)0) |
| 248 |
strconcat(__progname) |
| 249 |
else |
| 250 |
strconcat(msg_unknown); |
| 251 |
strconcat(msg_terminated); |
| 252 |
|
| 253 |
/* Write out error message to STDERR, to syslog if open */ |
| 254 |
INLINE_SYSCALL(write, 3, STDERR_FILENO, message, plen); |
| 255 |
if (connect_result != -1) |
| 256 |
INLINE_SYSCALL(write, 3, log_socket, message, plen); |
| 257 |
|
| 258 |
/* Dr. Etoh pre-gcc-4 style message */ |
| 259 |
plen = 0; |
| 260 |
if (__progname != (char *)0) |
| 261 |
strconcat(__progname) |
| 262 |
else |
| 263 |
strconcat(msg_unknown); |
| 264 |
strconcat(msg_ssa); |
| 265 |
strconcat(msg_inf); |
| 266 |
if (func != NULL) |
| 267 |
strconcat(func) |
| 268 |
else |
| 269 |
strconcat(msg_unknown); |
| 270 |
strconcat(msg_terminated); |
| 271 |
/* Write out error message to STDERR, to syslog if open */ |
| 272 |
INLINE_SYSCALL(write, 3, STDERR_FILENO, message, plen); |
| 273 |
if (connect_result != -1) |
| 274 |
INLINE_SYSCALL(write, 3, log_socket, message, plen); |
| 22 |
|
275 |
|
| 23 |
extern char **__libc_argv attribute_hidden; |
276 |
/* Write out error message to STDERR, to syslog if open */ |
|
|
277 |
INLINE_SYSCALL(write, 3, STDERR_FILENO, message, plen); |
| 278 |
if (connect_result != -1) |
| 279 |
INLINE_SYSCALL(write, 3, log_socket, message, plen); |
| 280 |
|
| 281 |
if (log_socket != -1) |
| 282 |
INLINE_SYSCALL(close, 1, log_socket); |
| 283 |
|
| 284 |
/* Suicide */ |
| 285 |
pid = INLINE_SYSCALL(getpid, 0); |
| 286 |
|
| 287 |
if (ENABLE_SSP_SMASH_DUMPS_CORE) { |
| 288 |
static struct sigaction default_abort_act; |
| 289 |
/* Remove any user-supplied handler for SIGABRT, before using it */ |
| 290 |
default_abort_act.sa_handler = SIG_DFL; |
| 291 |
default_abort_act.sa_sigaction = NULL; |
| 292 |
__sigfillset(&default_abort_act.sa_mask); |
| 293 |
default_abort_act.sa_flags = 0; |
| 294 |
if (DO_SIGACTION(SIGABRT, &default_abort_act, NULL) == 0) |
| 295 |
INLINE_SYSCALL(kill, 2, pid, SIGABRT); |
| 296 |
} |
| 297 |
|
| 298 |
/* Note; actions cannot be added to SIGKILL */ |
| 299 |
INLINE_SYSCALL(kill, 2, pid, SIGKILL); |
| 300 |
|
| 301 |
/* In case the kill didn't work, exit anyway |
| 302 |
* The loop prevents gcc thinking this routine returns |
| 303 |
*/ |
| 304 |
while (1) |
| 305 |
INLINE_SYSCALL(exit, 0); |
| 306 |
} |
| 24 |
|
307 |
|
| 25 |
void |
308 |
__attribute__ ((__noreturn__)) |
| 26 |
__attribute__ ((noreturn)) |
309 |
void __stack_chk_fail(void) |
| 27 |
__stack_chk_fail (void) |
|
|
| 28 |
{ |
310 |
{ |
| 29 |
__fortify_fail ("stack smashing detected"); |
311 |
__hardened_gentoo_stack_chk_fail(NULL, 0); |
| 30 |
} |
312 |
} |