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: 13642446 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 CACE4C4345F for ; Wed, 24 Apr 2024 19:18:09 +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=xtXTiIa7qWEFDfGB1tF7arEdCrDOXCP0iArKRCTPBF8=; b=ib1VD11YZ2PMqT LfKGud47KrgrfD+24r27LC/6rnHr8Cp7JhFbGwN+0W2B0s5IqoebGQAWP10rdUPtgcWwl/HMebUmt A060Ngssc8ZcySLin1c0xHoSzvxSNwTG3aesTPLDSXJ2dkCmoIwEG3nZD8ZvoZL8M6GYS5DO/SZrF YsVBhYDTr67wW5Eq1x6KjgFtIW/c/jJKnnZ1zz8ShpSbIU36dLNvO/Xn8dEiAAnQawRVuGlqqLeCy h9wABMD1CwRcFmrAlAyZtPgrlltaDkqyABW1oujiEnTG6Uoc+tST+cIk0dqZbcOi8ER5RZwwqRrg0 +TOY+m8eqOJZEo0lqLow==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi80-00000005cTR-0zxs; Wed, 24 Apr 2024 19:18:00 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi7o-00000005cL8-0GdR for linux-arm-kernel@lists.infradead.org; Wed, 24 Apr 2024 19:17:50 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1e65b29f703so1529205ad.3 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=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=vo2K8jVQRi43xxtGN8MvYubFuFXJHBPNUxXy1Vyz154=; b=lHyCJkRhwtL7yJk/zls5Kr67QW5PMky8WlW1+iUW7nbA3mgEU+3KxYkzSf+/GZFIqN 2igUgPWMfeRYQ/820RxPNdhC0FCQq5W0Axh5jXGVeOTS/dxGqR04o1Tv5k1cF+nys00v 08vANpCW/6ozVOr1S1HZHtf3qhTDCxdD9DgYw= 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=FL5k6MrCT3gPv89KN57fpncbWiOHv366uCRL7pQLkE/e5LYbvqsiu+kI6wy3MJh42G CFN3yPKMROhwYJO+bddFQ18Tqs3ThSAyqtNIl5kM2/19MNhjleYj5f/duArJZRej1IhQ q6X4FmDOmQaewNYzJQ/k1Nvi+YYTbcTv42UQ2yiADfCMvpR2mp2ocMCge+xJedvkEvZw ZJzEZ3Wp5rH+DZuQ7AsqZNN82BSCitBi1czwJCZ2siv4HnHoFpqg6JLqoFB9Gr0PzDPT sVlBVdQcRqMosCet7jUERIb7xJe2lPF+Wmd7fUW0+WdOJHFnHyK3Ydgn1DdL7apB5px3 Z/ew== X-Forwarded-Encrypted: i=1; AJvYcCVz5N1t1M+dscJtZCMB+u1WdcDHAWleZ5gjNk8V41NCeFULI4L2m9//HgRWajlTgi9usG4rcbnjZD9vx06Mf6e/ebMe7uwo1Z7RV87MSomOTTF6Xn8= X-Gm-Message-State: AOJu0YxefXxdYh0LuIpS7F9L0C752CNpxGrlec9MSPrNANmr6Z0QCpyL KYNC/hlhLMyWq5MBEb4KzbBUAa4s038QaHD3P7E8hik2TBI/yE9xff6ZYRtp6g== 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> 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240424_121748_262048_33C492FC X-CRM114-Status: GOOD ( 16.64 ) 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 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: 13642447 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 63A03C4345F for ; Wed, 24 Apr 2024 19:18:12 +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=hHPF9/wAz06pnLAKgDab/v5JVAU+ryx6wbaZjSeyoDo=; b=SZg3zG+dIQeHlW eeToCY8JqJCV6uaMBMJhOtnbBQJ6sUsPSu3bOTPKHoDqV1kx+nqy7b3hpwUdEuVV+xeenneXMgiUe NFe8wZEutTcfzZ0btQffqYWOIeaQXwLt7rrhtWoj8IpJRAKndOgztKm4fF+YHfblgpSRAU0pGVgn0 e7ofENidazhTahFaaYnzfJdy+4DQc7BKa8jYv5vkQ7Km4l8LstZuUtmRj7gDJGMopao+qImBG2Ms8 QmzZx2dt3eU4xmfzoLiW1pZArRKNvl4BlslfaqdgjJsyb37y/ljmMFHNmhROJiwHxwJl9XqNhCm3F ywtaUnxiowoqeqvg6pUg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi82-00000005cVc-1ICb; Wed, 24 Apr 2024 19:18:02 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi7o-00000005cLI-0pSe for linux-arm-kernel@lists.infradead.org; Wed, 24 Apr 2024 19:17:51 +0000 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6ed0e9ccca1so286346b3a.0 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=/ovZNJsNh+Q5y3YBgmePtlPx3aXBNKsLPIMy6gD2e/k=; b=oU4Sa7CxQpScQbKHycL19PaVDy0GM2zgyxk4Sjqtqe+YMS+OZNskvepwxyzhitdwSf f+qkmoAmOQrvvhaJqgBB1+lNYoWZD+S/p6TON7moy+x9Y1UFePdGOE8lzz4+N0sr4+BF hmvp9uPFBN60aqwiH7vbXmYSuDS9LRwZCLDeE= 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=coy5AXWW0i4gN6Iz3r+myJIN03kJxTI+A1sKOAJfgOzRWN9mAjWVsonP8ljI5Md0b7 xpCAY6iU2MZVJJUXTZhpuzmQ+v3iyos+Zi/5r39o4aznM7tRqvctdVQgQqpTi1ot8nlh W2nDs7ag5zoBiMGlupG89PTupEkJ4hgHUZ+EqZ1AIVSnA2b29ejU4qjCozJL4h164PJ9 XAxzoZrnNSu32seXaBEMJzIcseqdhAJsenwBGQiA4pojLREtoDDDN/A5hGVi7Bj0Vlgm t+7bXfNJ1Pq/tGZQNbYQcCGniEAM665L3F0H41Kblkli21PWDGaqOQWUKWnofOd23vz4 NvPw== X-Forwarded-Encrypted: i=1; AJvYcCXTiYucMBuIfEZqG4qGrNO/3wv34mr0/hpbXWzFVwLHDBZyS+F171FydszJkMKIhbitwB4BqWaJ4FWpHjhywl3pTZws+PcmjG/ElO92SFtVEYQK1UI= X-Gm-Message-State: AOJu0YyShiTinX6tNMLuvZFWVdBl7haSEJXnCWo/coEwa6kPbNMb2Tf6 ptFHszlUZwSfH4+wbjMULzffdiFk0fHYwyQm77LFvTZ1gn52jHGH+UfIQIJdGw== 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> 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240424_121748_307313_6B9BB34F X-CRM114-Status: GOOD ( 13.20 ) 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 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: 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 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: 13642445 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 5AC9DC4345F for ; Wed, 24 Apr 2024 19:17:59 +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=Nb3Zl22oab1ox6/JdyCe4NqNbt0+2oc0gDKs3ZJDv6s=; b=FKUDpnKqBx2K1Z m/+89EOx2qTrEnfRDpVLKKeePdKzdtrHil4RgbSEAUFVQnPw0yy4GOsOfHrlXFxqGQqby7ZZCFUnE lk2pizZ85Ya/cQB/rjQLHhhRSWRkKqNpCUTazSBWhoTt6i+nHcBExywpGV4tjS3pNDkX8ePSXV4Xr YPEtnv0o5KT1zkPCjHwBYZpyRPRHnR0KE/AV0oDnAI0chrLq8hshpvN13smcFlY+4A6IP2d8xngNP fyoGpKodqPR0G03TNGrYq8mciPaHa/kTCpJBmcdseVq2Um4hTSp6PCZOP+BlAmCVn4+5a6qVGgpAb zB9/3/qZfHIoJeTN943Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi7o-00000005cN3-2li5; Wed, 24 Apr 2024 19:17:48 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzi7j-00000005cLD-0xGI for linux-arm-kernel@lists.infradead.org; Wed, 24 Apr 2024 19:17:47 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1e3f17c64daso1264525ad.3 for ; Wed, 24 Apr 2024 12:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1713986261; x=1714591061; 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=E5yDcYDvu79ikloFjmN2ArtCsis9cNcuVqbWpK2seK4=; b=bMMpKQPQiwrpjV4yGW1S2sw/MzpbSz9AKf0c0QDTxff9vDo9TKlBWu/7TGW+fGKICl M1gtbvHgd0oifA6kBcMzeRQDDl2rE9Q2HpZiCIBvohAZ2/6DKG4B0g/6LYsjjhEyrG3n cwN4X00Ft47fNggAW890JEGb7kYsCu1+boCww= 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=oTfMKhv4wQBIdsemd7uL2ZZ923lMVSXSikaXWwAybOO4UhLFEhcko3/TvbW4sqmDBP tWbHz1XeUpEeSrSxxw6QSkkjwIG9OdiNIRDtUlo5PNUH2r+/WHnk7tPRBVpIa+QqjXVz F3hN1AuAuLklY8t+BNKZ6XROmVj9p0lCQwnqTp20IyzfG3GlVS8GsoVSTud1lcLHcW67 ZPxyHRaU4TjJ9u436sfa5THCQhyXVRgs806ICSNp3t8DXvfDDRY83H9aN/gNHgQTO1PB qxn/bhprgxrrwiPZeu9xvVl9Ahy94sR6jqP0sflELNd7tIG0M0ba/jZ19ZP8/6GAWT2w gBug== X-Forwarded-Encrypted: i=1; AJvYcCVeWfy6HFv/7iEUI/5JR/niBMUFR58PzJ+AFRMe9ih3j7fGYdd7A3/QcDgzymXcuVscKfmNLa/y3f2P4kiGcw9GHg4qIvAcGgx8sl+okuUbIZSbgkI= X-Gm-Message-State: AOJu0YwwgXPIwGE6GyYBrwpAPWBoW81MM9+cAdNZ6esPsFl0ixjJ7aIk AKzjU2iLZEKEhiESh9XU68HPn4OMZPCoMpKHtTyh3/V+507XfwKNg6e7/zccNQ== 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> 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240424_121743_319264_D87FFAF5 X-CRM114-Status: GOOD ( 20.10 ) 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 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;