authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-04 14:32:25+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 14:57:23-07:00
log594158a6925b4a79f490ef10a03a0c56a5a4ec3d
treeff346f41fe05fb0f806c90712c090b73b7631a6f
parentaf958e95cc0a78404e604400f509ea0c219614d1

Merge pull request #13142 from mllken/gzip-safety

gzip: add missing header fields and bounds for header parsing

1 files changed, 25 insertions(+), 23 deletions(-)

lib/std/compress/gzip.zig+25-23
...@@ -15,6 +15,8 @@ const FEXTRA = 1 << 2;...@@ -15,6 +15,8 @@ const FEXTRA = 1 << 2;
15const FNAME = 1 << 3;15const FNAME = 1 << 3;
16const FCOMMENT = 1 << 4;16const FCOMMENT = 1 << 4;
1717
18const max_string_len = 1024;
19
18pub fn GzipStream(comptime ReaderType: type) type {20pub fn GzipStream(comptime ReaderType: type) type {
19 return struct {21 return struct {
20 const Self = @This();22 const Self = @This();
...@@ -31,9 +33,11 @@ pub fn GzipStream(comptime ReaderType: type) type {...@@ -31,9 +33,11 @@ pub fn GzipStream(comptime ReaderType: type) type {
31 read_amt: usize,33 read_amt: usize,
3234
33 info: struct {35 info: struct {
36 extra: ?[]const u8,
34 filename: ?[]const u8,37 filename: ?[]const u8,
35 comment: ?[]const u8,38 comment: ?[]const u8,
36 modification_time: u32,39 modification_time: u32,
40 operating_system: u8,
37 },41 },
3842
39 fn init(allocator: mem.Allocator, source: ReaderType) !Self {43 fn init(allocator: mem.Allocator, source: ReaderType) !Self {
...@@ -57,33 +61,27 @@ pub fn GzipStream(comptime ReaderType: type) type {...@@ -57,33 +61,27 @@ pub fn GzipStream(comptime ReaderType: type) type {
57 // Operating system where the compression took place61 // Operating system where the compression took place
58 const OS = header[9];62 const OS = header[9];
59 _ = XFL;63 _ = XFL;
60 _ = OS;
6164
62 if (FLG & FEXTRA != 0) {65 const extra = if (FLG & FEXTRA != 0) blk: {
63 // Skip the extra data, we could read and expose it to the user
64 // if somebody needs it.
65 const len = try source.readIntLittle(u16);66 const len = try source.readIntLittle(u16);
66 try source.skipBytes(len, .{});67 const tmp_buf = try allocator.alloc(u8, len);
67 }68 errdefer allocator.free(tmp_buf);
6869
69 var filename: ?[]const u8 = null;70 try source.readNoEof(tmp_buf);
70 if (FLG & FNAME != 0) {71 break :blk tmp_buf;
71 filename = try source.readUntilDelimiterAlloc(72 } else null;
72 allocator,73 errdefer if (extra) |p| allocator.free(p);
73 0,74
74 std.math.maxInt(usize),75 const filename = if (FLG & FNAME != 0)
75 );76 try source.readUntilDelimiterAlloc(allocator, 0, max_string_len)
76 }77 else
78 null;
77 errdefer if (filename) |p| allocator.free(p);79 errdefer if (filename) |p| allocator.free(p);
7880
79 var comment: ?[]const u8 = null;81 const comment = if (FLG & FCOMMENT != 0)
80 if (FLG & FCOMMENT != 0) {82 try source.readUntilDelimiterAlloc(allocator, 0, max_string_len)
81 comment = try source.readUntilDelimiterAlloc(83 else
82 allocator,84 null;
83 0,
84 std.math.maxInt(usize),
85 );
86 }
87 errdefer if (comment) |p| allocator.free(p);85 errdefer if (comment) |p| allocator.free(p);
8886
89 if (FLG & FHCRC != 0) {87 if (FLG & FHCRC != 0) {
...@@ -100,7 +98,9 @@ pub fn GzipStream(comptime ReaderType: type) type {...@@ -100,7 +98,9 @@ pub fn GzipStream(comptime ReaderType: type) type {
100 .info = .{98 .info = .{
101 .filename = filename,99 .filename = filename,
102 .comment = comment,100 .comment = comment,
101 .extra = extra,
103 .modification_time = MTIME,102 .modification_time = MTIME,
103 .operating_system = OS,
104 },104 },
105 .read_amt = 0,105 .read_amt = 0,
106 };106 };
...@@ -108,6 +108,8 @@ pub fn GzipStream(comptime ReaderType: type) type {...@@ -108,6 +108,8 @@ pub fn GzipStream(comptime ReaderType: type) type {
108108
109 pub fn deinit(self: *Self) void {109 pub fn deinit(self: *Self) void {
110 self.inflater.deinit();110 self.inflater.deinit();
111 if (self.info.extra) |extra|
112 self.allocator.free(extra);
111 if (self.info.filename) |filename|113 if (self.info.filename) |filename|
112 self.allocator.free(filename);114 self.allocator.free(filename);
113 if (self.info.comment) |comment|115 if (self.info.comment) |comment|