Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3836434ybv; Tue, 25 Feb 2020 08:10:12 -0800 (PST) X-Google-Smtp-Source: APXvYqzMBlKXjIh83xjM33stwckhbpfJyjr0jrxli7PproEgl9ZacOpOMtF/J1jSr8e+kQ+bWx2N X-Received: by 2002:a9d:116:: with SMTP id 22mr21308465otu.149.1582647012139; Tue, 25 Feb 2020 08:10:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582647012; cv=none; d=google.com; s=arc-20160816; b=GB6dFGa6OMAv+/JoPgDbRZyX+Om81KwtAETe/ZFXVCgprWJvMkFns8oklo5MA/0zAZ ofkY+n1Nn2KfYnY5sGZ2YSGJOKDPT2/JBQxJ+cXbVtRnCWAYl4QneBecox2MPcmM3Ndl J2dslQj++PIbi447blSAHue0xy1AB9UF7g8/DV6YwV/5pWhxiFXu7EDyt7/+FYxoTniQ 3txMh0RU1SpaaQvK826ZFiKkR2ddtX7d+aeZH8Yp8/wxfq94vtsIRzsJjKiEt6An8EC9 VhOKQHdg7NaUPKo2g3ZKRxReeMniT1909SFfSc3VAdnEvWHJR86bwNYpYpsobMPoM9/u 95/g== 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 :references:in-reply-to:date:cc:to:from:subject:message-id :dkim-signature; bh=jzzR6L46tUOZAhynuJXzNpU9O0uz0Bf0qECc4tNwkIg=; b=kmGvha51vXdhbqXiXJgCIA1HC3NGHTD4KF9obuE20Buo0yDPv6ZFcDT8w9TmENRuah w0zBMEPG3w9Pi6Itt+83AmidFOocHDyBr/V4o1/BLA2mnAkRk7O1QYKR59+TmD56UGug dQsQclS0Izj/oTcxnaCbsRhgPG+kcCeKgn33WWOH6ZNJsHh36zfO8b+fz5RiCoINbG0E AzDPMWaNfFi4+VXfnkIxqlcn8SjOmo/DjiDPLla97mkVR9l1izsHC/Jb939hP9XfmMVg pTUJA7WzFtATWpsa5Lh2Gr4h8rUfR6BhiG7I1neglTq0QsNhB1AxpqroOloJcTJkJ9Hz WdAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=rZtO+Qsr; 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 x6si8222315ota.322.2020.02.25.08.09.59; Tue, 25 Feb 2020 08:10:12 -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=@lca.pw header.s=google header.b=rZtO+Qsr; 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 S1730729AbgBYPrB (ORCPT + 99 others); Tue, 25 Feb 2020 10:47:01 -0500 Received: from mail-qt1-f194.google.com ([209.85.160.194]:36742 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730566AbgBYPrA (ORCPT ); Tue, 25 Feb 2020 10:47:00 -0500 Received: by mail-qt1-f194.google.com with SMTP id t13so9352494qto.3 for ; Tue, 25 Feb 2020 07:46:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=jzzR6L46tUOZAhynuJXzNpU9O0uz0Bf0qECc4tNwkIg=; b=rZtO+QsrE3oj/5lFyd8Y3/K8ZQ8r1IDNHDwGoDkgnrb/nVLhmTI9e2PI2ohrrbqfM0 bEAbu6R8+pp0LB650RskEjp8tbYMa6WPYQuDYX85AcR98/GXNnc00u6KaTULY1nh9R02 h7joh4MOE0qdRwEDRb8TqJ70ug4V87jrId+YRPAX8ueDWb/Ym9MDZ7D/uLar6DDJnzSe 7gxdHkG/x0RlId4D112NQs3qwtM3pseZOtHu76echkC3TSCquc+1k7kGtLaS9/7E71PE N7ZguGElmb+5sB4xm3FmjEV1HJe/460Og7WXc3OiQ7B5Y+gj7L/7F2WlETxYgUl47m+m xvEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=jzzR6L46tUOZAhynuJXzNpU9O0uz0Bf0qECc4tNwkIg=; b=iHN+DuSDm7gns4jFETvwbQSJzn5kJXFqhadLsOs/op9/TA8q2gDbZqr1NoIJ5KqXpE eLNB6iB604bnhx5srzt7rupwF+tPfda9MCDl+tTqQsU2KU3gmjFKL/kVkgXtPQdrfX4u AJXgp3vJAVzSi3VUNkL7I97ZaQAzuHOqkJeGoY3QTVxF5vtgyAyiuoy2DP+biHw1ZrXc N6DJqKPv65tvGlTYjoIXNJQqRADe6DsBVfqwS22Z7vJJM2DI1bKbaBjqCrRVH+1voWt0 EWzJVzxt/vq07De0LvmJSrPRh/aR6bUSOPt7fQDzBKiqu7AZLV04R7o91cm5LtUrbA0P iepA== X-Gm-Message-State: APjAAAUtqIVCy1CaQRzafgLRtwI0z3l3JZaCssRTUyE+PZIGFALfI4aU xgmP5H3KqUExlNmnOm2lu0Mz9w== X-Received: by 2002:aed:2284:: with SMTP id p4mr53320481qtc.329.1582645619001; Tue, 25 Feb 2020 07:46:59 -0800 (PST) Received: from dhcp-41-57.bos.redhat.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id o55sm7921809qtf.46.2020.02.25.07.46.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Feb 2020 07:46:58 -0800 (PST) Message-ID: <1582645616.7365.118.camel@lca.pw> Subject: Re: [PATCH] xfs: fix an undefined behaviour in _da3_path_shift From: Qian Cai To: "Darrick J. Wong" Cc: linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org Date: Tue, 25 Feb 2020 10:46:56 -0500 In-Reply-To: <20200225152805.GG6740@magnolia> References: <1582641477-4011-1-git-send-email-cai@lca.pw> <20200225152805.GG6740@magnolia> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.22.6 (3.22.6-10.el7) Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2020-02-25 at 07:28 -0800, Darrick J. Wong wrote: > On Tue, Feb 25, 2020 at 09:37:57AM -0500, Qian Cai wrote: > > state->path.active could be 1 in xfs_da3_node_lookup_int() and then in > > xfs_da3_path_shift() could see state->path.blk[-1]. > > Under what circumstancs can it be 1? Is this a longstanding bug in XFS? > A corrupted filesystem? A deliberately corrupted filesystem? in xfs_da3_node_lookup_int(), for (blk = &state->path.blk[0], state->path.active = 1;  state->path.active <= XFS_DA_NODE_MAXDEPTH;  blk++, state->path.active++) { if (magic == XFS_ATTR_LEAF_MAGIC ||     magic == XFS_ATTR3_LEAF_MAGIC) { blk->magic = XFS_ATTR_LEAF_MAGIC; blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL); break; } if (magic == XFS_DIR2_LEAFN_MAGIC ||     magic == XFS_DIR3_LEAFN_MAGIC) { blk->magic = XFS_DIR2_LEAFN_MAGIC; blk->hashval = xfs_dir2_leaf_lasthash(args->dp,       blk->bp, NULL); break; Isn't that if the first iteration in the loop calls any of those "break", it will have state->path.active = 1 ? I suppose this is a long-standing bug that need UBSAN (no obvious harm could be done later because it will bail out immediately in xfs_da3_path_shift()) and a set of specific conditions to met to trigger. > > > > > UBSAN: Undefined behaviour in fs/xfs/libxfs/xfs_da_btree.c:1989:14 > > index -1 is out of range for type 'xfs_da_state_blk_t [5]' > > Call trace: > > dump_backtrace+0x0/0x2c8 > > show_stack+0x20/0x2c > > dump_stack+0xe8/0x150 > > __ubsan_handle_out_of_bounds+0xe4/0xfc > > xfs_da3_path_shift+0x860/0x86c [xfs] > > xfs_da3_node_lookup_int+0x7c8/0x934 [xfs] > > xfs_dir2_node_addname+0x2c8/0xcd0 [xfs] > > xfs_dir_createname+0x348/0x38c [xfs] > > xfs_create+0x6b0/0x8b4 [xfs] > > xfs_generic_create+0x12c/0x1f8 [xfs] > > xfs_vn_mknod+0x3c/0x4c [xfs] > > xfs_vn_create+0x34/0x44 [xfs] > > do_last+0xd4c/0x10c8 > > path_openat+0xbc/0x2f4 > > do_filp_open+0x74/0xf4 > > do_sys_openat2+0x98/0x180 > > __arm64_sys_openat+0xf8/0x170 > > do_el0_svc+0x170/0x240 > > el0_sync_handler+0x150/0x250 > > el0_sync+0x164/0x180 > > > > Signed-off-by: Qian Cai > > --- > > fs/xfs/libxfs/xfs_da_btree.c | 6 +++++- > > 1 file changed, 5 insertions(+), 1 deletion(-) > > > > diff --git a/fs/xfs/libxfs/xfs_da_btree.c b/fs/xfs/libxfs/xfs_da_btree.c > > index 875e04f82541..0906b7748a3f 100644 > > --- a/fs/xfs/libxfs/xfs_da_btree.c > > +++ b/fs/xfs/libxfs/xfs_da_btree.c > > @@ -1986,7 +1986,11 @@ static inline int xfs_dabuf_nfsb(struct xfs_mount *mp, int whichfork) > > ASSERT(path != NULL); > > ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); > > level = (path->active-1) - 1; /* skip bottom layer in path */ > > - for (blk = &path->blk[level]; level >= 0; blk--, level--) { > > + > > + if (level >= 0) > > + blk = &path->blk[level]; > > ...because if the reason is "corrupt metadata" then perhaps this should > return -EFSCORRUPTED? But I don't know enough about the context to know > the answer to that question. > > --D > > > + > > + for (; level >= 0; blk--, level--) { > > xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, > > blk->bp->b_addr); > > > > -- > > 1.8.3.1 > >