|
114 | 114 | workdir,
|
115 | 115 | )
|
116 | 116 | from mkosi.sandbox import (
|
| 117 | + CAP_SYS_ADMIN, |
117 | 118 | CLONE_NEWNS,
|
118 | 119 | MOUNT_ATTR_NODEV,
|
119 | 120 | MOUNT_ATTR_NOEXEC,
|
|
123 | 124 | MS_SLAVE,
|
124 | 125 | __version__,
|
125 | 126 | acquire_privileges,
|
| 127 | + have_effective_cap, |
126 | 128 | join_new_session_keyring,
|
127 | 129 | mount,
|
128 | 130 | mount_rbind,
|
@@ -4888,39 +4890,38 @@ def run_build(
|
4888 | 4890 | metadata_dir: Path,
|
4889 | 4891 | package_dir: Optional[Path] = None,
|
4890 | 4892 | ) -> None:
|
4891 |
| - if os.getuid() != 0: |
| 4893 | + if not have_effective_cap(CAP_SYS_ADMIN): |
4892 | 4894 | acquire_privileges()
|
4893 |
| - |
4894 |
| - unshare(CLONE_NEWNS) |
4895 |
| - |
4896 |
| - if os.getuid() == 0: |
| 4895 | + unshare(CLONE_NEWNS) |
| 4896 | + else: |
| 4897 | + unshare(CLONE_NEWNS) |
4897 | 4898 | mount("", "/", "", MS_SLAVE | MS_REC, "")
|
4898 | 4899 |
|
4899 |
| - # For extra safety when running as root, remount a bunch of directories read-only unless the output |
4900 |
| - # directory is located in it. |
4901 |
| - if os.getuid() == 0: |
4902 |
| - remount = ["/etc", "/opt", "/boot", "/efi", "/media", "/usr"] |
| 4900 | + # For extra safety when running as root with CAP_SYS_ADMIN, remount a bunch of directories read-only |
| 4901 | + # unless the output directory is located in it. |
| 4902 | + if os.getuid() == 0: |
| 4903 | + remount = ["/etc", "/opt", "/boot", "/efi", "/media", "/usr"] |
4903 | 4904 |
|
4904 |
| - for d in remount: |
4905 |
| - if not Path(d).exists(): |
4906 |
| - continue |
| 4905 | + for d in remount: |
| 4906 | + if not Path(d).exists(): |
| 4907 | + continue |
4907 | 4908 |
|
4908 |
| - if any( |
4909 |
| - p and p.is_relative_to(d) |
4910 |
| - for p in ( |
4911 |
| - config.workspace_dir_or_default(), |
4912 |
| - config.package_cache_dir_or_default(), |
4913 |
| - config.cache_dir, |
4914 |
| - config.output_dir_or_cwd(), |
4915 |
| - ) |
4916 |
| - ): |
4917 |
| - continue |
| 4909 | + if any( |
| 4910 | + p and p.is_relative_to(d) |
| 4911 | + for p in ( |
| 4912 | + config.workspace_dir_or_default(), |
| 4913 | + config.package_cache_dir_or_default(), |
| 4914 | + config.cache_dir, |
| 4915 | + config.output_dir_or_cwd(), |
| 4916 | + ) |
| 4917 | + ): |
| 4918 | + continue |
4918 | 4919 |
|
4919 |
| - attrs = MOUNT_ATTR_RDONLY |
4920 |
| - if d in ("/boot", "/efi"): |
4921 |
| - attrs |= MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC |
| 4920 | + attrs = MOUNT_ATTR_RDONLY |
| 4921 | + if d in ("/boot", "/efi"): |
| 4922 | + attrs |= MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC |
4922 | 4923 |
|
4923 |
| - mount_rbind(d, d, attrs) |
| 4924 | + mount_rbind(d, d, attrs) |
4924 | 4925 |
|
4925 | 4926 | with (
|
4926 | 4927 | complete_step(f"Building {config.image} image"),
|
|
0 commit comments