Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions fixtures/semgrep-classes/memory-bounds.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Annotated fixture for manifests/semgrep-classes/memory-bounds.yaml.
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <vector>

Expand Down Expand Up @@ -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);
}
55 changes: 55 additions & 0 deletions manifests/semgrep-classes/memory-bounds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)) { ... }
...
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Loading