Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp2399454ybi; Mon, 1 Jul 2019 11:19:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqxPrvwa/MB28t9exYqM+qjeNPuCzXw5iPKMIvLhHflVI29BHQP1gKYLiij2ELJUAiQiST4O X-Received: by 2002:a63:f510:: with SMTP id w16mr26672646pgh.0.1562005194735; Mon, 01 Jul 2019 11:19:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562005194; cv=none; d=google.com; s=arc-20160816; b=j0E8mT9Sq5ukgcRp+k0hLqtD9FPhQ8vIjwVq0mhWs5bNAsgm06OAJnLus5gRdyRWvX V+/cuy5pw2Lrn6MnA+LlYQwRXO04FK57X8L6pWlvEramuaB8L075vs4dm/n4Z4RzUkls OywSddOZExnWTZB908eiNPYQiHHGgAHQpicErvL4kOP0tWaFJDC9vkHK4EwwiUvTdV3M 8QlvRF8MGkvBqluHaYZm+jRvezMNUoyy92v4sUKv2jsibwXv2aU1nx+FvxWN12BawHgA ztzWHYIgJVNOQmsLHSScAF26/uyaFvKSioSZh2JVKXhEPXjgxGf3YAdB4D30n0nnD9zr Px2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=57FXAe/wgMHkImrRgxBMfz6F69lw3WXfZi6uoGW6nJo=; b=oVZ0txTPuVr0ipVm+T48wjGU4fdP/VNSI84UBMX8d8SUtFIJ6SqYGeRhr2OYw763S0 8MX95jlwv02mhuLr/dvpKRez/lQ/eB5K+Qm8RBEuEZZY8C7TAgMqFigXppxAJ12ri3zM 7psJP4n4Dvis60Gvf7M4AiZkiDqB7/KnZ7u34IJY9whzVLOFb6d6IDU6aF7058zbOcwK ooOXts8k+DOU+ngH1qRizowlk30w9KHcC8e3S9xKLuKJc5ky+zMMgVDSPzQ3b9p96LZ5 Vy1h3SrQR64XSYjp5lkfYwWdYOaiHQbY8nKsE4Mp6hM6qHbA2vuXZBxAEsZAW9tgupAX ah3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SYl2HFPL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v11si211274pjn.44.2019.07.01.11.19.06; Mon, 01 Jul 2019 11:19:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SYl2HFPL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729658AbfGARbR (ORCPT + 99 others); Mon, 1 Jul 2019 13:31:17 -0400 Received: from mail-qt1-f202.google.com ([209.85.160.202]:40169 "EHLO mail-qt1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727849AbfGARbR (ORCPT ); Mon, 1 Jul 2019 13:31:17 -0400 Received: by mail-qt1-f202.google.com with SMTP id z6so13977517qtj.7 for ; Mon, 01 Jul 2019 10:31:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=57FXAe/wgMHkImrRgxBMfz6F69lw3WXfZi6uoGW6nJo=; b=SYl2HFPLHC4vO1l86HKGG+KsnA3p3ugT/bhNQ/ORJtqVZc8wQfosqv55sqkPjCue2f 7e+f0w+fwy4nCLJm+aSX0gjcTPTFuslk7RJ2coLvl4tv42WYnQaSVWoyeshxUbGcyoO4 hhD0T0RPIoAUdBGs62mRr5atpiOg14m1NCtJjHk47BVcRRY/tyaLMM7grU8ZMoLNJZwx IcepxW70TRfXeKJAeFoYS7BQzeBLr9pEERYN8p01Y1/ZagLCW5l8l5vOesfkxnJcd8au EhxOm/nyih6HRvFbzX+vkO9xWXLQ+vCqk6tmWeWdmKAGMqonEnvKkeksITidwsCJDq9F iQEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=57FXAe/wgMHkImrRgxBMfz6F69lw3WXfZi6uoGW6nJo=; b=JNDex+O0k9ILV0t7OSn65rvPnzkrXxnj8DOlWdOymQ45dVMJWB0YO9jnpVXh/EeaXa 65i1PUw3gbYftnmkfYpq0Bvwni2uY/IHtfv/9iw+J+wxJEy74EdxGOgehprKueGMjlW3 bw40gBvz8g4Vh/QDZ0j17YqpwwtG/vdi2CpN3YgYd39QW1vS6ZW+FHCMGpqr10+5liPJ h87kJayR/yTdYfwwVXY1GnXgVUkn9bDJGEFO2D8T7+QVEHf1vxr/QsMRa/pGdC7xFGro i/WVDa8+XVgSkFJYDPhjI51pIZfWtap1F6RGKQuuXYd7gLCERH5M0/Eh9PsHqkDeDHNn VM4g== X-Gm-Message-State: APjAAAX7mFbpJgkMyFrtWRJbdJo/KtiFA+DKhktv/UFQDmGH7i0veNzL vYj0CEYfRDOdXLE5zaMhMka0lWlPtno4vj6x X-Received: by 2002:ac8:fbb:: with SMTP id b56mr2584122qtk.324.1562002276640; Mon, 01 Jul 2019 10:31:16 -0700 (PDT) Date: Mon, 1 Jul 2019 10:30:42 -0700 Message-Id: <20190701173042.221453-1-henryburns@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH] mm/z3fold: Fix z3fold_buddy_slots use after free From: Henry Burns To: Vitaly Wool , Andrew Morton Cc: Matthew Wilcox , Vitaly Vul , Mike Rapoport , Xidong Wang , Shakeel Butt , Jonathan Adams , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Henry Burns Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Running z3fold stress testing with address sanitization showed zhdr->slots was being used after it was freed. z3fold_free(z3fold_pool, handle) free_handle(handle) kmem_cache_free(pool->c_handle, zhdr->slots) release_z3fold_page_locked_list(kref) __release_z3fold_page(zhdr, true) zhdr_to_pool(zhdr) slots_to_pool(zhdr->slots) *BOOM* Instead we split free_handle into two functions, release_handle() and free_slots(). We use release_handle() in place of free_handle(), and use free_slots() to call kmem_cache_free() after __release_z3fold_page() is done. Fixes: 7c2b8baa61fe ("mm/z3fold.c: add structure for buddy handles") Signed-off-by: Henry Burns --- mm/z3fold.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/mm/z3fold.c b/mm/z3fold.c index f7993ff778df..e174d1549734 100644 --- a/mm/z3fold.c +++ b/mm/z3fold.c @@ -213,31 +213,24 @@ static inline struct z3fold_buddy_slots *handle_to_slots(unsigned long handle) return (struct z3fold_buddy_slots *)(handle & ~(SLOTS_ALIGN - 1)); } -static inline void free_handle(unsigned long handle) +static inline void release_handle(unsigned long handle) { - struct z3fold_buddy_slots *slots; - int i; - bool is_free; - if (handle & (1 << PAGE_HEADLESS)) return; WARN_ON(*(unsigned long *)handle == 0); *(unsigned long *)handle = 0; - slots = handle_to_slots(handle); - is_free = true; - for (i = 0; i <= BUDDY_MASK; i++) { - if (slots->slot[i]) { - is_free = false; - break; - } - } +} - if (is_free) { - struct z3fold_pool *pool = slots_to_pool(slots); +/* At this point all of the slots should be empty */ +static inline void free_slots(struct z3fold_buddy_slots *slots) +{ + struct z3fold_pool *pool = slots_to_pool(slots); + int i; - kmem_cache_free(pool->c_handle, slots); - } + for (i = 0; i <= BUDDY_MASK; i++) + VM_BUG_ON(slots->slot[i]); + kmem_cache_free(pool->c_handle, slots); } static struct dentry *z3fold_do_mount(struct file_system_type *fs_type, @@ -431,7 +424,8 @@ static inline struct z3fold_pool *zhdr_to_pool(struct z3fold_header *zhdr) static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked) { struct page *page = virt_to_page(zhdr); - struct z3fold_pool *pool = zhdr_to_pool(zhdr); + struct z3fold_buddy_slots *slots = zhdr->slots; + struct z3fold_pool *pool = slots_to_pool(slots); WARN_ON(!list_empty(&zhdr->buddy)); set_bit(PAGE_STALE, &page->private); @@ -442,6 +436,7 @@ static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked) spin_unlock(&pool->lock); if (locked) z3fold_page_unlock(zhdr); + free_slots(slots); spin_lock(&pool->stale_lock); list_add(&zhdr->buddy, &pool->stale); queue_work(pool->release_wq, &pool->work); @@ -1009,7 +1004,7 @@ static void z3fold_free(struct z3fold_pool *pool, unsigned long handle) return; } - free_handle(handle); + release_handle(handle); if (kref_put(&zhdr->refcount, release_z3fold_page_locked_list)) { atomic64_dec(&pool->pages_nr); return; -- 2.22.0.410.gd8fdbe21b5-goog