2009-01-05 16:46:27 +08:00
|
|
|
/*
|
|
|
|
* Squashfs - a compressed read only filesystem for Linux
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
|
2011-05-26 17:39:56 +08:00
|
|
|
* Phillip Lougher <phillip@squashfs.org.uk>
|
2009-01-05 16:46:27 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2,
|
|
|
|
* or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* block.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file implements the low-level routines to read and decompress
|
|
|
|
* datablocks and metadata blocks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/vfs.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/buffer_head.h>
|
|
|
|
|
|
|
|
#include "squashfs_fs.h"
|
|
|
|
#include "squashfs_fs_sb.h"
|
|
|
|
#include "squashfs.h"
|
2009-10-06 11:04:15 +08:00
|
|
|
#include "decompressor.h"
|
2013-11-18 10:59:12 +08:00
|
|
|
#include "page_actor.h"
|
2009-01-05 16:46:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the metadata block length, this is stored in the first two
|
|
|
|
* bytes of the metadata block.
|
|
|
|
*/
|
|
|
|
static struct buffer_head *get_block_length(struct super_block *sb,
|
|
|
|
u64 *cur_index, int *offset, int *length)
|
|
|
|
{
|
|
|
|
struct squashfs_sb_info *msblk = sb->s_fs_info;
|
|
|
|
struct buffer_head *bh;
|
|
|
|
|
|
|
|
bh = sb_bread(sb, *cur_index);
|
|
|
|
if (bh == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (msblk->devblksize - *offset == 1) {
|
|
|
|
*length = (unsigned char) bh->b_data[*offset];
|
|
|
|
put_bh(bh);
|
|
|
|
bh = sb_bread(sb, ++(*cur_index));
|
|
|
|
if (bh == NULL)
|
|
|
|
return NULL;
|
|
|
|
*length |= (unsigned char) bh->b_data[0] << 8;
|
|
|
|
*offset = 1;
|
|
|
|
} else {
|
|
|
|
*length = (unsigned char) bh->b_data[*offset] |
|
|
|
|
(unsigned char) bh->b_data[*offset + 1] << 8;
|
|
|
|
*offset += 2;
|
squashfs: fix use of uninitialised variable in zlib & xz decompressors
Fix potential use of uninitialised variable caused by recent
decompressor code optimisations.
In zlib_uncompress (zlib_wrapper.c) we have
int zlib_err, zlib_init = 0;
...
do {
...
if (avail == 0) {
offset = 0;
put_bh(bh[k++]);
continue;
}
...
zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
...
} while (zlib_err == Z_OK);
If continue is executed (avail == 0) then the while condition will be
evaluated testing zlib_err, which is uninitialised first time around the
loop.
Fix this by getting rid of the 'if (avail == 0)' condition test, this
edge condition should not be being handled in the decompressor code, and
instead handle it generically in the caller code.
Similarly for xz_wrapper.c.
Incidentally, on most architectures (bar Mips and Parisc), no
uninitialised variable warning is generated by gcc, this is because the
while condition test on continue is optimised out and not performed
(when executing continue zlib_err has not been changed since entering
the loop, and logically if the while condition was true previously, then
it's still true).
Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
Reported-by: Jesper Juhl <jj@chaosbits.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-01-26 07:07:34 +08:00
|
|
|
|
|
|
|
if (*offset == msblk->devblksize) {
|
|
|
|
put_bh(bh);
|
|
|
|
bh = sb_bread(sb, ++(*cur_index));
|
|
|
|
if (bh == NULL)
|
|
|
|
return NULL;
|
|
|
|
*offset = 0;
|
|
|
|
}
|
2009-01-05 16:46:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return bh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and decompress a metadata block or datablock. Length is non-zero
|
|
|
|
* if a datablock is being read (the size is stored elsewhere in the
|
|
|
|
* filesystem), otherwise the length is obtained from the first two bytes of
|
|
|
|
* the metadata block. A bit in the length field indicates if the block
|
|
|
|
* is stored uncompressed in the filesystem (usually because compression
|
2012-03-06 09:18:49 +08:00
|
|
|
* generated a larger block - this does occasionally happen with compression
|
|
|
|
* algorithms).
|
2009-01-05 16:46:27 +08:00
|
|
|
*/
|
2013-11-18 10:59:12 +08:00
|
|
|
int squashfs_read_data(struct super_block *sb, u64 index, int length,
|
|
|
|
u64 *next_index, struct squashfs_page_actor *output)
|
2009-01-05 16:46:27 +08:00
|
|
|
{
|
|
|
|
struct squashfs_sb_info *msblk = sb->s_fs_info;
|
|
|
|
struct buffer_head **bh;
|
|
|
|
int offset = index & ((1 << msblk->devblksize_log2) - 1);
|
|
|
|
u64 cur_index = index >> msblk->devblksize_log2;
|
2013-11-18 10:59:12 +08:00
|
|
|
int bytes, compressed, b = 0, k = 0, avail, i;
|
2009-01-05 16:46:27 +08:00
|
|
|
|
2013-11-18 10:59:12 +08:00
|
|
|
bh = kcalloc(((output->length + msblk->devblksize - 1)
|
2010-04-23 09:32:02 +08:00
|
|
|
>> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
|
2009-01-05 16:46:27 +08:00
|
|
|
if (bh == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (length) {
|
|
|
|
/*
|
|
|
|
* Datablock.
|
|
|
|
*/
|
|
|
|
bytes = -offset;
|
|
|
|
compressed = SQUASHFS_COMPRESSED_BLOCK(length);
|
|
|
|
length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
|
|
|
|
if (next_index)
|
|
|
|
*next_index = index + length;
|
|
|
|
|
|
|
|
TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
|
2013-11-18 10:59:12 +08:00
|
|
|
index, compressed ? "" : "un", length, output->length);
|
2009-01-05 16:46:27 +08:00
|
|
|
|
2013-11-18 10:59:12 +08:00
|
|
|
if (length < 0 || length > output->length ||
|
2009-01-05 16:46:27 +08:00
|
|
|
(index + length) > msblk->bytes_used)
|
|
|
|
goto read_failure;
|
|
|
|
|
|
|
|
for (b = 0; bytes < length; b++, cur_index++) {
|
|
|
|
bh[b] = sb_getblk(sb, cur_index);
|
|
|
|
if (bh[b] == NULL)
|
|
|
|
goto block_release;
|
|
|
|
bytes += msblk->devblksize;
|
|
|
|
}
|
|
|
|
ll_rw_block(READ, b, bh);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Metadata block.
|
|
|
|
*/
|
|
|
|
if ((index + 2) > msblk->bytes_used)
|
|
|
|
goto read_failure;
|
|
|
|
|
|
|
|
bh[0] = get_block_length(sb, &cur_index, &offset, &length);
|
|
|
|
if (bh[0] == NULL)
|
|
|
|
goto read_failure;
|
|
|
|
b = 1;
|
|
|
|
|
|
|
|
bytes = msblk->devblksize - offset;
|
|
|
|
compressed = SQUASHFS_COMPRESSED(length);
|
|
|
|
length = SQUASHFS_COMPRESSED_SIZE(length);
|
|
|
|
if (next_index)
|
|
|
|
*next_index = index + length + 2;
|
|
|
|
|
|
|
|
TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
|
|
|
|
compressed ? "" : "un", length);
|
|
|
|
|
2013-11-18 10:59:12 +08:00
|
|
|
if (length < 0 || length > output->length ||
|
2009-01-05 16:46:27 +08:00
|
|
|
(index + length) > msblk->bytes_used)
|
|
|
|
goto block_release;
|
|
|
|
|
|
|
|
for (; bytes < length; b++) {
|
|
|
|
bh[b] = sb_getblk(sb, ++cur_index);
|
|
|
|
if (bh[b] == NULL)
|
|
|
|
goto block_release;
|
|
|
|
bytes += msblk->devblksize;
|
|
|
|
}
|
|
|
|
ll_rw_block(READ, b - 1, bh + 1);
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:56:26 +08:00
|
|
|
for (i = 0; i < b; i++) {
|
|
|
|
wait_on_buffer(bh[i]);
|
|
|
|
if (!buffer_uptodate(bh[i]))
|
|
|
|
goto block_release;
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:46:27 +08:00
|
|
|
if (compressed) {
|
2013-11-18 10:59:12 +08:00
|
|
|
length = squashfs_decompress(msblk, bh, b, offset, length,
|
|
|
|
output);
|
2009-09-23 02:25:24 +08:00
|
|
|
if (length < 0)
|
|
|
|
goto read_failure;
|
2009-01-05 16:46:27 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Block is uncompressed.
|
|
|
|
*/
|
2013-09-05 01:01:23 +08:00
|
|
|
int in, pg_offset = 0;
|
2013-11-18 10:59:12 +08:00
|
|
|
void *data = squashfs_first_page(output);
|
2009-01-05 16:46:27 +08:00
|
|
|
|
|
|
|
for (bytes = length; k < b; k++) {
|
|
|
|
in = min(bytes, msblk->devblksize - offset);
|
|
|
|
bytes -= in;
|
|
|
|
while (in) {
|
|
|
|
if (pg_offset == PAGE_CACHE_SIZE) {
|
2013-11-18 10:59:12 +08:00
|
|
|
data = squashfs_next_page(output);
|
2009-01-05 16:46:27 +08:00
|
|
|
pg_offset = 0;
|
|
|
|
}
|
|
|
|
avail = min_t(int, in, PAGE_CACHE_SIZE -
|
|
|
|
pg_offset);
|
2013-11-18 10:59:12 +08:00
|
|
|
memcpy(data + pg_offset, bh[k]->b_data + offset,
|
|
|
|
avail);
|
2009-01-05 16:46:27 +08:00
|
|
|
in -= avail;
|
|
|
|
pg_offset += avail;
|
|
|
|
offset += avail;
|
|
|
|
}
|
|
|
|
offset = 0;
|
|
|
|
put_bh(bh[k]);
|
|
|
|
}
|
2013-11-18 10:59:12 +08:00
|
|
|
squashfs_finish_page(output);
|
2009-01-05 16:46:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kfree(bh);
|
|
|
|
return length;
|
|
|
|
|
|
|
|
block_release:
|
|
|
|
for (; k < b; k++)
|
|
|
|
put_bh(bh[k]);
|
|
|
|
|
|
|
|
read_failure:
|
2009-03-05 08:31:12 +08:00
|
|
|
ERROR("squashfs_read_data failed to read block 0x%llx\n",
|
|
|
|
(unsigned long long) index);
|
2009-01-05 16:46:27 +08:00
|
|
|
kfree(bh);
|
|
|
|
return -EIO;
|
|
|
|
}
|