Write file with specific permissions in Python

前端 未结 6 1226
一个人的身影
一个人的身影 2020-12-01 15:37

I\'m trying to create a file that is only user-readable and -writable (0600).

Is the only way to do so by using os.open() as follows?

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 16:21

    This answer addresses multiple concerns with the answer by vartec, especially the umask concern.

    import os
    import stat
    
    # Define file params
    fname = '/tmp/myfile'
    flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL  # Refer to "man 2 open".
    mode = stat.S_IRUSR | stat.S_IWUSR  # This is 0o600.
    umask = 0o777 ^ mode  # Prevents always downgrading umask to 0.
    
    # For security, remove file with potentially elevated mode
    try:
        os.remove(fname)
    except OSError:
        pass
    
    # Open file descriptor
    umask_original = os.umask(umask)
    try:
        fdesc = os.open(fname, flags, mode)
    finally:
        os.umask(umask_original)
    
    # Open file handle and write to file
    with os.fdopen(fdesc, 'w') as fout:
        fout.write('something\n')
    

    If the desired mode is 0600, it can more clearly be specified as the octal number 0o600. Even better, just use the stat module.

    Even though the old file is first deleted, a race condition is still possible. Including os.O_EXCL with os.O_CREAT in the flags will prevent the file from being created if it exists due to a race condition. This is a necessary secondary security measure to prevent opening a file that may already exist with a potentially elevated mode. In Python 3, FileExistsError with [Errno 17] is raised if the file exists.

    Failing to first set the umask to 0 or to 0o777 ^ mode can lead to an incorrect mode (permission) being set by os.open. This is because the default umask is usually not 0, and it will be applied to the specified mode. For example, if my original umask is 2 i.e. 0o002, and my specified mode is 0o222, if I fail to first set the umask, the resulting file can instead have a mode of 0o220, which is not what I wanted. Per man 2 open, the mode of the created file is mode & ~umask.

    The umask is restored to its original value as soon as possible. This getting and setting is not thread safe, and a threading.Lock must be used in a multithreaded application.

    For more info about umask, refer to this thread.

提交回复
热议问题