Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
os.makedirsの細かすぎるセキュリティの話
Search
Ryuji Tsutsui
February 10, 2021
71
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
os.makedirsの細かすぎるセキュリティの話
みんなのPython勉強会#66
懇親会LT
Ryuji Tsutsui
February 10, 2021
More Decks by Ryuji Tsutsui
See All by Ryuji Tsutsui
本番運用を想定したDjango settings.pyの書き方入門(OSC Fall 2021 version)
ryu22e
1
1.6k
本番運用を想定したDjango settings.pyの書き方入門
ryu22e
1
2.8k
HashiCorpの新ツール 「Waypoint」をCloud Runで使ってみた
ryu22e
0
480
Pythonチュートリアルイベント「Python Boot Camp」の紹介
ryu22e
0
200
Pythonとコミュニティと私
ryu22e
0
120
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Bash Introduction
62gerente
615
220k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Technical Leadership for Architectural Decision Making
baasie
3
620
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
490
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
380
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
47k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
540
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
300
Deep Space Network (abreviated)
tonyrice
0
390
Transcript
os.makedirsͷ ࡉ͔͗͢ΔηΩϡϦςΟͷ Ryuji Tsutsui ΈΜͳͷPythonษڧձ#66 ࠙ձLT
ࣗݾհ • Ryuji Tsutsui@ryu22e • ຊͷϑΟϯςοΫܥاۀͰDjangoΛͬͯWebαʔϏεΛ࡞͍ͬͯ·͢ • ৭ʑͳOSSίϛϡχςΟʹؔΘ͍ͬͯ·͢ • Python
Boot CampίΞελοϑ • Shonan.pyελοϑ • Python Charity Talks in Japanελοϑ • झຯ: өըؑɾ֨ಆٕ؍ઓ ݄ʢʣ։࠵ ࢀՃͯ͠Ͷʂ
ࠓ͢͜ͱ • Python 3.7Ҏ্ͷos.makedirs ʢෳ֊ͷσΟϨΫτϦΛ࡞͢ΔؔʣʹՃΘͬͨʮ͋Δมߋʯ ʹ͍ͭͯͷ • ͲΜͳมߋͳͷ͔ • ͳͥมߋ͞Εͨͷ͔
• ͜ͷมߋʹΑΓى͜Γ͏Δ੬ऑੑ
ͲΜͳมߋͳͷ͔
ެࣜυΩϡϝϯτΛಡΜͰΈΑ͏ • https://docs.python.org/3.7/library/os.html#os.makedirs ΑΓҾ༻ Changed in version 3.7: The mode
argument no longer affects the file permission bits of newly-created intermediate-level directories. όʔδϣϯ3.7ͷมߋ: modeҾɺ৽͘͠࡞͞ΕͨதؒϨϕϧͷσΟϨΫ τϦͷϑΝΠϧύʔϛογϣϯϏοτʹӨڹΛ༩͑ͳ͘ͳΓ·ͨ͠ɻ ↓༁
࣮ࡍʹίʔυΛॻ͍ͯݕূͯ͠ΈΑ͏ """example.py""" import os from pathlib import Path # dir1dir2ύʔϛογϣϯ700Λࢦఆͨͭ͠Γ
mode = 0o700 os.makedirs(Path.cwd() / "dir1" / "dir2", mode=mode)
Python 3.6Ͱͷ࣮ߦ݁Ռ $ python3.6 example.py $ stat --format='%a' dir1 dir1/dir2/
700 700
Python 3.7Ҏ্Ͱͷ࣮ߦ݁Ռ $ umask 077 && python3.7 example.py $ stat
--format='%a' dir1 dir1/dir2/ 700 700 $ umask 022 && python3.7 example.py $ stat --format='%a' dir1 dir1/dir2/ 755 700 $ umask 000 && python3.7 example.py $ stat --format='%a' dir1 dir1/dir2/ 777 700 $ umask 000 && python3.6 example.py $ stat --format='%a' dir1 dir1/dir2/ 700 700 EJSʢதؒϨϕϧͷσΟϨΫτϦʣͷ ύʔϛογϣϯNPEFҾͷӨڹΛड͚ͣɺ VNBTLͷʹΑͬͯ݁Ռ͕มΘΔ ʢPdVNBTLʣ 1ZUIPOͩͱVNBTLͷӨڹड͚ͳ͍
ͳͥมߋ͞Εͨͷ͔
CPythonͷιʔείʔυ͔Βgit blameͰḷͬͯ Έͨ • ʮos.makedirs('dir1/dir2', 0) always failsʯͱ͍͏Issue͕ڍ͕͍ͬͯ ͨ •
https://bugs.python.org/issue19930
• 3.6Ͱʮos.makedirs('dir1/dir2', 0)ʯΛ࣮ߦ͢Δͱʮdir1ʯͷύʔϛο γϣϯ͕0ʹͳΓɺʮdir2ʯ͕࡞Ͱ͖ͳ͍ɻ ʢσΟϨΫτϦͷύʔϛογϣϯʹॻ͖ࠐΈݖݶ͕ͳ͍ͷͰʣ • mkdirίϚϯυͰɺʮmkdir -p -m 0
dir1/dir2ʯͩͱdir1 ʮ0o777&~umaskʯͷύʔϛογϣϯ͕༩͑ΒΕΔɻ͜Εͱಉ༷͡ ʹͨ͠΄͏͕͍͍ͷͰʁ • ٞͷ݁Ռɺ͜ͷఏҊ͕ड͚ೖΕΒΕΔ͜ͱʹͳͬͨ ཁ͢Δͱ͜͏
͜ͷมߋʹΑΓى͜Γ͏Δ੬ऑੑ
͜ͷมߋ͕ݪҼͷDjango੬ऑੑ͕࣮ࡍʹ͋ͬͨ • CVE-2020-24583ɾCVE-2020-24584 ʢ20209݄1ʹमਖ਼൛ϦϦʔεʣ • ੩తϑΝΠϧΛஔ͘σΟϨΫτϦɾΩϟογϡͷॻ͖ࠐΈઌσΟϨ ΫτϦΛ࡞͢Δࡍʹos.makedirsΛmode͖Ͱ͍ͬͯΔ • umaskͷʹΑͬͯɺ͜ΕΒͷσΟϨΫτϦʹ༨ͳύʔϛο
γϣϯΛ༩͑ͯ͠·͏Մೳੑ͕͋ͬͨ
͜ͷΑ͏ʹରԠ͞Εͨ import os from pathlib import Path mode = 0o700
# modeʹࢦఆͨ͠ͱಉ͡ύʔϛογϣϯʹͳΔΑ͏Ұ୴umaskΛมߋ old_umask = os.umask(0o777 & ~mode) try: os.makedirs(Path.cwd() / "dir1" / "dir2", mode=mode) finally: # σΟϨΫτϦ࡞ޙʹumaskΛݩʹ͢ os.umask(old_umask)
·ͱΊ • Python 3.7͔Βos.makedirsͷmodeҾʮதؒϨϕϧͷσΟϨΫτ ϦʯʹӨڹΛ༩͑ͣɺumaskͷʹΑͬͯύʔϛογϣϯ͕ܾ·Δ • mkdirίϚϯυͷ༷ʹ߹Θͤͯ͜͏ͳͬͨ • 3.6·Ͱͱಉ͡ڍಈΛظ͢ΔίʔυΛॻ͘ͱɺσΟϨΫτϦʹ༨ ͳύʔϛογϣϯΛ༩͑ͯ͠·͏Մೳੑ͕͋ΔͷͰҙ
• ճආ͢Δʹos.makedirsͷલޙͰos.umaskΛݺͿ͜ͱ