-
Can you help get me closer to a rule that points directly at the source of an error? Background
This is based on a real-world investigation into an alert from Likely Bugs/Memory Management/StackAddressEscapes.ql. In alerts generated by that rule, the function that is pointed to as potentially erroneous is the function that is actually assigning the function's parameter (which at least one caller passed a stack address for). Unfortunately, the project is mature, so the function's definition will not be changed. Thus, I want to find the ultimate source ... which code caused a stack pointer to actually be used? For this project, there are three functions, each of which takes a Preferably, an alert would be generated that pinpoints the source location that causes the use of the stack location for these parameters. (see second question.) Specific depot / functions
In particular, this alert fires in the TinyUSB project. To start, I want to add alerts for any caller of the following three functions, where the caller provides a stack location as the 3rd parameter (
This is because, unless additional checks are done to wait for the completion of the request, such callers would invite stack corruption (as noted in StackAddressEscapes.ql). First-pass at Query (for review)
This first draft matches when the buffer pointer is stored in the stack. I'm looking for it to match when the pointer itself points to a stack location....
Question:
... ultimate source example ...
For example: //
static struct { void* forAsyncProcessing; } s_bufferToProcess;
bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) {
// validates things, then a line that willl cause alert from StackAddressEscapes.ql
s_bufferToProcess.forAsyncProcessing = buffer;
}
bool usbd_edpt_xfer_wrapper(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) {
// does some stuff... and then calls the function mentioned in the draft new rule
usbd_edpt_xfer(rhport, ep_addr, buffer, total_bytes);
}
static uint8_t static_buffer[10];
void foo() {
// this should not fire
usbd_edpt_xfer_wrapper(0, 0, &(static_buffer[0]), 10);
}
void bar() {
uint8_t stack_buffer[10];
// this should cause the rule to fire, pointing to next line as problem
usbd_edpt_xfer_wrapper(0, 0, &(stack_buffer[0]), 10);
} I feel close ... the query is finding callers who first store the pointer on the stack (vs. the pointed-to-location being on stack), and it feels like I just need a nudge in the right direction. Any help improving these and getting them to do the right thing is greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Does |
Beta Was this translation helpful? Give feedback.
How about this, which uses a
path-problem
query, a global dataflow configuration and the, true
qualifier that limitsstackPointerFlowsToUse
to highlighting local problems: