Skip to content

Commit 495f91f

Browse files
committed
runtime: add the checkPtraceScope to skip certain tests
Fixes #69932
1 parent 70f4717 commit 495f91f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/runtime/runtime-gdb_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,43 @@ 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+
euid := os.Geteuid()
125+
path := "/proc/sys/kernel/yama/ptrace_scope"
126+
_, err := os.Stat(path)
127+
128+
// If the kernel does not have the YAMA module enabled,
129+
// there will be no ptrace_scope file, which does not affect the tests.
130+
if !os.IsNotExist(err) {
131+
//file ptrace_scope exists
132+
data, err := os.ReadFile(path)
133+
if err != nil {
134+
t.Fatalf("failed to read file: %v", err)
135+
}
136+
value, err := strconv.Atoi(strings.TrimSpace(string(data)))
137+
if err != nil {
138+
t.Fatalf("failed converting value to int: %v", err)
139+
}
140+
141+
switch value {
142+
case 3:
143+
t.Skip("ptrace: Operation not permitted")
144+
case 2:
145+
if euid != 0 {
146+
t.Skip("ptrace: Operation not permitted with non-root user")
147+
}
148+
}
149+
}
150+
}
151+
115152
// NOTE: the maps below are allocated larger than abi.MapBucketCount
116153
// to ensure that they are not "optimized out".
117154

@@ -197,6 +234,7 @@ func testGdbPython(t *testing.T, cgo bool) {
197234
t.Parallel()
198235
checkGdbVersion(t)
199236
checkGdbPython(t)
237+
checkPtraceScope(t)
200238

201239
dir := t.TempDir()
202240

@@ -420,6 +458,7 @@ func TestGdbBacktrace(t *testing.T) {
420458
checkGdbEnvironment(t)
421459
t.Parallel()
422460
checkGdbVersion(t)
461+
checkPtraceScope(t)
423462

424463
dir := t.TempDir()
425464

@@ -538,6 +577,7 @@ func TestGdbAutotmpTypes(t *testing.T) {
538577
checkGdbEnvironment(t)
539578
t.Parallel()
540579
checkGdbVersion(t)
580+
checkPtraceScope(t)
541581

542582
if runtime.GOOS == "aix" && testing.Short() {
543583
t.Skip("TestGdbAutotmpTypes is too slow on aix/ppc64")
@@ -612,6 +652,7 @@ func TestGdbConst(t *testing.T) {
612652
checkGdbEnvironment(t)
613653
t.Parallel()
614654
checkGdbVersion(t)
655+
checkPtraceScope(t)
615656

616657
dir := t.TempDir()
617658

@@ -676,6 +717,7 @@ func TestGdbPanic(t *testing.T) {
676717
checkGdbEnvironment(t)
677718
t.Parallel()
678719
checkGdbVersion(t)
720+
checkPtraceScope(t)
679721

680722
if runtime.GOOS == "windows" {
681723
t.Skip("no signals on windows")
@@ -755,6 +797,7 @@ func TestGdbInfCallstack(t *testing.T) {
755797

756798
t.Parallel()
757799
checkGdbVersion(t)
800+
checkPtraceScope(t)
758801

759802
dir := t.TempDir()
760803

0 commit comments

Comments
 (0)