Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
linux_10.pdf
Search
Yosuke Furukawa
PRO
July 30, 2015
0
29
linux_10.pdf
Yosuke Furukawa
PRO
July 30, 2015
Tweet
Share
More Decks by Yosuke Furukawa
See All by Yosuke Furukawa
Welcome JSConf.jp 2024
yosuke_furukawa
PRO
0
230
tc39 x jsconf.jp Panel Discussion 2024
yosuke_furukawa
PRO
0
150
Removing Corepack
yosuke_furukawa
PRO
9
1.3k
JavaScript Runtime とはなにか
yosuke_furukawa
PRO
15
2.5k
Strip Types と Storage
yosuke_furukawa
PRO
4
340
Module Harmony について
yosuke_furukawa
PRO
3
1.5k
LTのやり方
yosuke_furukawa
PRO
16
2.2k
AppRouter Panel Talk
yosuke_furukawa
PRO
3
700
Node.js v22 で変わること
yosuke_furukawa
PRO
13
5.3k
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
120
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Side Projects
sachag
452
42k
How to Ace a Technical Interview
jacobian
276
23k
Thoughts on Productivity
jonyablonski
67
4.3k
Docker and Python
trallard
40
3.1k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
110
49k
A Philosophy of Restraint
colly
203
16k
Transcript
ৄղLinuxΧʔωϧྠಡձ ୈ10ষγεςϜίʔϧ @yosuke_furukawa
γεςϜίʔϧ • CPUɺσΟεΫɺϓϦϯλͱ͍ͬͨϋʔυΣΞͱ ϢʔβʔϓϩηεΛܨ͙ݴ༿ • ͜Ε͕ͳ͍ͱϋʔυΣΞʹAPIͰ͢ඞཁ͕ ͋Δ • ϢʔβʔϓϩηεΧʔωϧʹγεςϜίʔϧΛݺ ͼग़͢͜ͱͰϋʔυΣΞʹ໋ྩͰ͖ΔΑ͏ʹͳΔɻ
APIͱγεςϜίʔϧͷҧ͍ • APIؔΠϯλϑΣʔε • γεςϜίʔϧιϑτΣΞׂΓࠐΈΛར ༻ͯ͠ߦΘΕΔΧʔωϧʹର͢Δ໌ࣔతཁٻ • γεςϜίʔϧΛϥοϓͯ͠libcͷAPIͱͯ͠ ఏڙ͞Ε͍ͯΔɻ
ྫ͑ • malloc/calloc/freebrk() γεςϜίʔϧΛར༻͠ ͯϝϞϦׂΓͯղ์ͷཧΛߦ͏API • γεςϜίʔϧͱͦΕΛϥοϓ͢ΔAPIجຊతʹ ϢʔβʔϓϩηεΛѻ͏σϕϩούʹͱͬͯAPI ܦ༝Ͱ৮ΔͷͰมΘΒͳ͍͕ɺΧʔωϧૢ࡞͢Δ σϕϩούʹͱͬͯҟͳΔࣄΛҙࣝ͢Δඞཁ͕
͋Δɻ
γεςϜίʔϧϋϯυϥͱγε ςϜίʔϧαʔϏεϧʔνϯ • γεςϜίʔϧ͕ൃߦ͞ΕΔ·ͰͷྲྀΕ 1. ϢʔβʔϞʔυϓϩηε͕γεςϜίʔϧΛൃߦ͢Δ 2. ͜ͷ࣌ʹΞηϯϒϦͰॻ͔ΕͨγεςϜίʔϧϋϯυϥΛݺͼग़͢ 3. ΧʔωϧϞʔυελοΫ্ʹϨδελͷΛୀආͤͯ͞ɺΧʔωϧϞʔ
υʹͳΔ 4. γεςϜίʔϧαʔϏεϧʔνϯͱݺΕΔCͷؔΛݺͼग़ͯ͠ɺγ εςϜίʔϧΛॲཧ͢Δ 5. ୀආͨ͠ϨδελΛݩʹͯ͠CPUΛϢʔβʔϞʔυʹΓସ͑Δ
ਤʹ͢Δͱ͜͏
͜͜ͰୈҰষΧʔωϧϞʔυͱ ϢʔβʔϞʔυͷҧ͍ • ΧʔωϧϞʔυʹͳΔͱશϝϞϦۭؒʹΞΫ ηεՄೳ • γεςϜίʔϧൃߦͰ͖Δ • ೖग़ྗॲཧͰ͖Δ
γεςϜίʔϧͷೖޱͱग़ޱ • γεςϜίʔϧ2ͭͷೖޱͱग़ޱ͕͋Δɻ • Intel pentium 2 Ҏલͷݹ͍ํ๏ • ೖޱ:
int $0x80 ग़ޱ: iret • Intel pentium 2 Ҏ߱ͷ৽͍͠ํ๏ ͋ͱ ARMͬͪ͜ • ೖޱ: sysenter ग़ޱ: sysexit
৽͍͠ํ๏ 1.vsyscall 1.sysenter͕࣮͞ΕͯΔCPUͳΒsysenter 2.ແ͔ͬͨΒී௨ͷint $0x80 2.(͜͜ʹԶͨͪͷυϥϚ͕͋Δ) 1.ΧʔωϧϞʔυͷมߋ 2.Ϩδελͷୀආ 3.ϢʔβʔϞʔυʹ෮ؼ͢Δ 1.Կϑϥά()ཱ͕ͬͯͳ͔ͬͨΒ
sysexit Ͱଈ࠲ʹ෮ؼ 2.ϑϥάཱ͕ͬͯͨΒԿΒ͔ͷॲཧΛͯ͠ܧଓ or ෮ؼ
4ষͷ3અʹ͋ΔΧʔωϧೖΕࢠʹͳΔέʔε͕͋ΔɻԿΒ ͔ͷׂΓࠐΈॲཧ͕ൃੜ͢Δͱʢϑϥάཱ͕ͭͱʣϢʔβʔ ϞʔυʹΒͣΧʔωϧϞʔυͰॲཧΛܧଓ͢Δࣄ͋Δɻ
Ҿड͚͠ • γεςϜίʔϧؔͱಉ༷ʹҾΛड͚ ͢ॲཧ͕͋Δ • ྫɿmmap(void *addr, size_t len, int
prot, int flags, int fd, off_t offset);
Ҿड͚͠ • ௨ৗͷؔͷ߹ • ελοΫʹҾ٧Ή • ϩʔΧϧมͷѻ͍ͱಉ͡ • ϢʔβʔϞʔυελοΫ •
ΧʔωϧϞʔυελοΫ BSHVNFOU BSHVNFOU BSHVNFOU
Ҿड͚͠ • ͰγεςϜίʔϧͷ߹ελοΫʹஔ͚ͳ͍ • ͳͥͳΒϢʔβʔϞʔυͱΧʔωϧϞʔυͷؒͰΓͱ Γ͢ΔͨΊɺͲͪΒͷελοΫʹஔ͚ͳ͍ • ͪΖΜҰ୴ϢʔβʔελοΫʹ͓͍ͯɺΧʔωϧελο ΫʹಡΈࠐΉͱ͔ߟ͑ΒΕΔ͕ɺ2ͭͷελοΫΛಉ ࣌ʹѻ͏ͷෳࡶͳͷͰ͍ͬͯͳ͍ɻ
• ͡Ό͋Ͳ͜ʹ٧Ή͔ʁ
Ҿड͚͠ • Q.͡Ό͋Ͳ͜ʹ٧Ή͔? • A. Ϩδελ • eax (γεςϜίʔϧ൪߸͕֨ೲ͞ΕΔ, γες
ϜίʔϧຖʹҰҙͳ൪߸ ※ ausyscall ͰݟΕΔ) • ebx, ecx, edx, esi, edi, ebp (Ҿ༻ʹΘΕΔ) FBY FCY FEY FTJ
Ҿड͚͠ • Ϩδελʹ٧Ή্ؔɺγεςϜίʔϧԼ هͷ੍Λ͍࣋ͬͯΔ • ֤ҾͷαΠζ͕Ϩδελ(32bit)Λ͑ͳ ͍͜ͱ • Ҿͷ6ͭ·Ͱɻ
Ҿड͚͠ • ͨͩ͠32bitΛ͑ΔҾ͕͋ͬͨ߹ࢀর Λ͢ͷͰඞͣ32bitҎʹͳΔ • 6ݸҎ্ͷҾ͕͋Δ߹Ұ୴ϝϞϦྖҬΛ ༻ҙͯ͜͠ͷྖҬΛࢦ͢ϨδελΛҾͱ͠ ͯར༻͢Δɻ FTJ YYY
FCQ Y FEJ ZZZ BSHVNFOU BSHVNFOU BSHVNFOU
Ҿड͚͠ • ϓϩάϥϚʔ͕ࣗҾ͕32bitҎ্ͩͬͨΓ 6ݸҎ্ͷ߹ͷߟྀΛ͢Δඞཁͳ͍ɻ • ϥούʔϧʔνϯ͕༻ҙ͞Ε͍ͯΔͷͰͦΕ Λݺͼग़ͤجຊతʹී௨ͷؔݺͼग़͠ ͰݺͿͷͱಉ͡
Ҿड͚͠ • جຊతʹҾࢀরͰ͞ΕͨΓϝϞϦ͔Β ͞ΕΔ͜ͱ͕͋Δ • ٯʹݴ͑ͦͷϝϞϦʹΧʔωϧ͔Β”ΞΫηε ͍͍ͯ͠ͷ͔”Λ͔֬ΊΔඞཁ͕͋Δ • ྫ: ϓϩηεΞυϨεۭؒ֎ͷϝϞϦ͔Ͳ͏͔
Ҿड͚͠ • جຊతʹҾࢀরͰ͞ΕͨΓϝϞϦ͔Β ͞ΕΔ͜ͱ͕͋Δ • ٯʹݴ͑ͦͷϝϞϦʹΧʔωϧ͔Β”ΞΫηε ͍͍ͯ͠ͷ͔”Λ͔֬ΊΔඞཁ͕͋Δ • ྫ: ϓϩηεΞυϨεۭؒ֎ͷϝϞϦ͔Ͳ͏͔
Ҿड͚͠ • ҾʹΞΫηε͍͍ͯ͠ͷ͔Ͳ͏͔ • ࢦఆͨ͠ΞυϨε͕ϓϩηεۭؒʹଐͯ͠ϝϞϦ ϦʔδϣϯͷΞΫηεݖΛௐΔʢ͕͔͔࣌ؒΔʣ • ࠷ۙͰΞυϨε͕Χʔωϧ͕༧ͯ͠ΔΞυϨ ε͡Όͳ͍͜ͱ͚ͩΛνΣοΫͯ͠ऴΘ͍ͬͯΔ ʢେࡶͳ֬ೝʣ
Ҿड͚͠ • େࡶͳ֬ೝͩͱϓϩηεۭؒʹଐ͍ͯ͠Δ ͔ΛνΣοΫͰ͖ͳ͍ • ͨͩͬ͘͟Γͨ͠νΣοΫͰ͖Δɻ͜ΕͰ ࣮ࡍͷνΣοΫΛԆධՁ͍ͯ͠Δɻ JOUBDDFTT@PL DPOTUWPJE BEES
VOTJHOFEMPOHTJ[F \ VOTJOHFEMPOHB VOTJOHOFEMPOH BEES JG B TJ[FBccB TJ[FDVSSFOU@UISFBE@JOGP BEES@MJNJUTFH SFUVSO SFUVSO ^
Ҿड͚͠ • େࡶͳ֬ೝͩͱϓϩηεۭؒʹଐ͍ͯ͠Δ ͔ΛνΣοΫͰ͖ͳ͍ • ͨͩͬ͘͟Γͨ͠νΣοΫͰ͖Δɻ͜ΕͰ ࣮ࡍͷνΣοΫΛԆධՁ͍ͯ͠Δɻ JOUBDDFTT@PL DPOTUWPJE BEES
VOTJHOFEMPOHTJ[F \ VOTJOHFEMPOHB VOTJOHOFEMPOH BEES JG B TJ[FBccB TJ[FDVSSFOU@UISFBE@JOGP BEES@MJNJUTFH SFUVSO SFUVSO ^ 2^32 - 1 Λ͔͑ͨͲ͏͔
Ҿड͚͠ • େࡶͳ֬ೝͩͱϓϩηεۭؒʹଐ͍ͯ͠Δ ͔ΛνΣοΫͰ͖ͳ͍ • ͨͩͬ͘͟Γͨ͠νΣοΫͰ͖Δɻ͜ΕͰ ࣮ࡍͷνΣοΫΛԆධՁ͍ͯ͠Δɻ JOUBDDFTT@PL DPOTUWPJE BEES
VOTJHOFEMPOHTJ[F \ VOTJOHFEMPOHB VOTJOHOFEMPOH BEES JG B TJ[FBccB TJ[FDVSSFOU@UISFBE@JOGP BEES@MJNJUTFH SFUVSO SFUVSO ^ ΧϨϯτεϨουͷϦϛοτΛ͔͑ͨͲ͏͔ ΧʔωϧϞʔυͷ࣌0xfffffff ී௨ͷϓϩηε PAGE_OFFSET - 1
Ҿड͚͠ • େࡶͳ֬ೝͩͱϓϩηεۭؒʹଐ͍ͯ͠Δ ͔ΛνΣοΫͰ͖ͳ͍ • ϓϩηεΞυϨεۭؒ͡Όͳ͍ͱ… • ϖʔδϑΥϧτྫ֎͕ൃੜ͢Δ
Ҿड͚͠ • ϖʔδϑΥϧτྫ֎ͬͯҰޱʹݴͬͯࢁݪҼ͕͋Δ 1.γεςϜίʔϧͷҾ͕ϓϩηεΞυϨεۭؒ͡Όͳ͍ 2.ϋʔυΣΞͷҰ࣌తͳΤϥʔ →ྫ֎ςʔϒϧ 3.ϖʔδϑϨʔϜ͕ͳ͔ͬͨ ʢϝϞϦϦʔδϣϯʹ͋Δ͔Ͳ͏͔ɺ͋ΔͳΒ ϝϞϦ֬อ͢Δ͚ͩʣ 4.ϖʔδςʔϒϧΤϯυ͕ະॳظԽͩͬͨɹʢϖʔδϑΥϧτΛىͨ͜͠Ξυ
Ϩε͕ਖ਼͘͠Ϛοϐϯά͞Ε͍ͯͳ͍ɺϚελʔΧʔωϧϖʔδΛ֬ೝͯ͠ɺ ਖ਼͍͠ҐஔʹϚοϐϯάͤ͠OKʣ
Ҿड͚͠ • ͖ͬ͞ͷ1ͱ2ͷͲͬͪͷྫ֎͔Θ͔Βͳ͍߹ྫ֎ςʔϒϧΛ༻ҙ͢Δ • ྫ֎ςʔϒϧϓϩηεΞυϨεۭؒʹΞΫηε͢ΔΧʔωϧͷશ໋ྩͷΞυ Ϩε͕ೖ͍ͬͯΔɻ • ͦ͜·ͰདྷΔͱɺΧʔωϧϞʔυͰϖʔδϑΥϧτྫ֎͕ൃੜͨ͠߹ʹྫ֎ ͷൃੜͨ͠ΞυϨε͕ྫ֎ςʔϒϧʹ͋Δ͔Ͳ͏͔ΛௐΔ͚ͩͰ͍͍ɻ •
ೖ͍ͬͯͨΒ => γεςϜίʔϧͷҾ͕ෆਖ਼ • Ϩδελͷ͓আɺΤϥʔॲཧͷ࣮ߦΛͯ͠ऴྃ • ೖ͍ͬͯͳ͔ͬͨΒ => ΑΓਂࠁͳঢ়گʢϋʔυΣΞΤϥʔʣ
Ҿड͚͠ • ྫ֎ςʔϒϧ TUSVDUFYDFQUJPO@UBCMF@FOUSZ \ VOTJHOFEMPOHJOTO pYVQ ^ JOTOྫ֎ςʔϒϧͷΞυϨε pYVQJOTOͷࢦ͍ͯ͠Δ໋ྩ͕ϖʔδϑΥϧτྫ֎ͷ࣌ʹ࣮ߦ͢ΔΞηϯϒϦ໋ྩ
ͷΞυϨε
Χʔωϧϥούʔϧʔνϯ • ϥΠϒϥϦؔʹͳ͍ͬͯͳ͍γεςϜίʔϧΛݺͼग़͢͜ͱ͕Ͱ ͖Δɻ • _syscall0 ~ _syscall6 ·Ͱ͋Δ(Ҿͷݸ) •
CϥΠϒϥϦʹͳ͍ͬͯͳ͍ͷݺΔɻ • ͨͩ͠ࠓଘࡏ͠ͳ͍?? • manΛௐΔͱ syscall Λ͑ͬͯͳͬͯΔɻ http://linuxjm.osdn.jp/html/LDP_man-pages/man2/_syscall.2.html
Χʔωϧϥούʔϧʔνϯ #include <unistd.h> int main( int argc, char *argv[ ]
) { int err; const char *text = “Hello World\n"; write( STDOUT_FILENO, ( const void* )text, sizeof( text ) - 1 ); return( 0 ); } ී௨൛
Χʔωϧϥούʔϧʔνϯ #include <unistd.h> #include <sys/syscall.h> int main( int argc, char
*argv[ ] ) { long ret; const char *text = “Hello World\n"; ret = syscall( SYS_write, STDOUT_FILENO, text, sizeof( text ) - 1 ); return( 0 ); } Χʔωϧϥούʔϧʔνϯͬͨ൛
·ͱΊ • γεςϜίʔϧݺͼग़͠ͷ࣌ʹϢʔβʔϞʔυ͔ΒΧʔωϧϞʔ υʹϞʔυ͕มԽ͢Δɻ • ͦͷࡍʹϨδελୀආελοΫͷར༻͕ߦΘΕΔ • Ҿͷड͚͠ͷ࣌ʹड͚͞ΕͨҾ͕҆શ͔Ͳ͏͔Λ νΣοΫ͍ͯ͠ΔɻϖʔδϑΥϧτྫ֎ͷൃੜͨ͠ՕॴΛௐ Δػߏ͕͋Δɻ
• syscallΧʔωϧϥούʔϧʔνϯΛͬͯݺͼग़͢͜ͱ Ͱ͖Δ
͓·͚ • γεςϜίʔϧݺͼग़͠ݟͯΒͬͨͱ͓ΓOSʹͱͬͯෛՙͷ͔͔ΔॲཧʢϨδελୀ ආɺҾ͠νΣοΫɺϞʔυมߋetc etcʣ • ଟ༻͗͢͠Δͷྑ͘ͳ͍ɻΪϦΪϦ·ͰϢʔβʔϞʔυͰؤுͬͯ࠷ޙʹγεςϜίʔϧ͕ ͱΓ͋͑ͣమଇ(ͱ͍͏ͷΛΑ͘ݟ͔͚ͨ) • mTCPͱ͔ͦ͏͍͏ࢥͬΆ͍
• http://www.slideshare.net/hajimetazaki/mtcp-34743298 • https://github.com/eunyoung14/mtcp • ͨͩsyscalls͕ѱ͍͍ͬͯ͏ΑΓ୯७ʹFile I/OόοϑΝͪΌΜͱ͠Α͏Ͷ͍ͬͯ͏ͷ͕ଟ ͍ͷͰӏವΈʹ͍͚ͯ͠ͳ͍ • http://www.atmarkit.co.jp/ait/articles/1112/13/news117.html (͍ͩͿ࣌ؒΛແବʹͨ͠)