yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org> |
| 3 | * |
| 4 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 5 | */ |
| 6 | |
| 7 | #ifndef _DL_OSINFO_H |
| 8 | #define _DL_OSINFO_H 1 |
| 9 | |
| 10 | #include <features.h> |
| 11 | |
| 12 | #ifdef __UCLIBC_HAS_SSP__ |
| 13 | # if defined IS_IN_libc || defined IS_IN_rtld |
| 14 | |
| 15 | # if defined __SSP__ || defined __SSP_ALL__ |
| 16 | # error "file must not be compiled with stack protection enabled on it. Use -fno-stack-protector" |
| 17 | # endif |
| 18 | |
| 19 | # include <stdint.h> |
| 20 | # include <sys/time.h> |
| 21 | |
| 22 | # ifdef IS_IN_libc |
| 23 | #include <fcntl.h> |
| 24 | # define OPEN open |
| 25 | # define READ read |
| 26 | # define CLOSE close |
| 27 | # define GETTIMEOFDAY gettimeofday |
| 28 | # else |
| 29 | # define OPEN _dl_open |
| 30 | # define READ _dl_read |
| 31 | # define CLOSE _dl_close |
| 32 | # define GETTIMEOFDAY _dl_gettimeofday |
| 33 | # endif |
| 34 | |
| 35 | static __always_inline uintptr_t _dl_setup_stack_chk_guard(void) |
| 36 | { |
| 37 | uintptr_t ret; |
| 38 | # ifndef __SSP_QUICK_CANARY__ |
| 39 | { |
| 40 | int fd = OPEN("/dev/urandom", O_RDONLY, 0); |
| 41 | if (fd >= 0) { |
| 42 | size_t size = READ(fd, &ret, sizeof(ret)); |
| 43 | CLOSE(fd); |
| 44 | if (size == (size_t) sizeof(ret)) |
| 45 | return ret; |
| 46 | } |
| 47 | } |
| 48 | # endif /* !__SSP_QUICK_CANARY__ */ |
| 49 | |
| 50 | /* Start with the "terminator canary". */ |
| 51 | ret = 0xFF0A0D00UL; |
| 52 | |
| 53 | /* Everything failed? Or we are using a weakened model of the |
| 54 | * terminator canary */ |
| 55 | { |
| 56 | struct timeval tv; |
| 57 | if (GETTIMEOFDAY(&tv, NULL) != (-1)) |
| 58 | ret ^= tv.tv_usec ^ tv.tv_sec; |
| 59 | } |
| 60 | return ret; |
| 61 | } |
| 62 | # endif /* libc || rtld */ |
| 63 | #endif /* __UCLIBC_HAS_SSP__ */ |
| 64 | |
| 65 | #endif /* _DL_OSINFO_H */ |