check-config.sh 11 KB

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