@@ -112,6 +112,41 @@ 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
+ path := "/proc/sys/kernel/yama/ptrace_scope"
125
+
126
+ // If the kernel does not have the YAMA module enabled,
127
+ // there will be no ptrace_scope file, which does not affect the tests.
128
+ if _ , err := os .Stat (path ); os .IsNotExist (err ) {
129
+ return
130
+ }
131
+
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
+ switch value {
141
+ case 3 :
142
+ t .Skip ("ptrace: Operation not permitted" )
143
+ case 2 :
144
+ if os .Geteuid () != 0 {
145
+ t .Skip ("ptrace: Operation not permitted with non-root user" )
146
+ }
147
+ }
148
+ }
149
+
115
150
// NOTE: the maps below are allocated larger than abi.MapBucketCount
116
151
// to ensure that they are not "optimized out".
117
152
@@ -197,6 +232,7 @@ func testGdbPython(t *testing.T, cgo bool) {
197
232
t .Parallel ()
198
233
checkGdbVersion (t )
199
234
checkGdbPython (t )
235
+ checkPtraceScope (t )
200
236
201
237
dir := t .TempDir ()
202
238
@@ -420,6 +456,7 @@ func TestGdbBacktrace(t *testing.T) {
420
456
checkGdbEnvironment (t )
421
457
t .Parallel ()
422
458
checkGdbVersion (t )
459
+ checkPtraceScope (t )
423
460
424
461
dir := t .TempDir ()
425
462
@@ -538,6 +575,7 @@ func TestGdbAutotmpTypes(t *testing.T) {
538
575
checkGdbEnvironment (t )
539
576
t .Parallel ()
540
577
checkGdbVersion (t )
578
+ checkPtraceScope (t )
541
579
542
580
if runtime .GOOS == "aix" && testing .Short () {
543
581
t .Skip ("TestGdbAutotmpTypes is too slow on aix/ppc64" )
@@ -612,6 +650,7 @@ func TestGdbConst(t *testing.T) {
612
650
checkGdbEnvironment (t )
613
651
t .Parallel ()
614
652
checkGdbVersion (t )
653
+ checkPtraceScope (t )
615
654
616
655
dir := t .TempDir ()
617
656
@@ -676,6 +715,7 @@ func TestGdbPanic(t *testing.T) {
676
715
checkGdbEnvironment (t )
677
716
t .Parallel ()
678
717
checkGdbVersion (t )
718
+ checkPtraceScope (t )
679
719
680
720
if runtime .GOOS == "windows" {
681
721
t .Skip ("no signals on windows" )
@@ -755,6 +795,7 @@ func TestGdbInfCallstack(t *testing.T) {
755
795
756
796
t .Parallel ()
757
797
checkGdbVersion (t )
798
+ checkPtraceScope (t )
758
799
759
800
dir := t .TempDir ()
760
801
0 commit comments