From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1axdW7-0001Gk-Gq for mharc-grub-devel@gnu.org; Tue, 03 May 2016 12:49:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53530) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1axdVz-000140-1p for grub-devel@gnu.org; Tue, 03 May 2016 12:49:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1axdVn-0004TS-2O for grub-devel@gnu.org; Tue, 03 May 2016 12:49:33 -0400 Received: from mail-lf0-x235.google.com ([2a00:1450:4010:c07::235]:36524) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1axdVl-0004Lg-Fr for grub-devel@gnu.org; Tue, 03 May 2016 12:49:26 -0400 Received: by mail-lf0-x235.google.com with SMTP id u64so29159412lff.3 for ; Tue, 03 May 2016 09:49:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=5irLBUVbBY9sBYmAwLfpHTp4WwoL7p2UZ8ar7vHb8xw=; b=I3eG6wIUlnbq7MS+xn3eQcOFCYlGJZHqHm6yF9Llix/gs9myAp2fvvc0M/LmTNUlwN SY+Bv4UGwn775rK6kEnxQfPxoTeOnR9DOoftNVLmOsfUVF++IvAXlksgYMIcfJz5BCJa 4zumOCCmlI8wZv4fKX1FYkxDQ9S+QuL6rercZeOWKEuCDmSo/I7nRj2bUj+hblrIVlOV BFTX9OeiORp6sJNDhGvcUpw/zWg+C53+zW4KNDWaTwXLQdSmZXTLnEITc0LlV3gAqPad nvbXKr7hAf0XhjtnQVDcMOL2/WZE9gfswh5IZkylR7pnc72u+8n4Wv73NUbxnq2IdpHC c+kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=5irLBUVbBY9sBYmAwLfpHTp4WwoL7p2UZ8ar7vHb8xw=; b=kq+SLlul4+3FLEiNKv0RYjXGFo42ZV18uP6orsFSnYKeSIZmv7BaIv08qlfj6p1cVG 7rygSuYJmocqwfA71lSh8nbvcVk1dKj8LLSjeHjp9rV55zhJ4Sb3Gpv/ewMXMTzNIVyn SpjLt5LPagAzmynvaXigA7Q5XkW9s5wJDW2oYDMqnQ48JY5r21VSVN3RiTXcxUMQIeIi SBeCybUafJy8bwyTT8K9U4n+ORwuazuxrlNgp7VyFTXVV4WAc/EyABIObUViLjXrmHB7 3N39JHs1ZYjZCsNTppfGLbm8/dmHtziSHlU/H0upDsP98qMv6I21wwkT2spo0unj2KFM v0XA== X-Gm-Message-State: AOPr4FWiBzSX7Zz2V/o6Ykqev9rOfDL6XfxW1GRljU2Haj95C4T5zaZChQ7NzPdNwlp2oA== X-Received: by 10.112.95.114 with SMTP id dj18mr1706231lbb.136.1462294144587; Tue, 03 May 2016 09:49:04 -0700 (PDT) Received: from [192.168.1.42] (ppp109-252-90-74.pppoe.spdop.ru. [109.252.90.74]) by smtp.gmail.com with ESMTPSA id z1sm17104lbw.2.2016.05.03.09.49.03 for (version=TLSv1/SSLv3 cipher=OTHER); Tue, 03 May 2016 09:49:03 -0700 (PDT) Subject: Re: [PATCH] Implement checksum verification for gunzip To: The development of GNU GRUB References: From: Andrei Borzenkov Message-ID: <5728D67F.2010208@gmail.com> Date: Tue, 3 May 2016 19:49:03 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::235 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 May 2016 16:49:45 -0000 01.05.2016 15:32, Stefan Fritsch пишет: > This implements the crc32 check for the gzip format. Support for zlib's > adler checksum is not included, yet. > That looks fine, but it is something for post 2.02. Ping if it gets forgot. > diff --git a/contrib/grub2/grub-core/io/gzio.c b/contrib/grub2/grub-core/io/gzio.c > index 0f2ea6b..432f0aa 100644 > --- a/contrib/grub2/grub-core/io/gzio.c > +++ b/contrib/grub2/grub-core/io/gzio.c > @@ -43,6 +43,7 @@ > #include > #include > #include > +#include > > GRUB_MOD_LICENSE ("GPLv3+"); > > @@ -94,6 +95,14 @@ struct grub_gzio > struct huft *tl; > /* The distance code table. */ > struct huft *td; > + /* The checksum algorithm */ > + const gcry_md_spec_t *hdesc; Are different algorithms possible? > + /* The wanted checksum */ > + grub_uint32_t orig_checksum; > + /* The uncompressed length */ > + grub_size_t orig_len; > + /* Context for checksum calculation */ > + grub_uint8_t *hcontext; > /* The lookup bits for the literal/length code table. */ > int bl; > /* The lookup bits for the distance code table. */ > @@ -180,7 +189,7 @@ test_gzip_header (grub_file_t file) > grub_uint8_t os_type; > } hdr; > grub_uint16_t extra_len; > - grub_uint32_t orig_len; > + grub_uint32_t crc32; > grub_gzio_t gzio = file->data; > > if (grub_file_tell (gzio->file) != 0) > @@ -215,12 +224,15 @@ test_gzip_header (grub_file_t file) > > /* FIXME: don't do this on not easily seekable files. */ > { > - grub_file_seek (gzio->file, grub_file_size (gzio->file) - 4); > - if (grub_file_read (gzio->file, &orig_len, 4) != 4) > + grub_file_seek (gzio->file, grub_file_size (gzio->file) - 8); > + if (grub_file_read (gzio->file, &crc32, 4) != 4) > + return 0; > + gzio->orig_checksum = grub_le_to_cpu32 (crc32); > + if (grub_file_read (gzio->file, &gzio->orig_len, 4) != 4) > return 0; > /* FIXME: this does not handle files whose original size is over 4GB. > But how can we know the real original size? */ > - file->size = grub_le_to_cpu32 (orig_len); > + file->size = grub_le_to_cpu32 (gzio->orig_len); > } > > initialize_tables (gzio); > @@ -1095,7 +1107,23 @@ inflate_window (grub_gzio_t gzio) > > gzio->saved_offset += gzio->wp; > > - /* XXX do CRC calculation here! */ > + if (gzio->hcontext) > + { > + gzio->hdesc->write (gzio->hcontext, gzio->slide, gzio->wp); > + > + if (gzio->saved_offset == gzio->orig_len) > + { > + grub_uint32_t csum; > + > + gzio->hdesc->final (gzio->hcontext); > + csum = *(grub_uint32_t *)gzio->hdesc->read (gzio->hcontext); > + csum = grub_be_to_cpu32 (csum); > + if (csum != gzio->orig_checksum) > + grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, > + "checksum mismatch %08x/%08x", > + gzio->orig_checksum, csum); > + } > + } > } > > > @@ -1118,6 +1146,9 @@ initialize_tables (grub_gzio_t gzio) > huft_free (gzio->td); > gzio->tl = NULL; > gzio->td = NULL; > + > + if (gzio->hcontext) > + gzio->hdesc->init(gzio->hcontext); > } > > > @@ -1143,6 +1174,9 @@ grub_gzio_open (grub_file_t io, const char *name __attribute__ ((unused))) > > gzio->file = io; > > + gzio->hdesc = GRUB_MD_CRC32; > + gzio->hcontext = grub_malloc(gzio->hdesc->contextsize); > + > file->device = io->device; > file->data = gzio; > file->fs = &grub_gzio_fs; > @@ -1151,6 +1185,7 @@ grub_gzio_open (grub_file_t io, const char *name __attribute__ ((unused))) > if (! test_gzip_header (file)) > { > grub_errno = GRUB_ERR_NONE; > + grub_free (gzio->hcontext); > grub_free (gzio); > grub_free (file); > grub_file_seek (io, 0); > @@ -1287,6 +1322,7 @@ grub_gzio_close (grub_file_t file) > grub_file_close (gzio->file); > huft_free (gzio->tl); > huft_free (gzio->td); > + grub_free (gzio->hcontext); > grub_free (gzio); > > /* No need to close the same device twice. */ > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel >