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: 13642435 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 CEE12568A for ; Wed, 24 Apr 2024 19:17:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986263; cv=none; b=NHWMgJ5i6AOAnYP1i2WHiUFhEkrVmUG8OVIewsryXE1b2f+QNBGmekp05OlGniK+HRhj0lrDJB2nrjM4b6zx3C+Q6vgA+hQl1BA6CPoLpzab7WRD27BHCZEJIx7EDkWt0FyTbG2w8IkN8A2v+b6DlZe8/SGIRGZe5wq2puDKoMU= 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.182 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-f182.google.com with SMTP id d9443c01a7336-1e3f17c6491so1470445ad.2 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=rYywFWg72uU87s89r01yLNJzr30LPKVLFyIKa6LaLfUz2TLEuU03mjR0tsvl88OGZS yJi0CckdcLVDvDhmkGeZi1fdMzxo9Ob51wH0WoZxrJldMrJvpChhpZwMd2Zpsklc4vsM PfkOKBCy72nkLy5ruzdKD+sLR3e+hbIboLrs14Vf3vVukOOdeludsTg2ZMALwXJthvlt CvFsYpqufxIOKhPGJq5yIBugYi3U+/enP1cYAhIkdyYK1taFda3+0d9iD2LVnUCfZws8 hXM88tM6NHLfxXEFqIaoIk1fSUYzAes+mdPI6J2WjFLJt3IFMpgltaVmVhwb/4lolCSZ J2fg== X-Forwarded-Encrypted: i=1; AJvYcCVwcPkdW/3kXp4FzueePju+GxXG6PzeVz+7qkJwT6W5gFGxQIO8KE3MfVqZf1nBuHQX+1dJyJM4Yme7pC0/f24AG9uZEJsW X-Gm-Message-State: AOJu0YyQERTVXvOwJ+c8x90tyE653hZ2xyA7A6tnmOChuaddViWVhgmV RyHUKRaJ2LO52EnoffDqa1ZoD64TN2hOKWy+gIvA1dUD8FOUrjcALsf2Uqo1+A== 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: netdev@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 From patchwork Wed Apr 24 19:17:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13642437 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 61E55757F3 for ; Wed, 24 Apr 2024 19:17:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986265; cv=none; b=Bt9xpCGVFjCVClMHM9eTBEF2zA8A1m0sZZwc2XG9kOHrwhVSyAJ94DeIJAcEX8zgI9JKUw1XpFHIo8ZK9rjaQ77HT4m+ptCLdfZxjoGvnmjUsman1aPA/ceBGKZUNgpc3F/pKQ201bMDT9hILOFcw/b5n4wtSNJDVnKhkh56hxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986265; c=relaxed/simple; bh=xwLB8JfQnGZ+68nfRnKXcpeprEGbIQ5N7eZM3lFRlVk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X+sK1dDOkd9F+AFbM3y8biAftaLZYDtbGxOCajtBaA4RSGD4h32jt4b4JKzu2fyypHnx4noyUbQPj7fPwlHAiOCguWbwiyZ8MLW7QKdxgu6dZ//FjVgUOiDquT1lVbQIBJlOeCXmjVzOt0BsOxmLqfENI1bYK3ZR1KBK/p/qGD0= 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=gU3iIYOF; arc=none smtp.client-ip=209.85.210.178 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="gU3iIYOF" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6f103b541aeso245199b3a.3 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=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=/ovZNJsNh+Q5y3YBgmePtlPx3aXBNKsLPIMy6gD2e/k=; b=gU3iIYOFFuzLzxS0C2y7iC7rAUJK7SjM6UkJuLmQd8VBcNvUgb8ThJ/BGL6AUnGQAT WJ3nQv1wHgGdYe3aL5pRYtbbFHl0hUSxGDLYftKgaOSMr9r7591C2n00pobVT7nQfaZE 3SjWAH+l8RIx56ZVOc62Y1FhH1PsdNI1IMeVw= 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=/ovZNJsNh+Q5y3YBgmePtlPx3aXBNKsLPIMy6gD2e/k=; b=kKhSpJ9DqQzo0ud3hYuGFpeNBh0ErWF4BPUe9nespHSpIKAzxijN3ANRKUZannZD1z gT/AgvPej9yMqjKb3l/a3lXcfFhhQ4qArVVXsDaKcrMkMGYs88v9WSKiVIa1A5nlGeWO xgQN5Ky6or4YwRE9WdLUWg3L4By6XSAgv/SSCfZQRgSjHq41/pEgd6NxIQiQA+LtZfwo bjZlvi5HzrZ47GqtZV+GcrKcSjAlYALV5QAZNUGyFu/XImz9kTvfr5NfgBdpulOdTZNE Z4/oswuNz2ySTiT+N/RvYaekZuhRZQ3+BmUkiTZtvaF0ByN3oq5iLRh+h3r9kjBOFH8e oVSg== X-Forwarded-Encrypted: i=1; AJvYcCWS6KsPMkD9m0hoWGtH2ZmTtYyg3VHmAsiB4BnfUeATrRv/63tYLakWlQxB/FCbmB3hjFY3J9UiArcxWb3ufxBo5WIP23od X-Gm-Message-State: AOJu0YxNfTPS4fgMyB9zwo0FLk/SMy6EmAVjgRFj0mbf7dpTjQKnOqGt KJxcaLz+bZJc7EFTotkX97N16d9MfvMxDv614AkNiR+tBQuI8RokRAyMm1yr1Q== X-Google-Smtp-Source: AGHT+IH5PxCoRC7r3PuTmsotsJWh4LNkWmqcpOxpDSEhbERbm5LugRo2mXC3N2+7jUPKaLXdawyW1w== X-Received: by 2002:a05:6a00:21cd:b0:6e7:3939:505e with SMTP id t13-20020a056a0021cd00b006e73939505emr4435012pfj.2.1713986262601; 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 p7-20020a056a000a0700b006ea8ba9902asm11784540pfh.28.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 , Catalin Marinas , linux-arm-kernel@lists.infradead.org, Jakub Kicinski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Arnd Bergmann , Andrew Morton , "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , "Paul E. McKenney" , Uros Bizjak , linux-kernel@vger.kernel.org, x86@kernel.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/4] arm64: atomics: lse: Silence intentional wrapping addition Date: Wed, 24 Apr 2024 12:17:35 -0700 Message-Id: <20240424191740.3088894-2-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: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2182; i=keescook@chromium.org; h=from:subject; bh=xwLB8JfQnGZ+68nfRnKXcpeprEGbIQ5N7eZM3lFRlVk=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmKVrRbqMbwLvcfN/CYeO2dTbShkdvlexcHwL/5 YEg6VqdugyJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZila0QAKCRCJcvTf3G3A JmxbEACdESTOy4jkKFG20/bUsA+2niWwljmd0zr7tKnSdR7jpksWerZepL1D4SjqYa4O/LavsJ5 lFd/o7p1MTmSLppqZ5RNFhrAXffGJhvjZs/LhFSfihaH32GxNP/5smFSj95gTaKIgpghcplMG5J ByNnCX4z1k1dih5igMvnndR1vEj3i5zmohj/juFh6Gwur0F0eZDDT/hKmkzXLs89XHe4T8D+9mw +Ak5yTBAesLT4zSQ7cgFT4ihOnocVSo/S6GGmN4SNypgaWIWVr528R6/4/jlw2bssx8Sbkt2fZi vzwM8TMrof0cGiMS0h3BEPIlHo5o465p4D6Ns2/40gF9l/5yMIr2RrOIDyQisudC/hM3NGnrVt6 Rpr3L2g9GgwoqTupgCCes+a3Z/pL8Ehn2iwtlW3+8nMbU0vCQLI10C34kkBQyz7tzEuA+/d0yf4 EgjvKrBdM9sIoKmoI1sDwkDcII5r5mROTGOv4FEEj8Kh7W5aVZhk9k8uoYmuiYnG102+zY7hSe3 zI5EKKzAoFfA1FRCUqbu3rd/sdd9swVFJgjLylGG2OwkUn7DaH1HAEm4w05ft2MnZnWZmiuaoKS jPCSl6Ouz4rBGE4yLDtYT/3JLzIFZWH/GWFyXG3+I4o1/A6qeIn5xvfrkMOrWLLJby5smfXa2vt f4Zqx35NAt41MRw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Annotate atomic_add_return() and atomic_sub_return() to avoid signed overflow instrumentation. They are expected to wrap around. Signed-off-by: Kees Cook --- Cc: Will Deacon Cc: Peter Zijlstra Cc: Boqun Feng Cc: Mark Rutland Cc: Catalin Marinas Cc: linux-arm-kernel@lists.infradead.org --- arch/arm64/include/asm/atomic_lse.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/atomic_lse.h b/arch/arm64/include/asm/atomic_lse.h index 87f568a94e55..a33576b20b52 100644 --- a/arch/arm64/include/asm/atomic_lse.h +++ b/arch/arm64/include/asm/atomic_lse.h @@ -10,6 +10,8 @@ #ifndef __ASM_ATOMIC_LSE_H #define __ASM_ATOMIC_LSE_H +#include + #define ATOMIC_OP(op, asm_op) \ static __always_inline void \ __lse_atomic_##op(int i, atomic_t *v) \ @@ -82,13 +84,13 @@ ATOMIC_FETCH_OP_SUB( ) static __always_inline int \ __lse_atomic_add_return##name(int i, atomic_t *v) \ { \ - return __lse_atomic_fetch_add##name(i, v) + i; \ + return wrapping_add(int, __lse_atomic_fetch_add##name(i, v), i);\ } \ \ static __always_inline int \ __lse_atomic_sub_return##name(int i, atomic_t *v) \ { \ - return __lse_atomic_fetch_sub(i, v) - i; \ + return wrapping_sub(int, __lse_atomic_fetch_sub(i, v), i); \ } ATOMIC_OP_ADD_SUB_RETURN(_relaxed) @@ -189,13 +191,13 @@ ATOMIC64_FETCH_OP_SUB( ) static __always_inline long \ __lse_atomic64_add_return##name(s64 i, atomic64_t *v) \ { \ - return __lse_atomic64_fetch_add##name(i, v) + i; \ + return wrapping_add(s64, __lse_atomic64_fetch_add##name(i, v), i); \ } \ \ static __always_inline long \ __lse_atomic64_sub_return##name(s64 i, atomic64_t *v) \ { \ - return __lse_atomic64_fetch_sub##name(i, v) - i; \ + return wrapping_sub(s64, __lse_atomic64_fetch_sub##name(i, v), i); \ } ATOMIC64_OP_ADD_SUB_RETURN(_relaxed) 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: 13642438 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 9093E757FC for ; Wed, 24 Apr 2024 19:17:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986265; cv=none; b=sdVn/6oiCr8FQlxtuzXJPNnl2jAo3JwvoOS0JYW6W5VzSQ5DP9g8Vbu8tnWQfxfCOIQEM7v1mC/EVbvoQjtKWcX37VsW1ctvS5hMvbwmAAhm2kbPlzBHP0Nfz0/jgskYENPPKd4290rLSN9jctsKkX9voIFnfwgFTZqWO3/xt48= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986265; c=relaxed/simple; bh=p+vioqJHMrVAxi1bLzx7AQ2vntcfu+EHzlTtU17Neoc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nCYJaBXtGdxoyUta8rphGmJ9qFgqessY5ZiuDQ+N2NDdjqJw3FS1s0tMy5mPB3TWk99ricMtQDqHAR8Omi/lYB7DjWig560HTKFsPS6TURJ7SeiYS0fhLK0JjjRGQmDGBRaBhChLS+ABrsCI+FElSwuTcKQx6F5yRA03wrw1vHs= 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=PVDWgoj+; arc=none smtp.client-ip=209.85.214.180 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="PVDWgoj+" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1e83a2a4f2cso1279705ad.1 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=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=YEPqPttQfQibEvJLf164NSrzanGE7tc+dv4CafbEPd0=; b=PVDWgoj+r6wPynfTcoWtVvTh1T5JCBLWtLy25X9WgA7QXfztBCelHN09r/HNk1NuzA y49wQKQkMs/ybeKRbkAvj6ZGVawyyMpt/QtTxKGCPgzQjBSLCXy3KEazP9IPnRtB5Hws pB6aBoSK64X27WEVp05NGs7mGkGqAqi0pqYRg= 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=Nn32k4VHPpgIphmGe7Vt00wZdbD+9qs7BQ7AMgDifbJP6ZfhLznSn7y9QYYfrGk1yM ORo23WR5qJWyuXDtlueATr1DL+aWQmPFDVpFmKU0h2GCds68oz9gS5K8rTqWSLQ27snc GpXxysw1wTF+foxynbQPaeLKwKCeIi/UFTxMTSj9CuvvrdV9v4yBWn3TYd5Suj7LCOde x21Gv5LGXXIJ+FZFJzL3hvqYq8IlhPx3/ycTOHKGWgoKwgoo8lk1w77uf6MRGfwKWekF gP00IDyKEY1miqOThBIN8Tv2QEX8JGmvKGrxsFPpeG+5BKRxOWQ8Zz0uEDMypfYlrdUi VrTQ== X-Forwarded-Encrypted: i=1; AJvYcCWlod7hbn3Ec5UVCqK4C4jvx4kaxJxaRbiOxQv3QYk/sNFMfPqhocyW6J16b+8dktpwwj/kqr5A3YXgbqK4ZazKK18dDDLF X-Gm-Message-State: AOJu0YzsbbBTOAS4Q1RcSXFhQoNT0pdw5h9M9QYhvYzaLEg7a/NlJeUh ZN4UU3NPnp1DPdzQ5NbBCxCCL9Z5cw1Qy7sLCANA+H9gdk+CW7T1Og1bd9gsUQ== 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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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 From patchwork Wed Apr 24 19:17:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13642436 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 23B6E1C6A4 for ; Wed, 24 Apr 2024 19:17:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986263; cv=none; b=FQ174EARblDFSAu/HA6mFZtm+N9ijO9ROQq5cOl4jHDqZprvcwj71RfcAzPyDNcf03IoFPP0LWUkq7nK549Mf8e6g21ZfoBDILbyRFb30Zsy+/fJtxYPbSLQ7dyELEAYQtLX2MhLi7Q+xxoI/069EnDQVDlbqU2HjpIeqsu1o7A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713986263; c=relaxed/simple; bh=WfTBGnBFkRU20qhctvMnIHaixmJ1m8yq7qOHp+mKQX8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M4mtIEoAipSey0lvR/Kk7+hDLQ2aYGquD0SoRG5pdbHFPj1yzN+ahzX9YG0Cb77OT5wxDG+wO4Q4tYsHFJHsgADsZZFMbWMyO8BidcPTTUlokY+hlH8JGC3D+qJ1lLNA2FBbolpX/oFiIolL8z9bSVa7FDqvhx3EJ/aVOKhiEpc= 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=WdzHKOnR; arc=none smtp.client-ip=209.85.214.171 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="WdzHKOnR" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1e455b630acso1291515ad.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=E5yDcYDvu79ikloFjmN2ArtCsis9cNcuVqbWpK2seK4=; b=WdzHKOnR2NZYbZjMSuOUnxhgbN6UtJedTL0ijhz4M2j4e52gVe+Pt6iZfR0Rfl0JJs Lc4RQfwHcLmpOSzbRRQQXlj3pwkgaks6ElhyvlSGtFuKE9sBFzhzHNrBQxcF0wj3WujX SzmfZB1348TAfGvXZOEY8x6Aqk5/gTvCZHufU= 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=E5yDcYDvu79ikloFjmN2ArtCsis9cNcuVqbWpK2seK4=; b=vHxppIX9o1dap/PgP+q5xKf2MxLql+6hb1RZsFj2UJPj9Yd4HClfDHTMLscyfATkMs 74Ycm7ypi/p1ZJyGfenmu+/59m8+fhv+GMAMPRuiL0JzXa2q6Zp2DqUb2s/5DcI2qAQB PtwMPt8wZNVhOfuJpsYfqnZ4Qq0IbpbhJFrB9I1Wn6lGiEofanECMzI3vfZw1HM0iXxP xI241rQTPQEqM0Sa61+VMv/gtuqxS5NmkkHOCbeKI069PI9cJNiU3adRfx2xwWQaWnML aV7jrrac0GA3EavQ8y/vR29NpFuL30z0XkGYLJ9aanEpkOqYGxOh83WlZ1eiCZ/I5CfT VQKQ== X-Forwarded-Encrypted: i=1; AJvYcCUiYMAJByHWQVRqWLeMmSRCaLodk3ivAR9ezkq97zauJP2NvZw7ksTY7AlOcrlMQgGxNaWc8jW0M3hDDuSGXcd4Hi+rxzR1 X-Gm-Message-State: AOJu0YyVOZoWhMUgy1raVDdaJuRXCZFYePlH5sldiTxJDingAc8NyL6y CJfFZWFo1QK47tSV4tl67HhN2GE/Q/2kaX3M6tAM2LCijDTYLih9VnmX/5SJaQ== X-Google-Smtp-Source: AGHT+IEJ7QE65ebG0qqOnn+xdnnZ+q5pZtXGv5jZGzp9dsg2qcg4PCBbrB2p5kleElQysLvEwEP7kg== X-Received: by 2002:a17:903:22c8:b0:1ea:9596:9218 with SMTP id y8-20020a17090322c800b001ea95969218mr1597370plg.45.1713986261347; 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 f12-20020a170902ce8c00b001e1071cf0bbsm12295846plg.302.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 , Jakub Kicinski , "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , netdev@vger.kernel.org, Will Deacon , Peter Zijlstra , Boqun Feng , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Catalin Marinas , Arnd Bergmann , Andrew Morton , "Paul E. McKenney" , Uros Bizjak , linux-kernel@vger.kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, linux-arch@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 4/4] ipv4: Silence intentional wrapping addition Date: Wed, 24 Apr 2024 12:17:37 -0700 Message-Id: <20240424191740.3088894-4-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: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3326; i=keescook@chromium.org; h=from:subject; bh=WfTBGnBFkRU20qhctvMnIHaixmJ1m8yq7qOHp+mKQX8=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmKVrRYEsvdcnHI4MuZvwNOd4oD1dteUrKWikwo S1OiHOayDWJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZila0QAKCRCJcvTf3G3A JkkcD/401uVxwndvI6fIAQZvmMpVPjLvFTgxS4IRuBU7Pqf0z0a5RSlHhEAeKEg4DA1/5Qn/Nk4 7VXgUWo66jWzFRmlmmipMniWFPzLt29JLEXmEIi/HV0UsKdhNmPyzFXJRXOKetIyH8MmkvA0nAQ PDrehyaM/tAewU/mTA9oiLtlT3Pg1bfNwDz5pvHwwJ+ON0Y6wxXaEqePkQt3WfBRCtTqIGaGl1e ow4LBS1klep8nsTmXBxq+ACfXBFNBrZebSFAuwGdlBJGy7EAo9NmKw4zZjqGze1GxLRsImdA8EI UxYMHW372KXbTpFeK0tNXvnb4mrDNqQ6a8FhdTEWDKMo5d4V2DHssDDI9Ol4XN/WioLS4gJ+wtm 1Os3DVpUl2LAzSNYLSEE1TokL+uJEMfnHPZLep2xANF25i11AnPPluIaEpGaMJrTKe0r2JjJw3Q jl0+qHCiCYcwTnajqt5uItmDrlePlJSVkhWTGJJK0TAh/aA28ei9NkULfuqGBG/QjMoB+a9EZyQ HXOifu761IGfEi9g62Q3Smz8Ha3KKrhBtCzTaGRrkW0/IFeGrE/iqa9cpRZm/hPl5dbuFKmFuPY zWTLOe3+l8OTPy5xS07j+aMrOmS+PihzCAw+ZJn7ax6TlLG1LcomfeZoMEIjNpHm1d4OPWK1BEw jpwKjUXXLLcV1Kw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Patchwork-Delegate: kuba@kernel.org The overflow sanitizer quickly noticed what appears to have been an old sore spot involving intended wrap around: [ 22.192362] ------------[ cut here ]------------ [ 22.193329] UBSAN: signed-integer-overflow in ../arch/x86/include/asm/atomic.h:85:11 [ 22.194844] 1469769800 + 1671667352 cannot be represented in type 'int' [ 22.195975] CPU: 2 PID: 2260 Comm: nmbd Not tainted 6.7.0 #1 [ 22.196927] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 0.0.0 02/06/2015 [ 22.198231] Call Trace: [ 22.198641] [ 22.198641] dump_stack_lvl+0x64/0x80 [ 22.199533] handle_overflow+0x152/0x1a0 [ 22.200382] __ip_select_ident+0xe3/0x100 Explicitly mark ip_select_ident() as performing wrapping signed arithmetic. Update the passed type as a u32 since that is how it is used (it is either u16 or a literal "1" in callers, but used with a wrapping int, so it's actually a u32). Update the comment to mention annotation instead of -fno-strict-overflow, which is no longer the issue. Signed-off-by: Kees Cook --- Cc: Jakub Kicinski Cc: "David S. Miller" Cc: David Ahern Cc: Eric Dumazet Cc: Paolo Abeni Cc: netdev@vger.kernel.org --- include/net/ip.h | 4 ++-- net/ipv4/route.c | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/net/ip.h b/include/net/ip.h index 25cb688bdc62..09d502a0ae30 100644 --- a/include/net/ip.h +++ b/include/net/ip.h @@ -537,10 +537,10 @@ void ip_dst_metrics_put(struct dst_entry *dst) kfree(p); } -void __ip_select_ident(struct net *net, struct iphdr *iph, int segs); +void __ip_select_ident(struct net *net, struct iphdr *iph, u32 segs); static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb, - struct sock *sk, int segs) + struct sock *sk, u32 segs) { struct iphdr *iph = ip_hdr(skb); diff --git a/net/ipv4/route.c b/net/ipv4/route.c index c8f76f56dc16..400e7a16fdba 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -458,7 +458,7 @@ static u32 *ip_tstamps __read_mostly; * if one generator is seldom used. This makes hard for an attacker * to infer how many packets were sent between two points in time. */ -static u32 ip_idents_reserve(u32 hash, int segs) +static __signed_wrap u32 ip_idents_reserve(u32 hash, u32 segs) { u32 bucket, old, now = (u32)jiffies; atomic_t *p_id; @@ -473,14 +473,14 @@ static u32 ip_idents_reserve(u32 hash, int segs) if (old != now && cmpxchg(p_tstamp, old, now) == old) delta = get_random_u32_below(now - old); - /* If UBSAN reports an error there, please make sure your compiler - * supports -fno-strict-overflow before reporting it that was a bug - * in UBSAN, and it has been fixed in GCC-8. + /* If UBSAN reports an error here, please make sure your arch's + * atomic_add_return() implementation has been annotated with + * __signed_wrap or uses wrapping_add() internally. */ return atomic_add_return(segs + delta, p_id) - segs; } -void __ip_select_ident(struct net *net, struct iphdr *iph, int segs) +void __ip_select_ident(struct net *net, struct iphdr *iph, u32 segs) { u32 hash, id;