Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp641211lqd; Wed, 24 Apr 2024 12:18:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWk8kg4TuVwb7qaBwnCutbS/YkoatJ1gERw9/f284V96ZYc/DNaWkOGMZwgieO3F0DButLsjgV2XYgN80ndXH8nnv3Hg7zTT2qjiiOfeg== X-Google-Smtp-Source: AGHT+IHGtxw+7V9K8NuZWlEgslKP9PK+zVCmyQxxOPUxR+0SeDJ050wXs4dEIxh1iHcer69VmDTn X-Received: by 2002:a17:902:ce09:b0:1dd:81a3:8dc3 with SMTP id k9-20020a170902ce0900b001dd81a38dc3mr3801759plg.46.1713986289421; Wed, 24 Apr 2024 12:18:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713986289; cv=pass; d=google.com; s=arc-20160816; b=0fd0GUBjT6yzk+iulFnbrntI2avrMYAT1t26AtK+v333V7S82PoT0zy29+YXyjvbDi IH9OqQ81912RrJcWxyJqXc8gdGA3wXC1a7AExLRmBPifb4ac/pjENlcDCFB9A90+BkFn CX8tjeRl7BCSXN/PlqQVzJs+qEfmauGR7zdnUmoCwx9PvTqtNDQjjigArPkhCRl6ayXX c/SxS5yQRPOeY4Tho4rqWXwBEgweUBGF97DAVVGW/WrY0Z3Tdg5cQURAS1XRRmER1BXu KpCrF2LhiyXXpZS8rk7I4uycpwVgvlyAlF3oJxztUmEXlgIwe36JjsJw7JsFO6/fRw2F tQbQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=vo2K8jVQRi43xxtGN8MvYubFuFXJHBPNUxXy1Vyz154=; fh=iQ6KYQPzAjWMSH27OS91KsKQUgN8yO4F7Qp0kzyvgLY=; b=kKEOdUYSpUvVybsQUHOTWsudVqZEr6NjHicedKorZjgqc2jxnGSS+ASJ9Gz6yHiU23 YKyQvHjQtBcPrP5osTlbQ/F9VYjReSATrvr8kBoPTpVrD8s/Qj1kRi6xghsin7Un1uS4 6rRcIWDkiSiLwOLSq7S2fbLyR/9QSU2/PdS7F4tPaAxkd5O5Abp5+yiZU0eMq9UOO83j rCDMcPl0V4RdBF8GfBMyosbB66pBiFff74Xn84j6b3+BWk7fORP81AH6yK68Eifoufra UQmaTwcUW/cApwmxW5AjYsyAFnBxrytzdNCs8tn3rkSLIwFlqjRsHiMDuT31eTxsRqmB TEzQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nau83yEZ; 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-157597-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157597-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. [139.178.88.99]) by mx.google.com with ESMTPS id t7-20020a17090340c700b001e0892bb867si11668369pld.0.2024.04.24.12.18.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 12:18:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-157597-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=@chromium.org header.s=google header.b=nau83yEZ; 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-157597-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157597-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 038862844FB for ; Wed, 24 Apr 2024 19:18:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C43367EEE7; Wed, 24 Apr 2024 19:17:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="nau83yEZ" Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 AF4912BAEA for ; Wed, 24 Apr 2024 19:17:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986263; cv=none; b=PaDlPOZyj/dTNiIZSt4lmqNIyfCg2PoJFsHuPSwANEpmFEV7zZTfyFV2NhExhg1uPVxYM8bo5fML5swH6CTptR4Ky1C+tJJUhvMkc/UIGTojwJy75SrgLkX7oOovPoK7XVNB4pOVH1phLKasYDXxgIVIA8vwkAoNEgyR7cEjeuU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986263; c=relaxed/simple; bh=FYeUwjmTkimRdEar0nMCEB+zeOzWgwm4wTKMgd4cZTs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iyXPThqzzHGMI0+mKTOHPycqn5WTwCZi/lTVMYw9blox5DiyMOOjaDe15T7g/MKbeMfe67qbIOSJvoyrePNXWRLbd4CXxnOv5aDGd/YuDoM2mYPGzkJmH1B8dYggcj5748WoLKqIkzSKLHYO8bTJ69aiy1DoVr00n6HXTs+bTCY= 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=nau83yEZ; arc=none smtp.client-ip=209.85.214.175 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-f175.google.com with SMTP id d9443c01a7336-1e83a2a4f2cso1279505ad.1 for ; Wed, 24 Apr 2024 12:17:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1713986261; x=1714591061; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vo2K8jVQRi43xxtGN8MvYubFuFXJHBPNUxXy1Vyz154=; b=nau83yEZNXyYb15/5p9shTigu/gxX+KDznIIqt5wX0MFYqK2fWZisbPTxy+lftlnP8 0rq0EvC0g+l4WSNfI73VJJiHgCtu5IlWYMGiPA5QM/BK9Um16cv1d6PKsnUXDekNu9ko yR1ho75BwN2n3UcKSEMuYtbQBnjKXtG7ex7/c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713986261; x=1714591061; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vo2K8jVQRi43xxtGN8MvYubFuFXJHBPNUxXy1Vyz154=; b=M8+WJSRUz8qw8G8QPWmc/sXEsaPJlrbU3iGqTqi6hv6Q8hGL4JpgPyBK5i4SYNJ4Sx P3C+lqABpV7ZeTdIiJ3YzopBlRjbKUJl926fArVdtOXNreo5MDyA6UGdbMlglU0mQHJP SlEBJ8KXCd1K2C57hLBewqNuzgroTIk7b8pqvtDV5Th5u1sIjunOsuQ0HtukS/Uj+C9c ixQodDxftIgbinGamBq4VyAYz27DxoQzNlByZmnj1YZoKjuFd5UFoNokSZmkxXBnphx7 q+hPG6znv7WlE8OFtvP5sM688dNBsQqSNC18aMlofyPb3RtgjnybZY5R+MnrasiQ/SGu Obtg== X-Forwarded-Encrypted: i=1; AJvYcCUuqU9tYsCIIbkz7TBMkqnwVJHR1yo1yEm0EVZG7Ko88yg6iIeQYYNJPR/2uqMR+VlCOlxzxxpSlzarelsjG3565sk5DfXHI99/k2HW X-Gm-Message-State: AOJu0Yxwz7FwAI2UtZBAx0ZEveCX8hqVSf1mBTFULB48USMr7cWxhF0A lC2Dsr7KYNnCa2WJmLdUskdxXmM+dxaehDW7NW5JQb176W14bP3xiCpC+IDNmg== X-Received: by 2002:a17:902:9a0a:b0:1e2:88c9:6c08 with SMTP id v10-20020a1709029a0a00b001e288c96c08mr3007206plp.49.1713986261101; Wed, 24 Apr 2024 12:17:41 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id d10-20020a170903230a00b001e8b81de172sm10657799plh.262.2024.04.24.12.17.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 12:17:40 -0700 (PDT) From: Kees Cook To: Mark Rutland Cc: Kees Cook , Will Deacon , Peter Zijlstra , 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: [PATCH 1/4] locking/atomic/x86: Silence intentional wrapping addition Date: Wed, 24 Apr 2024 12:17:34 -0700 Message-Id: <20240424191740.3088894-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240424191225.work.780-kees@kernel.org> References: <20240424191225.work.780-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2485; i=keescook@chromium.org; h=from:subject; bh=FYeUwjmTkimRdEar0nMCEB+zeOzWgwm4wTKMgd4cZTs=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmKVrROPsWV+7wAsNj0gHWR6QvLgSLyEzC1bVpp NVRrZ63HliJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZila0QAKCRCJcvTf3G3A JkgzD/483ESp9GunXhngEBu3wqcJ70FbQue3kCmbpZPgaJNUscH+iguBQUrzVLdn7cu87nT79UE 3xRAFrXdq5GSxPYCb92aPEvd8aADFJvKoDecGHPMGvZZr8kounYftdQ8SEpVM9oCDOjPyjA+KbY MeySHGPTKhpplullPe8MqxiQHrtIoquHhMmPCgei09S6htYIiB8sqQA5TU0xWKlYx7WFL1vnzPI 5T2ndbMr6XQDFTlro5lIHHCPD4sXQQzkXxTcIAj3bNuGLnaKaJK1VAjG73ZDSlATjlSEkI4gwTe ONZaFwDrJBOJAk3CSrkxoK/DvAba4x+V0xqqUTX2v6iKZ5c3A5pcNuWX8si2iEPTgtyF7A4U+3H rEmjb2JKW+/zvKMc06jW2biYhQFLBOSCK8Cglq39Fg0nVhw4bLokjyM+9dneyphXqyV1k/FGfkY tbOSZtbZ6FOSnFbEmmELsvsZce6ReBSibvd6VPR347wmA7fNATxiOxhZULH5Kt04r84/4bJx3SL APbwpN8bVPbPRu9wywmI87MrMFEYOTXQqp2gJjba4fn0y5aGIWmrlroPuF68mNwDWGHb8AlHI8F Aycb/DYtsP8itByLJZTj9bWXlZJf/UxERnyLFLxuJGIETgsD3jCajtE/J8bdow6dwZZ0NAJKlw6 npZGDZtqE/FCMNg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit Use add_wrap() to annotate the addition in atomic_add_return() as expecting to wrap around. Signed-off-by: Kees Cook --- Cc: Will Deacon Cc: Peter Zijlstra Cc: Boqun Feng Cc: Mark Rutland Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" --- arch/x86/include/asm/atomic.h | 3 ++- arch/x86/include/asm/atomic64_32.h | 2 +- arch/x86/include/asm/atomic64_64.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/atomic.h b/arch/x86/include/asm/atomic.h index 55a55ec04350..a5862a258760 100644 --- a/arch/x86/include/asm/atomic.h +++ b/arch/x86/include/asm/atomic.h @@ -3,6 +3,7 @@ #define _ASM_X86_ATOMIC_H #include +#include #include #include #include @@ -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 diff --git a/arch/x86/include/asm/atomic64_32.h b/arch/x86/include/asm/atomic64_32.h index 3486d91b8595..608b100e8ffe 100644 --- a/arch/x86/include/asm/atomic64_32.h +++ b/arch/x86/include/asm/atomic64_32.h @@ -254,7 +254,7 @@ static __always_inline s64 arch_atomic64_fetch_add(s64 i, atomic64_t *v) { s64 old, c = 0; - while ((old = arch_atomic64_cmpxchg(v, c, c + i)) != c) + while ((old = arch_atomic64_cmpxchg(v, c, wrapping_add(s64, c, i))) != c) c = old; return old; diff --git a/arch/x86/include/asm/atomic64_64.h b/arch/x86/include/asm/atomic64_64.h index 3165c0feedf7..f1dc8aa54b52 100644 --- a/arch/x86/include/asm/atomic64_64.h +++ b/arch/x86/include/asm/atomic64_64.h @@ -76,7 +76,7 @@ static __always_inline bool arch_atomic64_add_negative(s64 i, atomic64_t *v) static __always_inline s64 arch_atomic64_add_return(s64 i, atomic64_t *v) { - return i + xadd(&v->counter, i); + return wrapping_add(s64, i, xadd(&v->counter, i)); } #define arch_atomic64_add_return arch_atomic64_add_return -- 2.34.1