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

147 lines
4.0 KiB
Plaintext
Raw Normal View History

2019-12-08 11:49:28 -05:00
#!/bin/bash
2019-12-19 12:01:08 -05: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 12:01:08 -05:00
exit_code=0
2019-12-08 11:49:28 -05:00
config_file="/etc/permission-hardening.conf"
2019-12-19 12:01:08 -05: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-19 12:01:08 -05:00
add_statoverride_entry() {
if [ "${nosuid}" = "true" ]; then
2019-12-20 02:14:45 -05:00
echo "fso: $fso"
2019-12-19 12:01:08 -05:00
while read -r line; do
if ! read -r file_name existing_mode owner group; then
continue
fi
2019-12-20 02:47:04 -05:00
## -h file True if file is a symbolic Link.
2019-12-20 02:43:33 -05:00
## -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 03:00:39 -05:00
if test -h "$file_name" ; then
## https://forums.whonix.org/t/kernel-hardening/7296/323
2019-12-20 03:00:39 -05:00
true "skip symlink: $file_name"
continue
fi
2019-12-20 03:00:39 -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
2019-12-20 02:40:42 -05:00
new_mode="${existing_mode:1}"
else
2019-12-20 03:04:41 -05:00
new_mode="$existing_mode"
fi
2019-12-20 03:00:39 -05:00
echo "suid - file_name: '$file_name' | existing_mode: '$existing_mode' | new_mode: '$new_mode'"
2019-12-19 12:01:08 -05:00
2019-12-20 03:00:39 -05:00
if dpkg-statoverride --list | grep -q "$file_name"; then
2019-12-20 03:03:40 -05:00
if ! dpkg-statoverride --list | grep -q "$owner $group $new_mode $file_name"; then
2019-12-20 03:00:39 -05:00
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 12:01:08 -05:00
fi
else
2019-12-20 03:03:40 -05:00
echo_wrapper dpkg-statoverride --add --update "$owner" "$group" "$new_mode" "$file_name"
2019-12-19 12:01:08 -05:00
fi
fi
2019-12-20 02:14:45 -05:00
done < <( stat -c "%n %a %U %G" "${fso%/}/"** )
2019-12-19 12:01:08 -05:00
else
2019-12-20 03:13:27 -05:00
echo_wrapper dpkg-statoverride --add --update "$owner" "$group" "$mode_from_config" "${fso%/}"
2019-12-19 12:01:08 -05:00
fi
}
2019-12-08 11:49:28 -05: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 11:49:28 -05: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 12:01:08 -05: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 11:49:28 -05:00
2019-12-20 02:14:45 -05:00
if ! [ -e "${fso}" ]; then
echo "INFO: fso '${fso}' does not exist!"
2019-12-08 11:49:28 -05:00
continue
fi
2019-12-19 12:01:08 -05:00
nosuid=""
2019-12-20 03:13:27 -05:00
if [ "$mode_from_config" = "nosuid" ]; then
2019-12-19 12:01:08 -05:00
nosuid="true"
2019-12-20 03:13:27 -05:00
elif ! seq -w 000 4777 | grep -qw "$mode_from_config"; then
echo "ERROR: Mode '$mode_from_config' is invalid!" >&2
2019-12-08 11:49:28 -05:00
continue
fi
2019-12-20 03:19:12 -05:00
if [ "$mode_from_config" = "nosuid" ]; then
## If mode_from_config is "nosuid" the config does not set owner and
## group. Therefore do not enforce owner/group check.
true OK
else
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 11:49:28 -05: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-08 11:49:28 -05:00
fi
2019-12-20 03:11:11 -05:00
## Use dpkg-statoverride so permissions are not reset during upgrades.
2019-12-20 02:14:45 -05:00
if dpkg-statoverride --list | grep -q "${fso%/}"; then
## If there is an entry for the fso, but the owner/group/mode do not
2019-12-08 11:49:28 -05:00
## match, we remove and re-add the entry to update it.
if ! dpkg-statoverride --list | grep -q "$owner $group $mode_from_config ${fso%/}"; then
2019-12-20 02:14:45 -05:00
echo_wrapper dpkg-statoverride --remove "${fso}"
2019-12-19 12:01:08 -05:00
add_statoverride_entry
2019-12-08 11:49:28 -05:00
fi
else
2019-12-19 12:01:08 -05:00
add_statoverride_entry
2019-12-08 11:49:28 -05:00
fi
2019-12-20 03:00:39 -05:00
if ! [ "$capability" = "" ]; then
if [ "$capability" = "none" ]; then
2019-12-20 02:14:45 -05:00
echo_wrapper setcap -r "${fso}"
2019-12-08 11:49:28 -05:00
else
2019-12-20 03:00:39 -05:00
if ! capsh --print | grep "Bounding set" | grep -q "$capability"; then
echo "ERROR: Capability '$capability' does not exist!" >&2
2019-12-08 11:49:28 -05:00
continue
fi
2019-12-20 02:14:45 -05:00
echo_wrapper setcap "${capability}+ep" "${fso}"
2019-12-08 11:49:28 -05:00
fi
fi
2019-12-09 02:22:16 -05:00
done < "${config_file}"
2019-12-08 11:49:28 -05:00
}
set_file_perms
exit "$exit_code"