Skip to content

Commit 25082ab

Browse files
authored
Merge pull request #33 from trailofbits/mschwager-codeql-query-format
Autoformat existing CodeQL queries and add CI check
2 parents d9b8058 + 87e7631 commit 25082ab

38 files changed

+829
-1234
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Format CodeQL .ql and .qll files using `codeql query format`
2+
b3b8f8f95bf1a149fdca9a8e16593caa34f4678b

.github/workflows/test.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ jobs:
1616
version: '2.23.8'
1717
platform: 'linux64'
1818
checksum: 'e61bc8aa8d86d45acd9d1c36629a12bbfb3365cd07a31666a2ebc91c6a1940b2'
19-
- run: |
20-
codeql test run --threads=0 ./cpp/test/
21-
codeql test run --threads=0 ./go/test/
22-
codeql test run --threads=0 ./java/test/
19+
- run: make format-check
20+
- run: make test

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
test:
2+
find . -iname "test" -type d -maxdepth 2 -mindepth 2 -print0 | \
3+
xargs -0 codeql test run --threads=0
4+
5+
format:
6+
find . \( -iname '*.ql' -o -iname '*.qll' \) -print0 | \
7+
xargs -0 codeql query format --in-place
8+
9+
format-check:
10+
find . \( -iname '*.ql' -o -iname '*.qll' \) -print0 | \
11+
xargs -0 codeql query format --check-only
12+
13+
pack-install:
14+
find . -iname "qlpack.yml" -exec \
15+
sh -c 'codeql pack install $$(dirname "$$1")' sh {} \;
16+
17+
pack-upgrade:
18+
find . -iname "qlpack.yml" -exec \
19+
sh -c 'codeql pack upgrade $$(dirname "$$1")' sh {} \;
20+
21+
.PHONY: test format format-check pack-install pack-upgrade

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,21 @@ codeql resolve packs | grep trailofbits
103103
#### Before committing
104104

105105
Run tests:
106+
107+
```sh
108+
make test
109+
```
110+
111+
Format queries:
112+
106113
```sh
107-
cd codeql-queries
108-
codeql test run ./cpp/test
109-
codeql test run ./go/test
110-
codeql test run ./java/test
114+
make format
111115
```
112116

113-
Update dependencies:
117+
Install dependencies:
118+
114119
```sh
115-
bash ./scripts/install_all.sh
120+
make install
116121
```
117122

118123
Generate query tables and copy-paste it to README.md file

cpp/lib/trailofbits/crypto/common.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import common.InitUpdateFinalPattern
22
import common.SymmetricCipherContext
3-
43
import common.Csprng
54
import common.ErrorCode
65
import common.HashFunction
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import cpp
22

3-
43
abstract class Csprng extends Function {
54
abstract int getAStrongRandomnessSource();
65

76
abstract int getRequestedBytes();
87
}
98

10-
119
class CsprngCall extends FunctionCall {
1210
Csprng csprng;
1311

1412
// Ensures that the call target is an instance of Csprng.
1513
CsprngCall() { csprng = this.getTarget() }
1614

17-
Expr getAStrongRandomnessSource() { result = this.getArgument(csprng.getAStrongRandomnessSource()) }
15+
Expr getAStrongRandomnessSource() {
16+
result = this.getArgument(csprng.getAStrongRandomnessSource())
17+
}
1818

1919
Expr getRequestedBytes() { result = this.getArgument(csprng.getRequestedBytes()) }
2020
}

cpp/lib/trailofbits/crypto/common/CsprngInitializer.qll

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
import cpp
22
import StrongRandomnessSink
33

4-
54
abstract class CsprngInitializer extends Function {
65
// Returns the index of the seed argument (if it exists).
76
abstract int getAStrongRandomnessSink();
87
}
98

109
class CsprngInitializerCall extends FunctionCall {
1110
CsprngInitializer init;
12-
13-
CsprngInitializerCall() {
14-
this.getTarget() = init
15-
}
11+
12+
CsprngInitializerCall() { this.getTarget() = init }
1613

1714
// Returns the seed argument.
18-
Expr getAStrongRandomnessSink() {
19-
result = this.getArgument(init.getAStrongRandomnessSink())
20-
}
15+
Expr getAStrongRandomnessSink() { result = this.getArgument(init.getAStrongRandomnessSink()) }
2116
}
2217

2318
// A type representing the seed argument of a CSPRNG initializer function.
2419
class CsprngInitializerSink extends StrongRandomnessSink {
2520
CsprngInitializerCall init;
2621

27-
CsprngInitializerSink() {
28-
this = init.getAStrongRandomnessSink()
29-
}
22+
CsprngInitializerSink() { this = init.getAStrongRandomnessSink() }
3023

3124
override string getDescription() {
3225
result = init.getTarget().getQualifiedName() + " parameter " + this.toString()

cpp/lib/trailofbits/crypto/common/CustomAllocator.qll

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,31 @@
11
import cpp
22
private import semmle.code.cpp.controlflow.StackVariableReachability
33

4-
54
/**
65
* A custom allocator which returns a pointer to the allocated object.
76
*/
87
abstract class CustomAllocator extends Function {
98
CustomDeallocator dealloc;
109

11-
CustomDeallocator getDeallocator() {
12-
result = dealloc
13-
}
10+
CustomDeallocator getDeallocator() { result = dealloc }
1411

1512
// Override this and return the index of the pointer argument if the
1613
// allocated pointer is passed as an argument.
17-
int getPointer() {
18-
result = -1
19-
}
14+
int getPointer() { result = -1 }
2015
}
2116

2217
class CustomAllocatorCall extends FunctionCall {
2318
CustomAllocator alloc;
2419

25-
CustomAllocatorCall() {
26-
this = alloc.getACallToThisFunction()
27-
}
20+
CustomAllocatorCall() { this = alloc.getACallToThisFunction() }
2821

2922
Expr getPointer() {
30-
if alloc.getPointer() < 0 then
31-
result = this
32-
else
33-
result = this.getArgument(alloc.getPointer())
23+
if alloc.getPointer() < 0 then result = this else result = this.getArgument(alloc.getPointer())
3424
}
3525

36-
CustomAllocator getAllocator() {
37-
result = alloc
38-
}
26+
CustomAllocator getAllocator() { result = alloc }
3927

40-
CustomDeallocatorCall getADeallocatorCall() {
41-
result.getTarget() = alloc.getDeallocator()
42-
}
28+
CustomDeallocatorCall getADeallocatorCall() { result.getTarget() = alloc.getDeallocator() }
4329
}
4430

4531
/**
@@ -52,27 +38,23 @@ abstract class CustomDeallocator extends Function {
5238
class CustomDeallocatorCall extends FunctionCall {
5339
CustomDeallocator dealloc;
5440

55-
CustomDeallocatorCall() {
56-
this = dealloc.getACallToThisFunction()
57-
}
41+
CustomDeallocatorCall() { this = dealloc.getACallToThisFunction() }
5842

59-
Expr getPointer() {
60-
result = this.getArgument(dealloc.getPointer())
61-
}
43+
Expr getPointer() { result = this.getArgument(dealloc.getPointer()) }
6244
}
6345

6446
class CustomAllocatorLeak extends StackVariableReachabilityWithReassignment {
6547
CustomAllocatorCall alloc;
6648

67-
CustomAllocatorLeak() {
68-
this = "CustomAllocatorLeak"
69-
}
49+
CustomAllocatorLeak() { this = "CustomAllocatorLeak" }
7050

7151
override predicate isSourceActual(ControlFlowNode node, StackVariable var) {
7252
// A source is an allocation of the variable.
73-
node = alloc.getPointer() and (
53+
node = alloc.getPointer() and
54+
(
7455
// When alloc returns allocated object.
75-
alloc.getPointer() = var.getAnAssignedValue() or
56+
alloc.getPointer() = var.getAnAssignedValue()
57+
or
7658
// When alloc takes a pointer as an argument the variable can be either a
7759
// pointer or an object v, in which case &v is passed to the function and
7860
// we need to invoke getAChild() to get a variable access.
@@ -87,24 +69,22 @@ class CustomAllocatorLeak extends StackVariableReachabilityWithReassignment {
8769

8870
override predicate isSinkActual(ControlFlowNode node, StackVariable var) {
8971
// A sink is a return statement not returning the allocated variable.
90-
var.getFunction() = node.(ReturnStmt).getEnclosingFunction()
91-
and not node.(ReturnStmt).getExpr() = var.getAnAccess()
72+
var.getFunction() = node.(ReturnStmt).getEnclosingFunction() and
73+
not node.(ReturnStmt).getExpr() = var.getAnAccess()
9274
}
9375
}
9476

9577
class CustomAllocatorUseAfterFree extends StackVariableReachabilityWithReassignment {
96-
CustomAllocatorUseAfterFree() {
97-
this = "CustomAllocatorUseAfterFree"
98-
}
78+
CustomAllocatorUseAfterFree() { this = "CustomAllocatorUseAfterFree" }
9979

10080
override predicate isSourceActual(ControlFlowNode node, StackVariable var) {
10181
node.(CustomDeallocatorCall).getPointer() = var.getAnAccess()
10282
}
10383

10484
override predicate isSinkActual(ControlFlowNode node, StackVariable var) {
10585
// A use is an access which is not a reassignment.
106-
node = var.getAnAccess() and not
107-
this.isAnAssignment(node, var)
86+
node = var.getAnAccess() and
87+
not this.isAnAssignment(node, var)
10888
}
10989

11090
override predicate isBarrier(ControlFlowNode node, StackVariable var) {

cpp/lib/trailofbits/crypto/common/ErrorCode.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import cpp
55
*/
66
abstract class ReturnsErrorCode extends Function { }
77

8-
98
/**
109
* A type modelling the return value from a function that returns an error or status code.
1110
*/
Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import cpp
22

3-
43
// A type modelling a (taint propagating) hash function.
54
abstract class HashFunction extends TaintFunction {
65
// Returns the index of the input data argument.
@@ -16,15 +15,15 @@ abstract class HashFunction extends TaintFunction {
1615
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
1716
input.isParameterDeref(this.getInput()) and
1817
(
19-
output.isParameterDeref(this.getAnOutput()) or
20-
(this.returnsOutput() = true and output.isReturnValueDeref())
18+
output.isParameterDeref(this.getAnOutput())
19+
or
20+
this.returnsOutput() = true and output.isReturnValueDeref()
2121
)
2222
}
2323
}
2424

2525
// A type modelling a (taint propagating) hash context.
26-
class HashContext extends Expr {
27-
}
26+
class HashContext extends Expr { }
2827

2928
// A type modelling a hash context initializer.
3029
abstract class HashContextInitializer extends Function {
@@ -40,14 +39,13 @@ class HashContextInitializerCall extends FunctionCall {
4039

4140
// Returns the hash context argument.
4241
HashContext getContext() {
43-
(init.returnsContext() = true and result = this) or
44-
(result = this.getArgument(init.getAContext()))
42+
init.returnsContext() = true and result = this
43+
or
44+
result = this.getArgument(init.getAContext())
4545
}
4646

4747
// Returns the corresponding initializer function.
48-
HashContextInitializer getInitializer() {
49-
result = init
50-
}
48+
HashContextInitializer getInitializer() { result = init }
5149
}
5250

5351
// A type modelling a hash context update function.
@@ -70,40 +68,35 @@ class HashContextUpdaterCall extends FunctionCall {
7068
HashContextUpdater update;
7169

7270
// Returns the hash context argument.
73-
HashContext getContext() {
74-
result = this.getArgument(update.getContext())
75-
}
71+
HashContext getContext() { result = this.getArgument(update.getContext()) }
7672

7773
// Returns the corresponding update function.
78-
HashContextInitializer getUpdater() {
79-
result = update
80-
}
81-
74+
HashContextInitializer getUpdater() { result = update }
75+
8276
// Returns an input data argument.
83-
Expr getAnInput() {
84-
result = this.getArgument(update.getAnInput())
85-
}
77+
Expr getAnInput() { result = this.getArgument(update.getAnInput()) }
8678
}
8779

8880
// A type modelling a hash context finalizer function.
8981
abstract class HashContextFinalizer extends TaintFunction {
9082
// Returns the index of the context argument.
9183
abstract int getContext();
92-
84+
9385
// Returns the output digest argument (if it exists).
9486
abstract int getAnOutput();
9587

9688
// True iff the finalizer function returns a pointer or reference to the
9789
// output digest.
9890
abstract boolean returnsOutput();
99-
91+
10092
// Since C++ does not have AdditionalTaintFlow we use TaintFunction to model
10193
// taint from input to output through the hash context.
10294
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
10395
input.isParameterDeref(this.getContext()) and
10496
(
105-
output.isParameterDeref(this.getAnOutput()) or
106-
(this.returnsOutput() = true and output.isReturnValueDeref())
97+
output.isParameterDeref(this.getAnOutput())
98+
or
99+
this.returnsOutput() = true and output.isReturnValueDeref()
107100
)
108101
}
109102
}
@@ -112,19 +105,15 @@ class HashContextFinalizerCall extends FunctionCall {
112105
HashContextFinalizer final;
113106

114107
// Returns the hash context argument.
115-
HashContext getContext() {
116-
result = this.getArgument(final.getContext())
117-
}
108+
HashContext getContext() { result = this.getArgument(final.getContext()) }
118109

119110
// Returns the corresponding finalizer function.
120-
HashContextInitializer getFinalizer() {
121-
result = final
122-
}
111+
HashContextInitializer getFinalizer() { result = final }
123112

124113
// Returns an expression representing the output digest.
125114
Expr getAnOutput() {
126-
(final.returnsOutput() = true and result = this) or
127-
(result = this.getArgument(final.getAnOutput()))
115+
final.returnsOutput() = true and result = this
116+
or
117+
result = this.getArgument(final.getAnOutput())
128118
}
129119
}
130-

0 commit comments

Comments
 (0)