]>
Commit | Line | Data |
---|---|---|
1718c60c | 1 | /* Operating system specific code for generic dynamic loader functions. Linux. |
d4697bc9 | 2 | Copyright (C) 2000-2014 Free Software Foundation, Inc. |
32b4fe6a UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
32b4fe6a UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
32b4fe6a | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
32b4fe6a | 18 | |
6ddd37a4 | 19 | #include <kernel-features.h> |
1718c60c | 20 | #include <dl-sysdep.h> |
15a856b1 | 21 | #include <endian.h> |
39546e34 | 22 | #include <fcntl.h> |
35f1e827 | 23 | #include <stdint.h> |
05f399e6 | 24 | #include <not-cancel.h> |
b4246fd6 UD |
25 | |
26 | #ifndef MIN | |
27 | # define MIN(a,b) (((a)<(b))?(a):(b)) | |
28 | #endif | |
32b4fe6a | 29 | |
1718c60c | 30 | #define DL_SYSDEP_OSCHECK(FATAL) \ |
32b4fe6a | 31 | do { \ |
1718c60c RM |
32 | /* Test whether the kernel is new enough. This test is only performed \ |
33 | if the library is not compiled to run on all kernels. */ \ | |
a334319f | 34 | \ |
1718c60c RM |
35 | int version = _dl_discover_osversion (); \ |
36 | if (__builtin_expect (version >= 0, 1)) \ | |
37 | { \ | |
609b254a UD |
38 | if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \ |
39 | || GLRO(dl_osversion) > version) \ | |
40 | GLRO(dl_osversion) = version; \ | |
32b4fe6a UD |
41 | \ |
42 | /* Now we can test with the required version. */ \ | |
1718c60c | 43 | if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \ |
32b4fe6a UD |
44 | /* Not sufficent. */ \ |
45 | FATAL ("FATAL: kernel too old\n"); \ | |
46 | } \ | |
1718c60c RM |
47 | else if (__LINUX_KERNEL_VERSION > 0) \ |
48 | FATAL ("FATAL: cannot determine kernel version\n"); \ | |
32b4fe6a | 49 | } while (0) |
35f1e827 UD |
50 | |
51 | static inline uintptr_t __attribute__ ((always_inline)) | |
965cb60a | 52 | _dl_setup_stack_chk_guard (void *dl_random) |
35f1e827 | 53 | { |
15a856b1 UD |
54 | union |
55 | { | |
56 | uintptr_t num; | |
57 | unsigned char bytes[sizeof (uintptr_t)]; | |
58 | } ret; | |
59 | ||
965cb60a UD |
60 | #ifndef __ASSUME_AT_RANDOM |
61 | if (__builtin_expect (dl_random == NULL, 0)) | |
35f1e827 | 62 | { |
15a856b1 UD |
63 | const size_t filllen = sizeof (ret.bytes) - 1; |
64 | ret.num = 0; | |
965cb60a | 65 | # ifdef ENABLE_STACKGUARD_RANDOMIZE |
05f399e6 | 66 | int fd = open_not_cancel_2 ("/dev/urandom", O_RDONLY); |
965cb60a UD |
67 | if (fd >= 0) |
68 | { | |
15a856b1 | 69 | ssize_t reslen = read_not_cancel (fd, ret.bytes + 1, filllen); |
05f399e6 | 70 | close_not_cancel_no_status (fd); |
8c297311 | 71 | if (reslen == (ssize_t) filllen) |
15a856b1 | 72 | return ret.num; |
965cb60a UD |
73 | } |
74 | # endif | |
6c6a98c9 TV |
75 | ret.bytes[filllen] = 255; |
76 | ret.bytes[filllen - 1] = '\n'; | |
35f1e827 | 77 | } |
965cb60a | 78 | else |
35f1e827 | 79 | #endif |
15a856b1 UD |
80 | { |
81 | /* We need in the moment only 8 bytes on 32-bit platforms and 16 | |
82 | bytes on 64-bit platforms. Therefore we can use the data | |
83 | directly and not use the kernel-provided data to seed a PRNG. */ | |
84 | memcpy (ret.bytes, dl_random, sizeof (ret)); | |
85 | #if BYTE_ORDER == LITTLE_ENDIAN | |
98bb2f1c | 86 | ret.num &= ~(uintptr_t) 0xff; |
15a856b1 | 87 | #elif BYTE_ORDER == BIG_ENDIAN |
98bb2f1c | 88 | ret.num &= ~((uintptr_t) 0xff << (8 * (sizeof (ret) - 1))); |
15a856b1 UD |
89 | #else |
90 | # error "BYTE_ORDER unknown" | |
91 | #endif | |
92 | } | |
93 | return ret.num; | |
965cb60a UD |
94 | } |
95 | ||
96 | static inline uintptr_t __attribute__ ((always_inline)) | |
97 | _dl_setup_pointer_guard (void *dl_random, uintptr_t stack_chk_guard) | |
98 | { | |
99 | uintptr_t ret; | |
100 | #ifndef __ASSUME_AT_RANDOM | |
101 | if (dl_random == NULL) | |
102 | { | |
103 | ret = stack_chk_guard; | |
104 | # ifndef HP_TIMING_NONAVAIL | |
105 | hp_timing_t now; | |
106 | HP_TIMING_NOW (now); | |
107 | ret ^= now; | |
108 | # endif | |
109 | } | |
110 | else | |
111 | #endif | |
112 | memcpy (&ret, (char *) dl_random + sizeof (ret), sizeof (ret)); | |
35f1e827 UD |
113 | return ret; |
114 | } |