Skip to content

Harden CAG KV-cache: authenticate blob, tighten dir perms, tenant namespace #1024

Closed

Description

@AmitoVrito

Summary

Hardening for the CAG KV-cache persistence layer (#1000 / #1010). Three findings from the post-merge security review:

D1 — Untrusted KV-cache blob fed to llama.cpp native deserializer (HIGH)

KVCacheStore.load (rag/kv_cache_store.py:81) validates only integrity metadata (corpus_fingerprint/model_id/n_ctx/cache_format_version) — an attacker who can write into the cache dir also writes the .json, so they trivially satisfy all four. The raw .bin bytes then flow to LlamaState(data=blob) → model.load_state(state) (llm/_llamacpp_cag_backend.py:59-62,146-149), a native C++ state parser with a history of insufficient bounds-checking → memory-corruption / potential in-process RCE on any shared/multi-tenant cache dir. The "pickle-free" claim is true but the blob is still unauthenticated.

Fix: HMAC-sign the blob with a per-install secret key (mirror the DreamMode signing-key pattern) and verify before load_state. On missing/invalid signature → treat as cache miss (return None) → safe RAG/regeneration fallback. Non-breaking: existing unsigned caches simply miss and regenerate.

D2 — Cache directory created world-readable (MEDIUM)

os.makedirs(cache_dir, exist_ok=True) (rag/kv_cache_store.py:28) uses the process umask (typically 0o755). The .bin is a materialization of the model's context over the whole corpus — confidential for a private corpus — and the default location is .synapsekit_cag_cache in CWD.

Fix: os.makedirs(cache_dir, mode=0o700, exist_ok=True) and os.chmod(cache_dir, 0o700) when it already exists (makedirs won't tighten an existing dir).

D3 — Cache key has no tenant namespace (LOW)

CacheKey is (corpus_fingerprint, model_id, n_ctx). Two tenants with identical corpus+model share a cache file. Combined with D1/D2, a shared cache dir crosses tenants.

Fix: add an optional namespace: str = "" field to CacheKey (folded into the filename hash). Additive/default — existing callers unaffected.

Severity

HIGH (D1) + MEDIUM (D2) + LOW (D3).

Acceptance

  • Regression tests: tampered .bin with valid metadata is rejected (miss, not loaded); cache dir is 0o700; namespace isolates keys; round-trip of a legitimately-signed blob still loads.
  • CHANGELOG [Unreleased] Security entry.

Activity

  1. added
    bug
    security
    on Sep 8, 2026
  2. added a commit that references this issue on Sep 12, 2026
    983abd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsecurity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions