From patchwork Wed May 20 08:21:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Wool X-Patchwork-Id: 11559697 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 50334138A for ; Wed, 20 May 2020 08:21:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1B9882065F for ; Wed, 20 May 2020 08:21:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="GjNT2dQa" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1B9882065F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 57F0C80007; Wed, 20 May 2020 04:21:09 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 52CDE900002; Wed, 20 May 2020 04:21:09 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4430080007; Wed, 20 May 2020 04:21:09 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0209.hostedemail.com [216.40.44.209]) by kanga.kvack.org (Postfix) with ESMTP id 2D011900002 for ; Wed, 20 May 2020 04:21:09 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D8D402466 for ; Wed, 20 May 2020 08:21:08 +0000 (UTC) X-FDA: 76836402216.15.geese20_7e625f2c36d35 X-Spam-Summary: 2,0,0,7a78d7cfe1ec9875,d41d8cd98f00b204,vitaly.wool@konsulko.com,,RULES_HIT:41:355:379:541:800:960:965:966:968:973:982:988:989:1260:1311:1314:1345:1437:1515:1535:1542:1711:1730:1747:1777:1792:2194:2196:2199:2200:2393:2553:2559:2562:3138:3139:3140:3141:3142:3353:3866:3867:3868:3870:3871:3872:3874:4250:4321:4385:4390:4395:4605:5007:6261:6653:7558:7576:7903:7904:8603:8660:8784:8957:9163:10004:11026:11473:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12679:12683:12895:13148:13230:13894:14096:14110:14181:14394:14721:21080:21094:21323:21433:21444:21451:21627:21987:21990:30028:30029:30054:30056:30090,0,RBL:209.85.221.66:@konsulko.com:.lbl8.mailshell.net-66.201.201.201 62.14.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:42,LUA_SUMMARY:none X-HE-Tag: geese20_7e625f2c36d35 X-Filterd-Recvd-Size: 5488 Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Wed, 20 May 2020 08:21:08 +0000 (UTC) Received: by mail-wr1-f66.google.com with SMTP id l18so2196755wrn.6 for ; Wed, 20 May 2020 01:21:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GyYFmyLcXeIyPhE27WYSdki7aWcLi6/AfrIYwKf/g0I=; b=GjNT2dQaEN0/52h/KQ7W62r7HmLQZKDNbHoE9XlnNuKfKZNFb21OsIzmdguSV2HQrx ZStxI0Tn1RMO9HxqkkJevJIYfnvtF4lCJEtl+bY/Pe4Sv3GPWeMFIjIjEjt/h9MJcyKH v1BKPUNLZDNTT43mvwsu/kIgegmBclZjjysaU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GyYFmyLcXeIyPhE27WYSdki7aWcLi6/AfrIYwKf/g0I=; b=kZLMFkNZmuH4pGZtwufL/+s4V0lCF6XqIBvU5QLll0q7uF2ikl1JjHFZtIt8PrzDp/ 4llFXg9geduV2KkUpfqyy9I5aen6wnGvBCX8uSBi92WcDvHqGw9tXIKG0XeufVG0xzpD 7JlNyOCPgyclcSrTItdao1eS6r+QXE5O+Nmc4hqKnkZEMFtZ/UrZBMHXrv9v1fw/zFUH alEpSsOjZf+PgMBl/AuaeEjCi78ZiE/E+wrYeTPOhRBr9Gj7zfJHmB84ahWDh2W5DPnT x73NGD4sS2lsEM9ciHA6O1gU3ejdq5L0loCSVcAOwwjHYJrp5slwt65Xc6STZcPA7tk5 oqGA== X-Gm-Message-State: AOAM531yhlOVosrifaW2uyLXrkq6RVYE8Ytg5CToVcziyHyES/NRDeIc Xgc09DcxA+GveWA6S+NTOjBXT8UcL0+QKBwq X-Google-Smtp-Source: ABdhPJy46CX+nOqi6e9vkmv5rm7kcuqmI5+GXCSjdJaVbD9rgminVQvg9UO0BQmtuJfpX5Snp49Gfw== X-Received: by 2002:adf:9d91:: with SMTP id p17mr2947544wre.119.1589962866465; Wed, 20 May 2020 01:21:06 -0700 (PDT) Received: from taos.konsulko.bg (lan.nucleusys.com. [92.247.61.126]) by smtp.gmail.com with ESMTPSA id 89sm2071486wrj.37.2020.05.20.01.21.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2020 01:21:05 -0700 (PDT) From: vitaly.wool@konsulko.com To: linux-mm@kvack.org Cc: stable@kernel.org, cai@lca.pw, shentino@gmail.com, akpm@linux-foundation.org, Uladzislau Rezki , Vitaly Wool Subject: [PATCH] z3fold: fix use-after-free when freeing handles Date: Wed, 20 May 2020 11:21:00 +0300 Message-Id: <20200520082100.28876-1-vitaly.wool@konsulko.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Uladzislau Rezki 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 Signed-off-by: Vitaly Wool --- mm/z3fold.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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);