Audited commit: bfa9d9f43c2fc0a6c008544ca6ea90ab2a4dbc57
Classification
- Type: Privilege escalation
- Severity: Medium
- Confidence: Certain
Affected Locations
src/util.zig:101-123— vulnerablewriteFileAtomicimplementation, with the unsafeopenatat line 103.src/persist.zig:108—dumpUserscall site.src/persist.zig:168—dumpChannelscall site.src/persist.zig:213—dumpBanscall site.
Summary
writeFileAtomic used the deterministic temporary path <destination>.tmp and opened it with CREAT|TRUNC without EXCL or NOFOLLOW. A lower-privileged local user able to create entries in the persistence directory could pre-create that path as a symlink. During a subsequent persistence dump, the server followed the symlink and truncated and overwrote any target writable by its runtime identity.
The written bytes are persistence data rather than fully attacker-selected content, but reliable truncation and replacement with partially attacker-influenced state constitutes unauthorized cross-privilege filesystem corruption.
Preconditions
- File-based persistence is active; the vulnerable path is not exercised when PostgreSQL persistence handles
dumpState(). - The attacker is a local user able to create the temporary directory entry beside a persistence file.
- The attacker can trigger or wait for a persistence dump.
- The server runtime identity can write the selected symlink target.
Proof
dumpUsers,dumpChannels, anddumpBanspass serialized persistence data toutil.writeFileAtomic.- The vulnerable implementation deterministically derives the temporary name as
<path>.tmp. - An attacker with persistence-directory write access pre-creates
<path>.tmpas a symlink to a server-writable target. openatusesWRONLY|CREAT|TRUNCwithoutNOFOLLOWor exclusive creation, so it follows the planted symlink and immediately truncates the target.- The write loop writes persistence data through that descriptor. The later
renamemoves the symlink directory entry onto the persistence path but does not undo the already completed target overwrite.
Thus, a user lacking direct permission to modify the target can cause the more privileged server runtime account to modify it.
Why This Is A Real Bug
The security boundary is the server’s post-drop runtime UID/GID, not necessarily root. Although a root-started server normally drops to its configured identity before entering run(), that identity may still own service configuration or data inaccessible to the attacker.
The primitive provides reliable truncation and replacement of any file writable by that identity. Full control over the replacement bytes is unnecessary for privilege escalation because destroying or corrupting protected service-owned files is itself an unauthorized privileged filesystem operation.
Fix Requirement
Create the temporary file in the destination directory using:
- A cryptographically randomized, unpredictable name.
O_CREAT|O_EXCLto reject any pre-existing directory entry.O_NOFOLLOWto reject symlinks.- A fail-closed response if secure randomness or secure creation fails.
- The existing same-directory rename after writing.
Patch Rationale
The patch obtains 64 bits from getentropy and incorporates them into the temporary filename. EXCL ensures an attacker-created entry is never opened, while NOFOLLOW independently prevents symlink traversal. Removing TRUNC avoids truncating an existing path because secure creation must succeed as a new file.
A random-name collision safely causes the operation to fail rather than opening an attacker-controlled object. Failure of getentropy also aborts the operation before any filesystem modification.
Residual Risk
None
Patch suggestion
diff --git a/src/util.zig b/src/util.zig
index 2350080..06eb836 100644
--- a/src/util.zig
+++ b/src/util.zig
@@ -98,12 +98,16 @@ pub fn readFileAlloc(allocator: std.mem.Allocator, path: []const u8, max: usize)
}
pub fn writeFileAtomic(allocator: std.mem.Allocator, path: []const u8, data: []const u8) !void {
- const tmp = try std.fmt.allocPrint(allocator, "{s}.tmp", .{path});
+ var random: [8]u8 = undefined;
+ if (c.getentropy(random[0..].ptr, random.len) != 0) return error.RandomFailed;
+ const tmp = try std.fmt.allocPrint(allocator, "{s}.tmp-{x}", .{
+ path, std.mem.readInt(u64, &random, .little),
+ });
defer allocator.free(tmp);
const fd = try std.posix.openat(
std.posix.AT.FDCWD,
tmp,
- .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true },
+ .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true, .NOFOLLOW = true },
0o644,
);
var written: usize = 0;Activity
0No activity yet.