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
68
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
190
Pythonとコミュニティと私
ryu22e
0
120
Featured
See All Featured
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
650
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
A designer walks into a library…
pauljervisheath
211
25k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.1k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
280
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.6k
How to train your dragon (web standard)
notwaldorf
97
6.8k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
430
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
500
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
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ΛݺͿ͜ͱ