2019-12-08 16:49:28 +00:00
#!/bin/bash
2024-12-25 19:48:28 -06:00
# shellcheck disable=SC2076
2019-12-08 16:49:28 +00:00
2024-12-31 13:26:21 -05:00
## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
2019-12-19 17:01:08 +00:00
## See the file COPYING for copying conditions.
2019-12-21 07:47:00 -05:00
## https://forums.whonix.org/t/disable-suid-binaries/7706
2019-12-20 06:34:37 -05:00
## https://forums.whonix.org/t/re-mount-home-and-other-with-noexec-and-nosuid-among-other-useful-mount-options-for-better-security/7707
2024-07-26 08:31:17 -04:00
## dpkg-statoverride does not support end-of-options ("--").
2024-12-25 19:48:28 -06:00
## SC2076 is disabled because ShellCheck seems to think that any use of
## [[ ... =~ ... ]] is supposed to be a regex match. But [[ '...' =~ '...' ]]
## works very well for literal matching, and it is used that way extensively
## throughout this script.
2024-01-02 12:17:16 +01:00
set -o errexit -o nounset -o pipefail
2019-12-19 17:01:08 +00:00
2024-12-25 19:48:28 -06:00
## Constants
2024-07-25 12:20:16 +02:00
# shellcheck disable=SC2034
2024-07-24 11:27:51 -04:00
log_level=notice
2025-01-10 10:21:12 -05:00
store_dir="/var/lib/permission-hardener-v2"
2024-12-25 19:48:28 -06:00
state_file="${store_dir}/existing_mode/statoverride"
dpkg_admindir_parameter_existing_mode="--admindir ${store_dir}/existing_mode"
dpkg_admindir_parameter_new_mode="--admindir ${store_dir}/new_mode"
delimiter="#permission-hardener-delimiter#"
## Library imports
# shellcheck disable=SC1091
source /usr/libexec/helper-scripts/safe_echo.sh
2024-07-23 18:46:47 +02:00
# shellcheck disable=SC1091
source /usr/libexec/helper-scripts/log_run_die.sh
2024-12-25 19:48:28 -06:00
## Functions
2019-12-20 11:03:22 -05:00
echo_wrapper_ignore() {
2024-12-25 19:48:28 -06:00
if [ "${1}" = 'verbose' ]; then
2024-07-23 18:46:47 +02:00
shift
2024-11-14 16:31:12 -05:00
log notice "Executing: $*"
2024-12-25 19:48:28 -06:00
elif [ "${1}" = 'silent' ]; then
2024-07-23 18:46:47 +02:00
shift
2024-12-25 19:48:28 -06:00
else
log error "Unrecognized command '${1}'! calling function name: '${FUNCNAME[1]}'" >&2
return
2024-07-22 17:06:07 +02:00
fi
2024-01-02 12:17:16 +01:00
"$@" 2>/dev/null || true
2019-12-23 01:32:42 -05:00
}
2019-12-20 11:03:22 -05:00
echo_wrapper_audit() {
2024-12-24 20:14:57 -06:00
local return_code
2024-12-25 19:48:28 -06:00
if [ "${1}" = 'verbose' ]; then
2024-07-23 18:46:47 +02:00
shift
2024-11-14 16:31:12 -05:00
log notice "Executing: $*"
2024-12-25 19:48:28 -06:00
elif [ "${1}" = 'silent' ]; then
2024-07-23 18:46:47 +02:00
shift
2024-12-25 19:48:28 -06:00
else
log error "Unrecognized command '${1}'! calling function name: '${FUNCNAME[1]}'" >&2
return
2024-07-22 17:06:07 +02:00
fi
2024-01-02 12:17:16 +01:00
return_code=0
"$@" ||
{
return_code="$?"
exit_code=203
2024-07-23 18:46:47 +02:00
log error "Command '$*' failed with exit code '${return_code}'! calling function name: '${FUNCNAME[1]}'" >&2
2024-01-02 12:17:16 +01:00
}
}
2024-07-25 12:20:16 +02:00
## Some tools may fail on newlines and even variable assignment to array may
## fail if a variable that will be assigned to an array element contains
## characters that are used as delimiters.
2024-12-24 20:14:57 -06:00
block_newlines() {
2024-07-25 12:20:16 +02:00
local newline_variable newline_value
2024-12-24 20:14:57 -06:00
newline_variable="${1:-}"
newline_value="${2:-}"
2024-07-25 12:20:16 +02:00
## dpkg-statoverride: error: path may not contain newlines
if [[ "${newline_value}" != "${newline_value//$'\n'/NEWLINE}" ]]; then
log warn "Skipping ${newline_variable} that contains newlines: '${newline_value}'" >&2
return 1
fi
}
2024-12-24 20:14:57 -06:00
output_stat() {
2025-01-21 21:49:03 -06:00
local file_name stat_output stat_output_newlined hardlink_count
2024-12-25 19:48:28 -06:00
declare -a arr
2024-12-24 20:14:57 -06:00
file_name="${1:-}"
2024-07-25 12:20:16 +02:00
2024-12-25 19:48:28 -06:00
if [ -z "${file_name}" ]; then
2024-07-25 12:20:16 +02:00
log error "File name is empty. file_name: '${file_name}'" >&2
return 1
fi
block_newlines file "${file_name}"
2024-12-25 19:48:28 -06:00
if [ ! -e "${file_name}" ]; then
log info "File does not exist. file_name: '${file_name}'" >&2
existing_mode=''
existing_owner=''
existing_group=''
file_name_from_stat=''
return 0
fi
2024-07-25 12:20:16 +02:00
2024-12-25 19:48:28 -06:00
if ! stat_output="$(stat -L \
2025-01-21 21:49:03 -06:00
--format="%a${delimiter}%U${delimiter}%G${delimiter}%n${delimiter}%h${delimiter}" \
2024-12-25 19:48:28 -06:00
-- "${file_name}")"; then
2024-07-25 12:20:16 +02:00
log error "Failed to run 'stat' on file: '${file_name}'!" >&2
return 1
fi
2024-12-30 19:23:20 -06:00
if [ -z "$stat_output" ]; then
2024-07-26 08:52:42 -04:00
log error "stat_output is empty.
File name: '${file_name}'
Stat output: '${stat_output}'
stat_output_newlined: '${stat_output_newlined}'
2024-12-24 20:14:57 -06:00
line: '${processed_config_line}'
2024-07-26 08:52:42 -04:00
" >&2
return 1
fi
2024-07-25 12:20:16 +02:00
stat_output_newlined="$(printf '%s\n' "${stat_output//${delimiter}/$'\n'}")"
2024-07-26 08:52:42 -04:00
2024-12-30 19:23:20 -06:00
if [ -z "${stat_output_newlined}" ]; then
2024-07-26 08:52:42 -04:00
log error "stat_output_newlined is empty.
File name: '${file_name}'
Stat output: '${stat_output}'
stat_output_newlined: '${stat_output_newlined}'
2024-12-24 20:14:57 -06:00
line: '${processed_config_line}'
2024-07-26 08:52:42 -04:00
" >&2
return 1
fi
2024-07-25 12:20:16 +02:00
readarray -t arr <<< "${stat_output_newlined}"
2024-12-25 19:48:28 -06:00
if [ "${#arr[@]}" = '0' ]; then
2024-07-26 08:49:14 -04:00
log error "Array length is 0.
2024-07-26 08:46:08 -04:00
File name: '${file_name}'
Stat output: '${stat_output}'
stat_output_newlined: '${stat_output_newlined}'
2024-12-24 20:14:57 -06:00
line: '${processed_config_line}'
2024-07-26 08:46:08 -04:00
" >&2
2024-07-25 12:20:16 +02:00
return 1
fi
existing_mode="${arr[0]}"
existing_owner="${arr[1]}"
existing_group="${arr[2]}"
file_name_from_stat="${arr[3]}"
2025-01-21 21:49:03 -06:00
hardlink_count="${arr[4]}"
2024-07-25 12:20:16 +02:00
2024-12-25 19:48:28 -06:00
if [ "$file_name" != "$file_name_from_stat" ]; then
2024-07-25 12:20:16 +02:00
log error "\
File name is different from file name received from stat:
2024-07-26 08:46:08 -04:00
File name: '${file_name}'
File name from stat: '${file_name_from_stat}'
2024-12-24 20:14:57 -06:00
line: '${processed_config_line}'
2024-07-26 08:46:08 -04:00
" >&2
2024-07-25 12:20:16 +02:00
return 1
fi
2025-01-21 21:49:03 -06:00
## We can't handle files with hardlinks because figuring out all of the files
## in a "hardlink pool" requires scanning the whole filesystem, which would
## result in an unacceptable performance hit for this script. We don't check
## directory hardlinks since directories can't have traditional hardlinks.
if [ ! -d "${file_name_from_stat}" ]; then
if (( hardlink_count > 1 )); then
log error "\
File has unexpected hardlinks, cannot handle.
File name: '${file_name}'
File name from stat: '${file_name_from_stat}'
line: '${processed_config_line}'
" >&2
return 1
fi
fi
2024-12-25 19:48:28 -06:00
if [ -z "${existing_mode}" ]; then
2024-12-24 20:14:57 -06:00
log error "Existing mode is empty. Stat output: '${stat_output}', line: '${processed_config_line}'" >&2
2024-07-25 12:20:16 +02:00
return 1
fi
2024-12-25 19:48:28 -06:00
if [ -z "${existing_owner}" ]; then
2024-12-24 20:14:57 -06:00
log error "Existing owner is empty. Stat output: '${stat_output}', line: '${processed_config_line}'" >&2
2024-07-25 12:20:16 +02:00
return 1
fi
2024-12-25 19:48:28 -06:00
if [ -z "${existing_group}" ]; then
2024-12-24 20:14:57 -06:00
log error "Existing group is empty. Stat output: '${stat_output}', line: '${processed_config_line}'" >&2
2024-07-25 12:20:16 +02:00
return 1
fi
2025-01-21 21:05:03 -06:00
## If a symlink was passed as input, return the original file's path rather
## than the symlink to avoid problems stemming from using the wrong path
if [ -h "${file_name_from_stat}" ]; then
file_name_from_stat="$(realpath "${file_name_from_stat}")"
fi
2024-07-25 12:20:16 +02:00
}
2024-12-24 20:14:57 -06:00
print_usage(){
safe_echo "Usage: ${0##*/} enable
${0##*/} disable [FILE|all]
2024-12-25 19:48:28 -06:00
${0##*/} print-policy
${0##*/} print-state
${0##*/} print-policy-applied-state
2025-01-20 22:43:55 -06:00
${0##*/} print-diagnostics
2024-12-24 20:14:57 -06:00
Examples:
${0##*/} enable
${0##*/} disable all
${0##*/} disable /usr/bin/newgrp" >&2
}
add_to_policy() {
local file_name file_mode file_owner file_group updated_entry policy_idx \
file_capabilities
file_name="${1:-}"
file_mode="${2:-}"
file_owner="${3:-}"
file_group="${4:-}"
file_capabilities="${5:-}"
updated_entry=false
2025-01-21 21:05:03 -06:00
if [ -h "${file_name}" ]; then
file_name="$(realpath "${file_name}")" || return 1
fi
2024-12-24 20:14:57 -06:00
for (( policy_idx=0; policy_idx < ${#policy_file_list[@]}; policy_idx++ )); do
if [ "${policy_file_list[policy_idx]}" = "${file_name}" ]; then
policy_mode_list[policy_idx]="${file_mode}"
policy_user_owner_list[policy_idx]="${file_owner}"
policy_group_owner_list[policy_idx]="${file_group}"
policy_capability_list[policy_idx]="${file_capabilities}"
updated_entry=true
break
fi
done
if [ "${updated_entry}" != 'true' ]; then
policy_file_list+=( "${file_name}" )
policy_mode_list+=( "${file_mode}" )
policy_user_owner_list+=( "${file_owner}" )
policy_group_owner_list+=( "${file_group}" )
policy_capability_list+=( "${file_capabilities}" )
fi
}
check_nosuid_whitelist() {
local target_file match_white_list_entry
target_file="${1:-}"
## Handle whitelists, if we're supposed to
2024-12-25 19:48:28 -06:00
[ "${whitelists_disable_all}" = 'true' ] && return 0
2024-12-24 20:14:57 -06:00
2024-12-25 19:48:28 -06:00
## literal matching is intentional here
[[ " ${policy_disable_white_list[*]} " =~ " ${target_file} " ]] && return 0
## literal matching is intentional here too
2024-12-30 19:23:20 -06:00
[[ " ${policy_exact_white_list[*]} " =~ " ${target_file} " ]] && return 1
2024-12-24 20:14:57 -06:00
2024-12-25 19:48:28 -06:00
for match_white_list_entry in "${policy_match_white_list[@]:-}"; do
if safe_echo "${target_file}" \
| grep --quiet --fixed-strings -- "${match_white_list_entry}"; then
return 1
fi
done
2024-12-24 20:14:57 -06:00
return 0
2019-12-23 00:59:24 -05:00
}
2024-12-24 20:14:57 -06:00
load_early_nosuid_policy() {
local target_file find_list_item
target_file="${1:-}"
# shellcheck disable=SC2185
while IFS="" read -r -d "" find_list_item; do
check_nosuid_whitelist "${find_list_item}" || continue
2024-07-24 10:58:05 -04:00
2024-07-26 08:07:08 -04:00
## sets:
## exiting_mode
## existing_owner
## existing_group
2024-12-24 20:14:57 -06:00
output_stat "${find_list_item}"
2024-12-25 19:48:28 -06:00
if [ -z "${file_name_from_stat}" ]; then
continue
fi
2024-01-02 12:17:16 +01:00
2024-12-25 19:48:28 -06:00
## -h file True if file is a symbolic link.
2024-12-24 20:14:57 -06:00
if [ -h "${find_list_item}" ]; then
2024-01-02 12:17:16 +01:00
## https://forums.whonix.org/t/disable-suid-binaries/7706/14
2024-12-24 20:14:57 -06:00
log info "Skip symlink: '${find_list_item}'"
2024-01-02 12:17:16 +01:00
continue
fi
2024-12-24 20:14:57 -06:00
if [ -d "${find_list_item}" ]; then
log info "Skip directory: '${find_list_item}'"
2024-01-02 12:17:16 +01:00
continue
fi
2024-12-25 19:48:28 -06:00
## Remove suid / gid and execute permission for 'group' and 'others'.
## Similar to: chmod og-ugx /path/to/filename
## Removing execution permission is useful to make binaries such as 'su'
## fail closed rather than fail open if suid was removed from these.
## Do not remove read access since no security benefit and easier to
## manually undo for users.
## Are there suid or sgid binaries which are still useful if suid / sgid
## has been removed from these?
2024-12-24 20:14:57 -06:00
local new_mode
new_mode='744'
2025-01-21 21:05:03 -06:00
add_to_policy "${file_name_from_stat}" "${new_mode}" "${existing_owner}" \
2024-12-24 20:14:57 -06:00
"${existing_group}"
2024-12-25 19:48:28 -06:00
done < <(safe_echo_nonewline "${target_file}" \
| find -files0-from - -perm /u=s,g=s -print0)
}
2024-12-30 19:23:20 -06:00
## If the "target file" matches the start of the state file name, that's a
## likely match. This is used by load_late_nosuid_policy for detecting info
## about files that need SUID-locked that are in the state.
2024-12-25 19:48:28 -06:00
match_dir() {
local base_str match_str base_arr match_arr base_idx
base_str="${1}"
match_str="${2}"
[[ "${base_str}" =~ '//' ]] && return 1
[[ "${match_str}" =~ '//' ]] && return 1
IFS='/' read -r -a base_arr <<< "${base_str}"
IFS='/' read -r -a match_arr <<< "${match_str}"
(( ${#base_arr[@]} > ${#match_arr[@]} )) && return 1
for (( base_idx=0; base_idx < ${#base_arr[@]}; base_idx++ )); do
if [ "${base_arr[base_idx]}" != "${match_arr[base_idx]}" ]; then
return 1
fi
done
return 0
2024-12-24 20:14:57 -06:00
}
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
load_late_nosuid_policy() {
local target_file state_idx state_file_item state_user_owner_item \
2024-12-25 19:48:28 -06:00
state_group_owner_item new_mode
2024-12-24 20:14:57 -06:00
target_file="${1:-}"
for (( state_idx=0; state_idx < ${#state_file_list[@]}; state_idx++ )); do
state_file_item="${state_file_list[state_idx]}"
check_nosuid_whitelist "${state_file_item}" || continue
2024-12-25 19:48:28 -06:00
match_dir "${target_file}" "${state_file_item}" || continue
2024-01-02 12:17:16 +01:00
2024-12-25 19:48:28 -06:00
if [ -h "${state_file_item}" ]; then
## https://forums.whonix.org/t/disable-suid-binaries/7706/14
log info "Skip symlink: '${state_file_item}'"
continue
fi
2024-07-24 13:30:30 -04:00
2024-12-25 19:48:28 -06:00
if [ -d "${state_file_item}" ]; then
log info "Skip directory: '${state_file_item}'"
continue
2024-01-02 12:17:16 +01:00
fi
2024-12-25 19:48:28 -06:00
state_user_owner_item="${state_user_owner_list[state_idx]}"
state_group_owner_item="${state_group_owner_list[state_idx]}"
new_mode='744'
add_to_policy "${state_file_item}" "${new_mode}" \
"${state_user_owner_item}" "${state_group_owner_item}"
2024-12-24 20:14:57 -06:00
done
2024-01-02 12:17:16 +01:00
}
2019-12-19 17:01:08 +00:00
2024-12-25 19:48:28 -06:00
load_state_without_policy() {
2024-12-30 19:23:20 -06:00
local line field_list
2024-12-25 19:48:28 -06:00
## Load the state file from disk
if [ -f "${state_file}" ]; then
while read -r line; do
2024-12-30 19:23:20 -06:00
read -r -a field_list <<< "${line}"
if (( ${#field_list[@]} != 4 )); then
2024-12-25 19:48:28 -06:00
log info \
"Invalid number of fields in state file line: '${line}'. Skipping."
continue
fi
2024-12-30 19:23:20 -06:00
state_user_owner_list+=( "${field_list[0]}" )
state_group_owner_list+=( "${field_list[1]}" )
state_mode_list+=( "${field_list[2]}" )
state_file_list+=( "${field_list[3]}" )
2024-12-25 19:48:28 -06:00
done < "${state_file}"
fi
}
2024-12-24 20:14:57 -06:00
load_state() {
## Config format:
## path options
## where options is one of:
## user_owner group_owner filemode [capability-setting]
## [nosuid|exactwhitelist|matchwhitelist|disablewhitelist]
2024-12-25 19:48:28 -06:00
##
## Additionally, the special value 'whitelists_disable_all=true' is understood
## to mean that all whitelisting should be ignored.
2024-07-26 08:40:10 -04:00
2024-12-30 19:23:20 -06:00
local config_file line field_list policy_nosuid_file_item policy_file_item
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
## Load configuration, deferring whitelist handling until later
for config_file in \
/usr/lib/permission-hardener.d/*.conf \
/etc/permission-hardener.d/*.conf \
/usr/local/etc/permission-hardener.d/*.conf \
/etc/permission-hardening.d/*.conf \
/usr/local/etc/permission-hardening.d/*.conf
do
if [ ! -f "${config_file}" ]; then
2024-01-02 12:17:16 +01:00
continue
fi
2024-12-25 19:48:28 -06:00
2024-12-24 20:14:57 -06:00
while read -r line; do
if [ -z "${line}" ]; then
true 'DEBUG: line is empty. Skipping.'
continue
fi
2024-12-25 19:48:28 -06:00
2024-12-24 20:14:57 -06:00
if [[ "${line}" =~ ^\s*# ]]; then
continue
fi
2024-12-25 19:48:28 -06:00
2024-12-30 19:23:20 -06:00
if ! [[ "${line}" =~ ^[-0-9a-zA-Z._/[:space:]]*$ ]]; then
2024-12-24 20:14:57 -06:00
exit_code=200
log error "Line contains invalid characters: '${line}'" >&2
## Safer to exit with error in this case.
## https://forums.whonix.org/t/disable-suid-binaries/7706/59
exit "${exit_code}"
fi
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
if [ "${line}" = 'whitelists_disable_all=true' ]; then
whitelists_disable_all=true
log info "whitelists_disable_all=true"
continue
fi
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
processed_config_line="${line}"
2024-01-02 12:17:16 +01:00
2024-12-30 19:23:20 -06:00
IFS=' ' read -r -a field_list <<< "${line}"
2024-01-02 12:17:16 +01:00
2025-01-02 20:41:40 -05:00
case "${#field_list[@]}" in
2|4|5) true;;
*)
exit_code=200
log error "Line contains an invalid number of fields: '${line}'" >&2
exit "${exit_code}"
;;
esac
2024-07-23 18:46:47 +02:00
2024-12-24 20:14:57 -06:00
# Strip trailing slash if appropriate
2024-12-30 19:23:20 -06:00
field_list[0]="${field_list[0]%/}"
2024-12-24 20:14:57 -06:00
2024-12-30 19:23:20 -06:00
case "${field_list[1]}" in
2024-12-24 20:14:57 -06:00
'exactwhitelist')
2024-12-30 19:23:20 -06:00
[ ! -e "${field_list[0]}" ] && continue
policy_exact_white_list+=( "${field_list[0]}" )
2024-12-24 20:14:57 -06:00
continue
;;
'matchwhitelist')
2024-12-30 19:23:20 -06:00
policy_match_white_list+=( "${field_list[0]}" )
2024-12-24 20:14:57 -06:00
continue
;;
'disablewhitelist')
2024-12-30 19:23:20 -06:00
policy_disable_white_list+=( "${field_list[0]}" )
2024-12-24 20:14:57 -06:00
continue
;;
'nosuid')
2024-12-30 19:23:20 -06:00
[ ! -e "${field_list[0]}" ] && continue
policy_nosuid_file_list+=( "${field_list[0]}" )
2024-12-24 20:14:57 -06:00
;;
*)
2024-12-30 19:23:20 -06:00
[ ! -e "${field_list[0]}" ] && continue
add_to_policy "${field_list[@]}"
2024-12-24 20:14:57 -06:00
;;
esac
done < "${config_file}"
done
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
## We have to handle nosuid files at the end since the whitelist arrays need
## built first.
for policy_nosuid_file_item in "${policy_nosuid_file_list[@]}"; do
load_early_nosuid_policy "${policy_nosuid_file_item}"
done
2024-01-02 12:17:16 +01:00
2024-12-25 19:48:28 -06:00
load_state_without_policy
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
## Find any files in the policy that don't already have a matching file in
## the state. Add those files to the state, and save them to the state file
## as well.
for policy_file_item in "${policy_file_list[@]}"; do
if [[ " ${state_file_list[*]} " =~ " ${policy_file_item} " ]]; then
2024-01-02 12:17:16 +01:00
continue
fi
2024-12-24 20:14:57 -06:00
output_stat "${policy_file_item}"
2024-12-25 19:48:28 -06:00
if [ -z "${file_name_from_stat}" ]; then
continue
fi
2025-01-21 21:05:03 -06:00
state_file_list+=( "${file_name_from_stat}" )
2024-12-24 20:14:57 -06:00
state_user_owner_list+=( "${existing_owner}" )
state_group_owner_list+=( "${existing_group}" )
state_mode_list+=( "${existing_mode}" )
# shellcheck disable=SC2086
echo_wrapper_audit silent dpkg-statoverride \
${dpkg_admindir_parameter_existing_mode} \
--add "${existing_owner}" "${existing_group}" "${existing_mode}" \
2025-01-21 21:05:03 -06:00
"${file_name_from_stat}"
2024-12-24 20:14:57 -06:00
done
2024-01-02 12:17:16 +01:00
2024-12-25 19:48:28 -06:00
## Fix up nosuid policies using state information
2024-12-24 20:14:57 -06:00
for policy_nosuid_file_item in "${policy_nosuid_file_list[@]}"; do
load_late_nosuid_policy "${policy_nosuid_file_item}"
done
}
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
apply_policy() {
local policy_idx did_state_update state_idx
## Modify the in-memory state so that all items that the policy affects match
## the policy. DO NOT save these changes to the state file!
for (( policy_idx=0; policy_idx < ${#policy_file_list[@]}; policy_idx++ )); do
did_state_update=false
for (( state_idx=0; state_idx < ${#state_file_list[@]}; state_idx++ )); do
if [ "${state_file_list[state_idx]}" = "${policy_file_list[policy_idx]}" ]; then
state_user_owner_list[state_idx]="${policy_user_owner_list[policy_idx]}"
state_group_owner_list[state_idx]="${policy_group_owner_list[policy_idx]}"
state_mode_list[state_idx]="${policy_mode_list[policy_idx]}"
did_state_update=true
break
2024-01-02 12:17:16 +01:00
fi
2024-12-24 20:14:57 -06:00
done
if [ "${did_state_update}" = 'false' ]; then
exit_code=206
2024-12-25 19:48:28 -06:00
log error \
"File exists in policy but not in state! File: '${policy_file_list[policy_idx]}'"
2024-12-24 20:14:57 -06:00
exit "${exit_code}"
fi
done
}
2019-12-20 02:43:33 -05:00
2024-12-24 20:14:57 -06:00
commit_policy() {
local policy_idx state_idx state_file_item \
state_user_owner_item state_group_owner_item \
state_mode_item orig_main_statoverride_db orig_new_statoverride_db \
policy_file_item policy_capability_item
## Check each file on the filesystem against the state, and update it if the
## state does not match. Also ensure the consistency of the new_mode database
## so that people can compare the original permissions of files with the new
## permissions.
orig_main_statoverride_db="$(dpkg-statoverride --list)" || true
# shellcheck disable=SC2086
orig_new_statoverride_db="$(dpkg-statoverride ${dpkg_admindir_parameter_new_mode} --list)" || true
for (( state_idx=0; state_idx < ${#state_file_list[@]}; state_idx++ )); do
state_file_item="${state_file_list[state_idx]}"
state_user_owner_item="${state_user_owner_list[state_idx]}"
state_group_owner_item="${state_group_owner_list[state_idx]}"
state_mode_item="${state_mode_list[state_idx]}"
## Get rid of leading zeros, stat doesn't output them due to how we use it.
## Using BASH_REMATCH is faster than sed. We capture all leading zeros into
## one group, and the rest of the string into a second group. The second
## group is the string we want. BASH_REMATCH[0] is the entire string,
## BASH_REMATCH[1] is the first match that we want to discard, and
## BASH_REMATCH[2] is the desired second group.
[[ "${state_mode_item}" =~ ^(0*)(.*) ]] || true;
state_mode_item="${BASH_REMATCH[2]}"
output_stat "${state_file_item}"
2024-12-25 19:48:28 -06:00
if [ -z "${file_name_from_stat}" ]; then
continue
fi
2024-12-24 20:14:57 -06:00
if [ "${existing_owner}" != "${state_user_owner_item}" ] \
|| [ "${existing_group}" != "${state_group_owner_item}" ] \
|| [ "${existing_mode}" != "${state_mode_item}" ]; then
2024-12-25 19:48:28 -06:00
if ! [[ "${passwd_file_contents}" =~ "${state_user_owner_item}:" ]]; then
2024-12-24 20:14:57 -06:00
log error "Owner from config does not exist: '${state_user_owner_item}'" >&2
2024-01-02 12:17:16 +01:00
continue
2019-12-22 19:42:40 -05:00
fi
2019-12-20 02:57:57 -05:00
2024-12-25 19:48:28 -06:00
if ! [[ "${group_file_contents}" =~ "${state_group_owner_item}:" ]]; then
2024-12-24 20:14:57 -06:00
log error "Group from config does not exist: '${state_group_owner_item}'" >&2
2024-01-02 12:17:16 +01:00
continue
2019-12-22 19:42:40 -05:00
fi
2024-12-25 19:48:28 -06:00
## Remove and reapply in main list
2025-01-21 21:05:03 -06:00
if [[ "${orig_main_statoverride_db}" =~ "${file_name_from_stat}" ]]; then
2024-12-24 20:14:57 -06:00
echo_wrapper_ignore silent dpkg-statoverride --remove \
2025-01-21 21:05:03 -06:00
"${file_name_from_stat}"
2019-12-22 19:42:40 -05:00
fi
2024-12-24 20:14:57 -06:00
echo_wrapper_audit verbose dpkg-statoverride --add --update \
"${state_user_owner_item}" "${state_group_owner_item}" \
2025-01-21 21:05:03 -06:00
"${state_mode_item}" "${file_name_from_stat}"
2024-01-02 12:17:16 +01:00
2024-12-25 19:48:28 -06:00
## Update item in secondary list
2025-01-21 21:05:03 -06:00
if [[ "${orig_new_statoverride_db}" =~ "${file_name_from_stat}" ]]; then
2024-01-02 12:17:16 +01:00
# shellcheck disable=SC2086
2024-12-24 20:14:57 -06:00
echo_wrapper_ignore silent dpkg-statoverride \
${dpkg_admindir_parameter_new_mode} --remove \
2025-01-21 21:05:03 -06:00
"${file_name_from_stat}"
2024-01-02 12:17:16 +01:00
fi
2024-12-24 20:14:57 -06:00
# shellcheck disable=SC2086
echo_wrapper_audit verbose dpkg-statoverride \
${dpkg_admindir_parameter_new_mode} --add \
"${state_user_owner_item}" "${state_group_owner_item}" \
2025-01-21 21:05:03 -06:00
"${state_mode_item}" "${file_name_from_stat}"
2024-01-02 12:17:16 +01:00
fi
2024-12-24 20:14:57 -06:00
done
## Apply capability hardening, dpkg-statoverride can't handle this so we have
## to do this manually
for (( policy_idx=0; policy_idx < ${#policy_file_list[@]}; policy_idx++ )); do
policy_file_item="${policy_file_list[policy_idx]}"
policy_capability_item="${policy_capability_list[policy_idx]}"
if [ -z "${policy_capability_item}" ]; then
2024-01-02 12:17:16 +01:00
continue
fi
2024-12-24 20:14:57 -06:00
if [ "${policy_capability_item}" = 'none' ]; then
echo_wrapper_ignore verbose setcap -r "${policy_file_item}"
if [ -n "$(getcap -- "${policy_file_item}")" ]; then
2024-01-02 12:17:16 +01:00
exit_code=205
2024-12-24 20:14:57 -06:00
log error \
"Removing capabilities failed. File: '${policy_file_item}'" >&2
2024-01-02 12:17:16 +01:00
continue
fi
else
2024-12-24 20:14:57 -06:00
if ! capsh --print \
| grep --fixed-strings -- "Bounding set" \
| grep --quiet -- "${policy_capability_item}"; then
log error \
"Capability from config does not exist: '${policy_capability_item}'" \
>&2
2024-01-02 12:17:16 +01:00
continue
2019-12-21 04:08:35 -05:00
fi
2024-01-02 12:17:16 +01:00
## feature request: dpkg-statoverride: support for capabilities
## https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=502580
2024-12-24 20:14:57 -06:00
echo_wrapper_audit verbose setcap "${policy_capability_item}+ep" \
-- "${policy_file_item}"
2024-01-02 12:17:16 +01:00
fi
done
2025-01-14 20:35:28 -06:00
log notice "\
To compare the current and previous permission modes, install 'meld' (or preferred diff tool) for comparison of file mode changes:
sudo apt install --no-install-recommends meld
meld ${store_dir}/existing_mode/statoverride ${store_dir}/new_mode/statoverride"
2024-01-02 12:17:16 +01:00
}
2019-12-20 05:59:05 -05:00
2024-12-24 20:14:57 -06:00
undo_policy_for_file() {
local undo_file state_idx state_file_item did_undo \
undo_all verbose orig_main_statoverride_db orig_new_statoverride_db \
state_user_owner_item state_group_owner_item state_mode_item
undo_file="${1}"
undo_all=false
verbose='--verbose'
if [ "${undo_file}" = 'all' ]; then
undo_all=true
verbose=''
fi
2024-01-02 12:17:16 +01:00
2024-12-24 20:14:57 -06:00
if [ ! -f "${state_file}" ]; then
true 'DEBUG: State file does not exist, hardening was not applied before.'
2024-01-02 12:17:16 +01:00
return 0
fi
2024-12-24 20:14:57 -06:00
did_undo=false
2019-12-23 00:36:41 -05:00
2024-12-24 20:14:57 -06:00
for (( state_idx=0; state_idx < ${#state_file_list[@]}; state_idx++ )); do
state_file_item="${state_file_list[state_idx]}"
if [ "${undo_all}" = 'true' ]; then
undo_file="${state_file_item}"
2024-01-02 12:17:16 +01:00
fi
2019-12-08 16:49:28 +00:00
2024-12-24 20:14:57 -06:00
if [ "${state_file_item}" = "${undo_file}" ]; then
orig_main_statoverride_db="$(dpkg-statoverride --list)" || true
2024-01-02 17:08:45 +01:00
# shellcheck disable=SC2086
2024-12-24 20:14:57 -06:00
orig_new_statoverride_db="$(dpkg-statoverride ${dpkg_admindir_parameter_new_mode} --list)" || true
2020-12-01 05:14:48 -05:00
2024-12-25 19:48:28 -06:00
if [[ "${orig_main_statoverride_db}" =~ "${undo_file}" ]]; then
2024-12-24 20:14:57 -06:00
echo_wrapper_ignore silent dpkg-statoverride --remove \
"${undo_file}"
fi
2024-12-25 19:48:28 -06:00
if [[ "${orig_new_statoverride_db}" =~ "${undo_file}" ]]; then
2024-12-24 20:14:57 -06:00
# shellcheck disable=SC2086
echo_wrapper_ignore silent dpkg-statoverride \
${dpkg_admindir_parameter_new_mode} --remove \
"${undo_file}"
fi
2019-12-20 03:11:11 -05:00
2024-12-24 20:14:57 -06:00
if [ -e "${undo_file}" ]; then
state_user_owner_item="${state_user_owner_list[state_idx]}"
state_group_owner_item="${state_group_owner_list[state_idx]}"
state_mode_item="${state_mode_list[state_idx]}"
2024-12-30 19:23:20 -06:00
chown ${verbose} -- "${state_user_owner_item}:${state_group_owner_item}" \
2024-12-24 20:14:57 -06:00
"${undo_file}" || exit_code=202
2024-12-25 19:48:28 -06:00
## chmod needs to be run after chown since chown removes suid.
2024-12-24 20:14:57 -06:00
chmod ${verbose} "${state_mode_item}" "${undo_file}" || exit_code=203
else
log info "File does not exist: '${undo_file}'"
fi
did_undo=true
2024-12-25 19:48:28 -06:00
if [ "${undo_all}" = 'false' ]; then
break
fi
2024-01-02 12:17:16 +01:00
fi
2024-12-24 20:14:57 -06:00
done
2019-12-23 02:29:47 -05:00
2024-12-24 20:14:57 -06:00
if ! [[ "${did_undo}" = 'false' ]]; then
log info "The specified file is not hardened, leaving unchanged.
2019-12-23 00:36:41 -05:00
2024-12-24 20:14:57 -06:00
File '${undo_file}' has not been removed from SUID Disabler and Permission Hardener during this invocation. This is expected if no policy was ever applied to the file before.
2019-12-20 03:43:36 -05:00
2024-07-23 09:55:02 +02:00
This program expects the full path to the file. Example:
$0 disable /usr/bin/newgrp # absolute path: works
$0 disable newgrp # relative path: does not work
2019-12-08 16:49:28 +00:00
2024-01-02 12:17:16 +01:00
To remove all:
$0 disable all
2023-11-05 16:40:49 -05:00
2024-01-16 09:23:54 -05:00
This change might not be permanent. For full instructions, see:
2024-01-02 12:17:16 +01:00
https://www.kicksecure.com/wiki/SUID_Disabler_and_Permission_Hardener
2023-11-05 16:39:10 -05:00
2024-01-02 12:17:16 +01:00
To view list of changed by SUID Disabler and Permission Hardener:
https://www.kicksecure.com/wiki/SUID_Disabler_and_Permission_Hardener#View_List_of_Permissions_Changed_by_SUID_Disabler_and_Permission_Hardener
2022-06-07 08:03:56 +00:00
2024-01-02 12:17:16 +01:00
For re-enabling any specific SUID binary:
https://www.kicksecure.com/wiki/SUID_Disabler_and_Permission_Hardener#Re-Enable_Specific_SUID_Binaries
For completely disabling SUID Disabler and Permission Hardener:
https://www.kicksecure.com/wiki/SUID_Disabler_and_Permission_Hardener#Disable_SUID_Disabler_and_Permission_Hardener"
fi
2019-12-20 05:49:11 -05:00
}
2024-12-24 20:14:57 -06:00
print_columns() {
local format_str bogus_str
format_str=''
for bogus_str in "$@"; do
format_str="${format_str}%s\t"
done
format_str="${format_str}\n"
2024-12-25 19:48:28 -06:00
## Using a dynamically generated format string on purpose.
2024-12-24 20:14:57 -06:00
# shellcheck disable=SC2059
printf "${format_str}" "$@"
2024-01-02 12:17:16 +01:00
}
2019-12-20 02:01:36 -05:00
2024-12-24 20:14:57 -06:00
print_policy() {
local policy_idx
2023-11-06 16:36:22 -05:00
2024-12-24 20:14:57 -06:00
print_columns 'File' 'User' 'Group' 'Mode' 'Capabilities'
for (( policy_idx=0; policy_idx < ${#policy_file_list[@]}; policy_idx++ )); do
print_columns \
"${policy_file_list[policy_idx]}" \
"${policy_user_owner_list[policy_idx]}" \
"${policy_group_owner_list[policy_idx]}" \
"${policy_mode_list[policy_idx]}" \
"${policy_capability_list[policy_idx]}"
done
2024-01-02 12:17:16 +01:00
}
2023-11-06 16:36:22 -05:00
2024-12-24 20:14:57 -06:00
print_state() {
local state_idx
print_columns 'File' 'User' 'Group' 'Mode'
for (( state_idx=0; state_idx < ${#state_file_list[@]}; state_idx++ )); do
print_columns \
"${state_file_list[state_idx]}" \
"${state_user_owner_list[state_idx]}" \
"${state_group_owner_list[state_idx]}" \
"${state_mode_list[state_idx]}"
done
}
2025-01-20 22:43:55 -06:00
print_raw_policy_config() {
2025-01-21 16:41:30 -06:00
local config_file
2025-01-20 22:43:55 -06:00
for config_file in \
/usr/lib/permission-hardener.d/*.conf \
/etc/permission-hardener.d/*.conf \
/usr/local/etc/permission-hardener.d/*.conf \
/etc/permission-hardening.d/*.conf \
/usr/local/etc/permission-hardening.d/*.conf
do
if [ ! -f "${config_file}" ]; then
continue
fi
echo "*** begin ${config_file} ***"
cat "${config_file}"
echo "*** end ${config_file} ***"
done
}
print_raw_state() {
local state_file
for state_file in "${store_dir}/existing_mode/statoverride" \
"${store_dir}/new_mode/statoverride"; do
echo "*** begin ${state_file} ***"
cat "${state_file}"
echo "*** end ${state_file} ***"
done
}
print_fs_audit() {
2025-01-21 16:41:30 -06:00
local state_idx state_file_item state_user_owner_item state_group_owner_item \
state_mode_item
2025-01-20 22:43:55 -06:00
echo 'Legend:'
echo '... - Warning about an unusual, but not necessarily wrong, condition'
echo '!!! - Warning about an unusual and definitely wrong condition'
echo '*** - File permission data, actual state on filesystem is consistent with policy'
echo '^^^ - File permission data, actual state on filesystem is inconsistent with policy'
echo 'vvv - File permissions specified by state, always shown after a ^^^ item'
echo
for (( state_idx=0; state_idx < ${#state_file_list[@]}; state_idx++ )); do
state_file_item="${state_file_list[state_idx]}"
state_user_owner_item="${state_user_owner_list[state_idx]}"
state_group_owner_item="${state_group_owner_list[state_idx]}"
state_mode_item="${state_mode_list[state_idx]}"
## Get rid of leading zeros, stat doesn't output them due to how we use it.
## Using BASH_REMATCH is faster than sed. We capture all leading zeros into
## one group, and the rest of the string into a second group. The second
## group is the string we want. BASH_REMATCH[0] is the entire string,
## BASH_REMATCH[1] is the first match that we want to discard, and
## BASH_REMATCH[2] is the desired second group.
[[ "${state_mode_item}" =~ ^(0*)(.*) ]] || true;
state_mode_item="${BASH_REMATCH[2]}"
output_stat "${state_file_item}"
if [ -z "${file_name_from_stat}" ]; then
2025-01-21 21:05:03 -06:00
echo "... '${file_name_from_stat}' does not exist"
2025-01-20 22:43:55 -06:00
continue
fi
if [ "${existing_owner}" != "${state_user_owner_item}" ] \
|| [ "${existing_group}" != "${state_group_owner_item}" ] \
|| [ "${existing_mode}" != "${state_mode_item}" ]; then
if ! [[ "${passwd_file_contents}" =~ "${state_user_owner_item}:" ]]; then
echo "!!! Owner from config does not exist: '${state_user_owner_item}'"
continue
fi
if ! [[ "${group_file_contents}" =~ "${state_group_owner_item}:" ]]; then
echo "!!! Group from config does not exist: '${state_group_owner_item}'"
continue
fi
echo "^^^ ${file_name_from_stat} ${existing_owner}:${existing_group} ${existing_mode}"
2025-01-21 21:05:03 -06:00
echo "vvv ${file_name_from_stat} ${state_user_owner_item}:${state_group_owner_item} ${state_mode_item}"
2025-01-20 22:43:55 -06:00
else
echo "*** ${file_name_from_stat} ${existing_owner}:${existing_group} ${existing_mode}"
fi
done
}
reset_global_vars() {
## Global variables
policy_file_list=()
policy_user_owner_list=()
policy_group_owner_list=()
policy_mode_list=()
policy_capability_list=()
policy_exact_white_list=()
policy_match_white_list=()
policy_disable_white_list=()
policy_nosuid_file_list=()
state_file_list=()
state_user_owner_list=()
state_group_owner_list=()
state_mode_list=()
whitelists_disable_all=false
existing_mode=''
existing_owner=''
existing_group=''
processed_config_line=''
file_name_from_stat=''
passwd_file_contents="$(getent passwd)"
group_file_contents="$(getent group)"
exit_code=0
}
reset_global_vars
2024-12-24 20:14:57 -06:00
## Setup and sanity checking
if [ "$(id -u)" != '0' ]; then
log error "Not running as root, aborting."
exit 1
fi
mkdir --parents "${store_dir}/existing_mode"
mkdir --parents "${store_dir}/new_mode"
echo_wrapper_audit silent which capsh getcap setcap stat find \
dpkg-statoverride getent grep 1>/dev/null
## Command parsing and execution
2024-01-02 12:17:16 +01:00
case "${1:-}" in
2024-12-24 20:14:57 -06:00
enable)
shift
load_state
apply_policy
commit_policy
;;
2024-01-02 12:17:16 +01:00
disable)
shift
case "${1:-}" in
2024-12-24 20:14:57 -06:00
"")
print_usage
exit 1
;;
*)
2024-12-25 19:48:28 -06:00
load_state_without_policy
2024-12-24 20:14:57 -06:00
undo_policy_for_file "${1}"
;;
2024-01-02 12:17:16 +01:00
esac
;;
2024-12-24 20:14:57 -06:00
print-policy)
load_state
print_policy
;;
print-state)
load_state
print_state
;;
print-policy-applied-state)
load_state
apply_policy
print_state
;;
2025-01-20 22:43:55 -06:00
print-diagnostics)
echo '=== BEGIN PERMISSION-HARDENER DIAGNOSTICS ==='
echo '--- BEGIN State without policy ---'
load_state_without_policy
print_state
echo '--- END State without policy ---'
reset_global_vars
echo '--- BEGIN Policy without state ---'
load_state
print_policy
echo '--- END Policy without state ---'
reset_global_vars
echo '--- BEGIN Policy-applied-state ---'
load_state
apply_policy
print_state
echo '--- END Policy-applied state ---'
reset_global_vars
echo '--- BEGIN Master dpkg-statoverride database ---'
dpkg-statoverride --list
echo '--- END Master dpkg-statoverride database ---'
echo '--- BEGIN Raw policy configuration ---'
print_raw_policy_config
echo '--- END Raw policy configuration ---'
echo '--- BEGIN Raw state data ---'
print_raw_state
echo '--- END Raw state data ---'
echo '--- BEGIN Filesystem state audit ---'
load_state
apply_policy
print_fs_audit
echo '--- END Filesystem state audit ---'
echo '=== END PERMISSION-HARDENER DIAGNOSTICS ==='
;;
2024-12-24 20:14:57 -06:00
-h|--help)
print_usage
exit 0
;;
*)
print_usage
exit 1
;;
2024-01-02 12:17:16 +01:00
esac
2024-12-25 19:48:28 -06:00
## Exit
2024-01-02 12:17:16 +01:00
if test "${exit_code}" != "0"; then
2024-07-23 18:46:47 +02:00
log error "Exiting with non-zero exit code: '${exit_code}'" >&2
2019-12-20 11:05:05 -05:00
fi
2024-01-02 12:17:16 +01:00
exit "${exit_code}"