Skip to content

Commit 9a555ae

Browse files
committed
Merge branch 'main' into redsun82/swift-diagnostics-locations
2 parents 9ffada3 + b214003 commit 9a555ae

File tree

73 files changed

+1685
-177
lines changed

Some content is hidden

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

73 files changed

+1685
-177
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Allocation.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ private module HeuristicAllocation {
414414
int sizeArg;
415415

416416
HeuristicAllocationFunctionByName() {
417-
Function.super.getName().matches("%alloc%") and
417+
Function.super.getName().matches(["%alloc%", "%Alloc%"]) and
418418
Function.super.getUnspecifiedType() instanceof PointerType and
419419
sizeArg = unique( | | getAnUnsignedParameter(this))
420420
}

cpp/ql/src/experimental/Likely Bugs/OverrunWriteProductFlow.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ VariableAccess getAVariableAccess(Expr e) { e.getAChild*() = result }
4747
* Holds if `(n, state)` pair represents the source of flow for the size
4848
* expression associated with `alloc`.
4949
*/
50-
predicate hasSize(AllocationExpr alloc, DataFlow::Node n, int state) {
50+
predicate hasSize(HeuristicAllocationExpr alloc, DataFlow::Node n, int state) {
5151
exists(VariableAccess va, Expr size, int delta |
5252
size = alloc.getSizeExpr() and
5353
// Get the unique variable in a size expression like `x` in `malloc(x + 1)`.

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-119/OverrunWriteProductFlow.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ edges
222222
| test.cpp:243:12:243:14 | str indirection [string] | test.cpp:243:12:243:21 | string |
223223
| test.cpp:243:12:243:14 | str indirection [string] | test.cpp:243:16:243:21 | string indirection |
224224
| test.cpp:243:16:243:21 | string indirection | test.cpp:243:12:243:21 | string |
225+
| test.cpp:249:20:249:27 | call to my_alloc | test.cpp:250:12:250:12 | p |
225226
nodes
226227
| test.cpp:16:11:16:21 | mk_string_t indirection [string] | semmle.label | mk_string_t indirection [string] |
227228
| test.cpp:18:5:18:30 | ... = ... | semmle.label | ... = ... |
@@ -402,6 +403,8 @@ nodes
402403
| test.cpp:243:12:243:14 | str indirection [string] | semmle.label | str indirection [string] |
403404
| test.cpp:243:12:243:21 | string | semmle.label | string |
404405
| test.cpp:243:16:243:21 | string indirection | semmle.label | string indirection |
406+
| test.cpp:249:20:249:27 | call to my_alloc | semmle.label | call to my_alloc |
407+
| test.cpp:250:12:250:12 | p | semmle.label | p |
405408
subpaths
406409
| test.cpp:242:22:242:27 | buffer | test.cpp:235:40:235:45 | buffer | test.cpp:236:12:236:17 | p_str indirection [post update] [string] | test.cpp:242:16:242:19 | set_string output argument [string] |
407410
#select
@@ -422,3 +425,4 @@ subpaths
422425
| test.cpp:207:9:207:15 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:207:22:207:27 | string | This write may overflow $@ by 3 elements. | test.cpp:207:22:207:27 | string | string |
423426
| test.cpp:232:3:232:8 | call to memset | test.cpp:228:43:228:48 | call to malloc | test.cpp:232:10:232:15 | buffer | This write may overflow $@ by 32 elements. | test.cpp:232:10:232:15 | buffer | buffer |
424427
| test.cpp:243:5:243:10 | call to memset | test.cpp:241:27:241:32 | call to malloc | test.cpp:243:12:243:21 | string | This write may overflow $@ by 1 element. | test.cpp:243:16:243:21 | string | string |
428+
| test.cpp:250:5:250:10 | call to memset | test.cpp:249:20:249:27 | call to my_alloc | test.cpp:250:12:250:12 | p | This write may overflow $@ by 1 element. | test.cpp:250:12:250:12 | p | p |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-119/test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,9 @@ void test_flow_through_setter(unsigned size) {
243243
memset(str.string, 0, size + 1); // BAD
244244
}
245245

246+
void* my_alloc(unsigned size);
247+
248+
void foo(unsigned size) {
249+
int* p = (int*)my_alloc(size); // BAD
250+
memset(p, 0, size + 1);
251+
}

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/InvalidPointerDeref.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ edges
649649
| test.cpp:280:13:280:24 | new[] | test.cpp:281:14:281:15 | xs |
650650
| test.cpp:290:13:290:24 | new[] | test.cpp:291:14:291:15 | xs |
651651
| test.cpp:290:13:290:24 | new[] | test.cpp:292:30:292:30 | x |
652+
| test.cpp:304:15:304:26 | new[] | test.cpp:307:5:307:6 | xs |
653+
| test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:6 | xs |
654+
| test.cpp:308:5:308:6 | xs | test.cpp:308:5:308:11 | access to array |
655+
| test.cpp:308:5:308:11 | access to array | test.cpp:308:5:308:29 | Store: ... = ... |
652656
#select
653657
| test.cpp:6:14:6:15 | Load: * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:6:14:6:15 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
654658
| test.cpp:8:14:8:21 | Load: * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:8:14:8:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
@@ -672,3 +676,4 @@ edges
672676
| test.cpp:254:9:254:16 | Store: ... = ... | test.cpp:248:24:248:30 | call to realloc | test.cpp:254:9:254:16 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:248:24:248:30 | call to realloc | call to realloc | test.cpp:254:11:254:11 | i | i |
673677
| test.cpp:264:13:264:14 | Load: * ... | test.cpp:260:13:260:24 | new[] | test.cpp:264:13:264:14 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:260:13:260:24 | new[] | new[] | test.cpp:261:19:261:21 | len | len |
674678
| test.cpp:274:5:274:10 | Store: ... = ... | test.cpp:270:13:270:24 | new[] | test.cpp:274:5:274:10 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:270:13:270:24 | new[] | new[] | test.cpp:271:19:271:21 | len | len |
679+
| test.cpp:308:5:308:29 | Store: ... = ... | test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:29 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:304:15:304:26 | new[] | new[] | test.cpp:308:8:308:10 | ... + ... | ... + ... |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/test.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,18 @@ void test20(unsigned len)
293293
{
294294
*x = 0; // GOOD
295295
}
296-
}
296+
}
297+
298+
void* test21_get(int n);
299+
300+
void test21() {
301+
int n = 0;
302+
while (test21_get(n)) n+=2;
303+
304+
void** xs = new void*[n];
305+
306+
for (int i = 0; i < n; i += 2) {
307+
xs[i] = test21_get(i);
308+
xs[i+1] = test21_get(i+1);
309+
}
310+
}

cpp/ql/test/library-tests/ir/range-analysis/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@
4949
return 0;
5050
}
5151

52+
void* f3_get(int n);
53+
54+
void f3() {
55+
int n = 0;
56+
while (f3_get(n)) n+=2;
57+
58+
for (int i = 0; i < n; i += 2) {
59+
range(i); // $ range=>=0 SPURIOUS: range="<=call to f3_get-1" range="<=call to f3_get-2"
60+
}
61+
}

go/ql/lib/semmle/go/security/SensitiveActions.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module HeuristicNames {
3535
*/
3636
string maybePassword() {
3737
result = "(?is).*pass(wd|word|code|phrase)(?!.*question).*" or
38-
result = "(?is).*(auth(entication|ori[sz]ation)?)key.*"
38+
result = "(?is).*(auth(entication|ori[sz]ation)?|api)key.*"
3939
}
4040

4141
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
func bad() interface{} {
3+
name := os.Args[1:]
4+
// This is bad. `name` can be something like `test?allowAllFiles=true&` which will allow an attacker to access local files.
5+
dbDSN := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", "username", "password", "127.0.0.1", 3306, name)
6+
db, _ := sql.Open("mysql", dbDSN)
7+
return db
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func good() (interface{}, error) {
2+
name := os.Args[1]
3+
hasBadChar, _ := regexp.MatchString(".*[?].*", name)
4+
5+
if hasBadChar {
6+
return nil, errors.New("Bad input")
7+
}
8+
9+
dbDSN := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", "username", "password", "127.0.0.1", 3306, name)
10+
db, _ := sql.Open("mysql", dbDSN)
11+
return db, nil
12+
}

0 commit comments

Comments
 (0)