Skip to content

Commit f351916

Browse files
committed
Merge branch 'master' into testmerge
2 parents c9c469b + ca26fee commit f351916

File tree

308 files changed

+17941
-10695
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

308 files changed

+17941
-10695
lines changed

CONTRIBUTING.md

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,65 @@
11
# Contributing to CodeQL
22

3-
We welcome contributions to our standard library and standard checks. Got an idea for a new check, or how to improve an existing query? Then please go ahead and open a pull request!
3+
We welcome contributions to our CodeQL libraries and queries. Got an idea for a new check, or how to improve an existing query? Then please go ahead and open a pull request!
44

5-
Before we accept your pull request, we require that you have agreed to our Contributor License Agreement, this is not something that you need to do before you submit your pull request, but until you've done so, we will be unable to accept your contribution.
5+
There is lots of useful documentation to help you write queries, ranging from information about query file structure to tutorials for specific target languages. For more information on the documentation available, see [Writing CodeQL queries](https://help.semmle.com/QL/learn-ql/writing-queries/writing-queries.html) on [help.semmle.com](https://help.semmle.com).
66

7-
## Adding a new query
87

9-
If you have an idea for a query that you would like to share with other Semmle users, please open a pull request to add it to this repository.
10-
Follow the steps below to help other users understand what your query does, and to ensure that your query is consistent with the other Semmle queries.
8+
## Submitting a new experimental query
119

12-
1. **Consult the documentation for query writers**
10+
If you have an idea for a query that you would like to share with other CodeQL users, please open a pull request to add it to this repository. New queries start out in a `<language>/ql/src/experimental` directory, to which they can be merged when they meet the following requirements.
1311

14-
There is lots of useful documentation to help you write queries, ranging from information about query file structure to tutorials for specific target languages. For more information on the documentation available, see [Writing CodeQL queries](https://help.semmle.com/QL/learn-ql/writing-queries/writing-queries.html) on [help.semmle.com](https://help.semmle.com).
12+
1. **Directory structure**
1513

16-
2. **Format your code correctly**
14+
There are five language-specific query directories in this repository:
1715

18-
All of Semmle's standard queries and libraries are uniformly formatted for clarity and consistency, so we strongly recommend that all contributions follow the same formatting guidelines. If you use CodeQL for VS Code, you can autoformat your query in the [Editor](https://help.semmle.com/codeql/codeql-for-vscode/reference/editor.html#autoformatting). For more information, see the [CodeQL style guide](https://github.com/Semmle/ql/blob/master/docs/ql-style-guide.md).
16+
* C/C++: `cpp/ql/src`
17+
* C#: `csharp/ql/src`
18+
* Java: `java/ql/src`
19+
* JavaScript: `javascript/ql/src`
20+
* Python: `python/ql/src`
1921

20-
3. **Make sure your query has the correct metadata**
22+
Each language-specific directory contains further subdirectories that group queries based on their `@tags` or purpose.
23+
- Experimental queries and libraries are stored in the `experimental` subdirectory within each language-specific directory in the [CodeQL repository](https://github.com/Semmle/ql). For example, experimental Java queries and libraries are stored in `java/ql/src/experimental` and any corresponding tests in `java/ql/test/experimental`.
24+
- The structure of an `experimental` subdirectory mirrors the structure of its parent directory.
25+
- Select or create an appropriate directory in `experimental` based on the existing directory structure of `experimental` or its parent directory.
2126

22-
Query metadata is used by Semmle's analysis to identify your query and make sure the query results are displayed properly.
23-
The most important metadata to include are the `@name`, `@description`, and the `@kind`.
24-
Other metadata properties (`@precision`, `@severity`, and `@tags`) are usually added after the query has been reviewed by Semmle staff.
25-
For more information on writing query metadata, see the [Query metadata style guide](https://github.com/Semmle/ql/blob/master/docs/query-metadata-style-guide.md).
27+
2. **Query metadata**
2628

27-
4. **Make sure the `select` statement is compatible with the query type**
29+
- The query `@id` must conform to all the requirements in the [guide on query metadata](docs/query-metadata-style-guide.md#query-id-id). In particular, it must not clash with any other queries in the repository, and it must start with the appropriate language-specific prefix.
30+
- The query must have a `@name` and `@description` to explain its purpose.
31+
- The query must have a `@kind` and `@problem.severity` as required by CodeQL tools.
2832

29-
The `select` statement of your query must be compatible with the query type (determined by the `@kind` metadata property) for alert or path results to be displayed correctly in LGTM and CodeQL for VS Code.
30-
For more information on `select` statement format, see [Introduction to query files](https://help.semmle.com/QL/learn-ql/writing-queries/introduction-to-queries.html#select-clause) on help.semmle.com.
33+
For details, see the [guide on query metadata](docs/query-metadata-style-guide.md).
3134

32-
5. **Save your query in a `.ql` file in the correct language directory in this repository**
35+
Make sure the `select` statement is compatible with the query `@kind`. See [Introduction to query files](https://help.semmle.com/QL/learn-ql/writing-queries/introduction-to-queries.html#select-clause) on help.semmle.com.
3336

34-
There are five language-specific directories in this repository:
35-
36-
* C/C++: `ql/cpp/ql/src`
37-
* C#: `ql/csharp/ql/src`
38-
* Java: `ql/java/ql/src`
39-
* JavaScript: `ql/javascript/ql/src`
40-
* Python: `ql/python/ql/src`
37+
3. **Formatting**
4138

42-
Each language-specific directory contains further subdirectories that group queries based on their `@tags` properties or purpose. Select the appropriate subdirectory for your new query, or create a new one if necessary.
39+
- The queries and libraries must be [autoformatted](https://help.semmle.com/codeql/codeql-for-vscode/reference/editor.html#autoformatting).
4340

44-
6. **Write a query help file**
41+
4. **Compilation**
4542

46-
Query help files explain the purpose of your query to other users. Write your query help in a `.qhelp` file and save it in the same directory as your new query.
47-
For more information on writing query help, see the [Query help style guide](https://github.com/Semmle/ql/blob/master/docs/query-help-style-guide.md).
43+
- Compilation of the query and any associated libraries and tests must be resilient to future development of the [supported](docs/supported-queries.md) libraries. This means that the functionality cannot use internal libraries, cannot depend on the output of `getAQlClass`, and cannot make use of regexp matching on `toString`.
44+
- The query and any associated libraries and tests must not cause any compiler warnings to be emitted (such as use of deprecated functionality or missing `override` annotations).
4845

49-
7. **Maintain backwards compatibility**
46+
5. **Results**
5047

51-
The standard CodeQL libraries must evolve in a backwards compatible manner. If any backwards incompatible changes need to be made, the existing API must first be marked as deprecated. This is done by adding a `deprecated` annotation along with a QLDoc reference to the replacement API. Only after at least one full release cycle has elapsed may the old API be removed.
48+
- The query must have at least one true positive result on some revision of a real project.
5249

53-
In addition to contributions to our standard queries and libraries, we also welcome contributions of a more experimental nature, which do not need to fulfill all the requirements listed above. See the guidelines for [experimental queries and libraries](docs/experimental.md) for details.
50+
6. **Contributor License Agreement**
51+
52+
- The contributor can satisfy the [CLA](#contributor-license-agreement).
53+
54+
Experimental queries and libraries may not be actively maintained as the [supported](docs/supported-queries.md) libraries evolve. They may also be changed in backwards-incompatible ways or may be removed entirely in the future without deprecation warnings.
55+
56+
After the experimental query is merged, we welcome pull requests to improve it. Before a query can be moved out of the `experimental` subdirectory, it must satisfy [the requirements for being a supported query](docs/supported-queries.md).
5457

5558
## Using your personal data
5659

5760
If you contribute to this project, we will record your name and email
5861
address (as provided by you with your contributions) as part of the code
59-
repositories, which might be made public. We might also use this information
62+
repositories, which are public. We might also use this information
6063
to contact you in relation to your contributions, as well as in the
6164
normal course of software development. We also store records of your
6265
CLA agreements. Under GDPR legislation, we do this

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CodeQL
22

3-
This open source repository contains the standard CodeQL libraries and queries that power [LGTM](https://lgtm.com), and the other products that [Semmle](https://semmle.com) makes available to its customers worldwide.
3+
This open source repository contains the standard CodeQL libraries and queries that power [LGTM](https://lgtm.com) and the other CodeQL products that [GitHub](https://github.com) makes available to its customers worldwide.
44

55
## How do I learn CodeQL and run queries?
66

@@ -13,4 +13,4 @@ We welcome contributions to our standard library and standard checks. Do you hav
1313

1414
## License
1515

16-
The code in this repository is licensed under [Apache License 2.0](LICENSE) by [Semmle](https://semmle.com).
16+
The code in this repository is licensed under [Apache License 2.0](LICENSE) by [GitHub](https://github.com).

change-notes/1.24/analysis-cpp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ The following changes in version 1.24 affect C/C++ analysis in all applications.
1818
| No space for zero terminator (`cpp/no-space-for-terminator`) | More true positive results | This query now identifies a wider variety of buffer allocations using the `semmle.code.cpp.models.interfaces.Allocation` library. |
1919
| Memory is never freed (`cpp/memory-never-freed`) | More true positive results | This query now identifies a wider variety of buffer allocations using the `semmle.code.cpp.models.interfaces.Allocation` library. |
2020
| Memory may not be freed (`cpp/memory-may-not-be-freed`) | More true positive results | This query now identifies a wider variety of buffer allocations using the `semmle.code.cpp.models.interfaces.Allocation` library. |
21+
| Mismatching new/free or malloc/delete (`cpp/new-free-mismatch`) | Fewer false positive results | Fixed false positive results in template code. |
2122
| Missing return statement (`cpp/missing-return`) | Fewer false positive results | Functions containing `asm` statements are no longer highlighted by this query. |
2223
| No space for zero terminator (`cpp/no-space-for-terminator`) | More correct results | String arguments to formatting functions are now (usually) expected to be null terminated strings. |
2324
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | | This query is no longer run on LGTM. |
2425
| No space for zero terminator (`cpp/no-space-for-terminator`) | Fewer false positive results | This query has been modified to be more conservative when identifying which pointers point to null-terminated strings. This approach produces fewer, more accurate results. |
2526
| Overloaded assignment does not return 'this' (`cpp/assignment-does-not-return-this`) | Fewer false positive results | This query no longer reports incorrect results in template classes. |
2627
| Unsafe array for days of the year (`cpp/leap-year/unsafe-array-for-days-of-the-year`) | | This query is no longer run on LGTM. |
28+
| Unsigned comparison to zero (`cpp/unsigned-comparison-zero`) | More correct results | This query now also looks for comparisons of the form `0 <= x`. |
2729

2830
## Changes to libraries
2931

change-notes/1.24/analysis-javascript.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
- Calls can now be resolved to indirectly-defined class members in more cases.
2020
- Calls through partial invocations such as `.bind` can now be resolved in more cases.
2121

22+
* Support for flow summaries has been more clearly marked as being experimental and moved to the new `experimental` folder.
23+
2224
* Support for the following frameworks and libraries has been improved:
2325
- [Electron](https://electronjs.org/)
26+
- [fstream](https://www.npmjs.com/package/fstream)
2427
- [Handlebars](https://www.npmjs.com/package/handlebars)
28+
- [jsonfile](https://www.npmjs.com/package/jsonfile)
2529
- [Koa](https://www.npmjs.com/package/koa)
2630
- [Node.js](https://nodejs.org/)
2731
- [Socket.IO](https://socket.io/)
@@ -30,10 +34,20 @@
3034
- [for-in](https://www.npmjs.com/package/for-in)
3135
- [for-own](https://www.npmjs.com/package/for-own)
3236
- [http2](https://nodejs.org/api/http2.html)
37+
- [jQuery](https://jquery.com/)
3338
- [lazy-cache](https://www.npmjs.com/package/lazy-cache)
39+
- [mongodb](https://www.npmjs.com/package/mongodb)
40+
- [ncp](https://www.npmjs.com/package/ncp)
41+
- [node-dir](https://www.npmjs.com/package/node-dir)
42+
- [path-exists](https://www.npmjs.com/package/path-exists)
3443
- [react](https://www.npmjs.com/package/react)
44+
- [recursive-readdir](https://www.npmjs.com/package/recursive-readdir)
45+
- [request](https://www.npmjs.com/package/request)
46+
- [rimraf](https://www.npmjs.com/package/rimraf)
3547
- [send](https://www.npmjs.com/package/send)
3648
- [typeahead.js](https://www.npmjs.com/package/typeahead.js)
49+
- [vinyl-fs](https://www.npmjs.com/package/vinyl-fs)
50+
- [write-file-atomic](https://www.npmjs.com/package/write-file-atomic)
3751
- [ws](https://github.com/websockets/ws)
3852

3953
## New queries
@@ -62,6 +76,10 @@
6276
| Missing CSRF middleware (`js/missing-token-validation`) | Fewer false positive results | The query reports fewer duplicates and only flags handlers that explicitly access cookie data. |
6377
| Uncontrolled data used in path expression (`js/path-injection`) | More results | This query now recognizes additional ways dangerous paths can be constructed and used. |
6478
| Uncontrolled command line (`js/command-line-injection`) | More results | This query now recognizes additional ways of constructing arguments to `cmd.exe` and `/bin/sh`. |
79+
| Syntax error (`js/syntax-error`) | Lower severity | This results of this query are now displayed with lower severity. |
80+
| Use of password hash with insufficient computational effort (`js/insufficient-password-hash`) | Fewer false positive results | This query now recognizes additional cases that do not require secure hashing. |
81+
| Useless regular-expression character escape (`js/useless-regexp-character-escape`) | Fewer false positive results | This query now distinguishes escapes in strings and regular expression literals. |
82+
| Identical operands (`js/redundant-operation`) | Fewer results | This query now recognizes cases where the operands change a value using ++/-- expressions. |
6583

6684
## Changes to libraries
6785

cpp/ql/src/Critical/NewDelete.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import semmle.code.cpp.dataflow.DataFlow
1212
*/
1313
predicate allocExpr(Expr alloc, string kind) {
1414
isAllocationExpr(alloc) and
15+
not alloc.isFromUninstantiatedTemplate(_) and
1516
(
1617
alloc instanceof FunctionCall and
1718
kind = "malloc"

cpp/ql/src/Likely Bugs/Arithmetic/UnsignedGEZero.qll

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ class ConstantZero extends Expr {
1919
* Holds if `candidate` is an expression such that if it's unsigned then we
2020
* want an alert at `ge`.
2121
*/
22-
private predicate lookForUnsignedAt(GEExpr ge, Expr candidate) {
23-
// Base case: `candidate >= 0`
24-
ge.getRightOperand() instanceof ConstantZero and
25-
candidate = ge.getLeftOperand().getFullyConverted() and
26-
// left operand was a signed or unsigned IntegralType before conversions
22+
private predicate lookForUnsignedAt(RelationalOperation ge, Expr candidate) {
23+
// Base case: `candidate >= 0` (or `0 <= candidate`)
24+
(
25+
ge instanceof GEExpr or
26+
ge instanceof LEExpr
27+
) and
28+
ge.getLesserOperand() instanceof ConstantZero and
29+
candidate = ge.getGreaterOperand().getFullyConverted() and
30+
// left/greater operand was a signed or unsigned IntegralType before conversions
2731
// (not a pointer, checking a pointer >= 0 is an entirely different mistake)
2832
// (not an enum, as the fully converted type of an enum is compiler dependent
2933
// so checking an enum >= 0 is always reasonable)
30-
ge.getLeftOperand().getUnderlyingType() instanceof IntegralType
34+
ge.getGreaterOperand().getUnderlyingType() instanceof IntegralType
3135
or
3236
// Recursive case: `...(largerType)candidate >= 0`
3337
exists(Conversion conversion |
@@ -37,7 +41,7 @@ private predicate lookForUnsignedAt(GEExpr ge, Expr candidate) {
3741
)
3842
}
3943

40-
class UnsignedGEZero extends GEExpr {
44+
class UnsignedGEZero extends ComparisonOperation {
4145
UnsignedGEZero() {
4246
exists(Expr ue |
4347
lookForUnsignedAt(this, ue) and
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include <stdlib.h>
2+
#include <sys/param.h>
3+
#include <unistd.h>
4+
#include <pwd.h>
5+
6+
void callSetuidAndCheck(int uid) {
7+
if (setuid(uid) != 0) {
8+
exit(1);
9+
}
10+
}
11+
12+
void callSetgidAndCheck(int gid) {
13+
if (setgid(gid) != 0) {
14+
exit(1);
15+
}
16+
}
17+
18+
/// Correct ways to drop priv.
19+
20+
void correctDropPrivInline() {
21+
if (setgroups(0, NULL)) {
22+
exit(1);
23+
}
24+
25+
if (setgid(-2) != 0) {
26+
exit(1);
27+
}
28+
29+
if (setuid(-2) != 0) {
30+
exit(1);
31+
}
32+
}
33+
34+
void correctDropPrivInScope() {
35+
{
36+
if (setgroups(0, NULL)) {
37+
exit(1);
38+
}
39+
}
40+
41+
{
42+
if (setgid(-2) != 0) {
43+
exit(1);
44+
}
45+
}
46+
47+
{
48+
if (setuid(-2) != 0) {
49+
exit(1);
50+
}
51+
}
52+
}
53+
54+
void correctOrderForInitgroups() {
55+
struct passwd *pw = getpwuid(0);
56+
if (pw) {
57+
if (initgroups(pw->pw_name, -2)) {
58+
exit(1);
59+
}
60+
} else {
61+
// Unhandled.
62+
}
63+
int rc = setuid(-2);
64+
if (rc) {
65+
exit(1);
66+
}
67+
}
68+
69+
void correctDropPrivInScopeParent() {
70+
{
71+
callSetgidAndCheck(-2);
72+
}
73+
correctOrderForInitgroups();
74+
}
75+
76+
void incorrectNoReturnCodeCheck() {
77+
int user = -2;
78+
if (user) {
79+
if (user) {
80+
int rc = setgid(user);
81+
(void)rc;
82+
initgroups("nobody", user);
83+
}
84+
if (user) {
85+
setuid(user);
86+
}
87+
}
88+
}
89+
90+
void correctDropPrivInFunctionCall() {
91+
if (setgroups(0, NULL)) {
92+
exit(1);
93+
}
94+
95+
callSetgidAndCheck(-2);
96+
callSetuidAndCheck(-2);
97+
}
98+
99+
/// Incorrect, out of order gid and uid.
100+
/// Calling uid before gid will fail.
101+
102+
void incorrectDropPrivOutOfOrderInline() {
103+
if (setuid(-2) != 0) {
104+
exit(1);
105+
}
106+
107+
if (setgid(-2) != 0) {
108+
exit(1);
109+
}
110+
}
111+
112+
void incorrectDropPrivOutOfOrderInScope() {
113+
{
114+
if (setuid(-2) != 0) {
115+
exit(1);
116+
}
117+
}
118+
119+
setgid(-2);
120+
}
121+
122+
void incorrectDropPrivOutOfOrderWithFunction() {
123+
callSetuidAndCheck(-2);
124+
125+
if (setgid(-2) != 0) {
126+
exit(1);
127+
}
128+
}
129+
130+
void incorrectDropPrivOutOfOrderWithFunction2() {
131+
callSetuidAndCheck(-2);
132+
callSetgidAndCheck(-2);
133+
}
134+
135+
void incorrectDropPrivNoCheck() {
136+
setgid(-2);
137+
setuid(-2);
138+
}

0 commit comments

Comments
 (0)