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
77
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
Transcript
15 slides
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ΛݺͿ͜ͱ
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
490
Pythonチュートリアルイベント「Python Boot Camp」の紹介
ryu22e
0
200
Pythonとコミュニティと私
ryu22e
0
120
Featured
See All Featured
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
670
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
550
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
23k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
390
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
340
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Testing 201, or: Great Expectations
jmmastey
46
8.4k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.7k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
4
1.3k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
570
Are puppies a ranking factor?
jonoalderson
2
4.2k
Leo the Paperboy
mayatellez
11
2.5k