Commit 40245f24 authored by Gao Xiang's avatar Gao Xiang Committed by Greg Kroah-Hartman

staging: erofs: compressed_pages should not be accessed again after freed

commit af692e11 upstream.

This patch resolves the following page use-after-free issue,
z_erofs_vle_unzip:
    ...
    for (i = 0; i < nr_pages; ++i) {
        ...
        z_erofs_onlinepage_endio(page);  (1)
    }

    for (i = 0; i < clusterpages; ++i) {
        page = compressed_pages[i];

        if (page->mapping == mngda)      (2)
            continue;
        /* recycle all individual staging pages */
        (void)z_erofs_gather_if_stagingpage(page_pool, page); (3)
        WRITE_ONCE(compressed_pages[i], NULL);
    }
    ...

After (1) is executed, page is freed and could be then reused, if
compressed_pages is scanned after that, it could fall info (2) or
(3) by mistake and that could finally be in a mess.

This patch aims to solve the above issue only with little changes
as much as possible in order to make the fix backport easier.

Fixes: 3883a79a ("staging: erofs: introduce VLE decompression support")
Cc: <stable@vger.kernel.org> # 4.19+
Signed-off-by: default avatarGao Xiang <gaoxiang25@huawei.com>
Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 1fa7c9b4
...@@ -925,11 +925,10 @@ static int z_erofs_vle_unzip(struct super_block *sb, ...@@ -925,11 +925,10 @@ static int z_erofs_vle_unzip(struct super_block *sb,
if (llen > grp->llen) if (llen > grp->llen)
llen = grp->llen; llen = grp->llen;
err = z_erofs_vle_unzip_fast_percpu(compressed_pages, err = z_erofs_vle_unzip_fast_percpu(compressed_pages, clusterpages,
clusterpages, pages, llen, work->pageofs, pages, llen, work->pageofs);
z_erofs_onlinepage_endio);
if (err != -ENOTSUPP) if (err != -ENOTSUPP)
goto out_percpu; goto out;
if (sparsemem_pages >= nr_pages) if (sparsemem_pages >= nr_pages)
goto skip_allocpage; goto skip_allocpage;
...@@ -950,8 +949,25 @@ static int z_erofs_vle_unzip(struct super_block *sb, ...@@ -950,8 +949,25 @@ static int z_erofs_vle_unzip(struct super_block *sb,
erofs_vunmap(vout, nr_pages); erofs_vunmap(vout, nr_pages);
out: out:
/* must handle all compressed pages before endding pages */
for (i = 0; i < clusterpages; ++i) {
page = compressed_pages[i];
#ifdef EROFS_FS_HAS_MANAGED_CACHE
if (page->mapping == mngda)
continue;
#endif
/* recycle all individual staging pages */
(void)z_erofs_gather_if_stagingpage(page_pool, page);
WRITE_ONCE(compressed_pages[i], NULL);
}
for (i = 0; i < nr_pages; ++i) { for (i = 0; i < nr_pages; ++i) {
page = pages[i]; page = pages[i];
if (!page)
continue;
DBG_BUGON(page->mapping == NULL); DBG_BUGON(page->mapping == NULL);
/* recycle all individual staging pages */ /* recycle all individual staging pages */
...@@ -964,20 +980,6 @@ static int z_erofs_vle_unzip(struct super_block *sb, ...@@ -964,20 +980,6 @@ static int z_erofs_vle_unzip(struct super_block *sb,
z_erofs_onlinepage_endio(page); z_erofs_onlinepage_endio(page);
} }
out_percpu:
for (i = 0; i < clusterpages; ++i) {
page = compressed_pages[i];
#ifdef EROFS_FS_HAS_MANAGED_CACHE
if (page->mapping == mngda)
continue;
#endif
/* recycle all individual staging pages */
(void)z_erofs_gather_if_stagingpage(page_pool, page);
WRITE_ONCE(compressed_pages[i], NULL);
}
if (pages == z_pagemap_global) if (pages == z_pagemap_global)
mutex_unlock(&z_pagemap_global_lock); mutex_unlock(&z_pagemap_global_lock);
else if (unlikely(pages != pages_onstack)) else if (unlikely(pages != pages_onstack))
......
...@@ -218,8 +218,7 @@ extern int z_erofs_vle_plain_copy(struct page **compressed_pages, ...@@ -218,8 +218,7 @@ extern int z_erofs_vle_plain_copy(struct page **compressed_pages,
extern int z_erofs_vle_unzip_fast_percpu(struct page **compressed_pages, extern int z_erofs_vle_unzip_fast_percpu(struct page **compressed_pages,
unsigned clusterpages, struct page **pages, unsigned clusterpages, struct page **pages,
unsigned outlen, unsigned short pageofs, unsigned int outlen, unsigned short pageofs);
void (*endio)(struct page *));
extern int z_erofs_vle_unzip_vmap(struct page **compressed_pages, extern int z_erofs_vle_unzip_vmap(struct page **compressed_pages,
unsigned clusterpages, void *vaddr, unsigned llen, unsigned clusterpages, void *vaddr, unsigned llen,
......
...@@ -105,8 +105,7 @@ int z_erofs_vle_unzip_fast_percpu(struct page **compressed_pages, ...@@ -105,8 +105,7 @@ int z_erofs_vle_unzip_fast_percpu(struct page **compressed_pages,
unsigned clusterpages, unsigned clusterpages,
struct page **pages, struct page **pages,
unsigned outlen, unsigned outlen,
unsigned short pageofs, unsigned short pageofs)
void (*endio)(struct page *))
{ {
void *vin, *vout; void *vin, *vout;
unsigned nr_pages, i, j; unsigned nr_pages, i, j;
...@@ -128,31 +127,30 @@ int z_erofs_vle_unzip_fast_percpu(struct page **compressed_pages, ...@@ -128,31 +127,30 @@ int z_erofs_vle_unzip_fast_percpu(struct page **compressed_pages,
ret = z_erofs_unzip_lz4(vin, vout + pageofs, ret = z_erofs_unzip_lz4(vin, vout + pageofs,
clusterpages * PAGE_SIZE, outlen); clusterpages * PAGE_SIZE, outlen);
if (ret >= 0) { if (ret < 0)
outlen = ret; goto out;
ret = 0; ret = 0;
}
for (i = 0; i < nr_pages; ++i) { for (i = 0; i < nr_pages; ++i) {
j = min((unsigned)PAGE_SIZE - pageofs, outlen); j = min((unsigned)PAGE_SIZE - pageofs, outlen);
if (pages[i] != NULL) { if (pages[i] != NULL) {
if (ret < 0) if (clusterpages == 1 &&
SetPageError(pages[i]); pages[i] == compressed_pages[0]) {
else if (clusterpages == 1 && pages[i] == compressed_pages[0])
memcpy(vin + pageofs, vout + pageofs, j); memcpy(vin + pageofs, vout + pageofs, j);
else { } else {
void *dst = kmap_atomic(pages[i]); void *dst = kmap_atomic(pages[i]);
memcpy(dst + pageofs, vout + pageofs, j); memcpy(dst + pageofs, vout + pageofs, j);
kunmap_atomic(dst); kunmap_atomic(dst);
} }
endio(pages[i]);
} }
vout += PAGE_SIZE; vout += PAGE_SIZE;
outlen -= j; outlen -= j;
pageofs = 0; pageofs = 0;
} }
out:
preempt_enable(); preempt_enable();
if (clusterpages == 1) if (clusterpages == 1)
......
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