| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# common.sh - helper functions for fscrypt command-line interface tests |
| 4 |
# |
| 5 |
# Copyright 2020 Google LLC |
| 6 |
# |
| 7 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
# use this file except in compliance with the License. You may obtain a copy of |
| 9 |
# the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, software |
| 14 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
# License for the specific language governing permissions and limitations under |
| 17 |
# the License. |
| 18 |
# |
| 19 |
|
| 20 |
# Use extra-strict mode. |
| 21 |
set -e -u -o pipefail |
| 22 |
|
| 23 |
# Don't allow running the test scripts directly. They need to be run via |
| 24 |
# run.sh, to set up everything correctly. |
| 25 |
if [ -z "${MNT:-}" ] || [ -z "${MNT_ROOT:-}" ]; then |
| 26 |
echo 1>&2 "ERROR: This script can only be run via run.sh, not on its own." |
| 27 |
exit 1 |
| 28 |
fi |
| 29 |
|
| 30 |
# Prints an error message, then fails the test by exiting with failure status. |
| 31 |
_fail() |
| 32 |
{ |
| 33 |
echo 1>&2 "ERROR: $1" |
| 34 |
exit 1 |
| 35 |
} |
| 36 |
|
| 37 |
# Runs a shell command and expects that it fails. |
| 38 |
_expect_failure() |
| 39 |
{ |
| 40 |
if eval "$1"; then |
| 41 |
_fail "command unexpectedly succeeded: \"$1\"" |
| 42 |
fi |
| 43 |
} |
| 44 |
|
| 45 |
# Prints a message to mark the beginning of the next part of the test. |
| 46 |
_print_header() |
| 47 |
{ |
| 48 |
echo |
| 49 |
echo "# $1" |
| 50 |
} |
| 51 |
|
| 52 |
# Deletes all files on the test filesystems, including all policies and |
| 53 |
# protectors. Leaves the fscrypt metadata directories themselves. |
| 54 |
_reset_filesystems() |
| 55 |
{ |
| 56 |
local mnt |
| 57 |
|
| 58 |
for mnt in "$MNT" "$MNT_ROOT"; do |
| 59 |
rm -rf "${mnt:?}"/* "${mnt:?}"/.fscrypt/{policies,protectors}/* |
| 60 |
done |
| 61 |
} |
| 62 |
|
| 63 |
# Prints the number of filesystems that have encryption support enabled. |
| 64 |
_get_enabled_fs_count() |
| 65 |
{ |
| 66 |
local count |
| 67 |
|
| 68 |
count=$(fscrypt status | awk '/filesystems supporting encryption/ { print $4 }') |
| 69 |
if [ -z "$count" ]; then |
| 70 |
_fail "encryption support status line not found" |
| 71 |
fi |
| 72 |
echo "$count" |
| 73 |
} |
| 74 |
|
| 75 |
# Gets the descriptor of the given protector. |
| 76 |
_get_protector_descriptor() |
| 77 |
{ |
| 78 |
local mnt=$1 |
| 79 |
local source=$2 |
| 80 |
|
| 81 |
case $source in |
| 82 |
custom) |
| 83 |
local name=$3 |
| 84 |
local description="custom protector \\\"$name\\\"" |
| 85 |
;; |
| 86 |
login) |
| 87 |
local user=$3 |
| 88 |
local description="login protector for $user" |
| 89 |
;; |
| 90 |
*) |
| 91 |
_fail "Unknown protector source $source" |
| 92 |
esac |
| 93 |
|
| 94 |
local descriptor |
| 95 |
descriptor=$(fscrypt status "$mnt" | |
| 96 |
awk -F ' *' '{ if ($3 == "'"$description"'") print $1 }') |
| 97 |
if [ -z "$descriptor" ]; then |
| 98 |
_fail "Can't find $description on $mnt" |
| 99 |
fi |
| 100 |
echo "$descriptor" |
| 101 |
} |
| 102 |
|
| 103 |
# Gets the descriptor of the login protector for $TEST_USER. |
| 104 |
_get_login_descriptor() |
| 105 |
{ |
| 106 |
_get_protector_descriptor "$MNT_ROOT" login "$TEST_USER" |
| 107 |
} |
| 108 |
|
| 109 |
# Prints the number of filesystems that have fscrypt metadata. |
| 110 |
_get_setup_fs_count() |
| 111 |
{ |
| 112 |
local count |
| 113 |
|
| 114 |
count=$(fscrypt status | awk '/filesystems with fscrypt metadata/ { print $5 }') |
| 115 |
if [ -z "$count" ]; then |
| 116 |
_fail "fscrypt metadata status line not found" |
| 117 |
fi |
| 118 |
echo "$count" |
| 119 |
} |
| 120 |
|
| 121 |
# Removes all fscrypt metadata from the given filesystem. |
| 122 |
_rm_metadata() |
| 123 |
{ |
| 124 |
rm -r "${1:?}/.fscrypt" |
| 125 |
} |
| 126 |
|
| 127 |
# Runs a shell command, ignoring its output (stdout and stderr) if it succeeds. |
| 128 |
# If the command fails, prints its output and fails the test. |
| 129 |
_run_noisy_command() |
| 130 |
{ |
| 131 |
if ! eval "$1" &> "$TMPDIR/out"; then |
| 132 |
_fail "Command failed: '$1'. Output was: $(cat "$TMPDIR/out")" |
| 133 |
fi |
| 134 |
} |
| 135 |
|
| 136 |
# Runs the given shell command as the test user. |
| 137 |
_user_do() |
| 138 |
{ |
| 139 |
su "$TEST_USER" --shell=/bin/bash --command="export PATH='$PATH'; $1" |
| 140 |
} |
| 141 |
|
| 142 |
# Runs the given shell command as the test user and expects it to fail. |
| 143 |
_user_do_and_expect_failure() |
| 144 |
{ |
| 145 |
_expect_failure "_user_do '$1'" |
| 146 |
} |
| 147 |
|
| 148 |
# Clear the test user's user keyring and unlink it from root's user keyring, if |
| 149 |
# it is linked into it. |
| 150 |
_cleanup_user_keyrings() |
| 151 |
{ |
| 152 |
local ringid |
| 153 |
|
| 154 |
ringid=$(_user_do "keyctl show @u" | awk '/keyring: _uid/{print $1}') |
| 155 |
|
| 156 |
_user_do "keyctl clear $ringid" |
| 157 |
keyctl unlink "$ringid" @u &> /dev/null || true |
| 158 |
} |
| 159 |
|
| 160 |
# Gives the test a new session keyring which contains the test user's keyring |
| 161 |
# but not root's keyring. Also clears the test user's keyring. This must be |
| 162 |
# called at the beginning of the test script as it may re-execute the script. |
| 163 |
_setup_session_keyring() |
| 164 |
{ |
| 165 |
# This *should* just use 'keyctl new_session', but that doesn't work if |
| 166 |
# the session keyring is owned by a user other than root. So instead we |
| 167 |
# have to use 'keyctl session' and re-execute the script. |
| 168 |
if [ -z "${FSCRYPT_SESSION_KEYRING_SET:-}" ]; then |
| 169 |
export FSCRYPT_SESSION_KEYRING_SET=1 |
| 170 |
set +e |
| 171 |
keyctl session - "$0" |& grep -v '^Joined session keyring' |
| 172 |
exit "${PIPESTATUS[0]}" |
| 173 |
fi |
| 174 |
|
| 175 |
# Link the test user's keyring into the new session keyring. |
| 176 |
keyctl setperm @s 0x3f000000 # all possessor permissions |
| 177 |
_user_do "keyctl link @u @s" |
| 178 |
|
| 179 |
# Clear the test user's keyring. |
| 180 |
_user_do "keyctl clear @u" |
| 181 |
} |
| 182 |
|
| 183 |
# Wraps the 'expect' command to force subprocesses to have 80-column output. |
| 184 |
expect() |
| 185 |
{ |
| 186 |
command expect -c 'set stty_init "cols 80"' -f - |
| 187 |
} |