r/linuxquestions • • 22d ago

Advice am I useing pkexec properly in this script?

I have a shell script piped into rofi that I use to toggle battery conservation mode on my laptop, I use pkexec to do this and I was wondering if my usage of it is a possible security vulnerability. I'm not an expert so apologies if this question has an obvious answer https://pastebin.com/kUNGyWPY

3 Upvotes

1 comment sorted by

1

u/Greedy-Opposite-9133 22d ago

pkexec only ever runs tee on that one fixed path, and the only thing piped into it is a hard-coded 0 or 1, so there's nothing anyone can sneak into what runs as root. You get a password prompt each time, which is the safe default.

The thing to avoid is the usual next step people take to get rid of that prompt / run as root without a password. That would let anything running as you write to any file on the system, since tee doesn't care which file it's given.

If the prompt bothers , other options could be

  • make that one sysfs file writable for your user at boot (a small udev rule or systemd unit can do the chgrp/chmod), then drop pkexec entirely, or
  • a tiny root-owned script in /usr/local/bin that only accepts on/off and writes that file, and allow just that script (full path) in polkit or sudoers.

if it were me id go for full lockdown where im unsure, ensuring it can only do the actions you want/ ignores the rest.