I was looking for a way to make my laptop's special keys work with i3-wm. I ran into this post and used the script there to create my own.
This is what I came up with for the screen brightness (on my machine, valid values seem to be between 0 and 937 - anything else gives a write error):
#!/bin/bash
#
# Usage: lcd_bright.sh <U|D> <value>
#
MODE=`echo $1 | tr '[a-z]' '[A-Z]'`
BRIGHTNESS='/sys/class/backlight/intel_backlight/brightness'
LCDVALUE=`cat $BRIGHTNESS`
if [ "$MODE" = "U" ]
then
NEWVALUE=$(( $LCDVALUE + $2 ))
if [ $NEWVALUE -le 937 ]
then
echo $NEWVALUE > $BRIGHTNESS
else
echo 937 > $BRIGHTNESS
fi
else
NEWVALUE=$(( $LCDVALUE - $2 ))
if [ $NEWVALUE -ge 0 ]
then
echo $NEWVALUE > $BRIGHTNESS
else
echo 0 > $BRIGHTNESS
fi
fi
And for the keyboard backlight (it has 4 levels):
#!/bin/bash
#
# Usage: kbd_bright.sh <U|D>
MODE=`echo $1 | tr '[a-z]' '[A-Z]'`
BRIGHTNESS='/sys/class/leds/asus::kbd_backlight/brightness'
KBDVALUE=`cat $BRIGHTNESS`
if [ "$MODE" = "U" ]
then
NEWVALUE=$(( $KBDVALUE + 1 ))
if [ $NEWVALUE -le 3 ]
then
echo $NEWVALUE > $BRIGHTNESS
else
echo 3 > $BRIGHTNESS
fi
else
NEWVALUE=$(( $KBDVALUE - 1 ))
if [ $NEWVALUE -ge 0 ]
then
echo $NEWVALUE > $BRIGHTNESS
else
echo 0 > $BRIGHTNESS
fi
fi
I added rules in sudoers.d/
so the scripts don't require a password, and the scripts are owned by root
and have permissions set to 0754
.
My i3 configuration for them is as follows:
# screen brightness control
bindsym XF86MonBrightnessUp exec sudo /home/ioana/.config/i3/lcd_bright.sh U 100
bindsym XF86MonBrightnessDown exec sudo /home/ioana/.config/i3/lcd_bright.sh D 100
# keyboard brightness control
bindsym XF86KbdBrightnessUp exec sudo /home/ioana/.config/i3/kbd_bright.sh U
bindsym XF86KbdBrightnessDown exec sudo /home/ioana/.config/i3/kbd_bright.sh D
While doing this, I saw that someone mentioned that such scripts pose a security risk, especially if they use an input (which mine do). I'd like to know more about what the specific security risks actually are with my scripts and what they imply.
What was actually said and its meaning
Quote from the linked post:
This talks about two cases:
What's the problem with scripts in general ? To paraphrase from Stephane Chazelaz's answer (which I strongly recommend reading), shell scripts can be a problem if run either in web servers or when privileged script runs something else. To a regular user who just has desktop - that means in most cases destroyed system - maybe attacker injected
rm -rf /
somewhere - or computer being overtaking by malware and being a part of botnet to attack commercial servers. But for commercial servers, this can mean anything from client credit card information being stolen to systems destroyed causing loss of money because system is down and customers go somewhere else. So when something is said to be a security issue, you need to know what that means for you. You also should know who could be your possible attacker - that defines what sort of approach they can use to compromise your system, and for someone interested in your credit card information or conversation with people, they'll likely to go after network traffic rather than the script, which means they'll likely go with MITM attack rather than the script.What can be the problem in your specific case ?
Your script executes 3 commands:
echo $1 | tr
, andcat
. If attacker replacescat
ortr
with malicious programs, it can mean either system destroyed, or leaking information every time you execute those commands. And because your script runs with root-level privilege - those commands also run with root level privilege. Sinceecho
is a shell built-in, it's immune against attacks where/bin/echo
is replaced ( unless you runenv echo
instead - that will call/bin/echo
). Arguably, if you have someone capable of replacing system-level binaries, it means they already have root access, which is more important problem than just your script.The scripts live in
/home/ioana/.config/i3/
with 0754 permissions. OK, that's fine. If your account is compromised attacker doesn't need root - they'll use your account to overwrite the script contents. What about permissions of the/home/ioana/.config/i3/
directory ? Deleting a file requires having write permissions on the directory where file lives, so if you have another user on your system and they do not have write permissions on script itself, if they have permissions to write into directory they can delete the script (not really a security issue, but a mini-DoS for ships and giggles).Another problem in theory can come from the command-line parameters. You have
echo $1 | tr '[a-z]' '[A-Z]'
. Say an attacker uses/*/*/*/*/../../../../*/*/*/*/../../../../*/*/*/*
as$1
mentioned in Stephane's answer. The shell will need to convert those*
into actual files,and all those expansions are expensive for CPU. That's a small way to make your computer lag, again mini-DoS.In case you're running outdated version of bash, it can be vulnerable to arbitrary code injection via exporting functions - aka shellsock. So one could export a malicious function before running your prvileged script.
If an attacker is fond of animal cruelty, they can abuse
cat
Of course, all these things can be combined with
wget
to download something else that's malicious on your computer and execute with root privileges.In the end, the fact that you're operating on
/sys/class/
type of directory isn't the problem. Problem lies with the level of what shell scripts can do and that shell scripts have mechanisms that aren't perfect. But let's not get overly paranoid. Like I said, if someone got access to your account (which hassudo
privileges) or root account - that's enough of a concern already.See also
There are a few ways to significantly reduce the attack surface of your scripts. Note that I'm no Bash expert, so this isn't an exhaustive list of issues, just a few issues I can see with your existing code.
kbd_brighter
andkbd_dimmer
). You can also make two scripts to raise and lower LCD brightness by 100./sys
file before writing to it. Does it exist? Does it contain only a single integer? What should we do if that integer is unexpectedly a negative integer? Handle all possible cases properly.[ $1 -eq 5 ]
becoming[ -eq 5 ]
is just asking for trouble) and to ensure that variables containing special characters like spaces or newlines are handled properly.PATH
attacks. For example,/bin/cat
instead ofcat
. You can see the path withtype cat
.VARIABLE=`< filename`
instead ofVARIABLE=`cat filename`
. Also, useecho
instead of/bin/echo
to use the Bash builtinecho
command. You can see that the builtin command is available withtype echo
.Summing up, here's an example
kbd_brighter
script. Note that I haven't tested it since I don't have an Asus. It might also still have security issues since I'm not an expert.