같은 프로그램을 실행할 때마다 가상 메모리 영역이 달라지는 이유는 무엇인가요?

같은 프로그램을 실행할 때마다 가상 메모리 영역이 달라지는 이유는 무엇인가요?

저는 Linux에서 메모리 영역의 가상 메모리 매핑 작업을 하고 있습니다. 실행 파일은 간단한 계산 프로그램입니다. 프로그램의 두 인스턴스가 실행될 때 아래에 표시된 매핑이 표시됩니다 /proc/pid/maps. 힙, 스택, vvars, vdso 등의 위치는 로드 시 무작위로 오프셋되는 것으로 나타납니다. 왜 이런 일을 하는가?

예시 1: 힙은 다음에서 시작됩니다.013f4000

00400000-00401000 r--p 00000000 08:16 3557412                            <program-exe>
00401000-00480000 r-xp 00001000 08:16 3557412                            <program-exe>
00480000-004a5000 r--p 00080000 08:16 3557412                            <program-exe>
004a6000-004ac000 rw-p 000a5000 08:16 3557412                            <program-exe>
004ac000-004ad000 rw-p 00000000 00:00 0 
013f4000-01417000 rw-p 00000000 00:00 0                                  [heap]
7ffd98bd8000-7ffd98bf9000 rw-p 00000000 00:00 0                          [stack]
7ffd98bfc000-7ffd98bff000 r--p 00000000 00:00 0                          [vvar]
7ffd98bff000-7ffd98c00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]

예시 2: 힙은 다음에서 시작됩니다.013cc000

00400000-00401000 r--p 00000000 08:16 3557412                            <program-exe>
00401000-00480000 r-xp 00001000 08:16 3557412                            <program-exe>
00480000-004a5000 r--p 00080000 08:16 3557412                            <program-exe>
004a6000-004ac000 rw-p 000a5000 08:16 3557412                            <program-exe>
004ac000-004ad000 rw-p 00000000 00:00 0 
013cc000-013ef000 rw-p 00000000 00:00 0                                  [heap]
7ffe3717d000-7ffe3719e000 rw-p 00000000 00:00 0                          [stack]
7ffe371fa000-7ffe371fd000 r--p 00000000 00:00 0                          [vvar]
7ffe371fd000-7ffe371fe000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]

답변1

이는 ASLR(Address Space Layout Randomization)이라는 보안 기능의 결과입니다. ASLR이 활성화되면 커널은 중요한 프로세스 세그먼트를 임의의 주소에 로드합니다. 모든 최신 Linux 커널은 기본적으로 ASLR을 활성화합니다. ASLR은 를 통해 제어할 수 있습니다 /proc/sys/kernel/randomize_va_space. ~에서절차 매뉴얼 페이지:

/proc/sys/kernel/randomize_va_space (Linux 2.6.12부터) 시스템의 ASLR(Address Space Layout Randomization) 정책을 선택합니다(ASLR을 지원하는 아키텍처에서). 이 파일은 세 가지 값을 지원합니다.

          0  Turn ASLR off.  This is the default for architectures that
             don't support ASLR, and when the kernel is booted with the
             norandmaps parameter.

          1  Make the addresses of mmap(2) allocations, the stack, and
             the VDSO page randomized.  Among other things, this means
             that shared libraries will be loaded at randomized
             addresses.  The text segment of PIE-linked binaries will
             also be loaded at a randomized address.  This value is the
             default if the kernel was configured with CONFIG_COM‐
             PAT_BRK.

          2  (Since Linux 2.6.25) Also support heap randomization.  This
             value is the default if the kernel was not configured with
             CONFIG_COMPAT_BRK.

따라서 ASLR을 일시적으로 비활성화하려면 루트로 다음 명령을 실행하십시오.

echo 0 > /proc/sys/kernel/randomize_va_space

이제 테스트를 다시 실행하면 두 프로세스 모두 동일한 주소 매핑을 갖게 되는 것을 확인할 수 있습니다.

관련 정보