jolausa

Untitled

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