ba9d937ba32b95f43f877348ac0f358ea9c7e071
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(msg, __FILE__, __func__, __LINE__)
28 /* getrandom is available in Linux >= 3.17. */
29 #if defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H)
31 #include <sys/random.h>
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
)
38 return getrandom(out_data
, size
, GRND_NONBLOCK
);
41 ssize_t
_call_getrandom_nonblock(char *out_data
, std::size_t size
)
43 const int grnd_nonblock_flag
= 0x1;
45 auto ret
= syscall(SYS_getrandom
, out_data
, size
, grnd_nonblock_flag
);
53 #endif /* defined(HAVE_GETRANDOM) */
55 /* Returns either with a full read or throws. */
56 void getrandom_nonblock(char *out_data
, std::size_t size
)
59 * Since GRND_RANDOM is _not_ used, a partial read can only be caused
60 * by a signal interruption. In this case, retry.
65 ret
= _call_getrandom_nonblock(out_data
, size
);
66 } while ((ret
> 0 && ret
!= size
) || (ret
== -1 && errno
== EINTR
));
70 lttng::format("Failed to get true random data using getrandom(): size={}",
75 #elif defined(HAVE_ARC4RANDOM)
80 * According to the MacOS / FreeBSD manpage, this function never fails nor blocks.
82 void getrandom_nonblock(char *out_data
, std::size_t size
)
84 arc4random_buf(out_data
, size
);
86 #else /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */
87 __attribute__((noreturn
)) void getrandom_nonblock(char *out_data
__attribute__((unused
)),
88 std::size_t size
__attribute__((unused
)))
90 LTTNG_THROW_RANDOM_PRODUCTION_ERROR("getrandom() is not supported by this platform");
92 #endif /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */
94 lttng::random::seed_t
produce_pseudo_random_seed()
97 struct timespec real_time
= {};
98 struct timespec monotonic_time
= {};
99 unsigned long hash_seed
;
100 char hostname
[LTTNG_HOST_NAME_MAX
] = {};
103 ret
= clock_gettime(CLOCK_REALTIME
, &real_time
);
105 LTTNG_THROW_POSIX("Failed to read real time while generating pseudo-random seed",
109 ret
= clock_gettime(CLOCK_MONOTONIC
, &monotonic_time
);
112 "Failed to read monotonic time while generating pseudo-random seed", errno
);
115 ret
= gethostname(hostname
, sizeof(hostname
));
117 LTTNG_THROW_POSIX("Failed to get host name while generating pseudo-random seed",
121 hash_seed
= (unsigned long) real_time
.tv_nsec
^ (unsigned long) real_time
.tv_sec
^
122 (unsigned long) monotonic_time
.tv_nsec
^ (unsigned long) monotonic_time
.tv_sec
;
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
);
128 const unsigned long pid
= getpid();
129 seed
^= hash_key_ulong((void *) pid
, hash_seed
);
130 seed
^= hash_key_str(hostname
, hash_seed
);
132 return static_cast<lttng::random::seed_t
>(seed
);
135 lttng::random::seed_t
produce_random_seed_from_urandom()
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.
141 lttng::file_descriptor urandom
{ []() {
142 const auto urandom_raw_fd
= open("/dev/urandom", O_RDONLY
| O_CLOEXEC
);
144 if (urandom_raw_fd
< 0) {
145 LTTNG_THROW_POSIX("Failed to open `/dev/urandom`", errno
);
148 return urandom_raw_fd
;
151 lttng::random::seed_t seed
;
153 urandom
.read(&seed
, sizeof(seed
));
154 } catch (const std::exception
& e
) {
155 LTTNG_THROW_RANDOM_PRODUCTION_ERROR(lttng::format(
156 "Failed to read from `/dev/urandom`: size={}: {}", sizeof(seed
), e
.what()));
164 lttng::random::production_error::production_error(const std::string
& msg
,
165 const char *file_name
,
166 const char *function_name
,
167 unsigned int line_number
) :
168 lttng::runtime_error(msg
, file_name
, function_name
, line_number
)
172 lttng::random::seed_t
lttng::random::produce_true_random_seed()
174 lttng::random::seed_t seed
;
176 getrandom_nonblock(reinterpret_cast<char *>(&seed
), sizeof(seed
));
180 lttng::random::seed_t
lttng::random::produce_best_effort_random_seed()
183 return lttng::random::produce_true_random_seed();
184 } catch (const std::exception
& e
) {
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: {}",
194 * Can fail for various reasons, including not being accessible
195 * under some containerized environments.
197 produce_random_seed_from_urandom();
198 } catch (const std::exception
& e
) {
200 lttng::format("Failed to produce a random seed from the urandom device: {}",
205 /* Fallback to time-based seed generation. */
206 return produce_pseudo_random_seed();
This page took 0.032597 seconds and 3 git commands to generate.