Changes in 4.9.291 binder: use euid from cred instead of using task binder: use cred instead of task for selinux checks xhci: Fix USB 3.1 enumeration issues by increasing roothub power-on-good delay Input: elantench - fix misreporting trackpoint coordinates Input: i8042 - Add quirk for Fujitsu Lifebook T725 libata: fix read log timeout value ocfs2: fix data corruption on truncate mmc: dw_mmc: Dont wait for DRTO on Write RSP error parisc: Fix ptrace check on syscall return media: ite-cir: IR receiver stop working after receive overflow ALSA: ua101: fix division by zero at probe ALSA: 6fire: fix control and bulk message timeouts ALSA: line6: fix control and interrupt message timeouts ALSA: synth: missing check for possible NULL after the call to kstrdup ALSA: timer: Fix use-after-free problem ALSA: timer: Unconditionally unlink slave instances, too x86/irq: Ensure PI wakeup handler is unregistered before module unload sfc: Don't use netif_info before net_device setup hyperv/vmbus: include linux/bitops.h mmc: winbond: don't build on M68K bpf: Prevent increasing bpf_jit_limit above max xen/netfront: stop tx queues during live migration spi: spl022: fix Microwire full duplex mode watchdog: Fix OMAP watchdog early handling vmxnet3: do not stop tx queues after netif_device_detach() btrfs: fix lost error handling when replaying directory deletes hwmon: (pmbus/lm25066) Add offset coefficients regulator: s5m8767: do not use reset value as DVS voltage if GPIO DVS is disabled regulator: dt-bindings: samsung,s5m8767: correct s5m8767,pmic-buck-default-dvs-idx property EDAC/sb_edac: Fix top-of-high-memory value for Broadwell/Haswell mwifiex: fix division by zero in fw download path ath6kl: fix division by zero in send path ath6kl: fix control-message timeout PCI: Mark Atheros QCA6174 to avoid bus reset rtl8187: fix control-message timeouts evm: mark evm_fixmode as __ro_after_init wcn36xx: Fix HT40 capability for 2Ghz band mwifiex: Read a PCI register after writing the TX ring write pointer wcn36xx: handle connection loss indication RDMA/qedr: Fix NULL deref for query_qp on the GSI QP signal: Remove the bogus sigkill_pending in ptrace_stop signal/mips: Update (_save|_restore)_fp_context to fail with -EFAULT power: supply: max17042_battery: Prevent int underflow in set_soc_threshold power: supply: max17042_battery: use VFSOC for capacity when no rsns powerpc/85xx: Fix oops when mpc85xx_smp_guts_ids node cannot be found serial: core: Fix initializing and restoring termios speed ALSA: mixer: oss: Fix racy access to slots ALSA: mixer: fix deadlock in snd_mixer_oss_set_volume PCI: aardvark: Read all 16-bits from PCIE_MSI_PAYLOAD_REG quota: check block number when reading the block in quota file quota: correct error number in free_dqentry() iio: dac: ad5446: Fix ad5622_write() return value USB: serial: keyspan: fix memleak on probe errors USB: iowarrior: fix control-message timeouts Bluetooth: sco: Fix lock_sock() blockage by memcpy_from_msg() Bluetooth: fix use-after-free error in lock_sock_nested() platform/x86: wmi: do not fail if disabling fails MIPS: lantiq: dma: add small delay after reset MIPS: lantiq: dma: reset correct number of channel locking/lockdep: Avoid RCU-induced noinstr fail smackfs: Fix use-after-free in netlbl_catmap_walk() x86: Increase exception stack sizes media: mt9p031: Fix corrupted frame after restarting stream media: netup_unidvb: handle interrupt properly according to the firmware media: uvcvideo: Set capability in s_param media: s5p-mfc: fix possible null-pointer dereference in s5p_mfc_probe() media: mceusb: return without resubmitting URB in case of -EPROTO error. ia64: don't do IA64_CMPXCHG_DEBUG without CONFIG_PRINTK ACPICA: Avoid evaluating methods too early during system resume media: usb: dvd-usb: fix uninit-value bug in dibusb_read_eeprom_byte() tracefs: Have tracefs directories not set OTH permission bits by default ath: dfs_pattern_detector: Fix possible null-pointer dereference in channel_detector_create() ACPI: battery: Accept charges over the design capacity as full memstick: r592: Fix a UAF bug when removing the driver lib/xz: Avoid overlapping memcpy() with invalid input with in-place decompression lib/xz: Validate the value before assigning it to an enum variable tracing/cfi: Fix cmp_entries_* functions signature mismatch mwl8k: Fix use-after-free in mwl8k_fw_state_machine() PM: hibernate: Get block device exclusively in swsusp_check() iwlwifi: mvm: disable RX-diversity in powersave smackfs: use __GFP_NOFAIL for smk_cipso_doi() ARM: clang: Do not rely on lr register for stacktrace ARM: 9136/1: ARMv7-M uses BE-8, not BE-32 spi: bcm-qspi: Fix missing clk_disable_unprepare() on error in bcm_qspi_probe() parisc: fix warning in flush_tlb_all parisc/kgdb: add kgdb_roundup() to make kgdb work with idle polling cgroup: Make rebind_subsystems() disable v2 controllers all at once media: dvb-usb: fix ununit-value in az6027_rc_query media: mtk-vpu: Fix a resource leak in the error handling path of 'mtk_vpu_probe()' media: si470x: Avoid card name truncation cpuidle: Fix kobject memory leaks in error paths ath9k: Fix potential interrupt storm on queue reset crypto: qat - detect PFVF collision after ACK crypto: qat - disregard spurious PFVF interrupts b43legacy: fix a lower bounds test b43: fix a lower bounds test memstick: avoid out-of-range warning memstick: jmb38x_ms: use appropriate free function in jmb38x_ms_alloc_host() hwmon: Fix possible memleak in __hwmon_device_register() ath10k: fix max antenna gain unit drm/msm: uninitialized variable in msm_gem_import() net: stream: don't purge sk_error_queue in sk_stream_kill_queues() mmc: mxs-mmc: disable regulator on error and in the remove function platform/x86: thinkpad_acpi: Fix bitwise vs. logical warning mwifiex: Send DELBA requests according to spec phy: micrel: ksz8041nl: do not use power down mode smackfs: use netlbl_cfg_cipsov4_del() for deleting cipso_v4_doi s390/gmap: don't unconditionally call pte_unmap_unlock() in __gmap_zap() irq: mips: avoid nested irq_enter() samples/kretprobes: Fix return value if register_kretprobe() failed libertas_tf: Fix possible memory leak in probe and disconnect libertas: Fix possible memory leak in probe and disconnect crypto: pcrypt - Delay write to padata->info RDMA/rxe: Fix wrong port_cap_flags ARM: s3c: irq-s3c24xx: Fix return value check for s3c24xx_init_intc() scsi: dc395: Fix error case unwinding MIPS: loongson64: make CPU_LOONGSON64 depends on MIPS_FP_SUPPORT JFS: fix memleak in jfs_mount arm: dts: omap3-gta04a4: accelerometer irq fix soc/tegra: Fix an error handling path in tegra_powergate_power_up() memory: fsl_ifc: fix leak of irq and nand_irq in fsl_ifc_ctrl_probe video: fbdev: chipsfb: use memset_io() instead of memset() serial: 8250_dw: Drop wrong use of ACPI_PTR() usb: gadget: hid: fix error code in do_config() power: supply: rt5033_battery: Change voltage values to µV scsi: csiostor: Uninitialized data in csio_ln_vnp_read_cbfn() RDMA/mlx4: Return missed an error if device doesn't support steering serial: xilinx_uartps: Fix race condition causing stuck TX power: supply: bq27xxx: Fix kernel crash on IRQ handler register error pnfs/flexfiles: Fix misplaced barrier in nfs4_ff_layout_prepare_ds drm/plane-helper: fix uninitialized variable reference PCI: aardvark: Don't spam about PIO Response Status fs: orangefs: fix error return code of orangefs_revalidate_lookup() mtd: spi-nor: hisi-sfc: Remove excessive clk_disable_unprepare() dmaengine: at_xdmac: fix AT_XDMAC_CC_PERID() macro auxdisplay: img-ascii-lcd: Fix lock-up when displaying empty string netfilter: nfnetlink_queue: fix OOB when mac header was cleared dmaengine: dmaengine_desc_callback_valid(): Check for `callback_result` m68k: set a default value for MEMORY_RESERVE watchdog: f71808e_wdt: fix inaccurate report in WDIOC_GETTIMEOUT scsi: qla2xxx: Turn off target reset during issue_lip i2c: xlr: Fix a resource leak in the error handling path of 'xlr_i2c_probe()' xen-pciback: Fix return in pm_ctrl_init() net: davinci_emac: Fix interrupt pacing disable ACPI: PMIC: Fix intel_pmic_regs_handler() read accesses bonding: Fix a use-after-free problem when bond_sysfs_slave_add() failed mm/zsmalloc.c: close race window between zs_pool_dec_isolated() and zs_unregister_migration() llc: fix out-of-bound array index in llc_sk_dev_hash() nfc: pn533: Fix double free when pn533_fill_fragment_skbs() fails vsock: prevent unnecessary refcnt inc for nonblocking connect USB: chipidea: fix interrupt deadlock ARM: 9156/1: drop cc-option fallbacks for architecture selection powerpc/bpf: Validate branch ranges powerpc/bpf: Fix BPF_SUB when imm == 0x80000000 mm, oom: pagefault_out_of_memory: don't force global OOM for dying tasks mm, oom: do not trigger out_of_memory from the #PF PCI: Add PCI_EXP_DEVCTL_PAYLOAD_* macros net: mdio-mux: fix unbalanced put_device parisc/entry: fix trace test in syscall exit path PCI/MSI: Destroy sysfs before freeing entries scsi: lpfc: Fix list_add() corruption in lpfc_drain_txq() usb: musb: tusb6010: check return value after calling platform_get_resource() scsi: advansys: Fix kernel pointer leak ARM: dts: omap: fix gpmc,mux-add-data type usb: host: ohci-tmio: check return value after calling platform_get_resource() tty: tty_buffer: Fix the softlockup issue in flush_to_ldisc MIPS: sni: Fix the build scsi: target: Fix ordered tag handling scsi: target: Fix alua_tg_pt_gps_count tracking powerpc/5200: dts: fix memory node unit name ALSA: gus: fix null pointer dereference on pointer block powerpc/dcr: Use cmplwi instead of 3-argument cmpli sh: check return code of request_irq maple: fix wrong return value of maple_bus_init(). sh: fix kconfig unmet dependency warning for FRAME_POINTER sh: define __BIG_ENDIAN for math-emu mips: BCM63XX: ensure that CPU_SUPPORTS_32BIT_KERNEL is set sched/core: Mitigate race cpus_share_cache()/update_top_cache_domain() net: bnx2x: fix variable dereferenced before check iavf: Fix for the false positive ASQ/ARQ errors while issuing VF reset mips: bcm63xx: add support for clk_get_parent() platform/x86: hp_accel: Fix an error handling path in 'lis3lv02d_probe()' NFC: reorganize the functions in nci_request NFC: reorder the logic in nfc_{un,}register_device perf/x86/intel/uncore: Fix filter_tid mask for CHA events on Skylake Server perf/x86/intel/uncore: Fix IIO event constraints for Skylake Server tun: fix bonding active backup with arp monitoring hexagon: export raw I/O routines for modules mm: kmemleak: slob: respect SLAB_NOLEAKTRACE flag btrfs: fix memory ordering between normal and ordered work functions parisc/sticon: fix reverse colors cfg80211: call cfg80211_stop_ap when switch from P2P_GO type drm/udl: fix control-message timeout drm/amdgpu: fix set scaling mode Full/Full aspect/Center not works on vga and dvi connectors batman-adv: Keep fragments equally sized batman-adv: Fix own OGM check in aggregated OGMs batman-adv: mcast: fix duplicate mcast packets in BLA backbone from LAN batman-adv: mcast: fix duplicate mcast packets from BLA backbone to mesh batman-adv: Consider fragmentation for needed_headroom batman-adv: Reserve needed_*room for fragments batman-adv: Don't always reallocate the fragmentation skb head ASoC: DAPM: Cover regression by kctl change notification fix usb: max-3421: Use driver data instead of maintaining a list of bound devices soc/tegra: pmc: Fix imbalanced clock disabling in error code path Linux 4.9.291 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: I23d798c10aebab1e51add60ccb34a8b289d49a4d
822 lines
19 KiB
C
822 lines
19 KiB
C
/*
|
|
* .xz Stream decoder
|
|
*
|
|
* Author: Lasse Collin <lasse.collin@tukaani.org>
|
|
*
|
|
* This file has been put into the public domain.
|
|
* You can do whatever you want with this file.
|
|
*/
|
|
|
|
#include "xz_private.h"
|
|
#include "xz_stream.h"
|
|
|
|
/* Hash used to validate the Index field */
|
|
struct xz_dec_hash {
|
|
vli_type unpadded;
|
|
vli_type uncompressed;
|
|
uint32_t crc32;
|
|
};
|
|
|
|
struct xz_dec {
|
|
/* Position in dec_main() */
|
|
enum {
|
|
SEQ_STREAM_HEADER,
|
|
SEQ_BLOCK_START,
|
|
SEQ_BLOCK_HEADER,
|
|
SEQ_BLOCK_UNCOMPRESS,
|
|
SEQ_BLOCK_PADDING,
|
|
SEQ_BLOCK_CHECK,
|
|
SEQ_INDEX,
|
|
SEQ_INDEX_PADDING,
|
|
SEQ_INDEX_CRC32,
|
|
SEQ_STREAM_FOOTER
|
|
} sequence;
|
|
|
|
/* Position in variable-length integers and Check fields */
|
|
uint32_t pos;
|
|
|
|
/* Variable-length integer decoded by dec_vli() */
|
|
vli_type vli;
|
|
|
|
/* Saved in_pos and out_pos */
|
|
size_t in_start;
|
|
size_t out_start;
|
|
|
|
/* CRC32 value in Block or Index */
|
|
uint32_t crc32;
|
|
|
|
/* Type of the integrity check calculated from uncompressed data */
|
|
enum xz_check check_type;
|
|
|
|
/* Operation mode */
|
|
enum xz_mode mode;
|
|
|
|
/*
|
|
* True if the next call to xz_dec_run() is allowed to return
|
|
* XZ_BUF_ERROR.
|
|
*/
|
|
bool allow_buf_error;
|
|
|
|
/* Information stored in Block Header */
|
|
struct {
|
|
/*
|
|
* Value stored in the Compressed Size field, or
|
|
* VLI_UNKNOWN if Compressed Size is not present.
|
|
*/
|
|
vli_type compressed;
|
|
|
|
/*
|
|
* Value stored in the Uncompressed Size field, or
|
|
* VLI_UNKNOWN if Uncompressed Size is not present.
|
|
*/
|
|
vli_type uncompressed;
|
|
|
|
/* Size of the Block Header field */
|
|
uint32_t size;
|
|
} block_header;
|
|
|
|
/* Information collected when decoding Blocks */
|
|
struct {
|
|
/* Observed compressed size of the current Block */
|
|
vli_type compressed;
|
|
|
|
/* Observed uncompressed size of the current Block */
|
|
vli_type uncompressed;
|
|
|
|
/* Number of Blocks decoded so far */
|
|
vli_type count;
|
|
|
|
/*
|
|
* Hash calculated from the Block sizes. This is used to
|
|
* validate the Index field.
|
|
*/
|
|
struct xz_dec_hash hash;
|
|
} block;
|
|
|
|
/* Variables needed when verifying the Index field */
|
|
struct {
|
|
/* Position in dec_index() */
|
|
enum {
|
|
SEQ_INDEX_COUNT,
|
|
SEQ_INDEX_UNPADDED,
|
|
SEQ_INDEX_UNCOMPRESSED
|
|
} sequence;
|
|
|
|
/* Size of the Index in bytes */
|
|
vli_type size;
|
|
|
|
/* Number of Records (matches block.count in valid files) */
|
|
vli_type count;
|
|
|
|
/*
|
|
* Hash calculated from the Records (matches block.hash in
|
|
* valid files).
|
|
*/
|
|
struct xz_dec_hash hash;
|
|
} index;
|
|
|
|
/*
|
|
* Temporary buffer needed to hold Stream Header, Block Header,
|
|
* and Stream Footer. The Block Header is the biggest (1 KiB)
|
|
* so we reserve space according to that. buf[] has to be aligned
|
|
* to a multiple of four bytes; the size_t variables before it
|
|
* should guarantee this.
|
|
*/
|
|
struct {
|
|
size_t pos;
|
|
size_t size;
|
|
uint8_t buf[1024];
|
|
} temp;
|
|
|
|
struct xz_dec_lzma2 *lzma2;
|
|
|
|
#ifdef XZ_DEC_BCJ
|
|
struct xz_dec_bcj *bcj;
|
|
bool bcj_active;
|
|
#endif
|
|
};
|
|
|
|
#ifdef XZ_DEC_ANY_CHECK
|
|
/* Sizes of the Check field with different Check IDs */
|
|
static const uint8_t check_sizes[16] = {
|
|
0,
|
|
4, 4, 4,
|
|
8, 8, 8,
|
|
16, 16, 16,
|
|
32, 32, 32,
|
|
64, 64, 64
|
|
};
|
|
#endif
|
|
|
|
/*
|
|
* Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
|
|
* must have set s->temp.pos to indicate how much data we are supposed
|
|
* to copy into s->temp.buf. Return true once s->temp.pos has reached
|
|
* s->temp.size.
|
|
*/
|
|
static bool fill_temp(struct xz_dec *s, struct xz_buf *b)
|
|
{
|
|
size_t copy_size = min_t(size_t,
|
|
b->in_size - b->in_pos, s->temp.size - s->temp.pos);
|
|
|
|
memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
|
|
b->in_pos += copy_size;
|
|
s->temp.pos += copy_size;
|
|
|
|
if (s->temp.pos == s->temp.size) {
|
|
s->temp.pos = 0;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/* Decode a variable-length integer (little-endian base-128 encoding) */
|
|
static enum xz_ret dec_vli(struct xz_dec *s, const uint8_t *in,
|
|
size_t *in_pos, size_t in_size)
|
|
{
|
|
uint8_t byte;
|
|
|
|
if (s->pos == 0)
|
|
s->vli = 0;
|
|
|
|
while (*in_pos < in_size) {
|
|
byte = in[*in_pos];
|
|
++*in_pos;
|
|
|
|
s->vli |= (vli_type)(byte & 0x7F) << s->pos;
|
|
|
|
if ((byte & 0x80) == 0) {
|
|
/* Don't allow non-minimal encodings. */
|
|
if (byte == 0 && s->pos != 0)
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->pos = 0;
|
|
return XZ_STREAM_END;
|
|
}
|
|
|
|
s->pos += 7;
|
|
if (s->pos == 7 * VLI_BYTES_MAX)
|
|
return XZ_DATA_ERROR;
|
|
}
|
|
|
|
return XZ_OK;
|
|
}
|
|
|
|
/*
|
|
* Decode the Compressed Data field from a Block. Update and validate
|
|
* the observed compressed and uncompressed sizes of the Block so that
|
|
* they don't exceed the values possibly stored in the Block Header
|
|
* (validation assumes that no integer overflow occurs, since vli_type
|
|
* is normally uint64_t). Update the CRC32 if presence of the CRC32
|
|
* field was indicated in Stream Header.
|
|
*
|
|
* Once the decoding is finished, validate that the observed sizes match
|
|
* the sizes possibly stored in the Block Header. Update the hash and
|
|
* Block count, which are later used to validate the Index field.
|
|
*/
|
|
static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)
|
|
{
|
|
enum xz_ret ret;
|
|
|
|
s->in_start = b->in_pos;
|
|
s->out_start = b->out_pos;
|
|
|
|
#ifdef XZ_DEC_BCJ
|
|
if (s->bcj_active)
|
|
ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
|
|
else
|
|
#endif
|
|
ret = xz_dec_lzma2_run(s->lzma2, b);
|
|
|
|
s->block.compressed += b->in_pos - s->in_start;
|
|
s->block.uncompressed += b->out_pos - s->out_start;
|
|
|
|
/*
|
|
* There is no need to separately check for VLI_UNKNOWN, since
|
|
* the observed sizes are always smaller than VLI_UNKNOWN.
|
|
*/
|
|
if (s->block.compressed > s->block_header.compressed
|
|
|| s->block.uncompressed
|
|
> s->block_header.uncompressed)
|
|
return XZ_DATA_ERROR;
|
|
|
|
if (s->check_type == XZ_CHECK_CRC32)
|
|
s->crc32 = xz_crc32(b->out + s->out_start,
|
|
b->out_pos - s->out_start, s->crc32);
|
|
|
|
if (ret == XZ_STREAM_END) {
|
|
if (s->block_header.compressed != VLI_UNKNOWN
|
|
&& s->block_header.compressed
|
|
!= s->block.compressed)
|
|
return XZ_DATA_ERROR;
|
|
|
|
if (s->block_header.uncompressed != VLI_UNKNOWN
|
|
&& s->block_header.uncompressed
|
|
!= s->block.uncompressed)
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->block.hash.unpadded += s->block_header.size
|
|
+ s->block.compressed;
|
|
|
|
#ifdef XZ_DEC_ANY_CHECK
|
|
s->block.hash.unpadded += check_sizes[s->check_type];
|
|
#else
|
|
if (s->check_type == XZ_CHECK_CRC32)
|
|
s->block.hash.unpadded += 4;
|
|
#endif
|
|
|
|
s->block.hash.uncompressed += s->block.uncompressed;
|
|
s->block.hash.crc32 = xz_crc32(
|
|
(const uint8_t *)&s->block.hash,
|
|
sizeof(s->block.hash), s->block.hash.crc32);
|
|
|
|
++s->block.count;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
/* Update the Index size and the CRC32 value. */
|
|
static void index_update(struct xz_dec *s, const struct xz_buf *b)
|
|
{
|
|
size_t in_used = b->in_pos - s->in_start;
|
|
s->index.size += in_used;
|
|
s->crc32 = xz_crc32(b->in + s->in_start, in_used, s->crc32);
|
|
}
|
|
|
|
/*
|
|
* Decode the Number of Records, Unpadded Size, and Uncompressed Size
|
|
* fields from the Index field. That is, Index Padding and CRC32 are not
|
|
* decoded by this function.
|
|
*
|
|
* This can return XZ_OK (more input needed), XZ_STREAM_END (everything
|
|
* successfully decoded), or XZ_DATA_ERROR (input is corrupt).
|
|
*/
|
|
static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b)
|
|
{
|
|
enum xz_ret ret;
|
|
|
|
do {
|
|
ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
|
|
if (ret != XZ_STREAM_END) {
|
|
index_update(s, b);
|
|
return ret;
|
|
}
|
|
|
|
switch (s->index.sequence) {
|
|
case SEQ_INDEX_COUNT:
|
|
s->index.count = s->vli;
|
|
|
|
/*
|
|
* Validate that the Number of Records field
|
|
* indicates the same number of Records as
|
|
* there were Blocks in the Stream.
|
|
*/
|
|
if (s->index.count != s->block.count)
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->index.sequence = SEQ_INDEX_UNPADDED;
|
|
break;
|
|
|
|
case SEQ_INDEX_UNPADDED:
|
|
s->index.hash.unpadded += s->vli;
|
|
s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
|
|
break;
|
|
|
|
case SEQ_INDEX_UNCOMPRESSED:
|
|
s->index.hash.uncompressed += s->vli;
|
|
s->index.hash.crc32 = xz_crc32(
|
|
(const uint8_t *)&s->index.hash,
|
|
sizeof(s->index.hash),
|
|
s->index.hash.crc32);
|
|
--s->index.count;
|
|
s->index.sequence = SEQ_INDEX_UNPADDED;
|
|
break;
|
|
}
|
|
} while (s->index.count > 0);
|
|
|
|
return XZ_STREAM_END;
|
|
}
|
|
|
|
/*
|
|
* Validate that the next four input bytes match the value of s->crc32.
|
|
* s->pos must be zero when starting to validate the first byte.
|
|
*/
|
|
static enum xz_ret crc32_validate(struct xz_dec *s, struct xz_buf *b)
|
|
{
|
|
do {
|
|
if (b->in_pos == b->in_size)
|
|
return XZ_OK;
|
|
|
|
if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++])
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->pos += 8;
|
|
|
|
} while (s->pos < 32);
|
|
|
|
s->crc32 = 0;
|
|
s->pos = 0;
|
|
|
|
return XZ_STREAM_END;
|
|
}
|
|
|
|
#ifdef XZ_DEC_ANY_CHECK
|
|
/*
|
|
* Skip over the Check field when the Check ID is not supported.
|
|
* Returns true once the whole Check field has been skipped over.
|
|
*/
|
|
static bool check_skip(struct xz_dec *s, struct xz_buf *b)
|
|
{
|
|
while (s->pos < check_sizes[s->check_type]) {
|
|
if (b->in_pos == b->in_size)
|
|
return false;
|
|
|
|
++b->in_pos;
|
|
++s->pos;
|
|
}
|
|
|
|
s->pos = 0;
|
|
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
|
|
static enum xz_ret dec_stream_header(struct xz_dec *s)
|
|
{
|
|
if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
|
|
return XZ_FORMAT_ERROR;
|
|
|
|
if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0)
|
|
!= get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))
|
|
return XZ_DATA_ERROR;
|
|
|
|
if (s->temp.buf[HEADER_MAGIC_SIZE] != 0)
|
|
return XZ_OPTIONS_ERROR;
|
|
|
|
/*
|
|
* Of integrity checks, we support only none (Check ID = 0) and
|
|
* CRC32 (Check ID = 1). However, if XZ_DEC_ANY_CHECK is defined,
|
|
* we will accept other check types too, but then the check won't
|
|
* be verified and a warning (XZ_UNSUPPORTED_CHECK) will be given.
|
|
*/
|
|
if (s->temp.buf[HEADER_MAGIC_SIZE + 1] > XZ_CHECK_MAX)
|
|
return XZ_OPTIONS_ERROR;
|
|
|
|
s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1];
|
|
|
|
#ifdef XZ_DEC_ANY_CHECK
|
|
if (s->check_type > XZ_CHECK_CRC32)
|
|
return XZ_UNSUPPORTED_CHECK;
|
|
#else
|
|
if (s->check_type > XZ_CHECK_CRC32)
|
|
return XZ_OPTIONS_ERROR;
|
|
#endif
|
|
|
|
return XZ_OK;
|
|
}
|
|
|
|
/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
|
|
static enum xz_ret dec_stream_footer(struct xz_dec *s)
|
|
{
|
|
if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
|
|
return XZ_DATA_ERROR;
|
|
|
|
if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf))
|
|
return XZ_DATA_ERROR;
|
|
|
|
/*
|
|
* Validate Backward Size. Note that we never added the size of the
|
|
* Index CRC32 field to s->index.size, thus we use s->index.size / 4
|
|
* instead of s->index.size / 4 - 1.
|
|
*/
|
|
if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
|
|
return XZ_DATA_ERROR;
|
|
|
|
if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type)
|
|
return XZ_DATA_ERROR;
|
|
|
|
/*
|
|
* Use XZ_STREAM_END instead of XZ_OK to be more convenient
|
|
* for the caller.
|
|
*/
|
|
return XZ_STREAM_END;
|
|
}
|
|
|
|
/* Decode the Block Header and initialize the filter chain. */
|
|
static enum xz_ret dec_block_header(struct xz_dec *s)
|
|
{
|
|
enum xz_ret ret;
|
|
|
|
/*
|
|
* Validate the CRC32. We know that the temp buffer is at least
|
|
* eight bytes so this is safe.
|
|
*/
|
|
s->temp.size -= 4;
|
|
if (xz_crc32(s->temp.buf, s->temp.size, 0)
|
|
!= get_le32(s->temp.buf + s->temp.size))
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->temp.pos = 2;
|
|
|
|
/*
|
|
* Catch unsupported Block Flags. We support only one or two filters
|
|
* in the chain, so we catch that with the same test.
|
|
*/
|
|
#ifdef XZ_DEC_BCJ
|
|
if (s->temp.buf[1] & 0x3E)
|
|
#else
|
|
if (s->temp.buf[1] & 0x3F)
|
|
#endif
|
|
return XZ_OPTIONS_ERROR;
|
|
|
|
/* Compressed Size */
|
|
if (s->temp.buf[1] & 0x40) {
|
|
if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
|
|
!= XZ_STREAM_END)
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->block_header.compressed = s->vli;
|
|
} else {
|
|
s->block_header.compressed = VLI_UNKNOWN;
|
|
}
|
|
|
|
/* Uncompressed Size */
|
|
if (s->temp.buf[1] & 0x80) {
|
|
if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
|
|
!= XZ_STREAM_END)
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->block_header.uncompressed = s->vli;
|
|
} else {
|
|
s->block_header.uncompressed = VLI_UNKNOWN;
|
|
}
|
|
|
|
#ifdef XZ_DEC_BCJ
|
|
/* If there are two filters, the first one must be a BCJ filter. */
|
|
s->bcj_active = s->temp.buf[1] & 0x01;
|
|
if (s->bcj_active) {
|
|
if (s->temp.size - s->temp.pos < 2)
|
|
return XZ_OPTIONS_ERROR;
|
|
|
|
ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
|
|
if (ret != XZ_OK)
|
|
return ret;
|
|
|
|
/*
|
|
* We don't support custom start offset,
|
|
* so Size of Properties must be zero.
|
|
*/
|
|
if (s->temp.buf[s->temp.pos++] != 0x00)
|
|
return XZ_OPTIONS_ERROR;
|
|
}
|
|
#endif
|
|
|
|
/* Valid Filter Flags always take at least two bytes. */
|
|
if (s->temp.size - s->temp.pos < 2)
|
|
return XZ_DATA_ERROR;
|
|
|
|
/* Filter ID = LZMA2 */
|
|
if (s->temp.buf[s->temp.pos++] != 0x21)
|
|
return XZ_OPTIONS_ERROR;
|
|
|
|
/* Size of Properties = 1-byte Filter Properties */
|
|
if (s->temp.buf[s->temp.pos++] != 0x01)
|
|
return XZ_OPTIONS_ERROR;
|
|
|
|
/* Filter Properties contains LZMA2 dictionary size. */
|
|
if (s->temp.size - s->temp.pos < 1)
|
|
return XZ_DATA_ERROR;
|
|
|
|
ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
|
|
if (ret != XZ_OK)
|
|
return ret;
|
|
|
|
/* The rest must be Header Padding. */
|
|
while (s->temp.pos < s->temp.size)
|
|
if (s->temp.buf[s->temp.pos++] != 0x00)
|
|
return XZ_OPTIONS_ERROR;
|
|
|
|
s->temp.pos = 0;
|
|
s->block.compressed = 0;
|
|
s->block.uncompressed = 0;
|
|
|
|
return XZ_OK;
|
|
}
|
|
|
|
static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b)
|
|
{
|
|
enum xz_ret ret;
|
|
|
|
/*
|
|
* Store the start position for the case when we are in the middle
|
|
* of the Index field.
|
|
*/
|
|
s->in_start = b->in_pos;
|
|
|
|
while (true) {
|
|
switch (s->sequence) {
|
|
case SEQ_STREAM_HEADER:
|
|
/*
|
|
* Stream Header is copied to s->temp, and then
|
|
* decoded from there. This way if the caller
|
|
* gives us only little input at a time, we can
|
|
* still keep the Stream Header decoding code
|
|
* simple. Similar approach is used in many places
|
|
* in this file.
|
|
*/
|
|
if (!fill_temp(s, b))
|
|
return XZ_OK;
|
|
|
|
/*
|
|
* If dec_stream_header() returns
|
|
* XZ_UNSUPPORTED_CHECK, it is still possible
|
|
* to continue decoding if working in multi-call
|
|
* mode. Thus, update s->sequence before calling
|
|
* dec_stream_header().
|
|
*/
|
|
s->sequence = SEQ_BLOCK_START;
|
|
|
|
ret = dec_stream_header(s);
|
|
if (ret != XZ_OK)
|
|
return ret;
|
|
|
|
case SEQ_BLOCK_START:
|
|
/* We need one byte of input to continue. */
|
|
if (b->in_pos == b->in_size)
|
|
return XZ_OK;
|
|
|
|
/* See if this is the beginning of the Index field. */
|
|
if (b->in[b->in_pos] == 0) {
|
|
s->in_start = b->in_pos++;
|
|
s->sequence = SEQ_INDEX;
|
|
break;
|
|
}
|
|
|
|
/*
|
|
* Calculate the size of the Block Header and
|
|
* prepare to decode it.
|
|
*/
|
|
s->block_header.size
|
|
= ((uint32_t)b->in[b->in_pos] + 1) * 4;
|
|
|
|
s->temp.size = s->block_header.size;
|
|
s->temp.pos = 0;
|
|
s->sequence = SEQ_BLOCK_HEADER;
|
|
|
|
case SEQ_BLOCK_HEADER:
|
|
if (!fill_temp(s, b))
|
|
return XZ_OK;
|
|
|
|
ret = dec_block_header(s);
|
|
if (ret != XZ_OK)
|
|
return ret;
|
|
|
|
s->sequence = SEQ_BLOCK_UNCOMPRESS;
|
|
|
|
case SEQ_BLOCK_UNCOMPRESS:
|
|
ret = dec_block(s, b);
|
|
if (ret != XZ_STREAM_END)
|
|
return ret;
|
|
|
|
s->sequence = SEQ_BLOCK_PADDING;
|
|
|
|
case SEQ_BLOCK_PADDING:
|
|
/*
|
|
* Size of Compressed Data + Block Padding
|
|
* must be a multiple of four. We don't need
|
|
* s->block.compressed for anything else
|
|
* anymore, so we use it here to test the size
|
|
* of the Block Padding field.
|
|
*/
|
|
while (s->block.compressed & 3) {
|
|
if (b->in_pos == b->in_size)
|
|
return XZ_OK;
|
|
|
|
if (b->in[b->in_pos++] != 0)
|
|
return XZ_DATA_ERROR;
|
|
|
|
++s->block.compressed;
|
|
}
|
|
|
|
s->sequence = SEQ_BLOCK_CHECK;
|
|
|
|
case SEQ_BLOCK_CHECK:
|
|
if (s->check_type == XZ_CHECK_CRC32) {
|
|
ret = crc32_validate(s, b);
|
|
if (ret != XZ_STREAM_END)
|
|
return ret;
|
|
}
|
|
#ifdef XZ_DEC_ANY_CHECK
|
|
else if (!check_skip(s, b)) {
|
|
return XZ_OK;
|
|
}
|
|
#endif
|
|
|
|
s->sequence = SEQ_BLOCK_START;
|
|
break;
|
|
|
|
case SEQ_INDEX:
|
|
ret = dec_index(s, b);
|
|
if (ret != XZ_STREAM_END)
|
|
return ret;
|
|
|
|
s->sequence = SEQ_INDEX_PADDING;
|
|
|
|
case SEQ_INDEX_PADDING:
|
|
while ((s->index.size + (b->in_pos - s->in_start))
|
|
& 3) {
|
|
if (b->in_pos == b->in_size) {
|
|
index_update(s, b);
|
|
return XZ_OK;
|
|
}
|
|
|
|
if (b->in[b->in_pos++] != 0)
|
|
return XZ_DATA_ERROR;
|
|
}
|
|
|
|
/* Finish the CRC32 value and Index size. */
|
|
index_update(s, b);
|
|
|
|
/* Compare the hashes to validate the Index field. */
|
|
if (!memeq(&s->block.hash, &s->index.hash,
|
|
sizeof(s->block.hash)))
|
|
return XZ_DATA_ERROR;
|
|
|
|
s->sequence = SEQ_INDEX_CRC32;
|
|
|
|
case SEQ_INDEX_CRC32:
|
|
ret = crc32_validate(s, b);
|
|
if (ret != XZ_STREAM_END)
|
|
return ret;
|
|
|
|
s->temp.size = STREAM_HEADER_SIZE;
|
|
s->sequence = SEQ_STREAM_FOOTER;
|
|
|
|
case SEQ_STREAM_FOOTER:
|
|
if (!fill_temp(s, b))
|
|
return XZ_OK;
|
|
|
|
return dec_stream_footer(s);
|
|
}
|
|
}
|
|
|
|
/* Never reached */
|
|
}
|
|
|
|
/*
|
|
* xz_dec_run() is a wrapper for dec_main() to handle some special cases in
|
|
* multi-call and single-call decoding.
|
|
*
|
|
* In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
|
|
* are not going to make any progress anymore. This is to prevent the caller
|
|
* from calling us infinitely when the input file is truncated or otherwise
|
|
* corrupt. Since zlib-style API allows that the caller fills the input buffer
|
|
* only when the decoder doesn't produce any new output, we have to be careful
|
|
* to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
|
|
* after the second consecutive call to xz_dec_run() that makes no progress.
|
|
*
|
|
* In single-call mode, if we couldn't decode everything and no error
|
|
* occurred, either the input is truncated or the output buffer is too small.
|
|
* Since we know that the last input byte never produces any output, we know
|
|
* that if all the input was consumed and decoding wasn't finished, the file
|
|
* must be corrupt. Otherwise the output buffer has to be too small or the
|
|
* file is corrupt in a way that decoding it produces too big output.
|
|
*
|
|
* If single-call decoding fails, we reset b->in_pos and b->out_pos back to
|
|
* their original values. This is because with some filter chains there won't
|
|
* be any valid uncompressed data in the output buffer unless the decoding
|
|
* actually succeeds (that's the price to pay of using the output buffer as
|
|
* the workspace).
|
|
*/
|
|
XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)
|
|
{
|
|
size_t in_start;
|
|
size_t out_start;
|
|
enum xz_ret ret;
|
|
|
|
if (DEC_IS_SINGLE(s->mode))
|
|
xz_dec_reset(s);
|
|
|
|
in_start = b->in_pos;
|
|
out_start = b->out_pos;
|
|
ret = dec_main(s, b);
|
|
|
|
if (DEC_IS_SINGLE(s->mode)) {
|
|
if (ret == XZ_OK)
|
|
ret = b->in_pos == b->in_size
|
|
? XZ_DATA_ERROR : XZ_BUF_ERROR;
|
|
|
|
if (ret != XZ_STREAM_END) {
|
|
b->in_pos = in_start;
|
|
b->out_pos = out_start;
|
|
}
|
|
|
|
} else if (ret == XZ_OK && in_start == b->in_pos
|
|
&& out_start == b->out_pos) {
|
|
if (s->allow_buf_error)
|
|
ret = XZ_BUF_ERROR;
|
|
|
|
s->allow_buf_error = true;
|
|
} else {
|
|
s->allow_buf_error = false;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max)
|
|
{
|
|
struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL);
|
|
if (s == NULL)
|
|
return NULL;
|
|
|
|
s->mode = mode;
|
|
|
|
#ifdef XZ_DEC_BCJ
|
|
s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode));
|
|
if (s->bcj == NULL)
|
|
goto error_bcj;
|
|
#endif
|
|
|
|
s->lzma2 = xz_dec_lzma2_create(mode, dict_max);
|
|
if (s->lzma2 == NULL)
|
|
goto error_lzma2;
|
|
|
|
xz_dec_reset(s);
|
|
return s;
|
|
|
|
error_lzma2:
|
|
#ifdef XZ_DEC_BCJ
|
|
xz_dec_bcj_end(s->bcj);
|
|
error_bcj:
|
|
#endif
|
|
kfree(s);
|
|
return NULL;
|
|
}
|
|
|
|
XZ_EXTERN void xz_dec_reset(struct xz_dec *s)
|
|
{
|
|
s->sequence = SEQ_STREAM_HEADER;
|
|
s->allow_buf_error = false;
|
|
s->pos = 0;
|
|
s->crc32 = 0;
|
|
memzero(&s->block, sizeof(s->block));
|
|
memzero(&s->index, sizeof(s->index));
|
|
s->temp.pos = 0;
|
|
s->temp.size = STREAM_HEADER_SIZE;
|
|
}
|
|
|
|
XZ_EXTERN void xz_dec_end(struct xz_dec *s)
|
|
{
|
|
if (s != NULL) {
|
|
xz_dec_lzma2_end(s->lzma2);
|
|
#ifdef XZ_DEC_BCJ
|
|
xz_dec_bcj_end(s->bcj);
|
|
#endif
|
|
kfree(s);
|
|
}
|
|
}
|