Skip to content

CPP: Fix some use after free FPs. #14275

Merged
MathiasVP merged 4 commits intogithub:mainfrom
alexet:fix-use-after-free-fp
Oct 3, 2023
Merged

CPP: Fix some use after free FPs. #14275
MathiasVP merged 4 commits intogithub:mainfrom
alexet:fix-use-after-free-fp

Conversation

@alexet
Copy link
Copy Markdown
Contributor

@alexet alexet commented Sep 20, 2023

We try and exclude source expressions that are frees themselves however if there are more than one expresssion for a node we may use one expression to detect it is a use but not eliminate it due to the other expression.

The dataflow node for a = b has two expressions (a = b and b). a = b is a function argument to an unknown function so we assume that it is used. b is not an argument to free. By using different expression we incorrectly work out that the node is a non-free use.

@github-actions github-actions Bot added the C++ label Sep 20, 2023
@alexet alexet marked this pull request as ready for review September 26, 2023 16:55
@alexet alexet requested a review from a team as a code owner September 26, 2023 16:55
@alexet alexet added the no-change-note-required This PR does not need a change note label Sep 26, 2023
@jketema
Copy link
Copy Markdown
Contributor

jketema commented Sep 27, 2023

I'm wondering if this fix will still work after the frontend upgrade, because the IR (and the expressions yielded) will be different for these kinds of assignments these. Let me check that.

@jketema
Copy link
Copy Markdown
Contributor

jketema commented Sep 27, 2023

I'm wondering if this fix will still work after the frontend upgrade, because the IR (and the expressions yielded) will be different for these kinds of assignments these. Let me check that.

The fix still woks, but no longer seems needed after the frontend upgrade.

void test_free_malloc() {
void *a = malloc(10);
void *b;
free(b = a);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs some kind of // GOOD or // BAD comment. However, it's not clear to me to which query being tested here the comment applies.

Comment thread cpp/ql/src/Critical/UseAfterFree.ql Outdated
predicate isUse0(DataFlow::Node n, Expr e) {
e = n.asExpr() and
not isFree(_, e, _) and
not isFree(n, _, _) and
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me a bit nervous. because isFree looks a (un)converted expressions explicitly. Was the change just made to bind n, or is there a deeper reason? If the former, can't we just drop the node argument of isUse0 completely?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change was leftover from an earlier attempt which I accidentality left in. I have now changed it to not have a DataFlow::Node argumebnt at all and call asExpr at call sites.

@alexet alexet force-pushed the fix-use-after-free-fp branch from d51c757 to 6b0ae0f Compare September 28, 2023 16:53
@jketema
Copy link
Copy Markdown
Contributor

jketema commented Sep 29, 2023

Thanks for the fixes. Your latest DCA experiment seemed to have failed hard. Would you mind kicking off a new one? Otherwise, this LGTM.

Kicked off a new DCA experiment myself.

Copy link
Copy Markdown
Contributor

@jketema jketema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DCA LGTM.

@MathiasVP MathiasVP merged commit 5632dd5 into github:main Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants