我正在强化我的 systemd 服务文件以适应 openconnect(8)。在我的设置中,我使用VPN 切片设置路由(我传递参数,使其不写入任何文件)并使用各种文件来定义连接参数和凭据。我的目标系统是 Debian Buster(systemd 版本 241)。
这就是我想出的:
[Unit]
Description=Openconnect VPN to %i
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
EnvironmentFile=-/etc/openconnect/_default.conf
EnvironmentFile=/etc/openconnect/%i.conf
StandardInput=file:/etc/openconnect/%i.pwd
# StandardOutput is changed to `inherit` when setting StandardInput.
StandardOutput=journal
ExecStart=/usr/sbin/openconnect --non-inter --setuid=nobody --passwd-on-stdin --user "${OC_USER}" --script "${OC_SCRIPT}" ${OC_OPTIONS} "${OC_SERVER}"
Restart=always
RestartSec=1s
## security
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
# required to downgrade to `nobody`
CapabilityBoundingSet=CAP_SETGID CAP_SETUID
NoNewPrivileges=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_NETLINK
LockPersonality=true
MemoryDenyWriteExecute=true
#PrivateDevices=yes
#DeviceAllow=/dev/net/tun rwm
#DeviceAllow=char-misc rwm
#ReadWritePaths=/dev/net/
#PrivateUsers=true
PrivateTmp=true
ProtectSystem=strict
ProtectControlGroups=true
ProtectHome=true
ProtectHostname=true
ProtectKernelLogs=true
ProtectKernelModules=true
ProtectKernelTunables=true
ReadWritePaths=/proc/sys/net/ipv4/route/flush
RestrictNamespaces=true
RestrictRealtime=true
SystemCallArchitectures=native
SystemCallFilter=@system-service
SystemCallFilter=~@privileged @resources
SystemCallFilter=@setuid
RemoveIPC=true
[Install]
WantedBy=multi-user.target
基本上,我发现我当前的单位定义存在两个问题:
- 我无法运行
PrivateDevices
。当激活DeviceAllow
及ReadWritePaths
以上时,该单元会提前失败:
[email protected]: Failed to set up mount namespacing: No such file or directory
[email protected]: Failed at step NAMESPACE spawning /usr/sbin/openconnect: No such file or directory
当我省略时ReadWritePaths
,vpn-slice
脚本会失败:
Traceback (most recent call last):
File "/usr/bin/vpn-slice", line 11, in <module>
load_entry_point('vpn-slice==0.15', 'console_scripts', 'vpn-slice')()
File "/usr/lib/python3/dist-packages/vpn_slice/__main__.py", line 592, in main
do_pre_init(env, args)
File "/usr/lib/python3/dist-packages/vpn_slice/__main__.py", line 112, in do_pre_init
providers.prep.create_tunnel()
File "/usr/lib/python3/dist-packages/vpn_slice/linux.py", line 106, in create_tunnel
os.makedirs(os.path.dirname(node), exist_ok=True)
File "/usr/lib/python3.7/os.py", line 221, in makedirs
mkdir(name, mode)
OSError: [Errno 30] Read-only file system: '/dev/net'
- 现在我正在传递
--setuid=nobody
给openconnect
。但是,当我激活PrivateUsers=true
openconnect 时失败:
Failed to bind local tun device (TUNSETIFF): Operation not permitted
To configure local networking, openconnect must be running as root
因此,我首先想知道这是否是最好的方法,并尝试以 的方式运行整个单元User=nobody
,但也无法使其工作(我尝试设置AmbientCapabilities=
)。
由于需要几个权限,我还想知道在具有上述所有限制的情况下,--setuid
让它正式运行是否最终会更安全。root
非常感谢任何有关更安全设置的建议。