forked from dnschneid/crouton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount-chroot
executable file
·384 lines (345 loc) · 13.1 KB
/
mount-chroot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/sh -e
# Copyright (c) 2016 The crouton Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
set -e
APPLICATION="${0##*/}"
BINDIR="`dirname "\`readlink -f -- "$0"\`"`"
CHROOTS="`readlink -m -- "$BINDIR/../chroots"`"
CREATE=''
ENCRYPT=''
KEYFILE=''
PRINT=''
ROOT="`readlink -m -- '/var/run/crouton'`"
MOUNTOPTS='rw,dev,exec,suid'
METRICSDIR='/run/metrics/external/crouton'
USAGE="$APPLICATION [options] name [...]
Mounts one or more chroots into a root-only subdirectory of $ROOT
Options:
-c CHROOTS Directory the chroots are in. Default: $CHROOTS
-e If the chroot is not encrypted, encrypt it.
If specified twice, prompt to change the encryption passphrase.
-k KEYFILE File or directory to store the (encrypted) encryption keys in.
If unspecified, the keys will be stored in the chroot if doing a
first encryption, or auto-detected on existing chroots.
-n Create the chroot if it doesn't exist.
-p Prints out the path to the mounted directory on stdout."
# Common functions
. "$BINDIR/../installer/functions"
# Process arguments
getopts_string='c:ek:np'
while getopts_nextarg; do
case "$getopts_var" in
c) CHROOTS="`readlink -m -- "$getopts_arg"`";;
e) ENCRYPT="$((ENCRYPT+1))";;
k) KEYFILE="$getopts_arg";;
n) CREATE='y';;
p) PRINT='y';;
\?) error 2 "$USAGE";;
esac
done
# Need at least one chroot listed
if [ $# = 0 ]; then
error 2 "$USAGE"
fi
# We need to run as root
if [ "$USER" != root -a "$UID" != 0 ]; then
error 2 "$APPLICATION must be run as root."
fi
# Make sure we always exit with echo on the tty.
addtrap "stty echo 2>/dev/null"
# Whitelists a directory for symlink and other hardening
whitelist() {
# As of 67, symlinks and fifos are blocked in stateful partitions.
# Add exceptions for this chroot.
local sec='/sys/kernel/security' mounted=''
if mountpoint -q "$sec"; then
mounted=y
elif ! mount -n -t securityfs -o nodev,noexec,nosuid securityfs "$sec"; then
return
fi
policies="$sec/chromiumos/inode_security_policies"
if [ -d "$policies" ]; then
for policy in "$policies/allow_"*; do
printf "$CHROOT" > "$policy"
done
fi
if [ -z "$mounted" ]; then
umount "$sec"
fi
}
# Function to prompt the user for a passphrase. Sets $passphrase.
promptNewPassphrase() {
echo_tty -n "Choose an encryption passphrase for $NAME: "
[ -t 0 ] && stty -echo
while [ -z "$passphrase" ]; do
read -r passphrase
if [ -z "$passphrase" ]; then
echo_tty ''
echo_tty -n 'You must specify a passphrase: '
continue
fi
echo_tty ''
echo_tty -n 'Please confirm your passphrase: '
read -r confirmation
if [ "$confirmation" != "$passphrase" ]; then
passphrase=''
echo_tty ''
echo_tty -n 'Passphrases do not match; try again: '
fi
confirmation=''
done
[ -t 0 ] && stty echo
echo_tty ''
}
# Mount each chroot
for NAME in "$@"; do
if ! validate_name "$NAME"; then
error 2 "Invalid chroot name '$NAME'."
fi
# Check for existence
CHROOT="$CHROOTS/$NAME"
movesrc=''
if [ -d "$CHROOT" ]; then
if [ -f "$CHROOT/.ecryptfs" -o -n "$ENCRYPT" ]; then
if [ -z "$ENCRYPT" ]; then
ENCRYPT=0
fi
# Check for non-encrypted files that we may need to move
for file in "$CHROOT/"*; do
if [ "${file#*/ECRYPTFS_FNEK_ENCRYPTED}" = "$file" ]; then
movesrc="$CHROOT"
break
fi
done
fi
elif [ -z "$CREATE" ]; then
error 1 "$CHROOT not found."
else
mkdir -p "$CHROOT"
fi
CHROOTSRC="$CHROOT"
CHROOT="$ROOT/${CHROOT#/}"
# Ensure there's a root-only folder for the bind-mounted chroot
mkdir -p -m 0700 "$CHROOT"
chown root:root "$ROOT"
chmod 700 "$ROOT"
# Extraordinarily vague usage stat; see https://crbug.com/989219
if [ -z "${CROUTON_DISABLE_STATS-}" -a -d "${METRICSDIR%/*}" ]; then
mkdir -p -m 0777 "$METRICSDIR"
chown root:root "$METRICSDIR"
chmod 777 "$METRICSDIR"
touch "$METRICSDIR/crouton-started"
fi || true
if [ -n "$PRINT" ]; then
echo "$CHROOT"
fi
# Check if we actually need to mount
if ! mountpoint -q "$CHROOT"; then
if [ -z "$ENCRYPT" ]; then
mount --bind "$CHROOTSRC" "$CHROOT"
mount -i -o "remount,$MOUNTOPTS" "$CHROOT"
mount -i -o 'remount,symfollow' "$CHROOT" 2>/dev/null || true
mount --make-private "$CHROOT"
whitelist "$CHROOT"
continue
fi
# We must be on a terminal, unless we already have a password in env.
if [ ! -t 0 -a -z "$CROUTON_PASSPHRASE$CROUTON_NEW_PASSPHRASE" ]; then
error 2 'STDIN is not a terminal; cannot request passwords.'
fi
# Ensure that there's a root password set before decrypting the chroot,
# unless the passphrase was specified via env, which isn't secure anyway
if [ ! -f '/mnt/stateful_partition/etc/devmode.passwd' ]; then
echo_tty \
'You must have a root password in Chromium OS to mount encrypted chroots.'
if [ -z "$CROUTON_PASSPHRASE$CROUTON_NEW_PASSPHRASE" ]; then
while ! chromeos-setdevpasswd; do :; done
fi
fi
# Detect the key file
if [ -z "$KEYFILE" ]; then
KEYFILE="$CHROOTSRC/.ecryptfs"
if [ -f "$KEYFILE" ]; then
header="`head -n1 "$KEYFILE"`"
if [ -n "$header" ]; then
KEYFILE="$header"
fi
fi
elif [ "${KEYFILE#/}" = "$KEYFILE" ]; then
KEYFILE="$PWD/$KEYFILE"
fi
if [ -d "$KEYFILE" -o "${KEYFILE%/}" != "$KEYFILE" ]; then
KEYFILE="${KEYFILE%/}/$NAME"
fi
if ! mkdir -p "`dirname "$KEYFILE"`"; then
error 1 "Unable to create directory for $KEYFILE"
fi
# If we just created it, choose and create the keyfile.
passphrase="${CROUTON_PASSPHRASE:-"$CROUTON_NEW_PASSPHRASE"}"
if [ ! -f "$CHROOTSRC/.ecryptfs" ]; then
if [ -e "$KEYFILE" ]; then
error 1 "Encryption key file $KEYFILE already exists. Refusing to overwrite!"
fi
promptNewPassphrase
if [ -z "$CROUTON_WEAK_RANDOM" ]; then
random="/dev/random"
echo 'Generating keys (move the mouse to generate entropy)...' 1>&2
else
random="/dev/urandom"
echo 'Generating keys from /dev/urandom...' 1>&2
fi
key="`hexdump -v -n32 -e'32/1 "%02x"' "$random"`"
fnek="`hexdump -v -n32 -e'32/1 "%02x"' "$random"`"
echo 'done' 1>&2
# Create key file
wrappedkey="`mktemp`"
wrappedfnek="`mktemp`"
addtrap "rm -f '$wrappedkey' '$wrappedfnek'"
echo -n "$key
$passphrase" | ecryptfs-wrap-passphrase "$wrappedkey" -
echo -n "$fnek
$passphrase" | ecryptfs-wrap-passphrase "$wrappedfnek" -
unset key fnek
echo | cat - "$wrappedkey" "$wrappedfnek" > "$KEYFILE"
if [ ! -f "$CHROOTSRC/.ecryptfs" ]; then
echo "$KEYFILE" > "$CHROOTSRC/.ecryptfs"
fi
elif [ ! -f "$KEYFILE" ]; then
error 1 "Unable to find encryption key file $KEYFILE"
else
echo_tty -n "Enter encryption passphrase for $NAME: "
[ -t 0 ] && stty -echo
if [ -z "$passphrase" ]; then
read -r passphrase
fi
[ -t 0 ] && stty echo
echo_tty ''
wrappedkey="`mktemp`"
wrappedfnek="`mktemp`"
addtrap "rm -f '$wrappedkey' '$wrappedfnek'"
# Extract wrapped keys from keyfile
wrappedtotal="$(($(wc -c < "$KEYFILE") - $(head -n 1 "$KEYFILE" | wc -c)))"
wrappedsize="$((wrappedtotal / 2))"
tail -c "$wrappedtotal" "$KEYFILE" | head -c "$wrappedsize" > "$wrappedkey"
tail -c "$wrappedsize" "$KEYFILE" > "$wrappedfnek"
# Change the passphrase if requested
if [ "${ENCRYPT:-0}" -ge 2 ]; then
oldpassphrase="$passphrase"
passphrase="$CROUTON_NEW_PASSPHRASE"
promptNewPassphrase
echo "Applying passphrase change" 1>&2
echo -n "$oldpassphrase
$passphrase" | ecryptfs-rewrap-passphrase "$wrappedkey" -
echo -n "$oldpassphrase
$passphrase" | ecryptfs-rewrap-passphrase "$wrappedfnek" -
echo | cat - "$wrappedkey" "$wrappedfnek" > "$KEYFILE"
unset oldpassphrase
fi
fi
# Add keys to keychain and extract
keysig="`echo -n "$passphrase" \
| ecryptfs-unwrap-passphrase "$wrappedkey" - 2>/dev/null \
| ecryptfs-add-passphrase - 2>/dev/null \
| sed -n 's/.*\[\([0-9a-zA-Z]*\)\].*/\1/p'`"
fneksig="`echo -n "$passphrase" \
| ecryptfs-unwrap-passphrase "$wrappedfnek" - 2>/dev/null \
| ecryptfs-add-passphrase - 2>/dev/null \
| sed -n 's/.*\[\([0-9a-zA-Z]*\)\].*/\1/p'`"
if [ -z "$keysig" -o -z "$fneksig" ]; then
error 1 "Failed to decrypt $NAME."
fi
# Create a new session, and link user keyring to that session,
# as required by ecryptfs.
keyctl new_session >/dev/null
keyctl link @u @s
mnt="ecryptfs_sig=$keysig,ecryptfs_fnek_sig=$fneksig"
mnt="$mnt,ecryptfs_cipher=aes,ecryptfs_key_bytes=16"
mnt="$mnt,ecryptfs_unlink_sigs,$MOUNTOPTS"
if ! mount -i -t ecryptfs -o "$mnt" "$CHROOTSRC" "$CHROOT"; then
error 1 "Failed to mount $NAME."
fi
mount -i -o 'remount,symfollow' "$CHROOT" 2>/dev/null || true
whitelist "$CHROOT"
fi
# Perform the move
if [ -z "$movesrc" ]; then
continue
fi
response=y
for file in "$movesrc/"*; do
if [ "${file#*/ECRYPTFS_FNEK_ENCRYPTED}" != "$file" ]; then
echo -n \
"About to continue encrypting the unencrypted portion of $NAME.
If this is unexpected, then it could mean that someone's trying to inject files
into your encrypted chroot, potentially allowing them to steal your data.
Please choose one of the following options:
yes -- You are sure you want to continue moving the files in. They're yours.
del -- You do not like these files and want them deleted permanently.
list -- You do not know what these files are and want to list them.
no -- You don't want to decide one way or another quite yet.
> " 1>&2
# Don't allow a response to be specified in env unless the password
# was also specified in env.
if [ -n "$CROUTON_PASSPHRASE" -a \
-n "$CROUTON_MOUNT_RESPONSE" ]; then
response="$CROUTON_MOUNT_RESPONSE"
echo "$response" 1>&2
else
read -r response
fi
break
fi
done
case "$response" in
y*|Y*) (
echo -n "Encrypting $NAME; please wait..." 1>&2
cd "$movesrc"
tmp="`mktemp -d --tmpdir=. 'ECRYPTFS_MOVE_STAGING_XXXXXX'`"
find -not -name 'ECRYPTFS_FNEK_ENCRYPTED*' \
-not -wholename './ECRYPTFS_MOVE_STAGING_*' \
-not -wholename '.' \
-not -wholename './.ecryptfs' \
-not -wholename './.crouton-targets' \
-exec mkdir -p "$tmp/{}" ';' \
-exec rmdir "$tmp/{}" ';' \
'(' -prune , -exec mv -fT '{}' "$tmp/{}" ';' ')' 1>&2
for tmp in ECRYPTFS_MOVE_STAGING_*; do
(
cd "$tmp"
find '!' '(' -type d -exec test -d "$CHROOT/{}" ';' ')' \
'(' -prune , -exec mv -fT '{}' "$CHROOT/{}" ';' ')' \
-exec echo -n . ';' 1>&2
find -depth -type d -not -wholename . \
-exec test -d "$CHROOT/{}" ';' \
-exec rmdir '{}' ';' 1>&2
)
rmdir "$tmp" 2>/dev/null || true
done
echo 'done.' 1>&2
);;
d*|D*) (
echo "Deleting unencrypted files in $NAME; please wait..." 1>&2
cd "$movesrc"
find -not -name 'ECRYPTFS_FNEK_ENCRYPTED*' \
-not -wholename '.' \
-not -wholename './.ecryptfs' \
-not -wholename './.crouton-targets' \
'(' -prune , -exec rm -rvf '{}' ';' ')' 1>&2
echo 'Done.' 1>&2
);;
l*|L*) (
echo "Listing unencrypted files in $NAME; please wait..." 1>&2
cd "$movesrc"
find -not -name 'ECRYPTFS_FNEK_ENCRYPTED*' \
-not -name 'ECRYPTFS_MOVE_STAGING_*' \
-not -wholename '.' \
-not -wholename './.ecryptfs' \
-not -wholename './.crouton-targets' \
-print -prune | cut -b2- 1>&2
echo 'Done.' 1>&2
exit 2
);;
esac
done
exit 0