fix: zapret2 file permission fixes

This commit is contained in:
FUFSoB committed 2026-10-21 21:51:39 +05:00
1 parent a41fbc10e6
commit 20f1bdd110
6 files changed
+63 -5

No files matched your search

+20
View File
@@ -109,6 +109,21 @@ let
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"}
${lib.optionalString (userControlAnyAllows "zapret") ''
# A list a member renamed in is theirs: nfqws2 appends to it through the group
# (after the ACLs, whose mask the group bits are).
for f in ${
lib.escapeShellArgs [
runtime.autoHostlistFile
runtime.userHostlistFile
runtime.excludeHostlistFile
"${runtime.stateDir}/verdicts.tsv"
"${runtime.circularStateDir}/state.tsv"
]
}; do
if [ -f "$f" ] && [ ! -L "$f" ]; then chmod g+w -- "$f"; fi
done
''}
${runtime.initScript} start_fw
'';
@@ -125,6 +140,11 @@ let
# systemd gives the state directory the unit's group on every start: the group's, which
# edits the lists (as autoProxy's units do).
// lib.optionalAttrs (userControlAllows "zapret") { Group = cfg.userControl.group; }
# userControl.groups' members own what they rename in, writable by their ACL groups:
# nfqws2 keeps these groups (runtime.daemonScript) to append to it.
// lib.optionalAttrs (userControlExtraGroupsFor "zapret" != [ ]) {
SupplementaryGroups = userControlExtraGroupsFor "zapret";
}
# nfqws2 runs as root and writes its lists in a directory the zapret scope's group
# writes to as well.
// constants.rootInSharedDirConfig
@@ -38,6 +38,9 @@ in
# Ahead of the proxy at boot, so it starts with the hosts already in.
before = [ "proxy-suite-socks.service" ];
wantedBy = [ "multi-user.target" ];
# nfqws2 writes verdicts in bursts (a page's unanswered addresses), each a start: the
# start limit would fail the path unit with it, and nothing would sync until a reboot.
startLimitIntervalSec = 0;
serviceConfig = {
Type = "oneshot";
ExecStart = "${sync} ${lib.escapeShellArg stateDir} ${lib.escapeShellArg outDir}";
+7 -1
View File
@@ -437,8 +437,14 @@ let
. "$ZAPRET_BASE/init.d/sysv/functions"
opt="--qnum=$QNUM $NFQWS2_OPT"
filter_apply_hostlist_target opt
# --user=root leaves nfqws2 root's own groups (initgroups), and with them only
# CAP_NET_ADMIN and CAP_NET_RAW: the unit's Group= and SupplementaryGroups=, through
# which it appends to the lists the zapret scope's group renames in (theirs, then),
# would be gone. --uid after it keeps uid 0 and the groups the unit started it with.
uid=
if [ "$(id -u)" = 0 ]; then uid="--uid=0:$(id -G | tr ' ' ,)"; fi
set -f
exec "$NFQWS2" $NFQWS2_OPT_BASE $opt
exec "$NFQWS2" $NFQWS2_OPT_BASE $uid $opt
'';
in
{
+11
View File
@@ -90,8 +90,14 @@ pkgs.testers.runNixOSTest {
];
networking.hosts."203.0.113.3" = [ "blocked.test" ];
environment.systemPackages = [ pkgs.curl ];
# A member of the group, whose edits leave the lists theirs.
users.users.alice = {
isNormalUser = true;
extraGroups = [ "proxy-suite" ];
};
services.proxy-suite = {
enable = true;
userControl.enable = true;
# An outbound to hand what zapret2 cannot fix to; nothing here dials it.
proxy = {
enable = true;
@@ -147,6 +153,11 @@ pkgs.testers.runNixOSTest {
def learned(host):
return f"grep -qx {host} ${state}/zapret-hosts-auto.txt"
with subtest("lists a member rewrote are theirs, and nfqws2 still writes to them"):
client.succeed("touch ${state}/verdicts.tsv", "su - alice -c 'proxy-ctl zapret auto clear'")
client.succeed("test \"$(stat -c %U ${state}/zapret-hosts-auto.txt ${state}/verdicts.tsv | sort -u)\" = alice")
# Everything below learns into them and judges into them.
with subtest("a site blocked two times in three is learned, successes in between"):
for _ in range(9):
curl("flaky.test")
+8 -4
View File
@@ -4476,21 +4476,25 @@ def _replace_lines(path, keep, extra=()):
"""Rewrites path with the lines keep() accepts, plus extra.
Via a temp file in the same directory, with the old file's owner and mode (a new one
world-readable: unprivileged proxy-ctl reads these lists too). Locked against another
proxy-ctl; what nfqws2 appended meanwhile, unlocked, is carried over before the rename.
world-readable: unprivileged proxy-ctl reads these lists too), group-writable where the
directory is: renamed in by a member, the file is theirs, and nfqws2 appends to it as one
of the group. Locked against another proxy-ctl; what nfqws2 appended meanwhile,
unlocked, is carried over before the rename.
"""
with _file_lock(path):
try:
old = os.lstat(path) if os.path.lexists(path) else None
text = read_shared_text(path) if old else ""
fd, tmp = tempfile.mkstemp(prefix=os.path.basename(path) + ".", dir=os.path.dirname(path) or ".")
directory = os.path.dirname(path) or "."
shared = os.stat(directory).st_mode & stat.S_IWGRP
fd, tmp = tempfile.mkstemp(prefix=os.path.basename(path) + ".", dir=directory)
except OSError:
denied(path, "write")
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.writelines(f"{line}\n" for line in [*filter(keep, lines(text)), *extra])
# By fd, not path: tmp's name could be swapped for a symlink in between.
os.fchmod(f.fileno(), stat.S_IMODE(old.st_mode) & 0o777 if old else 0o644)
os.fchmod(f.fileno(), (stat.S_IMODE(old.st_mode) & 0o777 if old else 0o644) | shared)
if old and os.geteuid() == 0:
os.fchown(f.fileno(), old.st_uid, old.st_gid)
# Newer than anything this edit means to drop: kept as they came.
+14
View File
@@ -1702,6 +1702,20 @@ class ZapretAutoTest(EnvTest):
ctl._replace_lines(path, lambda line: "b.example" not in line)
self.assertEqual(ctl.read_text(path), "works\ta.example\ttcp\nworks\tc.example\ttcp\n")
def test_rewrite_stays_writable_for_the_group_nfqws2_appends_through(self):
"""A member's rewrite makes the list theirs: group-writable where the directory is, for nfqws2."""
path = self.write("verdicts.tsv", "works\ta.example\ttcp\n")
os.chmod(path, 0o644)
os.chmod(self.dir, 0o2775)
ctl._replace_lines(path, lambda _: True)
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), 0o664)
os.chmod(self.dir, 0o755)
ctl._replace_lines(path, lambda _: True)
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), 0o664)
os.chmod(path, 0o644)
ctl._replace_lines(path, lambda _: True)
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), 0o644)
def test_rewrites_take_the_lock(self):
"""Another proxy-ctl mid-edit holds .<name>.lock: this one waits for it, then gives up."""
path = self.write("zapret-hosts-auto.txt", "a.example\n")