fix(system): set_hostname writes /etc/hosts through a mktemp file in /etc (#66) #67

Merged
christianmanivong merged 4 commits from fix/hosts-tempfile-2446 into main 2026-11-13 09:09:28 +00:00
Owner

What was wrong: set_hostname() wrote the new /etc/hosts as root to the fixed /tmp/_netork_hosts and moved it over /etc/hosts. A local user could create that file (or a symlink) beforehand: root's write went into the user's file, and the user owned /etc/hosts afterwards. Found in NetOrk/netork#2446.

Fix: t=$(mktemp /etc/.hosts.XXXXXX) (root's directory, same filesystem as the target), write, chmod 644, mv into place; a failed step removes the file (|| { rm -f "$t"; false; }). set_hostname keeps its length (quality baseline).

Tests: tests/test_set_hostname.py — the write goes through a mktemp file in /etc and no /tmp name (red before the fix); the rename tests still read what is written (their helper now finds the write by base64 -d and /etc/hosts). Checked the command in a real sh against a scratch directory: the file lands with mode 644 and no temporary file is left.

The five failures of tests/test_time_sync.py in my environment fail the same way on main.

Merge note: #65 (hostname quoting) appends to the same test file and CHANGELOG section; whichever goes second needs a trivial rebase/merge of those hunks.

Closes #66

**What was wrong:** `set_hostname()` wrote the new `/etc/hosts` as root to the fixed `/tmp/_netork_hosts` and moved it over `/etc/hosts`. A local user could create that file (or a symlink) beforehand: root's write went into the user's file, and the user owned `/etc/hosts` afterwards. Found in NetOrk/netork#2446. **Fix:** `t=$(mktemp /etc/.hosts.XXXXXX)` (root's directory, same filesystem as the target), write, `chmod 644`, `mv` into place; a failed step removes the file (`|| { rm -f "$t"; false; }`). `set_hostname` keeps its length (quality baseline). **Tests:** `tests/test_set_hostname.py` — the write goes through a mktemp file in /etc and no /tmp name (red before the fix); the rename tests still read what is written (their helper now finds the write by `base64 -d` and `/etc/hosts`). Checked the command in a real `sh` against a scratch directory: the file lands with mode 644 and no temporary file is left. The five failures of `tests/test_time_sync.py` in my environment fail the same way on `main`. Merge note: #65 (hostname quoting) appends to the same test file and CHANGELOG section; whichever goes second needs a trivial rebase/merge of those hunks. Closes #66
christianmanivong added 1 commit 2026-11-12 21:33:54 +00:00
fix(system): set_hostname writes /etc/hosts through a mktemp file in /etc (#66)
driver / standard (pull_request) Failing after 18s
driver / lint (pull_request) Successful in 17s
driver / types (pull_request) Successful in 1m1s
driver / test (3.12) (pull_request) Failing after 1m8s
driver / test (3.13) (pull_request) Failing after 57s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m7s
CI / driver (pull_request) Failing after 2m48s
driver / publish (pull_request) Skipped
a67e8492ee
The new /etc/hosts was written as root to the fixed /tmp/_netork_hosts
and moved over /etc/hosts. A local user could create that file (or a
symlink) beforehand: root's write went into the user's file, and the
user owned /etc/hosts afterwards (NetOrk/netork#2446).

The file is now made with mktemp beside the target (/etc/.hosts.XXXXXX,
root's directory, same filesystem), written, set to 644 and moved into
place; a failed step removes it.
christianmanivong added 2 commits 2026-11-13 07:56:00 +00:00
napalm-device-types 4.36.0 (released after main's last CI run) changed
PosixTimeSyncMixin: get_time_sync() reports DHCP-provided servers apart
(dhcp_ntp_servers), and configure_time_sync() no longer comments out the
DHCP client's chrony sourcedir; it gives back one that 4.35.0 took out
(NetOrk/netork#2380). CI installs the newest release, so five time-sync
tests failed on every branch.

The tests now expect that behaviour, read DHCP's servers apart, and check
that a node 4.35.0 aligned gets its DHCP sourcedir back. The floor moves
to 4.36.0, so the lowest-versions job tests the same behaviour; README,
CHANGELOG and the module docstring say what a write leaves alone.
chore(ratchet): set_hostname is a line shorter (#66)
driver / standard (pull_request) Successful in 14m29s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 1m7s
driver / lowest (pull_request) Successful in 29s
driver / test (3.13) (pull_request) Successful in 56s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 1m3s
CI / driver (pull_request) Successful in 16m53s
driver / publish (pull_request) Skipped
610d6f1093
The mktemp write of /etc/hosts made set_hostname one line shorter, and
the quality ratchet fails on a baseline entry that is no longer reached
(stale). Lowered with napalm-standard ratchet --write.
christianmanivong added 1 commit 2026-11-13 08:57:39 +00:00
Merge remote-tracking branch 'origin/main' into fix/hosts-tempfile-2446
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 50s
driver / test (3.12) (pull_request) Successful in 58s
driver / test (3.13) (pull_request) Successful in 58s
driver / lowest (pull_request) Successful in 31s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Successful in 2m23s
driver / publish (pull_request) Skipped
a421a595d4
christianmanivong merged commit eff6af8b45 into main 2026-11-13 09:09:28 +00:00
christianmanivong deleted branch fix/hosts-tempfile-2446 2026-11-13 09:09:28 +00:00
Sign in to join this conversation.