@@ -112,6 +112,44 @@ func checkCleanBacktrace(t *testing.T, backtrace string) {
112
112
// TODO(mundaym): check for unknown frames (e.g. "??").
113
113
}
114
114
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
+
115
153
// NOTE: the maps below are allocated larger than abi.MapBucketCount
116
154
// to ensure that they are not "optimized out".
117
155
@@ -194,6 +232,7 @@ func testGdbPython(t *testing.T, cgo bool) {
194
232
t .Parallel ()
195
233
checkGdbVersion (t )
196
234
checkGdbPython (t )
235
+ checkPtraceScope (t )
197
236
198
237
dir := t .TempDir ()
199
238
@@ -417,6 +456,7 @@ func TestGdbBacktrace(t *testing.T) {
417
456
checkGdbEnvironment (t )
418
457
t .Parallel ()
419
458
checkGdbVersion (t )
459
+ checkPtraceScope (t )
420
460
421
461
dir := t .TempDir ()
422
462
@@ -531,6 +571,7 @@ func TestGdbAutotmpTypes(t *testing.T) {
531
571
checkGdbEnvironment (t )
532
572
t .Parallel ()
533
573
checkGdbVersion (t )
574
+ checkPtraceScope (t )
534
575
535
576
if runtime .GOOS == "aix" && testing .Short () {
536
577
t .Skip ("TestGdbAutotmpTypes is too slow on aix/ppc64" )
@@ -616,6 +657,7 @@ func TestGdbConst(t *testing.T) {
616
657
checkGdbEnvironment (t )
617
658
t .Parallel ()
618
659
checkGdbVersion (t )
660
+ checkPtraceScope (t )
619
661
620
662
dir := t .TempDir ()
621
663
@@ -680,6 +722,7 @@ func TestGdbPanic(t *testing.T) {
680
722
checkGdbEnvironment (t )
681
723
t .Parallel ()
682
724
checkGdbVersion (t )
725
+ checkPtraceScope (t )
683
726
684
727
if runtime .GOOS == "windows" {
685
728
t .Skip ("no signals on windows" )
@@ -759,6 +802,7 @@ func TestGdbInfCallstack(t *testing.T) {
759
802
760
803
t .Parallel ()
761
804
checkGdbVersion (t )
805
+ checkPtraceScope (t )
762
806
763
807
dir := t .TempDir ()
764
808
0 commit comments