Skip to content
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

MEM53-CPP: False positive due to flow through realloc #420

Open
MathiasVP opened this issue Oct 31, 2023 · 1 comment
Open

MEM53-CPP: False positive due to flow through realloc #420

MathiasVP opened this issue Oct 31, 2023 · 1 comment
Labels
Difficulty-Medium A false positive or false negative report which is expected to take 1-5 days effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Low Stardard-CERT-C++

Comments

@MathiasVP
Copy link
Contributor

MathiasVP commented Oct 31, 2023

Affected rules

  • MEM53-CPP

Description

In github/codeql#14637 we added taint-flow through the indirection of the pointer passed to realloc to the indirection of the result. That is, flow through the following example:

int* p = ...;
*p = tainted_value;
int* q = (int*)realloc(p, 1024);
sink(*p);

this relies on the new taint-tracking library to distinguish between the result of realloc(...), and the result of what realloc(...) points to. Since the old AST-based taint-tracking library cannot do this this results in a FP in the testcases for MEM53-CPP (that we accepted on the next branch here: #419)

The query already tries to rule out realloc cases by excluding them in the definition of the taint-tracking configuration's isSource, but to get this query back to not reporting a FP here a barrier on realloc would have to be inserted.

As @jketema points out the affected test is actually really sketchy since there’s no guarantee that memory allocated with new can safely be realloc'ed. So maybe this scenario should be thought about more carefully by someone on your team.

@MathiasVP MathiasVP added the false positive/false negative An issue related to observed false positives or false negatives. label Oct 31, 2023
@lcartey lcartey added Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address Impact-Low Difficulty-Medium A false positive or false negative report which is expected to take 1-5 days effort to address and removed Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address labels Oct 18, 2024
@lcartey
Copy link
Collaborator

lcartey commented Oct 18, 2024

Thanks for the report! I recently merged this change from next into main, and modified the query to apply realloc as a barrier, which makes the behaviour consistent with how the query worked before github/codeql#14637.

I believe the current test case for realloc should use malloc for the allocation initially, before using placement new. Then the test case wouldn't trigger undefined behaviour.

In addition, we should consider whether we want to cover any of the realloc cases under this rule. I think a common cases would be an array of objects that is being expanded. Do we then expect to see a constructor call for each new instance added in this case, and flag otherwise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty-Medium A false positive or false negative report which is expected to take 1-5 days effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Low Stardard-CERT-C++
Projects
Development

No branches or pull requests

3 participants