GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — vm2api/scripts/gemini_host_apparmor.py at dev · dofastted/vm2api

Expand file tree

/

gemini_host_apparmor.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
164 lines (139 loc) · 5.24 KB
/

gemini_host_apparmor.py

Copy path

File metadata and controls

164 lines (139 loc) · 5.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Shared Gemini host AppArmor userns helpers and managed bwrap entry resolution."""
from __future__ import annotations
import errno
import os
import shutil
import stat
import tempfile
from pathlib import Path
APPARMOR_MARKER = "# Managed by vm2api Gemini host userns preparation."
APPARMOR_PROFILE_NAME = "vm2api-gemini"
GEMINI_RUNTIME_DIR = Path("/usr/local/lib/vm2api-gemini")
GEMINI_BWRAP_ENTRY = GEMINI_RUNTIME_DIR / "exec" / "bwrap"
BWRAP_USERNS_RESTRICT_STAGED = Path("/etc/apparmor.d/bwrap-userns-restrict")
BWRAP_ENV_OVERRIDE = "VM2API_GEMINI_BWRAP"
HOST_ROOT_OVERRIDE = "VM2API_GEMINI_HOST_ROOT"
def active_host_root() -> Path:
override = os.environ.get(HOST_ROOT_OVERRIDE, "").strip()
if override:
return Path(override)
return Path("/")
def host_path(host_root: Path, relative: str | Path) -> Path:
rel = Path(relative)
parts = rel.parts
if rel.is_absolute():
parts = parts[1:] if parts and parts[0] == "/" else parts
return host_root.joinpath(*parts)
def _read_flag(path: Path, accepted: frozenset[str]) -> bool:
try:
value = path.read_text(encoding="utf-8", errors="replace").strip()
except OSError:
return False
return value in accepted
def apparmor_userns_restricted(*, host_root: Path = Path("/")) -> bool:
enabled = host_path(host_root, "/sys/module/apparmor/parameters/enabled")
restricted = host_path(
host_root, "/proc/sys/kernel/apparmor_restrict_unprivileged_userns"
)
return _read_flag(enabled, frozenset({"Y", "1", "yes"})) and _read_flag(
restricted, frozenset({"1"})
)
def bwrap_userns_mediated(*, host_root: Path = Path("/")) -> bool:
staged = host_path(host_root, BWRAP_USERNS_RESTRICT_STAGED)
if not staged.is_file():
return False
profiles = host_path(host_root, "/sys/kernel/security/apparmor/profiles")
if profiles.is_file():
try:
raw = profiles.read_bytes()[: 2 * 1024 * 1024]
except OSError:
return True
if "bwrap" not in raw.decode("utf-8", errors="replace"):
return False
return True
def managed_bwrap_required(*, host_root: Path = Path("/")) -> bool:
return apparmor_userns_restricted(host_root=host_root) and bwrap_userns_mediated(
host_root=host_root
)
def resolve_bwrap_argv0(*, host_root: Path = Path("/")) -> str:
override = os.environ.get(BWRAP_ENV_OVERRIDE, "").strip()
if override:
candidate = Path(override)
if candidate.is_file() and os.access(candidate, os.X_OK):
return str(candidate)
entry = host_path(host_root, GEMINI_BWRAP_ENTRY)
if entry.is_file() and os.access(entry, os.X_OK):
return str(entry)
found = shutil.which("bwrap")
if found:
return found
return "bwrap"
def apparmor_profile_bytes(*, bwrap_entry: Path | None = None) -> bytes:
entry_rule = ""
if bwrap_entry is not None:
entry_rule = f" {bwrap_entry} ix,\n"
text = (
f"{APPARMOR_MARKER}\n"
"abi <abi/4.0>,\n"
f"profile {APPARMOR_PROFILE_NAME} flags=(unconfined) {{\n"
" userns,\n"
f"{entry_rule}"
"}\n"
)
return text.encode("ascii")
def managed_bwrap_entry_configured(*, host_root: Path = Path("/")) -> bool:
if not managed_bwrap_required(host_root=host_root):
return True
entry = host_path(host_root, GEMINI_BWRAP_ENTRY)
if not entry.is_file():
return False
profile = host_path(host_root, f"/etc/apparmor.d/{APPARMOR_PROFILE_NAME}")
if not profile.is_file():
return False
try:
body = profile.read_text(encoding="utf-8", errors="replace")
except OSError:
return False
return f" {entry} ix," in body or f"{entry} ix," in body
def _validate_managed_bwrap_entry(entry: Path) -> None:
info = entry.stat()
if not stat.S_ISREG(info.st_mode):
raise OSError("managed bwrap entry is not a regular file")
if info.st_mode & 0o022:
raise OSError("managed bwrap entry is group/world writable")
def install_managed_bwrap(*, host_root: Path = Path("/")) -> Path:
entry = host_path(host_root, GEMINI_BWRAP_ENTRY)
source = shutil.which("bwrap")
if not source:
raise FileNotFoundError("bwrap")
entry.parent.mkdir(parents=True, mode=0o755, exist_ok=True)
desired = Path(source).read_bytes()
if entry.is_file():
try:
if entry.read_bytes() == desired:
os.chmod(entry, stat.S_IMODE(0o755))
_validate_managed_bwrap_entry(entry)
return entry
except OSError:
pass
fd, tmp_name = tempfile.mkstemp(
dir=entry.parent, prefix=".bwrap-install-", suffix=".tmp"
)
tmp_path = Path(tmp_name)
try:
with os.fdopen(fd, "wb") as handle:
handle.write(desired)
os.chmod(tmp_path, stat.S_IMODE(0o755))
os.replace(tmp_path, entry)
with open(entry, "rb") as installed:
os.fsync(installed.fileno())
directory = os.open(entry.parent, os.O_RDONLY | os.O_DIRECTORY)
try:
os.fsync(directory)
finally:
os.close(directory)
except BaseException:
tmp_path.unlink(missing_ok=True)
raise
_validate_managed_bwrap_entry(entry)
return entry
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.