Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4492891ybx; Mon, 4 Nov 2019 14:19:30 -0800 (PST) X-Google-Smtp-Source: APXvYqxq+vqwWXmiReDnnrX2B5JbAQFkqQYlTzYA+qGJiMn/rUFCf+U0MO+QPkmwOSnsYLo69+fq X-Received: by 2002:a17:906:f753:: with SMTP id jp19mr12306712ejb.216.1572905970515; Mon, 04 Nov 2019 14:19:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905970; cv=none; d=google.com; s=arc-20160816; b=x+ji3szoF4MbnwbwEPqWfUVljtS91F2QW6bNx/tQbUWtT32cU0LrcxKQS3UCUEn4xO vb7JANt1KEHImdyF9JEVXqg/RbmV9JFfVUr1TeNVFv3nK0WG+AcfFlNqQ3bwKbY8yaK6 /GubG70OCi4BZaoVc7g0boF9mqBiKHsk6KjNrC6+LA/jIKQ1DJpmpBztdjd8fbRp4oJj xTuJjo60WkvEUi/HWAzXvjeIny0BkppvX7QXdnpXFlRvRpNHBHLvbgcj8uFIrzLzD/4V x9yRAqU4wXrVH3gNUAz5Aix3XAi0CIxsEGKJ4AVV7kYBlSQpgqd8IdEjZ8+ACFz3enlc 7jMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EezLMaqMDGKvU6Eu0kQuKZ9N52XHtBs5Ib7dXKD9pLc=; b=NUFPgxbCOyTYK1SgtbvMz+t7NRV5pWW5T9lw0q2t8H4mM3GDlEdSrqI9JKWbbtAAL4 k+QwnZMO45ASurzBZhnAza3TORH52SOwVXh3IpYkFQ6z90cd+fUAnKr4v1DHbtIE+/GR 0WFi+8vsGPZz7muQqdCfc88FyTcq+t7qTKX8pDyiRhArFA/CNOhEvHy+YnpH2L5R7APi 0tVUIbuVqX+eVunjnf4DrLO0OJq/oBckwjM4oXu6KZSaO2ykhakDHRUb5iAMWY7Xe4nq 4hufORTxZ5TPWKXWOYa69iq0fvBN+dqsfglAsGabjZL3Y3OPvj5KVZvC2HdXulwxV4Xg 0WDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jyQx9bPH; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a3si5132324eju.377.2019.11.04.14.19.07; Mon, 04 Nov 2019 14:19:30 -0800 (PST) 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=@kernel.org header.s=default header.b=jyQx9bPH; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389542AbfKDWRY (ORCPT + 99 others); Mon, 4 Nov 2019 17:17:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:60922 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389362AbfKDWCh (ORCPT ); Mon, 4 Nov 2019 17:02:37 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id ED02F20650; Mon, 4 Nov 2019 22:02:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572904956; bh=hPVGPpuQjbY6YzEOwRSUm73eB3cXzpvsyl8+rusrhQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jyQx9bPH9Np/ApLaaZnatvB8u5NWbXgTuIJ40bGR12rwnB8ghOjITadoyZYCaeIoz iKFzH4l2pbPVbDdsjPjyfWnjk60cbzofZzeym6iZUiCVXqE5PdBxexDw0SUFRtnE53 +xrWhVQISR2ocojFIWn+PSbkqz09YCwEppiQOWGs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jia-Ju Bai , Joseph Qi , Mark Fasheh , Joel Becker , Junxiao Bi , Changwei Ge , Gang He , Jun Piao , Stephen Rothwell , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.19 086/149] fs: ocfs2: fix possible null-pointer dereferences in ocfs2_xa_prepare_entry() Date: Mon, 4 Nov 2019 22:44:39 +0100 Message-Id: <20191104212142.574924647@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212126.090054740@linuxfoundation.org> References: <20191104212126.090054740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jia-Ju Bai [ Upstream commit 56e94ea132bb5c2c1d0b60a6aeb34dcb7d71a53d ] In ocfs2_xa_prepare_entry(), there is an if statement on line 2136 to check whether loc->xl_entry is NULL: if (loc->xl_entry) When loc->xl_entry is NULL, it is used on line 2158: ocfs2_xa_add_entry(loc, name_hash); loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash); loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size); and line 2164: ocfs2_xa_add_namevalue(loc, xi); loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len); loc->xl_entry->xe_name_len = xi->xi_name_len; Thus, possible null-pointer dereferences may occur. To fix these bugs, if loc-xl_entry is NULL, ocfs2_xa_prepare_entry() abnormally returns with -EINVAL. These bugs are found by a static analysis tool STCheck written by us. [akpm@linux-foundation.org: remove now-unused ocfs2_xa_add_entry()] Link: http://lkml.kernel.org/r/20190726101447.9153-1-baijiaju1990@gmail.com Signed-off-by: Jia-Ju Bai Reviewed-by: Joseph Qi Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Changwei Ge Cc: Gang He Cc: Jun Piao Cc: Stephen Rothwell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/ocfs2/xattr.c | 56 ++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index c146e12a8601f..0d80e0df6c241 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -1498,18 +1498,6 @@ static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc, return loc->xl_ops->xlo_check_space(loc, xi); } -static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash) -{ - loc->xl_ops->xlo_add_entry(loc, name_hash); - loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash); - /* - * We can't leave the new entry's xe_name_offset at zero or - * add_namevalue() will go nuts. We set it to the size of our - * storage so that it can never be less than any other entry. - */ - loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size); -} - static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc, struct ocfs2_xattr_info *xi) { @@ -2141,29 +2129,31 @@ static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc, if (rc) goto out; - if (loc->xl_entry) { - if (ocfs2_xa_can_reuse_entry(loc, xi)) { - orig_value_size = loc->xl_entry->xe_value_size; - rc = ocfs2_xa_reuse_entry(loc, xi, ctxt); - if (rc) - goto out; - goto alloc_value; - } + if (!loc->xl_entry) { + rc = -EINVAL; + goto out; + } - if (!ocfs2_xattr_is_local(loc->xl_entry)) { - orig_clusters = ocfs2_xa_value_clusters(loc); - rc = ocfs2_xa_value_truncate(loc, 0, ctxt); - if (rc) { - mlog_errno(rc); - ocfs2_xa_cleanup_value_truncate(loc, - "overwriting", - orig_clusters); - goto out; - } + if (ocfs2_xa_can_reuse_entry(loc, xi)) { + orig_value_size = loc->xl_entry->xe_value_size; + rc = ocfs2_xa_reuse_entry(loc, xi, ctxt); + if (rc) + goto out; + goto alloc_value; + } + + if (!ocfs2_xattr_is_local(loc->xl_entry)) { + orig_clusters = ocfs2_xa_value_clusters(loc); + rc = ocfs2_xa_value_truncate(loc, 0, ctxt); + if (rc) { + mlog_errno(rc); + ocfs2_xa_cleanup_value_truncate(loc, + "overwriting", + orig_clusters); + goto out; } - ocfs2_xa_wipe_namevalue(loc); - } else - ocfs2_xa_add_entry(loc, name_hash); + } + ocfs2_xa_wipe_namevalue(loc); /* * If we get here, we have a blank entry. Fill it. We grow our -- 2.20.1