Help: how to ensure a secure PointerFieldAccess chain #16842
-
Example source code:
About this C source:
Programmers here used to make assumptions about a raw packet buffer, i.e., trusting the cast, but this gives the attacker a chance to construct malformed packet to crash our application, explained below: In this example,
And this indeed caused a few crashes in our app, so I wanted to model the issue:
For example: (this is good)
(this is bad)
And here is my attempt:
I thought this is what I did:
The script indeed detects those unsafe calls to
Yes, there is an And then there is the uninitialized What I am missing here? Any help will be appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi @mindcrunch4u, I'm not sure if you really need a taint-tracking configuration for your query. The following snippet (which just uses a /**
* @kind problem
*/
import semmle.code.cpp.controlflow.Guards
import semmle.code.cpp.ir.ValueNumbering
/** Holds if `instr` is guarded to be non-null */
predicate isNullGuarded(Instruction instr) {
any(IRGuardCondition gc).ensuresEq(valueNumber(instr).getAUse(), 0, instr.getBlock(), false)
}
from LoadInstruction load, Instruction qualifier, PointerFieldAccess pfa
where
// The load is from a load of a pointer field access
load.getUnconvertedResultExpression() = pfa and
qualifier.getUnconvertedResultExpression() = pfa.getQualifier() and
not isNullGuarded(qualifier)
select pfa, "This dereference is not guarded." Perhaps you can use this as inspiration? Note that I didn't limit the query to be locally created pointers, but I'm sure you can adjust the query to only look at those. Note that I used the C++ intermediate representation (IR) for the above query. This is because the basic blocks we have at the AST level don't properly support guards like: if(s1 && s1->p) {
...
} When working at the IR level we can see that I hope this helps! If I've misunderstood anything about your problem feel free to let me know and I'm sure we can work something out :) |
Beta Was this translation helpful? Give feedback.
-
Hello @MathiasVP ! Your script looks very promising! But after fixing the
But I believe IRGuardCondition supports Another question if you don't mind: I feel like the official guide isn't introducing some parts of CodeQL, such as the Wish you a good day! |
Beta Was this translation helpful? Give feedback.
Hi @mindcrunch4u,
I'm not sure if you really need a taint-tracking configuration for your query. The following snippet (which just uses a
GuardCondition
) detects all the problematic cases in your example: