diff options
| author | matthew <> | 2014-07-18 21:40:54 +0000 |
|---|---|---|
| committer | matthew <> | 2014-07-18 21:40:54 +0000 |
| commit | cb6d7664a81745720f33266734161b267e83c5dd (patch) | |
| tree | d35b51e66c8c108b69caf1250f41710acc15f577 /src | |
| parent | bbdd86efa830966ab392f24545de3da7c1ebc213 (diff) | |
| download | openbsd-cb6d7664a81745720f33266734161b267e83c5dd.tar.gz openbsd-cb6d7664a81745720f33266734161b267e83c5dd.tar.bz2 openbsd-cb6d7664a81745720f33266734161b267e83c5dd.zip | |
Cleanup portable arc4random fork detection code:
1. Use "len" parameter instead of sizeof(*rs).
2. Simplify the atfork handler to be strictly async signal safe by
simply writing to a global volatile sig_atomic_t object, and then
checking for this in _rs_forkdetect(). (Idea from discussions with
Szabolcs Nagy and Rich Felker.)
3. Use memset(rs, 0, sizeof(*rs)) to match OpenBSD's MAP_INHERIT_ZERO
fork semantics to avoid any skew in behavior across platforms.
ok deraadt
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/arc4random/arc4random_linux.h | 19 | ||||
| -rw-r--r-- | src/lib/libcrypto/arc4random/arc4random_osx.h | 19 | ||||
| -rw-r--r-- | src/lib/libcrypto/arc4random/arc4random_solaris.h | 19 | ||||
| -rw-r--r-- | src/lib/libcrypto/crypto/arc4random_linux.h | 19 | ||||
| -rw-r--r-- | src/lib/libcrypto/crypto/arc4random_osx.h | 19 | ||||
| -rw-r--r-- | src/lib/libcrypto/crypto/arc4random_solaris.h | 19 |
6 files changed, 48 insertions, 66 deletions
diff --git a/src/lib/libcrypto/arc4random/arc4random_linux.h b/src/lib/libcrypto/arc4random/arc4random_linux.h index 2319ccbf42..f02ae388d5 100644 --- a/src/lib/libcrypto/arc4random/arc4random_linux.h +++ b/src/lib/libcrypto/arc4random/arc4random_linux.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random_linux.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random_linux.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -27,21 +27,18 @@ _rs_allocate(size_t len) | |||
| 27 | { | 27 | { |
| 28 | void *p; | 28 | void *p; |
| 29 | 29 | ||
| 30 | if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | 30 | if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
| 32 | return (NULL); | 32 | return (NULL); |
| 33 | return (p); | 33 | return (p); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static volatile sig_atomic_t _rs_forked; | ||
| 37 | |||
| 36 | static inline void | 38 | static inline void |
| 37 | _rs_forkhandler(void) | 39 | _rs_forkhandler(void) |
| 38 | { | 40 | { |
| 39 | /* | 41 | _rs_forked = 1; |
| 40 | * Race-free because we're running single-threaded in a new | ||
| 41 | * address space, and once allocated rs is never deallocated. | ||
| 42 | */ | ||
| 43 | if (rs) | ||
| 44 | rs->rs_count = 0; | ||
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | static inline void | 44 | static inline void |
| @@ -50,11 +47,11 @@ _rs_forkdetect(void) | |||
| 50 | static pid_t _rs_pid = 0; | 47 | static pid_t _rs_pid = 0; |
| 51 | pid_t pid = getpid(); | 48 | pid_t pid = getpid(); |
| 52 | 49 | ||
| 53 | /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ | 50 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { |
| 54 | if (_rs_pid == 0 || _rs_pid != pid) { | ||
| 55 | _rs_pid = pid; | 51 | _rs_pid = pid; |
| 52 | _rs_forked = 0; | ||
| 56 | if (rs) | 53 | if (rs) |
| 57 | rs->rs_count = 0; | 54 | memset(rs, 0, sizeof(*rs)); |
| 58 | } | 55 | } |
| 59 | } | 56 | } |
| 60 | 57 | ||
diff --git a/src/lib/libcrypto/arc4random/arc4random_osx.h b/src/lib/libcrypto/arc4random/arc4random_osx.h index 88433e17dd..46053a45b9 100644 --- a/src/lib/libcrypto/arc4random/arc4random_osx.h +++ b/src/lib/libcrypto/arc4random/arc4random_osx.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random_osx.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random_osx.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -27,21 +27,18 @@ _rs_allocate(size_t len) | |||
| 27 | { | 27 | { |
| 28 | void *p; | 28 | void *p; |
| 29 | 29 | ||
| 30 | if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | 30 | if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
| 32 | return (NULL); | 32 | return (NULL); |
| 33 | return (p); | 33 | return (p); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static volatile sig_atomic_t _rs_forked; | ||
| 37 | |||
| 36 | static inline void | 38 | static inline void |
| 37 | _rs_forkhandler(void) | 39 | _rs_forkhandler(void) |
| 38 | { | 40 | { |
| 39 | /* | 41 | _rs_forked = 1; |
| 40 | * Race-free because we're running single-threaded in a new | ||
| 41 | * address space, and once allocated rs is never deallocated. | ||
| 42 | */ | ||
| 43 | if (rs) | ||
| 44 | rs->rs_count = 0; | ||
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | static inline void | 44 | static inline void |
| @@ -50,11 +47,11 @@ _rs_forkdetect(void) | |||
| 50 | static pid_t _rs_pid = 0; | 47 | static pid_t _rs_pid = 0; |
| 51 | pid_t pid = getpid(); | 48 | pid_t pid = getpid(); |
| 52 | 49 | ||
| 53 | /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ | 50 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { |
| 54 | if (_rs_pid == 0 || _rs_pid != pid) { | ||
| 55 | _rs_pid = pid; | 51 | _rs_pid = pid; |
| 52 | _rs_forked = 0; | ||
| 56 | if (rs) | 53 | if (rs) |
| 57 | rs->rs_count = 0; | 54 | memset(rs, 0, sizeof(*rs)); |
| 58 | } | 55 | } |
| 59 | } | 56 | } |
| 60 | 57 | ||
diff --git a/src/lib/libcrypto/arc4random/arc4random_solaris.h b/src/lib/libcrypto/arc4random/arc4random_solaris.h index ca8e107e40..2386dbe885 100644 --- a/src/lib/libcrypto/arc4random/arc4random_solaris.h +++ b/src/lib/libcrypto/arc4random/arc4random_solaris.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random_solaris.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random_solaris.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -27,21 +27,18 @@ _rs_allocate(size_t len) | |||
| 27 | { | 27 | { |
| 28 | void *p; | 28 | void *p; |
| 29 | 29 | ||
| 30 | if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | 30 | if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
| 32 | return (NULL); | 32 | return (NULL); |
| 33 | return (p); | 33 | return (p); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static volatile sig_atomic_t _rs_forked; | ||
| 37 | |||
| 36 | static inline void | 38 | static inline void |
| 37 | _rs_forkhandler(void) | 39 | _rs_forkhandler(void) |
| 38 | { | 40 | { |
| 39 | /* | 41 | _rs_forked = 1; |
| 40 | * Race-free because we're running single-threaded in a new | ||
| 41 | * address space, and once allocated rs is never deallocated. | ||
| 42 | */ | ||
| 43 | if (rs) | ||
| 44 | rs->rs_count = 0; | ||
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | static inline void | 44 | static inline void |
| @@ -50,11 +47,11 @@ _rs_forkdetect(void) | |||
| 50 | static pid_t _rs_pid = 0; | 47 | static pid_t _rs_pid = 0; |
| 51 | pid_t pid = getpid(); | 48 | pid_t pid = getpid(); |
| 52 | 49 | ||
| 53 | /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ | 50 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { |
| 54 | if (_rs_pid == 0 || _rs_pid != pid) { | ||
| 55 | _rs_pid = pid; | 51 | _rs_pid = pid; |
| 52 | _rs_forked = 0; | ||
| 56 | if (rs) | 53 | if (rs) |
| 57 | rs->rs_count = 0; | 54 | memset(rs, 0, sizeof(*rs)); |
| 58 | } | 55 | } |
| 59 | } | 56 | } |
| 60 | 57 | ||
diff --git a/src/lib/libcrypto/crypto/arc4random_linux.h b/src/lib/libcrypto/crypto/arc4random_linux.h index 2319ccbf42..f02ae388d5 100644 --- a/src/lib/libcrypto/crypto/arc4random_linux.h +++ b/src/lib/libcrypto/crypto/arc4random_linux.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random_linux.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random_linux.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -27,21 +27,18 @@ _rs_allocate(size_t len) | |||
| 27 | { | 27 | { |
| 28 | void *p; | 28 | void *p; |
| 29 | 29 | ||
| 30 | if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | 30 | if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
| 32 | return (NULL); | 32 | return (NULL); |
| 33 | return (p); | 33 | return (p); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static volatile sig_atomic_t _rs_forked; | ||
| 37 | |||
| 36 | static inline void | 38 | static inline void |
| 37 | _rs_forkhandler(void) | 39 | _rs_forkhandler(void) |
| 38 | { | 40 | { |
| 39 | /* | 41 | _rs_forked = 1; |
| 40 | * Race-free because we're running single-threaded in a new | ||
| 41 | * address space, and once allocated rs is never deallocated. | ||
| 42 | */ | ||
| 43 | if (rs) | ||
| 44 | rs->rs_count = 0; | ||
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | static inline void | 44 | static inline void |
| @@ -50,11 +47,11 @@ _rs_forkdetect(void) | |||
| 50 | static pid_t _rs_pid = 0; | 47 | static pid_t _rs_pid = 0; |
| 51 | pid_t pid = getpid(); | 48 | pid_t pid = getpid(); |
| 52 | 49 | ||
| 53 | /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ | 50 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { |
| 54 | if (_rs_pid == 0 || _rs_pid != pid) { | ||
| 55 | _rs_pid = pid; | 51 | _rs_pid = pid; |
| 52 | _rs_forked = 0; | ||
| 56 | if (rs) | 53 | if (rs) |
| 57 | rs->rs_count = 0; | 54 | memset(rs, 0, sizeof(*rs)); |
| 58 | } | 55 | } |
| 59 | } | 56 | } |
| 60 | 57 | ||
diff --git a/src/lib/libcrypto/crypto/arc4random_osx.h b/src/lib/libcrypto/crypto/arc4random_osx.h index 88433e17dd..46053a45b9 100644 --- a/src/lib/libcrypto/crypto/arc4random_osx.h +++ b/src/lib/libcrypto/crypto/arc4random_osx.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random_osx.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random_osx.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -27,21 +27,18 @@ _rs_allocate(size_t len) | |||
| 27 | { | 27 | { |
| 28 | void *p; | 28 | void *p; |
| 29 | 29 | ||
| 30 | if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | 30 | if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
| 32 | return (NULL); | 32 | return (NULL); |
| 33 | return (p); | 33 | return (p); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static volatile sig_atomic_t _rs_forked; | ||
| 37 | |||
| 36 | static inline void | 38 | static inline void |
| 37 | _rs_forkhandler(void) | 39 | _rs_forkhandler(void) |
| 38 | { | 40 | { |
| 39 | /* | 41 | _rs_forked = 1; |
| 40 | * Race-free because we're running single-threaded in a new | ||
| 41 | * address space, and once allocated rs is never deallocated. | ||
| 42 | */ | ||
| 43 | if (rs) | ||
| 44 | rs->rs_count = 0; | ||
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | static inline void | 44 | static inline void |
| @@ -50,11 +47,11 @@ _rs_forkdetect(void) | |||
| 50 | static pid_t _rs_pid = 0; | 47 | static pid_t _rs_pid = 0; |
| 51 | pid_t pid = getpid(); | 48 | pid_t pid = getpid(); |
| 52 | 49 | ||
| 53 | /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ | 50 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { |
| 54 | if (_rs_pid == 0 || _rs_pid != pid) { | ||
| 55 | _rs_pid = pid; | 51 | _rs_pid = pid; |
| 52 | _rs_forked = 0; | ||
| 56 | if (rs) | 53 | if (rs) |
| 57 | rs->rs_count = 0; | 54 | memset(rs, 0, sizeof(*rs)); |
| 58 | } | 55 | } |
| 59 | } | 56 | } |
| 60 | 57 | ||
diff --git a/src/lib/libcrypto/crypto/arc4random_solaris.h b/src/lib/libcrypto/crypto/arc4random_solaris.h index ca8e107e40..2386dbe885 100644 --- a/src/lib/libcrypto/crypto/arc4random_solaris.h +++ b/src/lib/libcrypto/crypto/arc4random_solaris.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random_solaris.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random_solaris.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -27,21 +27,18 @@ _rs_allocate(size_t len) | |||
| 27 | { | 27 | { |
| 28 | void *p; | 28 | void *p; |
| 29 | 29 | ||
| 30 | if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, | 30 | if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE, |
| 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | 31 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
| 32 | return (NULL); | 32 | return (NULL); |
| 33 | return (p); | 33 | return (p); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static volatile sig_atomic_t _rs_forked; | ||
| 37 | |||
| 36 | static inline void | 38 | static inline void |
| 37 | _rs_forkhandler(void) | 39 | _rs_forkhandler(void) |
| 38 | { | 40 | { |
| 39 | /* | 41 | _rs_forked = 1; |
| 40 | * Race-free because we're running single-threaded in a new | ||
| 41 | * address space, and once allocated rs is never deallocated. | ||
| 42 | */ | ||
| 43 | if (rs) | ||
| 44 | rs->rs_count = 0; | ||
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | static inline void | 44 | static inline void |
| @@ -50,11 +47,11 @@ _rs_forkdetect(void) | |||
| 50 | static pid_t _rs_pid = 0; | 47 | static pid_t _rs_pid = 0; |
| 51 | pid_t pid = getpid(); | 48 | pid_t pid = getpid(); |
| 52 | 49 | ||
| 53 | /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ | 50 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { |
| 54 | if (_rs_pid == 0 || _rs_pid != pid) { | ||
| 55 | _rs_pid = pid; | 51 | _rs_pid = pid; |
| 52 | _rs_forked = 0; | ||
| 56 | if (rs) | 53 | if (rs) |
| 57 | rs->rs_count = 0; | 54 | memset(rs, 0, sizeof(*rs)); |
| 58 | } | 55 | } |
| 59 | } | 56 | } |
| 60 | 57 | ||
