Previously we had to replace shutil.copyfile() with shutil.copy(), because we want to copy the file permissions to the destination file along with the data.
It appears that shutil.copy() is opening the destination file with wide access (0666) first, and then it starts copying the data and at the end it is copying the permissions from source file to destination.
If we closely notice there appears a window between destination file is opened vs permissions are set on the destination file, which could allow a user to get the contents of the file when opening it at the right time.
The behavior is a bit unsteady here, it is noticed that, when saveconfig.json file exists, then on shutil.copy(), destination file is opened and a mask 0600 is applied on the file, in case shutil.copy() had to open a new destination saveconfig.json file, then mask 0644 is applied.
Thanks and Credits to 'Stefan Cornelius scorneli@redhat.com' for reporting this, here is the strace he shared from RHEL-7/python-2.7.5 env:
Case 1: When /etc/target/saveconfig.json doesn't exist:
Previously we had to replace shutil.copyfile() with shutil.copy(), because we want to copy the file permissions to the destination file along with the data.
It appears that shutil.copy() is opening the destination file with wide access (0666) first, and then it starts copying the data and at the end it is copying the permissions from source file to destination.
If we closely notice there appears a window between destination file is opened vs permissions are set on the destination file, which could allow a user to get the contents of the file when opening it at the right time.
The behavior is a bit unsteady here, it is noticed that, when saveconfig.json file exists, then on shutil.copy(), destination file is opened and a mask 0600 is applied on the file, in case shutil.copy() had to open a new destination saveconfig.json file, then mask 0644 is applied.
Thanks and Credits to 'Stefan Cornelius scorneli@redhat.com' for reporting this, here is the strace he shared from RHEL-7/python-2.7.5 env:
Case 1: When /etc/target/saveconfig.json doesn't exist:
Case 2: When /etc/target/saveconfig.json already exist:
Signed-off-by: Prasanna Kumar Kalever \prasanna.kalever@redhat.com\