mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 04:16:24 -05:00
VPN: read only the VPN files the Controller writes (#1037)
* VPN: read only the VPN files the Controller writes controller.yaml's vpn.config_file and vpn.auth_file were joined to the config directory as-is. The tunnel runs as root, so an absolute or ../ path would have it read (and send to the VPN server) any file, and the Controller API would return any file's text as the VPN config. Both now accept only vpn/wireguard.conf, vpn/openvpn.ovpn, and vpn/openvpn-auth.txt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * VPN: refuse a VPN file which links outside the vpn directory Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * VPN: read each VPN file once, never through a symlink The check resolved the path, then the file was opened again later: by root OpenVPN for the login (--auth-nocache re-reads it). The wrolpi user, who owns the library, could swap the file for a symlink in between. read_vpn_file() opens with O_NOFOLLOW and reads once. OpenVPN gets a 0600 copy of the login in the run directory, removed when the tunnel goes down. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
04ddc7c63c
commit
b1334c834b
4 changed files with 156 additions and 16 deletions
|
|
@ -19,6 +19,7 @@ Controller imports it to share the parsing and the defaults.
|
|||
"""
|
||||
import argparse
|
||||
import dataclasses
|
||||
import errno
|
||||
import hashlib
|
||||
import ipaddress
|
||||
import json
|
||||
|
|
@ -56,6 +57,13 @@ VPN_DEFAULTS = {
|
|||
# Fetched through the tunnel to show the address peers see. None disables the check.
|
||||
'public_ip_url': 'https://api.ipify.org',
|
||||
}
|
||||
# The only VPN files the Controller writes, relative to the config directory. The tunnel runs as root, so the
|
||||
# `config_file` and `auth_file` settings may name no other file.
|
||||
WIREGUARD_CONFIG = 'vpn/wireguard.conf'
|
||||
OPENVPN_CONFIG = 'vpn/openvpn.ovpn'
|
||||
OPENVPN_AUTH = 'vpn/openvpn-auth.txt'
|
||||
CONFIG_FILES = {'wireguard': WIREGUARD_CONFIG, 'openvpn': OPENVPN_CONFIG}
|
||||
|
||||
PORT_FORWARDING_MODES = ('none', 'fixed', 'natpmp')
|
||||
|
||||
TUNNEL_INTERFACE = 'vpn0'
|
||||
|
|
@ -73,6 +81,7 @@ STATUS_FILE = 'status.json'
|
|||
WROL_MODE_FLAG = '.wrol_mode'
|
||||
ENDPOINTS_FILE = 'endpoints.json'
|
||||
OPENVPN_CONFIG_FILE = 'openvpn.ovpn'
|
||||
OPENVPN_AUTH_FILE = 'openvpn-auth.txt'
|
||||
|
||||
LOOP_SECONDS = 15
|
||||
# WireGuard re-handshakes every two minutes while traffic flows.
|
||||
|
|
@ -808,7 +817,8 @@ class Plan:
|
|||
"""What the tunnel should be, from the settings and the config."""
|
||||
kind: str
|
||||
config_path: pathlib.Path
|
||||
auth_path: pathlib.Path | None
|
||||
# OpenVPN's username and password lines, or None.
|
||||
auth_text: str | None
|
||||
endpoints: list[Endpoint]
|
||||
hosts: dict[str, list[str]]
|
||||
settings: dict
|
||||
|
|
@ -818,6 +828,24 @@ class Plan:
|
|||
openvpn_dns: list[str] = dataclasses.field(default_factory=list)
|
||||
|
||||
|
||||
def read_vpn_file(config_directory: pathlib.Path, name: str, allowed) -> str:
|
||||
"""The text of a VPN file named in controller.yaml, which must be one of the `allowed` files WROLPi writes.
|
||||
|
||||
Read once, and never through a symlink: the library belongs to the wrolpi user, who could re-point one after it
|
||||
was checked. (The config and vpn directories may be symlinks.) Raises FileNotFoundError when it is missing."""
|
||||
if name not in allowed:
|
||||
raise VPNConfigError(f'{name} is not a VPN file WROLPi writes')
|
||||
path = config_directory / name
|
||||
try:
|
||||
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
|
||||
except OSError as e:
|
||||
if e.errno == errno.ELOOP:
|
||||
raise VPNConfigError(f'{name} is a symlink, not a VPN file WROLPi writes')
|
||||
raise
|
||||
with os.fdopen(fd) as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def make_plan(settings: dict, config_directory: pathlib.Path, run_dir: pathlib.Path) -> Plan | None:
|
||||
"""None when the VPN is disabled."""
|
||||
if not settings.get('enabled'):
|
||||
|
|
@ -829,11 +857,11 @@ def make_plan(settings: dict, config_directory: pathlib.Path, run_dir: pathlib.P
|
|||
config_path = config_directory / settings['config_file']
|
||||
kind = config_kind(config_path.name)
|
||||
try:
|
||||
text = config_path.read_text()
|
||||
text = read_vpn_file(config_directory, settings['config_file'], CONFIG_FILES.values())
|
||||
except FileNotFoundError:
|
||||
raise VPNConfigError(f'The VPN config {settings["config_file"]} is missing')
|
||||
hosts = read_endpoint_hosts(run_dir, text)
|
||||
auth_path, auth_text = None, ''
|
||||
auth_text = None
|
||||
if kind == 'wireguard':
|
||||
wireguard = parse_wireguard(text)
|
||||
endpoints = endpoints_of(wireguard.remotes(), hosts)
|
||||
|
|
@ -843,12 +871,14 @@ def make_plan(settings: dict, config_directory: pathlib.Path, run_dir: pathlib.P
|
|||
endpoints = endpoints_of(openvpn.remotes, hosts)
|
||||
plan_kwargs = {'openvpn_text': sanitize_openvpn(openvpn, hosts), 'openvpn_dns': openvpn.dns}
|
||||
if openvpn.wants_auth:
|
||||
if not settings.get('auth_file') or not (config_directory / settings['auth_file']).is_file():
|
||||
if not settings.get('auth_file'):
|
||||
raise VPNConfigError('This OpenVPN config needs a username and password')
|
||||
auth_path = config_directory / settings['auth_file']
|
||||
auth_text = auth_path.read_text()
|
||||
fingerprint = config_sha256(json.dumps([settings, text, auth_text, hosts], sort_keys=True, default=str))
|
||||
return Plan(kind, config_path, auth_path, endpoints, hosts, settings, fingerprint, **plan_kwargs)
|
||||
try:
|
||||
auth_text = read_vpn_file(config_directory, settings['auth_file'], (OPENVPN_AUTH,))
|
||||
except FileNotFoundError:
|
||||
raise VPNConfigError('This OpenVPN config needs a username and password')
|
||||
fingerprint = config_sha256(json.dumps([settings, text, auth_text or '', hosts], sort_keys=True, default=str))
|
||||
return Plan(kind, config_path, auth_text, endpoints, hosts, settings, fingerprint, **plan_kwargs)
|
||||
|
||||
|
||||
class Tunnel:
|
||||
|
|
@ -986,8 +1016,11 @@ class Tunnel:
|
|||
cmd = ['openvpn', '--config', str(config), '--script-security', '1', '--dev', TUNNEL_INTERFACE,
|
||||
'--dev-type', 'tun', '--disable-dco', '--route-noexec', '--persist-tun', '--persist-key',
|
||||
'--auth-nocache', '--verb', '3']
|
||||
if plan.auth_path:
|
||||
cmd.extend(['--auth-user-pass', str(plan.auth_path)])
|
||||
if plan.auth_text is not None:
|
||||
# A copy only root can read: OpenVPN re-reads it (--auth-nocache), and never opens the library's.
|
||||
auth = self.run_dir / OPENVPN_AUTH_FILE
|
||||
write_private(auth, plan.auth_text)
|
||||
cmd.extend(['--auth-user-pass', str(auth)])
|
||||
self.openvpn = OpenVPNProcess(cmd)
|
||||
deadline = time.monotonic() + OPENVPN_CONNECT_SECONDS
|
||||
while not self.openvpn.connected.wait(1):
|
||||
|
|
@ -1002,6 +1035,8 @@ class Tunnel:
|
|||
if self.openvpn:
|
||||
self.openvpn.stop()
|
||||
self.openvpn = None
|
||||
# The login's copy is kept only while OpenVPN may need it.
|
||||
(self.run_dir / OPENVPN_AUTH_FILE).unlink(missing_ok=True)
|
||||
if self.plan or self.routes:
|
||||
run_quietly('ip', 'link', 'del', TUNNEL_INTERFACE)
|
||||
for family, destination in self.routes:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue