Skip to content

Commit 87a89fa

Browse files
fuowanggopherbot
authored andcommitted
runtime: add the checkPtraceScope to skip certain tests
When the kernel parameter ptrace_scope is set to 2 or 3, certain test cases in runtime-gdb_test.go will fail. We should skip these tests. Fixes #69932 Change-Id: I685d1217f1521d7f8801680cf6b71d8e7a265188 GitHub-Last-Rev: 063759e GitHub-Pull-Request: #69933 Reviewed-on: https://go-review.googlesource.com/c/go/+/620857 Auto-Submit: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
1 parent 0b4168f commit 87a89fa

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/runtime/runtime-gdb_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,44 @@ func checkCleanBacktrace(t *testing.T, backtrace string) {
112112
// TODO(mundaym): check for unknown frames (e.g. "??").
113113
}
114114

115+
// checkPtraceScope checks the value of the kernel parameter ptrace_scope,
116+
// skips the test when gdb cannot attach to the target process via ptrace.
117+
// See issue 69932
118+
//
119+
// 0 - Default attach security permissions.
120+
// 1 - Restricted attach. Only child processes plus normal permissions.
121+
// 2 - Admin-only attach. Only executables with CAP_SYS_PTRACE.
122+
// 3 - No attach. No process may call ptrace at all. Irrevocable.
123+
func checkPtraceScope(t *testing.T) {
124+
if runtime.GOOS != "linux" {
125+
return
126+
}
127+
128+
// If the Linux kernel does not have the YAMA module enabled,
129+
// there will be no ptrace_scope file, which does not affect the tests.
130+
path := "/proc/sys/kernel/yama/ptrace_scope"
131+
if _, err := os.Stat(path); os.IsNotExist(err) {
132+
return
133+
}
134+
135+
data, err := os.ReadFile(path)
136+
if err != nil {
137+
t.Fatalf("failed to read file: %v", err)
138+
}
139+
value, err := strconv.Atoi(strings.TrimSpace(string(data)))
140+
if err != nil {
141+
t.Fatalf("failed converting value to int: %v", err)
142+
}
143+
switch value {
144+
case 3:
145+
t.Skip("skipping ptrace: Operation not permitted")
146+
case 2:
147+
if os.Geteuid() != 0 {
148+
t.Skip("skipping ptrace: Operation not permitted with non-root user")
149+
}
150+
}
151+
}
152+
115153
// NOTE: the maps below are allocated larger than abi.MapBucketCount
116154
// to ensure that they are not "optimized out".
117155

@@ -194,6 +232,7 @@ func testGdbPython(t *testing.T, cgo bool) {
194232
t.Parallel()
195233
checkGdbVersion(t)
196234
checkGdbPython(t)
235+
checkPtraceScope(t)
197236

198237
dir := t.TempDir()
199238

@@ -417,6 +456,7 @@ func TestGdbBacktrace(t *testing.T) {
417456
checkGdbEnvironment(t)
418457
t.Parallel()
419458
checkGdbVersion(t)
459+
checkPtraceScope(t)
420460

421461
dir := t.TempDir()
422462

@@ -531,6 +571,7 @@ func TestGdbAutotmpTypes(t *testing.T) {
531571
checkGdbEnvironment(t)
532572
t.Parallel()
533573
checkGdbVersion(t)
574+
checkPtraceScope(t)
534575

535576
if runtime.GOOS == "aix" && testing.Short() {
536577
t.Skip("TestGdbAutotmpTypes is too slow on aix/ppc64")
@@ -616,6 +657,7 @@ func TestGdbConst(t *testing.T) {
616657
checkGdbEnvironment(t)
617658
t.Parallel()
618659
checkGdbVersion(t)
660+
checkPtraceScope(t)
619661

620662
dir := t.TempDir()
621663

@@ -680,6 +722,7 @@ func TestGdbPanic(t *testing.T) {
680722
checkGdbEnvironment(t)
681723
t.Parallel()
682724
checkGdbVersion(t)
725+
checkPtraceScope(t)
683726

684727
if runtime.GOOS == "windows" {
685728
t.Skip("no signals on windows")
@@ -759,6 +802,7 @@ func TestGdbInfCallstack(t *testing.T) {
759802

760803
t.Parallel()
761804
checkGdbVersion(t)
805+
checkPtraceScope(t)
762806

763807
dir := t.TempDir()
764808

0 commit comments

Comments
 (0)