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( | |
28ab034a JG |
70 | fmt::format("Failed to get true random data using getrandom(): size={}", |
71 | size), | |
72 | errno); | |
57b90af7 JG |
73 | } |
74 | } | |
75 | #else /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ | |
45cc2527 MJ |
76 | __attribute__((noreturn)) |
77 | void getrandom_nonblock(char *out_data __attribute__((unused)), | |
78 | std::size_t size __attribute__((unused))) | |
57b90af7 | 79 | { |
28ab034a | 80 | LTTNG_THROW_RANDOM_PRODUCTION_ERROR("getrandom() is not supported by this platform"); |
57b90af7 JG |
81 | } |
82 | #endif /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ | |
83 | ||
84 | lttng::random::seed_t produce_pseudo_random_seed() | |
85 | { | |
86 | int ret; | |
87 | struct timespec real_time = {}; | |
88 | struct timespec monotonic_time = {}; | |
89 | unsigned long hash_seed; | |
90 | char hostname[LTTNG_HOST_NAME_MAX] = {}; | |
91 | unsigned long seed; | |
92 | ||
93 | ret = clock_gettime(CLOCK_REALTIME, &real_time); | |
94 | if (ret) { | |
95 | LTTNG_THROW_POSIX("Failed to read real time while generating pseudo-random seed", | |
28ab034a | 96 | errno); |
57b90af7 JG |
97 | } |
98 | ||
99 | ret = clock_gettime(CLOCK_MONOTONIC, &monotonic_time); | |
100 | if (ret) { | |
101 | LTTNG_THROW_POSIX( | |
28ab034a | 102 | "Failed to read monotonic time while generating pseudo-random seed", errno); |
57b90af7 JG |
103 | } |
104 | ||
105 | ret = gethostname(hostname, sizeof(hostname)); | |
106 | if (ret) { | |
107 | LTTNG_THROW_POSIX("Failed to get host name while generating pseudo-random seed", | |
28ab034a | 108 | errno); |
57b90af7 JG |
109 | } |
110 | ||
111 | hash_seed = (unsigned long) real_time.tv_nsec ^ (unsigned long) real_time.tv_sec ^ | |
28ab034a | 112 | (unsigned long) monotonic_time.tv_nsec ^ (unsigned long) monotonic_time.tv_sec; |
57b90af7 JG |
113 | seed = hash_key_ulong((void *) real_time.tv_sec, hash_seed); |
114 | seed ^= hash_key_ulong((void *) real_time.tv_nsec, hash_seed); | |
115 | seed ^= hash_key_ulong((void *) monotonic_time.tv_sec, hash_seed); | |
116 | seed ^= hash_key_ulong((void *) monotonic_time.tv_nsec, hash_seed); | |
117 | ||
118 | const unsigned long pid = getpid(); | |
119 | seed ^= hash_key_ulong((void *) pid, hash_seed); | |
120 | seed ^= hash_key_str(hostname, hash_seed); | |
121 | ||
122 | return static_cast<lttng::random::seed_t>(seed); | |
123 | } | |
124 | ||
125 | lttng::random::seed_t produce_random_seed_from_urandom() | |
126 | { | |
127 | /* | |
128 | * Open /dev/urandom as a file_descriptor, or throw on error. The | |
129 | * lambda is used to reduce the scope of the raw fd as much as possible. | |
130 | */ | |
28ab034a | 131 | lttng::file_descriptor urandom{ []() { |
57b90af7 JG |
132 | const auto urandom_raw_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); |
133 | ||
134 | if (urandom_raw_fd < 0) { | |
135 | LTTNG_THROW_POSIX("Failed to open `/dev/urandom`", errno); | |
136 | } | |
137 | ||
138 | return urandom_raw_fd; | |
28ab034a | 139 | }() }; |
57b90af7 JG |
140 | |
141 | lttng::random::seed_t seed; | |
20c734f5 JG |
142 | try { |
143 | urandom.read(&seed, sizeof(seed)); | |
144 | } catch (const std::exception& e) { | |
145 | LTTNG_THROW_RANDOM_PRODUCTION_ERROR(fmt::format( | |
146 | "Failed to read from `/dev/urandom`: size={}: {}", sizeof(seed), e.what())); | |
57b90af7 JG |
147 | } |
148 | ||
149 | return seed; | |
150 | } | |
151 | ||
152 | } /* namespace */ | |
153 | ||
154 | lttng::random::production_error::production_error(const std::string& msg, | |
28ab034a JG |
155 | const char *file_name, |
156 | const char *function_name, | |
157 | unsigned int line_number) : | |
57b90af7 JG |
158 | lttng::runtime_error(msg, file_name, function_name, line_number) |
159 | { | |
160 | } | |
161 | ||
162 | lttng::random::seed_t lttng::random::produce_true_random_seed() | |
163 | { | |
164 | lttng::random::seed_t seed; | |
165 | ||
166 | getrandom_nonblock(reinterpret_cast<char *>(&seed), sizeof(seed)); | |
167 | return seed; | |
168 | } | |
169 | ||
170 | lttng::random::seed_t lttng::random::produce_best_effort_random_seed() | |
171 | { | |
172 | try { | |
173 | return lttng::random::produce_true_random_seed(); | |
174 | } catch (std::exception& e) { | |
175 | WARN("%s", | |
28ab034a JG |
176 | fmt::format( |
177 | "Failed to produce a random seed using getrandom(), falling back to pseudo-random device seed generation which will block until its pool is initialized: {}", | |
178 | e.what()) | |
179 | .c_str()); | |
57b90af7 JG |
180 | } |
181 | ||
182 | try { | |
183 | /* | |
184 | * Can fail for various reasons, including not being accessible | |
185 | * under some containerized environments. | |
186 | */ | |
187 | produce_random_seed_from_urandom(); | |
188 | } catch (std::exception& e) { | |
189 | WARN("%s", | |
28ab034a JG |
190 | fmt::format("Failed to produce a random seed from the urandom device: {}", |
191 | e.what()) | |
192 | .c_str()); | |
57b90af7 JG |
193 | } |
194 | ||
195 | /* Fallback to time-based seed generation. */ | |
196 | return produce_pseudo_random_seed(); | |
197 | } |