JuliusBrussee/caveman · error

native session key random

Error message

native session key random: %w

What it means

crypto/rand.Read failed while generating the 32-byte HMAC session key. On any modern OS (getrandom(2) on Linux, RtlGenRandom/CNG on Windows) this call cannot block or fail, so hitting it indicates a constrained environment: a seccomp/sandbox profile blocking the randomness syscall, a minimal container missing /dev/urandom, or entropy starvation on pre-3.17 kernels at very early boot. No key file is written.

Solutions

  1. Verify /dev/urandom exists and is a character device: ls -l /dev/urandom
  2. Relax the seccomp/sandbox profile to allow getrandom(2), or use a standard runtime profile
  3. Move to a kernel 3.17+ / standard base image
  4. If at very early boot, wait and retry after the entropy pool initializes
Defensive patterns

Strategy: try-catch

Validate before calling

func probeSystemRNG() error {
	buf := make([]byte, 32)
	if _, err := rand.Read(buf); err != nil {
		return fmt.Errorf("system CSPRNG unavailable — check seccomp profile and /dev/urandom: %w", err)
	}
	return nil
}

// fail with a targeted message at app startup, before the key path runs
if err := probeSystemRNG(); err != nil {
	return err
}

Try / catch

key, err := nativeruntime.LoadOrCreateSessionKey(home)
if err != nil {
	if strings.Contains(err.Error(), "session key random") {
		// environment defect, not a code bug: sandbox blocks getrandom(2) or /dev/urandom is missing
		return fmt.Errorf("cannot start: system randomness unavailable in this sandbox: %w", err)
	}
	return err
}

Prevention

When it happens

Trigger: A hardened sandbox (gVisor, custom seccomp, AppArmor) denying getrandom(2); a container image without /dev/urandom or with a broken device node; an ancient kernel at early boot before the entropy pool initializes.

Common situations: Hand-rolled minimal Docker images; locked-down CI runners; embedded or legacy systems; endpoint-security software hooking CSRNG syscalls.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@766dce6b13 (2026-08-18). Data as JSON: /api/errors/520db03ad275b612. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/nativeruntime/marker.go:34

const sessionKeyBytes = 32

var markerPattern = regexp.MustCompile(`\[\[caveman-session-v1 sid="([A-Za-z0-9_-]{1,384})" sig="([0-9a-f]{64})"\]\]`)

// LoadOrCreateSessionKey returns one user-only HMAC key shared by CLI adapters
// and local proxy. O_EXCL makes concurrent first startup converge on one key.
func LoadOrCreateSessionKey(home string) ([]byte, error) {
	dir := filepath.Join(home, "runtime")
	if err := os.MkdirAll(dir, 0o700); err != nil {
		return nil, fmt.Errorf("native session key mkdir: %w", err)
	}
	if err := os.Chmod(dir, 0o700); err != nil {
		return nil, fmt.Errorf("native session key chmod dir: %w", err)
	}
	path := filepath.Join(dir, "session.key")
	key := make([]byte, sessionKeyBytes)
	if _, err := rand.Read(key); err != nil {
		return nil, fmt.Errorf("native session key random: %w", err)
	}
	file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
	if err == nil {
		if _, writeErr := file.Write(key); writeErr != nil {
			_ = file.Close()
			_ = os.Remove(path)
			return nil, fmt.Errorf("native session key write: %w", writeErr)
		}
		if syncErr := file.Sync(); syncErr != nil {
			_ = file.Close()
			_ = os.Remove(path)
			return nil, fmt.Errorf("native session key sync: %w", syncErr)
		}
		if closeErr := file.Close(); closeErr != nil {
			return nil, fmt.Errorf("native session key close: %w", closeErr)
		}
		return key, nil
	}

View on GitHub (pinned to 766dce6b13)