~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to grub-core/lib/xzembed/xz_dec_stream.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* xz_dec_stream.c - .xz Stream decoder */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2010  Free Software Foundation, Inc.
 
5
 *
 
6
 *  GRUB is free software: you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation, either version 3 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  GRUB is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
/*
 
20
 * This file is based on code from XZ embedded project
 
21
 * http://tukaani.org/xz/embedded.html
 
22
 */
 
23
 
 
24
#include "xz_config.h"
 
25
#include "xz_private.h"
 
26
#include "xz_stream.h"
 
27
 
 
28
#include <grub/crypto.h>
 
29
 
 
30
/* Hash used to validate the Index field */
 
31
struct xz_dec_hash {
 
32
        vli_type unpadded;
 
33
        vli_type uncompressed;
 
34
#ifndef GRUB_EMBED_DECOMPRESSOR
 
35
        uint8_t *crc32_context;
 
36
#endif
 
37
};
 
38
 
 
39
struct xz_dec {
 
40
        /* Position in dec_main() */
 
41
        enum {
 
42
                SEQ_STREAM_HEADER,
 
43
                SEQ_BLOCK_START,
 
44
                SEQ_BLOCK_HEADER,
 
45
                SEQ_BLOCK_UNCOMPRESS,
 
46
                SEQ_BLOCK_PADDING,
 
47
                SEQ_BLOCK_CHECK,
 
48
                SEQ_INDEX,
 
49
                SEQ_INDEX_PADDING,
 
50
                SEQ_INDEX_CRC32,
 
51
                SEQ_STREAM_FOOTER
 
52
        } sequence;
 
53
 
 
54
        /* Position in variable-length integers and Check fields */
 
55
        uint32_t pos;
 
56
 
 
57
        /* Variable-length integer decoded by dec_vli() */
 
58
        vli_type vli;
 
59
 
 
60
        /* Saved in_pos and out_pos */
 
61
        size_t in_start;
 
62
        size_t out_start;
 
63
 
 
64
        /* CRC32 value in Block or Index */
 
65
        uint32_t crc32_temp; /* need for crc32_validate*/
 
66
        uint8_t *crc32_context;
 
67
 
 
68
        /* True if CRC32 is calculated from uncompressed data */
 
69
        bool has_crc32;
 
70
 
 
71
        /* True if we are operating in single-call mode. */
 
72
        bool single_call;
 
73
 
 
74
        /*
 
75
         * True if the next call to xz_dec_run() is allowed to return
 
76
         * XZ_BUF_ERROR.
 
77
         */
 
78
        bool allow_buf_error;
 
79
 
 
80
        /* Information stored in Block Header */
 
81
        struct {
 
82
                /*
 
83
                 * Value stored in the Compressed Size field, or
 
84
                 * VLI_UNKNOWN if Compressed Size is not present.
 
85
                 */
 
86
                vli_type compressed;
 
87
 
 
88
                /*
 
89
                 * Value stored in the Uncompressed Size field, or
 
90
                 * VLI_UNKNOWN if Uncompressed Size is not present.
 
91
                 */
 
92
                vli_type uncompressed;
 
93
 
 
94
                /* Size of the Block Header field */
 
95
                uint32_t size;
 
96
        } block_header;
 
97
 
 
98
        /* Information collected when decoding Blocks */
 
99
        struct {
 
100
                /* Observed compressed size of the current Block */
 
101
                vli_type compressed;
 
102
 
 
103
                /* Observed uncompressed size of the current Block */
 
104
                vli_type uncompressed;
 
105
 
 
106
                /* Number of Blocks decoded so far */
 
107
                vli_type count;
 
108
 
 
109
                /*
 
110
                 * Hash calculated from the Block sizes. This is used to
 
111
                 * validate the Index field.
 
112
                 */
 
113
                struct xz_dec_hash hash;
 
114
        } block;
 
115
 
 
116
        /* Variables needed when verifying the Index field */
 
117
        struct {
 
118
                /* Position in dec_index() */
 
119
                enum {
 
120
                        SEQ_INDEX_COUNT,
 
121
                        SEQ_INDEX_UNPADDED,
 
122
                        SEQ_INDEX_UNCOMPRESSED
 
123
                } sequence;
 
124
 
 
125
                /* Size of the Index in bytes */
 
126
                vli_type size;
 
127
 
 
128
                /* Number of Records (matches block.count in valid files) */
 
129
                vli_type count;
 
130
 
 
131
                /*
 
132
                 * Hash calculated from the Records (matches block.hash in
 
133
                 * valid files).
 
134
                 */
 
135
                struct xz_dec_hash hash;
 
136
        } index;
 
137
 
 
138
        /*
 
139
         * Temporary buffer needed to hold Stream Header, Block Header,
 
140
         * and Stream Footer. The Block Header is the biggest (1 KiB)
 
141
         * so we reserve space according to that. buf[] has to be aligned
 
142
         * to a multiple of four bytes; the size_t variables before it
 
143
         * should guarantee this.
 
144
         */
 
145
        struct {
 
146
                size_t pos;
 
147
                size_t size;
 
148
                uint8_t buf[1024];
 
149
        } temp;
 
150
 
 
151
        struct xz_dec_lzma2 *lzma2;
 
152
 
 
153
#ifdef XZ_DEC_BCJ
 
154
        struct xz_dec_bcj *bcj;
 
155
        bool bcj_active;
 
156
#endif
 
157
};
 
158
 
 
159
/*
 
160
 * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
 
161
 * must have set s->temp.pos to indicate how much data we are supposed
 
162
 * to copy into s->temp.buf. Return true once s->temp.pos has reached
 
163
 * s->temp.size.
 
164
 */
 
165
static bool fill_temp(struct xz_dec *s, struct xz_buf *b)
 
166
{
 
167
        size_t copy_size = min_t(size_t,
 
168
                        b->in_size - b->in_pos, s->temp.size - s->temp.pos);
 
169
 
 
170
        memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
 
171
        b->in_pos += copy_size;
 
172
        s->temp.pos += copy_size;
 
173
 
 
174
        if (s->temp.pos == s->temp.size) {
 
175
                s->temp.pos = 0;
 
176
                return true;
 
177
        }
 
178
 
 
179
        return false;
 
180
}
 
181
 
 
182
/* Decode a variable-length integer (little-endian base-128 encoding) */
 
183
static enum xz_ret dec_vli(struct xz_dec *s,
 
184
                const uint8_t *in, size_t *in_pos, size_t in_size)
 
185
{
 
186
        uint8_t byte;
 
187
 
 
188
        if (s->pos == 0)
 
189
                s->vli = 0;
 
190
 
 
191
        while (*in_pos < in_size) {
 
192
                byte = in[*in_pos];
 
193
                ++*in_pos;
 
194
 
 
195
                s->vli |= (vli_type)(byte & 0x7F) << s->pos;
 
196
 
 
197
                if ((byte & 0x80) == 0) {
 
198
                        /* Don't allow non-minimal encodings. */
 
199
                        if (byte == 0 && s->pos != 0)
 
200
                                return XZ_DATA_ERROR;
 
201
 
 
202
                        s->pos = 0;
 
203
                        return XZ_STREAM_END;
 
204
                }
 
205
 
 
206
                s->pos += 7;
 
207
                if (s->pos == 7 * VLI_BYTES_MAX)
 
208
                        return XZ_DATA_ERROR;
 
209
        }
 
210
 
 
211
        return XZ_OK;
 
212
}
 
213
 
 
214
/*
 
215
 * Decode the Compressed Data field from a Block. Update and validate
 
216
 * the observed compressed and uncompressed sizes of the Block so that
 
217
 * they don't exceed the values possibly stored in the Block Header
 
218
 * (validation assumes that no integer overflow occurs, since vli_type
 
219
 * is normally uint64_t). Update the CRC32 if presence of the CRC32
 
220
 * field was indicated in Stream Header.
 
221
 *
 
222
 * Once the decoding is finished, validate that the observed sizes match
 
223
 * the sizes possibly stored in the Block Header. Update the hash and
 
224
 * Block count, which are later used to validate the Index field.
 
225
 */
 
226
static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)
 
227
{
 
228
        enum xz_ret ret;
 
229
 
 
230
        s->in_start = b->in_pos;
 
231
        s->out_start = b->out_pos;
 
232
 
 
233
#ifdef XZ_DEC_BCJ
 
234
        if (s->bcj_active)
 
235
                ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
 
236
        else
 
237
#endif
 
238
                ret = xz_dec_lzma2_run(s->lzma2, b);
 
239
 
 
240
        s->block.compressed += b->in_pos - s->in_start;
 
241
        s->block.uncompressed += b->out_pos - s->out_start;
 
242
 
 
243
        /*
 
244
         * There is no need to separately check for VLI_UNKNOWN, since
 
245
         * the observed sizes are always smaller than VLI_UNKNOWN.
 
246
         */
 
247
        if (s->block.compressed > s->block_header.compressed
 
248
                        || s->block.uncompressed
 
249
                                > s->block_header.uncompressed)
 
250
                return XZ_DATA_ERROR;
 
251
 
 
252
#ifndef GRUB_EMBED_DECOMPRESSOR
 
253
        if (s->has_crc32)
 
254
          GRUB_MD_CRC32->write(s->crc32_context,b->out + s->out_start,
 
255
                                b->out_pos - s->out_start);
 
256
#endif
 
257
 
 
258
        if (ret == XZ_STREAM_END) {
 
259
                if (s->block_header.compressed != VLI_UNKNOWN
 
260
                                && s->block_header.compressed
 
261
                                        != s->block.compressed)
 
262
                        return XZ_DATA_ERROR;
 
263
 
 
264
                if (s->block_header.uncompressed != VLI_UNKNOWN
 
265
                                && s->block_header.uncompressed
 
266
                                        != s->block.uncompressed)
 
267
                        return XZ_DATA_ERROR;
 
268
 
 
269
                s->block.hash.unpadded += s->block_header.size
 
270
                                + s->block.compressed;
 
271
                if (s->has_crc32)
 
272
                        s->block.hash.unpadded += 4;
 
273
 
 
274
                s->block.hash.uncompressed += s->block.uncompressed;
 
275
 
 
276
#ifndef GRUB_EMBED_DECOMPRESSOR
 
277
                GRUB_MD_CRC32->write(s->block.hash.crc32_context,
 
278
                                (const uint8_t *)&s->block.hash, 2 * sizeof(vli_type));
 
279
#endif
 
280
 
 
281
                ++s->block.count;
 
282
        }
 
283
 
 
284
        return ret;
 
285
}
 
286
 
 
287
/* Update the Index size and the CRC32 value. */
 
288
static void index_update(struct xz_dec *s, const struct xz_buf *b)
 
289
{
 
290
        size_t in_used = b->in_pos - s->in_start;
 
291
        s->index.size += in_used;
 
292
#ifndef GRUB_EMBED_DECOMPRESSOR
 
293
        GRUB_MD_CRC32->write(s->crc32_context,b->in + s->in_start, in_used);
 
294
#endif
 
295
}
 
296
 
 
297
/*
 
298
 * Decode the Number of Records, Unpadded Size, and Uncompressed Size
 
299
 * fields from the Index field. That is, Index Padding and CRC32 are not
 
300
 * decoded by this function.
 
301
 *
 
302
 * This can return XZ_OK (more input needed), XZ_STREAM_END (everything
 
303
 * successfully decoded), or XZ_DATA_ERROR (input is corrupt).
 
304
 */
 
305
static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b)
 
306
{
 
307
        enum xz_ret ret;
 
308
 
 
309
        do {
 
310
                ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
 
311
                if (ret != XZ_STREAM_END) {
 
312
                        index_update(s, b);
 
313
                        return ret;
 
314
                }
 
315
 
 
316
                switch (s->index.sequence) {
 
317
                case SEQ_INDEX_COUNT:
 
318
                        s->index.count = s->vli;
 
319
 
 
320
                        /*
 
321
                         * Validate that the Number of Records field
 
322
                         * indicates the same number of Records as
 
323
                         * there were Blocks in the Stream.
 
324
                         */
 
325
                        if (s->index.count != s->block.count)
 
326
                                return XZ_DATA_ERROR;
 
327
 
 
328
                        s->index.sequence = SEQ_INDEX_UNPADDED;
 
329
                        break;
 
330
 
 
331
                case SEQ_INDEX_UNPADDED:
 
332
                        s->index.hash.unpadded += s->vli;
 
333
                        s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
 
334
                        break;
 
335
 
 
336
                case SEQ_INDEX_UNCOMPRESSED:
 
337
                        s->index.hash.uncompressed += s->vli;
 
338
 
 
339
#ifndef GRUB_EMBED_DECOMPRESSOR
 
340
                        GRUB_MD_CRC32->write(s->index.hash.crc32_context,
 
341
                                        (const uint8_t *)&s->index.hash, 2 * sizeof(vli_type));
 
342
#endif
 
343
 
 
344
                        --s->index.count;
 
345
                        s->index.sequence = SEQ_INDEX_UNPADDED;
 
346
                        break;
 
347
                }
 
348
        } while (s->index.count > 0);
 
349
 
 
350
        return XZ_STREAM_END;
 
351
}
 
352
 
 
353
/*
 
354
 * Validate that the next four input bytes match the value of s->crc32.
 
355
 * s->pos must be zero when starting to validate the first byte.
 
356
 */
 
357
static enum xz_ret crc32_validate(struct xz_dec *s, struct xz_buf *b)
 
358
{
 
359
#ifndef GRUB_EMBED_DECOMPRESSOR
 
360
        if(s->crc32_temp == 0)
 
361
        {
 
362
          GRUB_MD_CRC32->final(s->crc32_context);
 
363
                s->crc32_temp = get_unaligned_be32(GRUB_MD_CRC32->read(s->crc32_context));
 
364
        }
 
365
#endif
 
366
 
 
367
        do {
 
368
                if (b->in_pos == b->in_size)
 
369
                        return XZ_OK;
 
370
 
 
371
#ifndef GRUB_EMBED_DECOMPRESSOR
 
372
                if (((s->crc32_temp >> s->pos) & 0xFF) != b->in[b->in_pos++])
 
373
                        return XZ_DATA_ERROR;
 
374
#endif
 
375
 
 
376
                s->pos += 8;
 
377
 
 
378
        } while (s->pos < 32);
 
379
 
 
380
#ifndef GRUB_EMBED_DECOMPRESSOR
 
381
        GRUB_MD_CRC32->init(s->crc32_context);
 
382
#endif
 
383
        s->crc32_temp = 0;
 
384
        s->pos = 0;
 
385
 
 
386
        return XZ_STREAM_END;
 
387
}
 
388
 
 
389
/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
 
390
static enum xz_ret dec_stream_header(struct xz_dec *s)
 
391
{
 
392
        if (! memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
 
393
                return XZ_FORMAT_ERROR;
 
394
 
 
395
#ifndef GRUB_EMBED_DECOMPRESSOR
 
396
        uint8_t crc32_context[GRUB_MD_CRC32->contextsize];
 
397
 
 
398
        GRUB_MD_CRC32->init(crc32_context);
 
399
        GRUB_MD_CRC32->write(crc32_context,s->temp.buf + HEADER_MAGIC_SIZE, 2);
 
400
        GRUB_MD_CRC32->final(crc32_context);
 
401
 
 
402
        uint32_t resultcrc = get_unaligned_be32(GRUB_MD_CRC32->read(crc32_context));
 
403
        uint32_t readcrc = get_unaligned_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2);
 
404
 
 
405
        if(resultcrc != readcrc)
 
406
                return XZ_DATA_ERROR;
 
407
#endif
 
408
 
 
409
        /*
 
410
         * Decode the Stream Flags field. Of integrity checks, we support
 
411
         * only none (Check ID = 0) and CRC32 (Check ID = 1).
 
412
         */
 
413
        if (s->temp.buf[HEADER_MAGIC_SIZE] != 0
 
414
                        || s->temp.buf[HEADER_MAGIC_SIZE + 1] > 1)
 
415
                return XZ_OPTIONS_ERROR;
 
416
 
 
417
        s->has_crc32 = s->temp.buf[HEADER_MAGIC_SIZE + 1];
 
418
 
 
419
        return XZ_OK;
 
420
}
 
421
 
 
422
/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
 
423
static enum xz_ret dec_stream_footer(struct xz_dec *s)
 
424
{
 
425
        if (! memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
 
426
                return XZ_DATA_ERROR;
 
427
 
 
428
#ifndef GRUB_EMBED_DECOMPRESSOR
 
429
        uint8_t crc32_context[GRUB_MD_CRC32->contextsize];
 
430
 
 
431
        GRUB_MD_CRC32->init(crc32_context);
 
432
        GRUB_MD_CRC32->write(crc32_context, s->temp.buf + 4, 6);
 
433
        GRUB_MD_CRC32->final(crc32_context);
 
434
 
 
435
        uint32_t resultcrc = get_unaligned_be32(GRUB_MD_CRC32->read(crc32_context));
 
436
        uint32_t readcrc = get_unaligned_le32(s->temp.buf);
 
437
 
 
438
        if(resultcrc != readcrc)
 
439
                return XZ_DATA_ERROR;
 
440
#endif
 
441
 
 
442
        /*
 
443
         * Validate Backward Size. Note that we never added the size of the
 
444
         * Index CRC32 field to s->index.size, thus we use s->index.size / 4
 
445
         * instead of s->index.size / 4 - 1.
 
446
         */
 
447
        if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
 
448
                return XZ_DATA_ERROR;
 
449
 
 
450
        if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->has_crc32)
 
451
                return XZ_DATA_ERROR;
 
452
 
 
453
        /*
 
454
         * Use XZ_STREAM_END instead of XZ_OK to be more convenient
 
455
         * for the caller.
 
456
         */
 
457
        return XZ_STREAM_END;
 
458
}
 
459
 
 
460
/* Decode the Block Header and initialize the filter chain. */
 
461
static enum xz_ret dec_block_header(struct xz_dec *s)
 
462
{
 
463
        enum xz_ret ret;
 
464
 
 
465
        /*
 
466
         * Validate the CRC32. We know that the temp buffer is at least
 
467
         * eight bytes so this is safe.
 
468
         */
 
469
        s->temp.size -= 4;
 
470
#ifndef GRUB_EMBED_DECOMPRESSOR
 
471
        uint8_t crc32_context[GRUB_MD_CRC32->contextsize];
 
472
 
 
473
        GRUB_MD_CRC32->init(crc32_context);
 
474
        GRUB_MD_CRC32->write(crc32_context, s->temp.buf, s->temp.size);
 
475
        GRUB_MD_CRC32->final(crc32_context);
 
476
 
 
477
        uint32_t resultcrc = get_unaligned_be32(GRUB_MD_CRC32->read(crc32_context));
 
478
        uint32_t readcrc = get_unaligned_le32(s->temp.buf + s->temp.size);
 
479
 
 
480
        if (resultcrc != readcrc)
 
481
                return XZ_DATA_ERROR;
 
482
#endif
 
483
 
 
484
        s->temp.pos = 2;
 
485
 
 
486
        /*
 
487
         * Catch unsupported Block Flags. We support only one or two filters
 
488
         * in the chain, so we catch that with the same test.
 
489
         */
 
490
#ifdef XZ_DEC_BCJ
 
491
        if (s->temp.buf[1] & 0x3E)
 
492
#else
 
493
        if (s->temp.buf[1] & 0x3F)
 
494
#endif
 
495
                return XZ_OPTIONS_ERROR;
 
496
 
 
497
        /* Compressed Size */
 
498
        if (s->temp.buf[1] & 0x40) {
 
499
                if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
 
500
                                        != XZ_STREAM_END)
 
501
                        return XZ_DATA_ERROR;
 
502
 
 
503
                s->block_header.compressed = s->vli;
 
504
        } else {
 
505
                s->block_header.compressed = VLI_UNKNOWN;
 
506
        }
 
507
 
 
508
        /* Uncompressed Size */
 
509
        if (s->temp.buf[1] & 0x80) {
 
510
                if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
 
511
                                != XZ_STREAM_END)
 
512
                        return XZ_DATA_ERROR;
 
513
 
 
514
                s->block_header.uncompressed = s->vli;
 
515
        } else {
 
516
                s->block_header.uncompressed = VLI_UNKNOWN;
 
517
        }
 
518
 
 
519
#ifdef XZ_DEC_BCJ
 
520
        /* If there are two filters, the first one must be a BCJ filter. */
 
521
        s->bcj_active = s->temp.buf[1] & 0x01;
 
522
        if (s->bcj_active) {
 
523
                if (s->temp.size - s->temp.pos < 2)
 
524
                        return XZ_OPTIONS_ERROR;
 
525
 
 
526
                ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
 
527
                if (ret != XZ_OK)
 
528
                        return ret;
 
529
 
 
530
                /*
 
531
                 * We don't support custom start offset,
 
532
                 * so Size of Properties must be zero.
 
533
                 */
 
534
                if (s->temp.buf[s->temp.pos++] != 0x00)
 
535
                        return XZ_OPTIONS_ERROR;
 
536
        }
 
537
#endif
 
538
 
 
539
        /* Valid Filter Flags always take at least two bytes. */
 
540
        if (s->temp.size - s->temp.pos < 2)
 
541
                return XZ_DATA_ERROR;
 
542
 
 
543
        /* Filter ID = LZMA2 */
 
544
        if (s->temp.buf[s->temp.pos++] != 0x21)
 
545
                return XZ_OPTIONS_ERROR;
 
546
 
 
547
        /* Size of Properties = 1-byte Filter Properties */
 
548
        if (s->temp.buf[s->temp.pos++] != 0x01)
 
549
                return XZ_OPTIONS_ERROR;
 
550
 
 
551
        /* Filter Properties contains LZMA2 dictionary size. */
 
552
        if (s->temp.size - s->temp.pos < 1)
 
553
                return XZ_DATA_ERROR;
 
554
 
 
555
        ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
 
556
        if (ret != XZ_OK)
 
557
                return ret;
 
558
 
 
559
        /* The rest must be Header Padding. */
 
560
        while (s->temp.pos < s->temp.size)
 
561
                if (s->temp.buf[s->temp.pos++] != 0x00)
 
562
                        return XZ_OPTIONS_ERROR;
 
563
 
 
564
        s->temp.pos = 0;
 
565
        s->block.compressed = 0;
 
566
        s->block.uncompressed = 0;
 
567
 
 
568
        return XZ_OK;
 
569
}
 
570
 
 
571
static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b)
 
572
{
 
573
        enum xz_ret ret;
 
574
 
 
575
        /*
 
576
         * Store the start position for the case when we are in the middle
 
577
         * of the Index field.
 
578
         */
 
579
        s->in_start = b->in_pos;
 
580
 
 
581
        while (true) {
 
582
                switch (s->sequence) {
 
583
                case SEQ_STREAM_HEADER:
 
584
                        /*
 
585
                         * Stream Header is copied to s->temp, and then
 
586
                         * decoded from there. This way if the caller
 
587
                         * gives us only little input at a time, we can
 
588
                         * still keep the Stream Header decoding code
 
589
                         * simple. Similar approach is used in many places
 
590
                         * in this file.
 
591
                         */
 
592
                        if (!fill_temp(s, b))
 
593
                                return XZ_OK;
 
594
 
 
595
                        ret = dec_stream_header(s);
 
596
                        if (ret != XZ_OK)
 
597
                                return ret;
 
598
 
 
599
                        s->sequence = SEQ_BLOCK_START;
 
600
 
 
601
                case SEQ_BLOCK_START:
 
602
                        /* We need one byte of input to continue. */
 
603
                        if (b->in_pos == b->in_size)
 
604
                                return XZ_OK;
 
605
 
 
606
                        /* See if this is the beginning of the Index field. */
 
607
                        if (b->in[b->in_pos] == 0) {
 
608
                                s->in_start = b->in_pos++;
 
609
                                s->sequence = SEQ_INDEX;
 
610
                                break;
 
611
                        }
 
612
 
 
613
                        /*
 
614
                         * Calculate the size of the Block Header and
 
615
                         * prepare to decode it.
 
616
                         */
 
617
                        s->block_header.size
 
618
                                = ((uint32_t)b->in[b->in_pos] + 1) * 4;
 
619
 
 
620
                        s->temp.size = s->block_header.size;
 
621
                        s->temp.pos = 0;
 
622
                        s->sequence = SEQ_BLOCK_HEADER;
 
623
 
 
624
                case SEQ_BLOCK_HEADER:
 
625
                        if (!fill_temp(s, b))
 
626
                                return XZ_OK;
 
627
 
 
628
                        ret = dec_block_header(s);
 
629
                        if (ret != XZ_OK)
 
630
                                return ret;
 
631
 
 
632
                        s->sequence = SEQ_BLOCK_UNCOMPRESS;
 
633
 
 
634
                case SEQ_BLOCK_UNCOMPRESS:
 
635
                        ret = dec_block(s, b);
 
636
                        if (ret != XZ_STREAM_END)
 
637
                                return ret;
 
638
 
 
639
                        s->sequence = SEQ_BLOCK_PADDING;
 
640
 
 
641
                case SEQ_BLOCK_PADDING:
 
642
                        /*
 
643
                         * Size of Compressed Data + Block Padding
 
644
                         * must be a multiple of four. We don't need
 
645
                         * s->block.compressed for anything else
 
646
                         * anymore, so we use it here to test the size
 
647
                         * of the Block Padding field.
 
648
                         */
 
649
                        while (s->block.compressed & 3) {
 
650
                                if (b->in_pos == b->in_size)
 
651
                                        return XZ_OK;
 
652
 
 
653
                                if (b->in[b->in_pos++] != 0)
 
654
                                        return XZ_DATA_ERROR;
 
655
 
 
656
                                ++s->block.compressed;
 
657
                        }
 
658
 
 
659
                        s->sequence = SEQ_BLOCK_CHECK;
 
660
 
 
661
                case SEQ_BLOCK_CHECK:
 
662
                        if (s->has_crc32) {
 
663
                                ret = crc32_validate(s, b);
 
664
                                if (ret != XZ_STREAM_END)
 
665
                                        return ret;
 
666
                        }
 
667
 
 
668
                        s->sequence = SEQ_BLOCK_START;
 
669
                        break;
 
670
 
 
671
                case SEQ_INDEX:
 
672
                        ret = dec_index(s, b);
 
673
                        if (ret != XZ_STREAM_END)
 
674
                                return ret;
 
675
 
 
676
                        s->sequence = SEQ_INDEX_PADDING;
 
677
 
 
678
                case SEQ_INDEX_PADDING:
 
679
                        while ((s->index.size + (b->in_pos - s->in_start))
 
680
                                        & 3) {
 
681
                                if (b->in_pos == b->in_size) {
 
682
                                        index_update(s, b);
 
683
                                        return XZ_OK;
 
684
                                }
 
685
 
 
686
                                if (b->in[b->in_pos++] != 0)
 
687
                                        return XZ_DATA_ERROR;
 
688
                        }
 
689
 
 
690
                        /* Finish the CRC32 value and Index size. */
 
691
                        index_update(s, b);
 
692
 
 
693
#ifndef GRUB_EMBED_DECOMPRESSOR
 
694
                        /* Compare the hashes to validate the Index field. */
 
695
                        GRUB_MD_CRC32->final(s->block.hash.crc32_context);
 
696
                        GRUB_MD_CRC32->final(s->index.hash.crc32_context);
 
697
                        uint32_t block_crc = *(uint32_t*)GRUB_MD_CRC32->read(s->block.hash.crc32_context);
 
698
                        uint32_t index_crc = *(uint32_t*)GRUB_MD_CRC32->read(s->index.hash.crc32_context);
 
699
 
 
700
                        if (s->block.hash.unpadded != s->index.hash.unpadded
 
701
                            || s->block.hash.uncompressed != s->index.hash.uncompressed
 
702
                            || block_crc != index_crc)
 
703
                        {
 
704
                                return XZ_DATA_ERROR;
 
705
                        }
 
706
#endif
 
707
 
 
708
                        s->sequence = SEQ_INDEX_CRC32;
 
709
 
 
710
                case SEQ_INDEX_CRC32:
 
711
                        ret = crc32_validate(s, b);
 
712
                        if (ret != XZ_STREAM_END)
 
713
                                return ret;
 
714
 
 
715
                        s->temp.size = STREAM_HEADER_SIZE;
 
716
                        s->sequence = SEQ_STREAM_FOOTER;
 
717
 
 
718
                case SEQ_STREAM_FOOTER:
 
719
                        if (!fill_temp(s, b))
 
720
                                return XZ_OK;
 
721
 
 
722
                        return dec_stream_footer(s);
 
723
                }
 
724
        }
 
725
 
 
726
        /* Never reached */
 
727
}
 
728
 
 
729
/*
 
730
 * xz_dec_run() is a wrapper for dec_main() to handle some special cases in
 
731
 * multi-call and single-call decoding.
 
732
 *
 
733
 * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
 
734
 * are not going to make any progress anymore. This is to prevent the caller
 
735
 * from calling us infinitely when the input file is truncated or otherwise
 
736
 * corrupt. Since zlib-style API allows that the caller fills the input buffer
 
737
 * only when the decoder doesn't produce any new output, we have to be careful
 
738
 * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
 
739
 * after the second consecutive call to xz_dec_run() that makes no progress.
 
740
 *
 
741
 * In single-call mode, if we couldn't decode everything and no error
 
742
 * occurred, either the input is truncated or the output buffer is too small.
 
743
 * Since we know that the last input byte never produces any output, we know
 
744
 * that if all the input was consumed and decoding wasn't finished, the file
 
745
 * must be corrupt. Otherwise the output buffer has to be too small or the
 
746
 * file is corrupt in a way that decoding it produces too big output.
 
747
 *
 
748
 * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
 
749
 * their original values. This is because with some filter chains there won't
 
750
 * be any valid uncompressed data in the output buffer unless the decoding
 
751
 * actually succeeds (that's the price to pay of using the output buffer as
 
752
 * the workspace).
 
753
 */
 
754
enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)
 
755
{
 
756
        size_t in_start;
 
757
        size_t out_start;
 
758
        enum xz_ret ret;
 
759
 
 
760
        if (s->single_call)
 
761
                xz_dec_reset(s);
 
762
 
 
763
        in_start = b->in_pos;
 
764
        out_start = b->out_pos;
 
765
        ret = dec_main(s, b);
 
766
 
 
767
        if (s->single_call) {
 
768
                if (ret == XZ_OK)
 
769
                        ret = b->in_pos == b->in_size
 
770
                                        ? XZ_DATA_ERROR : XZ_BUF_ERROR;
 
771
 
 
772
                if (ret != XZ_STREAM_END) {
 
773
                        b->in_pos = in_start;
 
774
                        b->out_pos = out_start;
 
775
                }
 
776
 
 
777
        } else if (ret == XZ_OK && in_start == b->in_pos
 
778
                        && out_start == b->out_pos) {
 
779
                if (s->allow_buf_error)
 
780
                        ret = XZ_BUF_ERROR;
 
781
 
 
782
                s->allow_buf_error = true;
 
783
        } else {
 
784
                s->allow_buf_error = false;
 
785
        }
 
786
 
 
787
        return ret;
 
788
}
 
789
 
 
790
#ifdef GRUB_EMBED_DECOMPRESSOR
 
791
struct xz_dec decoder;
 
792
#endif
 
793
 
 
794
struct xz_dec * xz_dec_init(uint32_t dict_max)
 
795
{
 
796
        struct xz_dec *s;
 
797
#ifdef GRUB_EMBED_DECOMPRESSOR
 
798
        s = &decoder;
 
799
#else
 
800
        s = kmalloc(sizeof(*s), GFP_KERNEL);
 
801
        if (s == NULL)
 
802
                return NULL;
 
803
#endif
 
804
 
 
805
#ifndef GRUB_EMBED_DECOMPRESSOR
 
806
        /* prepare CRC32 calculators */
 
807
        if(GRUB_MD_CRC32 == NULL)
 
808
        {
 
809
                kfree(s);
 
810
                return NULL;
 
811
        }
 
812
 
 
813
        s->crc32_context = kmalloc(GRUB_MD_CRC32->contextsize, GFP_KERNEL);
 
814
        if (s->crc32_context == NULL)
 
815
        {
 
816
                kfree(s);
 
817
                return NULL;
 
818
        }
 
819
 
 
820
        s->index.hash.crc32_context = kmalloc(GRUB_MD_CRC32->contextsize, GFP_KERNEL);
 
821
        if (s->index.hash.crc32_context == NULL)
 
822
        {
 
823
                kfree(s->crc32_context);
 
824
                kfree(s);
 
825
                return NULL;
 
826
        }
 
827
 
 
828
        s->block.hash.crc32_context = kmalloc(GRUB_MD_CRC32->contextsize, GFP_KERNEL);
 
829
        if (s->block.hash.crc32_context == NULL)
 
830
        {
 
831
                kfree(s->index.hash.crc32_context);
 
832
                kfree(s->crc32_context);
 
833
                kfree(s);
 
834
                return NULL;
 
835
        }
 
836
 
 
837
 
 
838
        GRUB_MD_CRC32->init(s->crc32_context);
 
839
        GRUB_MD_CRC32->init(s->index.hash.crc32_context);
 
840
        GRUB_MD_CRC32->init(s->block.hash.crc32_context);
 
841
#endif
 
842
 
 
843
        s->crc32_temp = 0;
 
844
 
 
845
        s->single_call = dict_max == 0;
 
846
 
 
847
#ifdef XZ_DEC_BCJ
 
848
        s->bcj = xz_dec_bcj_create(s->single_call);
 
849
        if (s->bcj == NULL)
 
850
                goto error_bcj;
 
851
#endif
 
852
 
 
853
        s->lzma2 = xz_dec_lzma2_create(dict_max);
 
854
        if (s->lzma2 == NULL)
 
855
                goto error_lzma2;
 
856
 
 
857
        xz_dec_reset(s);
 
858
        return s;
 
859
 
 
860
error_lzma2:
 
861
#ifdef XZ_DEC_BCJ
 
862
        xz_dec_bcj_end(s->bcj);
 
863
error_bcj:
 
864
#endif
 
865
#ifndef GRUB_EMBED_DECOMPRESSOR
 
866
        kfree(s);
 
867
#endif
 
868
        return NULL;
 
869
}
 
870
 
 
871
void xz_dec_reset(struct xz_dec *s)
 
872
{
 
873
        s->sequence = SEQ_STREAM_HEADER;
 
874
        s->allow_buf_error = false;
 
875
        s->pos = 0;
 
876
 
 
877
        {
 
878
#ifndef GRUB_EMBED_DECOMPRESSOR
 
879
                uint8_t *t;
 
880
                t = s->block.hash.crc32_context;
 
881
#endif
 
882
                memzero(&s->block, sizeof(s->block));
 
883
#ifndef GRUB_EMBED_DECOMPRESSOR
 
884
                s->block.hash.crc32_context = t;
 
885
                t = s->index.hash.crc32_context;
 
886
#endif
 
887
                memzero(&s->index, sizeof(s->index));
 
888
#ifndef GRUB_EMBED_DECOMPRESSOR
 
889
                s->index.hash.crc32_context = t;
 
890
#endif
 
891
        }
 
892
        s->temp.pos = 0;
 
893
        s->temp.size = STREAM_HEADER_SIZE;
 
894
 
 
895
#ifndef GRUB_EMBED_DECOMPRESSOR
 
896
        GRUB_MD_CRC32->init(s->crc32_context);
 
897
        GRUB_MD_CRC32->init(s->index.hash.crc32_context);
 
898
        GRUB_MD_CRC32->init(s->block.hash.crc32_context);
 
899
#endif
 
900
        s->crc32_temp = 0;
 
901
}
 
902
 
 
903
void xz_dec_end(struct xz_dec *s)
 
904
{
 
905
        if (s != NULL) {
 
906
                xz_dec_lzma2_end(s->lzma2);
 
907
#ifndef GRUB_EMBED_DECOMPRESSOR
 
908
                kfree(s->index.hash.crc32_context);
 
909
                kfree(s->block.hash.crc32_context);
 
910
                kfree(s->crc32_context);
 
911
#endif
 
912
#ifdef XZ_DEC_BCJ
 
913
                xz_dec_bcj_end(s->bcj);
 
914
#endif
 
915
#ifndef GRUB_EMBED_DECOMPRESSOR
 
916
                kfree(s);
 
917
#endif
 
918
        }
 
919
}