| 1 | ; -*- lisp -*- |
| 2 | ;;; GNU Guix --- Functional package management for GNU |
| 3 | ;;; Copyright © 2018, 2022 Ricardo Wurmus <[email protected]> |
| 4 | ;;; Copyright © 2020 Daniel Brooks <[email protected]> |
| 5 | ;;; Copyright © 2020 Marius Bakke <[email protected]> |
| 6 | ;;; |
| 7 | ;;; This file is part of GNU Guix. |
| 8 | ;;; |
| 9 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
| 10 | ;;; under the terms of the GNU General Public License as published by |
| 11 | ;;; the Free Software Foundation; either version 3 of the License, or (at |
| 12 | ;;; your option) any later version. |
| 13 | ;;; |
| 14 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
| 15 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | ;;; GNU General Public License for more details. |
| 18 | ;;; |
| 19 | ;;; You should have received a copy of the GNU General Public License |
| 20 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | ;; This is a specification for SELinux 2.7 written in the SELinux Common |
| 23 | ;; Intermediate Language (CIL). It refers to types that must be defined in |
| 24 | ;; the system's base policy. |
| 25 | |
| 26 | ;; If you, like me, need advice about fixing an SELinux policy, I recommend |
| 27 | ;; reading https://danwalsh.livejournal.com/55324.html |
| 28 | |
| 29 | ;; In particular, you can run semanage permissive -a guix_daemon.guix_daemon_t |
| 30 | ;; to allow guix-daemon to do whatever it wants. SELinux will still check its |
| 31 | ;; permissions, and when it doesn't have permission it will still send an |
| 32 | ;; audit message to your system logs. This lets you know what permissions it |
| 33 | ;; ought to have. Use ausearch --raw to find the permissions violations, then |
| 34 | ;; pipe that to audit2allow to generate an updated policy. You'll still need |
| 35 | ;; to translate that policy into CIL in order to update this file, but that's |
| 36 | ;; fairly straight-forward. Annoying, but easy. |
| 37 | |
| 38 | (block guix_daemon |
| 39 | ;; Require existing types |
| 40 | (typeattributeset cil_gen_require domain) |
| 41 | (typeattributeset cil_gen_require init_t) |
| 42 | (typeattributeset cil_gen_require init_var_run_t) |
| 43 | (typeattributeset cil_gen_require nscd_var_run_t) |
| 44 | (typeattributeset cil_gen_require system_dbusd_var_run_t) |
| 45 | (typeattributeset cil_gen_require tmp_t) |
| 46 | (typeattributeset cil_gen_require var_log_t) |
| 47 | |
| 48 | ;; Declare own types |
| 49 | (type guix_daemon_t) |
| 50 | (roletype object_r guix_daemon_t) |
| 51 | (type guix_daemon_conf_t) |
| 52 | (roletype object_r guix_daemon_conf_t) |
| 53 | (typeattributeset file_type guix_daemon_conf_t) |
| 54 | (type guix_daemon_exec_t) |
| 55 | (roletype object_r guix_daemon_exec_t) |
| 56 | (typeattributeset file_type guix_daemon_exec_t) |
| 57 | (type guix_daemon_socket_t) |
| 58 | (roletype object_r guix_daemon_socket_t) |
| 59 | (typeattributeset file_type guix_daemon_socket_t) |
| 60 | (type guix_store_content_t) |
| 61 | (roletype object_r guix_store_content_t) |
| 62 | (typeattributeset file_type guix_store_content_t) |
| 63 | (type guix_profiles_t) |
| 64 | (roletype object_r guix_profiles_t) |
| 65 | (typeattributeset file_type guix_profiles_t) |
| 66 | |
| 67 | ;; These types are domains, thereby allowing process rules |
| 68 | (typeattributeset domain (guix_daemon_t guix_daemon_exec_t)) |
| 69 | |
| 70 | (level low (s0)) |
| 71 | |
| 72 | ;; When a process in init_t or guix_store_content_t spawns a |
| 73 | ;; guix_daemon_exec_t process, let it run in the guix_daemon_t context |
| 74 | (typetransition init_t guix_daemon_exec_t |
| 75 | process guix_daemon_t) |
| 76 | (typetransition guix_store_content_t guix_daemon_exec_t |
| 77 | process guix_daemon_t) |
| 78 | |
| 79 | (roletype system_r guix_daemon_t) |
| 80 | |
| 81 | ;; allow init_t to read and execute guix files |
| 82 | (allow init_t |
| 83 | guix_profiles_t |
| 84 | (lnk_file (read))) |
| 85 | (allow init_t |
| 86 | guix_daemon_exec_t |
| 87 | (file (execute))) |
| 88 | (allow init_t |
| 89 | guix_daemon_t |
| 90 | (process (transition))) |
| 91 | (allow init_t |
| 92 | guix_store_content_t |
| 93 | (lnk_file (read))) |
| 94 | (allow init_t |
| 95 | guix_store_content_t |
| 96 | (file (open read execute))) |
| 97 | (allow init_t |
| 98 | guix_profiles_t |
| 99 | (dir (setattr))) |
| 100 | |
| 101 | ;; guix-daemon needs to know the names of users |
| 102 | (allow guix_daemon_t |
| 103 | passwd_file_t |
| 104 | (file (getattr open read))) |
| 105 | |
| 106 | ;; Permit communication with NSCD |
| 107 | (allow guix_daemon_t |
| 108 | nscd_var_run_t |
| 109 | (file (map read))) |
| 110 | (allow guix_daemon_t |
| 111 | nscd_var_run_t |
| 112 | (dir (search))) |
| 113 | (allow guix_daemon_t |
| 114 | nscd_var_run_t |
| 115 | (sock_file (write))) |
| 116 | (allow guix_daemon_t |
| 117 | nscd_t |
| 118 | (fd (use))) |
| 119 | (allow guix_daemon_t |
| 120 | nscd_t |
| 121 | (unix_stream_socket (connectto))) |
| 122 | (allow guix_daemon_t nscd_t |
| 123 | (nscd (getgrp gethost getpwd getserv shmemgrp shmemhost shmempwd shmemserv))) |
| 124 | |
| 125 | ;; permit downloading packages via HTTP(s) |
| 126 | (allow guix_daemon_t http_port_t |
| 127 | (tcp_socket (name_connect))) |
| 128 | (allow guix_daemon_t ftp_port_t |
| 129 | (tcp_socket (name_connect))) |
| 130 | (allow guix_daemon_t ephemeral_port_t |
| 131 | (tcp_socket (name_connect))) |
| 132 | |
| 133 | ;; Permit logging and temp file access |
| 134 | (allow guix_daemon_t |
| 135 | tmp_t |
| 136 | (lnk_file (create rename setattr unlink))) |
| 137 | (allow guix_daemon_t |
| 138 | tmp_t |
| 139 | (file (link |
| 140 | rename create execute execute_no_trans write |
| 141 | unlink setattr map relabelto relabelfrom))) |
| 142 | (allow guix_daemon_t |
| 143 | tmp_t |
| 144 | (fifo_file (open read write create getattr ioctl setattr unlink))) |
| 145 | (allow guix_daemon_t |
| 146 | tmp_t |
| 147 | (dir (create rename |
| 148 | rmdir relabelto relabelfrom reparent |
| 149 | add_name remove_name |
| 150 | open read write |
| 151 | getattr setattr |
| 152 | search))) |
| 153 | (allow guix_daemon_t |
| 154 | tmp_t |
| 155 | (sock_file (create getattr setattr unlink write))) |
| 156 | (allow guix_daemon_t |
| 157 | var_log_t |
| 158 | (file (create getattr open write))) |
| 159 | (allow guix_daemon_t |
| 160 | var_log_t |
| 161 | (dir (getattr create write add_name))) |
| 162 | (allow guix_daemon_t |
| 163 | var_run_t |
| 164 | (lnk_file (read))) |
| 165 | (allow guix_daemon_t |
| 166 | var_run_t |
| 167 | (dir (search))) |
| 168 | |
| 169 | ;; Spawning processes, execute helpers |
| 170 | (allow guix_daemon_t |
| 171 | self |
| 172 | (process (fork execmem setrlimit setpgid setsched))) |
| 173 | (allow guix_daemon_t |
| 174 | guix_daemon_exec_t |
| 175 | (file (execute |
| 176 | execute_no_trans read write open entrypoint map |
| 177 | getattr link unlink))) |
| 178 | ;; Needed to execute the 'newgidmap' helper. |
| 179 | (allow guix_daemon_t |
| 180 | bin_t |
| 181 | (file (execute execute_no_trans map))) |
| 182 | |
| 183 | ;; Remounting /gnu/store read-write. |
| 184 | (allow guix_daemon_t |
| 185 | fs_t |
| 186 | (filesystem (remount))) |
| 187 | |
| 188 | ;; TODO: unknown |
| 189 | (allow guix_daemon_t |
| 190 | root_t |
| 191 | (dir (mounton))) |
| 192 | (allow init_t |
| 193 | guix_daemon.guix_store_content_t |
| 194 | (dir (mounton))) |
| 195 | (allow guix_daemon_t |
| 196 | fs_t |
| 197 | (filesystem (getattr))) |
| 198 | (allow guix_daemon_conf_t |
| 199 | fs_t |
| 200 | (filesystem (associate))) |
| 201 | |
| 202 | ;; Build isolation |
| 203 | (allow guix_daemon_t |
| 204 | guix_store_content_t |
| 205 | (file (ioctl mounton))) |
| 206 | (allow guix_store_content_t |
| 207 | fs_t |
| 208 | (filesystem (associate))) |
| 209 | (allow guix_daemon_t |
| 210 | guix_store_content_t |
| 211 | (dir (read mounton))) |
| 212 | (allow guix_daemon_t |
| 213 | guix_daemon_t |
| 214 | (capability (net_admin |
| 215 | fsetid fowner |
| 216 | chown setuid setgid |
| 217 | dac_override dac_read_search |
| 218 | sys_chroot |
| 219 | sys_admin))) |
| 220 | (allow guix_daemon_t |
| 221 | fs_t |
| 222 | (filesystem (unmount))) |
| 223 | (allow guix_daemon_t |
| 224 | devpts_t |
| 225 | (dir (search))) |
| 226 | (allow guix_daemon_t |
| 227 | devpts_t |
| 228 | (filesystem (mount))) |
| 229 | (allow guix_daemon_t |
| 230 | devpts_t |
| 231 | (chr_file (ioctl open read write setattr getattr))) |
| 232 | (allow guix_daemon_t |
| 233 | tmpfs_t |
| 234 | (filesystem (getattr mount))) |
| 235 | (allow guix_daemon_t |
| 236 | tmpfs_t |
| 237 | (file (create open read unlink write))) |
| 238 | (allow guix_daemon_t ;same as above, but with tmp_t |
| 239 | tmp_t |
| 240 | (file (create open read unlink write))) |
| 241 | (allow guix_daemon_t |
| 242 | tmpfs_t |
| 243 | (dir (getattr add_name remove_name write))) |
| 244 | (allow guix_daemon_t |
| 245 | proc_t |
| 246 | (file (getattr open read))) |
| 247 | (allow guix_daemon_t |
| 248 | proc_t |
| 249 | (dir (read))) |
| 250 | (allow guix_daemon_t |
| 251 | proc_t |
| 252 | (filesystem (associate mount))) |
| 253 | (allow guix_daemon_t |
| 254 | null_device_t |
| 255 | (chr_file (getattr open read write))) |
| 256 | (allow guix_daemon_t |
| 257 | kvm_device_t |
| 258 | (chr_file (getattr))) |
| 259 | (allow guix_daemon_t |
| 260 | zero_device_t |
| 261 | (chr_file (getattr))) |
| 262 | (allow guix_daemon_t |
| 263 | urandom_device_t |
| 264 | (chr_file (getattr))) |
| 265 | (allow guix_daemon_t |
| 266 | random_device_t |
| 267 | (chr_file (getattr))) |
| 268 | (allow guix_daemon_t |
| 269 | devtty_t |
| 270 | (chr_file (getattr))) |
| 271 | |
| 272 | ;; Access to store items |
| 273 | (allow guix_daemon_t |
| 274 | guix_store_content_t |
| 275 | (dir (reparent |
| 276 | create |
| 277 | getattr setattr |
| 278 | search rename |
| 279 | add_name remove_name |
| 280 | open write |
| 281 | rmdir relabelfrom))) |
| 282 | (allow guix_daemon_t |
| 283 | guix_store_content_t |
| 284 | (file (create |
| 285 | lock |
| 286 | setattr getattr |
| 287 | execute execute_no_trans |
| 288 | link unlink |
| 289 | map |
| 290 | rename |
| 291 | append |
| 292 | open read write relabelfrom))) |
| 293 | (allow guix_daemon_t |
| 294 | guix_store_content_t |
| 295 | (lnk_file (create |
| 296 | getattr setattr |
| 297 | link unlink |
| 298 | read |
| 299 | rename))) |
| 300 | (allow guix_daemon_t |
| 301 | guix_store_content_t |
| 302 | (fifo_file (create getattr open read unlink write))) |
| 303 | (allow guix_daemon_t |
| 304 | guix_store_content_t |
| 305 | (sock_file (create getattr setattr unlink write))) |
| 306 | |
| 307 | ;; Access to run state directories |
| 308 | (allow guix_daemon_t |
| 309 | system_dbusd_var_run_t |
| 310 | (dir (search))) |
| 311 | (allow guix_daemon_t |
| 312 | init_var_run_t |
| 313 | (dir (search))) |
| 314 | |
| 315 | ;; Access to configuration files and directories |
| 316 | (allow guix_daemon_t |
| 317 | guix_daemon_conf_t |
| 318 | (dir (search create |
| 319 | setattr getattr |
| 320 | add_name remove_name |
| 321 | open read write))) |
| 322 | (allow guix_daemon_t |
| 323 | guix_daemon_conf_t |
| 324 | (file (create rename |
| 325 | lock |
| 326 | map |
| 327 | getattr setattr |
| 328 | unlink |
| 329 | open read write append))) |
| 330 | (allow guix_daemon_t |
| 331 | guix_daemon_conf_t |
| 332 | (lnk_file (create getattr rename unlink read))) |
| 333 | (allow guix_daemon_t net_conf_t |
| 334 | (file (getattr open read))) |
| 335 | (allow guix_daemon_t net_conf_t |
| 336 | (lnk_file (read))) |
| 337 | (allow guix_daemon_t NetworkManager_var_run_t |
| 338 | (dir (search))) |
| 339 | |
| 340 | ;; Access to profiles |
| 341 | (allow guix_daemon_t |
| 342 | guix_profiles_t |
| 343 | (dir (search getattr setattr read write open create add_name))) |
| 344 | (allow guix_daemon_t |
| 345 | guix_profiles_t |
| 346 | (lnk_file (read getattr))) |
| 347 | |
| 348 | ;; Access to profile links in the home directory |
| 349 | ;; TODO: allow access to profile links *anywhere* on the filesystem |
| 350 | (allow guix_daemon_t |
| 351 | user_home_t |
| 352 | (lnk_file (read getattr))) |
| 353 | (allow guix_daemon_t |
| 354 | user_home_t |
| 355 | (dir (search))) |
| 356 | (allow guix_daemon_t |
| 357 | cache_home_t |
| 358 | (dir (search))) |
| 359 | (allow guix_daemon_t |
| 360 | cache_home_t |
| 361 | (lnk_file (getattr read))) |
| 362 | |
| 363 | ;; self upgrades |
| 364 | (allow guix_daemon_t |
| 365 | self |
| 366 | (dir (add_name write))) |
| 367 | (allow guix_daemon_t |
| 368 | self |
| 369 | (netlink_route_socket (bind create getattr nlmsg_read read write getopt))) |
| 370 | |
| 371 | ;; Allow use of user namespaces |
| 372 | (allow guix_daemon_t |
| 373 | self |
| 374 | (cap_userns (setgid sys_admin net_admin sys_chroot))) |
| 375 | (allow guix_daemon_t |
| 376 | self |
| 377 | (user_namespace (create))) |
| 378 | |
| 379 | ;; Socket operations |
| 380 | (allow guix_daemon_t |
| 381 | guix_daemon_socket_t |
| 382 | (sock_file (unlink write))) |
| 383 | (allow guix_daemon_t |
| 384 | init_t |
| 385 | (fd (use))) |
| 386 | (allow guix_daemon_t |
| 387 | init_t |
| 388 | (unix_stream_socket (write))) |
| 389 | (allow guix_daemon_t |
| 390 | guix_daemon_conf_t |
| 391 | (unix_stream_socket (listen))) |
| 392 | (allow guix_daemon_t |
| 393 | guix_daemon_conf_t |
| 394 | (sock_file (create unlink write))) |
| 395 | (allow guix_daemon_t |
| 396 | self |
| 397 | (unix_stream_socket (create |
| 398 | read write |
| 399 | connect bind accept |
| 400 | getopt setopt))) |
| 401 | (allow guix_daemon_t |
| 402 | self |
| 403 | (tcp_socket (accept listen bind connect create setopt getopt getattr ioctl read write shutdown))) |
| 404 | (allow guix_daemon_t |
| 405 | unreserved_port_t |
| 406 | (tcp_socket (name_bind name_connect accept listen))) |
| 407 | (allow guix_daemon_t |
| 408 | self |
| 409 | (udp_socket (connect getattr bind getopt setopt read write))) |
| 410 | (allow guix_daemon_t |
| 411 | self |
| 412 | (fifo_file (write read))) |
| 413 | (allow guix_daemon_t |
| 414 | self |
| 415 | (udp_socket (ioctl create))) |
| 416 | (allow guix_daemon_t |
| 417 | self |
| 418 | (unix_stream_socket (connectto))) |
| 419 | (allow guix_daemon_t |
| 420 | self |
| 421 | (unix_dgram_socket (create bind connect sendto read write))) |
| 422 | |
| 423 | ;; For some esoteric build jobs (i.e. running PostgreSQL, etc). |
| 424 | (allow guix_daemon_t |
| 425 | self |
| 426 | (capability (kill))) |
| 427 | (allow guix_daemon_t |
| 428 | node_t |
| 429 | (tcp_socket (node_bind))) |
| 430 | (allow guix_daemon_t |
| 431 | node_t |
| 432 | (udp_socket (node_bind))) |
| 433 | (allow guix_daemon_t |
| 434 | port_t |
| 435 | (tcp_socket (name_connect))) |
| 436 | (allow guix_daemon_t |
| 437 | tmpfs_t |
| 438 | (file (map read write link getattr))) |
| 439 | (allow guix_daemon_t |
| 440 | usermodehelper_t |
| 441 | (file (read))) |
| 442 | (allow guix_daemon_t |
| 443 | hugetlbfs_t |
| 444 | (file (map read write))) |
| 445 | (allow guix_daemon_t |
| 446 | proc_net_t |
| 447 | (file (read))) |
| 448 | (allow guix_daemon_t |
| 449 | postgresql_port_t |
| 450 | (tcp_socket (name_connect name_bind))) |
| 451 | (allow guix_daemon_t |
| 452 | rtp_media_port_t |
| 453 | (udp_socket (name_bind))) |
| 454 | (allow guix_daemon_t |
| 455 | vnc_port_t |
| 456 | (tcp_socket (name_bind))) |
| 457 | |
| 458 | ;; 'guix gc' needs to go through /proc entries for all processes that are |
| 459 | ;; running. Strictly speaking, it means guix-daemon needs access to all |
| 460 | ;; process types in the SELinux policy. In practice, only processes from |
| 461 | ;; programs in the /gnu/store are relevant for finding roots for garbage |
| 462 | ;; collection. Since Guix currently doesn't install any SELinux policy for |
| 463 | ;; its packages, we can assume that all the processes it needs to access run |
| 464 | ;; as unconfined_t. |
| 465 | ;; |
| 466 | ;; FIXME: This doesn't stop 'guix gc' from generating a lot of unnecessary |
| 467 | ;; AVC denied audit messages. Perhaps guix-daemon could test whether it has |
| 468 | ;; access to the proc entry before trying to access it? |
| 469 | (allow guix_daemon_t |
| 470 | unconfined_t |
| 471 | (dir (search))) |
| 472 | (allow guix_daemon_t |
| 473 | unconfined_t |
| 474 | (file (read))) |
| 475 | (allow guix_daemon_t |
| 476 | unconfined_t |
| 477 | (lnk_file (read))) |
| 478 | |
| 479 | ;; I guess sometimes it needs random numbers |
| 480 | (allow guix_daemon_t |
| 481 | random_device_t |
| 482 | (chr_file (read))) |
| 483 | |
| 484 | ;; guix system vm |
| 485 | (allow guix_daemon_t |
| 486 | kvm_device_t |
| 487 | (chr_file (ioctl open read write))) |
| 488 | (allow guix_daemon_t |
| 489 | kernel_t |
| 490 | (system (ipc_info))) |
| 491 | |
| 492 | ;; Label file system |
| 493 | (filecon "@guix_sysconfdir@/guix(/.*)?" |
| 494 | any (system_u object_r guix_daemon_conf_t (low low))) |
| 495 | (filecon "@guix_localstatedir@/guix(/.*)?" |
| 496 | any (system_u object_r guix_daemon_conf_t (low low))) |
| 497 | (filecon "@guix_localstatedir@/guix/profiles(/.*)?" |
| 498 | any (system_u object_r guix_profiles_t (low low))) |
| 499 | (filecon "/gnu" |
| 500 | dir (unconfined_u object_r guix_store_content_t (low low))) |
| 501 | (filecon "@storedir@(/.+)?" |
| 502 | any (unconfined_u object_r guix_store_content_t (low low))) |
| 503 | (filecon "@storedir@/[^/]+/.+" |
| 504 | any (unconfined_u object_r guix_store_content_t (low low))) |
| 505 | (filecon "@prefix@/bin/guix-daemon" |
| 506 | file (system_u object_r guix_daemon_exec_t (low low))) |
| 507 | (filecon "@guix_localstatedir@/guix/profiles/per-user/[^/]+/current-guix/bin/guix-daemon" |
| 508 | file (system_u object_r guix_daemon_exec_t (low low))) |
| 509 | (filecon "@storedir@/.+-(guix-.+|profile)/bin/guix-daemon" |
| 510 | file (system_u object_r guix_daemon_exec_t (low low))) |
| 511 | (filecon "@storedir@/[a-z0-9]+-guix-daemon" |
| 512 | file (system_u object_r guix_daemon_exec_t (low low))) |
| 513 | (filecon "@guix_localstatedir@/guix/daemon-socket/socket" |
| 514 | any (system_u object_r guix_daemon_socket_t (low low)))) |