github.com/lacework-dev/go-moby@v20.10.12+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  if [ "$(stat -f -c %t /sys/fs/cgroup 2> /dev/null)" = '63677270' ]; then
   159  	echo "$(wrap_good 'cgroup hierarchy' 'cgroupv2')"
   160  else
   161  	cgroupSubsystemDir="$(awk '/[, ](cpu|cpuacct|cpuset|devices|freezer|memory)[, ]/ && $3 == "cgroup" { print $2 }' /proc/mounts | head -n1)"
   162  	cgroupDir="$(dirname "$cgroupSubsystemDir")"
   163  	if [ -d "$cgroupDir/cpu" ] || [ -d "$cgroupDir/cpuacct" ] || [ -d "$cgroupDir/cpuset" ] || [ -d "$cgroupDir/devices" ] || [ -d "$cgroupDir/freezer" ] || [ -d "$cgroupDir/memory" ]; then
   164  		echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]"
   165  	else
   166  		if [ "$cgroupSubsystemDir" ]; then
   167  			echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]"
   168  		else
   169  			wrap_bad 'cgroup hierarchy' 'nonexistent??'
   170  		fi
   171  		EXITCODE=1
   172  		echo "    $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)"
   173  	fi
   174  fi
   175  
   176  if [ "$(cat /sys/module/apparmor/parameters/enabled 2> /dev/null)" = 'Y' ]; then
   177  	echo -n '- '
   178  	if command -v apparmor_parser &> /dev/null; then
   179  		wrap_good 'apparmor' 'enabled and tools installed'
   180  	else
   181  		wrap_bad 'apparmor' 'enabled, but apparmor_parser missing'
   182  		echo -n '    '
   183  		if command -v apt-get &> /dev/null; then
   184  			wrap_color '(use "apt-get install apparmor" to fix this)'
   185  		elif command -v yum &> /dev/null; then
   186  			wrap_color '(your best bet is "yum install apparmor-parser")'
   187  		else
   188  			wrap_color '(look for an "apparmor" package for your distribution)'
   189  		fi
   190  		EXITCODE=1
   191  	fi
   192  fi
   193  
   194  flags=(
   195  	NAMESPACES {NET,PID,IPC,UTS}_NS
   196  	CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED CPUSETS MEMCG
   197  	KEYS
   198  	VETH BRIDGE BRIDGE_NETFILTER
   199  	IP_NF_FILTER IP_NF_TARGET_MASQUERADE
   200  	NETFILTER_XT_MATCH_{ADDRTYPE,CONNTRACK,IPVS}
   201  	NETFILTER_XT_MARK
   202  	IP_NF_NAT NF_NAT
   203  
   204  	# required for bind-mounting /dev/mqueue into containers
   205  	POSIX_MQUEUE
   206  )
   207  check_flags "${flags[@]}"
   208  if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -lt 8 ]); then
   209  	check_flags DEVPTS_MULTIPLE_INSTANCES
   210  fi
   211  
   212  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 1 ]; then
   213  	check_flags NF_NAT_IPV4
   214  fi
   215  
   216  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 2 ]; then
   217  	check_flags NF_NAT_NEEDED
   218  fi
   219  
   220  echo
   221  
   222  echo 'Optional Features:'
   223  {
   224  	check_flags USER_NS
   225  	check_distro_userns
   226  }
   227  {
   228  	check_flags SECCOMP
   229  }
   230  {
   231  	check_flags CGROUP_PIDS
   232  }
   233  {
   234  	check_flags MEMCG_SWAP
   235  	# Kernel v5.8+ removes MEMCG_SWAP_ENABLED.
   236  	if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 8 ]; then
   237  		CODE=${EXITCODE}
   238  		check_flags MEMCG_SWAP_ENABLED
   239  		# FIXME this check is cgroupv1-specific
   240  		if [ -e /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes ]; then
   241  			echo "    $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)"
   242  			EXITCODE=${CODE}
   243  		elif is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then
   244  			echo "    $(wrap_color '(cgroup swap accounting is currently not enabled, you can enable it by setting boot option "swapaccount=1")' bold black)"
   245  		fi
   246  	else
   247  		# Kernel v5.8+ enables swap accounting by default.
   248  		echo "    $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)"
   249  	fi
   250  }
   251  {
   252  	if is_set LEGACY_VSYSCALL_NATIVE; then
   253  		echo -n "- "
   254  		wrap_bad "CONFIG_LEGACY_VSYSCALL_NATIVE" 'enabled'
   255  		echo "    $(wrap_color '(dangerous, provides an ASLR-bypassing target with usable ROP gadgets.)' bold black)"
   256  	elif is_set LEGACY_VSYSCALL_EMULATE; then
   257  		echo -n "- "
   258  		wrap_good "CONFIG_LEGACY_VSYSCALL_EMULATE" 'enabled'
   259  	elif is_set LEGACY_VSYSCALL_NONE; then
   260  		echo -n "- "
   261  		wrap_bad "CONFIG_LEGACY_VSYSCALL_NONE" 'enabled'
   262  		echo "    $(wrap_color '(containers using eglibc <= 2.13 will not work. Switch to' bold black)"
   263  		echo "    $(wrap_color ' "CONFIG_VSYSCALL_[NATIVE|EMULATE]" or use "vsyscall=[native|emulate]"' bold black)"
   264  		echo "    $(wrap_color ' on kernel command line. Note that this will disable ASLR for the,' bold black)"
   265  		echo "    $(wrap_color ' VDSO which may assist in exploiting security vulnerabilities.)' bold black)"
   266  	# else Older kernels (prior to 3dc33bd30f3e, released in v4.40-rc1) do
   267  	#      not have these LEGACY_VSYSCALL options and are effectively
   268  	#      LEGACY_VSYSCALL_EMULATE. Even older kernels are presumably
   269  	#      effectively LEGACY_VSYSCALL_NATIVE.
   270  	fi
   271  }
   272  
   273  if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -le 5 ]); then
   274  	check_flags MEMCG_KMEM
   275  fi
   276  
   277  if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 18 ]); then
   278  	check_flags RESOURCE_COUNTERS
   279  fi
   280  
   281  if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 13 ]); then
   282  	netprio=NETPRIO_CGROUP
   283  else
   284  	netprio=CGROUP_NET_PRIO
   285  fi
   286  
   287  if [ "$kernelMajor" -lt 5 ]; then
   288  	check_flags IOSCHED_CFQ CFQ_GROUP_IOSCHED
   289  fi
   290  
   291  flags=(
   292  	BLK_CGROUP BLK_DEV_THROTTLING
   293  	CGROUP_PERF
   294  	CGROUP_HUGETLB
   295  	NET_CLS_CGROUP $netprio
   296  	CFS_BANDWIDTH FAIR_GROUP_SCHED RT_GROUP_SCHED
   297  	IP_NF_TARGET_REDIRECT
   298  	IP_VS
   299  	IP_VS_NFCT
   300  	IP_VS_PROTO_TCP
   301  	IP_VS_PROTO_UDP
   302  	IP_VS_RR
   303  )
   304  check_flags "${flags[@]}"
   305  
   306  if ! is_set EXT4_USE_FOR_EXT2; then
   307  	check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY
   308  	if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then
   309  		echo "    $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)"
   310  	fi
   311  fi
   312  
   313  check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY
   314  if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then
   315  	if is_set EXT4_USE_FOR_EXT2; then
   316  		echo "    $(wrap_color 'enable these ext4 configs if you are using ext3 or ext4 as backing filesystem' bold black)"
   317  	else
   318  		echo "    $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)"
   319  	fi
   320  fi
   321  
   322  echo '- Network Drivers:'
   323  echo "  - \"$(wrap_color 'overlay' blue)\":"
   324  check_flags VXLAN BRIDGE_VLAN_FILTERING | sed 's/^/    /'
   325  echo '      Optional (for encrypted networks):'
   326  check_flags CRYPTO CRYPTO_AEAD CRYPTO_GCM CRYPTO_SEQIV CRYPTO_GHASH \
   327  	XFRM XFRM_USER XFRM_ALGO INET_ESP | sed 's/^/      /'
   328  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 3 ]; then
   329  	check_flags INET_XFRM_MODE_TRANSPORT | sed 's/^/      /'
   330  fi
   331  echo "  - \"$(wrap_color 'ipvlan' blue)\":"
   332  check_flags IPVLAN | sed 's/^/    /'
   333  echo "  - \"$(wrap_color 'macvlan' blue)\":"
   334  check_flags MACVLAN DUMMY | sed 's/^/    /'
   335  echo "  - \"$(wrap_color 'ftp,tftp client in container' blue)\":"
   336  check_flags NF_NAT_FTP NF_CONNTRACK_FTP NF_NAT_TFTP NF_CONNTRACK_TFTP | sed 's/^/    /'
   337  
   338  # only fail if no storage drivers available
   339  CODE=${EXITCODE}
   340  EXITCODE=0
   341  STORAGE=1
   342  
   343  echo '- Storage Drivers:'
   344  echo "  - \"$(wrap_color 'aufs' blue)\":"
   345  check_flags AUFS_FS | sed 's/^/    /'
   346  if ! is_set AUFS_FS && grep -q aufs /proc/filesystems; then
   347  	echo "      $(wrap_color '(note that some kernels include AUFS patches but not the AUFS_FS flag)' bold black)"
   348  fi
   349  [ "$EXITCODE" = 0 ] && STORAGE=0
   350  EXITCODE=0
   351  
   352  echo "  - \"$(wrap_color 'btrfs' blue)\":"
   353  check_flags BTRFS_FS | sed 's/^/    /'
   354  check_flags BTRFS_FS_POSIX_ACL | sed 's/^/    /'
   355  [ "$EXITCODE" = 0 ] && STORAGE=0
   356  EXITCODE=0
   357  
   358  echo "  - \"$(wrap_color 'devicemapper' blue)\":"
   359  check_flags BLK_DEV_DM DM_THIN_PROVISIONING | sed 's/^/    /'
   360  [ "$EXITCODE" = 0 ] && STORAGE=0
   361  EXITCODE=0
   362  
   363  echo "  - \"$(wrap_color 'overlay' blue)\":"
   364  check_flags OVERLAY_FS | sed 's/^/    /'
   365  [ "$EXITCODE" = 0 ] && STORAGE=0
   366  EXITCODE=0
   367  
   368  echo "  - \"$(wrap_color 'zfs' blue)\":"
   369  echo -n "    - "
   370  check_device /dev/zfs
   371  echo -n "    - "
   372  check_command zfs
   373  echo -n "    - "
   374  check_command zpool
   375  [ "$EXITCODE" = 0 ] && STORAGE=0
   376  EXITCODE=0
   377  
   378  EXITCODE=$CODE
   379  [ "$STORAGE" = 1 ] && EXITCODE=1
   380  
   381  echo
   382  
   383  check_limit_over() {
   384  	if [ "$(cat "$1")" -le "$2" ]; then
   385  		wrap_bad "- $1" "$(cat "$1")"
   386  		wrap_color "    This should be set to at least $2, for example set: sysctl -w kernel/keys/root_maxkeys=1000000" bold black
   387  		EXITCODE=1
   388  	else
   389  		wrap_good "- $1" "$(cat "$1")"
   390  	fi
   391  }
   392  
   393  echo 'Limits:'
   394  check_limit_over /proc/sys/kernel/keys/root_maxkeys 10000
   395  echo
   396  
   397  exit $EXITCODE