분할 오류로 인해 코어 덤프가 기록되지 않음

분할 오류로 인해 코어 덤프가 기록되지 않음

저는 현재 The Shellcoder Handbook: Finding and Exploiting Security Vulnerability, Second Edition을 읽고 있습니다.

이와 같은 간단한 버퍼 오버플로 문제(C 코드)는 2장에서 고려됩니다.

int main () {
    int array[5];
    int i;
    for (i = 0; i <= 255; i++ ) {
        array[i] = 10;
    }
}

작성자는 코드를 컴파일 cc하고 실행합니다.

shellcoders@debian:~/chapter_2$ cc buffer2.c
shellcoders@debian:~/chapter_2$ ./a.out
Segmentation fault (core dumped)

그런 다음 작성된 코어 덤프를 살펴보았습니다 gdb.

shellcoders@debian:~/chapter_2$ gdb -q -c core
Program terminated with signal 11, Segmentation fault.
#0 0x0000000a in ?? ()
(gdb)

문제는 제 경우에는 코어 덤프가 작성되지 않았다는 점입니다. 메시지가 하나뿐입니다: zsh: segmentation fault ./a.out.

VirtualBox에서 Kali 2021.4a를 사용하고 있습니다. 기본 셸을 변경해 보았지만 chsh -s /bin/bash아무 것도 변경되지 않고 터미널은 계속 열려 있습니다 zsh.

오류 시 코어 덤프를 작성하는 방법은 무엇입니까? 실행 파일과 동일한 디렉터리에 작성된 파일이어야 하는 것 같습니다.

답변1

ulimit먼저, 명령(bash 또는 zsh)을 사용하여 제한 내에서 코어 파일 크기를 확인 해야 합니다 .

# ulimit -c
0

0이면 늘려야 합니다. 예를 들어 무한대로 늘리면 다음과 같습니다.

# ulimit -c unlimited
# ulimit -c          
unlimited

둘째, 코어 덤프가 생성된 위치를 확인해야 합니다. 이전 배포판에서 기본값은 일반적으로 CWD에서 "core"라는 파일입니다. 이것은 아마도 글을 쓰는 시점의 기본 설정일 것입니다.

# /sbin/sysctl kernel.core_pattern
kernel.core_pattern = core

그러나 요즘 대부분의 배포판에서는 더 이상 그렇지 않습니다. 오늘날 코어는 일반적으로 다음을 사용하여 생성됩니다.systemd-coredump(8).

# /sbin/sysctl kernel.core_pattern
kernel.core_pattern = |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %e

남자들에게서systemd-coredump(8):

기본적으로 systemd-coredump는 가능한 경우 트레이스백을 포함하여 코어 덤프를 로그에 기록하고 코어 덤프(프로세스 메모리 내용의 이미지) 자체를 외부 파일에 저장합니다. /var/lib/systemd/coredump.

이 디렉토리에서 코어 덤프를 찾거나 다음을 사용할 수 있습니다.coredumpctl다음을 나열하십시오(sudo 또는 루트로 실행해야 할 수도 있음).

# coredumpctl list
TIME                            PID   UID   GID SIG PRESENT EXE
Wed 2022-01-26 12:53:06 IST   10347   111   222 11  *       /tmp/a.out

*아래 "PRESENT"는 코어 덤프 파일이 생성되었음을 나타냅니다.

다음 명령을 사용하여 (압축된) 코어 덤프의 위치를 ​​볼 수 있습니다 coredumpctl info <pid>.

# coredumpctl info 10347 |grep Coredump
      Coredump: /var/lib/systemd/coredump/core.a\x2eout.111.1bd8e22a25e844f1b03a87d378b4ed9b.10347.1643194386000000.xz

xz다음 명령을 사용하여 파일의 압축을 풀 수 있습니다 .

# xz --decompress --stdout '/var/lib/systemd/coredump/core.a\x2eout.111.1bd8e22a25e844f1b03a87d378b4ed9b.10347.1643194386000000.xz' > core

또는 다음 명령을 사용하여 코어를 일부 대상으로 덤프합니다.

# coredumpctl -o core dump 10347

다음 명령 중 하나가 파일을 생성합니다.

# gdb -q -c core
[New LWP 10347]
Core was generated by `/tmp/a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000af7a56725 in ?? ()

답변2

에 따르면 man core코어 덤프 파일이 생성되지 않는 몇 가지 상황이 있습니다.

   *  The process does not have permission to write the core file.…

   *  A (writable, regular) file with the same name as would be used
      for the core dump already exists, but there is more than one
      hard link to that file.

   *  The filesystem where the core dump file would be created is
      full; or has run out of inodes; or is mounted read-only; or
      the user has reached their quota for the filesystem.

   *  The directory in which the core dump file is to be created
      does not exist.

   *  The RLIMIT_CORE (core file size) or RLIMIT_FSIZE (file size)
      resource limits for the process are set to zero;…

   *  The binary being executed by the process does not have read
      permission enabled.…

   *  The process is executing a set-user-ID (set-group-ID) program
      that is owned by a user (group) other than the real user
      (group) ID of the process, or the process is executing a
      program that has file capabilities…

   *  /proc/sys/kernel/core_pattern is empty and
      /proc/sys/kernel/core_uses_pid contains the value 0.  

   *  (Since Linux 3.7) The kernel was configured without the
      CONFIG_COREDUMP option.

따라서 이러한 조건을 먼저 확인해야 하며, 특히 가장 가능성이 높은 마지막 조건을 확인해야 합니다.CONFIG_COREDUMP 커널 옵션.

그런데 다음 사항도 참고하세요.

systemd(1)을 init 프레임워크로 사용하는 시스템에서는 코어 덤프가 systemd(1)에 의해 결정된 위치에 배치될 수 있습니다.

많은 구현에서 이는 일부 /var/lib/systemd하위 디렉터리에 해당합니다.

관련 정보