From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BB54AC19F4E for ; Wed, 24 Apr 2024 23:31:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3cByyu3DKADTw+A4ebZW5v62RLzVh5v8J8lRufywjz0=; b=uT0Q7aEBHFKCcl ZgsaN6BpiyuUrJcyNfZ/SmgHHGIKWVbQsFSUMCfcdXinduvQHfp8D+t/bmH3FJlqu8CpM47XRZGRT R8qIg0sI0a0odGU6aYuqtV66H1sU+mmsW01BeVD/8V0G1jOoCMS8W32JPylPfHuNUr8XMmRHWle12 IvQS2w2t7Zq/4n//4g/aixd1tngBoFCQsC8Pq2q60pbJIpFglyh9OD1EIMnVpdKf4PQbZ0Gk5uyKG APY3o4vQqJlAy8hlgOvuh4fbDyX08HFC7kblZUbLA+8vvS00+KcMo4rQgSsQ+9zgjHG6LL8vrfJGe 4X4w4pusj+/uLc0YBfJg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzm4n-00000006MzI-16mf; Wed, 24 Apr 2024 23:30:57 +0000 Received: from mail-ot1-x32a.google.com ([2607:f8b0:4864:20::32a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzm4k-00000006MyH-25p3 for linux-arm-kernel@lists.infradead.org; Wed, 24 Apr 2024 23:30:56 +0000 Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-6eb7d1a5d39so252473a34.2 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=lists.infradead.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=DEbpYS32ivbWzBgwVaxIh51lRu0LZjBkdpJQlzFlEH+x4JkDvEZcF7hfwAOdP8xdIT mzB/0FrNkBRfNO0z0EJDO/c3Gd/agPZORd/V1UzfgxHqFWvOih60oyXtrhvKBjJKupNo HjZRuvrqMlRO3WDeBO3ireOJHNEm+QgA77vxY= 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=Y7b9T0prb/w35HFqdw8raa/6IIJ21H3VtRAC5cbeMNMjxvsTS3GTElag/Q5QKVaBGT qkHxJAKu7M/wXbmpfwf5gdMCmziYP2j1t5soPc0hcQ4EbOZPBDs1ofArpTtVsYJMtzih ayIeqcCsH06F5OoG+Z3VVn914Cu4Do7Iu9w1J7RFz9eVlX4kTJRNM6pUBLoB8yJa8/bt BePN0tPwF5d63stTO20iX2yzpIGzw2U/p5pByRvE7DnoZC0o45Pm7mDSGiT9riYR59Lv R4d6Eb/Y1JySymrs7CdxrOTIK+G1SuBgjfJOE4nr8dkhEEh9bwZNm3At0uqXj88RgX6W vXDw== X-Forwarded-Encrypted: i=1; AJvYcCUUBASuZfuaJW89dZqKPbVFwBxiEEkc/Xw3czZXrHc9RbtA3koUJjK+EHzUKpcr4VsDnND/Pf9Jl24xf0x3wdcRejBcDRK/OHioFmJNEb6LKDjeatY= X-Gm-Message-State: AOJu0Yxga/1O8MkSV0+Lr/YplUR0MIuI2GHl4pDrHfiuJIyCwgIKSF3d hstHFfana4njeR0GJ0mmo97fmOMUUWwsuU6hfgCRwOO6bXocxVPRDaIm+ILqvA== X-Google-Smtp-Source: AGHT+IG1ie+7N7gLAuYGp0jXBUvNN0VNl/qnLnqCCnwse0PJuc/S/QdeclEgnM1MgF3ysfsWuiww9g== 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> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240424230500.GG12673@noisy.programming.kicks-ass.net> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240424_163054_591520_F47D8398 X-CRM114-Status: GOOD ( 28.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel