code

GDB를 프로세스에 연결하려고 할 때 "ptrace 작업이 허용되지 않음"을 해결하는 방법은 무엇입니까?

codestyles 2021. 1. 9. 09:56
반응형

GDB를 프로세스에 연결하려고 할 때 "ptrace 작업이 허용되지 않음"을 해결하는 방법은 무엇입니까?


gdb로 프로그램을 첨부하려고하는데 다음을 반환합니다.

프로세스에 연결 29139 프로세스에 연결할
수 없습니다. uid가 대상 프로세스의 uid와 일치하면 / proc / sys / kernel / yama / ptrace_scope의 설정을 확인하거나 루트 사용자로 다시 시도하십시오. 자세한 내용은 /etc/sysctl.d/10-ptrace.conf
ptrace : 작업이 허용되지 않음을 참조하십시오 .

gdb-debugger는 "프로세스에 연결하지 못했습니다. 권한을 확인하고 다시 시도하십시오."를 반환합니다.

strace는 "attach : ptrace (PTRACE_ATTACH, ...) : 작업이 허용되지 않음"을 반환합니다.

"kernel.yama.ptrace_scope"1을 0으로, /proc/sys/kernel/yama/ptrace_scope1을 0으로 변경하고 다음과 같이 시도 set environment LD_PRELOAD=./ptrace.so했습니다.

#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;
}

그러나 여전히 동일한 오류를 반환합니다. 디버거에 어떻게 연결할 수 있습니까?


Docker를 사용하는 경우 다음 옵션이 필요할 수 있습니다.

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

이는 Linux의 커널 강화 때문입니다. 이 동작을 비활성화 echo 0 > /proc/sys/kernel/yama/ptrace_scope하거나/etc/sysctl.d/10-ptrace.conf

참조 페도라 (22)에 대한이 문서 (문서에 대한 링크) 및 우분투에 대한이 댓글 스레드 와.


--security-opt apparmor=unconfined@wisbucky가 언급 한 옵션과 함께 필요한 사항을 추가하고 싶습니다 . 이것은 Ubuntu 18.04 (Docker 클라이언트 및 호스트 모두)에있었습니다. 따라서 컨테이너 내에서 gdb 디버깅을 활성화하기위한 전체 호출은 다음과 같습니다.

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


위의 사용 사례를 실제로 다루지는 않았지만이 문제가있었습니다.

문제점 : 제가 프로그램을 시작 sudo했을 때 gdb를 시작할 때 저에게 주어 ptrace: Operation not permitted졌습니다.

해결책 :sudo gdb ...


누군가이 프로세스를 gdb에 첨부했을 수 있습니다.

  • ps -ef | grep gdb

gdb는 동일한 프로세스를 두 번 연결할 수 없습니다.


Jesup의 대답은 정확합니다. Linux 커널 강화 때문입니다. 제 경우에는 Mac 용 Docker Community를 사용하고 있으며 플래그를 변경하려면 justin cormack의 nsenter (참조 : https://www.bretfisher.com/docker-for-mac-commands)를 사용하여 LinuxKit 셸을 입력해야합니다. -for-getting-into-local-docker-vm / ).

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

/ # 고양이 / etc / issue

LinuxKit에 오신 것을 환영합니다

                    ##         .
              ## ## ##        ==
           ## ## ## ## ##    ===
       /"""""""""""""""""\___/ ===
      {                       /  ===-
       \______ O           __/
         \    \         __/
          \____\_______/

/ # 고양이 / proc / sys / kernel / yama / ptrace_scope

1

/ # 에코 0> / proc / sys / kernel / yama / ptrace_scope

/ # 종료


나는 당신이 LD_PRELOAD 또는 ptrace 함수로 무엇을하는지 모르겠습니다.

아주 간단한 프로그램에 gdb를 첨부 해보는 건 어떨까요? 단순히 Hello 등을 출력하는 프로그램을 만들고 gdb --pid [hello program PID]를 사용하여 첨부합니다.

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.


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

ReferenceURL : https://stackoverflow.com/questions/19215177/how-to-solve-ptrace-operation-not-permitted-when-trying-to-attach-gdb-to-a-pro

반응형