@@ -112,6 +112,43 @@ 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
+ 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
+
115
152
// NOTE: the maps below are allocated larger than abi.MapBucketCount
116
153
// to ensure that they are not "optimized out".
117
154
@@ -197,6 +234,7 @@ func testGdbPython(t *testing.T, cgo bool) {
197
234
t .Parallel ()
198
235
checkGdbVersion (t )
199
236
checkGdbPython (t )
237
+ checkPtraceScope (t )
200
238
201
239
dir := t .TempDir ()
202
240
@@ -420,6 +458,7 @@ func TestGdbBacktrace(t *testing.T) {
420
458
checkGdbEnvironment (t )
421
459
t .Parallel ()
422
460
checkGdbVersion (t )
461
+ checkPtraceScope (t )
423
462
424
463
dir := t .TempDir ()
425
464
@@ -538,6 +577,7 @@ func TestGdbAutotmpTypes(t *testing.T) {
538
577
checkGdbEnvironment (t )
539
578
t .Parallel ()
540
579
checkGdbVersion (t )
580
+ checkPtraceScope (t )
541
581
542
582
if runtime .GOOS == "aix" && testing .Short () {
543
583
t .Skip ("TestGdbAutotmpTypes is too slow on aix/ppc64" )
@@ -612,6 +652,7 @@ func TestGdbConst(t *testing.T) {
612
652
checkGdbEnvironment (t )
613
653
t .Parallel ()
614
654
checkGdbVersion (t )
655
+ checkPtraceScope (t )
615
656
616
657
dir := t .TempDir ()
617
658
@@ -676,6 +717,7 @@ func TestGdbPanic(t *testing.T) {
676
717
checkGdbEnvironment (t )
677
718
t .Parallel ()
678
719
checkGdbVersion (t )
720
+ checkPtraceScope (t )
679
721
680
722
if runtime .GOOS == "windows" {
681
723
t .Skip ("no signals on windows" )
@@ -755,6 +797,7 @@ func TestGdbInfCallstack(t *testing.T) {
755
797
756
798
t .Parallel ()
757
799
checkGdbVersion (t )
800
+ checkPtraceScope (t )
758
801
759
802
dir := t .TempDir ()
760
803
0 commit comments