Skip to content

Commit 6b4f327

Browse files
committed
Fix openDir called on file
1 parent a9c0345 commit 6b4f327

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

src/tboot-nixos-install.zig

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,36 @@ fn ensureFilesystemState(
3838
}
3939

4040
fn installGeneration(
41-
allocator: std.mem.Allocator,
41+
arena_alloc: std.mem.Allocator,
4242
known_files: *StringSet,
4343
spec: *const BootSpecV1,
4444
generation: u32,
4545
esp: std.fs.Dir,
4646
args: *const Args,
4747
) !void {
48-
var entry_contents_list = std.ArrayList(u8).init(allocator);
48+
var entry_contents_list = std.ArrayList(u8).init(arena_alloc);
4949

5050
const linux_target_filename = try std.fmt.allocPrint(
51-
allocator,
51+
arena_alloc,
5252
"{s}-{s}",
5353
.{ path.basename(path.dirname(spec.kernel).?), path.basename(spec.kernel) },
5454
);
5555

56-
const linux_target = try path.join(allocator, &.{
56+
const linux_target = try path.join(arena_alloc, &.{
5757
"EFI",
5858
"nixos",
5959
linux_target_filename,
6060
});
6161

6262
const full_linux_path = try path.join(
63-
allocator,
63+
arena_alloc,
6464
&.{ args.efi_sys_mount_point, linux_target },
6565
);
6666

6767
if (!utils.pathExists(esp, linux_target)) {
6868
if (!args.dry_run) {
6969
try signFile(
70-
allocator,
70+
arena_alloc,
7171
args.private_key,
7272
args.public_key,
7373
spec.kernel,
@@ -83,18 +83,18 @@ fn installGeneration(
8383
const initrd_target = b: {
8484
if (spec.initrd) |initrd| {
8585
const initrd_target_filename = try std.fmt.allocPrint(
86-
allocator,
86+
arena_alloc,
8787
"{s}-{s}",
8888
.{ path.basename(path.dirname(initrd).?), path.basename(initrd) },
8989
);
9090

91-
const initrd_target = try path.join(allocator, &.{
91+
const initrd_target = try path.join(arena_alloc, &.{
9292
"EFI",
9393
"nixos",
9494
initrd_target_filename,
9595
});
9696

97-
const full_initrd_path = try path.join(allocator, &.{
97+
const full_initrd_path = try path.join(arena_alloc, &.{
9898
path.sep_str,
9999
args.efi_sys_mount_point,
100100
initrd_target,
@@ -103,7 +103,7 @@ fn installGeneration(
103103
if (!utils.pathExists(esp, initrd_target)) {
104104
if (!args.dry_run) {
105105
try signFile(
106-
allocator,
106+
arena_alloc,
107107
args.private_key,
108108
args.public_key,
109109
spec.initrd.?,
@@ -122,18 +122,18 @@ fn installGeneration(
122122
}
123123
};
124124

125-
const kernel_params_without_init = try std.mem.join(allocator, " ", spec.kernel_params);
125+
const kernel_params_without_init = try std.mem.join(arena_alloc, " ", spec.kernel_params);
126126

127127
const kernel_params = try std.fmt.allocPrint(
128-
allocator,
128+
arena_alloc,
129129
"init={s} {s}",
130130
.{ spec.init, kernel_params_without_init },
131131
);
132132

133133
const sub_name = if (spec.name) |name|
134-
try std.fmt.allocPrint(allocator, " ({s})", .{name})
134+
try std.fmt.allocPrint(arena_alloc, " ({s})", .{name})
135135
else
136-
try allocator.alloc(u8, 0);
136+
try arena_alloc.alloc(u8, 0);
137137

138138
try entry_contents_list.writer().print("title {s}{s}\n", .{ spec.label, sub_name });
139139
try entry_contents_list.writer().print("version {s}\n", .{spec.label});
@@ -145,30 +145,30 @@ fn installGeneration(
145145
const entry_contents = try entry_contents_list.toOwnedSlice();
146146

147147
const sub_entry_name = if (spec.name) |name|
148-
try std.fmt.allocPrint(allocator, "-specialisation-{s}", .{name})
148+
try std.fmt.allocPrint(arena_alloc, "-specialisation-{s}", .{name})
149149
else
150-
try allocator.alloc(u8, 0);
150+
try arena_alloc.alloc(u8, 0);
151151

152152
const entry_name = try std.fmt.allocPrint(
153-
allocator,
153+
arena_alloc,
154154
"nixos-generation-{d}{s}",
155155
.{ generation, sub_entry_name },
156156
);
157157

158158
const entry_filename_with_counters = try std.fmt.allocPrint(
159-
allocator,
159+
arena_alloc,
160160
"{s}+{d}-0.conf",
161161
.{ entry_name, args.max_tries },
162162
);
163163

164-
const entry_path = try path.join(allocator, &.{
164+
const entry_path = try path.join(arena_alloc, &.{
165165
"loader",
166166
"entries",
167167
entry_filename_with_counters,
168168
});
169169

170170
var entries_dir = try esp.openDir(
171-
entry_path,
171+
"loader/entries",
172172
.{ .iterate = true },
173173
);
174174
defer entries_dir.close();
@@ -183,7 +183,7 @@ fn installGeneration(
183183

184184
if (std.mem.eql(u8, existing_entry.name, entry_name)) {
185185
std.log.debug("entry {s} already installed", .{entry_name});
186-
const known_entry = try path.join(allocator, &.{
186+
const known_entry = try path.join(arena_alloc, &.{
187187
path.sep_str,
188188
args.efi_sys_mount_point,
189189
"loader",
@@ -249,7 +249,7 @@ pub fn main() !void {
249249
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
250250
defer arena.deinit();
251251

252-
const allocator = arena.allocator();
252+
const arena_alloc = arena.allocator();
253253

254254
const params = comptime clap.parseParamsComptime(
255255
\\-h, --help Display this help and exit.
@@ -315,7 +315,7 @@ pub fn main() !void {
315315
}
316316

317317
if (args.dry_run) {
318-
std.log.warn("running a dry run, no filesystem changes will occur", .{});
318+
std.log.warn("in dry run mode, no filesystem changes will occur", .{});
319319
}
320320

321321
const esp = try std.fs.cwd().openDir(args.efi_sys_mount_point, .{
@@ -333,7 +333,7 @@ pub fn main() !void {
333333
);
334334
defer nixos_system_profile_dir.close();
335335

336-
var known_files = StringSet.init(allocator);
336+
var known_files = StringSet.init(arena_alloc);
337337

338338
var it = nixos_system_profile_dir.iterate();
339339
while (try it.next()) |entry| {
@@ -355,23 +355,23 @@ pub fn main() !void {
355355
}
356356
};
357357

358-
const boot_json_path = try path.join(allocator, &.{
358+
const boot_json_path = try path.join(arena_alloc, &.{
359359
entry.name,
360360
"boot.json",
361361
});
362362

363363
var boot_json_file = try nixos_system_profile_dir.openFile(boot_json_path, .{});
364364
defer boot_json_file.close();
365365

366-
const boot_json_contents = try boot_json_file.readToEndAlloc(allocator, 8192);
366+
const boot_json_contents = try boot_json_file.readToEndAlloc(arena_alloc, 8192);
367367

368-
const boot_json = BootJson.parse(allocator, boot_json_contents) catch |err| {
368+
const boot_json = BootJson.parse(arena_alloc, boot_json_contents) catch |err| {
369369
std.log.err("failed to parse bootspec boot.json: {any}", .{err});
370370
continue;
371371
};
372372

373373
try installGeneration(
374-
allocator,
374+
arena_alloc,
375375
&known_files,
376376
&boot_json.spec,
377377
generation,
@@ -381,7 +381,7 @@ pub fn main() !void {
381381
if (boot_json.specialisations) |specialisations| {
382382
for (specialisations) |s| {
383383
try installGeneration(
384-
allocator,
384+
arena_alloc,
385385
&known_files,
386386
&s,
387387
generation,
@@ -392,12 +392,12 @@ pub fn main() !void {
392392
}
393393

394394
if (std.mem.eql(u8, boot_json.spec.toplevel, args.default_nixos_system_closure)) {
395-
const loader_conf_path = try path.join(allocator, &.{
395+
const loader_conf_path = try path.join(arena_alloc, &.{
396396
"loader",
397397
"loader.conf",
398398
});
399399

400-
const loader_conf_contents = try std.fmt.allocPrint(allocator,
400+
const loader_conf_contents = try std.fmt.allocPrint(arena_alloc,
401401
\\timeout {d}
402402
\\default nixos-generation-{d}
403403
, .{ args.timeout, generation });
@@ -414,6 +414,6 @@ pub fn main() !void {
414414
}
415415
}
416416

417-
try cleanupDir(allocator, &known_files, esp, "EFI/nixos", &args);
418-
try cleanupDir(allocator, &known_files, esp, "loader/entries", &args);
417+
try cleanupDir(arena_alloc, &known_files, esp, "EFI/nixos", &args);
418+
try cleanupDir(arena_alloc, &known_files, esp, "loader/entries", &args);
419419
}

src/tboot-sign.zig

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ fn drain_openssl_errors() void {
7373
while (C.ERR_get_error() != 0) {}
7474
}
7575

76-
fn readPrivateKey(allocator: std.mem.Allocator, filepath: []const u8) !*anyopaque {
77-
const filepathZ = try allocator.dupeZ(u8, filepath);
76+
fn readPrivateKey(arena_alloc: std.mem.Allocator, filepath: []const u8) !*anyopaque {
77+
const full_filepath = try std.fs.cwd().realpathAlloc(arena_alloc, filepath);
78+
const filepathZ = try arena_alloc.dupeZ(u8, full_filepath);
7879

7980
if (std.mem.startsWith(u8, filepath, "pkcs11:")) {
8081
C.ENGINE_load_builtin_engines();
@@ -94,7 +95,7 @@ fn readPrivateKey(allocator: std.mem.Allocator, filepath: []const u8) !*anyopaqu
9495
}
9596

9697
if (key_pass) |pass| {
97-
if (C.ENGINE_ctrl_cmd_string(engine, "PIN", try allocator.dupeZ(u8, pass), 0) == 0) {
98+
if (C.ENGINE_ctrl_cmd_string(engine, "PIN", try arena_alloc.dupeZ(u8, pass), 0) == 0) {
9899
displayOpensslErrors(@src());
99100
return error.OpensslError;
100101
}
@@ -118,8 +119,9 @@ fn readPrivateKey(allocator: std.mem.Allocator, filepath: []const u8) !*anyopaqu
118119
}
119120
}
120121

121-
fn readX509(allocator: std.mem.Allocator, filepath: []const u8) !*anyopaque {
122-
const filepathZ = try allocator.dupeZ(u8, filepath);
122+
fn readX509(arena_alloc: std.mem.Allocator, filepath: []const u8) !*anyopaque {
123+
const full_filepath = try std.fs.cwd().realpathAlloc(arena_alloc, filepath);
124+
const filepathZ = try arena_alloc.dupeZ(u8, full_filepath);
123125

124126
const bio = C.BIO_new_file(filepathZ, "rb") orelse {
125127
displayOpensslErrors(@src());
@@ -174,7 +176,7 @@ fn displayOpensslErrors(src: std.builtin.SourceLocation) void {
174176
}
175177

176178
pub fn signFile(
177-
allocator: std.mem.Allocator,
179+
arena_alloc: std.mem.Allocator,
178180
in_file: []const u8,
179181
out_file: []const u8,
180182
private_key_filepath: []const u8,
@@ -184,22 +186,22 @@ pub fn signFile(
184186
_ = C.OPENSSL_init_crypto(C.OPENSSL_INIT_LOAD_CRYPTO_STRINGS, null);
185187
C.ERR_clear_error();
186188

187-
var env = try std.process.getEnvMap(allocator);
189+
var env = try std.process.getEnvMap(arena_alloc);
188190

189191
key_pass = env.get("TBOOT_SIGN_PIN");
190192

191193
const in_bio = C.BIO_new_file(
192-
try allocator.dupeZ(u8, in_file),
194+
try arena_alloc.dupeZ(u8, in_file),
193195
"rb",
194196
) orelse {
195197
displayOpensslErrors(@src());
196198
return error.OpensslError;
197199
};
198200
defer _ = C.BIO_free(in_bio);
199201

200-
const private_key = try readPrivateKey(allocator, private_key_filepath);
202+
const private_key = try readPrivateKey(arena_alloc, private_key_filepath);
201203

202-
const public_key = try readX509(allocator, public_key_filepath);
204+
const public_key = try readX509(arena_alloc, public_key_filepath);
203205

204206
_ = C.OPENSSL_init_crypto(C.OPENSSL_INIT_ADD_ALL_DIGESTS, null);
205207
displayOpensslErrors(@src());
@@ -217,7 +219,7 @@ pub fn signFile(
217219
C.PKCS7_NOCERTS | C.PKCS7_BINARY | C.PKCS7_DETACHED | C.PKCS7_NOATTR,
218220
);
219221

220-
const out_bio = C.BIO_new_file(try allocator.dupeZ(u8, out_file), "wb") orelse {
222+
const out_bio = C.BIO_new_file(try arena_alloc.dupeZ(u8, out_file), "wb") orelse {
221223
displayOpensslErrors(@src());
222224
return error.OpensslError;
223225
};

0 commit comments

Comments
 (0)