Sitelet https://web.archive.org/web/20260330101142/https://github.com/github/codeql/pull/6335
Skip to content

C++: Improvements to the cpp/toctou-race-condition query#6335

Merged
rdmarsh2 merged 10 commits intogithub:mainfrom
geoffw0:toctou2
Jul 22, 2021
Merged

C++: Improvements to the cpp/toctou-race-condition query#6335
rdmarsh2 merged 10 commits intogithub:mainfrom
geoffw0:toctou2

Conversation

@geoffw0
Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 commented Jul 20, 2021

Improvements to the cpp/toctou-race-condition query. The main change is disallowing results from file operations such as fopen, rename etc from counting as the "check", so now the check must be output from a stat, access or similar call, whilst the "use" can still be a wide range of file operations. Most of the results this removes are either clear FPs or of dubious interest, so this is a big improvement to precision.

I've made a couple of other improvements that widen the results a little (not as much as the above narrows them), in hopefully uncontroversial ways.

  • fix the logic for use of stat arguments so that it actually works in most cases.
  • recognize _wfsopen.
  • recognize the second argument of rename (some implementations will overwrite an existing file).

LGTM diff query results here: https://lgtm.com/query/4593284742579958544/

I haven't upgraded the precision of this query to high as yet, but I'm thinking about it. The main remaining class of FP / weak result IMO is where stat is being used to check a file exists before doing something that would perhaps fail if it didn't; or stat is checking another not-so-security-relevant property. These are still race conditions, but not necessarily security issues, so I think I'd rather not report them but I don't have a strong opinion. On the flip side, this is pretty much what the examples on https://cwe.mitre.org/data/definitions/367.html do and what the SAMATE tests do, so perhaps I'm being too strict.

@geoffw0 geoffw0 added the C++ label Jul 20, 2021
@geoffw0 geoffw0 requested a review from a team as a code owner July 20, 2021 11:04
@geoffw0
Copy link
Copy Markdown
Contributor Author

geoffw0 commented Jul 20, 2021

Here are a couple of real world toctou CVEs:

The attacks often involve the program checking some security relevant property of the file (such as the user having permission to access it), then the attacker switches that file for a symlink to the real target, then the program operates on that target using its privilege but without having checked the property on it.

@geoffw0
Copy link
Copy Markdown
Contributor Author

geoffw0 commented Jul 20, 2021

Hmm, there are some regressions in the old internal tests. I'll make a PR to accept the changes, but I'm beginning to think there's a second case we really want to permit as well: any check, followed by a highly sensitive operation such as chmod. The motivating case is an fopen(filename, "w") followed by granting a permission on that (believed to be newly written) file using chmod. I can imagine other combinations such as rename followed by chown on the renamed file that would be dangerous if the file were switched with a symlink or something.

@geoffw0 geoffw0 added the depends on internal PR This PR should only be merged in sync with an internal Semmle PR label Jul 20, 2021
@geoffw0
Copy link
Copy Markdown
Contributor Author

geoffw0 commented Jul 20, 2021

Pushed changes and internal PR. New diff query: https://lgtm.com/query/6665823716934953527/ . There aren't many changes, but we get back what I believe are good results in a few places such as stellar/stellar-core.

@rdmarsh2 rdmarsh2 self-assigned this Jul 20, 2021
Copy link
Copy Markdown
Contributor

@rdmarsh2 rdmarsh2 left a comment

Choose a reason for hiding this comment

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

Results and code LGTM other than the one nitpick.

Copy link
Copy Markdown
Contributor

@rdmarsh2 rdmarsh2 left a comment

Choose a reason for hiding this comment

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

LGTM, and I'm merging now to avoid submodule merge conflicts. Do we want to bump the precision to high as a separate PR?

@rdmarsh2 rdmarsh2 merged commit 0e9d36b into github:main Jul 22, 2021
@geoffw0
Copy link
Copy Markdown
Contributor Author

geoffw0 commented Jul 26, 2021

Do we want to bump the precision to high as a separate PR?

I'll review the results again first, but yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ depends on internal PR This PR should only be merged in sync with an internal Semmle PR documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants