Skip to content

Commit 915643a

Browse files
authored
cmd/geth: add test to verify regexps in version check (#21962)
1 parent 40b6ccf commit 915643a

File tree

2 files changed

+72
-13
lines changed

2 files changed

+72
-13
lines changed

cmd/geth/testdata/vcheck/data2.json renamed to cmd/geth/testdata/vcheck/vulnerabilities.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,32 @@
77
"links": [
88
"https://github.com/ethereum/go-ethereum/pull/21793",
99
"https://blog.ethereum.org/2020/11/12/geth_security_release/",
10-
"https://github.com/ethereum/go-ethereum/commit/567d41d9363706b4b13ce0903804e8acf214af49"
10+
"https://github.com/ethereum/go-ethereum/commit/567d41d9363706b4b13ce0903804e8acf214af49",
11+
"https://github.com/ethereum/go-ethereum/security/advisories/GHSA-v592-xf75-856p"
1112
],
1213
"introduced": "v1.6.0",
1314
"fixed": "v1.9.24",
1415
"published": "2020-11-12",
1516
"severity": "Medium",
16-
"check": "Geth\\/v1\\.(6|7|8)\\..*|Geth\\/v1\\.9\\.2(1|2|3)-.*",
17-
"CVE": "correct"
17+
"CVE": "CVE-2020-26240",
18+
"check": "Geth\\/v1\\.(6|7|8)\\..*|Geth\\/v1\\.9\\.\\d-.*|Geth\\/v1\\.9\\.1.*|Geth\\/v1\\.9\\.2(0|1|2|3)-.*"
1819
},
1920
{
20-
"name": "GoCrash",
21+
"name": "Denial of service due to Go CVE-2020-28362",
2122
"uid": "GETH-2020-02",
2223
"summary": "A denial-of-service issue can be used to crash Geth nodes during block processing, due to an underlying bug in Go (CVE-2020-28362) versions < `1.15.5`, or `<1.14.12`",
2324
"description": "The DoS issue can be used to crash all Geth nodes during block processing, the effects of which would be that a major part of the Ethereum network went offline.\n\nOutside of Go-Ethereum, the issue is most likely relevant for all forks of Geth (such as TurboGeth or ETC’s core-geth) which is built with versions of Go which contains the vulnerability.",
2425
"links": [
2526
"https://blog.ethereum.org/2020/11/12/geth_security_release/",
2627
"https://groups.google.com/g/golang-announce/c/NpBGTTmKzpM",
27-
"https://github.com/golang/go/issues/42552"
28+
"https://github.com/golang/go/issues/42552",
29+
"https://github.com/ethereum/go-ethereum/security/advisories/GHSA-m6gx-rhvj-fh52"
2830
],
31+
"introduced": "v0.0.0",
2932
"fixed": "v1.9.24",
3033
"published": "2020-11-12",
3134
"severity": "Critical",
35+
"CVE": "CVE-2020-28362",
3236
"check": "Geth.*\\/go1\\.(11(.*)|12(.*)|13(.*)|14|14\\.(\\d|10|11|)|15|15\\.[0-4])$"
3337
},
3438
{
@@ -37,12 +41,14 @@
3741
"summary": "A consensus flaw in Geth, related to `datacopy` precompile",
3842
"description": "Geth erroneously performed a 'shallow' copy when the precompiled `datacopy` (at `0x00...04`) was invoked. An attacker could deploy a contract that uses the shallow copy to corrupt the contents of the `RETURNDATA`, thus causing a consensus failure.",
3943
"links": [
40-
"https://blog.ethereum.org/2020/11/12/geth_security_release/"
44+
"https://blog.ethereum.org/2020/11/12/geth_security_release/",
45+
"https://github.com/ethereum/go-ethereum/security/advisories/GHSA-69v6-xc2j-r2jf"
4146
],
4247
"introduced": "v1.9.7",
4348
"fixed": "v1.9.17",
4449
"published": "2020-11-12",
4550
"severity": "Critical",
51+
"CVE": "CVE-2020-26241",
4652
"check": "Geth\\/v1\\.9\\.(7|8|9|10|11|12|13|14|15|16).*$"
4753
},
4854
{
@@ -51,12 +57,14 @@
5157
"summary": "A denial-of-service issue can be used to crash Geth nodes during block processing",
5258
"description": "Full details to be disclosed at a later date",
5359
"links": [
54-
"https://blog.ethereum.org/2020/11/12/geth_security_release/"
60+
"https://blog.ethereum.org/2020/11/12/geth_security_release/",
61+
"https://github.com/ethereum/go-ethereum/security/advisories/GHSA-jm5c-rv3w-w83m"
5562
],
5663
"introduced": "v1.9.16",
5764
"fixed": "v1.9.18",
5865
"published": "2020-11-12",
5966
"severity": "Critical",
67+
"CVE": "CVE-2020-26242",
6068
"check": "Geth\\/v1\\.9.(16|17).*$"
6169
}
6270
]

cmd/geth/version_check_test.go

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ package main
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"io/ioutil"
2223
"path/filepath"
24+
"regexp"
25+
"strconv"
26+
"strings"
2327
"testing"
2428
)
2529

@@ -64,16 +68,63 @@ func testVerification(t *testing.T, pubkey, sigdir string) {
6468
}
6569
}
6670

67-
func TestJson(t *testing.T) {
68-
data, _ := ioutil.ReadFile("./testdata/vcheck/data2.json")
71+
func versionUint(v string) int {
72+
mustInt := func(s string) int {
73+
a, err := strconv.Atoi(s)
74+
if err != nil {
75+
panic(v)
76+
}
77+
return a
78+
}
79+
components := strings.Split(strings.TrimPrefix(v, "v"), ".")
80+
a := mustInt(components[0])
81+
b := mustInt(components[1])
82+
c := mustInt(components[2])
83+
return a*100*100 + b*100 + c
84+
}
85+
86+
// TestMatching can be used to check that the regexps are correct
87+
func TestMatching(t *testing.T) {
88+
data, _ := ioutil.ReadFile("./testdata/vcheck/vulnerabilities.json")
6989
var vulns []vulnJson
7090
if err := json.Unmarshal(data, &vulns); err != nil {
7191
t.Fatal(err)
7292
}
73-
if len(vulns) == 0 {
74-
t.Fatal("expected data, got none")
93+
check := func(version string) {
94+
vFull := fmt.Sprintf("Geth/%v-unstable-15339cf1-20201204/linux-amd64/go1.15.4", version)
95+
for _, vuln := range vulns {
96+
r, err := regexp.Compile(vuln.Check)
97+
vulnIntro := versionUint(vuln.Introduced)
98+
vulnFixed := versionUint(vuln.Fixed)
99+
current := versionUint(version)
100+
if err != nil {
101+
t.Fatal(err)
102+
}
103+
if vuln.Name == "Denial of service due to Go CVE-2020-28362" {
104+
// this one is not tied to geth-versions
105+
continue
106+
}
107+
if vulnIntro <= current && vulnFixed > current {
108+
// Should be vulnerable
109+
if !r.MatchString(vFull) {
110+
t.Errorf("Should be vulnerable, version %v, intro: %v, fixed: %v %v %v",
111+
version, vuln.Introduced, vuln.Fixed, vuln.Name, vuln.Check)
112+
}
113+
} else {
114+
if r.MatchString(vFull) {
115+
t.Errorf("Should not be flagged vulnerable, version %v, intro: %v, fixed: %v %v %d %d %d",
116+
version, vuln.Introduced, vuln.Fixed, vuln.Name, vulnIntro, current, vulnFixed)
117+
}
118+
}
119+
120+
}
75121
}
76-
if have, want := vulns[0].CVE, "correct"; have != want {
77-
t.Errorf("have %v, want %v", have, want)
122+
for major := 1; major < 2; major++ {
123+
for minor := 0; minor < 30; minor++ {
124+
for patch := 0; patch < 30; patch++ {
125+
vShort := fmt.Sprintf("v%d.%d.%d", major, minor, patch)
126+
check(vShort)
127+
}
128+
}
78129
}
79130
}

0 commit comments

Comments
 (0)