stack protector: use AT_RANDOM
Populate the stack canaries from the kernel supplied
AT_RANDOM value, which doesn't involve any system calls.
This is slightly faster (6 fewer syscalls) and avoids
unnecessarily reading /dev/urandom, which depletes entropy.
Bug: 7959813
Change-Id: If2b43100a2a9929666df3de56b6139fed969e0f1
diff --git a/libc/private/bionic_ssp.h b/libc/private/bionic_ssp.h
index 697216c..14ced64 100644
--- a/libc/private/bionic_ssp.h
+++ b/libc/private/bionic_ssp.h
@@ -29,8 +29,8 @@
#ifndef _PRIVATE_SSP_H
#define _PRIVATE_SSP_H
-#include <errno.h>
-#include <sys/cdefs.h>
+#include <string.h>
+#include <sys/auxv.h>
__BEGIN_DECLS
@@ -48,27 +48,11 @@
extern void __stack_chk_fail();
__inline__ static void* __attribute__((always_inline)) __generate_stack_chk_guard(void) {
- union {
- uintptr_t value;
- char bytes[sizeof(uintptr_t)];
- } u;
- /* Try pulling random bytes from /dev/urandom. */
- int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY));
- if (fd != -1) {
- ssize_t byte_count = TEMP_FAILURE_RETRY(read(fd, &u.bytes, sizeof(u)));
- close(fd);
- if (byte_count == sizeof(u)) {
- return (void*) u.value;
- }
- }
-
- /* If that failed, switch to 'terminator canary'. */
- u.bytes[0] = 0;
- u.bytes[1] = 0;
- u.bytes[2] = '\n';
- u.bytes[3] = 255;
- return (void*) u.value;
+ void* src = (void*) getauxval(AT_RANDOM);
+ void* result;
+ memcpy(&result, src, sizeof(result));
+ return result;
}
__END_DECLS