Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp42574ybf; Wed, 26 Feb 2020 08:30:27 -0800 (PST) X-Google-Smtp-Source: APXvYqyUlvo/alS4EdunXKMmpd/nL4RM/Bc/rVrYo4IP9Oxy1/srK1Y0/r5qGuuRAByWhHWl1wDV X-Received: by 2002:aca:5a04:: with SMTP id o4mr3775461oib.113.1582734627294; Wed, 26 Feb 2020 08:30:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582734627; cv=none; d=google.com; s=arc-20160816; b=ynIS+LSkTbSRCpF8CMIdFQ7w6K/qdntCDTU+xRVxGUM66hYYzNzIjuH09cBaKA9RM4 q3ACWgMWro6fGCCQo5i5O5/g7gzLJR5q9hFJV1XCaxaglvsLDnyUmc+qALf/cfCG0+qq Dc3RHNQ3jUoiO9mCQ05O7o0EeCUVeliT8qC11ylimHS6a387C1Y6Ns/omvQZDChh9R0g me4hfkNk6If0lK+CvAA1jVDkoD0ZBHPn33NLTtioec2biK7NyOc4mEznUNaKhByS4U5B wJtaFJ1c9dITvVBPU/F3MmyYfaud3DPA1eRCSQfsLAaFIu7fzL0/LAG8Sr7B8OGhZ88v UPuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=rSpxlhkAatwy5CEDZO3Imq99OvHCaK7NacrlAX8vWKw=; b=rmmP5fgpk4qj9snSRIto2FNrvXDZOPac/PD6aH7gv3sMZ2qjhoCAQOt3LNzjoNJF0Q XsoA917vTkNwCjtB5pBOvkcT431hbrUZRkhzEAc2/1I8Yvl1u01AOwsaEogqbdfjckFD e9W0dJe1jmDEkpmjUOl65+ZO90o6kcrr+yTnDAkHMQN8oiOanUMRdAt4bq9EdxJGMu99 cOmo1X0oF3jR68MxPHI7LJ6u5JR+M1EdRYN4qiQO4lb4y7FzTBcWYB5/xx3r48jpj2XA qmfXi1mwERzcsGQvF4MfUEula1XANhshEecUY4fR+np0eXpqS74jFcYpsBtGqk69rYD4 t3eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=LOeELr7z; 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=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d5si1304702oij.139.2020.02.26.08.30.13; Wed, 26 Feb 2020 08:30:27 -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=@oracle.com header.s=corp-2020-01-29 header.b=LOeELr7z; 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=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727304AbgBZQ2n (ORCPT + 99 others); Wed, 26 Feb 2020 11:28:43 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:34534 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726148AbgBZQ2n (ORCPT ); Wed, 26 Feb 2020 11:28:43 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 01QGMhGw065270; Wed, 26 Feb 2020 16:28:39 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2020-01-29; bh=rSpxlhkAatwy5CEDZO3Imq99OvHCaK7NacrlAX8vWKw=; b=LOeELr7zmIww0rks3k14dOY9o7JD1FLuXk2A77TyJ1M63qgaRhmJdHrpomlI/rRNQ+s5 HYejiw/0yhmqtE2iiR6Y6Bv/et8BrK89cSqRp7l81fXlkbVQdM80maarn+oK4vNbeApI t8zCWI4YNaVpmPM/n9/YVVVFrh8ApVqBNJzNXM1u4nGLp1dAY/u80fxMtV1n3ie71Tvr 7Mn+wByuH9BezL40rac/vWYM/eqsdzqfUwAOkH1raBYvi7sVN2FMhPVyQB4WyK6A4P/x /ibpoaLGZHqmBytO6jLhd2rcCjeK5jEOh95Fj/c/GwtWPWiCt/YiNxo6wGtWs/65KhPy Lw== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by userp2120.oracle.com with ESMTP id 2ydct34sh7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 26 Feb 2020 16:28:39 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 01QGMe4H076138; Wed, 26 Feb 2020 16:28:38 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3030.oracle.com with ESMTP id 2ydcsa5u2b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 26 Feb 2020 16:28:38 +0000 Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 01QGSbEU002323; Wed, 26 Feb 2020 16:28:37 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 26 Feb 2020 08:28:37 -0800 Date: Wed, 26 Feb 2020 08:28:36 -0800 From: "Darrick J. Wong" To: Qian Cai Cc: hch@infradead.org, linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] xfs: fix an undefined behaviour in _da3_path_shift Message-ID: <20200226162836.GC8045@magnolia> References: <20200226020637.1065-1-cai@lca.pw> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200226020637.1065-1-cai@lca.pw> User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9543 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 mlxscore=0 suspectscore=31 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2002260112 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9543 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 lowpriorityscore=0 bulkscore=0 impostorscore=0 spamscore=0 priorityscore=1501 malwarescore=0 adultscore=0 phishscore=0 mlxlogscore=999 mlxscore=0 suspectscore=31 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2002260112 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 25, 2020 at 09:06:37PM -0500, Qian Cai wrote: > In xfs_da3_path_shift() "blk" can be assigned to state->path.blk[-1] if > state->path.active is 1 (which is a valid state) when it tries to add an > entry to a single dir leaf block and then to shift forward to see if > there's a sibling block that would be a better place to put the new > entry. This causes a UBSAN warning given negative array indices are > undefined behavior in C. In practice the warning is entirely harmless > given that "blk" is never dereferenced in this case, but it is still > better to fix up the warning and slightly improve the code. > > 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 > > Suggested-by: Christoph Hellwig > Signed-off-by: Qian Cai Looks ok, Reviewed-by: Darrick J. Wong --D > --- > > v3: Borrow the commit log from Christoph. > v2: Update the commit log thanks to Darrick. > Simplify the code. > > fs/xfs/libxfs/xfs_da_btree.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/fs/xfs/libxfs/xfs_da_btree.c b/fs/xfs/libxfs/xfs_da_btree.c > index 875e04f82541..e864c3d47f60 100644 > --- a/fs/xfs/libxfs/xfs_da_btree.c > +++ b/fs/xfs/libxfs/xfs_da_btree.c > @@ -1986,7 +1986,8 @@ xfs_da3_path_shift( > 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--) { > + for (; level >= 0; level--) { > + blk = &path->blk[level]; > xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, > blk->bp->b_addr); > > -- > 2.21.0 (Apple Git-122.2) >