Background: _rtld_global
_rtld_global
๋ฆฌ๋
์ค ํ๋ก๊ทธ๋จ์ ์คํํ๋ฉด ํ๋ก๊ทธ๋จ์ ๋ช
์๋ ์ฝ๋์ ๊ฒฐ๊ณผ๊ฐ์ด ์ฐ๋ฆฌ์๊ฒ ์ถ๋ ฅ๋ฉ๋๋ค.
ํ๋ก๊ทธ๋จ์ด ์คํ๋์ด ํ๋ก์ธ์ค๋ก ๋ฑ๋ก ์ ํ๋ก๊ทธ๋จ์ ๋ช
์๋ ์ฝ๋๋ฟ๋ง ์๋ ํ๋ก๊ทธ๋จ์์
์ฌ์ฉ๋๋ ๋ณ์ ๊ด๋ฆฌ๋ฅผ ์ํ ์ฌ์ญ์ ํ ๋นํ๋ ๋ฑ์ ๋ค์ํ ์ฝ๋๊ฐ ๋ก๋์ ์ํด ์คํ๋ฉ๋๋ค.
ํ๋ก์ธ์ค๊ฐ ๋ฑ๋ก๋๊ฑฐ๋ ์ข
๋ฃ ์ ์ฐ์ด๋ ๋ณ์์ ์์ญ์ ์ ์ ์๋ค๋ฉด ๋ค์ํ ๋ฐฉ์์ ๊ณต๊ฒฉ ๊ธฐ๋ฒ ๊ฐ๋ฐ!
๊ทธ๋ฌ๋ฏ๋ก ์ ํฌ ๋ณด์์ชฝ์์ ์ด๋ฐ ๊ตฌ์กฐ์ ๊ตฌ๋์ ์ดํดํด์ผํฉ๋๋ค ๊ฐ์ด ์์๋ด
์๋ค.
12345 // Name: rtld.c// Compile: gcc -o rtld rtld.cint main() {return 0;}cs
__GI_exit
์์ ์์ ์ฝ๋๋ฅผ ๋ณด์๋ฉด ๋ณ๋ค๋ฅธ ์ฝ๋๋ฅผ ์คํX ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.
์ข
๋ฃ์์ ์ฐ๋ฆฌ๊ฐ ๋ชจ๋ฅด๋ ๋ง์ ์ฝ๋๋ค์ด ๋ด๋ถ์ ์ผ๋ก ์คํ๋๋๋ฐ, ํ๋ฒ ์ดํด๋ด
์๋ค.
์ผ๋จ main ํจ์ ๋ด ๋ฆฌํดํ๋ ๋ช
๋ น์ด์ ๋ธํฌ๋ฅผ ์ค์ si๋ฅผ ํตํด ๋ค์ ์ฝ๋๋ฅผ ์ดํด๋ณด๋ฉด..
๋๋ฒ๊น
๊ฒฐ๊ณผ์ main ํจ์ ๋ด์์ ๋ฆฌํด ๋ช
๋ น์ด๋ฅผ ์คํ ์ ์คํ ์ต์๋จ์ ์๋ __lib_start_main+122์
์ฝ๋๊ฐ ์คํ๋๊ณ , ๋ด๋ถ์ exitํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
์์ ์ค๋ฅธ์ชฝ์ exitํจ์์ ๋ด๋ถ ๋ชจ์ต์, ๋ ๋ค๋ฅธ __run_exit_handlersํจ์๊ฐ ๋ฑ์ฅํฉ๋๋ค.
ํด๋น ํจ์๋ ์ฝ๋์ ํฌ๊ธฐ๊ฐ ํฌ๋ฏ๋ก, lib์ฝ๋๋ฅผ ํตํด ๋ถ์ํด๋ด
์๋ค.
__run_exit_handlers
์ด์ ๋ถํด Dreamhack์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํฉ์๋ค..(์ค์ต์ด ์๋๊ฒ ๋ค์ ใ )
__run_exit_handlers ํจ์
12345678910111213141516171819202122232425262728293031323334353637 voidattribute_hidden__run_exit_handlers (int status, struct exit_function_list **listp,bool run_list_atexit, bool run_dtors){const struct exit_function *const f = &cur->fns[--cur->idx];switch (f->flavor){void (*atfct) (void);void (*onfct) (int status, void *arg);void (*cxafct) (void *arg, int status);case ef_free:case ef_us:break;case ef_on:onfct = f->func.on.fn;#ifdef PTR_DEMANGLEPTR_DEMANGLE (onfct);#endifonfct (status, f->func.on.arg);break;case ef_at:atfct = f->func.at;#ifdef PTR_DEMANGLEPTR_DEMANGLE (atfct);#endifatfct ();break;case ef_cxa:cxafct = f->func.cxa.fn;#ifdef PTR_DEMANGLEPTR_DEMANGLE (cxafct);#endifcxafct (f->func.cxa.arg, status);break;}}cs
__run_exit_handlers ํจ์์ ์ฝ๋๋ก, exit_function ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ ๋ณ์์ ๋ฐ๋ฅธ ํจ์ ํฌ์ธํฐ๋ฅผ ํธ์ถ
ํด๋น ๊ตฌ์กฐ์ฒด์ ๋ชจ์ต์ ์์ ๊ฐ์ผ๋ฉฐ, _dl_fini ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
exit_function ๊ตฌ์กฐ์ฒด
123456789101112131415161718192021 struct exit_function{/* `flavour' should be of type of the `enum' above but since we needthis element in an atomic operation we have to use `long int'. */long int flavor;union{void (*at) (void);struct{void (*fn) (int status, void *arg);void *arg;} on;struct{void (*fn) (void *arg, int status);void *arg;void *dso_handle;} cxa;} func;};cs
__dl_fini
__dl_fini ํจ์
123456789101112131415161718192021 struct exit_function{/* `flavour' should be of type of the `enum' above but since we needthis element in an atomic operation we have to use `long int'. */long int flavor;union{void (*at) (void);struct{void (*fn) (int status, void *arg);void *arg;} on;struct{void (*fn) (void *arg, int status);void *arg;void *dso_handle;} cxa;} func;};cs
_dl_fini ํจ์ ์ฝ๋์ ์ผ๋ถ์
๋๋ค. ์ฝ๋๋ฅผ _dl_load_lock์ ์ธ์๋ก __rtld_lock_lock_recursive
ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ๋งคํฌ๋ก๋ฅผ ํ์ธํด๋ณด๋ฉด, ํด๋น ํจ์๊ฐ..
dl_rtld_lock_recursive๋ผ๋ ํจ์ ํฌ์ธํฐ์์ ์ ์ ์์ต๋๋ค.
ํด๋น ํจ์ ํฌ์ธํฐ๋ _rtld_global ๊ตฌ์กฐ์ฒด์ ๋งด๋ฒ ๋ณ์์
๋๋ค.
ํด๋น ๊ตฌ์กฐ์ฒด๋ ๋งค์ฐ ๋ฐฉ๋ํ๊ธฐ ๋๋ฌธ์ ํจ์ ํฌ์ธํฐ์ ์ ๋ฌ๋๋ ์ธ์์ธ dl_load_lock๋ง์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
_rtld_global
gdb์์ _rtld_global ๊ตฌ์กฐ์ฒด๋ฅผ ์ถ๋ ฅํ ๋ชจ์ต์
๋๋ค.
๊ตฌ์กฐ์ฒด ๋ด _dl_rtld_lock_recursive ํจ์ ํฌ์ธํฐ์๋ rtld_lock_default_lock_recursive ํจ์ ์ฃผ์๋ฅผ ์ ์ฅํจ
๊ตฌ์กฐ์ฒด์ ํจ์ ํฌ์ธํฐ๊ฐ ์ ์ฅ๋ ์์ญ์ rw-๋ก์จ ๋ฎ์ด์ฐ๋ ๊ฒ๋ ๊ฐ๋ฅํฉ๋๋ค.
_rtld_global ๊ตฌ์กฐ์ฒด
_rtld_global ์ด๊ธฐํ
_rtld_global ํจ์ ํฌ์ธํฐ ์ด๊ธฐํ ์ฝ๋
1234567891011 static voiddl_main (const ElfW(Phdr) *phdr,ElfW(Word) phnum,ElfW(Addr) *user_entry,ElfW(auxv_t) *auxv){GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;#if defined SHARED && defined _LIBC_REENTRANT \&& defined __rtld_lock_default_lock_recursiveGL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;cs
ํ๋ก์ธ์ค๋ฅผ ๋ก๋ํ ๋ ํธ์ถ๋๋ dl_main ์ฝ๋์ ์ผ๋ถ๋ก, _rtld_global ๊ตฌ์กฐ์ฒด์
dl_rtld_lock_recursive ํจ์ ํฌ์ธํฐ๊ฐ ์ด๊ธฐํ๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
๋ง์น๋ฉฐ
๋ฆฌํด ๋ช
๋ น์ด๋ ์ค์ ๋ก ๋ก๋์์ ๋ค์ํ ํจ์๋ฅผ ํธ์ถํด ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํ๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
ํธ์ถํ๋ ํจ์ ์ค _rtld_global ๊ตฌ์กฐ์ฒด ๋ด ํจ์ ํฌ์ธํฐ๋ ํ๋ก์ธ์ค๊ฐ ์คํ๋๋ฉด์ ์ด๊ธฐํ๋๋ฉฐ,
์ด๋ ์ฝ๊ณ ์ธ ์ ์๋ ์์ญ์ ์์นํ๊ธฐ ๋๋ฌธ์ ์์ ์ฃผ์์ ๊ฐ์ ์ธ ์ ์๋ ์ทจ์ฝ์ ์ด ์๋ค๋ฉด
์ด๋ฅผ ๋ฎ์ด์ ์คํ ํ๋ฆ์ ์กฐ์ํ ์ ์์ต๋๋ค.
์ฐธ๊ณ ์๋ฃ
Background: _rtld_global
์ด๋ฒ ์ฝ์ค์์๋ ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋ ๋ ์คํ๋๋ ๋์์ ๋ํด ์ค๋ช ํ๊ณ , _rtld_global์ ๋ํด ์ค๋ช ํฉ๋๋ค.
dreamhack.io
์ฐธ๊ณ ์ด๋ฏธ์ง