fix: more hardening
This commit is contained in:
23 files changed
+929
-99
No files matched your search
@@ -14,9 +14,16 @@
|
||||
|
||||
let
|
||||
apCfg = cfg.proxy.autoProxy;
|
||||
inherit ((import ./derived.nix { inherit lib cfg; }).constants) rootInSharedDirConfig;
|
||||
# The autoProxy scope's group writes the directory: `proxy auto learn` queues there.
|
||||
stateDirMode = if userControlAllows "autoProxy" then "0771" else "0751";
|
||||
derived = import ./derived.nix { inherit lib cfg; };
|
||||
inherit (derived.constants) rootInSharedDirConfig;
|
||||
# The autoProxy scope's groups write the directory: `proxy auto learn` queues there. Those
|
||||
# in userControl.groups through ACLs, which the group bits (the ACL mask) leave open.
|
||||
stateDirMode = if derived.userControlAnyAllows "autoProxy" then "0771" else "0751";
|
||||
grantAcl = pkgs.writeShellScript "proxy-suite-autoproxy-acl" (
|
||||
derived.constants.grantDirAcl pkgs (lib.escapeShellArg autoProxyStateDir)
|
||||
(derived.userControlExtraGroupsFor "autoProxy")
|
||||
"rwX"
|
||||
);
|
||||
render = import ./autoproxy-render.nix {
|
||||
inherit pkgs;
|
||||
inherit ((import ./derived.nix { inherit lib cfg; }).constants) serviceUser ifPrivileged;
|
||||
@@ -47,7 +54,8 @@ let
|
||||
clashApiBlock = ''
|
||||
socks_config="$(dirname "$index")/config.json"
|
||||
clash_api=$(jq -r '.experimental.clash_api.external_controller // empty' "$socks_config" 2>/dev/null || true)
|
||||
clash_secret=$(jq -r '.experimental.clash_api.secret // empty' "$socks_config" 2>/dev/null || true)
|
||||
# Root's copy: config.json no longer holds it (start-scripts.nix).
|
||||
clash_secret=$(tr -d '\r\n' 2>/dev/null < "$(dirname "$index")/clash-secret" || true)
|
||||
clash_auth_header() { printf 'Authorization: Bearer %s\n' "$clash_secret"; }
|
||||
'';
|
||||
|
||||
@@ -470,6 +478,9 @@ let
|
||||
# rules/, which its group reads (autoproxy-render.nix), and nothing else.
|
||||
StateDirectoryMode = stateDirMode;
|
||||
UMask = "0027";
|
||||
# Inside the sandbox below, which keeps a name a member left there from leading it
|
||||
# anywhere else.
|
||||
ExecStartPre = "${grantAcl}";
|
||||
}
|
||||
// lib.optionalAttrs (userControlAllows "autoProxy") { Group = cfg.userControl.group; }
|
||||
# Root writes by fixed names in a directory the group writes to: nowhere else, whatever
|
||||
|
||||
@@ -455,6 +455,15 @@ let
|
||||
# proxy-suite-outbound-groups moves failover groups, and "failover" selection, along. Runtime
|
||||
# groups can be failover too, so it runs with every Clash API; with nothing to watch it idles.
|
||||
outboundGroupsWatch = proxyEnabled && clashApiEnabled;
|
||||
# proxy-suite-clash-api: the Clash API for those in the groups, as their scopes allow; the
|
||||
# secret itself stays root's. With listener.auth alone, userControl.group only reads.
|
||||
clashBrokerEnabled =
|
||||
proxyEnabled
|
||||
&& clashApiEnabled
|
||||
&& cfg.host.privileged
|
||||
&& (userControlEnabled || localProxy.authEnabled);
|
||||
clashBrokerGroups =
|
||||
if userControlEnabled then userControlGroupScopes else { ${userControlCfg.group} = [ ]; };
|
||||
|
||||
hasStaticOutbounds = proxyCfg.outbounds != [ ];
|
||||
hasSubscriptions = proxyCfg.subscriptions != [ ];
|
||||
@@ -478,10 +487,32 @@ let
|
||||
zapretCutoffProxyFallback =
|
||||
zapretCutoffEnabled && zapretCfg.zapret2.cutoff.proxyFallback && hasAvailableOutbounds;
|
||||
userControlEnabled = userControlCfg.enable;
|
||||
# Whether the group holds a scope; no scopes listed means all of them.
|
||||
# Whether userControl.group, which owns proxy-suite's files, holds a scope; no scopes
|
||||
# listed means all of them. The groups in userControl.groups get theirs through ACLs.
|
||||
userControlAllows =
|
||||
scope:
|
||||
userControlEnabled && (userControlCfg.scopes == [ ] || builtins.elem scope userControlCfg.scopes);
|
||||
# Every group and what it may do, "none listed" spelled out as every scope.
|
||||
userControlGroupScopes = lib.optionalAttrs userControlEnabled (
|
||||
lib.mapAttrs (_: scopes: if scopes == [ ] then import ./options/user-control-scopes.nix else scopes)
|
||||
(
|
||||
lib.mapAttrs (_: g: g.scopes) userControlCfg.groups
|
||||
// {
|
||||
${userControlCfg.group} = userControlCfg.scopes;
|
||||
}
|
||||
)
|
||||
);
|
||||
# The groups in userControl.groups that hold a scope.
|
||||
userControlExtraGroupsFor =
|
||||
scope:
|
||||
builtins.filter (g: g != userControlCfg.group && builtins.elem scope userControlGroupScopes.${g}) (
|
||||
builtins.attrNames userControlGroupScopes
|
||||
);
|
||||
# Whether any group holds it.
|
||||
userControlAnyAllows = scope: userControlAllows scope || userControlExtraGroupsFor scope != [ ];
|
||||
userControlExtraGroups = lib.optionals userControlEnabled (
|
||||
builtins.attrNames userControlCfg.groups
|
||||
);
|
||||
managerFlag = lib.optionalString (cfg.host.serviceManager == "systemd-user") " --user";
|
||||
constants = {
|
||||
inherit stateDir runtimeDir;
|
||||
@@ -539,6 +570,34 @@ let
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
};
|
||||
# POSIX ACLs for the groups in userControl.groups, which cannot own a file the way
|
||||
# userControl.group does. Shell text, for root.
|
||||
# grantFileAcl: after a file's chmod (which sets the ACL mask), `perm` for each group.
|
||||
grantFileAcl =
|
||||
pkgs: path: groups: perm:
|
||||
lib.optionalString (cfg.host.privileged && groups != [ ]) ''
|
||||
${pkgs.acl}/bin/setfacl -m ${lib.concatMapStringsSep "," (g: "g:${g}:${perm}") groups} -- ${path}
|
||||
'';
|
||||
# grantDirAcl: a directory and all in it, for each group, and what is made in it later
|
||||
# (default entries). What was granted before is taken away first, so a group dropped
|
||||
# from the configuration loses access; -b keeps each file's effective bits. Nothing for
|
||||
# a directory not there yet. -P: never through a symlink a member left in it; a unit
|
||||
# running it keeps the rest of the file system read-only as well.
|
||||
# -n: each file keeps its mask, its group bits, so a group gets no more than the
|
||||
# file's own group would: nothing of a 0600 key. Recalculated, the mask would take
|
||||
# `perm` whole.
|
||||
grantDirAcl =
|
||||
pkgs: path: groups: perm:
|
||||
lib.optionalString cfg.host.privileged ''
|
||||
if [ -d ${path} ]; then
|
||||
${pkgs.acl}/bin/setfacl -R -P -b -- ${path}
|
||||
${lib.optionalString (groups != [ ]) ''
|
||||
${pkgs.acl}/bin/setfacl -R -P -n -m ${
|
||||
lib.concatMapStringsSep "," (g: "g:${g}:${perm},d:g:${g}:${perm}") groups
|
||||
} -- ${path}
|
||||
''}
|
||||
fi
|
||||
'';
|
||||
# Socket marks and IP_TRANSPARENT, TUN and auto_redirect, listeners below 1024, ICMP.
|
||||
backendCaps = [
|
||||
"net_admin"
|
||||
@@ -593,6 +652,7 @@ let
|
||||
'';
|
||||
# Written by every backend start script; proxy-ctl reads the socks copy.
|
||||
outboundInventoryFile = "${runtimeDir}/proxy-suite-socks/outbounds.json";
|
||||
clashBrokerSocket = "${runtimeDir}/proxy-suite-clash/api.sock";
|
||||
|
||||
inboundStatsApiSocket = "${runtimeDir}/proxy-suite-inbounds/api/stats.sock";
|
||||
# Mark and table sending the AmneziaWG listeners' diverted packets to the local stack,
|
||||
@@ -709,6 +769,10 @@ in
|
||||
userControlCfg
|
||||
userControlEnabled
|
||||
userControlAllows
|
||||
userControlGroupScopes
|
||||
userControlExtraGroupsFor
|
||||
userControlAnyAllows
|
||||
userControlExtraGroups
|
||||
sshProxyCfg
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyNativeOutbound
|
||||
@@ -763,6 +827,8 @@ in
|
||||
subscriptionTags
|
||||
groupTags
|
||||
outboundGroupsWatch
|
||||
clashBrokerEnabled
|
||||
clashBrokerGroups
|
||||
hasSubscriptions
|
||||
hasAvailableOutbounds
|
||||
collapseNamedOutbounds
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# What userControl's groups may be given (options/user-control.nix); derived.nix reads it
|
||||
# to spell out "no scopes listed", which means all of them.
|
||||
[
|
||||
"services"
|
||||
"perApp"
|
||||
"routing"
|
||||
"outbounds"
|
||||
"secrets"
|
||||
"autoProxy"
|
||||
"zapret"
|
||||
"stats"
|
||||
"inbounds"
|
||||
"whitelistBypass"
|
||||
"amneziaWg"
|
||||
]
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
let
|
||||
inherit (lib) mkOption types;
|
||||
|
||||
scopeType = types.enum (import ./user-control-scopes.nix);
|
||||
in
|
||||
{
|
||||
options.services.proxy-suite.userControl = {
|
||||
@@ -14,21 +16,7 @@ in
|
||||
};
|
||||
|
||||
scopes = mkOption {
|
||||
type = types.listOf (
|
||||
types.enum [
|
||||
"services"
|
||||
"perApp"
|
||||
"routing"
|
||||
"outbounds"
|
||||
"secrets"
|
||||
"autoProxy"
|
||||
"zapret"
|
||||
"stats"
|
||||
"inbounds"
|
||||
"whitelistBypass"
|
||||
"amneziaWg"
|
||||
]
|
||||
);
|
||||
type = types.listOf scopeType;
|
||||
default = [ ];
|
||||
description = ''
|
||||
What the group may do. Empty allows everything.
|
||||
@@ -52,5 +40,30 @@ in
|
||||
"routing"
|
||||
];
|
||||
};
|
||||
|
||||
groups = mkOption {
|
||||
type = types.attrsOf (
|
||||
types.submodule {
|
||||
options.scopes = mkOption {
|
||||
type = types.listOf scopeType;
|
||||
default = [ ];
|
||||
description = "What this group may do, as in `userControl.scopes`. Empty allows everything.";
|
||||
};
|
||||
}
|
||||
);
|
||||
default = { };
|
||||
description = ''
|
||||
More groups, each with scopes of its own, alongside `userControl.group`. A member gets
|
||||
what all of their groups allow. `group` stays the one that owns proxy-suite's files;
|
||||
these are given access to them through POSIX ACLs, so the file systems of
|
||||
`/var/lib/proxy-suite` and `/run` must support them (the defaults do).
|
||||
'';
|
||||
example = lib.literalExpression ''
|
||||
{
|
||||
proxy-admins.scopes = [ ];
|
||||
proxy-users.scopes = [ "perApp" "routing" ];
|
||||
}
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -251,6 +251,18 @@ let
|
||||
(mkAssertion (cfg.userControl.scopes == [ ] || cfg.userControl.enable)
|
||||
"proxy-suite: userControl.scopes is set but userControl.enable is false, so the group gets nothing"
|
||||
)
|
||||
(mkAssertion (cfg.userControl.groups == { } || cfg.userControl.enable)
|
||||
"proxy-suite: userControl.groups is set but userControl.enable is false, so those groups get nothing"
|
||||
)
|
||||
(mkAssertion
|
||||
(lib.all (
|
||||
g:
|
||||
builtins.match "[a-z_][a-z0-9_-]*" g != null
|
||||
&& g != cfg.userControl.group
|
||||
&& g != derived.constants.serviceUser
|
||||
) (builtins.attrNames cfg.userControl.groups))
|
||||
"proxy-suite: userControl.groups names must be group names (lowercase letters, digits, - and _), and neither userControl.group nor ${derived.constants.serviceUser}"
|
||||
)
|
||||
(requireEnabled (
|
||||
cfg.warp.enable && cfg.warp.asOutbound != null
|
||||
) proxyEnabled "proxy-suite: warp.asOutbound requires proxy.enable = true")
|
||||
|
||||
@@ -52,7 +52,8 @@ let
|
||||
builders = import ./builders.nix { inherit lib pkgs; };
|
||||
|
||||
polkit = import ./polkit.nix {
|
||||
inherit lib pkgs userControlCfg;
|
||||
inherit lib pkgs;
|
||||
inherit (derived) userControlGroupScopes;
|
||||
unitNames =
|
||||
let
|
||||
declared =
|
||||
|
||||
@@ -136,6 +136,8 @@ in
|
||||
RUNTIME_OUTBOUNDS_DIR = constants.runtimeOutboundsDir;
|
||||
RUNTIME_SUBS_DIR = constants.runtimeSubscriptionsDir;
|
||||
USER_CONTROL_GROUP = userControlCfg.group;
|
||||
# {group: [scopes]}: what proxy-suite-clash-api lets each group's members ask.
|
||||
USER_CONTROL_GROUPS = builtins.toJSON ctx.clashBrokerGroups;
|
||||
LOCAL_PROXY_URL = "http://${localProxy.hostPart}:${toString proxyCfg.listener.port}";
|
||||
AUTOPROXY_ENABLED = flag proxyCfg.autoProxy.enable;
|
||||
AUTOPROXY_STATE_DIR = constants.autoProxyStateDir;
|
||||
@@ -146,6 +148,9 @@ in
|
||||
SERVICE_MANAGER = constants.serviceManager;
|
||||
PRIVILEGED = flag constants.privileged;
|
||||
}
|
||||
// lib.optionalAttrs ctx.clashBrokerEnabled {
|
||||
CLASH_BROKER = constants.clashBrokerSocket;
|
||||
}
|
||||
// lib.optionalAttrs (constants.serviceManager == "supervisor") {
|
||||
SUPERVISOR_CTL = constants.systemctl;
|
||||
};
|
||||
|
||||
@@ -56,6 +56,50 @@ let
|
||||
) derived.proxyInbounds;
|
||||
routingScripts = import ./routing-scripts.nix { inherit (context) ctx; };
|
||||
|
||||
# A spool directory the groups holding `scope` write to. userControl.group owns it; with
|
||||
# only userControl.groups holding the scope, root does, and they get it through ACLs
|
||||
# (proxy-suite-acls), which the group bits must leave open: they are the ACL mask.
|
||||
spoolDirMode =
|
||||
scope:
|
||||
if userControlAllows scope then
|
||||
"2770 root ${userControlCfg.group}"
|
||||
else if derived.userControlAnyAllows scope then
|
||||
"2770 root root"
|
||||
else if constants.privileged then
|
||||
"0700 root root"
|
||||
else
|
||||
"0700 - -";
|
||||
|
||||
# What proxy-suite-acls opens to userControl.groups: the spools made by tmpfiles, which no
|
||||
# unit of their own sets up.
|
||||
aclSpools =
|
||||
lib.optionals proxyEnabled [
|
||||
{
|
||||
path = constants.runtimeOutboundsDir;
|
||||
scope = "outbounds";
|
||||
}
|
||||
{
|
||||
path = constants.runtimeSubscriptionsDir;
|
||||
scope = "outbounds";
|
||||
}
|
||||
]
|
||||
++ lib.optional derived.proxyInboundsRuntimeEnabled {
|
||||
path = constants.runtimeInboundsDir;
|
||||
scope = "inbounds";
|
||||
}
|
||||
++ lib.optional derived.awgRuntimeGlobal {
|
||||
path = constants.runtimeAwgDir;
|
||||
scope = "amneziaWg";
|
||||
}
|
||||
++ lib.optional derived.zapretCutoffEnabled {
|
||||
path = "${constants.zapret2CutoffDir}/requests";
|
||||
scope = "zapret";
|
||||
}
|
||||
++ lib.optional cfg.whitelistBypass.enable {
|
||||
path = "${constants.stateDir}/whitelist-bypass";
|
||||
scope = "whitelistBypass";
|
||||
};
|
||||
|
||||
serviceUnits = import ./units.nix {
|
||||
ctx = context.ctx // {
|
||||
inherit routingScripts;
|
||||
@@ -109,10 +153,87 @@ let
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
# The Clash API's secret is root's: members of the userControl groups reach the API through
|
||||
# this, and only for what their scopes cover (proxy_ctl.py, _broker_allows).
|
||||
clashBrokerUnit = lib.mkIf (cfg.enable && derived.clashBrokerEnabled) {
|
||||
services.proxy-suite.internal.services.proxy-suite-clash-api = {
|
||||
description = "proxy-suite - the Clash API for userControl's groups, as their scopes allow";
|
||||
after = [ "proxy-suite-socks.service" ];
|
||||
bindsTo = [ "proxy-suite-socks.service" ];
|
||||
wantedBy = [ "proxy-suite-socks.service" ];
|
||||
serviceConfig = {
|
||||
ExecStart = "${control.proxyCtl}/bin/proxy-ctl proxy clash-broker";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 5;
|
||||
RuntimeDirectory = "proxy-suite-clash";
|
||||
RuntimeDirectoryMode = "0755";
|
||||
# Root for the secret's sake (its file is root's), with no capability at all.
|
||||
CapabilityBoundingSet = [ "" ];
|
||||
NoNewPrivileges = true;
|
||||
PrivateTmp = true;
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
ProtectClock = true;
|
||||
ProtectControlGroups = true;
|
||||
ProtectHostname = true;
|
||||
ProtectKernelLogs = true;
|
||||
ProtectKernelModules = true;
|
||||
ProtectKernelTunables = true;
|
||||
RestrictRealtime = true;
|
||||
RestrictSUIDSGID = true;
|
||||
LockPersonality = true;
|
||||
RestrictAddressFamilies = [
|
||||
"AF_UNIX"
|
||||
"AF_INET"
|
||||
"AF_INET6"
|
||||
];
|
||||
# The API is on loopback; nothing else to reach.
|
||||
IPAddressAllow = [ "localhost" ];
|
||||
IPAddressDeny = [ "any" ];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
# userControl.groups cannot own the spools the way userControl.group does: POSIX ACLs let
|
||||
# them in, set again on every boot and whenever the groups change (a changed unit
|
||||
# restarts), so a group dropped from the configuration loses what it had. With
|
||||
# userControl off too: it then only takes away what an earlier configuration granted.
|
||||
aclUnit = lib.mkIf (cfg.enable && constants.privileged && aclSpools != [ ]) {
|
||||
services.proxy-suite.internal.services.proxy-suite-acls = {
|
||||
description = "proxy-suite - give userControl.groups their access to the runtime spools";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "systemd-tmpfiles-setup.service" ];
|
||||
script = lib.concatMapStrings (
|
||||
spool:
|
||||
constants.grantDirAcl pkgs (lib.escapeShellArg spool.path)
|
||||
(derived.userControlExtraGroupsFor spool.scope)
|
||||
"rwX"
|
||||
) aclSpools;
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
# It walks directories the groups write to: whatever a name there comes to point
|
||||
# at, nothing but the spools can change.
|
||||
ProtectSystem = "strict";
|
||||
ReadWritePaths = map (spool: "-${spool.path}") aclSpools;
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
NoNewPrivileges = true;
|
||||
CapabilityBoundingSet = [
|
||||
"CAP_FOWNER"
|
||||
"CAP_DAC_OVERRIDE"
|
||||
"CAP_DAC_READ_SEARCH"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
in
|
||||
lib.mkMerge [
|
||||
autoProxyUnits
|
||||
outboundGroupsUnit
|
||||
clashBrokerUnit
|
||||
aclUnit
|
||||
{
|
||||
services.proxy-suite.internal = {
|
||||
# The GUI's pkexec needs its setuid wrapper.
|
||||
@@ -176,9 +297,9 @@ lib.mkMerge [
|
||||
];
|
||||
sysctl = tproxyLanSysctl;
|
||||
|
||||
groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) [
|
||||
userControlCfg.group
|
||||
];
|
||||
groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) (
|
||||
[ userControlCfg.group ] ++ derived.userControlExtraGroups
|
||||
);
|
||||
|
||||
polkit.enable = lib.mkIf (cfg.enable && (userControlEnabled || cfg.gui.enable)) true;
|
||||
# The GUI's "Retry as Root" and root toggle run proxy-ctl through pkexec: one
|
||||
@@ -195,7 +316,7 @@ lib.mkMerge [
|
||||
'')
|
||||
(lib.mkIf (cfg.enable && userControlEnabled) ''
|
||||
polkit.addRule(function(action, subject) {
|
||||
if (!subject.isInGroup("${userControlCfg.group}")) {
|
||||
if (!(${polkit.userControlPolkitMember})) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -217,49 +338,30 @@ lib.mkMerge [
|
||||
# outbound before the proxy has ever run.
|
||||
tmpfiles = lib.mkMerge [
|
||||
(lib.mkIf (cfg.enable && proxyEnabled) (
|
||||
map
|
||||
(
|
||||
dir:
|
||||
"d ${dir} ${
|
||||
if userControlAllows "outbounds" then
|
||||
"2770 root ${userControlCfg.group}"
|
||||
else if constants.privileged then
|
||||
"0700 root root"
|
||||
else
|
||||
"0700 - -"
|
||||
} -"
|
||||
)
|
||||
[
|
||||
constants.runtimeOutboundsDir
|
||||
constants.runtimeSubscriptionsDir
|
||||
]
|
||||
map (dir: "d ${dir} ${spoolDirMode "outbounds"} -") [
|
||||
constants.runtimeOutboundsDir
|
||||
constants.runtimeSubscriptionsDir
|
||||
]
|
||||
))
|
||||
# inbounds.runtime: the users' and listeners' files hold their secrets, so the dir is
|
||||
# as closed as outbounds.d, and group-writable only with the inbounds scope.
|
||||
(lib.mkIf (cfg.enable && derived.proxyInboundsRuntimeEnabled) (
|
||||
map
|
||||
(
|
||||
dir:
|
||||
"d ${dir} ${
|
||||
if userControlAllows "inbounds" then
|
||||
"2770 root ${userControlCfg.group}"
|
||||
else if constants.privileged then
|
||||
"0700 root root"
|
||||
else
|
||||
"0700 - -"
|
||||
} -"
|
||||
)
|
||||
[
|
||||
constants.runtimeInboundsDir
|
||||
"${constants.runtimeInboundsDir}/users"
|
||||
"${constants.runtimeInboundsDir}/listeners"
|
||||
]
|
||||
map (dir: "d ${dir} ${spoolDirMode "inbounds"} -") [
|
||||
constants.runtimeInboundsDir
|
||||
"${constants.runtimeInboundsDir}/users"
|
||||
"${constants.runtimeInboundsDir}/listeners"
|
||||
]
|
||||
))
|
||||
# Global AmneziaWG profiles added at runtime. Listable, so the tray and TUI show
|
||||
# their names to everyone; each file is 0600 (amneziawg_config.py writes it so).
|
||||
(lib.mkIf (cfg.enable && derived.awgRuntimeGlobal) [
|
||||
"d ${constants.runtimeAwgDir} ${
|
||||
if userControlAllows "amneziaWg" then "2775 root ${userControlCfg.group}" else "0755 root root"
|
||||
if userControlAllows "amneziaWg" then
|
||||
"2775 root ${userControlCfg.group}"
|
||||
else if derived.userControlAnyAllows "amneziaWg" then
|
||||
"2775 root root"
|
||||
else
|
||||
"0755 root root"
|
||||
} -"
|
||||
])
|
||||
];
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
userControlCfg,
|
||||
# {group: [scopes]}, every scope spelled out (derived.nix).
|
||||
userControlGroupScopes,
|
||||
# Declared system units, "name.service" or "name.timer"; templates as "name@".
|
||||
unitNames,
|
||||
}:
|
||||
@@ -88,10 +89,16 @@ in
|
||||
break;
|
||||
}
|
||||
}
|
||||
// No scopes listed means all of them.
|
||||
var scopes = ${builtins.toJSON userControlCfg.scopes};
|
||||
if (scopes.length === 0 || scopes.indexOf(scope) !== -1) {
|
||||
return polkit.Result.YES;
|
||||
// Any of the subject's groups that holds the scope.
|
||||
var groupScopes = ${builtins.toJSON userControlGroupScopes};
|
||||
for (var group in groupScopes) {
|
||||
if (groupScopes[group].indexOf(scope) !== -1 && subject.isInGroup(group)) {
|
||||
return polkit.Result.YES;
|
||||
}
|
||||
}
|
||||
'';
|
||||
# Whether the subject is in any userControl group at all: the rule's first test.
|
||||
userControlPolkitMember = lib.concatMapStringsSep " || " (
|
||||
group: "subject.isInGroup(${builtins.toJSON group})"
|
||||
) (builtins.attrNames userControlGroupScopes);
|
||||
}
|
||||
@@ -15,6 +15,7 @@ let
|
||||
proxyInboundViaOutbounds
|
||||
userControlCfg
|
||||
userControlAllows
|
||||
userControlExtraGroupsFor
|
||||
localProxyAuth
|
||||
localProxyAuthEnabled
|
||||
localProxyAuthPasswordSource
|
||||
@@ -150,6 +151,7 @@ let
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${linksFile}"
|
||||
''}
|
||||
chmod ${if userControlAllows "secrets" then "640" else "600"} "${linksFile}"
|
||||
${constants.grantFileAcl pkgs ''"${linksFile}"'' (userControlExtraGroupsFor "secrets") "r"}
|
||||
'';
|
||||
|
||||
# One file per user for the web server, plus a token index for proxy-ctl. Filled aside
|
||||
@@ -173,6 +175,7 @@ let
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${subscriptionsFile}"
|
||||
''}
|
||||
chmod ${if userControlAllows "secrets" then "640" else "600"} "${subscriptionsFile}"
|
||||
${constants.grantFileAcl pkgs ''"${subscriptionsFile}"'' (userControlExtraGroupsFor "secrets") "r"}
|
||||
'';
|
||||
|
||||
startInbounds = pkgs.writeShellScript "proxy-suite-inbounds" ''
|
||||
@@ -262,6 +265,10 @@ let
|
||||
chmod 640 "$RUNTIME_DIR/config.json.tmp"
|
||||
''
|
||||
}
|
||||
${constants.grantFileAcl pkgs ''"$RUNTIME_DIR/config.json.tmp"''
|
||||
(userControlExtraGroupsFor "secrets")
|
||||
"r"
|
||||
}
|
||||
mv "$RUNTIME_DIR/config.json.tmp" "$RUNTIME_DIR/config.json"
|
||||
|
||||
# The stats API's socket takes this setgid directory's group: XRay cannot chown it.
|
||||
@@ -277,6 +284,13 @@ let
|
||||
else
|
||||
''install -d -m 2750 -o ${constants.serviceUser} -g ${constants.serviceUser} "$API_DIR"''
|
||||
}
|
||||
# userControl.groups with the stats scope: into the directory, and onto the socket XRay
|
||||
# makes in it as its own group is (a default entry; the socket's mode caps it).
|
||||
${lib.optionalString (constants.privileged && userControlExtraGroupsFor "stats" != [ ]) ''
|
||||
${pkgs.acl}/bin/setfacl -m ${
|
||||
lib.concatMapStringsSep "," (g: "g:${g}:rx,d:g:${g}:rwx") (userControlExtraGroupsFor "stats")
|
||||
} -- "$API_DIR"
|
||||
''}
|
||||
|
||||
${writeLinksBlock}
|
||||
${writeSubscriptionsBlock}
|
||||
@@ -326,6 +340,7 @@ let
|
||||
echo "proxy-suite: group ${userControlCfg.group} cannot read the stats; they stay root-only" >&2
|
||||
''}
|
||||
chmod ${if userControlAllows "stats" then "640" else "600"} "$tmp"
|
||||
${constants.grantFileAcl pkgs ''"$tmp"'' (userControlExtraGroupsFor "stats") "r"}
|
||||
mv -f "$tmp" "$file"
|
||||
'';
|
||||
# inbounds.runtime: after proxy-ctl changes the spool. The AmneziaWG interfaces only when
|
||||
|
||||
@@ -166,6 +166,11 @@ let
|
||||
} > "${runtimeProxychainsConfig}"
|
||||
${constants.ifPrivileged ''${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${runtimeProxychainsConfig}"''}
|
||||
chmod 640 "${runtimeProxychainsConfig}"
|
||||
# It holds the proxy's password: userControl.groups that run apps through proxychains.
|
||||
${constants.grantFileAcl pkgs ''"${runtimeProxychainsConfig}"''
|
||||
(ctx.userControlExtraGroupsFor "perApp")
|
||||
"r"
|
||||
}
|
||||
'';
|
||||
|
||||
mkStartScript =
|
||||
@@ -231,7 +236,7 @@ let
|
||||
AUTOPROXY_DIR=${lib.escapeShellArg autoProxyStateDir}
|
||||
# 0751: sing-box, running as ${constants.serviceUser}, reaches the rules/ inside;
|
||||
# 0771 with userControl's autoProxy scope, whose group queues learn requests there.
|
||||
install -d -m ${if userControlAllows "autoProxy" then "0771" else "0751"} "$AUTOPROXY_DIR"
|
||||
install -d -m ${if ctx.userControlAnyAllows "autoProxy" then "0771" else "0751"} "$AUTOPROXY_DIR"
|
||||
[ -s "$AUTOPROXY_DIR/state.json" ] || echo '{"domains":{},"hosts":{},"exits":{},"backlog":{}}' > "$AUTOPROXY_DIR/state.json"
|
||||
|
||||
# direct is always exit 0; state is keyed by tag, so shifting indices are
|
||||
@@ -293,6 +298,7 @@ let
|
||||
''
|
||||
${constants.ifPrivileged ''${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$ENDPOINTS_TMP"''}
|
||||
chmod 640 "$ENDPOINTS_TMP"
|
||||
${constants.grantFileAcl pkgs ''"$ENDPOINTS_TMP"'' ctx.userControlExtraGroups "r"}
|
||||
''
|
||||
else
|
||||
''chmod 600 "$ENDPOINTS_TMP"''
|
||||
@@ -314,6 +320,7 @@ let
|
||||
' <<< "$OUTBOUNDS_JSON" > "$SHARE_TMP")
|
||||
${lib.optionalString (userControlAllows "secrets") ''${chgrp} ${userControlGroup} "$SHARE_TMP"''}
|
||||
chmod ${if userControlAllows "secrets" then "640" else "600"} "$SHARE_TMP"
|
||||
${constants.grantFileAcl pkgs ''"$SHARE_TMP"'' (ctx.userControlExtraGroupsFor "secrets") "r"}
|
||||
mv "$SHARE_TMP" "$RUNTIME_DIR/outbound-share.json"
|
||||
|
||||
${lib.optionalString (!pureXrayEnabled) ''
|
||||
@@ -388,22 +395,23 @@ let
|
||||
''}
|
||||
# The Clash API lists every connection and switches outbounds. Loopback alone lets
|
||||
# every local user at it, and every web page too (sing-box allows any origin): a
|
||||
# secret per start, readable where outbound-endpoints.json is.
|
||||
# secret per start.
|
||||
BACKEND_EXTRA_CONFIG=()
|
||||
if ${jq} -e '.experimental.clash_api? != null' "$RUNTIME_DIR/config.json" >/dev/null; then
|
||||
CLASH_SECRET=$(${pkgs.coreutils}/bin/od -An -tx1 -N24 /dev/urandom | ${pkgs.coreutils}/bin/tr -d ' \n')
|
||||
(umask 077 && ${jq} --rawfile s <(printf '%s' "$CLASH_SECRET") '.experimental.clash_api.secret = $s' \
|
||||
"$RUNTIME_DIR/config.json" > "$RUNTIME_DIR/config.json.next")
|
||||
mv "$RUNTIME_DIR/config.json.next" "$RUNTIME_DIR/config.json"
|
||||
# Not in config.json, which the secrets scope reads (`proxy config --raw`): the
|
||||
# backend's alone, which sing-box merges in (a second -c).
|
||||
(umask 077 && ${jq} -n --rawfile s <(printf '%s' "$CLASH_SECRET") '{experimental: {clash_api: {secret: $s}}}' \
|
||||
> "$RUNTIME_DIR/clash-api.json.tmp")
|
||||
${constants.ifPrivileged ''${pkgs.coreutils}/bin/chown ${constants.serviceUser}:${constants.serviceUser} "$RUNTIME_DIR/clash-api.json.tmp"''}
|
||||
chmod 400 "$RUNTIME_DIR/clash-api.json.tmp"
|
||||
mv "$RUNTIME_DIR/clash-api.json.tmp" "$RUNTIME_DIR/clash-api.json"
|
||||
BACKEND_EXTRA_CONFIG=(-c "$RUNTIME_DIR/clash-api.json")
|
||||
# Root's alone where the host is privileged: userControl's groups ask
|
||||
# proxy-suite-clash-api, which lets them do what their scopes cover. A rootless host
|
||||
# runs as its one user anyway.
|
||||
(umask 077 && printf '%s\n' "$CLASH_SECRET" > "$RUNTIME_DIR/clash-secret.tmp")
|
||||
${
|
||||
if userControlCfg.enable || localProxyAuthEnabled then
|
||||
''
|
||||
${constants.ifPrivileged ''${chgrp} ${userControlGroup} "$RUNTIME_DIR/clash-secret.tmp"''}
|
||||
chmod 640 "$RUNTIME_DIR/clash-secret.tmp"
|
||||
''
|
||||
else
|
||||
''chmod 600 "$RUNTIME_DIR/clash-secret.tmp"''
|
||||
}
|
||||
chmod 600 "$RUNTIME_DIR/clash-secret.tmp"
|
||||
mv "$RUNTIME_DIR/clash-secret.tmp" "$RUNTIME_DIR/clash-secret"
|
||||
fi
|
||||
# Credentials, read by the backend running as ${constants.serviceUser}. With
|
||||
@@ -423,6 +431,7 @@ let
|
||||
chmod 640 "$backend_config"
|
||||
''
|
||||
}
|
||||
${constants.grantFileAcl pkgs ''"$backend_config"'' (ctx.userControlExtraGroupsFor "secrets") "r"}
|
||||
done
|
||||
FAKE_IP_CACHE=$(${jq} -r '.experimental.cache_file.path? // empty' "$RUNTIME_DIR/config.json")
|
||||
if [ -n "$FAKE_IP_CACHE" ]; then
|
||||
@@ -452,7 +461,7 @@ let
|
||||
exit "$XRAY_SIDECAR_STATUS"
|
||||
fi
|
||||
|
||||
${runBackend} ${singBox} run -c "$RUNTIME_DIR/config.json" &
|
||||
${runBackend} ${singBox} run -c "$RUNTIME_DIR/config.json" "''${BACKEND_EXTRA_CONFIG[@]}" &
|
||||
SING_BOX_PID="$!"
|
||||
# Either one going away breaks the outbounds: exit, and let systemd restart both.
|
||||
SING_BOX_STATUS=0
|
||||
@@ -460,11 +469,11 @@ let
|
||||
exit "$(( SING_BOX_STATUS == 0 ? 1 : SING_BOX_STATUS ))"
|
||||
fi
|
||||
|
||||
exec ${runBackend} ${singBox} run -c "$RUNTIME_DIR/config.json"
|
||||
exec ${runBackend} ${singBox} run -c "$RUNTIME_DIR/config.json" "''${BACKEND_EXTRA_CONFIG[@]}"
|
||||
''
|
||||
else
|
||||
''
|
||||
exec ${runBackend} ${backendBin} run -c "$RUNTIME_DIR/config.json"
|
||||
exec ${runBackend} ${backendBin} run -c "$RUNTIME_DIR/config.json" "''${BACKEND_EXTRA_CONFIG[@]}"
|
||||
''
|
||||
}
|
||||
'';
|
||||
|
||||
@@ -28,10 +28,16 @@ let
|
||||
# Logins, calls and the links joiners take at runtime (`proxy-ctl wl auth|join|new`); the
|
||||
# whitelistBypass scope's group writes them too. tmpfiles makes it before any unit has run,
|
||||
# so a creator or joiner that waits on a file there can be given one.
|
||||
groupAccess = privileged && derived.userControlAllows "whitelistBypass";
|
||||
# Groups in userControl.groups get it through ACLs (proxy-suite-acls), which the mode must
|
||||
# leave open: systemd applies it on every start.
|
||||
groupAccess = privileged && derived.userControlAnyAllows "whitelistBypass";
|
||||
stateDir = "${derived.constants.stateDir}/whitelist-bypass";
|
||||
stateMode = if groupAccess then "0770" else "0700";
|
||||
stateGroup = if groupAccess then cfg.userControl.group else serviceUser;
|
||||
stateGroup =
|
||||
if privileged && derived.userControlAllows "whitelistBypass" then
|
||||
cfg.userControl.group
|
||||
else
|
||||
serviceUser;
|
||||
# A runtime file the unit cannot start without, when nothing in the configuration stands in.
|
||||
waitFor = file: {
|
||||
unitConfig.ConditionPathExists = "%S/proxy-suite/whitelist-bypass/${file}";
|
||||
|
||||
@@ -19,6 +19,8 @@ let
|
||||
inherit (import ./derived.nix { inherit lib cfg; })
|
||||
constants
|
||||
userControlAllows
|
||||
userControlAnyAllows
|
||||
userControlExtraGroupsFor
|
||||
zapretGlobalEnabled
|
||||
;
|
||||
inherit
|
||||
@@ -88,9 +90,17 @@ let
|
||||
mkPreStart = ''
|
||||
${lib.getExe' pkgs.kmod "modprobe"} nfnetlink_queue 2>/dev/null || true
|
||||
install -d -m ${
|
||||
if userControlAllows "zapret" then "2775 -g ${lib.escapeShellArg cfg.userControl.group}" else "0755"
|
||||
if userControlAllows "zapret" then
|
||||
"2775 -g ${lib.escapeShellArg cfg.userControl.group}"
|
||||
# userControl.groups only, through ACLs: the group bits (their mask) stay open.
|
||||
else if userControlAnyAllows "zapret" then
|
||||
"2775"
|
||||
else
|
||||
"0755"
|
||||
} ${runtime.stateDir} ${runtime.circularStateDir}
|
||||
touch ${runtime.autoHostlistFile} ${runtime.userHostlistFile} ${runtime.excludeHostlistFile}
|
||||
# In this unit's sandbox: a name a member left here leads nowhere else.
|
||||
${constants.grantDirAcl pkgs runtime.stateDir (userControlExtraGroupsFor "zapret") "rwX"}
|
||||
${runtime.initScript} start_fw
|
||||
'';
|
||||
|
||||
@@ -102,7 +112,7 @@ let
|
||||
ExecReload = "${lib.getExe' pkgs.coreutils "kill"} -HUP $MAINPID";
|
||||
Environment = runtimeEnv;
|
||||
# Its own part of the state only: under the sandbox below, the rest stays read-only.
|
||||
StateDirectoryMode = if userControlAllows "zapret" then "2775" else "0755";
|
||||
StateDirectoryMode = if userControlAnyAllows "zapret" then "2775" else "0755";
|
||||
}
|
||||
# systemd gives the state directory the unit's group on every start: the group's, which
|
||||
# edits the lists (as autoProxy's units do).
|
||||
|
||||
@@ -15,6 +15,7 @@ let
|
||||
inherit (import ../derived.nix { inherit lib cfg; })
|
||||
constants
|
||||
userControlAllows
|
||||
userControlAnyAllows
|
||||
zapretGlobalEnabled
|
||||
;
|
||||
z2k = zapret2Sources.z2k;
|
||||
@@ -180,7 +181,13 @@ in
|
||||
|
||||
tmpfiles = [
|
||||
"d ${dir}/requests ${
|
||||
if userControlAllows "zapret" then "2770 root ${cfg.userControl.group}" else "0700 root root"
|
||||
if userControlAllows "zapret" then
|
||||
"2770 root ${cfg.userControl.group}"
|
||||
# userControl.groups only, through ACLs (proxy-suite-acls): the mode keeps them open.
|
||||
else if userControlAnyAllows "zapret" then
|
||||
"2770 root root"
|
||||
else
|
||||
"0700 root root"
|
||||
} -"
|
||||
];
|
||||
|
||||
|
||||
Reference in new issue
Block a user