Something went wrong. Try again.
Sign git commits and tags with an OpenPGP key kept in Bitwarden.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! An allocator for secrets: memory that is kept out of swap and core dumps//! where the operating system allows it, and wiped when it is freed.//!//! Every allocation is its own run of whole pages from the page allocator,//! which is wasteful and deliberate. Locking memory works on pages, so two//! secrets sharing a page with something else would have that something else in//! locked memory too and, worse, released from it when it was freed. The agent//! holds a handful of keys and makes a handful of allocations per signature, so//! the waste is a few pages.//!//! Locking memory can fail. Linux caps locked memory per process//! (`RLIMIT_MEMLOCK`, 8 MiB under current systemd), and an Argon2id derivation//! with the server's default settings wants 64 MiB of it. That is not a reason//! to refuse to work, so memory that could not be locked is counted and//! reported by `memoryLockFailures`, and is still wiped when it is freed.
const std = @import("std");const builtin = @import("builtin");const Allocator = std.mem.Allocator;const Alignment = std.mem.Alignment;const page_allocator = std.heap.page_allocator;
const windows = struct { extern "kernel32" fn VirtualLock(address: *anyopaque, size: usize) callconv(.winapi) std.os.windows.BOOL; extern "kernel32" fn VirtualUnlock(address: *anyopaque, size: usize) callconv(.winapi) std.os.windows.BOOL;};
var memory_lock_failures: std.atomic.Value(usize) = .init(0);
/// How many allocations could not be locked into memory.pub fn memoryLockFailures() usize { return memory_lock_failures.load(.monotonic);}
pub const allocator: Allocator = .{ .ptr = undefined, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free, },};
fn pageLength(len: usize) usize { return std.mem.alignForward(usize, len, std.heap.pageSize());}
fn lockPages(ptr: [*]u8, len: usize) void { switch (builtin.os.tag) { .linux => { const linux = std.os.linux; if (linux.errno(linux.mlock(ptr, len)) != .SUCCESS) { _ = memory_lock_failures.fetchAdd(1, .monotonic); } // Kept out of core dumps even if the process were dumpable. _ = linux.madvise(ptr, len, linux.MADV.DONTDUMP); }, .windows => { if (windows.VirtualLock(ptr, len) == .FALSE) { _ = memory_lock_failures.fetchAdd(1, .monotonic); } }, else => {}, }}
fn unlockPages(ptr: [*]u8, len: usize) void { switch (builtin.os.tag) { .linux => _ = std.os.linux.munlock(ptr, len), .windows => _ = windows.VirtualUnlock(ptr, len), else => {}, }}
fn alloc(_: *anyopaque, len: usize, alignment: Alignment, ret_addr: usize) ?[*]u8 { const pages = pageLength(len); const ptr = page_allocator.rawAlloc(pages, alignment, ret_addr) orelse return null; lockPages(ptr, pages); return ptr;}
fn resize(_: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) bool { _ = alignment; _ = ret_addr; // Within the pages already held, growing or shrinking is free. Shrinking // wipes what is given up, since it stays mapped and locked. if (pageLength(new_len) != pageLength(memory.len)) return false; if (new_len < memory.len) std.crypto.secureZero(u8, memory[new_len..]); return true;}
fn remap(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) ?[*]u8 { return if (resize(ctx, memory, alignment, new_len, ret_addr)) memory.ptr else null;}
fn free(_: *anyopaque, memory: []u8, alignment: Alignment, ret_addr: usize) void { const pages = pageLength(memory.len); const whole = memory.ptr[0..pages]; std.crypto.secureZero(u8, whole); unlockPages(memory.ptr, pages); page_allocator.rawFree(whole, alignment, ret_addr);}
/// Makes the process harder to read secrets out of: no core dumps, and on/// Linux no ptrace attach by other processes of the same user.pub fn hardenProcess() void { switch (builtin.os.tag) { .linux => { const linux = std.os.linux; _ = linux.prctl(@backingInt(linux.PR.SET_DUMPABLE), 0, 0, 0, 0); const zero: linux.rlimit = .{ .cur = 0, .max = 0 }; _ = linux.setrlimit(.CORE, &zero); }, else => {}, }}
test "allocations are wiped and page-sized" { const a = allocator; const buf = try a.alloc(u8, 100); @memset(buf, 0xaa); // Growing within the page works in place. try std.testing.expect(a.resize(buf, 200)); var grown: []u8 = buf; grown.len = 200; // Shrinking wipes the tail. try std.testing.expect(a.resize(grown, 50)); try std.testing.expectEqual(@as(u8, 0), grown[60]); grown.len = 50; a.free(grown);
var list: std.ArrayList(u8) = .empty; defer list.deinit(a); for (0..10000) |i| try list.append(a, @truncate(i)); try std.testing.expectEqual(@as(usize, 10000), list.items.len);}