Skip to content

Commit 3199bd6

Browse files
committed
fix(audit): remediate Carbon v1.3.0 audit findings F1-F6, E2, E3, N1, N3
- F1 (+N1): Deepest-existing-ancestor symlink resolution (40-hop cap), RFC 8089 file://localhost/ + %XX normalization, interior quote/backslash unquoting, allowlist hardening, and exact _cowrie_* control key stripping across Rust trust.rs, Python __init__.py, and mcp-proxy params.arguments. - F2: Deterministic session taint root (0x53 0x20 || SHA-256('COWRIE_TAINT_SET_V1\0' || sorted_unique_cids)) accepted alongside per-CID tokens in check_tool_call_v2, mcp-proxy, CowrieToolGuard, and StatelessChallengeVerifier; dedup all 5 taint push sites in main.rs; add cowrie_session_taint_root C-ABI. - F3: Charge MAX_DECODED_BYTES (64 MiB) + MAX_KEY_LEN (65536) in parse_json_impl; add O(1)-drain bounded line reader on mcp-proxy stdin and child stdout; fix O(N^2) number scan in wasm/mcp-mediator.mjs. - F4: API-preserving canonical Eq/PartialEq on Value::FloatBits(u64); skew-guarded expiry pruning (expiry + MAX_CLOCK_SKEW_SECS < now) + crash-safe atomic CWNL0002 compaction in Rust NonceLedger and Python DurableNonceLedger. - F5: Add read-only O(1) cowrie_statepage_probe_header (Page 0 + 94B footer + HMAC) and scheduler match caching so full Merkle scan runs once on worker load while keeping cowrie_statepage_open_strict fail-closed; fix TP/PP>1 doc_cid mismatch; fix _ReusableSlabPool fragmentation eviction, _fast_staging.clear() leak, CUDA D2H stream sync on abort, and V4 manifest/padding edge cases. - F6: Remove incompatible fallback_path in _find_lib() (raise clear ABI version mismatch RuntimeError); enforce _is_safe_shared_lib and fail-closed existence/ABI checks on COWRIE_LIB and COWRIE_LIB_PATH. - E2: Add ivf/zonemap features to cowrie-core (default-on for C-ABI/conformance), build cowrie-cli with default-features=false, gate bench-live-ivf behind bench feature, and guard optional ivf/zonemap ctypes bindings with hasattr. - E3 (+N3): Fix lens.rs UTF-8 char-boundary panic, O(N^2) bounded-wildcard grep, RFC 6901 atlas JSON escaping, and SLICE double |TAINTED|TAINTED; harden evidence.rs (reject state=revoked with revoked_at=0, tensor_count!=0, non-policy frame headers, wasm32 usize casts, JSON escaping), Python evidence.py sentinel escaping on approval args, and initialize *out_json on FFI error.
1 parent e06936b commit 3199bd6

19 files changed

Lines changed: 3301 additions & 538 deletions

File tree

‎adapters/vllm/cowrie_kv_connector.py‎

Lines changed: 270 additions & 147 deletions
Large diffs are not rendered by default.

‎crates/cowrie-cli/Cargo.toml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ description = "cowrie — inspection, verification, lens projection, and zero-tr
1313
name = "cowrie"
1414
path = "src/main.rs"
1515

16+
[features]
17+
default = []
18+
bench = ["cowrie-core/ivf"]
19+
1620
[dependencies]
17-
cowrie-core = { path = "../cowrie-core" }
21+
cowrie-core = { path = "../cowrie-core", default-features = false }

‎crates/cowrie-cli/src/main.rs‎

Lines changed: 219 additions & 28 deletions
Large diffs are not rendered by default.

‎crates/cowrie-core/Cargo.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ description = "Cowrie: 16 KB-aligned, content-addressed, trust-tagged binary for
1313
name = "cowrie_core"
1414
crate-type = ["rlib", "cdylib", "staticlib"]
1515

16+
[features]
17+
default = ["ivf", "zonemap"]
18+
ivf = []
19+
zonemap = []
20+
1621
[dependencies]
1722
# Zero external dependencies: compiles offline in <2s with minimal supply-chain surface.

‎crates/cowrie-core/include/cowrie.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ int cowrie_statepage_open_strict(
174174
CowrieStatePageHandle** out_handle
175175
);
176176

177+
/* Read-only O(1) StatePage header + HMAC + pin probe (reads only Page 0 + 94B footer; never exposes data). */
178+
int cowrie_statepage_probe_header(
179+
const char* path_cstr,
180+
const char* expected_model_sha_cstr,
181+
const char* expected_doc_cid_cstr,
182+
const char* expected_runtime_id_cstr,
183+
int expected_kv_quant,
184+
int expected_ssm_dtype,
185+
uint32_t* out_ctx_tokens,
186+
uint64_t* out_state_len
187+
);
188+
177189
const uint8_t* cowrie_statepage_data(const CowrieStatePageHandle* handle);
178190
size_t cowrie_statepage_len(const CowrieStatePageHandle* handle);
179191
uint32_t cowrie_statepage_ctx_tokens(const CowrieStatePageHandle* handle);
@@ -214,6 +226,12 @@ int cowrie_evidence_verify_bundle(
214226
const uint8_t* expected_merkle_34_or_null,
215227
CowrieBuf* out_json
216228
);
229+
int cowrie_session_taint_root(
230+
const uint8_t* cids_34_flat,
231+
size_t cid_count,
232+
uint8_t* out_root_32,
233+
uint8_t* out_root_cid_34
234+
);
217235
int cowrie_canonical_args_hash(const uint8_t* args_json_utf8, size_t args_json_len, uint8_t* out_hash_32);
218236
int cowrie_cap_v2_sign(
219237
const uint8_t* seed_32,

‎crates/cowrie-core/src/canonical.rs‎

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ pub fn checked_tensor_min_bytes(dtype: u8, dims: &[u64]) -> Result<u64, CowrieEr
140140
}
141141

142142
/// Canonical logical value tree for Plane A frames.
143-
#[derive(Clone, Debug, PartialEq, Eq)]
143+
#[derive(Clone, Debug)]
144144
pub enum Value {
145145
Null,
146146
Bool(bool),
147147
Int(i64),
148148
Uint(u64),
149-
/// Stored as exact canonical IEEE-754 little-endian bits for exact bijective Eq.
149+
/// Stored as IEEE-754 little-endian bits; `PartialEq`/`Eq` compare `canonicalize_f64_bits`
150+
/// so `-0.0` == `+0.0` and all NaN payloads compare equal in memory as on the wire.
150151
FloatBits(u64),
151152
Str(String),
152153
Bytes(Vec<u8>),
@@ -161,14 +162,39 @@ pub enum Value {
161162
},
162163
}
163164

165+
impl PartialEq for Value {
166+
fn eq(&self, other: &Self) -> bool {
167+
match (self, other) {
168+
(Value::Null, Value::Null) => true,
169+
(Value::Bool(a), Value::Bool(b)) => a == b,
170+
(Value::Int(a), Value::Int(b)) => a == b,
171+
(Value::Uint(a), Value::Uint(b)) => a == b,
172+
(Value::FloatBits(a), Value::FloatBits(b)) => {
173+
canonicalize_f64_bits(*a) == canonicalize_f64_bits(*b)
174+
}
175+
(Value::Str(a), Value::Str(b)) => a == b,
176+
(Value::Bytes(a), Value::Bytes(b)) => a == b,
177+
(Value::Array(a), Value::Array(b)) => a == b,
178+
(Value::Map(a), Value::Map(b)) => a == b,
179+
(
180+
Value::TensorRef { arena_idx: a1, dtype: d1, dims: ds1, space: s1 },
181+
Value::TensorRef { arena_idx: a2, dtype: d2, dims: ds2, space: s2 },
182+
) => a1 == a2 && d1 == d2 && ds1 == ds2 && s1 == s2,
183+
_ => false,
184+
}
185+
}
186+
}
187+
188+
impl Eq for Value {}
189+
164190
impl Value {
165191
pub fn from_f64(x: f64) -> Self {
166192
Self::FloatBits(canonicalize_f64_bits(x.to_bits()))
167193
}
168194

169195
pub fn as_f64(&self) -> Option<f64> {
170196
match self {
171-
Value::FloatBits(b) => Some(f64::from_bits(*b)),
197+
Value::FloatBits(b) => Some(f64::from_bits(canonicalize_f64_bits(*b))),
172198
_ => None,
173199
}
174200
}
@@ -1139,6 +1165,7 @@ fn parse_json_impl(input: &str, promote_sentinels: bool) -> Result<Value, Cowrie
11391165
b: &'a [u8],
11401166
i: usize,
11411167
promote_sentinels: bool,
1168+
used_bytes: usize,
11421169
}
11431170
impl<'a> Parser<'a> {
11441171
fn ws(&mut self) {
@@ -1154,6 +1181,7 @@ fn parse_json_impl(input: &str, promote_sentinels: bool) -> Result<Value, Cowrie
11541181
if self.i >= self.b.len() {
11551182
return Err(CowrieError::Truncated);
11561183
}
1184+
charge_budget(&mut self.used_bytes, 32)?;
11571185
match self.b[self.i] {
11581186
b'{' => {
11591187
self.i += 1;
@@ -1167,6 +1195,10 @@ fn parse_json_impl(input: &str, promote_sentinels: bool) -> Result<Value, Cowrie
11671195
loop {
11681196
self.ws();
11691197
let k = self.pstr()?;
1198+
if k.len() > MAX_KEY_LEN {
1199+
return Err(CowrieError::AllocLimit);
1200+
}
1201+
charge_budget(&mut self.used_bytes, 32)?;
11701202
if let Some(ref mut set) = seen {
11711203
if !set.insert(k.clone()) {
11721204
return Err(CowrieError::DuplicateKey);
@@ -1311,14 +1343,14 @@ fn parse_json_impl(input: &str, promote_sentinels: bool) -> Result<Value, Cowrie
13111343
return Err(CowrieError::Truncated);
13121344
}
13131345
match self.b[self.i] {
1314-
b'"' => s.push('"'),
1315-
b'\\' => s.push('\\'),
1316-
b'/' => s.push('/'),
1317-
b'b' => s.push('\u{0008}'),
1318-
b'f' => s.push('\u{000C}'),
1319-
b'n' => s.push('\n'),
1320-
b'r' => s.push('\r'),
1321-
b't' => s.push('\t'),
1346+
b'"' => { charge_budget(&mut self.used_bytes, 1)?; s.push('"'); }
1347+
b'\\' => { charge_budget(&mut self.used_bytes, 1)?; s.push('\\'); }
1348+
b'/' => { charge_budget(&mut self.used_bytes, 1)?; s.push('/'); }
1349+
b'b' => { charge_budget(&mut self.used_bytes, 1)?; s.push('\u{0008}'); }
1350+
b'f' => { charge_budget(&mut self.used_bytes, 1)?; s.push('\u{000C}'); }
1351+
b'n' => { charge_budget(&mut self.used_bytes, 1)?; s.push('\n'); }
1352+
b'r' => { charge_budget(&mut self.used_bytes, 1)?; s.push('\r'); }
1353+
b't' => { charge_budget(&mut self.used_bytes, 1)?; s.push('\t'); }
13221354
b'u' => {
13231355
if self.i + 5 > self.b.len() {
13241356
return Err(CowrieError::Truncated);
@@ -1353,9 +1385,13 @@ fn parse_json_impl(input: &str, promote_sentinels: bool) -> Result<Value, Cowrie
13531385
}
13541386
self.i += 6;
13551387
let full = 0x10000 + (((cp - 0xD800) << 10) | (cp2 - 0xDC00));
1356-
s.push(char::from_u32(full).ok_or(CowrieError::InvalidJson)?);
1388+
let ch = char::from_u32(full).ok_or(CowrieError::InvalidJson)?;
1389+
charge_budget(&mut self.used_bytes, ch.len_utf8())?;
1390+
s.push(ch);
13571391
} else {
1358-
s.push(char::from_u32(cp).ok_or(CowrieError::InvalidJson)?);
1392+
let ch = char::from_u32(cp).ok_or(CowrieError::InvalidJson)?;
1393+
charge_budget(&mut self.used_bytes, ch.len_utf8())?;
1394+
s.push(ch);
13591395
}
13601396
}
13611397
_ => return Err(CowrieError::InvalidJson),
@@ -1371,6 +1407,7 @@ fn parse_json_impl(input: &str, promote_sentinels: bool) -> Result<Value, Cowrie
13711407
}
13721408
let chunk = std::str::from_utf8(&self.b[st..self.i])
13731409
.map_err(|_| CowrieError::InvalidUtf8)?;
1410+
charge_budget(&mut self.used_bytes, chunk.len())?;
13741411
s.push_str(chunk);
13751412
}
13761413
}
@@ -1386,6 +1423,7 @@ fn parse_json_impl(input: &str, promote_sentinels: bool) -> Result<Value, Cowrie
13861423
b: input.as_bytes(),
13871424
i: 0,
13881425
promote_sentinels,
1426+
used_bytes: 0,
13891427
};
13901428
let val = p.parse(0, true)?;
13911429
p.ws();
@@ -1458,7 +1496,7 @@ fn value_to_json_impl(v: &Value, lossless_sentinels: bool) -> String {
14581496
Value::Int(n) => out.push_str(&n.to_string()),
14591497
Value::Uint(u) => out.push_str(&u.to_string()),
14601498
Value::FloatBits(bits) => {
1461-
let f = f64::from_bits(*bits);
1499+
let f = f64::from_bits(canonicalize_f64_bits(*bits));
14621500
if f.is_finite() {
14631501
let s = format!("{}", f);
14641502
out.push_str(&s);

0 commit comments

Comments
 (0)