From patchwork Wed Apr 24 19:17:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13642449 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 597BBC4345F for ; Wed, 24 Apr 2024 19:18:19 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=kCFxf2aYKVsh9KJ7Me8A+ITreVsXCIEJoDei/Ux7Ot0=; b=aBAmZ+2xQWS/cc 8IVRmp4S5qBinPuFq/QpKhW6PUs8/f5GqLSpvuhPcDeHyIKBwIX3V0b/RCDynlrvzKL97C1STzoEx TdGm5rSWmGUq3SVE3lbw7Z/MGtPkrnYYW3RQwRyxu+eL6HeGWc5BDMMHt4PidJkpdG+UXOmGuZthq AWnpx0CYqn+uaYOONJs4ZlsEk+lg0EcOHCiihVKcm7vhP0jwbyXjMebZKUfTt4ueO+X/1RB906wyq +gsWzzdcEEbRvB5hMWBNLaFwiEgXMZOXni1ncp7CMPZOYIcQeyRTwVAqpTnsxYs9S9df/Et17A+Xq SFrh4wJ1auRIXMQdSB3A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi83-00000005cWd-2aM0; Wed, 24 Apr 2024 19:18:03 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi7o-00000005cLN-0HXW for linux-arm-kernel@lists.infradead.org; Wed, 24 Apr 2024 19:17:53 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1e3f17c6491so1470725ad.2 for ; Wed, 24 Apr 2024 12:17:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1713986263; x=1714591063; darn=lists.infradead.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=YEPqPttQfQibEvJLf164NSrzanGE7tc+dv4CafbEPd0=; b=MTTTnF1ZXx9iH4IP8SAVO7LutU6lnxooBc7BIfXbTnI46/Ht2H6QcsplA9BYHrYDfV lEYFHNqhckHJChA/sEQUcAJhrxjiRsboZW3PAc0MHume3LXYK1DLBzPCW83Hew5fClpd J7zMjsc98fJGi/qvx3cCHNMAubu3OCOhzH5DI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713986263; x=1714591063; 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=YEPqPttQfQibEvJLf164NSrzanGE7tc+dv4CafbEPd0=; b=cv75OqFG+7IdqXeOKfSOjAdtcNxI/aH/MfnNm69H/Hkt4nKbaFxiQyBdXVsfObMEdd sSC7u3oJb2LP8hhKM+qwpmf/Wx3r3+v8YZrOOeD6lDd56TYHxdBhG5KsVQYYmnjlI4Dw 5upCkJZCf2oS8FrZ8vqYC0RzvWKquz/i9EPvGt33mWhY1LUtdkVpLJTfDghS4vByNzgL i22FN+rGcmhrOWnvu/3Qm7XYDoGcnaNURNC4WZw/mo4S1CCngo/DgcvEpIDGqwb58+7T giQYmU1ydUhIYjwC3QtufbX6I4kUR9DSXGfHJZqe0DMCNo0g9VXOWlRndRf0NljhSCF0 tnIA== X-Forwarded-Encrypted: i=1; AJvYcCXhfKvotgUrH9v++AZZdopEWN7nA3dGZWqPTSFvs/CdXLiw27gFvcOd7aeuGGiGRero+LB8mtKfpFA5VQM3kliV7tf5bFUfSRoKHrSZX36pwuFWjzc= X-Gm-Message-State: AOJu0YzFST7MWyUkvUP3RezIZl5mjnN/+4H3zpmLfq+ile/pIcFPiK6V +PRyfAtXaseLK33c7v2FqcMZtcf9K7BWI4eDDi34MR5itEkPy0YetaxOKZaNQw== X-Google-Smtp-Source: AGHT+IG8y4hy7B6G3NmN+Im4mCezDRd9ywIoXz+NBZ5qvURqQRP/1I3KkB7n9jrCh4TwWAmxPr8I5Q== X-Received: by 2002:a17:903:11c8:b0:1e0:bacc:9977 with SMTP id q8-20020a17090311c800b001e0bacc9977mr4180689plh.59.1713986262829; Wed, 24 Apr 2024 12:17:42 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id w9-20020a1709026f0900b001e20be11688sm12487228plk.229.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 , Arnd Bergmann , Andrew Morton , linux-arch@vger.kernel.org, Jakub Kicinski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Catalin Marinas , "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , "Paul E. McKenney" , Uros Bizjak , linux-kernel@vger.kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 3/4] locking/atomic: Annotate generic atomics with wrapping Date: Wed, 24 Apr 2024 12:17:36 -0700 Message-Id: <20240424191740.3088894-3-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> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=12519; i=keescook@chromium.org; h=from:subject; bh=p+vioqJHMrVAxi1bLzx7AQ2vntcfu+EHzlTtU17Neoc=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmKVrRf2E4KMRiQ6V7SM0Vpu4063D54ihXAdSW1 fcu1lTXPGOJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZila0QAKCRCJcvTf3G3A JilIEACBFQIUJ2T51sviwA0k1wadwtR9Gb4WZamGcp9CqDySqaD1udUvFGRL1AULMABYirtFv0B +cyM6s8NSC83+hyRH+eUPvBXSUGSYhWNyebqJ8C3hD6WUAJo97b6AJAj5TcWRePFwg+KQl86uQF XOPdxxta6YmHtAZdwdT85XbTnUgfMoNwLhJz2KTXT6cQN0J9n713TUbFA+g2ReAcSPpHd78n3ih iIlaMyuQLBwWbqixUMSFrSuzC4xnm5w8oboBhMlWzUH7F2CStJnEJRCkcp90e9Dbzu/xwDFfp7J jql+lkTa358V9qUgeeJllBz0PzhcfpGLGwbqYRdv7JFTM2OYNhpjIvL92+IhXvtE5b/wGW/nM0u 2x1R2RsYX/KKWU/rFZafAkgnZzjAlgEoiSSav9u3DPGBqeAmIY7da+HeASXcgUiBnvAR1eRqvjG QCA0Eboui0AzuTFvwgeuB9ljrz4Viu0wSN9sorlx+nvSbdmtB7GMAcG+1mhHtak21LoYf4d3LKi Rkpyx3GIfgvjA9Nu+e3v1s20oGEd7hwh5+tzJQvc6z9lpsEPt+8Ia4dhDGYmEm/mAw/7g/1ORVi +5NWw+Vci85gZpYHzTWJ919eW8XZZfN0rqTx7B2O3f/0cJbUUpVNkBsD6CR/exm6WYMx+QGwpfA QiKOHEwFSuvLsGg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240424_121748_291748_F976FE0C X-CRM114-Status: GOOD ( 21.39 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Because atomics depend on signed wrap-around, we need to use helpers to perform the operations so that it is not instrumented by the signed wrap-around sanitizer. Refresh generated files by running scripts/atomic/gen-atomics.sh. Signed-off-by: Kees Cook --- Cc: Mark Rutland Cc: Will Deacon Cc: Peter Zijlstra Cc: Boqun Feng Cc: Arnd Bergmann Cc: Andrew Morton Cc: linux-arch@vger.kernel.org --- include/asm-generic/atomic.h | 6 +++--- include/asm-generic/atomic64.h | 6 +++--- include/linux/atomic/atomic-arch-fallback.h | 19 ++++++++++--------- include/linux/atomic/atomic-instrumented.h | 3 ++- include/linux/atomic/atomic-long.h | 3 ++- lib/atomic64.c | 10 +++++----- scripts/atomic/fallbacks/dec_if_positive | 2 +- scripts/atomic/fallbacks/dec_unless_positive | 2 +- scripts/atomic/fallbacks/fetch_add_unless | 2 +- scripts/atomic/fallbacks/inc_unless_negative | 2 +- scripts/atomic/gen-atomic-fallback.sh | 1 + scripts/atomic/gen-atomic-instrumented.sh | 1 + scripts/atomic/gen-atomic-long.sh | 1 + 13 files changed, 32 insertions(+), 26 deletions(-) diff --git a/include/asm-generic/atomic.h b/include/asm-generic/atomic.h index 22142c71d35a..1b54e9b1cd02 100644 --- a/include/asm-generic/atomic.h +++ b/include/asm-generic/atomic.h @@ -55,7 +55,7 @@ static inline int generic_atomic_fetch_##op(int i, atomic_t *v) \ #include #define ATOMIC_OP(op, c_op) \ -static inline void generic_atomic_##op(int i, atomic_t *v) \ +static inline void __signed_wrap generic_atomic_##op(int i, atomic_t *v)\ { \ unsigned long flags; \ \ @@ -65,7 +65,7 @@ static inline void generic_atomic_##op(int i, atomic_t *v) \ } #define ATOMIC_OP_RETURN(op, c_op) \ -static inline int generic_atomic_##op##_return(int i, atomic_t *v) \ +static inline int __signed_wrap generic_atomic_##op##_return(int i, atomic_t *v)\ { \ unsigned long flags; \ int ret; \ @@ -78,7 +78,7 @@ static inline int generic_atomic_##op##_return(int i, atomic_t *v) \ } #define ATOMIC_FETCH_OP(op, c_op) \ -static inline int generic_atomic_fetch_##op(int i, atomic_t *v) \ +static inline int __signed_wrap generic_atomic_fetch_##op(int i, atomic_t *v)\ { \ unsigned long flags; \ int ret; \ diff --git a/include/asm-generic/atomic64.h b/include/asm-generic/atomic64.h index 100d24b02e52..0084867fe399 100644 --- a/include/asm-generic/atomic64.h +++ b/include/asm-generic/atomic64.h @@ -19,13 +19,13 @@ extern s64 generic_atomic64_read(const atomic64_t *v); extern void generic_atomic64_set(atomic64_t *v, s64 i); #define ATOMIC64_OP(op) \ -extern void generic_atomic64_##op(s64 a, atomic64_t *v); +extern void __signed_wrap generic_atomic64_##op(s64 a, atomic64_t *v); #define ATOMIC64_OP_RETURN(op) \ -extern s64 generic_atomic64_##op##_return(s64 a, atomic64_t *v); +extern s64 __signed_wrap generic_atomic64_##op##_return(s64 a, atomic64_t *v); #define ATOMIC64_FETCH_OP(op) \ -extern s64 generic_atomic64_fetch_##op(s64 a, atomic64_t *v); +extern s64 __signed_wrap generic_atomic64_fetch_##op(s64 a, atomic64_t *v); #define ATOMIC64_OPS(op) ATOMIC64_OP(op) ATOMIC64_OP_RETURN(op) ATOMIC64_FETCH_OP(op) diff --git a/include/linux/atomic/atomic-arch-fallback.h b/include/linux/atomic/atomic-arch-fallback.h index 956bcba5dbf2..2d2ebb4e0f8f 100644 --- a/include/linux/atomic/atomic-arch-fallback.h +++ b/include/linux/atomic/atomic-arch-fallback.h @@ -7,6 +7,7 @@ #define _LINUX_ATOMIC_FALLBACK_H #include +#include #if defined(arch_xchg) #define raw_xchg arch_xchg @@ -2428,7 +2429,7 @@ raw_atomic_fetch_add_unless(atomic_t *v, int a, int u) do { if (unlikely(c == u)) break; - } while (!raw_atomic_try_cmpxchg(v, &c, c + a)); + } while (!raw_atomic_try_cmpxchg(v, &c, wrapping_add(int, c, a))); return c; #endif @@ -2500,7 +2501,7 @@ raw_atomic_inc_unless_negative(atomic_t *v) do { if (unlikely(c < 0)) return false; - } while (!raw_atomic_try_cmpxchg(v, &c, c + 1)); + } while (!raw_atomic_try_cmpxchg(v, &c, wrapping_add(int, c, 1))); return true; #endif @@ -2528,7 +2529,7 @@ raw_atomic_dec_unless_positive(atomic_t *v) do { if (unlikely(c > 0)) return false; - } while (!raw_atomic_try_cmpxchg(v, &c, c - 1)); + } while (!raw_atomic_try_cmpxchg(v, &c, wrapping_sub(int, c, 1))); return true; #endif @@ -2554,7 +2555,7 @@ raw_atomic_dec_if_positive(atomic_t *v) int dec, c = raw_atomic_read(v); do { - dec = c - 1; + dec = wrapping_sub(int, c, 1); if (unlikely(dec < 0)) break; } while (!raw_atomic_try_cmpxchg(v, &c, dec)); @@ -4554,7 +4555,7 @@ raw_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u) do { if (unlikely(c == u)) break; - } while (!raw_atomic64_try_cmpxchg(v, &c, c + a)); + } while (!raw_atomic64_try_cmpxchg(v, &c, wrapping_add(s64, c, a))); return c; #endif @@ -4626,7 +4627,7 @@ raw_atomic64_inc_unless_negative(atomic64_t *v) do { if (unlikely(c < 0)) return false; - } while (!raw_atomic64_try_cmpxchg(v, &c, c + 1)); + } while (!raw_atomic64_try_cmpxchg(v, &c, wrapping_add(s64, c, 1))); return true; #endif @@ -4654,7 +4655,7 @@ raw_atomic64_dec_unless_positive(atomic64_t *v) do { if (unlikely(c > 0)) return false; - } while (!raw_atomic64_try_cmpxchg(v, &c, c - 1)); + } while (!raw_atomic64_try_cmpxchg(v, &c, wrapping_sub(s64, c, 1))); return true; #endif @@ -4680,7 +4681,7 @@ raw_atomic64_dec_if_positive(atomic64_t *v) s64 dec, c = raw_atomic64_read(v); do { - dec = c - 1; + dec = wrapping_sub(s64, c, 1); if (unlikely(dec < 0)) break; } while (!raw_atomic64_try_cmpxchg(v, &c, dec)); @@ -4690,4 +4691,4 @@ raw_atomic64_dec_if_positive(atomic64_t *v) } #endif /* _LINUX_ATOMIC_FALLBACK_H */ -// 14850c0b0db20c62fdc78ccd1d42b98b88d76331 +// 1278e3a674d0a36c2f0eb9f5fd0ddfcbf3690406 diff --git a/include/linux/atomic/atomic-instrumented.h b/include/linux/atomic/atomic-instrumented.h index debd487fe971..af103189bd7d 100644 --- a/include/linux/atomic/atomic-instrumented.h +++ b/include/linux/atomic/atomic-instrumented.h @@ -15,6 +15,7 @@ #include #include #include +#include /** * atomic_read() - atomic load with relaxed ordering @@ -5050,4 +5051,4 @@ atomic_long_dec_if_positive(atomic_long_t *v) #endif /* _LINUX_ATOMIC_INSTRUMENTED_H */ -// ce5b65e0f1f8a276268b667194581d24bed219d4 +// b9cd8314e11c4c818fb469dbd18c7390fcaf9b3c diff --git a/include/linux/atomic/atomic-long.h b/include/linux/atomic/atomic-long.h index 3ef844b3ab8a..07c1625a2d92 100644 --- a/include/linux/atomic/atomic-long.h +++ b/include/linux/atomic/atomic-long.h @@ -7,6 +7,7 @@ #define _LINUX_ATOMIC_LONG_H #include +#include #include #ifdef CONFIG_64BIT @@ -1809,4 +1810,4 @@ raw_atomic_long_dec_if_positive(atomic_long_t *v) } #endif /* _LINUX_ATOMIC_LONG_H */ -// 1c4a26fc77f345342953770ebe3c4d08e7ce2f9a +// 01a5fe70d091e84c1de5eea7e9c09ebeaf7799b3 diff --git a/lib/atomic64.c b/lib/atomic64.c index caf895789a1e..25cc8993d7da 100644 --- a/lib/atomic64.c +++ b/lib/atomic64.c @@ -67,7 +67,7 @@ void generic_atomic64_set(atomic64_t *v, s64 i) EXPORT_SYMBOL(generic_atomic64_set); #define ATOMIC64_OP(op, c_op) \ -void generic_atomic64_##op(s64 a, atomic64_t *v) \ +void __signed_wrap generic_atomic64_##op(s64 a, atomic64_t *v) \ { \ unsigned long flags; \ raw_spinlock_t *lock = lock_addr(v); \ @@ -79,7 +79,7 @@ void generic_atomic64_##op(s64 a, atomic64_t *v) \ EXPORT_SYMBOL(generic_atomic64_##op); #define ATOMIC64_OP_RETURN(op, c_op) \ -s64 generic_atomic64_##op##_return(s64 a, atomic64_t *v) \ +s64 __signed_wrap generic_atomic64_##op##_return(s64 a, atomic64_t *v) \ { \ unsigned long flags; \ raw_spinlock_t *lock = lock_addr(v); \ @@ -93,7 +93,7 @@ s64 generic_atomic64_##op##_return(s64 a, atomic64_t *v) \ EXPORT_SYMBOL(generic_atomic64_##op##_return); #define ATOMIC64_FETCH_OP(op, c_op) \ -s64 generic_atomic64_fetch_##op(s64 a, atomic64_t *v) \ +s64 __signed_wrap generic_atomic64_fetch_##op(s64 a, atomic64_t *v) \ { \ unsigned long flags; \ raw_spinlock_t *lock = lock_addr(v); \ @@ -135,7 +135,7 @@ s64 generic_atomic64_dec_if_positive(atomic64_t *v) s64 val; raw_spin_lock_irqsave(lock, flags); - val = v->counter - 1; + val = wrapping_sub(typeof(val), v->counter, 1); if (val >= 0) v->counter = val; raw_spin_unlock_irqrestore(lock, flags); @@ -181,7 +181,7 @@ s64 generic_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u) raw_spin_lock_irqsave(lock, flags); val = v->counter; if (val != u) - v->counter += a; + wrapping_assign_add(v->counter, a); raw_spin_unlock_irqrestore(lock, flags); return val; diff --git a/scripts/atomic/fallbacks/dec_if_positive b/scripts/atomic/fallbacks/dec_if_positive index f65c11b4b85b..910a6d4ef398 100755 --- a/scripts/atomic/fallbacks/dec_if_positive +++ b/scripts/atomic/fallbacks/dec_if_positive @@ -2,7 +2,7 @@ cat < 0)) return false; - } while (!raw_${atomic}_try_cmpxchg(v, &c, c - 1)); + } while (!raw_${atomic}_try_cmpxchg(v, &c, wrapping_sub(${int}, c, 1))); return true; EOF diff --git a/scripts/atomic/fallbacks/fetch_add_unless b/scripts/atomic/fallbacks/fetch_add_unless index 8db7e9e17fac..a9a11675a4d7 100755 --- a/scripts/atomic/fallbacks/fetch_add_unless +++ b/scripts/atomic/fallbacks/fetch_add_unless @@ -4,7 +4,7 @@ cat << EOF do { if (unlikely(c == u)) break; - } while (!raw_${atomic}_try_cmpxchg(v, &c, c + a)); + } while (!raw_${atomic}_try_cmpxchg(v, &c, wrapping_add(${int}, c, a))); return c; EOF diff --git a/scripts/atomic/fallbacks/inc_unless_negative b/scripts/atomic/fallbacks/inc_unless_negative index 7b4b09868842..0275d3c683eb 100755 --- a/scripts/atomic/fallbacks/inc_unless_negative +++ b/scripts/atomic/fallbacks/inc_unless_negative @@ -4,7 +4,7 @@ cat < +#include EOF diff --git a/scripts/atomic/gen-atomic-instrumented.sh b/scripts/atomic/gen-atomic-instrumented.sh index 592f3ec89b5f..fbc6c2f0abd3 100755 --- a/scripts/atomic/gen-atomic-instrumented.sh +++ b/scripts/atomic/gen-atomic-instrumented.sh @@ -146,6 +146,7 @@ cat << EOF #include #include #include +#include EOF diff --git a/scripts/atomic/gen-atomic-long.sh b/scripts/atomic/gen-atomic-long.sh index 9826be3ba986..ae6d549c9079 100755 --- a/scripts/atomic/gen-atomic-long.sh +++ b/scripts/atomic/gen-atomic-long.sh @@ -75,6 +75,7 @@ cat << EOF #define _LINUX_ATOMIC_LONG_H #include +#include #include #ifdef CONFIG_64BIT