1 f9ab77a8 2023-08-23 op /* $OpenBSD: arc4random_linux.h,v 1.12 2019/07/11 10:37:28 inoguchi Exp $ */
4 f9ab77a8 2023-08-23 op * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 f9ab77a8 2023-08-23 op * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 f9ab77a8 2023-08-23 op * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 f9ab77a8 2023-08-23 op * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
9 f9ab77a8 2023-08-23 op * Permission to use, copy, modify, and distribute this software for any
10 f9ab77a8 2023-08-23 op * purpose with or without fee is hereby granted, provided that the above
11 f9ab77a8 2023-08-23 op * copyright notice and this permission notice appear in all copies.
13 f9ab77a8 2023-08-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 f9ab77a8 2023-08-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 f9ab77a8 2023-08-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 f9ab77a8 2023-08-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 f9ab77a8 2023-08-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 f9ab77a8 2023-08-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 f9ab77a8 2023-08-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 f9ab77a8 2023-08-23 op * Stub functions for portability. From LibreSSL with some adaptations.
26 f9ab77a8 2023-08-23 op #include <sys/mman.h>
28 f9ab77a8 2023-08-23 op #include <signal.h>
30 f9ab77a8 2023-08-23 op /* OpenSSH isn't multithreaded */
31 f9ab77a8 2023-08-23 op #define _ARC4_LOCK()
32 f9ab77a8 2023-08-23 op #define _ARC4_UNLOCK()
33 f9ab77a8 2023-08-23 op #define _ARC4_ATFORK(f)
35 f9ab77a8 2023-08-23 op static inline void
36 f9ab77a8 2023-08-23 op _getentropy_fail(void)
38 f9ab77a8 2023-08-23 op fatal("getentropy failed");
41 f9ab77a8 2023-08-23 op static volatile sig_atomic_t _rs_forked;
43 f9ab77a8 2023-08-23 op static inline void
44 f9ab77a8 2023-08-23 op _rs_forkhandler(void)
46 f9ab77a8 2023-08-23 op _rs_forked = 1;
49 f9ab77a8 2023-08-23 op static inline void
50 f9ab77a8 2023-08-23 op _rs_forkdetect(void)
52 f9ab77a8 2023-08-23 op static pid_t _rs_pid = 0;
53 f9ab77a8 2023-08-23 op pid_t pid = getpid();
55 f9ab77a8 2023-08-23 op if (_rs_pid == 0 || _rs_pid == 1 || _rs_pid != pid || _rs_forked) {
56 f9ab77a8 2023-08-23 op _rs_pid = pid;
57 f9ab77a8 2023-08-23 op _rs_forked = 0;
59 f9ab77a8 2023-08-23 op memset(rs, 0, sizeof(*rs));
63 f9ab77a8 2023-08-23 op static inline int
64 f9ab77a8 2023-08-23 op _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
66 f9ab77a8 2023-08-23 op #if defined(MAP_ANON) && defined(MAP_PRIVATE)
67 f9ab77a8 2023-08-23 op if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
68 f9ab77a8 2023-08-23 op MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
71 f9ab77a8 2023-08-23 op if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
72 f9ab77a8 2023-08-23 op MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
73 f9ab77a8 2023-08-23 op munmap(*rsp, sizeof(**rsp));
78 f9ab77a8 2023-08-23 op if ((*rsp = calloc(1, sizeof(**rsp))) == NULL)
80 f9ab77a8 2023-08-23 op if ((*rsxp = calloc(1, sizeof(**rsxp))) == NULL) {
87 f9ab77a8 2023-08-23 op _ARC4_ATFORK(_rs_forkhandler);