1#!/usr/bin/env bash
2set -e
3
4EXITCODE=0
5
6# bits of this were adapted from lxc-checkconfig
7# see also https://github.com/lxc/lxc/blob/lxc-1.0.2/src/lxc/lxc-checkconfig.in
8
9possibleConfigs=(
10	'/proc/config.gz'
11	"/boot/config-$(uname -r)"
12	"/usr/src/linux-$(uname -r)/.config"
13	'/usr/src/linux/.config'
14)
15
16if [ $# -gt 0 ]; then
17	CONFIG="$1"
18else
19	: ${CONFIG:="${possibleConfigs[0]}"}
20fi
21
22if ! command -v zgrep &> /dev/null; then
23	zgrep() {
24		zcat "$2" | grep "$1"
25	}
26fi
27
28kernelVersion="$(uname -r)"
29kernelMajor="${kernelVersion%%.*}"
30kernelMinor="${kernelVersion#$kernelMajor.}"
31kernelMinor="${kernelMinor%%.*}"
32
33is_set() {
34	zgrep "CONFIG_$1=[y|m]" "$CONFIG" > /dev/null
35}
36is_set_in_kernel() {
37	zgrep "CONFIG_$1=y" "$CONFIG" > /dev/null
38}
39is_set_as_module() {
40	zgrep "CONFIG_$1=m" "$CONFIG" > /dev/null
41}
42
43color() {
44	local codes=()
45	if [ "$1" = 'bold' ]; then
46		codes=( "${codes[@]}" '1' )
47		shift
48	fi
49	if [ "$#" -gt 0 ]; then
50		local code=
51		case "$1" in
52			# see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
53			black) code=30 ;;
54			red) code=31 ;;
55			green) code=32 ;;
56			yellow) code=33 ;;
57			blue) code=34 ;;
58			magenta) code=35 ;;
59			cyan) code=36 ;;
60			white) code=37 ;;
61		esac
62		if [ "$code" ]; then
63			codes=( "${codes[@]}" "$code" )
64		fi
65	fi
66	local IFS=';'
67	echo -en '\033['"${codes[*]}"'m'
68}
69wrap_color() {
70	text="$1"
71	shift
72	color "$@"
73	echo -n "$text"
74	color reset
75	echo
76}
77
78wrap_good() {
79	echo "$(wrap_color "$1" white): $(wrap_color "$2" green)"
80}
81wrap_bad() {
82	echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)"
83}
84wrap_warning() {
85	wrap_color >&2 "$*" red
86}
87
88check_flag() {
89	if is_set_in_kernel "$1"; then
90		wrap_good "CONFIG_$1" 'enabled'
91	elif is_set_as_module "$1"; then
92		wrap_good "CONFIG_$1" 'enabled (as module)'
93	else
94		wrap_bad "CONFIG_$1" 'missing'
95		EXITCODE=1
96	fi
97}
98
99check_flags() {
100	for flag in "$@"; do
101		echo -n "- "; check_flag "$flag"
102	done
103}
104
105check_command() {
106	if command -v "$1" >/dev/null 2>&1; then
107		wrap_good "$1 command" 'available'
108	else
109		wrap_bad "$1 command" 'missing'
110		EXITCODE=1
111	fi
112}
113
114check_device() {
115	if [ -c "$1" ]; then
116		wrap_good "$1" 'present'
117	else
118		wrap_bad "$1" 'missing'
119		EXITCODE=1
120	fi
121}
122
123check_distro_userns() {
124	source /etc/os-release 2>/dev/null || /bin/true
125	if [[ "${ID}" =~ ^(centos|rhel)$ && "${VERSION_ID}" =~ ^7 ]]; then
126		# this is a CentOS7 or RHEL7 system
127		grep -q "user_namespace.enable=1" /proc/cmdline || {
128			# no user namespace support enabled
129			wrap_bad "  (RHEL7/CentOS7" "User namespaces disabled; add 'user_namespace.enable=1' to boot command line)"
130			EXITCODE=1
131		}
132	fi
133}
134
135if [ ! -e "$CONFIG" ]; then
136	wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config ..."
137	for tryConfig in "${possibleConfigs[@]}"; do
138		if [ -e "$tryConfig" ]; then
139			CONFIG="$tryConfig"
140			break
141		fi
142	done
143	if [ ! -e "$CONFIG" ]; then
144		wrap_warning "error: cannot find kernel config"
145		wrap_warning "  try running this script again, specifying the kernel config:"
146		wrap_warning "    CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config"
147		exit 1
148	fi
149fi
150
151wrap_color "info: reading kernel config from $CONFIG ..." white
152echo
153
154echo 'Generally Necessary:'
155
156echo -n '- '
157cgroupSubsystemDir="$(awk '/[, ](cpu|cpuacct|cpuset|devices|freezer|memory)[, ]/ && $3 == "cgroup" { print $2 }' /proc/mounts | head -n1)"
158cgroupDir="$(dirname "$cgroupSubsystemDir")"
159if [ -d "$cgroupDir/cpu" -o -d "$cgroupDir/cpuacct" -o -d "$cgroupDir/cpuset" -o -d "$cgroupDir/devices" -o -d "$cgroupDir/freezer" -o -d "$cgroupDir/memory" ]; then
160	echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]"
161else
162	if [ "$cgroupSubsystemDir" ]; then
163		echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]"
164	else
165		echo "$(wrap_bad 'cgroup hierarchy' 'nonexistent??')"
166	fi
167	EXITCODE=1
168	echo "    $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)"
169fi
170
171if [ "$(cat /sys/module/apparmor/parameters/enabled 2>/dev/null)" = 'Y' ]; then
172	echo -n '- '
173	if command -v apparmor_parser &> /dev/null; then
174		echo "$(wrap_good 'apparmor' 'enabled and tools installed')"
175	else
176		echo "$(wrap_bad 'apparmor' 'enabled, but apparmor_parser missing')"
177		echo -n '    '
178		if command -v apt-get &> /dev/null; then
179			echo "$(wrap_color '(use "apt-get install apparmor" to fix this)')"
180		elif command -v yum &> /dev/null; then
181			echo "$(wrap_color '(your best bet is "yum install apparmor-parser")')"
182		else
183			echo "$(wrap_color '(look for an "apparmor" package for your distribution)')"
184		fi
185		EXITCODE=1
186	fi
187fi
188
189flags=(
190	NAMESPACES {NET,PID,IPC,UTS}_NS
191	CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED CPUSETS MEMCG
192	KEYS
193	VETH BRIDGE BRIDGE_NETFILTER
194	NF_NAT_IPV4 IP_NF_FILTER IP_NF_TARGET_MASQUERADE
195	NETFILTER_XT_MATCH_{ADDRTYPE,CONNTRACK,IPVS}
196	IP_NF_NAT NF_NAT NF_NAT_NEEDED
197
198	# required for bind-mounting /dev/mqueue into containers
199	POSIX_MQUEUE
200)
201check_flags "${flags[@]}"
202if [ "$kernelMajor" -lt 4 ] || [ "$kernelMajor" -eq 4 -a "$kernelMinor" -lt 8 ]; then
203        check_flags DEVPTS_MULTIPLE_INSTANCES
204fi
205
206echo
207
208echo 'Optional Features:'
209{
210	check_flags USER_NS
211	check_distro_userns
212}
213{
214	check_flags SECCOMP
215}
216{
217	check_flags CGROUP_PIDS
218}
219{
220	CODE=${EXITCODE}
221	check_flags MEMCG_SWAP MEMCG_SWAP_ENABLED
222	if [ -e /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes ]; then
223		echo "    $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)"
224		EXITCODE=${CODE}
225	elif is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then
226		echo "    $(wrap_color '(cgroup swap accounting is currently not enabled, you can enable it by setting boot option "swapaccount=1")' bold black)"
227	fi
228}
229{
230	if is_set LEGACY_VSYSCALL_NATIVE; then
231		echo -n "- "; wrap_bad "CONFIG_LEGACY_VSYSCALL_NATIVE" 'enabled'
232		echo "    $(wrap_color '(dangerous, provides an ASLR-bypassing target with usable ROP gadgets.)' bold black)"
233	elif is_set LEGACY_VSYSCALL_EMULATE; then
234		echo -n "- "; wrap_good "CONFIG_LEGACY_VSYSCALL_EMULATE" 'enabled'
235	elif is_set LEGACY_VSYSCALL_NONE; then
236		echo -n "- "; wrap_bad "CONFIG_LEGACY_VSYSCALL_NONE" 'enabled'
237		echo "    $(wrap_color '(containers using eglibc <= 2.13 will not work. Switch to' bold black)"
238		echo "    $(wrap_color ' "CONFIG_VSYSCALL_[NATIVE|EMULATE]" or use "vsyscall=[native|emulate]"' bold black)"
239		echo "    $(wrap_color ' on kernel command line. Note that this will disable ASLR for the,' bold black)"
240		echo "    $(wrap_color ' VDSO which may assist in exploiting security vulnerabilities.)' bold black)"
241	# else Older kernels (prior to 3dc33bd30f3e, released in v4.40-rc1) do
242	#      not have these LEGACY_VSYSCALL options and are effectively
243	#      LEGACY_VSYSCALL_EMULATE. Even older kernels are presumably
244	#      effectively LEGACY_VSYSCALL_NATIVE.
245	fi
246}
247
248if [ "$kernelMajor" -lt 4 ] || [ "$kernelMajor" -eq 4 -a "$kernelMinor" -le 5 ]; then
249	check_flags MEMCG_KMEM
250fi
251
252if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 18 ]; then
253	check_flags RESOURCE_COUNTERS
254fi
255
256if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 13 ]; then
257	netprio=NETPRIO_CGROUP
258else
259	netprio=CGROUP_NET_PRIO
260fi
261
262flags=(
263	BLK_CGROUP BLK_DEV_THROTTLING IOSCHED_CFQ CFQ_GROUP_IOSCHED
264	CGROUP_PERF
265	CGROUP_HUGETLB
266	NET_CLS_CGROUP $netprio
267	CFS_BANDWIDTH FAIR_GROUP_SCHED RT_GROUP_SCHED
268	IP_VS
269	IP_VS_NFCT
270 	IP_VS_RR
271)
272check_flags "${flags[@]}"
273
274if ! is_set EXT4_USE_FOR_EXT2; then
275	check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY
276	if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then
277		echo "    $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)"
278	fi
279fi
280
281check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY
282if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then
283	if is_set EXT4_USE_FOR_EXT2; then
284		echo "    $(wrap_color 'enable these ext4 configs if you are using ext3 or ext4 as backing filesystem' bold black)"
285	else
286		echo "    $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)"
287	fi
288fi
289
290echo '- Network Drivers:'
291echo '  - "'$(wrap_color 'overlay' blue)'":'
292check_flags VXLAN | sed 's/^/    /'
293echo '      Optional (for encrypted networks):'
294check_flags CRYPTO CRYPTO_AEAD CRYPTO_GCM CRYPTO_SEQIV CRYPTO_GHASH \
295            XFRM XFRM_USER XFRM_ALGO INET_ESP INET_XFRM_MODE_TRANSPORT | sed 's/^/      /'
296echo '  - "'$(wrap_color 'ipvlan' blue)'":'
297check_flags IPVLAN | sed 's/^/    /'
298echo '  - "'$(wrap_color 'macvlan' blue)'":'
299check_flags MACVLAN DUMMY | sed 's/^/    /'
300echo '  - "'$(wrap_color 'ftp,tftp client in container' blue)'":'
301check_flags NF_NAT_FTP NF_CONNTRACK_FTP NF_NAT_TFTP NF_CONNTRACK_TFTP | sed 's/^/    /'
302
303# only fail if no storage drivers available
304CODE=${EXITCODE}
305EXITCODE=0
306STORAGE=1
307
308echo '- Storage Drivers:'
309echo '  - "'$(wrap_color 'aufs' blue)'":'
310check_flags AUFS_FS | sed 's/^/    /'
311if ! is_set AUFS_FS && grep -q aufs /proc/filesystems; then
312	echo "      $(wrap_color '(note that some kernels include AUFS patches but not the AUFS_FS flag)' bold black)"
313fi
314[ "$EXITCODE" = 0 ] && STORAGE=0
315EXITCODE=0
316
317echo '  - "'$(wrap_color 'btrfs' blue)'":'
318check_flags BTRFS_FS | sed 's/^/    /'
319check_flags BTRFS_FS_POSIX_ACL | sed 's/^/    /'
320[ "$EXITCODE" = 0 ] && STORAGE=0
321EXITCODE=0
322
323echo '  - "'$(wrap_color 'devicemapper' blue)'":'
324check_flags BLK_DEV_DM DM_THIN_PROVISIONING | sed 's/^/    /'
325[ "$EXITCODE" = 0 ] && STORAGE=0
326EXITCODE=0
327
328echo '  - "'$(wrap_color 'overlay' blue)'":'
329check_flags OVERLAY_FS | sed 's/^/    /'
330[ "$EXITCODE" = 0 ] && STORAGE=0
331EXITCODE=0
332
333echo '  - "'$(wrap_color 'zfs' blue)'":'
334echo -n "    - "; check_device /dev/zfs
335echo -n "    - "; check_command zfs
336echo -n "    - "; check_command zpool
337[ "$EXITCODE" = 0 ] && STORAGE=0
338EXITCODE=0
339
340EXITCODE=$CODE
341[ "$STORAGE" = 1 ] && EXITCODE=1
342
343echo
344
345check_limit_over()
346{
347	if [ $(cat "$1") -le "$2" ]; then
348		wrap_bad "- $1" "$(cat $1)"
349		wrap_color "    This should be set to at least $2, for example set: sysctl -w kernel/keys/root_maxkeys=1000000" bold black
350		EXITCODE=1
351	else
352		wrap_good "- $1" "$(cat $1)"
353	fi
354}
355
356echo 'Limits:'
357check_limit_over /proc/sys/kernel/keys/root_maxkeys 10000
358echo
359
360exit $EXITCODE
361