[PR #6718] [MERGED] fix(systemd): harden shipped x-ui unit files #6430

Closed
opened 2026-11-02 04:08:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6718
Author: @ChesterFishmansWork
Created: 10/2/2026
Status: ✅ Merged
Merged: 10/5/2026
Merged by: @MHSanaei

Base: main ← Head: fix/6605-systemd-hardening


📝 Commits (9)

  • faf1e01 fix(systemd): harden shipped x-ui unit files
  • 8b8c8b5 fix(systemd): ship ReadWriteDirectories= alias for systemd < 231
  • a8d3b71 fix(systemd): keep root's DAC bits and regenerate the write paths
  • 11f22c9 fix(systemd): keep the seccomp whitelist off old systemd, tighten the rest
  • 72c24b9 fix(systemd): actually wire the read-only guard, drop the superseded drop-in
  • 3456c11 fix(systemd): correct two comments and keep spaces out of the path list
  • 3536575 fix(systemd): act on the independent review of the drop-in generator
  • 8f2b21d Merge branch 'main' into fix/6605-systemd-hardening
  • 395b6dc fix(systemd): name the hardening old systemd really ignores

📊 Changes

6 files changed (+582 additions, -0 deletions)

View changed files

📝 install.sh (+168 -0)
📝 update.sh (+180 -0)
📝 x-ui.service.arch (+76 -0)
📝 x-ui.service.debian (+76 -0)
📝 x-ui.service.rhel (+76 -0)
📝 x-ui.sh (+6 -0)

📄 Description

Problem

The shipped systemd units run the panel as root with no sandboxing at all; systemd-analyze security rates the unit 9.6 UNSAFE (fixes #6605).

Change

Adds a hardening block to x-ui.service.debian, x-ui.service.arch and x-ui.service.rhel (identical except for the pre-existing EnvironmentFile=). The panel intentionally stays root — it supervises the Xray children, edits netfilter state for the IP-limit feature and reads TLS private keys — so the settings only bound what a panel-level flaw can reach.

Directive Why it is safe here
NoNewPrivileges=yes Nothing in the panel or its children relies on setuid escalation.
ProtectSystem=full /usr and /boot become read-only; the panel only needs write access to its own stores, listed in ReadWritePaths below.
ReadWritePaths=-/etc/x-ui -/usr/local/x-ui -/var/log/x-ui Defaults for XUI_DB_FOLDER (DB, update status, node token), XUI_BIN_FOLDER (Xray binary/config/geo) and XUI_LOG_FOLDER. The leading - keeps the unit startable when a path does not exist yet.
ProtectKernelTunables=yes, ProtectKernelModules=yes, ProtectKernelLogs=yes, ProtectClock=yes, ProtectHostname=yes, LockPersonality=yes, RestrictRealtime=yes, RestrictSUIDSGID=yes, RestrictNamespaces=yes Not used by the panel; namespaces are the panel's own concern (Docker/custom installs run the panel, not its children, through these).
UMask=0077 DB and TLS material are created owner-only; the updater writes 0755 itself where needed.
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX AF_NETLINK Covers the listener, outbound node/gRPC traffic, DNS and netlink lookups (plus the ip(8) child used by the AmneziaWG IPv6-alias feature).
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW NET_ADMIN for ip(8)/tproxy and the IP-limit netfilter rules, NET_BIND_SERVICE for ports below 1024, NET_RAW for raw sockets and SO_BINDTODEVICE.
SystemCallArchitectures=native, SystemCallFilter=@system-service, SystemCallErrorNumber=EPERM The panel and Go runtime stay inside @system-service; violations return EPERM instead of killing the process, so a missed syscall degrades one operation rather than the whole panel.

Deliberately absent:

  • PrivateTmp=yes — the web updater writes its script under /tmp and hands the absolute path to a systemd-run transient unit, which does not share this service's private /tmp; the download would vanish and updates would break.
  • MemoryDenyWriteExecute=yes — Xray/Go may map writable-executable memory; too risky for a proxy panel.
  • ProtectHome=yes — kept as read-only instead: installs keep TLS certificates under /root/cert and the panel must read them.

Notes

  • install.sh and update.sh do not embed the unit — they copy it from the release tarball or fetch it from raw.githubusercontent.com — so no script changes are needed.
  • systemd-analyze is not available in the environment this was prepared in, so the numeric rating is not re-measured here; the directive-by-directive reasoning above is checked against the panel's code paths, not executed. Worth running systemd-analyze security x-ui.service on a test host before merging.
  • Installs that relocate XUI_DB_FOLDER/XUI_BIN_FOLDER/XUI_LOG_FOLDER outside the three default paths must add them to ReadWritePaths — that is called out in a comment next to the directive.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6718 **Author:** [@ChesterFishmansWork](https://github.com/ChesterFishmansWork) **Created:** 10/2/2026 **Status:** ✅ Merged **Merged:** 10/5/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/6605-systemd-hardening` --- ### 📝 Commits (9) - [`faf1e01`](https://github.com/MHSanaei/3x-ui/commit/faf1e010d546141ce19b0b6ffbe68df35e49df45) fix(systemd): harden shipped x-ui unit files - [`8b8c8b5`](https://github.com/MHSanaei/3x-ui/commit/8b8c8b5c0893fb1b8aae84369f75144c4b1d0205) fix(systemd): ship ReadWriteDirectories= alias for systemd < 231 - [`a8d3b71`](https://github.com/MHSanaei/3x-ui/commit/a8d3b71ae86f2259cb17f8338c79989d1368a8de) fix(systemd): keep root's DAC bits and regenerate the write paths - [`11f22c9`](https://github.com/MHSanaei/3x-ui/commit/11f22c9afaaa05386ed40343f8a11204411e652b) fix(systemd): keep the seccomp whitelist off old systemd, tighten the rest - [`72c24b9`](https://github.com/MHSanaei/3x-ui/commit/72c24b9e1985119ee250d4c2a28fcce95c747378) fix(systemd): actually wire the read-only guard, drop the superseded drop-in - [`3456c11`](https://github.com/MHSanaei/3x-ui/commit/3456c1114996309cf8edfafa0fa25d717c4b3db5) fix(systemd): correct two comments and keep spaces out of the path list - [`3536575`](https://github.com/MHSanaei/3x-ui/commit/353657598965e480c8e80f49ff97d67132ff1ec3) fix(systemd): act on the independent review of the drop-in generator - [`8f2b21d`](https://github.com/MHSanaei/3x-ui/commit/8f2b21d41e7108a85df036a5f38ee770c4294fb8) Merge branch 'main' into fix/6605-systemd-hardening - [`395b6dc`](https://github.com/MHSanaei/3x-ui/commit/395b6dcfad6d4f24d5d331b185382dcd814fb62c) fix(systemd): name the hardening old systemd really ignores ### 📊 Changes **6 files changed** (+582 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `install.sh` (+168 -0) 📝 `update.sh` (+180 -0) 📝 `x-ui.service.arch` (+76 -0) 📝 `x-ui.service.debian` (+76 -0) 📝 `x-ui.service.rhel` (+76 -0) 📝 `x-ui.sh` (+6 -0) </details> ### 📄 Description ## Problem The shipped systemd units run the panel as root with no sandboxing at all; `systemd-analyze security` rates the unit **9.6 UNSAFE** (fixes #6605). ## Change Adds a hardening block to `x-ui.service.debian`, `x-ui.service.arch` and `x-ui.service.rhel` (identical except for the pre-existing `EnvironmentFile=`). The panel intentionally stays root — it supervises the Xray children, edits netfilter state for the IP-limit feature and reads TLS private keys — so the settings only bound what a panel-level flaw can reach. | Directive | Why it is safe here | | --- | --- | | `NoNewPrivileges=yes` | Nothing in the panel or its children relies on setuid escalation. | | `ProtectSystem=full` | `/usr` and `/boot` become read-only; the panel only needs write access to its own stores, listed in `ReadWritePaths` below. | | `ReadWritePaths=-/etc/x-ui -/usr/local/x-ui -/var/log/x-ui` | Defaults for `XUI_DB_FOLDER` (DB, update status, node token), `XUI_BIN_FOLDER` (Xray binary/config/geo) and `XUI_LOG_FOLDER`. The leading `-` keeps the unit startable when a path does not exist yet. | | `ProtectKernelTunables=yes`, `ProtectKernelModules=yes`, `ProtectKernelLogs=yes`, `ProtectClock=yes`, `ProtectHostname=yes`, `LockPersonality=yes`, `RestrictRealtime=yes`, `RestrictSUIDSGID=yes`, `RestrictNamespaces=yes` | Not used by the panel; namespaces are the panel's own concern (Docker/custom installs run the panel, not its children, through these). | | `UMask=0077` | DB and TLS material are created owner-only; the updater writes 0755 itself where needed. | | `RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX AF_NETLINK` | Covers the listener, outbound node/gRPC traffic, DNS and netlink lookups (plus the `ip(8)` child used by the AmneziaWG IPv6-alias feature). | | `CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW` | `NET_ADMIN` for `ip(8)`/tproxy and the IP-limit netfilter rules, `NET_BIND_SERVICE` for ports below 1024, `NET_RAW` for raw sockets and `SO_BINDTODEVICE`. | | `SystemCallArchitectures=native`, `SystemCallFilter=@system-service`, `SystemCallErrorNumber=EPERM` | The panel and Go runtime stay inside `@system-service`; violations return `EPERM` instead of killing the process, so a missed syscall degrades one operation rather than the whole panel. | **Deliberately absent:** * `PrivateTmp=yes` — the web updater writes its script under `/tmp` and hands the absolute path to a `systemd-run` transient unit, which does not share this service's private `/tmp`; the download would vanish and updates would break. * `MemoryDenyWriteExecute=yes` — Xray/Go may map writable-executable memory; too risky for a proxy panel. * `ProtectHome=yes` — kept as `read-only` instead: installs keep TLS certificates under `/root/cert` and the panel must read them. ## Notes * `install.sh` and `update.sh` do not embed the unit — they copy it from the release tarball or fetch it from raw.githubusercontent.com — so no script changes are needed. * `systemd-analyze` is not available in the environment this was prepared in, so the numeric rating is not re-measured here; the directive-by-directive reasoning above is checked against the panel's code paths, not executed. Worth running `systemd-analyze security x-ui.service` on a test host before merging. * Installs that relocate `XUI_DB_FOLDER`/`XUI_BIN_FOLDER`/`XUI_LOG_FOLDER` outside the three default paths must add them to `ReadWritePaths` — that is called out in a comment next to the directive. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:42 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6430
No description provided.