-3

Is there any way for other users to write to root-owned files like those in procfs, sysfs, or devfs? The file permissions are set to 644, so I can read them, but I can't write to them.

I'm curious if it's possible to do this with a python script. The script is automatically run by the system, so I can't manually execute it with sudo or anything like that. I’m guessing this might not even be possible because of security restrictions, though.

Thanks!

4
  • 3
    if you don't have write permission, you don't have write permission, with vim, with textedit, with python, or any other tool. Commented Apr 7 at 5:21
  • You can run your script as a service with root permission, but for other users it shouldn’t as @Julien noted! Commented Apr 7 at 6:06
  • Thanks for your answers. Is there any way to allow another administration account(such as admin) to write? Even if root and admin are in the same group, there is no way to grant write access since the file permission is 644 instead of 664, isn't it? In that case, should I set the ownership of the required sysfs file to admin? Commented Apr 7 at 6:25
  • The Python interpreter needs to run with appropriate privileges. You can't change those privileges from within your Python code unless you invoke another instance of the interpreter via e.g., sudo Commented Apr 7 at 6:45

1 Answer 1

0

You can arrange for your script to re-run itself with appropriate privileges as follows (Unix-type OS):

import sys
import subprocess
import os

PASSWORD = b"root password goes here" # note: bytes
DUMMY = "__dummy__" # arbitrary non-empty string

if __name__ == "__main__":
    if len(sys.argv) == 1:
        cmd = ["sudo", "-S", sys.executable, __file__, DUMMY]
        # -S flag causes the password input prompt to be sent to stderr
        # we don't want to see that so redirect to the null device
        p = subprocess.run(cmd, input=PASSWORD, stderr=subprocess.DEVNULL)
        assert p.returncode == 0
    else:
        assert os.getuid() == 0
        print("Running with root privileges")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.