github.com/rawahars/moby@v24.0.4+incompatible/contrib/check-config.sh (about)

     1  #!/usr/bin/env sh
     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:=/proc/config.gz}"
    20  fi
    21  
    22  if ! command -v zgrep > /dev/null 2>&1; 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  	codes=
    45  	if [ "$1" = 'bold' ]; then
    46  		codes='1'
    47  		shift
    48  	fi
    49  	if [ "$#" -gt 0 ]; then
    50  		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:+$codes;}$code"
    64  		fi
    65  	fi
    66  	printf '\033[%sm' "$codes"
    67  }
    68  wrap_color() {
    69  	text="$1"
    70  	shift
    71  	color "$@"
    72  	printf '%s' "$text"
    73  	color reset
    74  	echo
    75  }
    76  
    77  wrap_good() {
    78  	echo "$(wrap_color "$1" white): $(wrap_color "$2" green)"
    79  }
    80  wrap_bad() {
    81  	echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)"
    82  }
    83  wrap_warning() {
    84  	wrap_color >&2 "$*" red
    85  }
    86  
    87  check_flag() {
    88  	if is_set_in_kernel "$1"; then
    89  		wrap_good "CONFIG_$1" 'enabled'
    90  	elif is_set_as_module "$1"; then
    91  		wrap_good "CONFIG_$1" 'enabled (as module)'
    92  	else
    93  		wrap_bad "CONFIG_$1" 'missing'
    94  		EXITCODE=1
    95  	fi
    96  }
    97  
    98  check_flags() {
    99  	for flag in "$@"; do
   100  		printf -- '- '
   101  		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  	if [ ! -e /etc/os-release ]; then
   125  		return
   126  	fi
   127  	. /etc/os-release 2> /dev/null || /bin/true
   128  	case "$ID" in
   129  		centos | rhel)
   130  			case "$VERSION_ID" in
   131  				7*)
   132  					# this is a CentOS7 or RHEL7 system
   133  					grep -q 'user_namespace.enable=1' /proc/cmdline || {
   134  						# no user namespace support enabled
   135  						wrap_bad "  (RHEL7/CentOS7" "User namespaces disabled; add 'user_namespace.enable=1' to boot command line)"
   136  						EXITCODE=1
   137  					}
   138  					;;
   139  			esac
   140  			;;
   141  	esac
   142  }
   143  
   144  if [ ! -e "$CONFIG" ]; then
   145  	wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config ..."
   146  	for tryConfig in $possibleConfigs; do
   147  		if [ -e "$tryConfig" ]; then
   148  			CONFIG="$tryConfig"
   149  			break
   150  		fi
   151  	done
   152  	if [ ! -e "$CONFIG" ]; then
   153  		wrap_warning "error: cannot find kernel config"
   154  		wrap_warning "  try running this script again, specifying the kernel config:"
   155  		wrap_warning "    CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config"
   156  		exit 1
   157  	fi
   158  fi
   159  
   160  wrap_color "info: reading kernel config from $CONFIG ..." white
   161  echo
   162  
   163  echo 'Generally Necessary:'
   164  
   165  printf -- '- '
   166  if [ "$(stat -f -c %t /sys/fs/cgroup 2> /dev/null)" = '63677270' ]; then
   167  	wrap_good 'cgroup hierarchy' 'cgroupv2'
   168  	cgroupv2ControllerFile='/sys/fs/cgroup/cgroup.controllers'
   169  	if [ -f "$cgroupv2ControllerFile" ]; then
   170  		echo '  Controllers:'
   171  		for controller in cpu cpuset io memory pids; do
   172  			if grep -qE '(^| )'"$controller"'($| )' "$cgroupv2ControllerFile"; then
   173  				echo "  - $(wrap_good "$controller" 'available')"
   174  			else
   175  				echo "  - $(wrap_bad "$controller" 'missing')"
   176  			fi
   177  		done
   178  	else
   179  		wrap_bad "$cgroupv2ControllerFile" 'nonexistent??'
   180  	fi
   181  	# TODO find an efficient way to check if cgroup.freeze exists in subdir
   182  else
   183  	cgroupSubsystemDir="$(sed -rne '/^[^ ]+ ([^ ]+) cgroup ([^ ]*,)?(cpu|cpuacct|cpuset|devices|freezer|memory)[, ].*$/ { s//\1/p; q }' /proc/mounts)"
   184  	cgroupDir="$(dirname "$cgroupSubsystemDir")"
   185  	if [ -d "$cgroupDir/cpu" ] || [ -d "$cgroupDir/cpuacct" ] || [ -d "$cgroupDir/cpuset" ] || [ -d "$cgroupDir/devices" ] || [ -d "$cgroupDir/freezer" ] || [ -d "$cgroupDir/memory" ]; then
   186  		echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]"
   187  	else
   188  		if [ "$cgroupSubsystemDir" ]; then
   189  			echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]"
   190  		else
   191  			wrap_bad 'cgroup hierarchy' 'nonexistent??'
   192  		fi
   193  		EXITCODE=1
   194  		echo "    $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)"
   195  	fi
   196  fi
   197  
   198  if [ "$(cat /sys/module/apparmor/parameters/enabled 2> /dev/null)" = 'Y' ]; then
   199  	printf -- '- '
   200  	if command -v apparmor_parser > /dev/null 2>&1; then
   201  		wrap_good 'apparmor' 'enabled and tools installed'
   202  	else
   203  		wrap_bad 'apparmor' 'enabled, but apparmor_parser missing'
   204  		printf '    '
   205  		if command -v apt-get > /dev/null 2>&1; then
   206  			wrap_color '(use "apt-get install apparmor" to fix this)'
   207  		elif command -v yum > /dev/null 2>&1; then
   208  			wrap_color '(your best bet is "yum install apparmor-parser")'
   209  		else
   210  			wrap_color '(look for an "apparmor" package for your distribution)'
   211  		fi
   212  		EXITCODE=1
   213  	fi
   214  fi
   215  
   216  check_flags \
   217  	NAMESPACES NET_NS PID_NS IPC_NS UTS_NS \
   218  	CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED CPUSETS MEMCG \
   219  	KEYS \
   220  	VETH BRIDGE BRIDGE_NETFILTER \
   221  	IP_NF_FILTER IP_NF_TARGET_MASQUERADE \
   222  	NETFILTER_XT_MATCH_ADDRTYPE \
   223  	NETFILTER_XT_MATCH_CONNTRACK \
   224  	NETFILTER_XT_MATCH_IPVS \
   225  	NETFILTER_XT_MARK \
   226  	IP_NF_NAT NF_NAT \
   227  	POSIX_MQUEUE
   228  # (POSIX_MQUEUE is required for bind-mounting /dev/mqueue into containers)
   229  
   230  if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -lt 8 ]); then
   231  	check_flags DEVPTS_MULTIPLE_INSTANCES
   232  fi
   233  
   234  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 1 ]; then
   235  	check_flags NF_NAT_IPV4
   236  fi
   237  
   238  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 2 ]; then
   239  	check_flags NF_NAT_NEEDED
   240  fi
   241  # check availability of BPF_CGROUP_DEVICE support
   242  if [ "$kernelMajor" -ge 5 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -ge 15 ]); then
   243  	check_flags CGROUP_BPF
   244  fi
   245  
   246  echo
   247  
   248  echo 'Optional Features:'
   249  {
   250  	check_flags USER_NS
   251  	check_distro_userns
   252  }
   253  {
   254  	check_flags SECCOMP
   255  	check_flags SECCOMP_FILTER
   256  }
   257  {
   258  	check_flags CGROUP_PIDS
   259  }
   260  {
   261  	check_flags MEMCG_SWAP
   262  	# Kernel v5.8+ removes MEMCG_SWAP_ENABLED.
   263  	if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 8 ]; then
   264  		CODE=${EXITCODE}
   265  		check_flags MEMCG_SWAP_ENABLED
   266  		# FIXME this check is cgroupv1-specific
   267  		if [ -e /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes ]; then
   268  			echo "    $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)"
   269  			EXITCODE=${CODE}
   270  		elif is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then
   271  			echo "    $(wrap_color '(cgroup swap accounting is currently not enabled, you can enable it by setting boot option "swapaccount=1")' bold black)"
   272  		fi
   273  	else
   274  		# Kernel v5.8+ enables swap accounting by default.
   275  		echo "    $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)"
   276  	fi
   277  }
   278  {
   279  	if is_set LEGACY_VSYSCALL_NATIVE; then
   280  		printf -- '- '
   281  		wrap_bad "CONFIG_LEGACY_VSYSCALL_NATIVE" 'enabled'
   282  		echo "    $(wrap_color '(dangerous, provides an ASLR-bypassing target with usable ROP gadgets.)' bold black)"
   283  	elif is_set LEGACY_VSYSCALL_EMULATE; then
   284  		printf -- '- '
   285  		wrap_good "CONFIG_LEGACY_VSYSCALL_EMULATE" 'enabled'
   286  	elif is_set LEGACY_VSYSCALL_NONE; then
   287  		printf -- '- '
   288  		wrap_bad "CONFIG_LEGACY_VSYSCALL_NONE" 'enabled'
   289  		echo "    $(wrap_color '(containers using eglibc <= 2.13 will not work. Switch to' bold black)"
   290  		echo "    $(wrap_color ' "CONFIG_VSYSCALL_[NATIVE|EMULATE]" or use "vsyscall=[native|emulate]"' bold black)"
   291  		echo "    $(wrap_color ' on kernel command line. Note that this will disable ASLR for the,' bold black)"
   292  		echo "    $(wrap_color ' VDSO which may assist in exploiting security vulnerabilities.)' bold black)"
   293  	# else Older kernels (prior to 3dc33bd30f3e, released in v4.40-rc1) do
   294  	#      not have these LEGACY_VSYSCALL options and are effectively
   295  	#      LEGACY_VSYSCALL_EMULATE. Even older kernels are presumably
   296  	#      effectively LEGACY_VSYSCALL_NATIVE.
   297  	fi
   298  }
   299  
   300  if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -le 5 ]); then
   301  	check_flags MEMCG_KMEM
   302  fi
   303  
   304  if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 18 ]); then
   305  	check_flags RESOURCE_COUNTERS
   306  fi
   307  
   308  if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 13 ]); then
   309  	netprio=NETPRIO_CGROUP
   310  else
   311  	netprio=CGROUP_NET_PRIO
   312  fi
   313  
   314  if [ "$kernelMajor" -lt 5 ]; then
   315  	check_flags IOSCHED_CFQ CFQ_GROUP_IOSCHED
   316  fi
   317  
   318  check_flags \
   319  	BLK_CGROUP BLK_DEV_THROTTLING \
   320  	CGROUP_PERF \
   321  	CGROUP_HUGETLB \
   322  	NET_CLS_CGROUP $netprio \
   323  	CFS_BANDWIDTH FAIR_GROUP_SCHED \
   324  	IP_NF_TARGET_REDIRECT \
   325  	IP_VS \
   326  	IP_VS_NFCT \
   327  	IP_VS_PROTO_TCP \
   328  	IP_VS_PROTO_UDP \
   329  	IP_VS_RR \
   330  	SECURITY_SELINUX \
   331  	SECURITY_APPARMOR
   332  
   333  if ! is_set EXT4_USE_FOR_EXT2; then
   334  	check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY
   335  	if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then
   336  		echo "    $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)"
   337  	fi
   338  fi
   339  
   340  check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY
   341  if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then
   342  	if is_set EXT4_USE_FOR_EXT2; then
   343  		echo "    $(wrap_color 'enable these ext4 configs if you are using ext3 or ext4 as backing filesystem' bold black)"
   344  	else
   345  		echo "    $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)"
   346  	fi
   347  fi
   348  
   349  echo '- Network Drivers:'
   350  echo "  - \"$(wrap_color 'overlay' blue)\":"
   351  check_flags VXLAN BRIDGE_VLAN_FILTERING | sed 's/^/    /'
   352  echo '      Optional (for encrypted networks):'
   353  check_flags CRYPTO CRYPTO_AEAD CRYPTO_GCM CRYPTO_SEQIV CRYPTO_GHASH \
   354  	XFRM XFRM_USER XFRM_ALGO INET_ESP NETFILTER_XT_MATCH_BPF | sed 's/^/      /'
   355  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 3 ]; then
   356  	check_flags INET_XFRM_MODE_TRANSPORT | sed 's/^/      /'
   357  fi
   358  echo "  - \"$(wrap_color 'ipvlan' blue)\":"
   359  check_flags IPVLAN | sed 's/^/    /'
   360  echo "  - \"$(wrap_color 'macvlan' blue)\":"
   361  check_flags MACVLAN DUMMY | sed 's/^/    /'
   362  echo "  - \"$(wrap_color 'ftp,tftp client in container' blue)\":"
   363  check_flags NF_NAT_FTP NF_CONNTRACK_FTP NF_NAT_TFTP NF_CONNTRACK_TFTP | sed 's/^/    /'
   364  
   365  # only fail if no storage drivers available
   366  CODE=${EXITCODE}
   367  EXITCODE=0
   368  STORAGE=1
   369  
   370  echo '- Storage Drivers:'
   371  echo "  - \"$(wrap_color 'btrfs' blue)\":"
   372  check_flags BTRFS_FS | sed 's/^/    /'
   373  check_flags BTRFS_FS_POSIX_ACL | sed 's/^/    /'
   374  [ "$EXITCODE" = 0 ] && STORAGE=0
   375  EXITCODE=0
   376  
   377  echo "  - \"$(wrap_color 'devicemapper' blue)\":"
   378  check_flags BLK_DEV_DM DM_THIN_PROVISIONING | sed 's/^/    /'
   379  [ "$EXITCODE" = 0 ] && STORAGE=0
   380  EXITCODE=0
   381  
   382  echo "  - \"$(wrap_color 'overlay' blue)\":"
   383  check_flags OVERLAY_FS | sed 's/^/    /'
   384  [ "$EXITCODE" = 0 ] && STORAGE=0
   385  EXITCODE=0
   386  
   387  echo "  - \"$(wrap_color 'zfs' blue)\":"
   388  printf '    - '
   389  check_device /dev/zfs
   390  printf '    - '
   391  check_command zfs
   392  printf '    - '
   393  check_command zpool
   394  [ "$EXITCODE" = 0 ] && STORAGE=0
   395  EXITCODE=0
   396  
   397  EXITCODE=$CODE
   398  [ "$STORAGE" = 1 ] && EXITCODE=1
   399  
   400  echo
   401  
   402  check_limit_over() {
   403  	if [ "$(cat "$1")" -le "$2" ]; then
   404  		wrap_bad "- $1" "$(cat "$1")"
   405  		wrap_color "    This should be set to at least $2, for example set: sysctl -w kernel/keys/root_maxkeys=1000000" bold black
   406  		EXITCODE=1
   407  	else
   408  		wrap_good "- $1" "$(cat "$1")"
   409  	fi
   410  }
   411  
   412  echo 'Limits:'
   413  check_limit_over /proc/sys/kernel/keys/root_maxkeys 10000
   414  echo
   415  
   416  exit $EXITCODE