From patchwork Sun Mar 24 05:05:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shigeru Yoshida X-Patchwork-Id: 13600760 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE291944F for ; Sun, 24 Mar 2024 05:06:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711256766; cv=none; b=tM2ev4hj9l10nC3CEHi3YKiAM2g84l1hbD31mAUDV0sLtm39b7bz8QNLq8DAPWiOdC9VnmX/J69LugcfewR59C4VSoHE/MyyFpMTKalhrHvsvdeqNFypoDluI43e37Csa+eNYRvbuSKU73rro8HA++2VLSn3ON++jyrhBP3rqUI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711256766; c=relaxed/simple; bh=R1JIMxCTBe/b+a+qC/2RObOqzW1ENmG9//PB/KrwKSk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YwW4O0G1lwX4ssb83PuSOp/RHSBwU3H/4BtMCOAa7ksrJF8cqaB2gDF3ZnyfHMGrLZOG1sLe5CQ1OI4EEHjTVVKiMEQ74oyNzjgmQ3A0FKs0mp00Q+WZdZL2eK/S9uGMe+s4wdZOPJsGcEkqCn9o1DYk0hkRd395lTHhKvzgHKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dZuLejmo; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dZuLejmo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711256763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=9duXSLuXHV8vFOCQXD8+b0A+1FBJ4vf3M5EGD4lf1a4=; b=dZuLejmo1vKHfuGe/f9hWOPrKb3Od/2BrM2G3qrHnxEHQUdHf/chSa+alO6qF4WS1gfePq JmstYlqPvIcIblwbuVwaiYwiNzDnitQVKKjeTdkQsiNrJ9V12/t1PpyOcRGAw9h3WYDYS1 Q78HnsYryW3zybeVL9VgQ2vNorIAW+0= Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-541-nIWxQRj0NbOT4IPxIiU7yQ-1; Sun, 24 Mar 2024 01:06:02 -0400 X-MC-Unique: nIWxQRj0NbOT4IPxIiU7yQ-1 Received: by mail-pg1-f200.google.com with SMTP id 41be03b00d2f7-5cfd6ba1c11so2196817a12.0 for ; Sat, 23 Mar 2024 22:06:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711256761; x=1711861561; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9duXSLuXHV8vFOCQXD8+b0A+1FBJ4vf3M5EGD4lf1a4=; b=OqSU9TGR3pWFfMw6VlyMhER1YK066DfwbkJ5FNkuVIFiKdbo3DFsaC61Bn/lozkTHB J37Y1lQlsMWnovcpa9YX6VLUNvbl3C+DVquyxml3bBO0PUc25ZtS3/l+vjLCXMBrq3pl AW7BCDP5FBek50uog88ReTVMlkqy5jlUNduYmYCPnmFDDB+V7OCi0DZVQGm85o4nTrqB aVgMVMXd0FbA/JLTPHVXXey3yx+0KiFqmzy9RjfdZmzRTdZC2s19f2zBW7TOTDkAOYiI s0EiCmFWcLdVn7XtxOl5VFT1xzerM1U5iIu+7tk5YF9c4CAA4sCEfhxWknRmJDeBbsns 0KCQ== X-Gm-Message-State: AOJu0YzFUQFN6BanUizr1HoWzJSnEnnRBRphcEPfI2+Ppd/v0mC4llx/ Br2VIsAmKwjMXhPmeV6mXPUw42oTUX4WnYYjz0y38JvLnTLGyEXVgwhjJswSI8DD8irrQKWVlk9 n3ifaP1Bnz+FVmW/4IEJazg8yEIGCqbAIOoWB8TSWhHnhiaJGr1e3KQ== X-Received: by 2002:a17:902:6ac9:b0:1dd:d431:3388 with SMTP id i9-20020a1709026ac900b001ddd4313388mr3044295plt.68.1711256761169; Sat, 23 Mar 2024 22:06:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEz5guYJ9WMtWgo31CoOgbHkCiVmglV+L41zWnEE9mIBfhDjcUN0Qp+RadlqkxX65rQUFW1AQ== X-Received: by 2002:a17:902:6ac9:b0:1dd:d431:3388 with SMTP id i9-20020a1709026ac900b001ddd4313388mr3044285plt.68.1711256760796; Sat, 23 Mar 2024 22:06:00 -0700 (PDT) Received: from kernel-devel.local ([240d:1a:c0d:9f00:6883:65ff:fe1c:cf69]) by smtp.gmail.com with ESMTPSA id x16-20020a1709027c1000b001dc486f0cbesm2379329pll.222.2024.03.23.22.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 22:06:00 -0700 (PDT) From: Shigeru Yoshida To: davem@davemloft.net, dsahern@kernel.org, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Shigeru Yoshida , syzkaller Subject: [PATCH net] ipv4: Fix uninit-value access in __ip_make_skb() Date: Sun, 24 Mar 2024 14:05:54 +0900 Message-ID: <20240324050554.1609460-1-syoshida@redhat.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org KMSAN reported uninit-value access in __ip_make_skb() [1]. __ip_make_skb() tests HDRINCL to know if the skb has icmphdr. However, HDRINCL can cause a race condition. If calling setsockopt(2) with IP_HDRINCL changes HDRINCL while __ip_make_skb() is running, the function will access icmphdr in the skb even if it is not included. This causes the issue reported by KMSAN. Check FLOWI_FLAG_KNOWN_NH on fl4->flowi4_flags instead of testing HDRINCL on the socket. [1] BUG: KMSAN: uninit-value in __ip_make_skb+0x2b74/0x2d20 net/ipv4/ip_output.c:1481 __ip_make_skb+0x2b74/0x2d20 net/ipv4/ip_output.c:1481 ip_finish_skb include/net/ip.h:243 [inline] ip_push_pending_frames+0x4c/0x5c0 net/ipv4/ip_output.c:1508 raw_sendmsg+0x2381/0x2690 net/ipv4/raw.c:654 inet_sendmsg+0x27b/0x2a0 net/ipv4/af_inet.c:851 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg+0x274/0x3c0 net/socket.c:745 __sys_sendto+0x62c/0x7b0 net/socket.c:2191 __do_sys_sendto net/socket.c:2203 [inline] __se_sys_sendto net/socket.c:2199 [inline] __x64_sys_sendto+0x130/0x200 net/socket.c:2199 do_syscall_64+0xd8/0x1f0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x6d/0x75 Uninit was created at: slab_post_alloc_hook mm/slub.c:3804 [inline] slab_alloc_node mm/slub.c:3845 [inline] kmem_cache_alloc_node+0x5f6/0xc50 mm/slub.c:3888 kmalloc_reserve+0x13c/0x4a0 net/core/skbuff.c:577 __alloc_skb+0x35a/0x7c0 net/core/skbuff.c:668 alloc_skb include/linux/skbuff.h:1318 [inline] __ip_append_data+0x49ab/0x68c0 net/ipv4/ip_output.c:1128 ip_append_data+0x1e7/0x260 net/ipv4/ip_output.c:1365 raw_sendmsg+0x22b1/0x2690 net/ipv4/raw.c:648 inet_sendmsg+0x27b/0x2a0 net/ipv4/af_inet.c:851 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg+0x274/0x3c0 net/socket.c:745 __sys_sendto+0x62c/0x7b0 net/socket.c:2191 __do_sys_sendto net/socket.c:2203 [inline] __se_sys_sendto net/socket.c:2199 [inline] __x64_sys_sendto+0x130/0x200 net/socket.c:2199 do_syscall_64+0xd8/0x1f0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x6d/0x75 Fixes: 99e5acae193e ("ipv4: Fix potential uninit variable access bug in __ip_make_skb()") Reported-by: syzkaller Signed-off-by: Shigeru Yoshida --- I think IPv6 has a similar issue. If this patch is accepted, I will send a patch for IPv6. --- net/ipv4/ip_output.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 1fe794967211..39229fd0601a 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -1473,7 +1473,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk, * by icmp_hdr(skb)->type. */ if (sk->sk_type == SOCK_RAW && - !inet_test_bit(HDRINCL, sk)) + !(fl4->flowi4_flags & FLOWI_FLAG_KNOWN_NH)) icmp_type = fl4->fl4_icmp_type; else icmp_type = icmp_hdr(skb)->type;