2017-04-17 14:23:59 -04:00
|
|
|
#!/bin/sh
|
|
|
|
|
2018-04-12 15:24:46 -04:00
|
|
|
## Protect startup of Qubes VMs from /rw scripts ##
|
|
|
|
## https://github.com/tasket/Qubes-VM-hardening ##
|
|
|
|
## Copyright 2017-2018 Christopher Laprise ##
|
|
|
|
## tasket@protonmail.com ##
|
|
|
|
|
|
|
|
# This is part of Qubes-VM-hardening.
|
|
|
|
# Qubes-VM-hardening is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Qubes-VM-hardening is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-09 06:48:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Source Qubes library.
|
|
|
|
. /usr/lib/qubes/init/functions
|
2017-04-20 19:33:44 -04:00
|
|
|
|
2017-05-12 05:47:53 -04:00
|
|
|
# Define sh, bash, X and desktop init scripts in /home/user
|
2017-04-17 14:23:59 -04:00
|
|
|
# to be protected
|
|
|
|
chfiles=".bashrc .bash_profile .bash_login .bash_logout .profile \
|
|
|
|
.xprofile .xinitrc .xserverrc .xsession"
|
2018-02-20 17:40:29 -05:00
|
|
|
chdirs="bin .local/bin .config/autostart .config/plasma-workspace/env \
|
2017-05-13 15:00:13 -04:00
|
|
|
.config/plasma-workspace/shutdown .config/autostart-scripts"
|
2018-04-02 10:55:55 -04:00
|
|
|
|
2017-05-12 05:47:53 -04:00
|
|
|
vmname=`qubesdb-read /name`
|
|
|
|
rw=/mnt/rwtmp
|
2018-04-03 15:54:55 -04:00
|
|
|
rwbak=$rw/vm-boot-protect
|
2018-04-02 10:55:55 -04:00
|
|
|
errlog=/var/run/vm-protect-error
|
2018-04-03 11:01:31 -04:00
|
|
|
defdir=/etc/default/vms
|
|
|
|
|
2017-04-17 14:23:59 -04:00
|
|
|
|
2017-05-13 15:00:13 -04:00
|
|
|
# Function: Make user scripts immutable.
|
2017-05-09 06:48:12 -04:00
|
|
|
make_immutable() {
|
2017-05-13 15:00:13 -04:00
|
|
|
#initialize_home $rw/home ifneeded
|
2017-05-09 06:48:12 -04:00
|
|
|
cd $rw/home/user
|
|
|
|
mkdir -p $chdirs
|
|
|
|
touch $chfiles
|
|
|
|
chattr -R -f +i $chfiles $chdirs
|
2017-05-13 15:00:13 -04:00
|
|
|
cd /root
|
2018-02-20 17:40:29 -05:00
|
|
|
#touch $rw/home/user/FIXED #debug
|
2017-05-09 06:48:12 -04:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:55:55 -04:00
|
|
|
# Start rescue shell then exit/fail
|
|
|
|
abort_startup() {
|
|
|
|
echo "$1" >>$errlog
|
|
|
|
cat $errlog
|
|
|
|
|
|
|
|
umount /dev/xvdb
|
|
|
|
mv -f /dev/xvdb /dev/badxvdb
|
|
|
|
truncate --size=500M /root/dev-xvdb
|
|
|
|
loop=`losetup --find --show /root/dev-xvdb`
|
|
|
|
mv -f $loop /dev/xvdb
|
|
|
|
|
|
|
|
cat /etc/bashrc /etc/bash.bashrc >/etc/bashrc-insert
|
|
|
|
echo "echo '** VM-BOOT-PROTECT SERVICE SHELL'" >/etc/bashrc
|
2018-04-03 16:49:45 -04:00
|
|
|
echo "echo '** Private volume is located at /dev/badxvdb'" >>/etc/bashrc
|
2018-04-02 10:55:55 -04:00
|
|
|
echo "cat $errlog" >>/etc/bashrc
|
|
|
|
echo ". /etc/bashrc-insert" >>/etc/bashrc
|
|
|
|
ln -f /etc/bashrc /etc/bash.bashrc
|
|
|
|
echo '/usr/bin/nohup /usr/bin/xterm /bin/bash 0<&- &>/dev/null &' \
|
|
|
|
>/etc/X11/Xsession.d/98rescue
|
2017-05-12 05:47:53 -04:00
|
|
|
exit 1
|
2018-04-02 10:55:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Don't bother with root protections in template or standalone
|
|
|
|
if ! is_rwonly_persistent; then
|
2018-04-03 16:53:21 -04:00
|
|
|
if qsvc vm-boot-protect; then
|
|
|
|
make_immutable
|
|
|
|
fi
|
2018-04-02 10:55:55 -04:00
|
|
|
exit 0
|
2017-04-17 14:23:59 -04:00
|
|
|
fi
|
2018-04-02 10:55:55 -04:00
|
|
|
|
|
|
|
echo >$errlog # Clear
|
|
|
|
|
2018-03-29 02:57:06 -04:00
|
|
|
if qsvc vm-boot-protect-cli; then
|
2018-04-02 10:55:55 -04:00
|
|
|
abort_startup "CLI requested."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Mount private volume in temp location
|
|
|
|
mkdir -p $rw
|
|
|
|
if [ -e /dev/xvdb ] && mount -o ro /dev/xvdb $rw ; then
|
|
|
|
echo "Good read-only mount."
|
|
|
|
else
|
2018-04-12 15:43:14 -04:00
|
|
|
echo "Mount failed. Let qubes-mount-dirs (re)initialize volume..."
|
|
|
|
exit 0
|
2017-05-13 15:00:13 -04:00
|
|
|
fi
|
|
|
|
|
2017-04-17 14:23:59 -04:00
|
|
|
|
2018-04-02 10:55:55 -04:00
|
|
|
|
2017-05-09 06:48:12 -04:00
|
|
|
# Protection measures for /rw dirs:
|
2018-03-29 02:57:06 -04:00
|
|
|
# Activated by presence of vm-boot-protect-root Qubes service.
|
2017-05-12 05:47:53 -04:00
|
|
|
# * Hashes in vms/vms.all.SHA and vms/$vmname.SHA files will be checked.
|
2018-04-02 10:55:55 -04:00
|
|
|
# * Remove /rw root startup files (config, usrlocal, bind-dirs).
|
2017-05-12 05:47:53 -04:00
|
|
|
# * Contents of vms/vms.all and vms/$vmname folders will be copied.
|
2018-04-03 15:54:55 -04:00
|
|
|
privdirs=${privdirs:-"/rw/config /rw/usrlocal /rw/bind-dirs"}
|
2017-05-09 06:48:12 -04:00
|
|
|
|
2018-03-29 02:57:06 -04:00
|
|
|
if qsvc vm-boot-protect-root && is_rwonly_persistent; then
|
2017-05-09 06:48:12 -04:00
|
|
|
|
|
|
|
# Check hashes
|
|
|
|
checkcode=0
|
|
|
|
echo "File hash checks:" >/tmp/vm-protect-sum-error
|
2017-05-12 05:47:53 -04:00
|
|
|
for vmset in vms.all $vmname; do
|
2017-05-09 06:48:12 -04:00
|
|
|
if [ -f $defdir/$vmset.SHA ]; then
|
2018-04-02 10:55:55 -04:00
|
|
|
sha256sum --strict -c $defdir/$vmset.SHA >>$errlog 2>&1
|
2017-05-09 06:48:12 -04:00
|
|
|
checkcode=$((checkcode+$?))
|
|
|
|
fi
|
|
|
|
done
|
2017-05-12 05:47:53 -04:00
|
|
|
|
|
|
|
# Stop system startup on checksum mismatch:
|
2017-05-09 06:48:12 -04:00
|
|
|
if [ $checkcode != 0 ]; then
|
2018-04-02 10:55:55 -04:00
|
|
|
abort_startup "Hash check failed!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Begin write operations
|
|
|
|
if [ -e /dev/xvdb ] && mount -o remount,rw /dev/xvdb $rw ; then
|
|
|
|
echo Good rw remount.
|
|
|
|
else
|
|
|
|
abort_startup "Remount failed!"
|
2017-05-09 06:48:12 -04:00
|
|
|
fi
|
|
|
|
|
2017-05-09 19:02:54 -04:00
|
|
|
# Files mutable for del/copy operations
|
2017-05-09 06:48:12 -04:00
|
|
|
cd $rw/home/user
|
2017-05-12 05:47:53 -04:00
|
|
|
chattr -R -f -i $chfiles $chdirs $privdirs
|
2017-05-13 15:00:13 -04:00
|
|
|
cd /root
|
2017-05-12 05:47:53 -04:00
|
|
|
|
2018-04-02 10:55:55 -04:00
|
|
|
|
2017-05-12 05:47:53 -04:00
|
|
|
# Deactivate private.img config dirs
|
2018-04-03 15:54:55 -04:00
|
|
|
mkdir -p $rwbak
|
|
|
|
for dir in $privdirs; do # maybe use 'eval' for privdirs quotes/escaping
|
2018-04-02 10:55:55 -04:00
|
|
|
echo "Deactivate $dir"
|
2018-04-03 15:54:55 -04:00
|
|
|
subdir=`echo $dir |sed -r 's|^/rw/||'`
|
|
|
|
bakdir="$rwbak/BAK-$subdir"
|
|
|
|
origdir="$rwbak/ORIG-$subdir"
|
|
|
|
if [ -d "$bakdir" ] && [ ! -d "$origdir" ]; then
|
|
|
|
mv "$bakdir" "$origdir"
|
2018-01-25 07:46:33 -05:00
|
|
|
fi
|
2018-04-03 15:54:55 -04:00
|
|
|
rm -rf "$bakdir"
|
|
|
|
mv "$rw/$subdir" "$bakdir"
|
|
|
|
mkdir -p "$rw/$subdir"
|
2017-05-09 19:02:54 -04:00
|
|
|
done
|
2017-05-12 05:47:53 -04:00
|
|
|
|
|
|
|
for vmset in vms.all $vmname; do
|
|
|
|
|
|
|
|
# Process whitelists...
|
2017-05-13 15:00:13 -04:00
|
|
|
cat $defdir/$vmset.whitelist \
|
|
|
|
| while read wlfile; do
|
2017-05-12 05:47:53 -04:00
|
|
|
# Must begin with '/rw/'
|
2018-04-03 15:54:55 -04:00
|
|
|
if echo $wlfile |grep -q "^\/rw\/"; then
|
|
|
|
srcfile="`echo $wlfile |sed -r \"s|^/rw/(.+)$|$rwbak/BAK-\1|\"`"
|
2017-05-13 15:00:13 -04:00
|
|
|
dstfile="`echo $wlfile |sed -r \"s|^/rw/(.+)$|$rw/\1|\"`"
|
|
|
|
dstdir="`dirname \"$dstfile\"`"
|
|
|
|
if [ ! -e "$srcfile" ]; then
|
2018-04-02 10:55:55 -04:00
|
|
|
echo "Whitelist entry not present in filesystem:"
|
|
|
|
echo "$srcfile"
|
2017-05-13 15:00:13 -04:00
|
|
|
continue
|
|
|
|
# For very large dirs: mv whole dir when entry ends with '/'
|
|
|
|
elif echo $wlfile |grep -q "\/$"; then
|
|
|
|
echo "Whitelist mv $srcfile"
|
2018-04-03 15:54:55 -04:00
|
|
|
echo "to $dstfile"
|
2017-05-13 15:00:13 -04:00
|
|
|
mkdir -p "$dstdir"
|
2018-04-03 15:54:55 -04:00
|
|
|
mv -T "$srcfile" "$dstfile"
|
2017-05-12 05:47:53 -04:00
|
|
|
else
|
2017-05-13 15:00:13 -04:00
|
|
|
echo "Whitelist cp $srcfile"
|
2018-04-03 15:54:55 -04:00
|
|
|
mkdir -p "$dstdir"
|
2017-05-13 15:00:13 -04:00
|
|
|
cp -a --link "$srcfile" "$dstdir"
|
2017-05-12 05:47:53 -04:00
|
|
|
fi
|
2017-05-13 15:00:13 -04:00
|
|
|
elif [ -n "$wlfile" ]; then
|
2018-04-02 10:55:55 -04:00
|
|
|
echo "Whitelist path must begin with /rw/. Skipped."
|
2017-05-12 05:47:53 -04:00
|
|
|
fi
|
2017-05-13 15:00:13 -04:00
|
|
|
done
|
2017-05-09 06:48:12 -04:00
|
|
|
|
2017-05-12 05:47:53 -04:00
|
|
|
# Copy default files...
|
|
|
|
if [ -d $defdir/$vmset/rw ]; then
|
2018-04-02 10:55:55 -04:00
|
|
|
echo "Copy files from $defdir/$vmset/rw"
|
2018-03-29 09:54:31 -04:00
|
|
|
cp -af $defdir/$vmset/rw/* $rw
|
2018-04-03 15:54:55 -04:00
|
|
|
fi
|
|
|
|
|
2017-05-09 06:48:12 -04:00
|
|
|
done
|
|
|
|
|
2018-03-29 07:31:40 -04:00
|
|
|
# Keep configs invisible at runtime...
|
2018-04-03 11:01:31 -04:00
|
|
|
rm -rf "$defdir"
|
2018-03-29 07:31:40 -04:00
|
|
|
|
2017-05-09 06:48:12 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
make_immutable
|
2017-05-13 15:00:13 -04:00
|
|
|
umount $rw
|
2017-05-09 06:48:12 -04:00
|
|
|
exit 0
|