/src/pdns/ext/arc4random/arc4random.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: arc4random_linux.h,v 1.12 2019/07/11 10:37:28 inoguchi Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
5 | | * Copyright (c) 2008, Damien Miller <djm@openbsd.org> |
6 | | * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> |
7 | | * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org> |
8 | | * |
9 | | * Permission to use, copy, modify, and distribute this software for any |
10 | | * purpose with or without fee is hereby granted, provided that the above |
11 | | * copyright notice and this permission notice appear in all copies. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
14 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
15 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
16 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
17 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
18 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
19 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
20 | | */ |
21 | | |
22 | | /* |
23 | | * Stub functions for portability. From LibreSSL with some adaptations. |
24 | | */ |
25 | | |
26 | | #include <sys/mman.h> |
27 | | |
28 | | #include <signal.h> |
29 | | |
30 | | /* OpenSSH isn't multithreaded */ |
31 | 0 | #define _ARC4_LOCK() pthread_mutex_lock(&arc4mutex); |
32 | 0 | #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4mutex); |
33 | | #define _ARC4_ATFORK(f) |
34 | | |
35 | | static inline void |
36 | | _getentropy_fail(void) |
37 | 0 | { |
38 | 0 | fatal("getentropy failed"); |
39 | 0 | } |
40 | | |
41 | | static volatile sig_atomic_t _rs_forked; |
42 | | |
43 | | static inline void |
44 | | _rs_forkhandler(void) |
45 | 0 | { |
46 | 0 | _rs_forked = 1; |
47 | 0 | } |
48 | | |
49 | | static inline void |
50 | | _rs_forkdetect(void) |
51 | 0 | { |
52 | 0 | static pid_t _rs_pid = 0; |
53 | 0 | pid_t pid = getpid(); |
54 | |
|
55 | 0 | if (_rs_pid == 0 || _rs_pid == 1 || _rs_pid != pid || _rs_forked) { |
56 | 0 | _rs_pid = pid; |
57 | 0 | _rs_forked = 0; |
58 | 0 | if (rs) |
59 | 0 | memset(rs, 0, sizeof(*rs)); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | static inline int |
64 | | _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) |
65 | 0 | { |
66 | 0 | #if defined(MAP_ANON) && defined(MAP_PRIVATE) |
67 | 0 | if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE, |
68 | 0 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
69 | 0 | return (-1); |
70 | | |
71 | 0 | if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE, |
72 | 0 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { |
73 | 0 | munmap(*rsp, sizeof(**rsp)); |
74 | 0 | *rsp = NULL; |
75 | 0 | return (-1); |
76 | 0 | } |
77 | | #else |
78 | | if ((*rsp = calloc(1, sizeof(**rsp))) == NULL) |
79 | | return (-1); |
80 | | if ((*rsxp = calloc(1, sizeof(**rsxp))) == NULL) { |
81 | | free(*rsp); |
82 | | *rsp = NULL; |
83 | | return (-1); |
84 | | } |
85 | | #endif |
86 | | |
87 | 0 | _ARC4_ATFORK(_rs_forkhandler); |
88 | 0 | return (0); |
89 | 0 | } |