Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the code example in the python os module documentation create a security hole?

The documentation for the os module makes the following assertion:


Note

Using access() to check if a user is authorized to e.g. open a file before actually doing so using open() creates a security hole, because the user might exploit the short time interval between checking and opening the file to manipulate it. It’s preferable to use EAFP techniques. For example:

if os.access("myfile", os.R_OK):
    with open("myfile") as fp:
    return fp.read()
return "some default data"`

is better written as:

try:
    fp = open("myfile")
except PermissionError:
    return "some default data"
else:
    with fp:
        return fp.read()

I don't understand how a user "might exploit" the interval. If open was going to raise an exception, I'm not sure how os.access would prevent that exception from being raised. Likewise, if the user manipulate the file somehow, why not perform the file manipulation prior to the EAFP version's open command?

I do understand that the second version may be more robust, since os.access may fail to correctly recognize a condition that could raise a PermissionError, but I don't see how the LBYL version is less secure. Can someone explain this?

like image 224
K. Nielson Avatar asked Dec 05 '18 23:12

K. Nielson


People also ask

How does os module work in Python?

The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc. You first need to import the os module to interact with the underlying operating system.

Which module of Python is used to apply the methods related to os?

The python os module makes this possible, it provides a means for us to interact with the underlying operating system in many different ways and provides us with a portable way to use the operating system dependent functionalities.

What Python module allows us to interact with the underlying operating system?

The functions OS module provides allows us to operate on underlying Operating System tasks, irrespective of it being a Windows Platform, Macintosh or Linux.

How do I open an os module file in Python?

open() method in Python is used to open a specified file path and set various flags according to the specified flags and its mode according to specified mode. This method returns a file descriptor for newly open file. The returned file descriptor is non-inheritable.


1 Answers

While the access-using code is unsafe, the second code snippet doesn't solve the security issues. These docs should be changed.

The purpose of os.access is to test whether the real user id has permission to access a file when the real and effective user ids are different. The danger with the os.access snippet is that the user could place a file access returns True for in the location being tested, then swap it out for a file access would have returned False for, bypassing the check. Since open uses the effective user id, the open call can still work, bypassing the access check.

The second snippet doesn't solve this problem. Now the user doesn't even need to go through the switcheroo. No part of the second snippet checks the real user id.

like image 149
user2357112 supports Monica Avatar answered Oct 31 '22 21:10

user2357112 supports Monica