z3fold: fix use-after-free when freeing handles

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:

  ==================================================================
  BUG: KASAN: use-after-free in LZ4_decompress_safe+0x2c4/0x3b8
  Read of size 1 at addr ffffffc976695ca3 by task GoogleApiHandle/4121

  CPU: 0 PID: 4121 Comm: GoogleApiHandle Tainted: P S         OE     4.19.81-perf+ #162
  Hardware name: Sony Mobile Communications. PDX-203(KONA) (DT)
  Call trace:
     LZ4_decompress_safe+0x2c4/0x3b8
     lz4_decompress_crypto+0x3c/0x70
     crypto_decompress+0x58/0x70
     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>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Qian Cai <cai@lca.pw>
Cc: Raymond Jennings <shentino@gmail.com>
Cc: <stable@vger.kernel.org>
Link: http://lkml.kernel.org/r/20200520082100.28876-1-vitaly.wool@konsulko.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Uladzislau Rezki 2020-05-22 22:23:12 -07:00 committed by Linus Torvalds
parent c2bc26f7ca
commit d8f117abb3
1 changed files with 6 additions and 5 deletions

View File

@ -318,16 +318,16 @@ static inline void free_handle(unsigned long handle)
slots = handle_to_slots(handle); slots = handle_to_slots(handle);
write_lock(&slots->lock); write_lock(&slots->lock);
*(unsigned long *)handle = 0; *(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 */ return; /* simple case, nothing else to do */
}
/* we are freeing a foreign handle if we are here */ /* we are freeing a foreign handle if we are here */
zhdr->foreign_handles--; zhdr->foreign_handles--;
is_free = true; is_free = true;
read_lock(&slots->lock);
if (!test_bit(HANDLES_ORPHANED, &slots->pool)) { if (!test_bit(HANDLES_ORPHANED, &slots->pool)) {
read_unlock(&slots->lock); write_unlock(&slots->lock);
return; return;
} }
for (i = 0; i <= BUDDY_MASK; i++) { for (i = 0; i <= BUDDY_MASK; i++) {
@ -336,7 +336,7 @@ static inline void free_handle(unsigned long handle)
break; break;
} }
} }
read_unlock(&slots->lock); write_unlock(&slots->lock);
if (is_free) { if (is_free) {
struct z3fold_pool *pool = slots_to_pool(slots); 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->start_middle = 0;
zhdr->cpu = -1; zhdr->cpu = -1;
zhdr->foreign_handles = 0; zhdr->foreign_handles = 0;
zhdr->mapped_count = 0;
zhdr->slots = slots; zhdr->slots = slots;
zhdr->pool = pool; zhdr->pool = pool;
INIT_LIST_HEAD(&zhdr->buddy); INIT_LIST_HEAD(&zhdr->buddy);