-
Notifications
You must be signed in to change notification settings - Fork 51
/
remount-secure
executable file
·388 lines (331 loc) · 9.83 KB
/
remount-secure
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
385
386
387
388
#!/bin/bash
## Copyright (C) 2019 - 2024 ENCRYPTED SUPPORT LP <[email protected]>
## See the file COPYING for copying conditions.
## features:
## - nodev,nosuid where appropriate
## - optional noexec for most except /home
## - optional noexec for all including /home
## - idempotent (script can be safely re-run)
## - can be run from:
## - systemd
## - dracut
## - manually from command line
## - can safely handle non-existing folders
## - error handling
## - log output:
## - shows each and every command executed
## - shows old mount options prior running remount-secure
## - shows new mount options after running remount-secure
## noexec in /tmp and/or /home can break some malware but also legitimate
## applications.
## https://www.kicksecure.com/wiki/Noexec
## https://www.kicksecure.com/wiki/Dev/remount-secure
## https://forums.whonix.org/t/re-mount-home-and-other-with-noexec-and-nosuid-among-other-useful-mount-options-for-better-security/7707
#set -x
set -e
set -o pipefail
set -o nounset
init() {
if test -o xtrace ; then
output_command=true
else
output_command=echo
fi
$output_command "$0: INFO: START"
## dracut does not have id. Saving space in initial ramdisk.
if command -v id &>/dev/null ; then
if [ "$(id -u)" != "0" ]; then
$output_command "ERROR: must be run as root! sudo $0"
exit 1
fi
fi
mkdir --parents "/run/remount-secure"
exit_code=0
## dracut sets NEWROOT=/sysroot
[[ -v NEWROOT ]] || NEWROOT=""
if [ "$NEWROOT" = "" ]; then
$output_command "INFO: dracut detected: no"
else
$output_command "INFO: dracut detected: yes - NEWROOT: '$NEWROOT'"
fi
## Debugging.
#echo "ls -la /root/"
#ls -la / || true
#echo "ls -la /sysroot/"
#ls -la /sysroot/ || true
#echo "env"
#env || true
}
parse_options() {
## Thanks to:
## https://mywiki.wooledge.org/BashFAQ/035
while :
do
case ${1:-} in
0)
$output_command "WARNING: Not using remount-secure."
exit 0
shift
;;
1)
$output_command "INFO: level 1/3 (low)"
most_noexec_maybe=""
home_noexec_maybe=""
parsed=true
shift
;;
2)
$output_command "INFO: level 2/3 (medium)"
most_noexec_maybe=",noexec"
home_noexec_maybe=""
parsed=true
shift
;;
3)
$output_command "INFO: level 3/3 (high)"
most_noexec_maybe=",noexec"
home_noexec_maybe=",noexec"
parsed=true
shift
;;
--force)
$output_command "INFO: --force"
option_force=true
shift
;;
--)
shift
break
;;
-*)
echo "ERROR: unknown option: $1" >&2
exit 1
;;
*)
break
;;
esac
done
[[ -v option_force ]] || option_force=""
[[ -v parsed ]] || parsed=false
[[ -v home_noexec_maybe ]] || home_noexec_maybe=""
[[ -v most_noexec_maybe ]] || most_noexec_maybe=""
$output_command "INFO: using nosuid,nodev: yes"
if [ "$home_noexec_maybe" = "" ]; then
$output_command "INFO: using noexec for all: no"
else
$output_command "INFO: using noexec for all: yes"
return 0
fi
if [ "$most_noexec_maybe" = "" ]; then
$output_command "INFO: using noexec for most: no"
else
$output_command "INFO: using noexec for most (not all): yes"
return 0
fi
if [ "$parsed" = "true" ]; then
return 0
fi
$output_command "ERROR: syntax error. use either:
$0 0
$0 1
$0 2
$0 3"
exit 1
}
preparation() {
## Debugging.
#$output_command "INFO: 'findmnt --list' output at the START."
#$output_command "$(findmnt --list)"
#$output_command ""
true
}
remount_secure() {
$output_command ""
## ${FUNCNAME[1]} is the name of the calling function. I.e. the function
## which called this function.
status_file_name="${FUNCNAME[1]}"
## example status_file_name:
## _home
status_file_full_path="/run/remount-secure/${status_file_name}"
## example status_file_full_path:
## /run/remount-secure/_home
old_mount_options="$(findmnt --noheadings --output options -- "$mount_folder")" || true
## example old_mount_options:
## rw,nosuid,nodev,relatime,discard
$output_command "INFO: '$mount_folder' old_mount_options: '$old_mount_options'"
if echo "$old_mount_options" | grep --quiet "$intended_mount_options" ; then
$output_command "INFO: '$mount_folder' has already intended mount options. ('$intended_mount_options')"
return 0
fi
## When this package is upgraded, the systemd unit will run again.
## If the user meanwhile manually relaxed mount options, this should not be undone.
if [ ! "$option_force" == "true" ]; then
if [ -e "$status_file_full_path" ]; then
$output_command "INFO: '$mount_folder' already remounted earlier. Not remounting again. Use --force if this is what you want."
return 0
fi
fi
if ! test -d "$mount_folder" ; then
## For example /boot/efi does not always exist on all systems.
$output_command "INFO: '$mount_folder' folder exists: no"
return 0
fi
$output_command "INFO: '$mount_folder' folder exists: yes"
if findmnt --noheadings "$mount_folder" >/dev/null ; then
$output_command "INFO: '$mount_folder' already mounted, therefore using remount."
$output_command INFO: Executing: mount --make-private --options "remount,${intended_mount_options}" "$mount_folder"
mount --make-private --options "remount,${intended_mount_options}" "$mount_folder" || exit_code=100
else
$output_command "INFO: '$mount_folder' not yet mounted, therefore using mount bind."
$output_command INFO: Executing: mount --make-private --options "$intended_mount_options" --bind "$mount_folder" "$mount_folder"
mount --make-private --options "$intended_mount_options" --bind "$mount_folder" "$mount_folder" || exit_code=101
fi
new_mount_options="$(findmnt --noheadings --output options -- "$mount_folder")" || true
$output_command "INFO: '$mount_folder' new_mount_options: '$new_mount_options'"
touch "$status_file_full_path"
}
_boot() {
mount_folder="$NEWROOT/boot"
## https://lists.freedesktop.org/archives/systemd-devel/2015-February/028456.html
intended_mount_options="nosuid,nodev,noexec"
remount_secure
}
_boot_efi() {
## TODO: new, test
mount_folder="$NEWROOT/boot/efi"
intended_mount_options="nosuid,nodev,noexec"
remount_secure
}
_run() {
mount_folder="/run"
## https://lists.freedesktop.org/archives/systemd-devel/2015-February/028456.html
intended_mount_options="nosuid,nodev${most_noexec_maybe}"
remount_secure
}
_dev() {
mount_folder="/dev"
## /dev should be nosuid,noexec as per:
## https://bugs.launchpad.net/ubuntu/+source/systemd/+bug/1991975
intended_mount_options="nosuid,noexec"
remount_secure
}
_dev_shm() {
mount_folder="/dev/shm"
intended_mount_options="nosuid,nodev${most_noexec_maybe}"
remount_secure
}
_sys() {
## TODO: new, test
mount_folder="/sys"
intended_mount_options="nosuid,nodev,noexec"
remount_secure
}
_tmp() {
mount_folder="$NEWROOT/tmp"
intended_mount_options="nosuid,nodev${most_noexec_maybe}"
remount_secure
}
_var_tmp() {
mount_folder="$NEWROOT/var/tmp"
intended_mount_options="nosuid,nodev${most_noexec_maybe}"
remount_secure
}
_var_log() {
mount_folder="$NEWROOT/var/log"
intended_mount_options="nosuid,nodev,noexec"
remount_secure
}
_var() {
mount_folder="$NEWROOT/var"
## noexec: Not possible. Reason:
## Debian stores executable maintainer scripts in /var/lib/dpkg/info folder.
intended_mount_options="nosuid,nodev"
remount_secure
}
_usr() {
## TODO: new, test
mount_folder="$NEWROOT/usr"
intended_mount_options="nodev"
remount_secure
}
_home() {
mount_folder="$NEWROOT/home"
intended_mount_options="nosuid,nodev${home_noexec_maybe}"
remount_secure
}
_root() {
## TODO: new, test
mount_folder="$NEWROOT/root"
intended_mount_options="nosuid,nodev${home_noexec_maybe}"
remount_secure
}
_srv() {
## TODO: new, test
mount_folder="$NEWROOT/srv"
intended_mount_options="nosuid,nodev${most_noexec_maybe}"
remount_secure
}
_media() {
## TODO: new, test
mount_folder="$NEWROOT/media"
intended_mount_options="nosuid,nodev${most_noexec_maybe}"
remount_secure
}
_mnt() {
## TODO: new, test
mount_folder="$NEWROOT/mnt"
intended_mount_options="nosuid,nodev${most_noexec_maybe}"
remount_secure
}
_opt() {
## TODO: new, test
mount_folder="$NEWROOT/opt"
## Allow /opt exec as usually optional binaries are placed there such as Firefox
## when manually installed from tarball.
intended_mount_options="nosuid,nodev"
remount_secure
}
_etc() {
## TODO: new, test
## /etc cannot be noexec because various executables are there. To find, run:
## sudo find /etc -executable
mount_folder="$NEWROOT/etc"
intended_mount_options="nosuid,nodev"
remount_secure
}
end() {
## Debugging.
#$output_command "INFO: 'findmnt --list' output at the END."
#$output_command "$(findmnt --list)"
$output_command ""
$output_command "INFO: exit_code: $exit_code"
$output_command "$0: INFO: END"
exit $exit_code
}
main() {
init
parse_options "$@"
preparation
_boot
_boot_efi
_run
_dev
_dev_shm
_tmp
_var_tmp
_var_log
_var
_usr
_home
_root
_srv
_media
_mnt
_opt
_etc
end
}
## TODO: see also hidepid /usr/lib/systemd/system/proc-hidepid.service
#mount --options defaults,nosuid,nodev,noexec,remount,subset=pid /proc
main "$@"