Uh oh!
There was an error while loading. Please reload this page.
cpp: Add 'cpp/mmio-unsanitized-memcpy' query - #22438
Conversation
Hi @github/codeql-cpp just checking in on this query submission! The query adds MMIO/DMA-to-memcpy bounds modeling for embedded C/C++ drivers, complete with unit tests and QL documentation. Let me know whenever the team has a moment to review or trigger CI. |
There was a problem hiding this comment.
Hi, I'll try to find a reviewer for you. However, this query has very low quality (see below), not what we would consider "medium". Hence, at the very least it should be moved into the directory for experimental queries.
I ran the query on about 1000 databases, and most of the results seem unrelated to memory mapped I/O and look more cases where volatile is used for other (incorrect) reasons.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Relocate the query under experimental/, narrow sources to allowlisted MMIO register macros only, drop the security-extended suite include, and update tests and change notes for maintainer feedback on PR github#22438. Co-authored-by: Cursor <cursoragent@cursor.com>
Tito0015
commented
Sep 2, 2026
Hi @jketema — thank you again for the feedback and for running this across the DB corpus! You were spot on regarding the generic volatile False Positive trap. I have updated the PR with the following changes:
Let me know if this updated AST modeling looks ready for the experimental queue! |
Uh oh!
There was an error while loading. Please reload this page.
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added a new experimental query, `cpp/experimental/mmio-unsanitized-memcpy`, to detect memory copy operations whose size argument is derived from allowlisted MMIO/DMA register-read macros without sufficient bounds validation. |
There was a problem hiding this comment.
We don't publish change notes for experimental queries.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
971e182 to
5c92f5eCompareQHelp previews: cpp/ql/src/experimental/Security/CWE/CWE-120/MmioUnsanitizedMemcpy.qhelpMMIO/DMA unsanitized memory copyFirmware and embedded drivers often copy data into buffers using lengths read from allowlisted MMIO register macros such as RecommendationAlways validate MMIO/DMA-derived lengths before passing them to ExampleBad: length from an MMIO register used directly as the copy size. #defineREAD_REG(addr) (*(volatile unsigned int *)(addr))
#defineMAX_DMA_LEN 64
void*memcpy(void*dest, constvoid*src, unsigned longn);
voidbad_mmio_memcpy(char*dst, char*src) {
unsigned intlen=READ_REG(0x40001000);
memcpy(dst, src, len);
}Good: defensive bounds check before the copy. #defineREAD_REG(addr) (*(volatile unsigned int *)(addr))
#defineMAX_DMA_LEN 64
void*memcpy(void*dest, constvoid*src, unsigned longn);
voidgood_mmio_memcpy(char*dst, char*src) {
unsigned intlen=READ_REG(0x40001000);
if (len <= MAX_DMA_LEN)
memcpy(dst, src, len);
}References |
jketema
commented
Sep 3, 2026
|
Uh oh!
There was an error while loading. Please reload this page.
5c92f5e to
9ad7072CompareRelocate the query under experimental/, narrow sources to allowlisted MMIO register macros only, drop the security-extended suite include, and update tests and change notes for maintainer feedback on PR github#22438. Co-authored-by: Cursor <cursoragent@cursor.com>
Tito0015
commented
Sep 4, 2026
Hi @jketema — updated the query to use sink.getNode() as the alert location, updated the test .expected files, and added the new query path to not_included_in_qls.expected. Rebased on main. Thanks! |
I'm now seeing the following failure: |
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Relocate the query under experimental/, narrow sources to allowlisted MMIO register macros only, drop the security-extended suite include, and update tests and change notes for maintainer feedback on PR github#22438. Co-authored-by: Cursor <cursoragent@cursor.com>
9ad7072 to
b858cfdCompareTito0015
commented
Sep 4, 2026
Thanks for catching that! Fixed the em dash comment in test.c to use ASCII-only text and rebased against upstream/main. Ready for a fresh CI pass. |
Summary
Adds a new security query
cpp/mmio-unsanitized-memcpytargeting unsanitized memory copy operations (memcpy,memmove,strncpy) where size parameters derive directly from hardware registers (MMIO/DMA) without relational bounds checks.Motivation & Domain Context
Standard buffer overflow queries (
UnboundedWrite.ql,OverrunWrite.ql) model user-space strings and generic memory ops, but do not model volatile register macro reads (READ_REG,GET_MMIO) commonly found in microcontroller drivers, RTOS kernels, and embedded hardware stacks. This query fills a gap for embedded C/C++ static analysis.Query Design & Architecture
DataFlow::ConfigSigwithTaintTracking::Global.READ_REG,GET_MMIO,REG_READ,DMA_READ).memcpy,memmove,strncpy,wmemcpy,wmemmove.IRGuardsviaDataFlow::BarrierGuard<lessThanOrEqual/3>to recognizeif (len <= MAX)conditions and prevent false positives.lessThanOrEqualuses the publicOperand+getConvertedResultExpression()pattern.cpp,TaintTracking,IRGuards). Zerointernal./DataFlowImplCommondependencies.Verification & Test Results
codeql test run cpp/ql/test/query-tests/Security/CWE/CWE-120/MmioUnsanitizedMemcpy/All 1 tests passed(3 positive alerts, 3 false-positive barrier test cases clean).codeql generate query-helppassed DTD verification and rendered clean markdown.includeofcpp/mmio-unsanitized-memcpyincpp-security-extended.qls.Checklist
@kind path-problem,@precision medium,@security-severity 8.6)..qhelpfile provided with valid DTD structure and Bad/Good examples..qlref,test.c, and verified.expectedoutput..qlssuite.internal.module imports used.