fix(flatpak): construct null-terminated array for arguments

The variant format string `^aay` is said to be equivalent to
g_variant_new_bytestring_array. Given that no length parameter is
provided, g_variant_new assumed a null-terminated array, but the array
constructed by the code was not, causing a crash as glib exceed the read
boundaries to copy arbitrary memory.

This commit replaces the array construction code to use its arena
equivalents instead of trying to build one using glib, and make sure
that the resulting array is null-terminated.
This commit is contained in:
Leorize
2025-01-18 13:38:29 -06:00
parent 3f7c3afaf9
commit ecad3e75ff

View File

@ -265,16 +265,12 @@ pub const FlatpakHostCommand = struct {
} }
// Build our args // Build our args
const args_ptr = c.g_ptr_array_new(); const args = try arena.alloc(?[*:0]u8, self.argv.len + 1);
{ for (0.., self.argv) |i, arg| {
errdefer _ = c.g_ptr_array_free(args_ptr, 1); const argZ = try arena.dupeZ(u8, arg);
for (self.argv) |arg| { args[i] = argZ.ptr;
const argZ = try arena.dupeZ(u8, arg);
c.g_ptr_array_add(args_ptr, argZ.ptr);
}
} }
const args = c.g_ptr_array_free(args_ptr, 0); args[args.len - 1] = null;
defer c.g_free(@as(?*anyopaque, @ptrCast(args)));
// Get the cwd in case we don't have ours set. A small optimization // Get the cwd in case we don't have ours set. A small optimization
// would be to do this only if we need it but this isn't a // would be to do this only if we need it but this isn't a
@ -286,7 +282,7 @@ pub const FlatpakHostCommand = struct {
const params = c.g_variant_new( const params = c.g_variant_new(
"(^ay^aay@a{uh}@a{ss}u)", "(^ay^aay@a{uh}@a{ss}u)",
@as(*const anyopaque, if (self.cwd) |*cwd| cwd.ptr else g_cwd), @as(*const anyopaque, if (self.cwd) |*cwd| cwd.ptr else g_cwd),
args, args.ptr,
c.g_variant_builder_end(fd_builder), c.g_variant_builder_end(fd_builder),
c.g_variant_builder_end(env_builder), c.g_variant_builder_end(env_builder),
@as(c_int, 0), @as(c_int, 0),