Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp1345282lqo; Fri, 17 May 2024 22:11:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUt3MPYK4aBkoJEVG5kZ+um7dE/Xy49XbZdUT2H/pGO8e/xtQs3YXmPDp4nzMh7DoN4i6d+6Q2TJrC+8B6PT5ACIA1hyC/Rl/cB/nMP3g== X-Google-Smtp-Source: AGHT+IHcpvt5aAFk1Ii5Z3foBV1mm6IpcL2Q38jsj8UWN3YUtFR8LNZMqS0Ve+5LAeW0KuJGE6qU X-Received: by 2002:a17:902:7248:b0:1ee:2a58:cb7c with SMTP id d9443c01a7336-1ef43e26586mr229325435ad.35.1716009101304; Fri, 17 May 2024 22:11:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716009101; cv=pass; d=google.com; s=arc-20160816; b=o5wum9scDP+Jlr90ZY4HvG10Qm30mObDEUK2DI7l+4fj+NimpZeNuxBMUtMCs6oKD7 vWMoFRwiiq2lZwwm/bpldnYHVkrxelQeXjcjkSaF9g5hmOGtfv+5ySNO2mRO8VPJqNuR 1GyiR12Za5njrzCYP2e1sC+rj190sDLp9osjlv/AH+eNt1bTmuJJHqIlz9iY6X3u9L3R h+gxePQ4PJvfo/Da+6T70zqCc/jCFM6dShNIatGNp79IYMLX2FH/pS3lejYl7ba6bsBe o/Cm2VIfVvdUyIuqfXBcwIzTgYyFafzyOFjWdclngbHIwcyodBQx3mPk7NZl1WXd7K6N Fs+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=RoAGfadQ7PdVFjjVcYuXWttxVHnjeRkrlb3syjR3P+0=; fh=p7j3gGZlkuSpAyZ0p160ZQ1yvVtv25arMSH9lpVjszQ=; b=paI/krnIH7OncPki5R0wHZXW52RmeY1CRcXTHGSVyHVxgpuIlHRRFTe3sUK4VMAF6U XsaYmQyFwjsR6Utp3GGOYQCD03eV10Q4YRgkrPyDjDh6+zHr1AlT6lJ0FgeHfCS7UGdX 2hw/Fe68GlD1WtnnMS6vkIOj1wooYkOo750zdbAcSpeKvo4EuIZiYwantgfSxLkrwnJG hFJOviemT59d9myn7Q9QQvjeFw3aoo98PSGGS42IQCLFfVfl2Of4OwI3XKFu58MLo1Ob s0XZ2uUN7VuaMm9JhUEr69f5jQucsTLM9FpsXJHQJZUjIt5Id0PDPOclkB8asaqZYMmp u51Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=fJOMrUGX; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-182773-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182773-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0b9cf544si190631775ad.123.2024.05.17.22.11.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 22:11:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-182773-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=fJOMrUGX; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-182773-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182773-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BEB05282CC1 for ; Sat, 18 May 2024 05:11:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A65778C07; Sat, 18 May 2024 05:11:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="fJOMrUGX" Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FBA32F43; Sat, 18 May 2024 05:11:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716009094; cv=none; b=I/IN6osR/93fEUBYvp7mwa5Xw/S6ckpGuVuQgr9kU5YAxfhptxfyfogWLcM/XTQl1XABaNJAnc2C2epQMzFXZPWrcibErN2jpHLf5NMoflLzQ9f4i3p9gpeRVcyPjeNS9i8H/noK10D1CoOjaCqN4VMpZNpu27MknpnuHHPw+Is= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716009094; c=relaxed/simple; bh=EsXtHbU2yk5VmkzRSlicLeffzpHEB9yQz3OE5uXkSWY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iZZ/WwQAXKYUPv1ySxqDtZdsAOn7p+JqKbF9pb+Bd5oemuOPjOBW7BtDq18eRkbWoNE3ROJ7WiXIIHm5t8ss5q+pyvOlZvviiE/xpyeFvrwGCAZrePoVAGXllmsK7FF9n9AbpbtMZEXJQ504cgQHe/oeficj7B8eS6NREbnwbiI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=fJOMrUGX; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=RoAGfadQ7PdVFjjVcYuXWttxVHnjeRkrlb3syjR3P+0=; b=fJOMrUGXOjxXmyL5KL1dhgWmka EGheI4UAYl0+iPcoNaHQ43zTsjOYaHU7TVJLZwADXTXWhZk5uGgyfoTsjcjbirLkX+TGJmnP791IO 7/n7p/ajmhCgw+/V7/+g2HtBGWsxM/FL4vvG13jN6ryuIRYeObpzEihyhX9FFqQef7yp/yAE2te// nU9KcIEHlmvv3+GyvIduCMJNO7UqY24/nAgVZxk4fpcJHmmEb3qvxe0maDgG88Rb64OMz+t+PbiVd pWzHe7e/3T91gTLXiTvGWDdpVD1ulOGHmtwg7i4nAy2pHLfEIpCnVtjV2s7w2wlFvsg6OR9hirClN BMKMoKaA==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s8CLu-0000000DhSK-1QJh; Sat, 18 May 2024 05:11:26 +0000 Date: Sat, 18 May 2024 06:11:26 +0100 From: Matthew Wilcox To: Kees Cook Cc: Linus Torvalds , Justin Stitt , Peter Zijlstra , Mark Rutland , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [RFC] Mitigating unexpected arithmetic overflow Message-ID: References: <202404291502.612E0A10@keescook> <202405081144.D5FCC44A@keescook> <202405081354.B0A8194B3C@keescook> <202405081949.0565810E46@keescook> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202405081949.0565810E46@keescook> On Wed, May 08, 2024 at 11:11:35PM -0700, Kees Cook wrote: > Or even just simple math between u8s: > > while (xas->xa_offset == 255) { > xas->xa_offset = xas->xa_node->offset - 1; > > Is it expecting to wrap (truncate)? How is it distinguished from > the "num_elems++" case? Hi ;-) This looks to me like it's walking up the tree, looking to go to the 'prev' element. So yes, the wrapping from 0 to 255 is intentional, because once we find an offset that's not 0, we've set offset to the right one. Just to give more context, here's the few lines around it: if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) xas->xa_offset--; while (xas->xa_offset == 255) { xas->xa_offset = xas->xa_node->offset - 1; xas->xa_node = xa_parent(xas->xa, xas->xa_node); if (!xas->xa_node) return set_bounds(xas); } So it's kind of nasty. I don't want to write it as: while (xas->xa_offset == 0) { xas->xa_offset = xas->xa_node->offset; xas->xa_node = xa_parent(xas->xa, xas->xa_node); } xas->xa_offset--; because there's that conditional subtraction before the loop starts. The xarray test-suite is pretty good if anyone thinks they have a clearer way to write this loop ...