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