From patchwork Sat Jan 28 19:19:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill O'Donnell X-Patchwork-Id: 9543441 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3E35F60415 for ; Sat, 28 Jan 2017 19:27:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 30ACF28178 for ; Sat, 28 Jan 2017 19:27:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2561F28335; Sat, 28 Jan 2017 19:27:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 81CC428319 for ; Sat, 28 Jan 2017 19:27:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751768AbdA1T1U (ORCPT ); Sat, 28 Jan 2017 14:27:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49522 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751968AbdA1T1Q (ORCPT ); Sat, 28 Jan 2017 14:27:16 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 66D977F0A3; Sat, 28 Jan 2017 19:20:00 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-118-182.rdu2.redhat.com [10.10.118.182]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v0SJJxYp004972; Sat, 28 Jan 2017 14:19:59 -0500 From: "Bill O'Donnell" To: linux-xfs@vger.kernel.org Cc: darrick.wong@oracle.com Subject: [PATCH] xfs: correct null checks and error processing in xfs_initialize_perag Date: Sat, 28 Jan 2017 13:19:57 -0600 Message-Id: <20170128191957.13851-1-billodo@redhat.com> In-Reply-To: <20170120142642.21698-1-colin.king@canonical.com> References: <20170120142642.21698-1-colin.king@canonical.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Sat, 28 Jan 2017 19:20:00 +0000 (UTC) Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If pag cannot be allocated, the current error exit path will trip a null pointer deference error when calling xfs_buf_hash_destroy with a null pag. Fix this by adding a new error exit labels and jumping to those accordingly, avoiding the hash destroy and unnecessary kmem_free on pag. Up to three things need to be properly unwound: 1) pag memory allocation 2) xfs_buf_hash_init 3) radix_tree_insert For any given iteration through the loop, any of the above which succeed must be unwound for /this/ pag, and then all prior initialized pags must be unwound. Fixes CoverityScan CID#1397628 ("Dereference after null check") Reported-by: Colin Ian King Signed-off-by: Bill O'Donnell --- fs/xfs/xfs_mount.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c index 9b9540d..b074d2a 100644 --- a/fs/xfs/xfs_mount.c +++ b/fs/xfs/xfs_mount.c @@ -190,6 +190,7 @@ xfs_initialize_perag( xfs_agnumber_t first_initialised = 0; xfs_perag_t *pag; int error = -ENOMEM; + bool first_init_done = false; /* * Walk the current per-ag tree so we don't try to initialise AGs @@ -202,22 +203,20 @@ xfs_initialize_perag( xfs_perag_put(pag); continue; } - if (!first_initialised) - first_initialised = index; pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); if (!pag) - goto out_unwind; + goto out_unwind_new_pags; pag->pag_agno = index; pag->pag_mount = mp; spin_lock_init(&pag->pag_ici_lock); mutex_init(&pag->pag_ici_reclaim_lock); INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); if (xfs_buf_hash_init(pag)) - goto out_unwind; + goto out_free_pag; if (radix_tree_preload(GFP_NOFS)) - goto out_unwind; + goto out_free_pag; spin_lock(&mp->m_perag_lock); if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { @@ -225,10 +224,15 @@ xfs_initialize_perag( spin_unlock(&mp->m_perag_lock); radix_tree_preload_end(); error = -EEXIST; - goto out_unwind; + goto out_hash_destroy; } spin_unlock(&mp->m_perag_lock); radix_tree_preload_end(); + /* a pag is fully initialized */ + if (!first_init_done) { + first_initialised = index; + first_init_done = true; + } } index = xfs_set_inode_alloc(mp, agcount); @@ -239,13 +243,24 @@ xfs_initialize_perag( mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); return 0; -out_unwind: +out_hash_destroy: xfs_buf_hash_destroy(pag); +out_free_pag: kmem_free(pag); - for (; index > first_initialised; index--) { - pag = radix_tree_delete(&mp->m_perag_tree, index); - xfs_buf_hash_destroy(pag); - kmem_free(pag); +out_unwind_new_pags: + /* unwind any newly initialized pags */ + if (first_init_done) { + index--; + do { + pag = radix_tree_delete(&mp->m_perag_tree, index); + if (pag) { + xfs_buf_hash_destroy(pag); + kmem_free(pag); + } + if (!index) + break; + index--; + } while (index >= first_initialised); } return error; }