Elevate privileges between Python program ( and Inter-process_communication )

Hi everyone,

I have a Python program (A) that run under a regular user account. (good)

When some events occur in (A) I need to modify my nftables and only the root is allowed to do so.

I’ve come up with 3 ways to do that (if you know other please share) but I don’t which would be the best.

  1. Make a sudo call from (A) with from subprocess import run but I will need to store the password ! and I don’t think is possible to keep it encrypted and decrypted when need it (it’s a flaw)
    .
  2. Make (A) writing a file with the requests. Create a (B) daemon (that run as root) that check that file every X and do the necessary
    .
  3. Make (A) do an IPC ( Linux socket ) to (B) daemon (that run as root) and does the necessary.

I suppose that the solution 2 is less heavy that the 3 ? But if I’m not mistaken it will react also slower ?

Thanks.

🐧

solrize, (edited )

Method 2 could use inotify to wake up when the file changes. It wouldn’t have to poll. Method 3 could launch from inetd so it wouldn’t have to always be running if these events are infrequent.

fubarx,

Have you looked into the suid bit? You can set it on the file, then change the script owner to root and it runs in elevated mode: linuxhandbook.com/suid-sgid-sticky-bit/

axzxc1236,

If your command doesn’t change (doesn’t require dynamic input), sudoers file can make specific command+argument run without password required.

cyberciti.biz/…/linux-unix-running-sudo-command-w… (ctrl+f search “A better solution”)

(You can also use wildcards in sudoers file but with nftables I imagine it’s a big security risk)

taaz, (edited )
  1. Is the usual solution, but instead of file use unix socket and user/group permissions as auth - the running user has to be part of some group so that the control client (A) can access the control socket of (B) daemon.

Alternatively you could use capabilities:

stackoverflow.com/a/414258

man7.org/linux/man-pages/…/capabilities.7.html

SpongeB0B,
@SpongeB0B@programming.dev avatar

Thank you very much @taaz

So you say 2 but with unix socket so it the same as my proposal number 3 ? no ?

I’ll check capabilities

taaz,

Yeah kinda, unix socket does count as ipc

KaninchenSpeed,

You could try pkexec insted of sudo. Pkexec pops up the password prompt in a window insted of prompting in the terminal.

Strit,
@Strit@lemmy.linuxuserspace.show avatar

It’s a good way of solving it. It’s not scriptable though as it requires user-input.

SpongeB0B,
@SpongeB0B@programming.dev avatar

indeed I need it to be scriptable.

baru,

Then implement polkit perhaps? polkit.pages.freedesktop.org/…/polkit-apps.html

Basically the root using bit is handled via polkit. Three unprivileged bit calls the privileged bit via polkit.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • fightinggames
  • All magazines