diff --git a/fixtures/semgrep-classes/memory-bounds.cpp b/fixtures/semgrep-classes/memory-bounds.cpp index ae016f9a..1e7f8aec 100644 --- a/fixtures/semgrep-classes/memory-bounds.cpp +++ b/fixtures/semgrep-classes/memory-bounds.cpp @@ -1,6 +1,7 @@ // Annotated fixture for manifests/semgrep-classes/memory-bounds.yaml. #include #include +#include #include #include @@ -44,3 +45,26 @@ const uint8_t* sliceSafe(const uint8_t* data, size_t length, uint64_t offset, ui } return data + offset; } + +// A device-style buffer reached through a struct member: the shape a caller fills, where the +// contract "the caller validates" is what goes wrong. This is the Hercules write-exit defect. +struct Device { + uint8_t* buf; + size_t bufoffhi; +}; + +void copyUnchecked(Device* dev, size_t off, const uint8_t* src, size_t len) { + // ruleid: cw-unchecked-copy-into-member-buffer + memcpy(dev->buf + off, src, len); +} + +// A check in the enclosing function, so the copy is not this rule's business — even though the +// check itself is the wrapping form, which is the OTHER rule's business. +void copyChecked(Device* dev, size_t off, const uint8_t* src, size_t len) { + // ruleid: cw-bounds-check-sum-overflow + if (off + len > dev->bufoffhi) { + throw std::range_error("out of bounds"); + } + // ok: cw-unchecked-copy-into-member-buffer + memcpy(dev->buf + off, src, len); +} diff --git a/manifests/semgrep-classes/memory-bounds.yaml b/manifests/semgrep-classes/memory-bounds.yaml index 5cad3ef3..b3cf0b51 100644 --- a/manifests/semgrep-classes/memory-bounds.yaml +++ b/manifests/semgrep-classes/memory-bounds.yaml @@ -71,3 +71,58 @@ rules: - metavariable-regex: metavariable: $SZ regex: '(?i)^(?:[a-z_]*_)?(size|len|length|count|nbytes|bytes|sizeinbytes|width)$' + + # The companion to cw-bounds-check-sum-overflow, and the reason it exists: that rule matches an + # INSUFFICIENT check (a sum that can wrap), so a function with NO check at all has no sum to match + # and reads as clean. Found on SDL Hercules-390 (2026-11-29): 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 on the copy. + # + # Scoped to a struct member destination on purpose. A local array is the registry's + # unbounded-copy-to-stack-buffer; a member buffer is the device/context buffer that a caller + # fills, which is the shape that goes unchecked when the contract is "the caller validates". + # Measured on the Hercules tree: 6 hits, 3 of them the real defect — including two compressed-FBA + # exits the sum rule was silent on — 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. + - id: cw-unchecked-copy-into-member-buffer + languages: [c, cpp] + severity: WARNING + message: >- + A copy writes into a struct member buffer at a variable offset with a variable length, and no + comparison of offset + length against a size appears in the enclosing function. If the offset + or the length reaches this function from outside it, the copy runs past the buffer. Validate + both against the buffer's length before the copy, or take the length from the buffer rather + than from the caller. A guard written as a clamp (a ternary, a min) or a capacity call is not + seen by this rule, so a hit here is a prompt to read the function, not a verdict. + metadata: + category: security + subcategory: [audit] + cwe: ["CWE-787: Out-of-bounds Write", "CWE-120: Buffer Copy without Checking Size of Input"] + confidence: LOW + class: unchecked-copy-into-member-buffer + patterns: + - pattern-either: + - pattern: memcpy($OBJ->$FIELD + $OFF, $SRC, $LEN) + - pattern: memmove($OBJ->$FIELD + $OFF, $SRC, $LEN) + # A constant length cannot be attacker-sized, and it is the shape of the fixed-field copies + # that dominate an emulator's I/O paths. Requiring a lowercase identifier keeps them out. + - metavariable-regex: + metavariable: $LEN + regex: '^[a-z_][a-z0-9_]*$' + # Every direction the guard can be written in, since a check that exists in the other + # polarity is still a check. `sizeof` is included because that is the safe form. + - pattern-not-inside: | + if ($OFF + $LEN > $SIZE) { ... } + ... + - pattern-not-inside: | + if ($OFF + $LEN >= $SIZE) { ... } + ... + - pattern-not-inside: | + if ($SIZE < $OFF + $LEN) { ... } + ... + - pattern-not-inside: | + if ($SIZE <= $OFF + $LEN) { ... } + ... + - pattern-not-inside: | + if ($OFF + $LEN <= sizeof(...)) { ... } + ... diff --git a/package.json b/package.json index b28543df..7cccc60d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "commitwork", - "version": "0.6.346", + "version": "0.6.347", "description": "Repository security scanner, local CI runner and scheduled monitor, with remediation and compliance-evidence tooling. Manifest-driven; runs against any git repository.", "homepage": "https://www.commitwork.online", "repository": {