Files
security-misc/usr/lib/security-misc/permission-hardening

152 lines
4.4 KiB
Plaintext
Raw Normal View History

2019-12-08 16:49:28 +00:00
#!/bin/bash
2019-12-19 17:01:08 +00:00
## Copyright (C) 2012 - 2019 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## See the file COPYING for copying conditions.
2019-12-20 02:08:05 -05:00
#set -x
2019-12-19 17:01:08 +00:00
exit_code=0
2019-12-08 16:49:28 +00:00
config_file="/etc/permission-hardening.conf"
2019-12-19 17:01:08 +00:00
shopt -s globstar
2019-12-20 02:07:49 -05:00
echo_wrapper() {
echo "run: $@"
2019-12-20 03:04:13 -05:00
## TODO
2019-12-20 02:16:32 -05:00
#"$@"
2019-12-20 02:07:49 -05:00
}
2019-12-20 04:08:46 -05:00
add_nosuid_statoverride_entry() {
echo "fso: $fso"
while read -r line; do
if ! read -r file_name existing_mode owner group; then
continue
fi
2019-12-19 17:01:08 +00:00
2019-12-20 04:08:46 -05:00
## -h file True if file is a symbolic Link.
## -u file True if file has its set-user-id bit set.
## -g file True if file has its set-group-id bit set.
2019-12-20 02:43:33 -05:00
2019-12-20 04:08:46 -05:00
if test -h "$file_name" ; then
## https://forums.whonix.org/t/kernel-hardening/7296/323
true "skip symlink: $file_name"
continue
fi
2019-12-20 04:08:46 -05:00
if test -u "$file_name" || test -g "$file_name"; then
string_length_of_existing_mode="${#existing_mode}"
if [ "$string_length_of_existing_mode" = "4" ]; then
new_mode="${existing_mode:1}"
else
new_mode="$existing_mode"
fi
2019-12-20 04:08:46 -05:00
echo "suid - file_name: '$file_name' | existing_mode: '$existing_mode' | new_mode: '$new_mode'"
2019-12-19 17:01:08 +00:00
2019-12-20 04:08:46 -05:00
if dpkg-statoverride --list | grep -q "$file_name"; then
if ! dpkg-statoverride --list | grep -q "$owner $group $new_mode $file_name"; then
echo_wrapper dpkg-statoverride --remove "$file_name"
2019-12-20 03:03:40 -05:00
echo_wrapper dpkg-statoverride --add --update "$owner" "$group" "$new_mode" "$file_name"
2019-12-19 17:01:08 +00:00
fi
2019-12-20 04:08:46 -05:00
else
echo_wrapper dpkg-statoverride --add --update "$owner" "$group" "$new_mode" "$file_name"
2019-12-19 17:01:08 +00:00
fi
2019-12-20 04:08:46 -05:00
fi
done < <( stat -c "%n %a %U %G" "${fso_without_trailing_slash}/"** )
2019-12-19 17:01:08 +00:00
}
2019-12-08 16:49:28 +00:00
set_file_perms() {
while read -r line; do
2019-12-20 01:58:35 -05:00
if [ "$line" = "" ]; then
2019-12-20 01:31:37 -05:00
continue
fi
2019-12-08 16:49:28 +00:00
2019-12-20 01:58:35 -05:00
if [[ "$line" =~ ^# ]]; then
continue
fi
if [[ "$line" =~ [0-9a-zA-Z/] ]]; then
true OK
else
exit_code=200
2019-12-20 03:02:26 -05:00
echo "ERROR: cannot parse line with invalid character: $line" >&2
2019-12-19 17:01:08 +00:00
continue
fi
2019-12-20 03:13:27 -05:00
if ! read -r fso mode_from_config owner group capability <<< "$line" ; then
exit_code=201
2019-12-20 03:02:26 -05:00
echo "ERROR: cannot parse line: $line" >&2
continue
fi
2019-12-08 16:49:28 +00:00
2019-12-20 03:40:47 -05:00
if ! [ -e "$fso" ]; then
echo "INFO: fso '$fso' does not exist!"
2019-12-08 16:49:28 +00:00
continue
fi
2019-12-20 03:42:09 -05:00
fso_without_trailing_slash="${fso%/}"
2019-12-20 04:06:28 -05:00
## Use dpkg-statoverride so permissions are not reset during upgrades.
2019-12-19 17:01:08 +00:00
nosuid=""
2019-12-20 03:13:27 -05:00
if [ "$mode_from_config" = "nosuid" ]; then
2019-12-19 17:01:08 +00:00
nosuid="true"
2019-12-20 03:25:17 -05:00
## If mode_from_config is "nosuid" the config does not set owner and
## group. Therefore do not enforce owner/group check.
2019-12-20 04:06:28 -05:00
2019-12-20 04:08:46 -05:00
add_nosuid_statoverride_entry
2019-12-20 03:24:07 -05:00
else
if ! seq -w 000 4777 | grep -qw "$mode_from_config"; then
echo "ERROR: Mode '$mode_from_config' is invalid!" >&2
continue
fi
2019-12-08 16:49:28 +00:00
2019-12-20 02:20:54 -05:00
if ! getent passwd | grep -q "^${owner}:"; then
2019-12-20 03:02:26 -05:00
echo "ERROR: User '$owner' does not exist!" >&2
2019-12-20 02:20:54 -05:00
continue
fi
2019-12-08 16:49:28 +00:00
2019-12-20 02:20:54 -05:00
if ! getent group | grep -q "^${group}:"; then
2019-12-20 03:02:26 -05:00
echo "ERROR: Group '$group' does not exist!" >&2
2019-12-20 02:20:54 -05:00
continue
fi
2019-12-20 03:11:11 -05:00
2019-12-20 04:06:28 -05:00
## Check there is an entry for the fso.
if dpkg-statoverride --list | grep -q "$fso_without_trailing_slash"; then
## There is an fso entry. Check if owner/group/mode match.
if ! dpkg-statoverride --list | grep -q "$owner $group $mode_from_config $fso_without_trailing_slash"; then
## The owner/group/mode do not match, therefore remove and re-add the entry to update it.
## fso_without_trailing_slash instead of fso to prevent
## "dpkg-statoverride: warning: stripping trailing /"
echo_wrapper dpkg-statoverride --remove "$fso_without_trailing_slash"
2019-12-20 04:08:46 -05:00
echo_wrapper dpkg-statoverride --add --update "$owner" "$group" "$mode_from_config" "$fso_without_trailing_slash"
2019-12-20 04:06:28 -05:00
fi
else
## There is no fso entry. Therefore add one.
2019-12-20 04:08:46 -05:00
echo_wrapper dpkg-statoverride --add --update "$owner" "$group" "$mode_from_config" "$fso_without_trailing_slash"
2019-12-08 16:49:28 +00:00
fi
fi
2019-12-20 03:43:36 -05:00
if [ "$capability" = "" ]; then
continue
fi
2019-12-08 16:49:28 +00:00
2019-12-20 03:43:36 -05:00
if [ "$capability" = "none" ]; then
echo_wrapper setcap -r "$fso"
else
if ! capsh --print | grep "Bounding set" | grep -q "$capability"; then
echo "ERROR: Capability '$capability' does not exist!" >&2
continue
2019-12-08 16:49:28 +00:00
fi
2019-12-20 03:43:36 -05:00
echo_wrapper setcap "${capability}+ep" "$fso"
2019-12-08 16:49:28 +00:00
fi
2019-12-20 03:46:50 -05:00
done < "$config_file"
2019-12-08 16:49:28 +00:00
}
set_file_perms
exit "$exit_code"