2 * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.hpp>
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>
16 #include <lttng/constant.h>
20 #ifdef HAVE_SYS_SYSCALL_H
21 #include <sys/syscall.h>
24 #define LTTNG_THROW_RANDOM_PRODUCTION_ERROR(msg) \
25 throw lttng::random::production_error( \
26 msg, lttng::source_location(__FILE__, __func__, __LINE__))
29 /* getrandom is available in Linux >= 3.17. */
30 #if defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H)
32 #include <sys/random.h>
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
)
39 return getrandom(out_data
, size
, GRND_NONBLOCK
);
42 ssize_t
_call_getrandom_nonblock(char *out_data
, std::size_t size
)
44 const int grnd_nonblock_flag
= 0x1;
46 auto ret
= syscall(SYS_getrandom
, out_data
, size
, grnd_nonblock_flag
);
54 #endif /* defined(HAVE_GETRANDOM) */
56 /* Returns either with a full read or throws. */
57 void getrandom_nonblock(char *out_data
, std::size_t size
)
60 * Since GRND_RANDOM is _not_ used, a partial read can only be caused
61 * by a signal interruption. In this case, retry.
66 ret
= _call_getrandom_nonblock(out_data
, size
);
67 } while ((ret
> 0 && ret
!= size
) || (ret
== -1 && errno
== EINTR
));
71 lttng::format("Failed to get true random data using getrandom(): size={}",
76 #elif defined(HAVE_ARC4RANDOM)
81 * According to the MacOS / FreeBSD manpage, this function never fails nor blocks.
83 void getrandom_nonblock(char *out_data
, std::size_t size
)
85 arc4random_buf(out_data
, size
);
87 #else /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */
88 __attribute__((noreturn
)) void getrandom_nonblock(char *out_data
__attribute__((unused
)),
89 std::size_t size
__attribute__((unused
)))
91 LTTNG_THROW_RANDOM_PRODUCTION_ERROR("getrandom() is not supported by this platform");
93 #endif /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */
95 lttng::random::seed_t
produce_pseudo_random_seed()
98 struct timespec real_time
= {};
99 struct timespec monotonic_time
= {};
100 unsigned long hash_seed
;
101 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
104 ret
= clock_gettime(CLOCK_REALTIME
, &real_time
);
106 LTTNG_THROW_POSIX("Failed to read real time while generating pseudo-random seed",
110 ret
= clock_gettime(CLOCK_MONOTONIC
, &monotonic_time
);
113 "Failed to read monotonic time while generating pseudo-random seed", errno
);
116 ret
= gethostname(hostname
, sizeof(hostname
));
118 LTTNG_THROW_POSIX("Failed to get host name while generating pseudo-random seed",
122 hash_seed
= (unsigned long) real_time
.tv_nsec
^ (unsigned long) real_time
.tv_sec
^
123 (unsigned long) monotonic_time
.tv_nsec
^ (unsigned long) monotonic_time
.tv_sec
;
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
);
129 const unsigned long pid
= getpid();
130 seed
^= hash_key_ulong((void *) pid
, hash_seed
);
131 seed
^= hash_key_str(hostname
, hash_seed
);
133 return static_cast<lttng::random::seed_t
>(seed
);
136 lttng::random::seed_t
produce_random_seed_from_urandom()
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.
142 lttng::file_descriptor urandom
{ []() {
143 const auto urandom_raw_fd
= open("/dev/urandom", O_RDONLY
| O_CLOEXEC
);
145 if (urandom_raw_fd
< 0) {
146 LTTNG_THROW_POSIX("Failed to open `/dev/urandom`", errno
);
149 return urandom_raw_fd
;
152 lttng::random::seed_t seed
;
154 urandom
.read(&seed
, sizeof(seed
));
155 } catch (const std::exception
& e
) {
156 LTTNG_THROW_RANDOM_PRODUCTION_ERROR(lttng::format(
157 "Failed to read from `/dev/urandom`: size={}: {}", sizeof(seed
), e
.what()));
165 lttng::random::production_error::production_error(const std::string
& msg
,
166 const lttng::source_location
& location
) :
167 lttng::runtime_error(msg
, location
)
171 lttng::random::seed_t
lttng::random::produce_true_random_seed()
173 lttng::random::seed_t seed
;
175 getrandom_nonblock(reinterpret_cast<char *>(&seed
), sizeof(seed
));
179 lttng::random::seed_t
lttng::random::produce_best_effort_random_seed()
182 return lttng::random::produce_true_random_seed();
183 } catch (const std::exception
& e
) {
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: {}",
193 * Can fail for various reasons, including not being accessible
194 * under some containerized environments.
196 produce_random_seed_from_urandom();
197 } catch (const std::exception
& e
) {
199 lttng::format("Failed to produce a random seed from the urandom device: {}",
204 /* Fallback to time-based seed generation. */
205 return produce_pseudo_random_seed();
This page took 0.032801 seconds and 4 git commands to generate.