Upgrade to Pro — share decks privately, control downloads, hide ads and more …

os.makedirsの細かすぎるセキュリティの話

Ryuji Tsutsui
February 10, 2021
50

 os.makedirsの細かすぎるセキュリティの話

Ryuji Tsutsui

February 10, 2021
Tweet

Transcript

  1. ࣗݾ঺հ • Ryuji Tsutsui@ryu22e • ࿡ຊ໦ͷϑΟϯςοΫܥاۀͰDjangoΛ࢖ͬͯWebαʔϏεΛ࡞͍ͬͯ·͢ • ৭ʑͳOSSίϛϡχςΟʹؔΘ͍ͬͯ·͢ • Python

    Boot CampίΞελοϑ • Shonan.pyελοϑ • Python Charity Talks in Japanελοϑ • झຯ: өըؑ৆ɾ֨ಆٕ؍ઓ ݄೔ʢ౔ʣ։࠵ ࢀՃͯ͠Ͷʂ
  2. ެࣜυΩϡϝϯτΛಡΜͰΈΑ͏ • 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Ҿ਺͸ɺ৽͘͠࡞੒͞ΕͨதؒϨϕϧͷσΟϨΫ τϦͷϑΝΠϧύʔϛογϣϯϏοτʹӨڹΛ༩͑ͳ͘ͳΓ·ͨ͠ɻ ↓࿨༁
  3. 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ͷӨڹ͸ड͚ͳ͍
  4. • 3.6Ͱʮos.makedirs('dir1/dir2', 0)ʯΛ࣮ߦ͢Δͱʮdir1ʯͷύʔϛο γϣϯ͕0ʹͳΓɺʮdir2ʯ͕࡞੒Ͱ͖ͳ͍ɻ
 ʢ਌σΟϨΫτϦͷύʔϛογϣϯʹॻ͖ࠐΈݖݶ͕ͳ͍ͷͰʣ • mkdirίϚϯυͰ͸ɺʮmkdir -p -m 0

    dir1/dir2ʯͩͱdir1͸ ʮ0o777&~umaskʯͷύʔϛογϣϯ͕༩͑ΒΕΔɻ͜Εͱಉ͡࢓༷ ʹͨ͠΄͏͕͍͍ͷͰ͸ʁ • ٞ࿦ͷ݁Ռɺ͜ͷఏҊ͕ड͚ೖΕΒΕΔ͜ͱʹͳͬͨ ཁ໿͢Δͱ͜͏
  5. ͜ͷΑ͏ʹରԠ͞Εͨ 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)