least-authority.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix least-authority)
  19. #:use-module (guix gexp)
  20. #:use-module (guix modules)
  21. #:use-module ((guix store) #:select (%store-prefix))
  22. #:autoload (gnu build linux-container) (%namespaces)
  23. #:autoload (gnu system file-systems) (file-system-mapping
  24. file-system-mapping-source
  25. spec->file-system
  26. file-system->spec
  27. file-system-mapping->bind-mount)
  28. #:export (least-authority-wrapper))
  29. ;;; Commentary:
  30. ;;;
  31. ;;; This module provides tools to execute programs with the least authority
  32. ;;; necessary, using Linux namespaces.
  33. ;;;
  34. ;;; Code:
  35. (define %precious-variables
  36. ;; Environment variables preserved by the wrapper by default.
  37. '("HOME" "USER" "LOGNAME" "DISPLAY" "XAUTHORITY" "TERM" "TZ" "PAGER"))
  38. (define* (least-authority-wrapper program
  39. #:key (name "pola-wrapper")
  40. (guest-uid 1000)
  41. (guest-gid 1000)
  42. (mappings '())
  43. (namespaces %namespaces)
  44. (directory "/")
  45. (preserved-environment-variables
  46. %precious-variables))
  47. "Return a wrapper of PROGRAM that executes it with the least authority.
  48. PROGRAM is executed in separate namespaces according to NAMESPACES, a list of
  49. symbols; it runs with GUEST-UID and GUEST-GID. MAPPINGS is a list of
  50. <file-system-mapping> records indicating directories mirrored inside the
  51. execution environment of PROGRAM. DIRECTORY is the working directory of the
  52. wrapped process. Each environment listed in PRESERVED-ENVIRONMENT-VARIABLES
  53. is preserved; other environment variables are erased."
  54. (define code
  55. (with-imported-modules (source-module-closure
  56. '((gnu system file-systems)
  57. (gnu build shepherd)
  58. (gnu build linux-container)))
  59. #~(begin
  60. (use-modules (gnu system file-systems)
  61. (gnu build linux-container)
  62. ((gnu build shepherd) #:select (default-mounts))
  63. (srfi srfi-1))
  64. (define variables
  65. (filter-map (lambda (variable)
  66. (let ((value (getenv variable)))
  67. (and value
  68. (string-append variable "=" value))))
  69. '#$preserved-environment-variables))
  70. (define (read-file file)
  71. (call-with-input-file file read))
  72. (define references
  73. (delete-duplicates
  74. (append-map read-file
  75. '#$(map references-file
  76. (cons program
  77. (map file-system-mapping-source
  78. mappings))))))
  79. (define (store? file-system)
  80. (string=? (file-system-mount-point file-system)
  81. #$(%store-prefix)))
  82. (define mounts
  83. (append (map (lambda (item)
  84. (file-system-mapping->bind-mount
  85. (file-system-mapping (source item)
  86. (target item))))
  87. references)
  88. (remove store?
  89. (default-mounts
  90. #:namespaces '#$namespaces))
  91. (map spec->file-system
  92. '#$(map (compose file-system->spec
  93. file-system-mapping->bind-mount)
  94. mappings))))
  95. (define (reify-exit-status status)
  96. (cond ((status:exit-val status) => exit)
  97. ((or (status:term-sig status)
  98. (status:stop-sig status))
  99. => (lambda (signal)
  100. (format (current-error-port)
  101. "~a terminated with signal ~a~%"
  102. #$program signal)
  103. (exit (+ 128 signal))))))
  104. ;; Note: 'call-with-container' creates a sub-process that this one
  105. ;; waits for. This might seem suboptimal but unshare(2) isn't
  106. ;; really applicable: the process would still run in the same PID
  107. ;; namespace.
  108. (reify-exit-status
  109. (call-with-container mounts
  110. (lambda ()
  111. (chdir #$directory)
  112. (environ variables)
  113. (apply execl #$program #$program (cdr (command-line))))
  114. ;; Don't assume PROGRAM can behave as an init process.
  115. #:child-is-pid1? #f
  116. #:guest-uid #$guest-uid
  117. #:guest-gid #$guest-gid
  118. #:namespaces '#$namespaces)))))
  119. (program-file name code))