activation.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2013 Andreas Enge <andreas@enge.fr>
  5. ;;; Copyright © 2015, 2018 Mark H Weaver <mhw@netris.org>
  6. ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
  7. ;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
  8. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (gnu build activation)
  25. #:use-module (gnu system accounts)
  26. #:use-module (gnu build accounts)
  27. #:use-module (gnu build linux-boot)
  28. #:use-module (guix build utils)
  29. #:use-module ((guix build syscalls) #:select (with-file-lock))
  30. #:use-module (ice-9 ftw)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 vlist)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-11)
  35. #:use-module (srfi srfi-26)
  36. #:export (activate-users+groups
  37. activate-user-home
  38. activate-etc
  39. activate-setuid-programs
  40. activate-special-files
  41. activate-modprobe
  42. activate-firmware
  43. activate-ptrace-attach
  44. activate-current-system
  45. mkdir-p/perms))
  46. ;;; Commentary:
  47. ;;;
  48. ;;; This module provides "activation" helpers. Activation is the process that
  49. ;;; consists in setting up system-wide files and directories so that an
  50. ;;; 'operating-system' configuration becomes active.
  51. ;;;
  52. ;;; Code:
  53. (define %skeleton-directory
  54. ;; Directory containing skeleton files for new accounts.
  55. ;; Note: keep the trailing '/' so that 'scandir' enters it.
  56. "/etc/skel/")
  57. (define (dot-or-dot-dot? file)
  58. (member file '("." "..")))
  59. ;; Based upon mkdir-p from (guix build utils)
  60. (define (verify-not-symbolic dir)
  61. "Verify DIR or its ancestors aren't symbolic links."
  62. (define absolute?
  63. (string-prefix? "/" dir))
  64. (define not-slash
  65. (char-set-complement (char-set #\/)))
  66. (define (verify-component file)
  67. (unless (eq? 'directory (stat:type (lstat file)))
  68. (error "file name component is not a directory" dir)))
  69. (let loop ((components (string-tokenize dir not-slash))
  70. (root (if absolute?
  71. ""
  72. ".")))
  73. (match components
  74. ((head tail ...)
  75. (let ((file (string-append root "/" head)))
  76. (catch 'system-error
  77. (lambda ()
  78. (verify-component file)
  79. (loop tail file))
  80. (lambda args
  81. (if (= ENOENT (system-error-errno args))
  82. #t
  83. (apply throw args))))))
  84. (() #t))))
  85. ;; TODO: the TOCTTOU race can be addressed once guile has bindings
  86. ;; for fstatat, openat and friends.
  87. (define (mkdir-p/perms directory owner bits)
  88. "Create the directory DIRECTORY and all its ancestors.
  89. Verify no component of DIRECTORY is a symbolic link.
  90. Warning: this is currently suspect to a TOCTTOU race!"
  91. (verify-not-symbolic directory)
  92. (mkdir-p directory)
  93. (chown directory (passwd:uid owner) (passwd:gid owner))
  94. (chmod directory bits))
  95. (define* (copy-account-skeletons home
  96. #:key
  97. (directory %skeleton-directory)
  98. uid gid)
  99. "Copy the account skeletons from DIRECTORY to HOME. When UID is an integer,
  100. make it the owner of all the files created except the home directory; likewise
  101. for GID."
  102. (define (set-owner file)
  103. (when (or uid gid)
  104. (chown file (or uid -1) (or gid -1))))
  105. (let ((files (scandir directory (negate dot-or-dot-dot?)
  106. string<?)))
  107. (mkdir-p home)
  108. (for-each (lambda (file)
  109. (let ((target (string-append home "/" file)))
  110. (copy-recursively (string-append directory "/" file)
  111. target
  112. #:log (%make-void-port "w"))
  113. (for-each set-owner
  114. (find-files target (const #t)
  115. #:directories? #t))
  116. (make-file-writable target)))
  117. files)))
  118. (define* (make-skeletons-writable home
  119. #:optional (directory %skeleton-directory))
  120. "Make sure that the files that have been copied from DIRECTORY to HOME are
  121. owner-writable in HOME."
  122. (let ((files (scandir directory (negate dot-or-dot-dot?)
  123. string<?)))
  124. (for-each (lambda (file)
  125. (let ((target (string-append home "/" file)))
  126. (when (file-exists? target)
  127. (make-file-writable target))))
  128. files)))
  129. (define (duplicates lst)
  130. "Return elements from LST present more than once in LST."
  131. (let loop ((lst lst)
  132. (seen vlist-null)
  133. (result '()))
  134. (match lst
  135. (()
  136. (reverse result))
  137. ((head . tail)
  138. (loop tail
  139. (vhash-cons head #t seen)
  140. (if (vhash-assoc head seen)
  141. (cons head result)
  142. result))))))
  143. (define (activate-users+groups users groups)
  144. "Make sure USERS (a list of user account records) and GROUPS (a list of user
  145. group records) are all available."
  146. (define (make-home-directory user)
  147. (let ((home (user-account-home-directory user))
  148. (pwd (getpwnam (user-account-name user))))
  149. (mkdir-p home)
  150. ;; Always set ownership and permissions for home directories of system
  151. ;; accounts. If a service needs looser permissions on its home
  152. ;; directories, it can always chmod it in an activation snippet.
  153. (chown home (passwd:uid pwd) (passwd:gid pwd))
  154. (chmod home #o700)))
  155. (define system-accounts
  156. (filter (lambda (user)
  157. (and (user-account-system? user)
  158. (user-account-create-home-directory? user)))
  159. users))
  160. ;; Allow home directories to be created under /var/lib.
  161. (mkdir-p "/var/lib")
  162. ;; Take same lock as libc's 'lckpwdf' (but without a timeout) while we read
  163. ;; and write the databases. This ensures there's no race condition with
  164. ;; other tools that might be accessing it at the same time.
  165. (with-file-lock %password-lock-file
  166. (let-values (((groups passwd shadow)
  167. (user+group-databases users groups)))
  168. (write-group groups)
  169. (write-passwd passwd)
  170. (write-shadow shadow)))
  171. ;; Home directories of non-system accounts are created by
  172. ;; 'activate-user-home'.
  173. (for-each make-home-directory system-accounts)
  174. ;; Turn shared home directories, such as /var/empty, into root-owned,
  175. ;; read-only places.
  176. (for-each (lambda (directory)
  177. (chown directory 0 0)
  178. (chmod directory #o555))
  179. (duplicates (map user-account-home-directory system-accounts))))
  180. (define (activate-user-home users)
  181. "Create and populate the home directory of USERS, a list of tuples, unless
  182. they already exist."
  183. (define ensure-user-home
  184. (lambda (user)
  185. (let ((name (user-account-name user))
  186. (home (user-account-home-directory user))
  187. (create-home? (user-account-create-home-directory? user))
  188. (system? (user-account-system? user)))
  189. ;; The home directories of system accounts are created during
  190. ;; activation, not here.
  191. (unless (or (not home) (not create-home?) system?
  192. (directory-exists? home))
  193. (let* ((pw (getpwnam name))
  194. (uid (passwd:uid pw))
  195. (gid (passwd:gid pw)))
  196. (mkdir-p home)
  197. (chmod home #o700)
  198. (copy-account-skeletons home
  199. #:uid uid #:gid gid)
  200. ;; It is important 'chown' be called after
  201. ;; 'copy-account-skeletons'. Otherwise, a malicious user with
  202. ;; good timing could create a symlink in HOME that would be
  203. ;; dereferenced by 'copy-account-skeletons'.
  204. (chown home uid gid))))))
  205. (for-each ensure-user-home users))
  206. (define (activate-etc etc)
  207. "Install ETC, a directory in the store, as the source of static files for
  208. /etc."
  209. ;; /etc is a mixture of static and dynamic settings. Here is where we
  210. ;; initialize it from the static part.
  211. (define (rm-f file)
  212. (false-if-exception (delete-file file)))
  213. (format #t "populating /etc from ~a...~%" etc)
  214. (mkdir-p "/etc")
  215. ;; Create the /etc/ssl -> /run/current-system/profile/etc/ssl symlink. This
  216. ;; symlink, to a target outside of the store, probably doesn't belong in the
  217. ;; static 'etc' store directory. However, if it were to be put there,
  218. ;; beware that if /run/current-system/profile/etc/ssl doesn't exist at the
  219. ;; time of activation (e.g. when installing a fresh system), the call to
  220. ;; 'file-is-directory?' below will fail because it uses 'stat', not 'lstat'.
  221. (rm-f "/etc/ssl")
  222. (symlink "/run/current-system/profile/etc/ssl" "/etc/ssl")
  223. (rm-f "/etc/static")
  224. (symlink etc "/etc/static")
  225. (for-each (lambda (file)
  226. (let ((target (string-append "/etc/" file))
  227. (source (string-append "/etc/static/" file)))
  228. (rm-f target)
  229. ;; Things such as /etc/sudoers must be regular files, not
  230. ;; symlinks; furthermore, they could be modified behind our
  231. ;; back---e.g., with 'visudo'. Thus, make a copy instead of
  232. ;; symlinking them.
  233. (if (file-is-directory? source)
  234. (symlink source target)
  235. (copy-file source target))
  236. ;; XXX: Dirty hack to meet sudo's expectations.
  237. (when (string=? (basename target) "sudoers")
  238. (chmod target #o440))))
  239. (scandir etc (negate dot-or-dot-dot?)
  240. ;; The default is 'string-locale<?', but we don't have
  241. ;; it when run from the initrd's statically-linked
  242. ;; Guile.
  243. string<?)))
  244. (define %setuid-directory
  245. ;; Place where setuid programs are stored.
  246. "/run/setuid-programs")
  247. (define (activate-setuid-programs programs)
  248. "Turn PROGRAMS, a list of file names, into setuid programs stored under
  249. %SETUID-DIRECTORY."
  250. (define (make-setuid-program prog)
  251. (let ((target (string-append %setuid-directory
  252. "/" (basename prog))))
  253. (copy-file prog target)
  254. (chown target 0 0)
  255. (chmod target #o4555)))
  256. (format #t "setting up setuid programs in '~a'...~%"
  257. %setuid-directory)
  258. (if (file-exists? %setuid-directory)
  259. (for-each (compose delete-file
  260. (cut string-append %setuid-directory "/" <>))
  261. (scandir %setuid-directory
  262. (lambda (file)
  263. (not (member file '("." ".."))))
  264. string<?))
  265. (mkdir-p %setuid-directory))
  266. (for-each (lambda (program)
  267. (catch 'system-error
  268. (lambda ()
  269. (make-setuid-program program))
  270. (lambda args
  271. ;; If we fail to create a setuid program, better keep going
  272. ;; so that we don't leave %SETUID-DIRECTORY empty or
  273. ;; half-populated. This can happen if PROGRAMS contains
  274. ;; incorrect file names: <https://bugs.gnu.org/38800>.
  275. (format (current-error-port)
  276. "warning: failed to make '~a' setuid-root: ~a~%"
  277. program (strerror (system-error-errno args))))))
  278. programs))
  279. (define (activate-special-files special-files)
  280. "Install the files listed in SPECIAL-FILES. Each element of SPECIAL-FILES
  281. is a pair where the first element is the name of the special file and the
  282. second element is the name it should appear at, such as:
  283. ((\"/bin/sh\" \"/gnu/store/…-bash/bin/sh\")
  284. (\"/usr/bin/env\" \"/gnu/store/…-coreutils/bin/env\"))
  285. "
  286. (define install-special-file
  287. (match-lambda
  288. ((target file)
  289. (let ((pivot (string-append target ".new")))
  290. (mkdir-p (dirname target))
  291. (symlink file pivot)
  292. (rename-file pivot target)))))
  293. (for-each install-special-file special-files))
  294. (define (activate-modprobe modprobe)
  295. "Tell the kernel to use MODPROBE to load modules."
  296. ;; If the kernel was built without loadable module support, this file is
  297. ;; unavailable, so check for its existence first.
  298. (when (file-exists? "/proc/sys/kernel/modprobe")
  299. (call-with-output-file "/proc/sys/kernel/modprobe"
  300. (lambda (port)
  301. (display modprobe port)))))
  302. (define (activate-firmware directory)
  303. "Tell the kernel to look for device firmware under DIRECTORY. This
  304. mechanism bypasses udev: it allows Linux to handle firmware loading directly
  305. by itself, without having to resort to a \"user helper\"."
  306. (call-with-output-file "/sys/module/firmware_class/parameters/path"
  307. (lambda (port)
  308. (display directory port))))
  309. (define (activate-ptrace-attach)
  310. "Allow users to PTRACE_ATTACH their own processes.
  311. This works around a regression introduced in the default \"security\" policy
  312. found in Linux 3.4 onward that prevents users from attaching to their own
  313. processes--see Yama.txt in the Linux source tree for the rationale. This
  314. sounds like an unacceptable restriction for little or no security
  315. improvement."
  316. (let ((file "/proc/sys/kernel/yama/ptrace_scope"))
  317. (when (file-exists? file)
  318. (call-with-output-file file
  319. (lambda (port)
  320. (display 0 port))))))
  321. (define %current-system
  322. ;; The system that is current (a symlink.) This is not necessarily the same
  323. ;; as the system we booted (aka. /run/booted-system) because we can re-build
  324. ;; a new system configuration and activate it, without rebooting.
  325. "/run/current-system")
  326. (define (boot-time-system)
  327. "Return the '--system' argument passed on the kernel command line."
  328. (find-long-option "--system" (if (string-contains %host-type "linux-gnu")
  329. (linux-command-line)
  330. (command-line))))
  331. (define* (activate-current-system
  332. #:optional (system (or (getenv "GUIX_NEW_SYSTEM")
  333. (boot-time-system))))
  334. "Atomically make SYSTEM the current system."
  335. ;; The 'GUIX_NEW_SYSTEM' environment variable is used as a way for 'guix
  336. ;; system reconfigure' to pass the file name of the new system.
  337. (format #t "making '~a' the current system...~%" system)
  338. ;; Atomically make SYSTEM current.
  339. (let ((new (string-append %current-system ".new")))
  340. (symlink system new)
  341. (rename-file new %current-system)))
  342. ;;; activation.scm ends here