check-config.sh 12 KB

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