github.com/kunnos/engine@v1.13.1/contrib/check-config.sh (about) 1 #!/usr/bin/env bash 2 set -e 3 4 EXITCODE=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 9 possibleConfigs=( 10 '/proc/config.gz' 11 "/boot/config-$(uname -r)" 12 "/usr/src/linux-$(uname -r)/.config" 13 '/usr/src/linux/.config' 14 ) 15 16 if [ $# -gt 0 ]; then 17 CONFIG="$1" 18 else 19 : ${CONFIG:="${possibleConfigs[0]}"} 20 fi 21 22 if ! command -v zgrep &> /dev/null; then 23 zgrep() { 24 zcat "$2" | grep "$1" 25 } 26 fi 27 28 kernelVersion="$(uname -r)" 29 kernelMajor="${kernelVersion%%.*}" 30 kernelMinor="${kernelVersion#$kernelMajor.}" 31 kernelMinor="${kernelMinor%%.*}" 32 33 is_set() { 34 zgrep "CONFIG_$1=[y|m]" "$CONFIG" > /dev/null 35 } 36 is_set_in_kernel() { 37 zgrep "CONFIG_$1=y" "$CONFIG" > /dev/null 38 } 39 is_set_as_module() { 40 zgrep "CONFIG_$1=m" "$CONFIG" > /dev/null 41 } 42 43 color() { 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 } 69 wrap_color() { 70 text="$1" 71 shift 72 color "$@" 73 echo -n "$text" 74 color reset 75 echo 76 } 77 78 wrap_good() { 79 echo "$(wrap_color "$1" white): $(wrap_color "$2" green)" 80 } 81 wrap_bad() { 82 echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)" 83 } 84 wrap_warning() { 85 wrap_color >&2 "$*" red 86 } 87 88 check_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 99 check_flags() { 100 for flag in "$@"; do 101 echo -n "- "; check_flag "$flag" 102 done 103 } 104 105 check_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 114 check_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 123 check_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 135 if [ ! -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 149 fi 150 151 wrap_color "info: reading kernel config from $CONFIG ..." white 152 echo 153 154 echo 'Generally Necessary:' 155 156 echo -n '- ' 157 cgroupSubsystemDir="$(awk '/[, ](cpu|cpuacct|cpuset|devices|freezer|memory)[, ]/ && $3 == "cgroup" { print $2 }' /proc/mounts | head -n1)" 158 cgroupDir="$(dirname "$cgroupSubsystemDir")" 159 if [ -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]" 161 else 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)" 169 fi 170 171 if [ "$(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 187 fi 188 189 flags=( 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 ) 201 check_flags "${flags[@]}" 202 if [ "$kernelMajor" -lt 4 ] || [ "$kernelMajor" -eq 4 -a "$kernelMinor" -lt 8 ]; then 203 check_flags DEVPTS_MULTIPLE_INSTANCES 204 fi 205 206 echo 207 208 echo '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 check_flags MEMCG_SWAP MEMCG_SWAP_ENABLED 221 if is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then 222 echo " $(wrap_color '(note that cgroup swap accounting is not enabled in your kernel config, you can enable it by setting boot option "swapaccount=1")' bold black)" 223 fi 224 } 225 { 226 if is_set LEGACY_VSYSCALL_NATIVE; then 227 echo -n "- "; wrap_bad "CONFIG_LEGACY_VSYSCALL_NATIVE" 'enabled' 228 echo " $(wrap_color '(dangerous, provides an ASLR-bypassing target with usable ROP gadgets.)' bold black)" 229 elif is_set LEGACY_VSYSCALL_EMULATE; then 230 echo -n "- "; wrap_good "CONFIG_LEGACY_VSYSCALL_EMULATE" 'enabled' 231 elif is_set LEGACY_VSYSCALL_NONE; then 232 echo -n "- "; wrap_bad "CONFIG_LEGACY_VSYSCALL_NONE" 'enabled' 233 echo " $(wrap_color '(containers using eglibc <= 2.13 will not work. Switch to' bold black)" 234 echo " $(wrap_color ' "CONFIG_VSYSCALL_[NATIVE|EMULATE]" or use "vsyscall=[native|emulate]"' bold black)" 235 echo " $(wrap_color ' on kernel command line. Note that this will disable ASLR for the,' bold black)" 236 echo " $(wrap_color ' VDSO which may assist in exploiting security vulnerabilities.)' bold black)" 237 # else Older kernels (prior to 3dc33bd30f3e, released in v4.40-rc1) do 238 # not have these LEGACY_VSYSCALL options and are effectively 239 # LEGACY_VSYSCALL_EMULATE. Even older kernels are presumably 240 # effectively LEGACY_VSYSCALL_NATIVE. 241 fi 242 } 243 244 if [ "$kernelMajor" -lt 4 ] || [ "$kernelMajor" -eq 4 -a "$kernelMinor" -le 5 ]; then 245 check_flags MEMCG_KMEM 246 fi 247 248 if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 18 ]; then 249 check_flags RESOURCE_COUNTERS 250 fi 251 252 if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 13 ]; then 253 netprio=NETPRIO_CGROUP 254 else 255 netprio=CGROUP_NET_PRIO 256 fi 257 258 flags=( 259 BLK_CGROUP BLK_DEV_THROTTLING IOSCHED_CFQ CFQ_GROUP_IOSCHED 260 CGROUP_PERF 261 CGROUP_HUGETLB 262 NET_CLS_CGROUP $netprio 263 CFS_BANDWIDTH FAIR_GROUP_SCHED RT_GROUP_SCHED 264 IP_VS 265 IP_VS_NFCT 266 IP_VS_RR 267 ) 268 check_flags "${flags[@]}" 269 270 if ! is_set EXT4_USE_FOR_EXT2; then 271 check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY 272 if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then 273 echo " $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)" 274 fi 275 fi 276 277 check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY 278 if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then 279 if is_set EXT4_USE_FOR_EXT2; then 280 echo " $(wrap_color 'enable these ext4 configs if you are using ext3 or ext4 as backing filesystem' bold black)" 281 else 282 echo " $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)" 283 fi 284 fi 285 286 echo '- Network Drivers:' 287 echo ' - "'$(wrap_color 'overlay' blue)'":' 288 check_flags VXLAN | sed 's/^/ /' 289 echo ' Optional (for encrypted networks):' 290 check_flags CRYPTO CRYPTO_AEAD CRYPTO_GCM CRYPTO_SEQIV CRYPTO_GHASH \ 291 XFRM XFRM_USER XFRM_ALGO INET_ESP INET_XFRM_MODE_TRANSPORT | sed 's/^/ /' 292 echo ' - "'$(wrap_color 'ipvlan' blue)'":' 293 check_flags IPVLAN | sed 's/^/ /' 294 echo ' - "'$(wrap_color 'macvlan' blue)'":' 295 check_flags MACVLAN DUMMY | sed 's/^/ /' 296 297 # only fail if no storage drivers available 298 CODE=${EXITCODE} 299 EXITCODE=0 300 STORAGE=1 301 302 echo '- Storage Drivers:' 303 echo ' - "'$(wrap_color 'aufs' blue)'":' 304 check_flags AUFS_FS | sed 's/^/ /' 305 if ! is_set AUFS_FS && grep -q aufs /proc/filesystems; then 306 echo " $(wrap_color '(note that some kernels include AUFS patches but not the AUFS_FS flag)' bold black)" 307 fi 308 [ "$EXITCODE" = 0 ] && STORAGE=0 309 EXITCODE=0 310 311 echo ' - "'$(wrap_color 'btrfs' blue)'":' 312 check_flags BTRFS_FS | sed 's/^/ /' 313 check_flags BTRFS_FS_POSIX_ACL | sed 's/^/ /' 314 [ "$EXITCODE" = 0 ] && STORAGE=0 315 EXITCODE=0 316 317 echo ' - "'$(wrap_color 'devicemapper' blue)'":' 318 check_flags BLK_DEV_DM DM_THIN_PROVISIONING | sed 's/^/ /' 319 [ "$EXITCODE" = 0 ] && STORAGE=0 320 EXITCODE=0 321 322 echo ' - "'$(wrap_color 'overlay' blue)'":' 323 check_flags OVERLAY_FS | sed 's/^/ /' 324 [ "$EXITCODE" = 0 ] && STORAGE=0 325 EXITCODE=0 326 327 echo ' - "'$(wrap_color 'zfs' blue)'":' 328 echo -n " - "; check_device /dev/zfs 329 echo -n " - "; check_command zfs 330 echo -n " - "; check_command zpool 331 [ "$EXITCODE" = 0 ] && STORAGE=0 332 EXITCODE=0 333 334 EXITCODE=$CODE 335 [ "$STORAGE" = 1 ] && EXITCODE=1 336 337 echo 338 339 check_limit_over() 340 { 341 if [ $(cat "$1") -le "$2" ]; then 342 wrap_bad "- $1" "$(cat $1)" 343 wrap_color " This should be set to at least $2, for example set: sysctl -w kernel/keys/root_maxkeys=1000000" bold black 344 EXITCODE=1 345 else 346 wrap_good "- $1" "$(cat $1)" 347 fi 348 } 349 350 echo 'Limits:' 351 check_limit_over /proc/sys/kernel/keys/root_maxkeys 10000 352 echo 353 354 exit $EXITCODE