Commit | Line | Data |
---|---|---|
57b90af7 JG |
1 | /* |
2 | * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
20c734f5 | 8 | #include <common/error.hpp> |
57b90af7 JG |
9 | #include <common/file-descriptor.hpp> |
10 | #include <common/format.hpp> | |
11 | #include <common/hashtable/utils.hpp> | |
12 | #include <common/random.hpp> | |
13 | #include <common/readwrite.hpp> | |
14 | #include <common/time.hpp> | |
15 | ||
28ab034a JG |
16 | #include <lttng/constant.h> |
17 | ||
57b90af7 JG |
18 | #include <fcntl.h> |
19 | ||
20 | #ifdef HAVE_SYS_SYSCALL_H | |
21 | #include <sys/syscall.h> | |
22 | #endif | |
23 | ||
24 | #define LTTNG_THROW_RANDOM_PRODUCTION_ERROR(msg) \ | |
25 | throw lttng::random::production_error(msg, __FILE__, __func__, __LINE__) | |
26 | ||
27 | namespace { | |
28 | /* getrandom is available in Linux >= 3.17. */ | |
29 | #if defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) | |
30 | ||
31 | #include <sys/random.h> | |
32 | ||
33 | /* A glibc wrapper is provided only for glibc >= 2.25. */ | |
34 | #if defined(HAVE_GETRANDOM) | |
35 | /* Simply use the existing wrapper, passing the non-block flag. */ | |
36 | ssize_t _call_getrandom_nonblock(char *out_data, std::size_t size) | |
37 | { | |
38 | return getrandom(out_data, size, GRND_NONBLOCK); | |
39 | } | |
40 | #else | |
41 | ssize_t _call_getrandom_nonblock(char *out_data, std::size_t size) | |
42 | { | |
43 | const int grnd_nonblock_flag = 0x1; | |
44 | ||
45 | auto ret = syscall(SYS_getrandom, out_data, size, grnd_nonblock_flag); | |
46 | if (ret < 0) { | |
47 | errno = -ret; | |
48 | ret = -1; | |
49 | } | |
50 | ||
51 | return ret; | |
52 | } | |
53 | #endif /* defined(HAVE_GETRANDOM) */ | |
54 | ||
55 | /* Returns either with a full read or throws. */ | |
56 | void getrandom_nonblock(char *out_data, std::size_t size) | |
57 | { | |
58 | /* | |
59 | * Since GRND_RANDOM is _not_ used, a partial read can only be caused | |
60 | * by a signal interruption. In this case, retry. | |
61 | */ | |
62 | ssize_t ret; | |
63 | ||
64 | do { | |
65 | ret = _call_getrandom_nonblock(out_data, size); | |
66 | } while ((ret > 0 && ret != size) || (ret == -1 && errno == EINTR)); | |
67 | ||
68 | if (ret < 0) { | |
69 | LTTNG_THROW_POSIX( | |
f9a41357 JG |
70 | lttng::format("Failed to get true random data using getrandom(): size={}", |
71 | size), | |
28ab034a | 72 | errno); |
57b90af7 JG |
73 | } |
74 | } | |
5a23e035 MJ |
75 | #elif defined(HAVE_ARC4RANDOM) |
76 | ||
77 | #include <stdlib.h> | |
78 | ||
79 | /* | |
80 | * According to the MacOS / FreeBSD manpage, this function never fails nor blocks. | |
81 | */ | |
82 | void getrandom_nonblock(char *out_data, std::size_t size) | |
83 | { | |
84 | arc4random_buf(out_data, size); | |
85 | } | |
57b90af7 | 86 | #else /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ |
28f23191 JG |
87 | __attribute__((noreturn)) void getrandom_nonblock(char *out_data __attribute__((unused)), |
88 | std::size_t size __attribute__((unused))) | |
57b90af7 | 89 | { |
28ab034a | 90 | LTTNG_THROW_RANDOM_PRODUCTION_ERROR("getrandom() is not supported by this platform"); |
57b90af7 JG |
91 | } |
92 | #endif /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ | |
93 | ||
94 | lttng::random::seed_t produce_pseudo_random_seed() | |
95 | { | |
96 | int ret; | |
97 | struct timespec real_time = {}; | |
98 | struct timespec monotonic_time = {}; | |
99 | unsigned long hash_seed; | |
100 | char hostname[LTTNG_HOST_NAME_MAX] = {}; | |
101 | unsigned long seed; | |
102 | ||
103 | ret = clock_gettime(CLOCK_REALTIME, &real_time); | |
104 | if (ret) { | |
105 | LTTNG_THROW_POSIX("Failed to read real time while generating pseudo-random seed", | |
28ab034a | 106 | errno); |
57b90af7 JG |
107 | } |
108 | ||
109 | ret = clock_gettime(CLOCK_MONOTONIC, &monotonic_time); | |
110 | if (ret) { | |
111 | LTTNG_THROW_POSIX( | |
28ab034a | 112 | "Failed to read monotonic time while generating pseudo-random seed", errno); |
57b90af7 JG |
113 | } |
114 | ||
115 | ret = gethostname(hostname, sizeof(hostname)); | |
116 | if (ret) { | |
117 | LTTNG_THROW_POSIX("Failed to get host name while generating pseudo-random seed", | |
28ab034a | 118 | errno); |
57b90af7 JG |
119 | } |
120 | ||
121 | hash_seed = (unsigned long) real_time.tv_nsec ^ (unsigned long) real_time.tv_sec ^ | |
28ab034a | 122 | (unsigned long) monotonic_time.tv_nsec ^ (unsigned long) monotonic_time.tv_sec; |
57b90af7 JG |
123 | seed = hash_key_ulong((void *) real_time.tv_sec, hash_seed); |
124 | seed ^= hash_key_ulong((void *) real_time.tv_nsec, hash_seed); | |
125 | seed ^= hash_key_ulong((void *) monotonic_time.tv_sec, hash_seed); | |
126 | seed ^= hash_key_ulong((void *) monotonic_time.tv_nsec, hash_seed); | |
127 | ||
128 | const unsigned long pid = getpid(); | |
129 | seed ^= hash_key_ulong((void *) pid, hash_seed); | |
130 | seed ^= hash_key_str(hostname, hash_seed); | |
131 | ||
132 | return static_cast<lttng::random::seed_t>(seed); | |
133 | } | |
134 | ||
135 | lttng::random::seed_t produce_random_seed_from_urandom() | |
136 | { | |
137 | /* | |
138 | * Open /dev/urandom as a file_descriptor, or throw on error. The | |
139 | * lambda is used to reduce the scope of the raw fd as much as possible. | |
140 | */ | |
28ab034a | 141 | lttng::file_descriptor urandom{ []() { |
57b90af7 JG |
142 | const auto urandom_raw_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); |
143 | ||
144 | if (urandom_raw_fd < 0) { | |
145 | LTTNG_THROW_POSIX("Failed to open `/dev/urandom`", errno); | |
146 | } | |
147 | ||
148 | return urandom_raw_fd; | |
28ab034a | 149 | }() }; |
57b90af7 JG |
150 | |
151 | lttng::random::seed_t seed; | |
20c734f5 JG |
152 | try { |
153 | urandom.read(&seed, sizeof(seed)); | |
154 | } catch (const std::exception& e) { | |
f9a41357 | 155 | LTTNG_THROW_RANDOM_PRODUCTION_ERROR(lttng::format( |
20c734f5 | 156 | "Failed to read from `/dev/urandom`: size={}: {}", sizeof(seed), e.what())); |
57b90af7 JG |
157 | } |
158 | ||
159 | return seed; | |
160 | } | |
161 | ||
162 | } /* namespace */ | |
163 | ||
164 | lttng::random::production_error::production_error(const std::string& msg, | |
28ab034a JG |
165 | const char *file_name, |
166 | const char *function_name, | |
167 | unsigned int line_number) : | |
57b90af7 JG |
168 | lttng::runtime_error(msg, file_name, function_name, line_number) |
169 | { | |
170 | } | |
171 | ||
172 | lttng::random::seed_t lttng::random::produce_true_random_seed() | |
173 | { | |
174 | lttng::random::seed_t seed; | |
175 | ||
176 | getrandom_nonblock(reinterpret_cast<char *>(&seed), sizeof(seed)); | |
177 | return seed; | |
178 | } | |
179 | ||
180 | lttng::random::seed_t lttng::random::produce_best_effort_random_seed() | |
181 | { | |
182 | try { | |
183 | return lttng::random::produce_true_random_seed(); | |
5b9eda8a | 184 | } catch (const std::exception& e) { |
57b90af7 | 185 | WARN("%s", |
f9a41357 | 186 | lttng::format( |
28ab034a JG |
187 | "Failed to produce a random seed using getrandom(), falling back to pseudo-random device seed generation which will block until its pool is initialized: {}", |
188 | e.what()) | |
189 | .c_str()); | |
57b90af7 JG |
190 | } |
191 | ||
192 | try { | |
193 | /* | |
194 | * Can fail for various reasons, including not being accessible | |
195 | * under some containerized environments. | |
196 | */ | |
197 | produce_random_seed_from_urandom(); | |
5b9eda8a | 198 | } catch (const std::exception& e) { |
57b90af7 | 199 | WARN("%s", |
f9a41357 JG |
200 | lttng::format("Failed to produce a random seed from the urandom device: {}", |
201 | e.what()) | |
28ab034a | 202 | .c_str()); |
57b90af7 JG |
203 | } |
204 | ||
205 | /* Fallback to time-based seed generation. */ | |
206 | return produce_pseudo_random_seed(); | |
207 | } |