Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4481494ybx; Mon, 4 Nov 2019 14:09:08 -0800 (PST) X-Google-Smtp-Source: APXvYqx2FerPXX75937g8WjvRKilVXli6NuHG2yvMeROvgA3ioG93tGiXS9wv9uO/4h1fwY2RsfS X-Received: by 2002:a17:906:f109:: with SMTP id gv9mr4973621ejb.196.1572905348585; Mon, 04 Nov 2019 14:09:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905348; cv=none; d=google.com; s=arc-20160816; b=S/NT9CktxMfGh8Sg/O48kowssb15Zoevh27vl5UospLKigPE4PQoHnKUPw4sphh48s J4pAj39WGoytITHzc9V4jyHNj739zm7entkgNZabKaWea0jx+y7lasFniDHxe9ARhmSn 8gEUmlIK0UTXcI67iLeQzSpJrBSjhw9tdy5xEVWfMsHE0VrAopDXSepbKrgAfmtCyEbm QCQ4GTeG3AwIvH5LdbICnfOi/uDoc5hCP/hjR+b/9SRKMs84ewrGzbbTH/A0M2Ruu26G 3BZSfuBFD5XsWqOHqHpuxuDQFWcfWms5v2+gA1hc9FCeTT5eZzkdk7S+1+NmYQzb+UnM wwKQ== 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=EQE8lU0KQyV/NjmlAtMdFO8ihzzuyGA6O5Gxaw5zbW0=; b=cyidbIgDx/aUyF5PUaM3ECglOsC91ur/xZZWcVry9Q2uv502e3S9qIIEqNzeZI7NkM I9nCeo8t1nS8c0hXUqVAppqCTqMjf/ljMceyuCze7CNum3tfuqFzOiMYHfvKLQZgx4SG iPsvZ1hAROTaNGOvBKFLt5byTik9jNSh61WGhB6qhPnls3e2gl9yRTzVw8YUKs2PxWA5 RPPT/B2A2GVn3Cgutb9P7HG4V/vq58tWRvpBKQ1cWaJ+ItJ/FM414MgJrN77YPU58R3Z HA3Fvh2RyoVdlBtdDDsYVIs2loL9Ib5C4mFz80f8Jbnx9K5pVoqDLPwQj66GsToupOqE DXig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=X+qMSEBb; 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 h13si3584613ejq.54.2019.11.04.14.08.45; Mon, 04 Nov 2019 14:09:08 -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=X+qMSEBb; 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 S2389976AbfKDWGe (ORCPT + 99 others); Mon, 4 Nov 2019 17:06:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:38246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389965AbfKDWG1 (ORCPT ); Mon, 4 Nov 2019 17:06:27 -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 4205020650; Mon, 4 Nov 2019 22:06:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572905186; bh=jQsDchaHHk7q+hECoo6OxNL9DQD/CglCbpxAufwSRk4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X+qMSEBbG0b8HVhuOST1WsYpD2tML3Fyj6bG5Mwn+q3rAsZscbC27+ys1O9cAUZLI dH7/Lv/9iRV1KtGsdhx9zAFue9FblZZWaqmE7cQyUcukh5Hfvx62++++9damTOZAjJ cqy+uXZYe/yUWteXCIjYwATsPgRZDUk2ZHwUFCMs= 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 5.3 059/163] fs: ocfs2: fix possible null-pointer dereferences in ocfs2_xa_prepare_entry() Date: Mon, 4 Nov 2019 22:44:09 +0100 Message-Id: <20191104212144.352654938@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212140.046021995@linuxfoundation.org> References: <20191104212140.046021995@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 90c830e3758e2..d8507972ee135 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -1490,18 +1490,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) { @@ -2133,29 +2121,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