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;