-
Notifications
You must be signed in to change notification settings - Fork 247
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
Add query to detect out-of-order cpp/linux privilege dropping #37
Conversation
Very cool. I have a few scripts that can let you bulk import projects into your LGTM account so you can run this query against large numbers of projects. I see you're not in the slack channel. I recommend reaching out to the GitHub Security Lab to get an invite. I'm more than happy to share them with you there. |
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.
Thanks for your submission. This is really impressive for a first query.
I've made some suggestions about both performance and general approach, see the PR comments below.
For testing on a larger set of projects, you can use lgtm.com, which automatically builds CodeQL snapshots for a large set of open-source projects and makes them available for download or online querying through the query console. We can also run the query across all projects on lgtm.com, but that's typically one of the last steps in the process.
CodeQL_Queries/cpp/PrivilegeDropping/PrivilegeDroppingOutoforder.ql
Outdated
Show resolved
Hide resolved
CodeQL_Queries/cpp/PrivilegeDropping/PrivilegeDroppingOutoforder.ql
Outdated
Show resolved
Hide resolved
CodeQL_Queries/cpp/PrivilegeDropping/PrivilegeDroppingOutoforder.ql
Outdated
Show resolved
Hide resolved
CodeQL_Queries/cpp/PrivilegeDropping/PrivilegeDroppingOutoforder.ql
Outdated
Show resolved
Hide resolved
CodeQL_Queries/cpp/PrivilegeDropping/PrivilegeDroppingOutoforder.ql
Outdated
Show resolved
Hide resolved
// This introduces false negatives where the return is checked but then | ||
// errno == EPERM allows execution to continue. |
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.
You could potentially use semmle.code.cpp.controlflow.Guards
to require a check of errno
, but handling wrapper functions would be a bit tricky.
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 am going to pass on this for the moment. I do not think this happens often (where someone ignores the result for specific values of errno
). My goal with the comment was to highlight that this additional checking should be considered.
If we are able to run the query on a massive set of projects and find that adding a check here is worthwhile I am happy to follow up and explore adding a guard. :) (I am going to go learn more about guard implementations right now.)
Wow, thank you so much for code review @rdmarsh2! It will take me some time to apply the feedback because I’d like to dig into some of what you’re mentioning. |
Alright, I re-tested this against my sample set of projects (n=100). All of these projects use Here are some examples of the follow up
It is interesting if you take away the requirement for the result to flow into a condition. These results are more prone to false positives but are fun for code reviewing. |
Should these reports have CVE numbers assigned to them? |
IMO no unless there's precedent for documenting similar issues. But I can break the issues into specifics:
The SwayVM case falls into the last category and I can see documenting that as a CVE. I will look for precedent this weekend and we can apply consistency. |
Hey @theopolis, Let me know if you have any question |
Hi @nicowaisman,
I am not confident enough that this query should be added to the the set of main queries. I'd rather keep it in the group of queries in this repo unless someone can run it across all of the LGTM projects and comment on the false positive rate.
I was mostly interested in learning CodeQL and getting feedback on my query (which I have, and the feedback has been great!) Since this query finds issues that are focused on correctness around mitigations it feels a little below the bar for qualifying for a bounty. If you think it does I'd rather pursue the bug slayer template due to my concern above about confidence.
|
The way the program is setup, you can actually do both with the same vuln. You can use the query to find vulns in open source projects and get CVEs assigned for it (you need a minimum of 4 for Bug Slayer). That's the first BB program. Then for the second, submit it to the Semmle/ql repo for the All For One and that's a second BB submission. I'm doing that here with a query I already had accepted to the 'All-For-One' program:
|
Ok so my next step is to open a similar PR for the |
Hi folks, I am working on this query (my first!) to detect usage of
setuid
beforesetgid
,initgroups
, etc and when error checking is missing. This situation indicates an attempt to drop privileges incorrectly, and the most important part of the bug is the lack of error handling. This would effect setuid/setgid-bit executables and daemons run as root with the intention to later dropping privileges. I added inline comments about my thoughts on false positives and false negatives.I think this category of checks can be extended, for example another query can be added to find instances where
initgroups
orsetgroups
(dropping supplemental groups) are missing or where capabilities are not dropped, etc. For this reason I created a subfolder for the query, test code, and helpfile.I tried to create the small set of test cases to improve accuracy. I also browsed GitHub for about 100 projects that may be impacted, by looking for usage of
setuid
. At this point I am mildly confident this is working as intended. But this is my first time using CodeQL and I am sure it could use a lot of feedback. On my mind are: is this performance enough, am I using the QL stdlib correctly, can I simplify the logic, is there a way to test it on a larger set of projects?