Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp753991lqd; Wed, 24 Apr 2024 16:31:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVUAtBz4jwEWu3pM0k7NBkrqG7pCOQbx/3MuKaKRCYPa79/MpCFsigeJQIXz7tJNKW1bLOsh6ZClBYWwtN7ENYkCL+mdz1gri5WevrTCw== X-Google-Smtp-Source: AGHT+IEE4xkmNEGIDaTNcdqwsGxPS2YcqMWi2/G5n5mEqxjAGj6Zo/Ya6OEzPE7jwwKQfLGF8w1z X-Received: by 2002:a50:9514:0:b0:56e:2a0a:c131 with SMTP id u20-20020a509514000000b0056e2a0ac131mr2881602eda.17.1714001469845; Wed, 24 Apr 2024 16:31:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714001469; cv=pass; d=google.com; s=arc-20160816; b=HUoql8af38tH6K7+dPfMKso2JbFbdybI1hk1hanumEdWl3Q4nAdixrhJf9RglexEoI amBfZJfWPKttTzb0AucxEyCILlpbcdjlpmiz/Du49RwirXbT0dsOOLmqr56Bt7wNpeNj 4RmKfbeuEqOs08unygVp1JbFote3uYAsM9iRP+T6AntBu2hY1/d7ByPsCmvGVRHnDimp +cfBPK3cUD7nwr+cXhRGCGa2cIGPZQRbjNFKdt5yW7MUjzZUuW8N/He+w1vUsKsYrNEM bG6D+A4a285P/YyFmEwCPrGjdEsMvLAWNRfkc5mQ+/WZ3lyf4XwiMf+zSaUywfiuGKvA KlwA== 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=pwtnqARf0+6Z9GG1dv72c6Df9XT02q1HeJoWwzmw6v8=; fh=XgBwkoM53X98IM3SVmIbKUfnKbPoOaa0r/AkSfv3YLg=; b=K+pvBZttNKsTFjhOu0meQuqETCvMNbgqWikM7yIj6RPU73af7c4S60laG7j9HLnFgV IHDFpWwvqEAHW+I497fDdQS/Aag5F4qSCOgk1IWOuaedwHPvJmIYhROvoK1VH2mUY+HR B1QCCWoXPD9WCQPnq+zgQtc6hX+mH3lBT1S7M8xBLUZu1Y1ZLGxIAxU5doD1047eOFhJ R4dqvarKSAmJFdf6LMX4YdzAuFcjfK2pm+MGhG3FMCCEfTydy5+57ufLNh7YwCGFfQ9+ oa6sxY57aGE8w6Lsn0pCUeGix0aJwCOxbysy+yP1v3xlFKlZDTWVia+dqxRlNqJ7Y0Ln 440A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PuaOvteq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-157841-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157841-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id m9-20020a056402050900b0057215cc4d46si3252384edv.413.2024.04.24.16.31.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:31:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-157841-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PuaOvteq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-157841-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157841-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 6723E1F22F7B for ; Wed, 24 Apr 2024 23:31:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4396F16F260; Wed, 24 Apr 2024 23:30:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="PuaOvteq" Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A586016EBEF for ; Wed, 24 Apr 2024 23:30:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714001455; cv=none; b=YSw5Xf4zewMMg9xFLDL0+nmsQhLRLS5FXANE6i7nxVKKp24LB2BCLs/T6OV5RSgQoDKtBWv/opQylTcFeWAixbqKaRRBdOcnyB7Ni0AT87eMi2mSwIINdsrtpfLXEMUa3g3ThR1m9fDXgY2ondn0Pke0W2matq+iiy8jgU9d3vs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714001455; c=relaxed/simple; bh=oH09D5zkFXRFNfU7QJiNFf+gIh+lrRtBdFHPc4eg0Ho=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WCSWHTVJ7LlQ3wuZLsR+1EC9LNJpkSQzHtBX9NtCRE48Jue1DWCcE7D2cIRcee5OuVnH7mGvNmT79maeh8wJpwFurpm81j/DFCcNXM2gxf3SIedQO+oDwFId3IP0+YEJVr847Yp8VLMlsUtOAYx5bXT5bKZRXe/IzgSBYUrIzgg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=PuaOvteq; arc=none smtp.client-ip=209.85.210.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-6ebc92079b5so280380a34.0 for ; Wed, 24 Apr 2024 16:30:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714001452; x=1714606252; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=pwtnqARf0+6Z9GG1dv72c6Df9XT02q1HeJoWwzmw6v8=; b=PuaOvteq0mMRgO3Jlb2dgmiCdAPFB3Btc8zIHX74LLL3fcOxKUNDdejxB4uqBY5JPG gzeSqc5Q6og7ag+mA85KLwOLD2cgygAFMqFSk4UJczXMpWloJJYfcMV9Dq0DpY7UpbE3 IpKBWIDyFj1vZDAhupVMhZaYLMQxGDePYUc5Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714001452; x=1714606252; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pwtnqARf0+6Z9GG1dv72c6Df9XT02q1HeJoWwzmw6v8=; b=HYUy/qM4wnBIm2lBx90ijijL3LB3bpZTn6pFXBLMHNhruCcT0XI3mE6YSndtmh53Xw qXOvWpQCBsONFhhvjeQ9GDq1Wy37JGOIc7Ggu0q8DRNSH32QawddSgHiWcGIttxJWqAR i0lnvC58Or6OC7b+sq1qSIBa99+C3KmK6bzwqU25Mtnokp3haWj5NWdaxfjnOCB9flHn sFNS1knqQtlkESMsSQ9ffiA0StZR6FVJoWEa5lvkvQkjWPyq6czX/4XRJ07w3eQf4PMo bheV/r5hhm5Yvj6Jap9lvcfO6dJs7pGP2M8kO0paXXH/Le14tPZ8WC1twKkDFckRZAa/ Grtw== X-Forwarded-Encrypted: i=1; AJvYcCXOjKp89Qbr+FJnRQ5xC2Ynxf2SsQwQn7yMagTlkaOYtJq36KUwbfAxCjCYnESnLmezc3di0NejuCmqw5qjaJSINA13Jb2bDJxhZR5T X-Gm-Message-State: AOJu0YwfFXonToGAgyp+1wPjE/kKphButQFcEtHriXfZNPc8qubVsqBT braJKTG4FvV8jDTNIzoF9ZXoha4bdB1wwe79BiVGdgQdTKCB6E2OPT7WQNYrcQ== X-Received: by 2002:a05:6871:7410:b0:233:56e5:ff99 with SMTP id nw16-20020a056871741000b0023356e5ff99mr4180313oac.23.1714001451762; Wed, 24 Apr 2024 16:30:51 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id k73-20020a636f4c000000b005df41b00ee9sm600140pgc.68.2024.04.24.16.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:30:51 -0700 (PDT) Date: Wed, 24 Apr 2024 16:30:50 -0700 From: Kees Cook To: Peter Zijlstra Cc: Mark Rutland , Will Deacon , Boqun Feng , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Jakub Kicinski , Catalin Marinas , Arnd Bergmann , Andrew Morton , "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , "Paul E. McKenney" , Uros Bizjak , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH 1/4] locking/atomic/x86: Silence intentional wrapping addition Message-ID: <202404241621.8286B8A@keescook> References: <20240424191225.work.780-kees@kernel.org> <20240424191740.3088894-1-keescook@chromium.org> <20240424224141.GX40213@noisy.programming.kicks-ass.net> <202404241542.6AFC3042C1@keescook> <20240424225436.GY40213@noisy.programming.kicks-ass.net> <20240424230500.GG12673@noisy.programming.kicks-ass.net> 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: <20240424230500.GG12673@noisy.programming.kicks-ass.net> On Thu, Apr 25, 2024 at 01:05:00AM +0200, Peter Zijlstra wrote: > On Thu, Apr 25, 2024 at 12:54:36AM +0200, Peter Zijlstra wrote: > > On Wed, Apr 24, 2024 at 03:45:07PM -0700, Kees Cook wrote: > > > On Thu, Apr 25, 2024 at 12:41:41AM +0200, Peter Zijlstra wrote: > > > > On Wed, Apr 24, 2024 at 12:17:34PM -0700, Kees Cook wrote: > > > > > > > > > @@ -82,7 +83,7 @@ static __always_inline bool arch_atomic_add_negative(int i, atomic_t *v) > > > > > > > > > > static __always_inline int arch_atomic_add_return(int i, atomic_t *v) > > > > > { > > > > > - return i + xadd(&v->counter, i); > > > > > + return wrapping_add(int, i, xadd(&v->counter, i)); > > > > > } > > > > > #define arch_atomic_add_return arch_atomic_add_return > > > > > > > > this is going to get old *real* quick :-/ > > > > > > > > This must be the ugliest possible way to annotate all this, and then > > > > litter the kernel with all this... urgh. > > > > > > I'm expecting to have explicit wrapping type annotations soon[1], but for > > > the atomics, it's kind of a wash on how intrusive the annotations get. I > > > had originally wanted to mark the function (as I did in other cases) > > > rather than using the helper, but Mark preferred it this way. I'm happy > > > to do whatever! :) > > > > > > -Kees > > > > > > [1] https://github.com/llvm/llvm-project/pull/86618 > > > > This is arse-about-face. Signed stuff wraps per -fno-strict-overflow. > > We've been writing code for years under that assumption. > > > > You want to mark the non-wrapping case. > > That is, anything that actively warns about signed overflow when build > with -fno-strict-overflow is a bug. If you want this warning you have to > explicitly mark things. This is confusing UB with "overflow detection". We're doing the latter. > Signed overflow is not UB, is not a bug. > > Now, it might be unexpected in some places, but fundamentally we run on > 2s complement and expect 2s complement. If you want more, mark it so. Regular C never provided us with enough choice in types to be able to select the overflow resolution strategy. :( So we're stuck mixing expectations into our types. (One early defense you were involved in touched on this too: refcount_t uses a saturating overflow strategy, as that works best for how it gets used.) Regardless, yes, someone intent on wrapping gets their expected 2s complement results, but in the cases were a few values started collecting in some dark corner of protocol handling, having a calculation wrap around is at best a behavioral bug and at worst a total system compromise. Wrapping is the uncommon case here, so we mark those. -- Kees Cook