From patchwork Wed Apr 24 19:17:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13642440 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 AF4C92D05E 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="nau83yEZ" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1e3e84a302eso1567485ad.0 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=LhicPPUhFqux9caLLbuyIv+J8H7FEtSqzGxqCGgks5qI7NWpPe8PZxFKYuPLCXIEAq ndRQdi4waZsV2kmNBvB03H5S6Z30/uHC7ddniRt8wIeHAKceSUFeyK69sch36pFVJ691 VpMtL4PlM8tJciE/EOMD611vsMMD+9aGcz58ubssEHEXeX3wS4E7M45hPoB7a8uEUHWu hluy0L+xm+JA3sgFepUn9hQoWK1tjh8hWu8ydSc/FpnhXIKwykHyXKJ3IdnqVFj2cEDb HtwFiEXankKs+PnIOa1Pk/LC4bFfxzEySH1X8BfKi/IV2MYxpY7Qwr+UC8uLM3+xiEzd Rf7g== X-Forwarded-Encrypted: i=1; AJvYcCXtu9YbTZkD833YSA+JG8YWc1xX3wBUyMY6Su4w+Y/fJ1GzbVu1GBlATajtgFYt2yY7PHcR7Cjm5q0NlFGk7qXSEmchf6M+irEOYkTxx5aS X-Gm-Message-State: AOJu0YxELNT+Vg0MbUeoMzVMzYPPcF/xUIw4vjZP+Vlkr5+644WyNprw qLig7si8BRyWVRC/KGU/pwOHw8/R7AyB6RGxBCKwHOHKQdqu8zXxrs1bJ7iENw== X-Google-Smtp-Source: AGHT+IHRQ9hlNd8Q/9S+swXrlzFZSC66Zi7MONSBZn2dTrR2Cr0f8ozzcVQ3tJLvlRKVSkey9HjOwQ== 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-hardening@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 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