Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1929905rdb; Tue, 5 Sep 2023 09:05:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF+UBATAvdvczJS9bbYdhS1G1HQOCt7SFsOGXx/lIb9kt6sX3P7W4VsctK/u/wuHToAFEbm X-Received: by 2002:aa7:cd13:0:b0:523:4996:a4f9 with SMTP id b19-20020aa7cd13000000b005234996a4f9mr189751edw.34.1693929949062; Tue, 05 Sep 2023 09:05:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693929949; cv=none; d=google.com; s=arc-20160816; b=TYlnIwDAyEPO5tAbIRAkWSfwG0rlaQ/TDaz2arbLoRFKQ8e9le4q/ED7W3xroVHlsk dV0Dvy9nZjgUPG8Gf2K3hlDR700pdw7QxUyXFxO0md9pKO27G7rpD5Mxcb7CnoqzsRgF Ye5VUDQBZmqnsZJ1ivQ8SdegR13p7l4yUtsQcIBEDP6HoiSlA3ZzW0ndTC6F1TyeCBaJ yNLuY4mH2UaeGPN+a2Hlrktrgui18dI8SPWS8/q0j4tGh0qxo6GGf9kQFwrmNd7c8jLz y/JvXqOOV7XemW6sftXhUM6zt0k1I1JPPjQUQ1Ff4eF0u5YW6sOlOvpjqrrWJgkl84Mo FCnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=MET/hQwpeova7Ho1GgzgDeTkiQ/1n+JC7QsFR+YAzPA=; fh=ltRwyIXEerPrUR7VpPtvDyC/GbSwgTF9IGywpDQt39Y=; b=cYAKANeRb2askg0LHfDlU76aib928iAIftAowepnKZFNof4a168A9tGZ9Z4m44C8tx +/zIypIOwuEl77ZTOkfHxgiA7cNlsVGO9UFG5iwkG4wqFcnLnnbRHUZ9rnnP7hyXs/G/ ZdbzJ5xrn32W5AukLkWxXoKhNbIzfrpqMEGWgG5qtMvQIC83hVxtulxiesJmBjWCJGo/ gl7V2E92YHnBH0tQeyf6tWeUhkRCWVLi2y0jn3anG5XLnc3lwblAaoYCpveSUZnjpEFi N015QwhrA6tSUrda0hKIGXWBcsJfyfSmBsV3OvjR8n+kX39RhcqbgGaRAJB/d6aWIT9F Cp8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@purestorage.com header.s=google2022 header.b=RMNs5f1W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=purestorage.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g5-20020a50ee05000000b0052556e760ffsi844581eds.74.2023.09.05.09.05.45; Tue, 05 Sep 2023 09:05:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@purestorage.com header.s=google2022 header.b=RMNs5f1W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=purestorage.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244877AbjHaIRg (ORCPT + 7 others); Thu, 31 Aug 2023 04:17:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230222AbjHaIRf (ORCPT ); Thu, 31 Aug 2023 04:17:35 -0400 Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8990819A for ; Thu, 31 Aug 2023 01:17:31 -0700 (PDT) Received: by mail-io1-xd36.google.com with SMTP id ca18e2360f4ac-7926b7f8636so12133639f.1 for ; Thu, 31 Aug 2023 01:17:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1693469851; x=1694074651; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=MET/hQwpeova7Ho1GgzgDeTkiQ/1n+JC7QsFR+YAzPA=; b=RMNs5f1WFrirnZtwwIPAdXnGsNAL4yKRBufYVnB32Mm8ZlWubGHYWAWjapSgn38uWN C2fyT+m/4dKBbvVGQVRdeLjLFNCeZLCDFzkCXbt0LTkZTnj7cSssEE2aR3joc0+Iu3of FIPKeOSWessfrNcU0Gymfq9mwJSc2tuHhtj+lJ7qCKycFiCZdyIcTXhImIOpK+9X8NMk DssKoJssc6hEy3thG19BSYN80PDZKgMDCgZE2kqhOwEUOgpVIvzupPgHT60q2OQFC8CL 9jV1blsY5+xBQTF9GIyPR+4JmERSd7MGiLH2yhV9T+y3mwlbY1eFl62Dp2gnP5p4gqHO n/OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693469851; x=1694074651; h=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=MET/hQwpeova7Ho1GgzgDeTkiQ/1n+JC7QsFR+YAzPA=; b=RnkkBwpHXgfiYaUarT6uE/WPmM/vvMWMSMcJMGzwDSHtB6c2ysz5mcU5dPsG6PWUfN PuzHqQyvuqW+v1ufqtg2Afe5NfnaorvuA4qknqPpQCMpIEXFUXAvZ584FE5xbUVxjJw2 demVqboaSJNeeWJVvTPdCzsW+DtYQkUshcahlJrZApu7PQaR9Lmb2i0L9e6TU5WAy/5B k6yqftWvQSQMRywwulyH8siLvtJw9OheQUNapGhi5w8i4A2GwRAogBqqTtyiI9nWhn88 CBJmz5maQYMP5jthh2PXPuh5d+m5VURG94fpQOsoYuI7ekwA5QDjv4bJEy1oLXa3Gt6+ P+AQ== X-Gm-Message-State: AOJu0Ywm+xo38deXtNJY7p/gCSrcOVOoyOZ27Qpv19tTjth/HgX6EDuX JBiRHAft0h2RnZt5zTi+g74EbA== X-Received: by 2002:a5d:9d92:0:b0:791:8f62:31ef with SMTP id ay18-20020a5d9d92000000b007918f6231efmr2154312iob.5.1693469850925; Thu, 31 Aug 2023 01:17:30 -0700 (PDT) Received: from dev-mkhalfella2.dev.purestorage.com ([208.88.159.128]) by smtp.googlemail.com with ESMTPSA id dm8-20020a0566023b8800b00791e6ae3aa4sm312282iob.23.2023.08.31.01.17.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 01:17:30 -0700 (PDT) From: Mohamed Khalfella To: willemdebruijn.kernel@gmail.com Cc: alexanderduyck@fb.com, bpf@vger.kernel.org, brouer@redhat.com, davem@davemloft.net, dhowells@redhat.com, edumazet@google.com, keescook@chromium.org, kuba@kernel.org, linux-kernel@vger.kernel.org, mkhalfella@purestorage.com, netdev@vger.kernel.org, pabeni@redhat.com, willemb@google.com, stable@vger.kernel.org Subject: [PATCH v3] skbuff: skb_segment, Call zero copy functions before using skbuff frags Date: Thu, 31 Aug 2023 02:17:02 -0600 Message-Id: <20230831081702.101342-1-mkhalfella@purestorage.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <64ed7188a2745_9cf208e1@penguin.notmuch> References: <64ed7188a2745_9cf208e1@penguin.notmuch> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,T_SPF_PERMERROR autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit bf5c25d60861 ("skbuff: in skb_segment, call zerocopy functions once per nskb") added the call to zero copy functions in skb_segment(). The change introduced a bug in skb_segment() because skb_orphan_frags() may possibly change the number of fragments or allocate new fragments altogether leaving nrfrags and frag to point to the old values. This can cause a panic with stacktrace like the one below. [ 193.894380] BUG: kernel NULL pointer dereference, address: 00000000000000bc [ 193.895273] CPU: 13 PID: 18164 Comm: vh-net-17428 Kdump: loaded Tainted: G O 5.15.123+ #26 [ 193.903919] RIP: 0010:skb_segment+0xb0e/0x12f0 [ 194.021892] Call Trace: [ 194.027422] [ 194.072861] tcp_gso_segment+0x107/0x540 [ 194.082031] inet_gso_segment+0x15c/0x3d0 [ 194.090783] skb_mac_gso_segment+0x9f/0x110 [ 194.095016] __skb_gso_segment+0xc1/0x190 [ 194.103131] netem_enqueue+0x290/0xb10 [sch_netem] [ 194.107071] dev_qdisc_enqueue+0x16/0x70 [ 194.110884] __dev_queue_xmit+0x63b/0xb30 [ 194.121670] bond_start_xmit+0x159/0x380 [bonding] [ 194.128506] dev_hard_start_xmit+0xc3/0x1e0 [ 194.131787] __dev_queue_xmit+0x8a0/0xb30 [ 194.138225] macvlan_start_xmit+0x4f/0x100 [macvlan] [ 194.141477] dev_hard_start_xmit+0xc3/0x1e0 [ 194.144622] sch_direct_xmit+0xe3/0x280 [ 194.147748] __dev_queue_xmit+0x54a/0xb30 [ 194.154131] tap_get_user+0x2a8/0x9c0 [tap] [ 194.157358] tap_sendmsg+0x52/0x8e0 [tap] [ 194.167049] handle_tx_zerocopy+0x14e/0x4c0 [vhost_net] [ 194.173631] handle_tx+0xcd/0xe0 [vhost_net] [ 194.176959] vhost_worker+0x76/0xb0 [vhost] [ 194.183667] kthread+0x118/0x140 [ 194.190358] ret_from_fork+0x1f/0x30 [ 194.193670] In this case calling skb_orphan_frags() updated nr_frags leaving nrfrags local variable in skb_segment() stale. This resulted in the code hitting i >= nrfrags prematurely and trying to move to next frag_skb using list_skb pointer, which was NULL, and caused kernel panic. Move the call to zero copy functions before using frags and nr_frags. Fixes: bf5c25d60861 ("skbuff: in skb_segment, call zerocopy functions once per nskb") Signed-off-by: Mohamed Khalfella Reported-by: Amit Goyal Cc: stable@vger.kernel.org --- net/core/skbuff.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index a298992060e6..74a8829a6b59 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -4354,21 +4354,20 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb, struct sk_buff *segs = NULL; struct sk_buff *tail = NULL; struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; - skb_frag_t *frag = skb_shinfo(head_skb)->frags; unsigned int mss = skb_shinfo(head_skb)->gso_size; unsigned int doffset = head_skb->data - skb_mac_header(head_skb); - struct sk_buff *frag_skb = head_skb; unsigned int offset = doffset; unsigned int tnl_hlen = skb_tnl_header_len(head_skb); unsigned int partial_segs = 0; unsigned int headroom; unsigned int len = head_skb->len; + struct sk_buff *frag_skb; + skb_frag_t *frag; __be16 proto; bool csum, sg; - int nfrags = skb_shinfo(head_skb)->nr_frags; int err = -ENOMEM; int i = 0; - int pos; + int nfrags, pos; if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { @@ -4445,6 +4444,13 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb, headroom = skb_headroom(head_skb); pos = skb_headlen(head_skb); + if (skb_orphan_frags(head_skb, GFP_ATOMIC)) + return ERR_PTR(-ENOMEM); + + nfrags = skb_shinfo(head_skb)->nr_frags; + frag = skb_shinfo(head_skb)->frags; + frag_skb = head_skb; + do { struct sk_buff *nskb; skb_frag_t *nskb_frag; @@ -4465,6 +4471,10 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb, (skb_headlen(list_skb) == len || sg)) { BUG_ON(skb_headlen(list_skb) > len); + nskb = skb_clone(list_skb, GFP_ATOMIC); + if (unlikely(!nskb)) + goto err; + i = 0; nfrags = skb_shinfo(list_skb)->nr_frags; frag = skb_shinfo(list_skb)->frags; @@ -4483,12 +4493,8 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb, frag++; } - nskb = skb_clone(list_skb, GFP_ATOMIC); list_skb = list_skb->next; - if (unlikely(!nskb)) - goto err; - if (unlikely(pskb_trim(nskb, len))) { kfree_skb(nskb); goto err; @@ -4564,12 +4570,16 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb, skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & SKBFL_SHARED_FRAG; - if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || - skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) + if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) goto err; while (pos < offset + len) { if (i >= nfrags) { + if (skb_orphan_frags(list_skb, GFP_ATOMIC) || + skb_zerocopy_clone(nskb, list_skb, + GFP_ATOMIC)) + goto err; + i = 0; nfrags = skb_shinfo(list_skb)->nr_frags; frag = skb_shinfo(list_skb)->frags; @@ -4583,10 +4593,6 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb, i--; frag--; } - if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || - skb_zerocopy_clone(nskb, frag_skb, - GFP_ATOMIC)) - goto err; list_skb = list_skb->next; } -- 2.17.1