Skip to content

feat(manifests): catch an unchecked copy into a member buffer - #21

Merged
Portll merged 1 commit into
mainfrom
fix/cw-unchecked-copy-detector-20261206
Dec 6, 2026
Merged

Portll merged 1 commit into
mainfrom
fix/cw-unchecked-copy-detector-20261206

Conversation

@Portll

@Portll Portll commented Dec 6, 2026 •

Copy link
Copy Markdown
Owner

Why

cw-bounds-check-sum-overflow matches an insufficient check — a sum that can wrap. A function with no check at all has no sum to match, so it reads as clean. That is a silent clean over the exact class the rule was written for.

Found while scanning SDL Hercules-390 (develop @ 8ab5c3f1). Four DASD write exits copy a client-supplied offset and length into a fixed device buffer:

Exit Check Sum rule This rule
ckd_dasd_update_track (ckddasd.c:1234) off + len > bufoffhi fires quiet (a check exists)
fba_dasd_update_blkgrp (fbadasd.c:766) none fires at :771 — the update-range line, not the copy fires
cfba_write_block (cckddasd.c:1158) none silent fires
cfba64_write_block (cckddasd64.c:888) none silent fires

The two compressed-FBA exits are the gap: no check, so nothing for the sum rule to match. cfba64_write_block is the same defect in code with no callers — declared in cckddasd.h and hexterns.h, never wired into a handler table. A real defect that is not reachable today, which is why the rule firing on it is correct rather than alarming.

What it does

cw-unchecked-copy-into-member-buffer — a memcpy/memmove into a struct member buffer at a variable offset with a variable length, where no comparison of offset + length against a size appears in the enclosing function.

Three deliberate narrowings:

  • Member destination only. A local array is the registry's unbounded-copy-to-stack-buffer. A member buffer is the device/context buffer a caller fills — the shape that goes unchecked when the contract is "the caller validates".
  • Variable length only. A constant length cannot be attacker-sized, and fixed-field copies dominate an emulator's I/O paths.
  • All guard polarities. >, >=, <, <= and sizeof are each excluded, because a check written the other way round is still a check.

Precision, measured

On the Hercules tree (201 .c / 155 .h, ~436k lines): 6 hits, 3 of them the real defect.

  • True positives: cckddasd.c:1158, cckddasd64.c:888 (dead code), fbadasd.c:766
  • Review-worthy: dasdload2.h:2468 — the guard bounds dlen vs nllen, not offset + nllen vs the buffer
  • False positives: wscnsl.c:139 (length clamped by j = 64 - i), wscnsl.c:700 (guarded by an ensure_buf capacity call)

Both false positives are guards this rule cannot see — a clamp and a capacity call rather than a comparison. That is why it is confidence: LOW and why the message says a hit is a prompt to read the function, not a verdict.

Tests

monitor/test/semgrep-classes.test.mjs — both cases pass:

✔ every rule is a pattern rule with a class and a CWE, and every rule has a fixture case both ways
✔ the fixtures match exactly: no annotated case missed, none matched against its annotation, nothing unannotated

The fixture pairs an unguarded copy (ruleid) with a guarded one (ok). The guarded case's check is the wrapping form, so that line carries a ruleid: cw-bounds-check-sum-overflow — the two rules doing their separate jobs on one function.

Caveat worth knowing

Semgrep's C parser is unreliable on this codebase: 92 errors over the tree, including 15 outright syntax errors and partial parses. Under load its default per-file timeout also drops hits — cckddasd.c:1158 disappeared at the default timeout and reappeared at --timeout 120. A file that times out or fails to parse yields no findings, which is the same silent-clean class this rule exists to close. Not fixed here; recorded so the next reader does not read a short result list as a clean tree.

Refs: cw-hercules-scan-20261129 F6

@Portll
Portll force-pushed the fix/cw-unchecked-copy-detector-20261206 branch from 0ee6b58 to c6f3929 Compare December 6, 2026 06:36
cw-bounds-check-sum-overflow matches an insufficient check. A function with
no check at all has no sum to match, and reads as clean. On SDL
Hercules-390 three write exits copied a client-supplied offset and length
into a fixed device buffer. The sum rule was silent on two of them, and on
the third it fired on the update-range line rather than the copy.

Scoped to a struct member destination: a local array is the registry's
unbounded-copy-to-stack-buffer, while a member buffer is the device or
context buffer a caller fills, which is the shape that goes unchecked when
the contract is "the caller validates". A constant length is excluded
because it cannot be attacker-sized.

Measured on the Hercules tree: 6 hits, 3 of them the real defect, and 3
bounded copies whose guard is a clamp or a capacity call rather than a
comparison. LOW confidence is the honest reading of that ratio. The message
says a hit is a prompt to read the function, not a verdict.

Refs: cw-hercules-scan-20261129 F6
@Portll
Portll force-pushed the fix/cw-unchecked-copy-detector-20261206 branch from c6f3929 to 66586c2 Compare December 6, 2026 06:39
@Portll
Portll merged commit 3aea2b7 into main Dec 6, 2026
35 checks passed
@Portll
Portll deleted the fix/cw-unchecked-copy-detector-20261206 branch December 6, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant