-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: stitch paths and ignore cast arrays in constant off-by-one query #13045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++: stitch paths and ignore cast arrays in constant off-by-one query #13045
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of comments, but otherwise this LGTM once we have a successful DCA run
cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Outdated
Show resolved
Hide resolved
cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Outdated
Show resolved
Hide resolved
cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Outdated
Show resolved
Hide resolved
cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Outdated
Show resolved
Hide resolved
FieldAddressToPointerArithmeticFlow::flowPath(fieldSource, sink) and | ||
isFieldAddressSource(f, fieldSource.getNode()) and | ||
pai.getLeft() = sink.getNode().(DataFlow::InstructionNode).asInstruction() and | ||
pai.getElementSize() = f.getUnspecifiedType().(ArrayType).getBaseType().getSize() and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should push the f.getUnspecifiedType() instanceof ArrayType
conjunct into the FieldAddressToPointerArithmeticConfig::isSource
predicate (or even better: into the isFieldAddressSource
predicate) to get a smaller isSource
predicate for the first dataflow traversal.
Thinking more about this: If we push the restriction saying that f.getUnspecifiedType()
must be an ArrayType
into the isSource
predicate we can use a flow state to remember the size of the array. Does that not mean that we would be able to handle cases like the one you added:
char *charBuf = (char*) arr->buf;
charBuf[MAX_SIZE_BYTES] = 0;
then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this new approach, but I fear there's a potential performance problem with the current code.
cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Outdated
Show resolved
Hide resolved
cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of small remaining things, but otherwise this LGTM!
cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once DCA shows that performance is fine. Although, it looks like the latest DCA run still shows some performance issues?
I've pushed two performance-related commits @rdmarsh2 to your branch. Feel free to modify them as you see fit. I'll start a DCA run to check the performance impact of them. Hopefully that's done by the time your day starts tomorrow 🤞. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This PR does two things - stitch the first and second data flow paths together to make reviewing results easier, and then fix a false positive where casts to a differently-sized type (e.g.
int[]
tochar*
) would result in false positives.