ReviewOS

also looking at this

mail-os/mail

Harden encrypted mail storage, recovery, and forwarding

#8
Merged chris wants to merge codex/mail-storage-hardening into main
50 files +1701 -9001
packages/zig/src/protocol/imap.zigmodified+130-6
Changes to packages/zig/src/protocol/imap.zig
@@ -17,6 +17,8 @@ const typesense = @import("../search/typesense.zig");
1717// std.posix.getpid is not available on this Zig version's std; libc is linked
1818// (the binary links sqlite3 + libc), so declare the C symbol directly.
1919extern "c" fn getpid() c_int;
20extern "c" fn readlink(path: [*:0]const u8, buf: [*]u8, bufsiz: usize) isize;
21extern "c" fn symlink(target: [*:0]const u8, linkpath: [*:0]const u8) c_int;
2022
2123/// Upper bound on an APPEND literal we are willing to buffer in memory, to
2224/// prevent a client from exhausting server memory with a huge {N} literal.
@@ -794,6 +796,29 @@ fn renameFile(old_path: []const u8, new_path: []const u8) bool {
794796 return std.c.rename(old_z, new_z) == 0;
795797}
796798
799fn pathIsSymlink(path: []const u8) bool {
800 var path_buf: [4097]u8 = undefined;
801 var target_buf: [1]u8 = undefined;
802 if (path.len >= path_buf.len) return false;
803 @memcpy(path_buf[0..path.len], path);
804 path_buf[path.len] = 0;
805 const path_z: [*:0]const u8 = @ptrCast(&path_buf);
806 return readlink(path_z, &target_buf, target_buf.len) >= 0;
807}
808
809fn createRelativeSymlink(target: []const u8, path: []const u8) bool {
810 var target_buf: [4097]u8 = undefined;
811 var path_buf: [4097]u8 = undefined;
812 if (target.len >= target_buf.len or path.len >= path_buf.len) return false;
813 @memcpy(target_buf[0..target.len], target);
814 target_buf[target.len] = 0;
815 @memcpy(path_buf[0..path.len], path);
816 path_buf[path.len] = 0;
817 const target_z: [*:0]const u8 = @ptrCast(&target_buf);
818 const path_z: [*:0]const u8 = @ptrCast(&path_buf);
819 return symlink(target_z, path_z) == 0;
820}
821
797822/// IMAP session
798823pub const ImapSession = struct {
799824 allocator: std.mem.Allocator,
@@ -3361,6 +3386,51 @@ pub const ImapSession = struct {
33613386 _ = std.c.unlink(@ptrCast(&path_buf));
33623387 }
33633388
3389 if (pathIsSymlink(dir_path)) {
3390 var visible_buf: [4097]u8 = undefined;
3391 const visible = std.fmt.bufPrint(&visible_buf, "{s}", .{dir_path}) catch {
3392 try self.sendResponse(tag, "NO", "DELETE failed");
3393 return;
3394 };
3395 visible_buf[visible.len] = 0;
3396 _ = std.c.unlink(@ptrCast(&visible_buf));
3397
3398 const canonical = try std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local_part, name });
3399 defer self.allocator.free(canonical);
3400 for ([_][]const u8{ "tmp", "new", "cur" }) |component| {
3401 const child = try std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ canonical, component });
3402 defer self.allocator.free(child);
3403 const child_files = fs_compat.listEmlFiles(self.allocator, child) catch &.{};
3404 defer {
3405 for (child_files) |file_name| self.allocator.free(file_name);
3406 if (child_files.len > 0) self.allocator.free(child_files);
3407 }
3408 for (child_files) |file_name| {
3409 var child_file_buf: [4097]u8 = undefined;
3410 const child_file = std.fmt.bufPrint(&child_file_buf, "{s}/{s}", .{ child, file_name }) catch continue;
3411 child_file_buf[child_file.len] = 0;
3412 _ = std.c.unlink(@ptrCast(&child_file_buf));
3413 }
3414 var child_buf: [4097]u8 = undefined;
3415 const child_path = std.fmt.bufPrint(&child_buf, "{s}", .{child}) catch continue;
3416 child_buf[child_path.len] = 0;
3417 _ = std.c.rmdir(@ptrCast(&child_buf));
3418 }
3419 var canonical_buf: [4097]u8 = undefined;
3420 const canonical_path = std.fmt.bufPrint(&canonical_buf, "{s}", .{canonical}) catch {
3421 try self.sendResponse(tag, "NO", "DELETE failed");
3422 return;
3423 };
3424 canonical_buf[canonical_path.len] = 0;
3425 if (std.c.rmdir(@ptrCast(&canonical_buf)) != 0) {
3426 try self.sendResponse(tag, "NO", "DELETE failed");
3427 return;
3428 }
3429 std.log.info("IMAP DELETE: Removed Maildir++ mailbox {s}", .{name});
3430 try self.sendResponse(tag, "OK", "DELETE completed");
3431 return;
3432 }
3433
33643434 var dir_buf: [4097]u8 = undefined;
33653435 const dir_written = std.fmt.bufPrint(&dir_buf, "{s}", .{dir_path}) catch {
33663436 try self.sendResponse(tag, "OK", "DELETE completed");
@@ -3404,6 +3474,39 @@ pub const ImapSession = struct {
34043474 const new_path = try std.fmt.allocPrint(self.allocator, "mail/{s}/{s}", .{ local_part, new });
34053475 defer self.allocator.free(new_path);
34063476
3477 if (pathIsSymlink(old_path)) {
3478 const old_canonical = try std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local_part, old });
3479 defer self.allocator.free(old_canonical);
3480 const new_canonical = try std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local_part, new });
3481 defer self.allocator.free(new_canonical);
3482 const new_target = try std.fmt.allocPrint(self.allocator, ".{s}/cur", .{new});
3483 defer self.allocator.free(new_target);
3484 const old_target = try std.fmt.allocPrint(self.allocator, ".{s}/cur", .{old});
3485 defer self.allocator.free(old_target);
3486
3487 if (!renameFile(old_canonical, new_canonical)) {
3488 try self.sendResponse(tag, "NO", "RENAME failed");
3489 return;
3490 }
3491 var old_buf: [4097]u8 = undefined;
3492 const old_visible = std.fmt.bufPrint(&old_buf, "{s}", .{old_path}) catch {
3493 _ = renameFile(new_canonical, old_canonical);
3494 try self.sendResponse(tag, "NO", "RENAME failed");
3495 return;
3496 };
3497 old_buf[old_visible.len] = 0;
3498 _ = std.c.unlink(@ptrCast(&old_buf));
3499 if (!createRelativeSymlink(new_target, new_path)) {
3500 _ = renameFile(new_canonical, old_canonical);
3501 _ = createRelativeSymlink(old_target, old_path);
3502 try self.sendResponse(tag, "NO", "RENAME failed");
3503 return;
3504 }
3505 std.log.info("IMAP RENAME: {s} -> {s}", .{ old, new });
3506 try self.sendResponse(tag, "OK", "RENAME completed");
3507 return;
3508 }
3509
34073510 if (renameFile(old_path, new_path)) {
34083511 std.log.info("IMAP RENAME: {s} -> {s}", .{ old, new });
34093512 try self.sendResponse(tag, "OK", "RENAME completed");
@@ -3859,21 +3962,42 @@ pub const ImapSession = struct {
38593962 try self.sendResponse(tag, "OK", "ID completed");
38603963 },
38613964 .create => {
3862 // CREATE mailbox - create the Maildir folder
3965 // CREATE mailbox in canonical Maildir++ form. The visible
3966 // folder symlink preserves compatibility with older server
3967 // paths while external Maildir tools use .Folder/{tmp,new,cur}.
38633968 const create_name = stripQuotes(parts.next() orelse "");
38643969 if (!isSafeMailboxName(create_name)) {
38653970 // Rejects empty names and any path-traversal attempt.
38663971 try self.sendResponse(tag, "NO", "Invalid mailbox name");
38673972 } else {
3973 var created = false;
38683974 if (self.username) |uname| {
38693975 const local = uname;
3870 const dir = std.fmt.allocPrint(self.allocator, "mail/{s}/{s}", .{ local, create_name }) catch null;
3871 if (dir) |d| {
3872 fs_compat.cwd().makePath(d) catch {};
3873 self.allocator.free(d);
3976 const visible = std.fmt.allocPrint(self.allocator, "mail/{s}/{s}", .{ local, create_name }) catch null;
3977 const canonical = std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local, create_name }) catch null;
3978 const target = std.fmt.allocPrint(self.allocator, ".{s}/cur", .{create_name}) catch null;
3979 if (visible != null and canonical != null and target != null) {
3980 const tmp = std.fmt.allocPrint(self.allocator, "{s}/tmp", .{canonical.?}) catch null;
3981 const new = std.fmt.allocPrint(self.allocator, "{s}/new", .{canonical.?}) catch null;
3982 const cur = std.fmt.allocPrint(self.allocator, "{s}/cur", .{canonical.?}) catch null;
3983 if (tmp != null and new != null and cur != null) {
3984 fs_compat.cwd().makePath(tmp.?) catch {};
3985 fs_compat.cwd().makePath(new.?) catch {};
3986 fs_compat.cwd().makePath(cur.?) catch {};
3987 created = createRelativeSymlink(target.?, visible.?);
3988 }
3989 if (tmp) |value| self.allocator.free(value);
3990 if (new) |value| self.allocator.free(value);
3991 if (cur) |value| self.allocator.free(value);
38743992 }
3993 if (visible) |value| self.allocator.free(value);
3994 if (canonical) |value| self.allocator.free(value);
3995 if (target) |value| self.allocator.free(value);
38753996 }
3876 try self.sendResponse(tag, "OK", "CREATE completed");
3997 if (created)
3998 try self.sendResponse(tag, "OK", "CREATE completed")
3999 else
4000 try self.sendResponse(tag, "NO", "Mailbox already exists or could not be created");
38774001 }
38784002 },
38794003 .subscribe => {