Commit 21b13a6f authored by Linus Torvalds's avatar Linus Torvalds Committed by Stefan Bader

squashfs metadata 2: electric boogaloo

BugLink: https://bugs.launchpad.net/bugs/1816756

Anatoly continues to find issues with fuzzed squashfs images.

This time, corrupt, missing, or undersized data for the page filling
wasn't checked for, because the squashfs_{copy,read}_cache() functions
did the squashfs_copy_data() call without checking the resulting data
size.

Which could result in the page cache pages being incompletely filled in,
and no error indication to the user space reading garbage data.

So make a helper function for the "fill in pages" case, because the
exact same incomplete sequence existed in two places.

[ I should have made a squashfs branch for these things, but I didn't
  intend to start doing them in the first place.

  My historical connection through cramfs is why I got into looking at
  these issues at all, and every time I (continue to) think it's a
  one-off.

  Because _this_ time is always the last time. Right?   - Linus ]
Reported-by: default avatarAnatoly Trosinenko <anatoly.trosinenko@gmail.com>
Tested-by: default avatarWilly Tarreau <w@1wt.eu>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Phillip Lougher <phillip@squashfs.org.uk>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit cdbb65c4)
Signed-off-by: default avatarPaolo Pisati <paolo.pisati@canonical.com>
Acked-by: default avatarThadeu Lima de Souza Cascardo <cascardo@canonical.com>
Acked-by: default avatarTyler Hicks <tyhicks@canonical.com>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
parent 8e82ae25
...@@ -374,13 +374,29 @@ static int read_blocklist(struct inode *inode, int index, u64 *block) ...@@ -374,13 +374,29 @@ static int read_blocklist(struct inode *inode, int index, u64 *block)
return squashfs_block_size(size); return squashfs_block_size(size);
} }
void squashfs_fill_page(struct page *page, struct squashfs_cache_entry *buffer, int offset, int avail)
{
int copied;
void *pageaddr;
pageaddr = kmap_atomic(page);
copied = squashfs_copy_data(pageaddr, buffer, offset, avail);
memset(pageaddr + copied, 0, PAGE_CACHE_SIZE - copied);
kunmap_atomic(pageaddr);
flush_dcache_page(page);
if (copied == avail)
SetPageUptodate(page);
else
SetPageError(page);
}
/* Copy data into page cache */ /* Copy data into page cache */
void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer, void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
int bytes, int offset) int bytes, int offset)
{ {
struct inode *inode = page->mapping->host; struct inode *inode = page->mapping->host;
struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
void *pageaddr;
int i, mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1; int i, mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
int start_index = page->index & ~mask, end_index = start_index | mask; int start_index = page->index & ~mask, end_index = start_index | mask;
...@@ -406,12 +422,7 @@ void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer, ...@@ -406,12 +422,7 @@ void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
if (PageUptodate(push_page)) if (PageUptodate(push_page))
goto skip_page; goto skip_page;
pageaddr = kmap_atomic(push_page); squashfs_fill_page(push_page, buffer, offset, avail);
squashfs_copy_data(pageaddr, buffer, offset, avail);
memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
kunmap_atomic(pageaddr);
flush_dcache_page(push_page);
SetPageUptodate(push_page);
skip_page: skip_page:
unlock_page(push_page); unlock_page(push_page);
if (i != page->index) if (i != page->index)
......
...@@ -144,7 +144,6 @@ static int squashfs_read_cache(struct page *target_page, u64 block, int bsize, ...@@ -144,7 +144,6 @@ static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
struct squashfs_cache_entry *buffer = squashfs_get_datablock(i->i_sb, struct squashfs_cache_entry *buffer = squashfs_get_datablock(i->i_sb,
block, bsize); block, bsize);
int bytes = buffer->length, res = buffer->error, n, offset = 0; int bytes = buffer->length, res = buffer->error, n, offset = 0;
void *pageaddr;
if (res) { if (res) {
ERROR("Unable to read page, block %llx, size %x\n", block, ERROR("Unable to read page, block %llx, size %x\n", block,
...@@ -159,12 +158,7 @@ static int squashfs_read_cache(struct page *target_page, u64 block, int bsize, ...@@ -159,12 +158,7 @@ static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
if (page[n] == NULL) if (page[n] == NULL)
continue; continue;
pageaddr = kmap_atomic(page[n]); squashfs_fill_page(page[n], buffer, offset, avail);
squashfs_copy_data(pageaddr, buffer, offset, avail);
memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
kunmap_atomic(pageaddr);
flush_dcache_page(page[n]);
SetPageUptodate(page[n]);
unlock_page(page[n]); unlock_page(page[n]);
if (page[n] != target_page) if (page[n] != target_page)
page_cache_release(page[n]); page_cache_release(page[n]);
......
...@@ -67,6 +67,7 @@ extern __le64 *squashfs_read_fragment_index_table(struct super_block *, ...@@ -67,6 +67,7 @@ extern __le64 *squashfs_read_fragment_index_table(struct super_block *,
u64, u64, unsigned int); u64, u64, unsigned int);
/* file.c */ /* file.c */
void squashfs_fill_page(struct page *, struct squashfs_cache_entry *, int, int);
void squashfs_copy_cache(struct page *, struct squashfs_cache_entry *, int, void squashfs_copy_cache(struct page *, struct squashfs_cache_entry *, int,
int); int);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment