| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
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. |
Sorry, something went wrong.
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.
Sorry, something went wrong.
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>
|
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! |
Sorry, something went wrong.
|
QHelp 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 READ_REG or GET_MMIO. When those lengths are not validated against the destination buffer size, an attacker who can influence hardware registers or DMA metadata can trigger buffer overflows. RecommendationAlways validate MMIO/DMA-derived lengths before passing them to memcpy, memmove, or strncpy. Compare against a compile-time maximum and reject or clamp out-of-range values before copying. ExampleBad: length from an MMIO register used directly as the copy size. #define READ_REG(addr) (*(volatile unsigned int *)(addr))
#define MAX_DMA_LEN 64
void *memcpy(void *dest, const void *src, unsigned long n);
void bad_mmio_memcpy(char *dst, char *src) {
unsigned int len = READ_REG(0x40001000);
memcpy(dst, src, len);
}Good: defensive bounds check before the copy. #define READ_REG(addr) (*(volatile unsigned int *)(addr))
#define MAX_DMA_LEN 64
void *memcpy(void *dest, const void *src, unsigned long n);
void good_mmio_memcpy(char *dst, char *src) {
unsigned int len = READ_REG(0x40001000);
if (len <= MAX_DMA_LEN)
memcpy(dst, src, len);
}References |
Sorry, something went wrong.
|
ql/cpp/ql/integration-tests/query-suite/test.py::test_not_included_queries fails because the new query has not been added to the test results. |
Sorry, something went wrong.
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>
|
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! |
Sorry, something went wrong.
|
I'm now seeing the following failure: File "ql/cpp/ql/test/experimental/query-tests/Security/CWE/CWE-120/MmioUnsanitizedMemcpy/test.c" contains a non-ASCII character at the location marked with `|` in:
len = READ_REG(0x40001000);
memcpy(dst, src, 32); // GOOD |
ASCII check failed!
|
Sorry, something went wrong.
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>
|
Thanks @jketema 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. |
Sorry, something went wrong.
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.
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.
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.
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.
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.
There was a problem hiding this comment.
Hi @Tito0015,
Thank you for fixing the various issues that have come up here - I think we're close to merging now. The query as it stands now seems reasonably designed, behaves well in practice, and the checks now pass (🎉). I do have a couple of tiny nitpicks.
Sorry, something went wrong.
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.
|
Addressed both nits (removed manual CWE entries from .qhelp and cleaned up the test.c header). Rebased cleanly onto latest main. Ready for final review! |
Sorry, something went wrong.
There was a problem hiding this comment.
All LGTM now. Thanks again for fixing the various nits.
And thank you for your contribution to CPP analysis! 🎉 😃 🚀
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Summary
Adds a new security query cpp/mmio-unsanitized-memcpy targeting 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
Verification & Test Results
Checklist