include/punk/punk_entropy.h

filter: Statementstmt 91.6% 11 / 12 Branchbran 100% 5 / 6 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.4% 16 / 18 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_compat.h ↑ Summary include/punk/punk_password.h »
1 #ifndef PUNK_ENTROPY_H
2 #define PUNK_ENTROPY_H
3  
4 /* Sliced entropy, for the things that need a fresh unguessable value on every
5 * request: a request id, a CSP nonce.
6 *
7 * ---- why it is sliced -----------------------------------------------------
8 *
9 * `getentropy` costs per CALL, not per byte. Measured on darwin/arm64:
10 *
11 * bytes ns/call amortised ns per 16-byte draw
12 * 16 731.2 731.2
13 * 256 669.7 41.9
14 *
15 * A flat ~640-730 ns whether it is asked for 16 bytes or 256. So it is asked
16 * for 256 - its documented maximum - and sixteen draws come out of the
17 * result. That one constant is worth 17x, and for scale a bare Punk request
18 * is about 1 us: per-call entropy would be most of it.
19 *
20 * A LARGER buffer buys nothing. CAP bytes still needs CAP/256 calls, so draws
21 * per syscall is pinned at 16 however much is held; 256 B and 256 KB measure
22 * the same. So 256 B: least memory, and the least unused entropy sitting in a
23 * worker's address space.
24 *
25 * ---- why the pid guard is not optional ------------------------------------
26 *
27 * A buffer filled once and inherited through `fork` hands every worker the
28 * same bytes. Measured while building Punk::Plugin::RequestId: 767 duplicate
29 * values in 8000 across four workers, and it looks perfectly random the whole
30 * time - the code reads correctly, the output passes any eyeball test, and
31 * only a forked-pool test finds it.
32 *
33 * For a request id that is a nuisance. For a CSP nonce it is the
34 * vulnerability, reached from the inside: two users on two workers served the
35 * same nonce means one of them can predict the other's, and a predictable
36 * nonce is an attacker's `<script nonce="...">` running on somebody else's
37 * page.
38 *
39 * `getpid()` is 1.5 ns on macOS - libc-cached, not a syscall - so the guard
40 * costs nothing measurable there. On glibc 2.25 and later it IS a syscall
41 * (the cache was removed), about 50-60 ns; still a fraction of what the
42 * slicing won. Worth checking rather than assuming: a guard that was itself
43 * a syscall would have handed back everything the slicing won.
44 *
45 * Needs punk_csrf.h first, for pk_urandom_fd.
46 */
47  
48 #define PK_ENT_CAP 256 /* getentropy's documented maximum */
49  
50 static unsigned char pk_ent_buf[PK_ENT_CAP];
51 static size_t pk_ent_off = sizeof pk_ent_buf;
52 static Pid_t pk_ent_pid = 0;
53  
54 9953 static void pk_ent_fill(pTHX) {
55 #ifdef PUNK_HAVE_GETENTROPY
56 /*
57 # uncoverable branch true
58 # uncoverable condition left count:1 class:ignore_covered_err */
59 9953 ▶ if (getentropy(pk_ent_buf, sizeof pk_ent_buf) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (getentropy(pk_ent_buf, sizeof pk_ent_buf) != 0) 0 9953 9953
60 0 croak("Punk: getentropy failed: %s", Strerror(errno)); /* # uncoverable statement */
61 #else
62 /* The same cached descriptor punk_csrf.h uses, and the same refusal to
63 * fall back to anything weaker: a guessable nonce or token is worse than
64 * a server that will not start. */
65 size_t off = 0;
66 if (pk_urandom_fd < 0) {
67 pk_urandom_fd = PerlLIO_open3("/dev/urandom", O_RDONLY, 0);
68 if (pk_urandom_fd < 0)
69 croak("Punk: cannot open /dev/urandom: %s", Strerror(errno));
70 }
71 while (off < sizeof pk_ent_buf) {
72 SSize_t got = PerlLIO_read(pk_urandom_fd, pk_ent_buf + off,
73 sizeof pk_ent_buf - off);
74 if (got <= 0) croak("Punk: short read from /dev/urandom");
75 off += (size_t)got;
76 }
77 #endif
78 9953 pk_ent_off = 0;
79 9953 pk_ent_pid = getpid();
80 9953 }
81  
82 /* n bytes of entropy. n must be <= PK_ENT_CAP. */
83 159192 static void pk_ent_take(pTHX_ unsigned char *out, size_t n) {
84 159192 ▶ if (pk_ent_pid != getpid() || pk_ent_off + n > sizeof pk_ent_buf)
Branch: 2 branches
BranchTrueFalseTotal
if (pk_ent_pid != getpid() || pk_ent_off + n > sizeof pk_ent_buf) 159133 59 159192
if (pk_ent_pid != getpid() || pk_ent_off + n > sizeof pk_ent_buf) 9894 149239 159133
85 9953 pk_ent_fill(aTHX);
86 159192 Copy(pk_ent_buf + pk_ent_off, out, n, unsigned char);
87 159192 pk_ent_off += n;
88 159192 }
89  
90 #endif /* PUNK_ENTROPY_H */
« include/punk/punk_compat.h ↑ Summary include/punk/punk_password.h »