* [PATCH] Implement checksum verification for gunzip
@ 2016-05-01 12:32 Stefan Fritsch
2016-05-03 16:49 ` Andrei Borzenkov
2017-08-30 14:08 ` Vladimir 'phcoder' Serbinenko
0 siblings, 2 replies; 5+ messages in thread
From: Stefan Fritsch @ 2016-05-01 12:32 UTC (permalink / raw)
To: grub-devel
This implements the crc32 check for the gzip format. Support for zlib's
adler checksum is not included, yet.
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 <grub/dl.h>
#include <grub/deflate.h>
#include <grub/i18n.h>
+#include <grub/crypto.h>
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;
+ /* 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. */
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] Implement checksum verification for gunzip
2016-05-01 12:32 [PATCH] Implement checksum verification for gunzip Stefan Fritsch
@ 2016-05-03 16:49 ` Andrei Borzenkov
2016-05-04 11:38 ` Stefan Fritsch
2017-08-30 14:08 ` Vladimir 'phcoder' Serbinenko
1 sibling, 1 reply; 5+ messages in thread
From: Andrei Borzenkov @ 2016-05-03 16:49 UTC (permalink / raw)
To: The development of GNU GRUB
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 <grub/dl.h>
> #include <grub/deflate.h>
> #include <grub/i18n.h>
> +#include <grub/crypto.h>
>
> 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
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Implement checksum verification for gunzip
2016-05-03 16:49 ` Andrei Borzenkov
@ 2016-05-04 11:38 ` Stefan Fritsch
2017-07-20 12:12 ` Stefan Fritsch
0 siblings, 1 reply; 5+ messages in thread
From: Stefan Fritsch @ 2016-05-04 11:38 UTC (permalink / raw)
To: The development of GNU GRUB
[-- Attachment #1: Type: TEXT/PLAIN, Size: 1040 bytes --]
On Tue, 3 May 2016, Andrei Borzenkov wrote:
> 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.
Sure.
> > 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 <grub/dl.h>
> > #include <grub/deflate.h>
> > #include <grub/i18n.h>
> > +#include <grub/crypto.h>
> >
> > 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?
>
Yes. The struct is shared by zlib and gzip, and zlib uses adler instead of
crc32.
Cheers,
Stefan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Implement checksum verification for gunzip
2016-05-04 11:38 ` Stefan Fritsch
@ 2017-07-20 12:12 ` Stefan Fritsch
0 siblings, 0 replies; 5+ messages in thread
From: Stefan Fritsch @ 2017-07-20 12:12 UTC (permalink / raw)
To: The development of GNU GRUB
[-- Attachment #1: Type: TEXT/PLAIN, Size: 1225 bytes --]
On Wed, 4 May 2016, Stefan Fritsch wrote:
> On Tue, 3 May 2016, Andrei Borzenkov wrote:
>
> > 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.
>
> Sure.
Ping. 2.02 is released now. Should I resend the patch? It still applies.
Cheers,
Stefan
> > > 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 <grub/dl.h>
> > > #include <grub/deflate.h>
> > > #include <grub/i18n.h>
> > > +#include <grub/crypto.h>
> > >
> > > 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?
> >
>
> Yes. The struct is shared by zlib and gzip, and zlib uses adler instead of
> crc32.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Implement checksum verification for gunzip
2016-05-01 12:32 [PATCH] Implement checksum verification for gunzip Stefan Fritsch
2016-05-03 16:49 ` Andrei Borzenkov
@ 2017-08-30 14:08 ` Vladimir 'phcoder' Serbinenko
1 sibling, 0 replies; 5+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2017-08-30 14:08 UTC (permalink / raw)
To: The development of GNU GRUB
[-- Attachment #1: Type: text/plain, Size: 4311 bytes --]
Committed, thanks
Le Sun, May 1, 2016 à 2:33 PM, Stefan Fritsch <sf@sfritsch.de> a écrit :
> This implements the crc32 check for the gzip format. Support for zlib's
> adler checksum is not included, yet.
>
> 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 <grub/dl.h>
> #include <grub/deflate.h>
> #include <grub/i18n.h>
> +#include <grub/crypto.h>
>
> 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;
> + /* 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
>
[-- Attachment #2: Type: text/html, Size: 5395 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2017-08-30 14:08 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-01 12:32 [PATCH] Implement checksum verification for gunzip Stefan Fritsch
2016-05-03 16:49 ` Andrei Borzenkov
2016-05-04 11:38 ` Stefan Fritsch
2017-07-20 12:12 ` Stefan Fritsch
2017-08-30 14:08 ` Vladimir 'phcoder' Serbinenko
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.