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