Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp750299lqd; Wed, 24 Apr 2024 16:20:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU0gaQuoytXbHwTe6PT/ZW5o589XqxZZEkW8GOIr8VZ7M/Q4htANYCqJXpDOAcvqjxgcMleayxtuujo2SarOi2z3AKIPA8TkH6k8PAE3Q== X-Google-Smtp-Source: AGHT+IETst7eOzQeqyxKHa3mpOQRF9qEuqXa2zi6AQW/1aEpfpIHaFqcyQ6Rz8sYZJ1M1JyIz935 X-Received: by 2002:a17:902:ced1:b0:1e4:60d4:916b with SMTP id d17-20020a170902ced100b001e460d4916bmr4661918plg.64.1714000834348; Wed, 24 Apr 2024 16:20:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714000834; cv=pass; d=google.com; s=arc-20160816; b=Q8FuHVZJTqfNvL31IDOqOPQqlHBm06yFPXJw5QpSIyJTXQbSFOUxWj7mfB8wZ/+9uS j1jG0un0+mNki3SiDIffE2mBbb4cGmrw+KTl7GSPGfDGr/Xe9MS6m/r0I6915EuhjpGA HangI32nuLuU/QGceCjl6iPO4cYVXm8WHQ3iD2fD+u227vJGdRY37x+7jwbvNIuECmw0 QyBT9tbTCN6uzNsuCtjfHDMp0VnHT2oatjKpYFpkX54hz4TY37cawsFKv1jcNNYA6WGU AqyBRr1H6ynYY45HjWnVpRE7Wx8QaAtkWFT191XrzznJSQY2ZK7y27sDffrhdlnIVDTq mknQ== 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=53galFO5b9NlY6rYFvZBx1xxKB41abzyybNlJJlu7mM=; fh=Kb0qMYUv+XTSJg7YcBtPr1gdeHj1AZq2TqkRRfCZYrw=; b=ZYBsG0i0ZMFLGQ0Xm85bPKUWg1aAkjmUroLWJ1Clf/4s0WtXzicZps6W4fhALDnEYO vovJw/B8lVAGKTZiZTe024+9PWBQCZ88l15GeFpppT0QWjigSUhx7HAD4KrAV5c1g+Xd 2TcJiBagRHP2g3G2cKDaOBYq0HiLc9vB/96prO7PDftws0q71y4PjwBNUDIJ3y4c00Mh VbQ6GkyHdtog74G8cWS39YvfaDnv5O55FM6it06WVAbuOY7CxW++ytyBO+vvg03jhyFs aOPUcAIkJDiFPfD998ZnXTC/hQ/zyDRsD3TJXyTvRRn7V94a2UdYbGWvvQdrcOh2WLtu sLXg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YjOkppfY; 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-157834-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157834-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id i6-20020a170902c94600b001e40d171220si12142068pla.5.2024.04.24.16.20.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:20:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-157834-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YjOkppfY; 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-157834-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157834-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 006FD28680E for ; Wed, 24 Apr 2024 23:20:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 47FE816F0E8; Wed, 24 Apr 2024 23:20:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="YjOkppfY" Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 0F9AC16EC1C for ; Wed, 24 Apr 2024 23:20:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714000823; cv=none; b=jNHcHLawSAJ1pz5aLCGVV0FtbjDQq6sJMkJt+gh+0SxNzqW85N/t0Bw7oTyAQxuZ4HLlHtz+0imgwjl3qbRIzgKOl/jjTJwcKaWjgEA4yzwbrjmR2RB0Y9CXAtfva/CcCzcXO9RnUGuPYlUB470rIQP/KY6rYDSilsc5TFR0N6c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714000823; c=relaxed/simple; bh=xMdC6gPfhPx0eqt9pE8Cf6dx1C6F38ZaVJBhnbPRa2E=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=HWvKYD6ADgCg7mCbbWZav7g8rHPdcITM52L3I9W1vjHS1YJwjx7AICUDY5cynaP1bu70UiIMqdX9EU5zfVdgLzRUBDv+UJKDjQVNCDVAyAgAo77lzc7xjRMzFd8+22jCf8aWJkNLloacr6OJHJwxJygFL84+aUSAHozg2OUbAAc= 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=YjOkppfY; arc=none smtp.client-ip=209.85.214.174 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-pl1-f174.google.com with SMTP id d9443c01a7336-1eab16dcfd8so1388875ad.0 for ; Wed, 24 Apr 2024 16:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714000821; x=1714605621; 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=53galFO5b9NlY6rYFvZBx1xxKB41abzyybNlJJlu7mM=; b=YjOkppfYENLsgSowL4ZSX3/HwEPO/rWaGdecpkQt5gyw/H5ZATAG+gW+W2tC+ojKok MDqG3tof6H/HtrbSwsMYkIbwjtGjzCmmqvw2O5Sw6eFYjDC33lZoOHsxxzCcHM0elPT6 YEf1+Ieh2UUWDqca+ScK4hNpsq1jjUD6IVZak= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714000821; x=1714605621; 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=53galFO5b9NlY6rYFvZBx1xxKB41abzyybNlJJlu7mM=; b=I9P2mss24xS27YNAWsCY3ODJ5E4GPDlrf/lDR+C4L45AkUN3ADyvGXBjD3efPRFWGk 0bHVu9kFPmk4ABLWBTFFVUeIKjuV+q6Xij6wUwQThMTs/epothOelGVJBCB6bEWXBXsv 9ksc+mCrs8lmyLkDTiRyE24Vw47Syyr7azKtf2aTSzxSlpRTy8Sr1lWX/CNimMUCW2ur PhDqtL0tEfhKC9V+07uae10B1nS6AnMxzaB65BTQJirtJ1oJDTFUQZSvsXiueZMa7tbV 5jeuTPuHujEiNFFjgI4Qqtzx84HfLPJd3HDeQ+Xj3uifYf8sdXOFWDnFStpDwN/O0v/A kFfw== X-Forwarded-Encrypted: i=1; AJvYcCUssl+hSN3vJt10G+QUi6dlJUw2Db2RaghwPkaaoxYQ58moFrNDleQpKL0/zycOJdL6vRNyZmzZfd6gxGUeTu9vp94kftSPtuorDdkN X-Gm-Message-State: AOJu0YzxjGa4Kxh0fUmsWmVppKNKOwI3xDdwX7EV9NxoqoFsXLCQhqFn Hiq64vcyJlW1OB6JS6Rrozego0ERzKEUq16koDm1CqWXiPD3/sNFvyzqlhd4Kg== X-Received: by 2002:a17:902:868f:b0:1dd:2eed:52a5 with SMTP id g15-20020a170902868f00b001dd2eed52a5mr3803324plo.37.1714000821414; Wed, 24 Apr 2024 16:20:21 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id c10-20020a170902d48a00b001dd0c5d5227sm12448914plg.193.2024.04.24.16.20.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:20:20 -0700 (PDT) Date: Wed, 24 Apr 2024 16:20:20 -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: <202404241602.276D4ADA@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> 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: <20240424225436.GY40213@noisy.programming.kicks-ass.net> 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. Right, which is why this is going to take time to roll out. :) What we were really doing with -fno-strict-overflow was getting rid of undefined behavior. That was really really horrible; we don't need the compiler hallucinating. > You want to mark the non-wrapping case. What we want is lack of ambiguity. Having done these kinds of things in the kernel for a while now, I have strong evidence that we get much better results with the "fail safe" approach, but start by making it non-fatal. That way we get full coverage, but we don't melt the world for anyone that doesn't want it, and we can shake things out over a few years. For example, it has worked well for CONFIG_FORTIFY, CONFIG_UBSAN_BOUNDS, KCFI, etc. The riskier condition is having something wrap when it wasn't expected (e.g. allocations, pointer offsets, etc), so we start by defining our regular types as non-wrapping, and annotate the wrapping types (or specific calculations or functions). For signed types in particular, wrapping is overwhelmingly the uncommon case, so from a purely "how much annotations is needed" perspective, marking wrapping is also easiest. Yes, there are cases of expected wrapping, but we'll track them all down and get them marked unambiguously. One thing on the short list is atomics, so here we are. :) -Kees -- Kees Cook