preinit 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/sh -e
  2. # This script is spawned by s6-overlay-suexec, as the
  3. # first thing in the userland boot process.
  4. # It is normally run as root, but some configurations want to
  5. # run completely unprivileged and s6-overlay-suexec is denied
  6. # suid, in which case preinit will be unprivileged as well.
  7. # The point of preinit is to sanity check the system to make
  8. # sure s6-linux-init can run in a safe configuration. If we
  9. # are root, or if the system has been correctly prepared by the
  10. # container manager for a privilegeless execution, we can fix
  11. # any issue we encounter. Otherwise, we just report the problem
  12. # and abort execution.
  13. # The UID, USER, EUID, GID, GROUP and EGID variables are set for
  14. # us by s6-overlay-suexec, so we can check every possible case.
  15. prog=/package/admin/s6-overlay/libexec/preinit
  16. checknoexec () {
  17. IFS=,
  18. set -- $1
  19. IFS=
  20. while test "$#" -gt 0 ; do
  21. if test "$1" = noexec ; then
  22. return 0
  23. fi
  24. shift
  25. done
  26. return 1
  27. }
  28. # Ensure /run is writable
  29. if test "0$S6_READ_ONLY_ROOT" -ne 0 ; then
  30. echo "$prog: info: read-only root" 1>&2
  31. if ! test -d /run ; then
  32. echo "$prog: fatal: /run is missing or not a directory" 1>&2
  33. exit 100
  34. fi
  35. if : > '/run/test of writability' 2>/dev/null ; then
  36. echo "$prog: info: writable /run. Checking for executability." 1>&2
  37. s6-rmrf '/run/test of writability'
  38. if ! s6-mount -o remount,rw,exec tmpfs /run 2>/dev/null ; then
  39. notfound=true
  40. while read these filesystem type options please ; do
  41. if test $filesystem = /run ; then
  42. notfound=false
  43. if checknoexec "$options" ; then
  44. echo "$prog: warning: your container manager pre-mounts run with \
  45. the incorrect noexec option, which s6-overlay cannot work with; expect /init \
  46. to crash soon. To fix the issue, either pre-mount /run with the exec option, \
  47. or as a workaround give this container the CAP_SYS_ADMIN capability so \
  48. s6-overlay can fix it at run time." 1>&2
  49. fi
  50. break
  51. fi
  52. done < /proc/mounts
  53. if $notfound ; then
  54. echo "$prog: warning: unable to find /run in /proc/mounts, check that \
  55. your container manager pre-mounts /proc, and that /run is a tmpfs. The container \
  56. is likely to crash soon, if /run is (incorrectly) mounted noexec." 1>&2
  57. fi
  58. fi
  59. else
  60. echo "$prog: info: creating a tmpfs on /run" 1>&2
  61. s6-mount -wt tmpfs -o exec,mode=0755 tmpfs /run
  62. fi
  63. else
  64. s6-mkdir -p -m 0755 /run
  65. fi
  66. eval `s6-overlay-stat /run`
  67. if test "0$S6_VERBOSITY" -gt 1 ; then
  68. echo "$prog: info: container permissions: uid=$UID ($USER), euid=$EUID, gid=$GID ($GROUP), egid=$EGID"
  69. echo "$prog: info: /run permissions: uid=$uid ($user), gid=$gid ($group), perms=$perms"
  70. fi
  71. if test "$UID" -ne "$uid" ; then # /run does not belong to the container user
  72. if test "$EUID" -eq 0 ; then
  73. echo "$prog: info: /run belongs to uid $uid instead of $UID - fixing it"
  74. s6-chown -U -- /run
  75. s6-chmod 0755 /run
  76. elif test "$GID" -eq 0 && test "$gid" -eq 0 ; then # Unprivileged Kubernetes Environment
  77. if echo "$perms" | s6-grep -qF gxgwgr && echo "$perms" | s6-grep -qvF ow ; then
  78. echo "$prog: info: using /run with gid 0"
  79. else
  80. echo "$prog: fatal: wrong permissions on /run for a gid 0 setup"
  81. exit 100
  82. fi
  83. else
  84. echo "$prog: fatal: /run belongs to uid $uid instead of $UID and we're lacking the privileges to fix it."
  85. exit 100
  86. fi
  87. fi
  88. # Ensure /var/run is a symlink to /run
  89. if test -L /var/run && test "`s6-linkname -f /var/run`" = /run ; then : ; else
  90. echo "$prog: notice: /var/run is not a symlink to /run, fixing it" 1>&2
  91. s6-rmrf /var/run
  92. s6-ln -s /run /var/run
  93. fi
  94. # Clean up in case /run hasn't been wiped or USER has changed
  95. s6-rmrf /run/s6 /run/service /run/uncaught-logs /run/s6-rc*
  96. s6-mkdir -m 0755 /run/s6 /run/service
  97. if test "0$UID" -ne 0 ; then
  98. s6-chown -U -- /run/s6
  99. s6-chown -U -- /run/service
  100. fi