diff options
Diffstat (limited to 'src/lib/libc')
| -rw-r--r-- | src/lib/libc/crypt/arc4random.c | 47 | ||||
| -rw-r--r-- | src/lib/libc/crypt/arc4random.h | 46 |
2 files changed, 57 insertions, 36 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c index 565bfa0333..d42022c455 100644 --- a/src/lib/libc/crypt/arc4random.c +++ b/src/lib/libc/crypt/arc4random.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random.c,v 1.46 2014/07/17 14:30:41 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random.c,v 1.47 2014/07/18 02:05:55 deraadt Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -52,11 +52,16 @@ | |||
| 52 | #define RSBUFSZ (16*BLOCKSZ) | 52 | #define RSBUFSZ (16*BLOCKSZ) |
| 53 | 53 | ||
| 54 | /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */ | 54 | /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */ |
| 55 | static struct { | 55 | static struct _rs { |
| 56 | size_t rs_have; /* valid bytes at end of rs_buf */ | 56 | size_t rs_have; /* valid bytes at end of rs_buf */ |
| 57 | size_t rs_count; /* bytes till reseed */ | 57 | size_t rs_count; /* bytes till reseed */ |
| 58 | } *rs; | 58 | } *rs; |
| 59 | 59 | ||
| 60 | static inline void *_rs_allocate(size_t len); | ||
| 61 | static inline void _rs_forkdetect(void); | ||
| 62 | static inline void _rs_forkdetectsetup(struct _rs *buf, size_t len); | ||
| 63 | #include "arc4random.h" | ||
| 64 | |||
| 60 | /* Preserved in fork children. */ | 65 | /* Preserved in fork children. */ |
| 61 | static struct { | 66 | static struct { |
| 62 | chacha_ctx rs_chacha; /* chacha context for random keystream */ | 67 | chacha_ctx rs_chacha; /* chacha context for random keystream */ |
| @@ -65,19 +70,6 @@ static struct { | |||
| 65 | 70 | ||
| 66 | static inline void _rs_rekey(u_char *dat, size_t datlen); | 71 | static inline void _rs_rekey(u_char *dat, size_t datlen); |
| 67 | 72 | ||
| 68 | #ifndef MAP_INHERIT_ZERO | ||
| 69 | static inline void | ||
| 70 | _rs_forkhandler(void) | ||
| 71 | { | ||
| 72 | /* | ||
| 73 | * Race-free because we're running single-threaded in a new | ||
| 74 | * address space, and once allocated rs is never deallocated. | ||
| 75 | */ | ||
| 76 | if (rs) | ||
| 77 | rs->rs_count = 0; | ||
| 78 | } | ||
| 79 | #endif /* MAP_INHERIT_ZERO */ | ||
| 80 | |||
| 81 | static inline void | 73 | static inline void |
| 82 | _rs_init(u_char *buf, size_t n) | 74 | _rs_init(u_char *buf, size_t n) |
| 83 | { | 75 | { |
| @@ -85,19 +77,12 @@ _rs_init(u_char *buf, size_t n) | |||
| 85 | return; | 77 | return; |
| 86 | 78 | ||
| 87 | if (rs == NULL) { | 79 | if (rs == NULL) { |
| 88 | if ((rs = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | 80 | if ((rs = _rs_allocate(sizeof(*rs))) == NULL) |
| 89 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | ||
| 90 | abort(); | ||
| 91 | #ifdef MAP_INHERIT_ZERO | ||
| 92 | if (minherit(rs, sizeof(*rs), MAP_INHERIT_ZERO) == -1) | ||
| 93 | abort(); | 81 | abort(); |
| 94 | #else | 82 | _rs_forkdetectsetup(rs, sizeof(*rs)); |
| 95 | _ARC4_ATFORK(_rs_forkhandler); | ||
| 96 | #endif | ||
| 97 | } | 83 | } |
| 98 | if (rsx == NULL) { | 84 | if (rsx == NULL) { |
| 99 | if ((rsx = mmap(NULL, sizeof(*rsx), PROT_READ|PROT_WRITE, | 85 | if ((rsx = _rs_allocate(sizeof(*rsx))) == NULL) |
| 100 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | ||
| 101 | abort(); | 86 | abort(); |
| 102 | } | 87 | } |
| 103 | 88 | ||
| @@ -129,17 +114,7 @@ _rs_stir(void) | |||
| 129 | static inline void | 114 | static inline void |
| 130 | _rs_stir_if_needed(size_t len) | 115 | _rs_stir_if_needed(size_t len) |
| 131 | { | 116 | { |
| 132 | #ifndef MAP_INHERIT_ZERO | 117 | _rs_forkdetect(); |
| 133 | static pid_t _rs_pid = 0; | ||
| 134 | pid_t pid = getpid(); | ||
| 135 | |||
| 136 | /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ | ||
| 137 | if (_rs_pid == 0 || _rs_pid != pid) { | ||
| 138 | _rs_pid = pid; | ||
| 139 | if (rs) | ||
| 140 | rs->rs_count = 0; | ||
| 141 | } | ||
| 142 | #endif | ||
| 143 | if (!rs || rs->rs_count <= len) | 118 | if (!rs || rs->rs_count <= len) |
| 144 | _rs_stir(); | 119 | _rs_stir(); |
| 145 | if (rs->rs_count <= len) | 120 | if (rs->rs_count <= len) |
diff --git a/src/lib/libc/crypt/arc4random.h b/src/lib/libc/crypt/arc4random.h new file mode 100644 index 0000000000..e0309a3184 --- /dev/null +++ b/src/lib/libc/crypt/arc4random.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* $OpenBSD: arc4random.h,v 1.1 2014/07/18 02:05:55 deraadt 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 | * | ||
| 8 | * Permission to use, copy, modify, and distribute this software for any | ||
| 9 | * purpose with or without fee is hereby granted, provided that the above | ||
| 10 | * copyright notice and this permission notice appear in all copies. | ||
| 11 | * | ||
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 19 | */ | ||
| 20 | |||
| 21 | /* | ||
| 22 | * Stub functions for portability. | ||
| 23 | */ | ||
| 24 | |||
| 25 | static inline void * | ||
| 26 | _rs_allocate(size_t len) | ||
| 27 | { | ||
| 28 | void *p; | ||
| 29 | |||
| 30 | if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | ||
| 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | ||
| 32 | return (NULL); | ||
| 33 | return (p); | ||
| 34 | } | ||
| 35 | |||
| 36 | static inline void | ||
| 37 | _rs_forkdetect(void) | ||
| 38 | { | ||
| 39 | } | ||
| 40 | |||
| 41 | static inline void | ||
| 42 | _rs_forkdetectsetup(struct _rs *rs, size_t len) | ||
| 43 | { | ||
| 44 | if (minherit(rs, len, MAP_INHERIT_ZERO) == -1) | ||
| 45 | abort(); | ||
| 46 | } | ||
