if mode != None:
os.popen(‘sudo chmod %s %s’%(mode, file_path))
if owner != None:
os.popen(‘sudo chown -R %s:%s %s’%(owner, owner, dir))
This code is called every time, even if the file already exists.
I would suggest changing the code.
As a minimum the chown and chmod only should be done when the file is actually created.
But better would be to refactor the code so the function file_check_create should only do a check not a create. Creation of files like /opt/picar-x/picar-x.conf should be done at installation time (could be an empty file). If the file then does not exist at runtime this is an error and a proper install is needed.
BTW: I also noticed that the picar-x.conf file is mode rwxrwxrwx
The x bit is definitely not needed here and is a potential security risk.
One could also argue that the file should not be world writable, but only by members of a group picar-x (or sunfounder or so)
Initially I wanted to report my finding in an issue in the robot-hat github repo, but I did not see any activity in the open issues that indicated that sunfounder is actually reading and adressing these issues. Hence I posted here.
One additional finding
I checked for sudo in all of the robot-hat repo.
These are the places where I found sudo
install.py: warn(“Script must be run as root. Try “sudo python3 install.py”.”)
robot_hat/filedb.py: os.popen(‘sudo chmod %s %s’%(mode, file_path))
robot_hat/filedb.py: os.popen(‘sudo chown -R %s:%s %s’%(owner, owner, dir))
robot_hat/audio.py: cmd = “sudo -u %s env %s sh -c %s” % (
robot_hat/doctor.py: “”“Prefix a command with sudo when the doctor is not run as root.”“”
robot_hat/doctor.py: return “sudo " + cmd
robot_hat/utils.py: cmd = “sudo amixer -M sset ‘PCM’ %d%%” % value
robot_hat/_cli.py: cmd = “sudo " + cmd
robot_hat/_cli.py: os.system(“sudo -v 2>/dev/null”)
robot_hat/_cli.py: os.system(“sudo reboot”)
robot_hat/_cli.py: print(” Reboot later with: sudo reboot”)
robot_hat/_cli.py: status = os.system(“curl -fsSL %s | sudo bash” % url)
robot_hat/config.py: os.popen(‘sudo chmod %s %s’%(mode, path))
robot_hat/config.py: os.popen(‘sudo chown -R %s:%s %s’%(owner, owner, dir))
A few interesting observations
config.py seems to contain the same file_check_create function as filedp.py so one of them could be eliminated.
And I found this one very dangerous:
status = os.system(“curl -fsSL %s | sudo bash” % url)
Basically it gets some shell code from a url and executes it.
I don’t think I want that to happen on my system. Then again I am not sure if any of the code I use accesses _cli.py
Hi FransM, thanks for the detailed report — we went through the code and you are right on all counts.
On the current 2.5.x line:
file_check_create() in filedb.py runs sudo chmod and sudo chown -R on every instantiation, even when the file already exists. /opt/picar-x is already owned by the installing user, so none of that needs root.
config.py carries a copy of the same function, so the code has two implementations doing the same job.
The 777 mode comes from picar-x itself (fileDB(config, 777, …)); the +x bit serves no purpose.
These are fixed in robot-hat 2.6, which is the next release: the file-db layer is replaced by a JSON config, so that path no longer sets a mode, no longer calls chmod/chown, and there is only one implementation.
One caveat, so nobody gets bitten: 2.6 is still under development and is not a drop-in replacement for 2.5 — fileDB is gone, Config has a different API, and a couple of other classes have changed, so an existing 2.5 installation should not be upgraded to it yet. We will announce it once it is ready.
On _cli.py: it is from an unmerged branch and is not in any release tag; the curl | sudo bash there only runs on an explicit robot_hat update against our own install.sh.
Thank you very much for offering to help test version 2.6 — we really appreciate your support.
Just a heads-up: 2.6 is still under development and may not be released very soon. Once the version is ready for outside testing, we will get back to you with how to get it and where to send feedback.
Thanks again for your careful review and your support. If you find anything else, feel free to keep the discussion going here.