Skip to content

Commit 7e78503

Browse files
committed
Implement standard library models for Go 1.20
1 parent 7d2b78b commit 7e78503

File tree

9 files changed

+216
-0
lines changed

9 files changed

+216
-0
lines changed

go/ql/lib/semmle/go/frameworks/Stdlib.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import semmle.go.frameworks.stdlib.Syscall
6565
import semmle.go.frameworks.stdlib.TextScanner
6666
import semmle.go.frameworks.stdlib.TextTabwriter
6767
import semmle.go.frameworks.stdlib.TextTemplate
68+
import semmle.go.frameworks.stdlib.Unsafe
6869

6970
/** A `String()` method. */
7071
class StringMethod extends TaintTracking::FunctionModel, Method {

go/ql/lib/semmle/go/frameworks/stdlib/Bytes.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ module Bytes {
1111
FunctionOutput outp;
1212

1313
FunctionModels() {
14+
hasQualifiedName("bytes", "Clone") and
15+
(inp.isParameter(0) and outp.isResult())
16+
or
17+
hasQualifiedName("bytes", "Cut") and
18+
(inp.isParameter(0) and outp.isResult([0, 1]))
19+
or
20+
hasQualifiedName("bytes", ["CutPrefix", "CutSuffix"]) and
21+
(inp.isParameter(0) and outp.isResult(0))
22+
or
1423
// signature: func Fields(s []byte) [][]byte
1524
hasQualifiedName("bytes", "Fields") and
1625
(inp.isParameter(0) and outp.isResult())

go/ql/lib/semmle/go/frameworks/stdlib/Errors.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ module Errors {
2222
// signature: func Unwrap(err error) error
2323
hasQualifiedName("errors", "Unwrap") and
2424
(inp.isParameter(0) and outp.isResult())
25+
or
26+
// signature: func Join(errs ...error) error
27+
hasQualifiedName("errors", "Join") and
28+
(inp.isParameter(_) and outp.isResult())
2529
}
2630

2731
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {

go/ql/lib/semmle/go/frameworks/stdlib/Sync.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module Sync {
1111
FunctionOutput outp;
1212

1313
MethodModels() {
14+
hasQualifiedName("sync", "Map", "CompareAndSwap") and
15+
(inp.isParameter(2) and outp.isReceiver())
16+
or
1417
// signature: func (*Map) Load(key interface{}) (value interface{}, ok bool)
1518
hasQualifiedName("sync", "Map", "Load") and
1619
(inp.isReceiver() and outp.isResult(0))
@@ -28,6 +31,13 @@ module Sync {
2831
hasQualifiedName("sync", "Map", "Store") and
2932
(inp.isParameter(_) and outp.isReceiver())
3033
or
34+
hasQualifiedName("sync", "Map", "Swap") and
35+
(
36+
inp.isReceiver() and outp.isResult(0)
37+
or
38+
inp.isParameter(_) and outp.isReceiver()
39+
)
40+
or
3141
// signature: func (*Pool) Get() interface{}
3242
hasQualifiedName("sync", "Pool", "Get") and
3343
(inp.isReceiver() and outp.isResult())
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Provides classes modeling security-relevant aspects of the `unsafe` package.
3+
*/
4+
5+
import go
6+
7+
/** Provides models of commonly used functions in the `unsafe` package. */
8+
module Unsafe {
9+
private class FunctionModels extends TaintTracking::FunctionModel {
10+
FunctionInput inp;
11+
FunctionOutput outp;
12+
13+
FunctionModels() {
14+
hasQualifiedName("unsafe", ["String", "StringData", "Slice", "SliceData"]) and
15+
(inp.isParameter(0) and outp.isResult())
16+
}
17+
18+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
19+
input = inp and output = outp
20+
}
21+
}
22+
}

go/ql/test/library-tests/semmle/go/frameworks/StdlibTaintFlow/Bytes.go

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/ql/test/library-tests/semmle/go/frameworks/StdlibTaintFlow/Errors.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/ql/test/library-tests/semmle/go/frameworks/StdlibTaintFlow/Sync.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import "unsafe"
4+
5+
func TaintStepTest_UnsafeSlice(sourceCQL interface{}) interface{} {
6+
s := sourceCQL.(*byte)
7+
return unsafe.Slice(s, 1)
8+
}
9+
10+
func TaintStepTest_UnsafeSliceData(sourceCQL interface{}) interface{} {
11+
s := sourceCQL.([]byte)
12+
return unsafe.SliceData(s)
13+
}
14+
15+
func TaintStepTest_UnsafeString(sourceCQL interface{}) interface{} {
16+
s := sourceCQL.(*byte)
17+
return unsafe.String(s, 1)
18+
}
19+
20+
func TaintStepTest_UnsafeStringData(sourceCQL interface{}) interface{} {
21+
s := sourceCQL.(string)
22+
return unsafe.StringData(s)
23+
}
24+
25+
func RunAllTaints_Sync() {
26+
{
27+
source := newSource(0)
28+
out := TaintStepTest_UnsafeSlice(source)
29+
sink(0, out)
30+
}
31+
{
32+
source := newSource(1)
33+
out := TaintStepTest_UnsafeSliceData(source)
34+
sink(1, out)
35+
}
36+
{
37+
source := newSource(2)
38+
out := TaintStepTest_UnsafeString(source)
39+
sink(2, out)
40+
}
41+
{
42+
source := newSource(3)
43+
out := TaintStepTest_UnsafeStringData(source)
44+
sink(3, out)
45+
}
46+
}

0 commit comments

Comments
 (0)