diff options
author | bcook <> | 2014-11-03 06:23:30 +0000 |
---|---|---|
committer | bcook <> | 2014-11-03 06:23:30 +0000 |
commit | a3eca90dbf58e4b053bf7a401300f044cfdc2256 (patch) | |
tree | 9c175467713d63cf2e0e331b942d923c801a93d4 /src | |
parent | ae4a0ba982e7f6609f71539c65c23a5bdfdf446d (diff) | |
download | openbsd-a3eca90dbf58e4b053bf7a401300f044cfdc2256.tar.gz openbsd-a3eca90dbf58e4b053bf7a401300f044cfdc2256.tar.bz2 openbsd-a3eca90dbf58e4b053bf7a401300f044cfdc2256.zip |
Add hooks to override native arc4random_buf on FreeBSD.
The FreeBSD-native arc4random_buf implementation falls back to weak sources of
entropy if the sysctl fails. Remove these dangerous fallbacks by overriding
locally.
Unfortunately, pthread_atfork() is also broken on FreeBSD (at least 9 and 10)
if a program does not link to -lthr. Callbacks registered with pthread_atfork()
simply fail silently. So, it is not always possible to detect a PID wraparound.
I wish we could do better.
This improves arc4random_buf's safety compared to the native FreeBSD
implementation.
Tested on FreeBSD 9 and 10.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/arc4random/arc4random_freebsd.h | 85 | ||||
-rw-r--r-- | src/lib/libcrypto/arc4random/getentropy_freebsd.c | 64 | ||||
-rw-r--r-- | src/lib/libcrypto/crypto/arc4random_freebsd.h | 85 | ||||
-rw-r--r-- | src/lib/libcrypto/crypto/getentropy_freebsd.c | 64 |
4 files changed, 298 insertions, 0 deletions
diff --git a/src/lib/libcrypto/arc4random/arc4random_freebsd.h b/src/lib/libcrypto/arc4random/arc4random_freebsd.h new file mode 100644 index 0000000000..0231ff2d4c --- /dev/null +++ b/src/lib/libcrypto/arc4random/arc4random_freebsd.h | |||
@@ -0,0 +1,85 @@ | |||
1 | /* $OpenBSD: arc4random_freebsd.h,v 1.1 2014/11/03 06:23:30 bcook 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 | #include <sys/mman.h> | ||
26 | |||
27 | #include <pthread.h> | ||
28 | #include <signal.h> | ||
29 | |||
30 | static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; | ||
31 | #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx) | ||
32 | #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx) | ||
33 | |||
34 | /* | ||
35 | * Unfortunately, pthread_atfork() is broken on FreeBSD (at least 9 and 10) if | ||
36 | * a program does not link to -lthr. Callbacks registered with pthread_atfork() | ||
37 | * appear to fail silently. So, it is not always possible to detect a PID | ||
38 | * wraparound. | ||
39 | */ | ||
40 | #define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f)) | ||
41 | |||
42 | static inline void | ||
43 | _getentropy_fail(void) | ||
44 | { | ||
45 | raise(SIGKILL); | ||
46 | } | ||
47 | |||
48 | static volatile sig_atomic_t _rs_forked; | ||
49 | |||
50 | static inline void | ||
51 | _rs_forkhandler(void) | ||
52 | { | ||
53 | _rs_forked = 1; | ||
54 | } | ||
55 | |||
56 | static inline void | ||
57 | _rs_forkdetect(void) | ||
58 | { | ||
59 | static pid_t _rs_pid = 0; | ||
60 | pid_t pid = getpid(); | ||
61 | |||
62 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { | ||
63 | _rs_pid = pid; | ||
64 | _rs_forked = 0; | ||
65 | if (rs) | ||
66 | memset(rs, 0, sizeof(*rs)); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | static inline int | ||
71 | _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) | ||
72 | { | ||
73 | if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE, | ||
74 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | ||
75 | return -1; | ||
76 | |||
77 | if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE, | ||
78 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | ||
79 | munmap(*rsp, sizeof(**rsp)); | ||
80 | return -1; | ||
81 | } | ||
82 | |||
83 | _ARC4_ATFORK(_rs_forkhandler); | ||
84 | return 0; | ||
85 | } | ||
diff --git a/src/lib/libcrypto/arc4random/getentropy_freebsd.c b/src/lib/libcrypto/arc4random/getentropy_freebsd.c new file mode 100644 index 0000000000..ec760ae43d --- /dev/null +++ b/src/lib/libcrypto/arc4random/getentropy_freebsd.c | |||
@@ -0,0 +1,64 @@ | |||
1 | /* $OpenBSD: getentropy_freebsd.c,v 1.1 2014/11/03 06:23:30 bcook Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2014 Pawel Jakub Dawidek <pjd@FreeBSD.org> | ||
5 | * Copyright (c) 2014 Brent Cook <bcook@openbsd.org> | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
18 | * | ||
19 | * Emulation of getentropy(2) as documented at: | ||
20 | * http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2 | ||
21 | */ | ||
22 | |||
23 | #include <sys/types.h> | ||
24 | #include <sys/sysctl.h> | ||
25 | |||
26 | #include <errno.h> | ||
27 | #include <stddef.h> | ||
28 | |||
29 | /* | ||
30 | * Derived from lib/libc/gen/arc4random.c from FreeBSD. | ||
31 | */ | ||
32 | static size_t | ||
33 | getentropy_sysctl(u_char *buf, size_t size) | ||
34 | { | ||
35 | int mib[2]; | ||
36 | size_t len, done; | ||
37 | |||
38 | mib[0] = CTL_KERN; | ||
39 | mib[1] = KERN_ARND; | ||
40 | done = 0; | ||
41 | |||
42 | do { | ||
43 | len = size; | ||
44 | if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) | ||
45 | return (done); | ||
46 | done += len; | ||
47 | buf += len; | ||
48 | size -= len; | ||
49 | } while (size > 0); | ||
50 | |||
51 | return (done); | ||
52 | } | ||
53 | |||
54 | int | ||
55 | getentropy(void *buf, size_t len) | ||
56 | { | ||
57 | if (len <= 256 && | ||
58 | getentropy_sysctl(buf, len) == len) { | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | errno = EIO; | ||
63 | return -1; | ||
64 | } | ||
diff --git a/src/lib/libcrypto/crypto/arc4random_freebsd.h b/src/lib/libcrypto/crypto/arc4random_freebsd.h new file mode 100644 index 0000000000..0231ff2d4c --- /dev/null +++ b/src/lib/libcrypto/crypto/arc4random_freebsd.h | |||
@@ -0,0 +1,85 @@ | |||
1 | /* $OpenBSD: arc4random_freebsd.h,v 1.1 2014/11/03 06:23:30 bcook 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 | #include <sys/mman.h> | ||
26 | |||
27 | #include <pthread.h> | ||
28 | #include <signal.h> | ||
29 | |||
30 | static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; | ||
31 | #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx) | ||
32 | #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx) | ||
33 | |||
34 | /* | ||
35 | * Unfortunately, pthread_atfork() is broken on FreeBSD (at least 9 and 10) if | ||
36 | * a program does not link to -lthr. Callbacks registered with pthread_atfork() | ||
37 | * appear to fail silently. So, it is not always possible to detect a PID | ||
38 | * wraparound. | ||
39 | */ | ||
40 | #define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f)) | ||
41 | |||
42 | static inline void | ||
43 | _getentropy_fail(void) | ||
44 | { | ||
45 | raise(SIGKILL); | ||
46 | } | ||
47 | |||
48 | static volatile sig_atomic_t _rs_forked; | ||
49 | |||
50 | static inline void | ||
51 | _rs_forkhandler(void) | ||
52 | { | ||
53 | _rs_forked = 1; | ||
54 | } | ||
55 | |||
56 | static inline void | ||
57 | _rs_forkdetect(void) | ||
58 | { | ||
59 | static pid_t _rs_pid = 0; | ||
60 | pid_t pid = getpid(); | ||
61 | |||
62 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { | ||
63 | _rs_pid = pid; | ||
64 | _rs_forked = 0; | ||
65 | if (rs) | ||
66 | memset(rs, 0, sizeof(*rs)); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | static inline int | ||
71 | _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) | ||
72 | { | ||
73 | if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE, | ||
74 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | ||
75 | return -1; | ||
76 | |||
77 | if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE, | ||
78 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | ||
79 | munmap(*rsp, sizeof(**rsp)); | ||
80 | return -1; | ||
81 | } | ||
82 | |||
83 | _ARC4_ATFORK(_rs_forkhandler); | ||
84 | return 0; | ||
85 | } | ||
diff --git a/src/lib/libcrypto/crypto/getentropy_freebsd.c b/src/lib/libcrypto/crypto/getentropy_freebsd.c new file mode 100644 index 0000000000..ec760ae43d --- /dev/null +++ b/src/lib/libcrypto/crypto/getentropy_freebsd.c | |||
@@ -0,0 +1,64 @@ | |||
1 | /* $OpenBSD: getentropy_freebsd.c,v 1.1 2014/11/03 06:23:30 bcook Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2014 Pawel Jakub Dawidek <pjd@FreeBSD.org> | ||
5 | * Copyright (c) 2014 Brent Cook <bcook@openbsd.org> | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
18 | * | ||
19 | * Emulation of getentropy(2) as documented at: | ||
20 | * http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2 | ||
21 | */ | ||
22 | |||
23 | #include <sys/types.h> | ||
24 | #include <sys/sysctl.h> | ||
25 | |||
26 | #include <errno.h> | ||
27 | #include <stddef.h> | ||
28 | |||
29 | /* | ||
30 | * Derived from lib/libc/gen/arc4random.c from FreeBSD. | ||
31 | */ | ||
32 | static size_t | ||
33 | getentropy_sysctl(u_char *buf, size_t size) | ||
34 | { | ||
35 | int mib[2]; | ||
36 | size_t len, done; | ||
37 | |||
38 | mib[0] = CTL_KERN; | ||
39 | mib[1] = KERN_ARND; | ||
40 | done = 0; | ||
41 | |||
42 | do { | ||
43 | len = size; | ||
44 | if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) | ||
45 | return (done); | ||
46 | done += len; | ||
47 | buf += len; | ||
48 | size -= len; | ||
49 | } while (size > 0); | ||
50 | |||
51 | return (done); | ||
52 | } | ||
53 | |||
54 | int | ||
55 | getentropy(void *buf, size_t len) | ||
56 | { | ||
57 | if (len <= 256 && | ||
58 | getentropy_sysctl(buf, len) == len) { | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | errno = EIO; | ||
63 | return -1; | ||
64 | } | ||