]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/dl-osinfo.h
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
1 /* Operating system specific code for generic dynamic loader functions. Linux.
2 Copyright (C) 2000-2002,2004-2009,2011 Free Software Foundation, Inc.
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
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.
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
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <kernel-features.h>
20 #include <dl-sysdep.h>
21 #include <endian.h>
22 #include <fcntl.h>
23 #include <stdint.h>
24 #include <not-cancel.h>
25
26 #ifndef MIN
27 # define MIN(a,b) (((a)<(b))?(a):(b))
28 #endif
29
30 #ifdef SHARED
31 /* This is the function used in the dynamic linker to print the fatal error
32 message. */
33 static void
34 __attribute__ ((__noreturn__))
35 dl_fatal (const char *str)
36 {
37 _dl_dprintf (2, str);
38 _exit (1);
39 }
40 #endif
41
42 #define DL_SYSDEP_OSCHECK(FATAL) \
43 do { \
44 /* Test whether the kernel is new enough. This test is only performed \
45 if the library is not compiled to run on all kernels. */ \
46 \
47 int version = _dl_discover_osversion (); \
48 if (__builtin_expect (version >= 0, 1)) \
49 { \
50 if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \
51 || GLRO(dl_osversion) > version) \
52 GLRO(dl_osversion) = version; \
53 \
54 /* Now we can test with the required version. */ \
55 if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \
56 /* Not sufficent. */ \
57 FATAL ("FATAL: kernel too old\n"); \
58 } \
59 else if (__LINUX_KERNEL_VERSION > 0) \
60 FATAL ("FATAL: cannot determine kernel version\n"); \
61 } while (0)
62
63 static inline uintptr_t __attribute__ ((always_inline))
64 _dl_setup_stack_chk_guard (void *dl_random)
65 {
66 union
67 {
68 uintptr_t num;
69 unsigned char bytes[sizeof (uintptr_t)];
70 } ret;
71
72 #ifndef __ASSUME_AT_RANDOM
73 if (__builtin_expect (dl_random == NULL, 0))
74 {
75 const size_t filllen = sizeof (ret.bytes) - 1;
76 ret.num = 0;
77 # ifdef ENABLE_STACKGUARD_RANDOMIZE
78 int fd = open_not_cancel_2 ("/dev/urandom", O_RDONLY);
79 if (fd >= 0)
80 {
81 ssize_t reslen = read_not_cancel (fd, ret.bytes + 1, filllen);
82 close_not_cancel_no_status (fd);
83 if (reslen == (ssize_t) filllen)
84 return ret.num;
85 }
86 # endif
87 ret.bytes[filllen - 2] = 255;
88 ret.bytes[filllen - 3] = '\n';
89 }
90 else
91 #endif
92 {
93 /* We need in the moment only 8 bytes on 32-bit platforms and 16
94 bytes on 64-bit platforms. Therefore we can use the data
95 directly and not use the kernel-provided data to seed a PRNG. */
96 memcpy (ret.bytes, dl_random, sizeof (ret));
97 #if BYTE_ORDER == LITTLE_ENDIAN
98 ret.num &= ~0xff;
99 #elif BYTE_ORDER == BIG_ENDIAN
100 ret.num &= ~(0xff << (8 * (sizeof (ret) - 1)));
101 #else
102 # error "BYTE_ORDER unknown"
103 #endif
104 }
105 return ret.num;
106 }
107
108 static inline uintptr_t __attribute__ ((always_inline))
109 _dl_setup_pointer_guard (void *dl_random, uintptr_t stack_chk_guard)
110 {
111 uintptr_t ret;
112 #ifndef __ASSUME_AT_RANDOM
113 if (dl_random == NULL)
114 {
115 ret = stack_chk_guard;
116 # ifndef HP_TIMING_NONAVAIL
117 hp_timing_t now;
118 HP_TIMING_NOW (now);
119 ret ^= now;
120 # endif
121 }
122 else
123 #endif
124 memcpy (&ret, (char *) dl_random + sizeof (ret), sizeof (ret));
125 return ret;
126 }