| 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 |
2151 |
| static void pk_ent_fill(pTHX) { |
| 55 |
|
| #ifdef PUNK_HAVE_GETENTROPY |
| 56 |
2151 |
▶ | if (getentropy(pk_ent_buf, sizeof pk_ent_buf) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (getentropy(pk_ent_buf, sizeof pk_ent_buf) != 0) |
0 |
2151 |
2151 |
|
| 57 |
0 |
| croak("Punk: getentropy failed: %s", Strerror(errno)); |
| 58 |
|
| #else |
| 59 |
|
| /* The same cached descriptor punk_csrf.h uses, and the same refusal to |
| 60 |
|
| * fall back to anything weaker: a guessable nonce or token is worse than |
| 61 |
|
| * a server that will not start. */ |
| 62 |
|
| size_t off = 0; |
| 63 |
|
| if (pk_urandom_fd < 0) { |
| 64 |
|
| pk_urandom_fd = PerlLIO_open3("/dev/urandom", O_RDONLY, 0); |
| 65 |
|
| if (pk_urandom_fd < 0) |
| 66 |
|
| croak("Punk: cannot open /dev/urandom: %s", Strerror(errno)); |
| 67 |
|
| } |
| 68 |
|
| while (off < sizeof pk_ent_buf) { |
| 69 |
|
| SSize_t got = PerlLIO_read(pk_urandom_fd, pk_ent_buf + off, |
| 70 |
|
| sizeof pk_ent_buf - off); |
| 71 |
|
| if (got <= 0) croak("Punk: short read from /dev/urandom"); |
| 72 |
|
| off += (size_t)got; |
| 73 |
|
| } |
| 74 |
|
| #endif |
| 75 |
2151 |
| pk_ent_off = 0; |
| 76 |
2151 |
| pk_ent_pid = getpid(); |
| 77 |
2151 |
| } |
| 78 |
|
| |
| 79 |
|
| /* n bytes of entropy. n must be <= PK_ENT_CAP. */ |
| 80 |
34246 |
| static void pk_ent_take(pTHX_ unsigned char *out, size_t n) { |
| 81 |
34246 |
▶ | if (pk_ent_pid != getpid() || pk_ent_off + n > sizeof pk_ent_buf) |
Branch: 2 branches
| Branch | True | False | Total |
| if (pk_ent_pid != getpid() || pk_ent_off + n > sizeof pk_ent_buf) |
34210 |
36 |
34246 |
| if (pk_ent_pid != getpid() || pk_ent_off + n > sizeof pk_ent_buf) |
2115 |
32095 |
34210 |
|
| 82 |
2151 |
| pk_ent_fill(aTHX); |
| 83 |
34246 |
| Copy(pk_ent_buf + pk_ent_off, out, n, unsigned char); |
| 84 |
34246 |
| pk_ent_off += n; |
| 85 |
34246 |
| } |
| 86 |
|
| |
| 87 |
|
| #endif /* PUNK_ENTROPY_H */ |