Skip to content

std.compress.xz: Avoid possible integer overflow in a few places #14521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/std/compress/xz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn Decompress(comptime ReaderType: type) type {
var hasher = std.compress.hashedReader(self.in_reader, Crc32.init());
const hashed_reader = hasher.reader();

const backward_size = (try hashed_reader.readIntLittle(u32) + 1) * 4;
const backward_size = (@as(u64, try hashed_reader.readIntLittle(u32)) + 1) * 4;
if (backward_size != index_size)
return error.CorruptInput;

Expand Down
4 changes: 2 additions & 2 deletions lib/std/compress/xz/block.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn Decoder(comptime ReaderType: type) type {
var header_hasher = std.compress.hashedReader(block_reader, Crc32.init());
const header_reader = header_hasher.reader();

const header_size = try header_reader.readByte() * 4;
const header_size = @as(u64, try header_reader.readByte()) * 4;
if (header_size == 0)
return error.EndOfStreamWithNoError;

Expand Down Expand Up @@ -217,7 +217,7 @@ pub fn Decoder(comptime ReaderType: type) type {
if (status == 1)
try self.accum.reset(self.allocator);

const size = try packed_reader.readIntBig(u16) + 1;
const size = @as(u17, try packed_reader.readIntBig(u16)) + 1;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will be superseded by/conflict with #14518 (comment)

Let me know if it'd be better to defer this fix to that PR

try self.accum.ensureUnusedCapacity(self.allocator, size);

var i: usize = 0;
Expand Down
20 changes: 20 additions & 0 deletions lib/std/compress/xz/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,23 @@ test "unsupported" {
);
}
}

fn testDontPanic(data: []const u8) !void {
const buf = decompress(data) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => return,
};
defer testing.allocator.free(buf);
}

test "size fields: integer overflow avoidance" {
// These cases were found via fuzz testing and each previously caused
// an integer overflow when decoding. We just want to ensure they no longer
// cause a panic
const header_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6z";
try testDontPanic(header_size_overflow);
const lzma2_chunk_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6\x02\x00!\x01\x08\x00\x00\x00\xd8\x0f#\x13\x01\xff\xff";
try testDontPanic(lzma2_chunk_size_overflow);
const backward_size_overflow = "\xfd7zXZ\x00\x00\x01i\"\xde6\x00\x00\x00\x00\x1c\xdfD!\x90B\x99\r\x01\x00\x00\xff\xff\x10\x00\x00\x00\x01DD\xff\xff\xff\x01";
try testDontPanic(backward_size_overflow);
}