Table of Contents
Requirement background
Common methods and defect analysis
Improved python file creation method
Home Backend Development Python Tutorial How to use fdopen to minimize permissions on files generated by the Python process

How to use fdopen to minimize permissions on files generated by the Python process

Apr 28, 2023 pm 10:22 PM
python

Requirement background

When using python to create, read and write files, we rarely pay attention to the permission configuration of the created files. For some systems with higher security, if the file permissions we create have read permissions for other users or other users in the same user group, it may lead to the risk of unnecessary information leakage. Therefore, in addition to creating a more secure and private personal environment (such as a container environment, etc.), we can also minimize the permissions of the configuration of the generated files.

Common methods and defect analysis

The commonly used method of creating, reading and writing python files is to create a file directly through the built-in open function. If it is created using the with syntax, the opened object will be automatically closed after ending the statement. If you use the open function directly to define an object, you need to manually perform the close operation at the end of the task. The following demonstrates the usage of the built-in function open and its file operation attributes. First, create a file named file-test.py:

# file-test.py
 
with open('test1.txt', 'w') as file:
    file.write('hello world!')
Copy after login

The content of this task is: Create a file named test1.txt in the current directory file, clear the contents of the file, and write the string hello world! in the file. Next, use python3 to execute the file:

[dechin@dechin-manjaro os_security]$ python3 file-test.py
[dechin@dechin-manjaro os_security]$ ll
Total usage 8
-rw-r--r-- 1 dechin dechin 83 January 25 13:43 file-test.py
-rw-r--r-- 1 dechin dechin 12 January 25 13:43 test1.txt

Here we found that the file test1.txt was successfully generated after execution, and its permissions were configured to 644, consistent with the file-test.py created previously. When I didn't know the implementation principle of the built-in function open, I originally thought that the generated file permission configuration was consistent with the current py file. However, after further testing, the permissions of the py file were configured to 440 and then the file was re-executed:

[dechin@dechin-manjaro os_security]$ chmod 440 file-test.py
[ dechin@dechin-manjaro os_security]$ ll
Total usage 8
-r--r----- 1 dechin dechin 83 January 25 13:43 file-test.py
-rw-r --r-- 1 dechin dechin 12 January 25 13:43 test1.txt
[dechin@dechin-manjaro os_security]$ rm test1.txt
[dechin@dechin-manjaro os_security]$ python3 file-test .py
[dechin@dechin-manjaro os_security]$ ll
Total usage 8
-r--r----- 1 dechin dechin 83 January 25 13:43 file-test.py
-rw-r--r-- 1 dechin dechin 12 January 25 13:44 test1.txt

Here we can see from the test results that the file generated by python’s built-in function open The type is independent of the source py file. Regarding whether the execution of this py file requires executable permissions, you can refer to this blog.

Improved python file creation method

Through the fdopen library and special permission specification, we can set the access permissions of the generated file. A python code example is shown below:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test2.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')
Copy after login

After execution, we can find that a file named test2.txt is generated in the current directory, and its permissions are configured as 600, compared with the mode = stat.S_IRUSR | stat.S_IWUSR we set in the code. Here we first explain some of the parameters: os.O_WRONLY means opening in write-only mode, os.O_CREAT means creating and opening a new file, os.O_EXCL means reporting an error if the file already exists. The permissions configured in mode correspond to the rwx configuration respectively, and USR, GRP, and OTH have subdivided configurations for users, user groups, and other users respectively, so that we can realize all types of permission configurations by changing the mode parameters. .

We can try to adjust the mode in the above use case, such as adding an executable permission to 700:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test3.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')
Copy after login

Or, we need to add executable permissions for other users in the user group Access permissions, such as 640 permissions:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test4.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')
Copy after login

We can even write out the system’s native 644 file permissions:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test5.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')
Copy after login

Finally, let’s take a look at the results obtained after executing the above python examples :

[dechin@dechin-manjaro os_security]$ ll
Total usage 28
-rw-r--r-- 1 dechin dechin 269 January 25 14:58 fdopen- test.py
-r--r----- 1 dechin dechin 84 January 25 14:11 file-test.py
-rw-r--r-- 1 dechin dechin 12 January 25 13:44 test1.txt
-rw------- 1 dechin dechin 12 January 25 14:44 test2.txt
-rwx------ 1 dechin dechin 12 January 25 14 :48 test3.txt
-rw-r----- 1 dechin dechin 12 January 25 14:56 test4.txt
-rw-r--r-- 1 dechin dechin 12 January 25 14 :58 test5.txt

We can see from the results that all the generated files test*.txt are generated according to our expected file permission configuration. At this point we have completed all expected goals. .

The above is the detailed content of How to use fdopen to minimize permissions on files generated by the Python process. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Can Python parameter annotations use strings? Can Python parameter annotations use strings? Apr 01, 2025 pm 08:39 PM

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Apr 01, 2025 pm 05:24 PM

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

Why can't my code get the data returned by the API? How to solve this problem? Why can't my code get the data returned by the API? How to solve this problem? Apr 01, 2025 pm 08:09 PM

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values ​​when API calls, which is not only confusing...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How do Python scripts clear output to cursor position at a specific location? How do Python scripts clear output to cursor position at a specific location? Apr 01, 2025 pm 11:30 PM

How do Python scripts clear output to cursor position at a specific location? When writing Python scripts, it is common to clear the previous output to the cursor position...

Do Google and AWS provide public PyPI image sources? Do Google and AWS provide public PyPI image sources? Apr 01, 2025 pm 05:15 PM

Many developers rely on PyPI (PythonPackageIndex)...

See all articles