How to solve "ptrace operation not permitted" when trying to attach GDB to a process?

CLinuxDebuggingGdbStrace

C Problem Overview


I'm trying to attach a program with gdb but it returns:

> Attaching to process 29139
> Could not attach to process. If your uid matches the uid of the target > process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try > again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
> ptrace: Operation not permitted.

gdb-debugger returns "Failed to attach to process, please check privileges and try again."

strace returns "attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted"

I changed "kernel.yama.ptrace_scope" 1 to 0 and /proc/sys/kernel/yama/ptrace_scope 1 to 0 and tried set environment LD_PRELOAD=./ptrace.so with this:

#include <stdio.h>
int ptrace(int i, int j, int k, int l) {
    printf(" ptrace(%i, %i, %i, %i), returning -1\n", i, j, k, l);
    return 0;
}

But it still returns the same error. How can I attach it to debuggers?

C Solutions


Solution 1 - C

If you are using Docker, you will probably need these options:

docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined

If you are using Podman, you will probably need its --cap-add option too:

podman run --cap-add=SYS_PTRACE

Solution 2 - C

This is due to kernel hardening in Linux; you can disable this behavior by echo 0 > /proc/sys/kernel/yama/ptrace_scope or by modifying it in /etc/sysctl.d/10-ptrace.conf

See also this article about it in Fedora 22 (with links to the documentation) and this comment thread about Ubuntu and .

Solution 3 - C

I would like to add that I needed --security-opt apparmor=unconfined along with the options that @wisbucky mentioned. This was on Ubuntu 18.04 (both Docker client and host). Therefore, the full invocation for enabling gdb debugging within a container is:

docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --security-opt apparmor=unconfined

Solution 4 - C

Not really addressing the above use-case but I had this problem:

Problem: It happened that I started my program with sudo, so when launching gdb it was giving me ptrace: Operation not permitted.

Solution: sudo gdb ...

Solution 5 - C

Just want to emphasize a related answer. Let's say that you're root and you've done:

strace -p 700

and get:

strace: attach: ptrace(PTRACE_SEIZE, 700): Operation not permitted

Check:

grep TracerPid /proc/700/status

If you see something like TracerPid: 12, i.e. not 0, that's the PID of the program that is already using the ptrace system call. Both gdb and strace use it, and there can only be one active at a time.

Solution 6 - C

As most of us land here for Docker issues I'll add the Kubernetes answer as it might come in handy for someone...


You must add the SYS_PTRACE capability in your pod's security context at spec.containers.securityContext:

       securityContext:
          capabilities:
            add: [ "SYS_PTRACE" ]

There are 2 securityContext keys at 2 different places. If it tells you that the key is not recognized than you missplaced it. Try the other one.

You probably need to have a root user too as default. So in the other security context (spec.securityContext) add :

      securityContext:
        runAsUser: 0
        runAsGroup: 0
        fsGroup: 101

FYI : 0 is root. But the fsGroup value is unknown to me. For what I'm doing I don't care but you might.

Now you can do :

strace -s 100000 -e write=1  -e trace=write -p 16

You won't get the permission denied anymore !

BEWARE : This is the Pandora box. Having this in production it NOT recommended.

Solution 7 - C

I was running my code with higher privileges to deal with Ethernet Raw Sockets by setting set capability command in Debian Distribution. I tried the above solution: echo 0 > /proc/sys/kernel/yama/ptrace_scope or by modifying it in /etc/sysctl.d/10-ptrace.conf but that did not work for me.

Additionally, I also tried with set capabilities command for gdb in installed directory (usr/bin/gdb) and it works: /sbin/setcap CAP_SYS_PTRACE=+eip /usr/bin/gdb. Be sure to run this command with root privileges.

Solution 8 - C

Jesup's answer is correct; it is due to Linux kernel hardening. In my case, I am using Docker Community for Mac, and in order to do change the flag I must enter the LinuxKit shell using justin cormack's nsenter (ref: https://www.bretfisher.com/docker-for-mac-commands-for-getting-into-local-docker-vm/ ).

docker run -it --rm --privileged --pid=host justincormack/nsenter1

> / # cat /etc/issue > > Welcome to LinuxKit > > ## . > ## ## ## == > ## ## ## ## ## === > /"""""""""""""""""_/ === > { / ===- > ____ O / > \ \ / > _____/ > > / # cat /proc/sys/kernel/yama/ptrace_scope > > 1 > > / # echo 0 > /proc/sys/kernel/yama/ptrace_scope > > / # exit

Solution 9 - C

Maybe someone has attached this process with gdb.

  • ps -ef | grep gdb

can't gdb attach the same process twice.

Solution 10 - C

I was going to answer this old question as it is unaccepted and any other answers are not got the point. The real answer may be already written in /etc/sysctl.d/10-ptrace.conf as it is my case under Ubuntu. This file says:

> For applications launching crash handlers that need PTRACE, exceptions can be registered by the debugee by declaring in the segfault handler specifically which process will be using PTRACE on the debugee: prctl(PR_SET_PTRACER, debugger_pid, 0, 0, 0);

So just do the same thing as above: keep /proc/sys/kernel/yama/ptrace_scope as 1 and add prctl(PR_SET_PTRACER, debugger_pid, 0, 0, 0); in the debugee. Then the debugee will allow debugger to debug it. This works without sudo and without reboot.

Usually, debugee also need to call waitpid to avoid exit after crash so debugger can find the pid of debugee.

Solution 11 - C

If permissions are a problem, you probably will want to use gdbserver. (I almost always use gdbserver when I gdb, docker or no, for numerous reasons.) You will need gdbserver (Deb) or gdb-gdbserver (RH) installed in the docker image. Run the program in docker with

$ sudo gdbserver :34567 myprogram arguments

(pick a port number, 1025-65535). Then, in gdb on the host, say

(gdb) target remote 172.17.0.4:34567

where 172.17.0.4 is the IP address of the docker image as reported by /sbin/ip addr list run in the docker image. This will attach at a point before main runs. You can tb main and c to stop at main, or wherever you like. Run gdb under cgdb, emacs, vim, or even in some IDE, or plain. You can run gdb in your source or build tree, so it knows where everything is. (If it can't find your sources, use the dir command.) This is usually much better than running it in the docker image.

gdbserver relies on ptrace, so you will also need to do the other things suggested above. --privileged --pid=host sufficed for me.

If you deploy to other OSes or embedded targets, you can run gdbserver or a gdb stub there, and run gdb the same way, connecting across a real network or even via a serial port (/dev/ttyS0).

Solution 12 - C

I don't know what you are doing with LD_PRELOAD or your ptrace function.

Why don't you try attaching gdb to a very simple program? Make a program that simply repeatedly prints Hello or something and use gdb --pid [hello program PID] to attach to it.

If that does not work then you really do have a problem.

Another issue is the user ID. Is the program that you are tracing setting itself to another UID? If it is then you cannot ptrace it unless you are using the same user ID or are root.

Solution 13 - C

I have faced the same problem and try a lot of solution but finally, I have found the solution, but really I don't know what the problem was. First I modified the ptrace_conf value and login into Ubuntu as a root but the problem still appears. But the most strange thing that happened is the gdb showed me a message that says:

Could not attach to process. If your uid matches the uid of the target process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user.
For more details, see /etc/sysctl.d/10-ptrace.conf warning: process 3767 is already traced by process 3755 ptrace: Operation not permitted.

With ps command terminal, the process 3755 was not listed.

I found the process 3755 in /proc/$pid but I don't understand what was it!!

Finally, I deleted the target file (foo.c) that I try to attach it vid gdb and tracer c program using PTRACE_ATTACH syscall, and in the other folder, I created another c program and compiled it.

the problem is solved and I was enabled to attach to another process either by gdb or ptrace_attach syscall.

(gdb) attach 4416

Attaching to process 4416

and I send a lot of signals to process 4416. I tested it with both gdb and ptrace, both of them run correctly.

really I don't know the problem what was, but I think it is not a bug in Ubuntu as a lot of sites have referred to it, such <https://askubuntu.com/questions/143561/why-wont-strace-gdb-attach-to-a-process-even-though-im-root>

Solution 14 - C

Extra information

If you wanna make changes in the interfaces such as add the ovs bridge, you must use --privileged instead of --cap-add NET_ADMIN.

sudo docker run -itd --name=testliz --privileged --cap-add=SYS_PTRACE --security-opt seccomp=unconfined ubuntu

Solution 15 - C

If you are using FreeBSD, edit /etc/sysctl.conf, change the line

security.bsd.unprivileged_proc_debug=0

to

security.bsd.unprivileged_proc_debug=1

Then reboot.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbbaytemirView Question on Stackoverflow
Solution 1 - CwisbuckyView Answer on Stackoverflow
Solution 2 - CjesupView Answer on Stackoverflow
Solution 3 - CJuraj OršulićView Answer on Stackoverflow
Solution 4 - CMaxim ChetruscaView Answer on Stackoverflow
Solution 5 - CNagevView Answer on Stackoverflow
Solution 6 - CDoctorView Answer on Stackoverflow
Solution 7 - CSufABView Answer on Stackoverflow
Solution 8 - CYudhiWidyatamaView Answer on Stackoverflow
Solution 9 - CRaymondView Answer on Stackoverflow
Solution 10 - Clinux40View Answer on Stackoverflow
Solution 11 - CNathan MyersView Answer on Stackoverflow
Solution 12 - CZan LynxView Answer on Stackoverflow
Solution 13 - Chusin alhaj ahmadeView Answer on Stackoverflow
Solution 14 - CLIZ AGUIRREView Answer on Stackoverflow
Solution 15 - CBruno HaibleView Answer on Stackoverflow