{"record":{"id":"520db03ad275b612","repo":"JuliusBrussee/caveman","slug":"native-session-key-random-w","errorCode":null,"errorMessage":"native session key random: %w","messagePattern":"native session key random: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/nativeruntime/marker.go","lineNumber":34,"sourceCode":"\nconst sessionKeyBytes = 32\n\nvar markerPattern = regexp.MustCompile(`\\[\\[caveman-session-v1 sid=\"([A-Za-z0-9_-]{1,384})\" sig=\"([0-9a-f]{64})\"\\]\\]`)\n\n// LoadOrCreateSessionKey returns one user-only HMAC key shared by CLI adapters\n// and local proxy. O_EXCL makes concurrent first startup converge on one key.\nfunc LoadOrCreateSessionKey(home string) ([]byte, error) {\n\tdir := filepath.Join(home, \"runtime\")\n\tif err := os.MkdirAll(dir, 0o700); err != nil {\n\t\treturn nil, fmt.Errorf(\"native session key mkdir: %w\", err)\n\t}\n\tif err := os.Chmod(dir, 0o700); err != nil {\n\t\treturn nil, fmt.Errorf(\"native session key chmod dir: %w\", err)\n\t}\n\tpath := filepath.Join(dir, \"session.key\")\n\tkey := make([]byte, sessionKeyBytes)\n\tif _, err := rand.Read(key); err != nil {\n\t\treturn nil, fmt.Errorf(\"native session key random: %w\", err)\n\t}\n\tfile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)\n\tif err == nil {\n\t\tif _, writeErr := file.Write(key); writeErr != nil {\n\t\t\t_ = file.Close()\n\t\t\t_ = os.Remove(path)\n\t\t\treturn nil, fmt.Errorf(\"native session key write: %w\", writeErr)\n\t\t}\n\t\tif syncErr := file.Sync(); syncErr != nil {\n\t\t\t_ = file.Close()\n\t\t\t_ = os.Remove(path)\n\t\t\treturn nil, fmt.Errorf(\"native session key sync: %w\", syncErr)\n\t\t}\n\t\tif closeErr := file.Close(); closeErr != nil {\n\t\t\treturn nil, fmt.Errorf(\"native session key close: %w\", closeErr)\n\t\t}\n\t\treturn key, nil\n\t}","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/proxy/internal/nativeruntime/marker.go#L16-L52","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hand-rolled minimal Docker images; locked-down CI runners; embedded or legacy systems; endpoint-security software hooking CSRNG syscalls.","solutions":["Verify /dev/urandom exists and is a character device: ls -l /dev/urandom","Relax the seccomp/sandbox profile to allow getrandom(2), or use a standard runtime profile","Move to a kernel 3.17+ / standard base image","If at very early boot, wait and retry after the entropy pool initializes"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"func probeSystemRNG() error {\n\tbuf := make([]byte, 32)\n\tif _, err := rand.Read(buf); err != nil {\n\t\treturn fmt.Errorf(\"system CSPRNG unavailable — check seccomp profile and /dev/urandom: %w\", err)\n\t}\n\treturn nil\n}\n\n// fail with a targeted message at app startup, before the key path runs\nif err := probeSystemRNG(); err != nil {\n\treturn err\n}","typeGuard":null,"tryCatchPattern":"key, err := nativeruntime.LoadOrCreateSessionKey(home)\nif err != nil {\n\tif strings.Contains(err.Error(), \"session key random\") {\n\t\t// environment defect, not a code bug: sandbox blocks getrandom(2) or /dev/urandom is missing\n\t\treturn fmt.Errorf(\"cannot start: system randomness unavailable in this sandbox: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Use standard base images that ship a correct /dev/urandom device node","Allow getrandom(2) in any custom seccomp/AppArmor profile applied to the process","Probe crypto/rand once at startup so failures surface with a clear cause before deeper initialization","Avoid pre-3.17 kernels for deployments that generate keys"],"tags":["go","crypto","randomness","sandboxing"],"backgroundTag":"crypto-rand-unavailable","analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}