diff mbox series

z3fold: fix use-after-free when freeing handles

Message ID 20200520082100.28876-1-vitaly.wool@konsulko.com (mailing list archive)
State New, archived
Headers show
Series z3fold: fix use-after-free when freeing handles | expand

Commit Message

Vitaly Wool May 20, 2020, 8:21 a.m. UTC
From: Uladzislau Rezki <uladzislau.rezki@sony.com>

free_handle() for a foreign handle may race with inter-page
compaction, what can lead to memory corruption. To avoid that,
take write lock not read lock in free_handle to be synchronized
with __release_z3fold_page().

For example KASAN can detect it:

[   33.723357] ==================================================================
[   33.723401] BUG: KASAN: use-after-free in LZ4_decompress_safe+0x2c4/0x3b8
[   33.723418] Read of size 1 at addr ffffffc976695ca3 by task GoogleApiHandle/4121
[   33.723428]
[   33.723449] CPU: 0 PID: 4121 Comm: GoogleApiHandle Tainted: P S         OE     4.19.81-perf+ #162
[   33.723461] Hardware name: Sony Mobile Communications. PDX-203(KONA) (DT)
[   33.723473] Call trace:
[   33.723495] dump_backtrace+0x0/0x288
[   33.723512] show_stack+0x14/0x20
[   33.723533] dump_stack+0xe4/0x124
[   33.723551] print_address_description+0x80/0x2e0
[   33.723566] kasan_report+0x268/0x2d0
[   33.723584] __asan_load1+0x4c/0x58
[   33.723601] LZ4_decompress_safe+0x2c4/0x3b8
[   33.723619] lz4_decompress_crypto+0x3c/0x70
[   33.723636] crypto_decompress+0x58/0x70
[   33.723656] zcomp_decompress+0xd4/0x120
...

Apart from that, initialize zhdr->mapped_count in init_z3fold_page()
and remove "newpage" variable because it is not used anywhere.

Signed-off-by: Uladzislau Rezki <uladzislau.rezki@sony.com>
Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.com>
---
 mm/z3fold.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Andrew Morton May 21, 2020, 12:46 a.m. UTC | #1
On Wed, 20 May 2020 11:21:00 +0300 vitaly.wool@konsulko.com wrote:

> From: Uladzislau Rezki <uladzislau.rezki@sony.com>
> 
> free_handle() for a foreign handle may race with inter-page
> compaction, what can lead to memory corruption. To avoid that,
> take write lock not read lock in free_handle to be synchronized
> with __release_z3fold_page().
> 
> For example KASAN can detect it:
> 
> [   33.723357] ==================================================================
> [   33.723401] BUG: KASAN: use-after-free in LZ4_decompress_safe+0x2c4/0x3b8
> [   33.723418] Read of size 1 at addr ffffffc976695ca3 by task GoogleApiHandle/4121
> [   33.723428]
> [   33.723449] CPU: 0 PID: 4121 Comm: GoogleApiHandle Tainted: P S         OE     4.19.81-perf+ #162
> [   33.723461] Hardware name: Sony Mobile Communications. PDX-203(KONA) (DT)
> [   33.723473] Call trace:
> [   33.723495] dump_backtrace+0x0/0x288
> [   33.723512] show_stack+0x14/0x20
> [   33.723533] dump_stack+0xe4/0x124
> [   33.723551] print_address_description+0x80/0x2e0
> [   33.723566] kasan_report+0x268/0x2d0
> [   33.723584] __asan_load1+0x4c/0x58
> [   33.723601] LZ4_decompress_safe+0x2c4/0x3b8
> [   33.723619] lz4_decompress_crypto+0x3c/0x70
> [   33.723636] crypto_decompress+0x58/0x70
> [   33.723656] zcomp_decompress+0xd4/0x120
> ...
> 
> Apart from that, initialize zhdr->mapped_count in init_z3fold_page()
> and remove "newpage" variable because it is not used anywhere.
> 
> Signed-off-by: Uladzislau Rezki <uladzislau.rezki@sony.com>
> Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.com>

I assume that a cc:stable is appropriate here?
Vitaly Wool May 21, 2020, 8:04 a.m. UTC | #2
On Thu, May 21, 2020 at 2:46 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Wed, 20 May 2020 11:21:00 +0300 vitaly.wool@konsulko.com wrote:
>
> > From: Uladzislau Rezki <uladzislau.rezki@sony.com>
> >
> > free_handle() for a foreign handle may race with inter-page
> > compaction, what can lead to memory corruption. To avoid that,
> > take write lock not read lock in free_handle to be synchronized
> > with __release_z3fold_page().
> >
> > For example KASAN can detect it:
> >
> > [   33.723357] ==================================================================
> > [   33.723401] BUG: KASAN: use-after-free in LZ4_decompress_safe+0x2c4/0x3b8
> > [   33.723418] Read of size 1 at addr ffffffc976695ca3 by task GoogleApiHandle/4121
> > [   33.723428]
> > [   33.723449] CPU: 0 PID: 4121 Comm: GoogleApiHandle Tainted: P S         OE     4.19.81-perf+ #162
> > [   33.723461] Hardware name: Sony Mobile Communications. PDX-203(KONA) (DT)
> > [   33.723473] Call trace:
> > [   33.723495] dump_backtrace+0x0/0x288
> > [   33.723512] show_stack+0x14/0x20
> > [   33.723533] dump_stack+0xe4/0x124
> > [   33.723551] print_address_description+0x80/0x2e0
> > [   33.723566] kasan_report+0x268/0x2d0
> > [   33.723584] __asan_load1+0x4c/0x58
> > [   33.723601] LZ4_decompress_safe+0x2c4/0x3b8
> > [   33.723619] lz4_decompress_crypto+0x3c/0x70
> > [   33.723636] crypto_decompress+0x58/0x70
> > [   33.723656] zcomp_decompress+0xd4/0x120
> > ...
> >
> > Apart from that, initialize zhdr->mapped_count in init_z3fold_page()
> > and remove "newpage" variable because it is not used anywhere.
> >
> > Signed-off-by: Uladzislau Rezki <uladzislau.rezki@sony.com>
> > Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.com>
>
> I assume that a cc:stable is appropriate here?

Absolutely. stable was in fact in CC: but it didn't reflect in the
patch for some reason. Thanks!

~Vitaly
diff mbox series

Patch

diff --git a/mm/z3fold.c b/mm/z3fold.c
index 42f31c4b53ad..8c3bb5e508b8 100644
--- a/mm/z3fold.c
+++ b/mm/z3fold.c
@@ -318,16 +318,16 @@  static inline void free_handle(unsigned long handle)
 	slots = handle_to_slots(handle);
 	write_lock(&slots->lock);
 	*(unsigned long *)handle = 0;
-	write_unlock(&slots->lock);
-	if (zhdr->slots == slots)
+	if (zhdr->slots == slots) {
+		write_unlock(&slots->lock);
 		return; /* simple case, nothing else to do */
+	}
 
 	/* we are freeing a foreign handle if we are here */
 	zhdr->foreign_handles--;
 	is_free = true;
-	read_lock(&slots->lock);
 	if (!test_bit(HANDLES_ORPHANED, &slots->pool)) {
-		read_unlock(&slots->lock);
+		write_unlock(&slots->lock);
 		return;
 	}
 	for (i = 0; i <= BUDDY_MASK; i++) {
@@ -336,7 +336,7 @@  static inline void free_handle(unsigned long handle)
 			break;
 		}
 	}
-	read_unlock(&slots->lock);
+	write_unlock(&slots->lock);
 
 	if (is_free) {
 		struct z3fold_pool *pool = slots_to_pool(slots);
@@ -422,6 +422,7 @@  static struct z3fold_header *init_z3fold_page(struct page *page, bool headless,
 	zhdr->start_middle = 0;
 	zhdr->cpu = -1;
 	zhdr->foreign_handles = 0;
+	zhdr->mapped_count = 0;
 	zhdr->slots = slots;
 	zhdr->pool = pool;
 	INIT_LIST_HEAD(&zhdr->buddy);