From patchwork Tue Jul 16 04:58:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerald Yang X-Patchwork-Id: 1960879 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WNRgc3FZnz1xqc for ; Tue, 16 Jul 2024 14:59:24 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sTaHX-0007nb-1q; Tue, 16 Jul 2024 04:59:19 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sTaHV-0007mv-CL for kernel-team@lists.ubuntu.com; Tue, 16 Jul 2024 04:59:17 +0000 Received: from mail-oa1-f71.google.com (mail-oa1-f71.google.com [209.85.160.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id EDEE33F298 for ; Tue, 16 Jul 2024 04:59:15 +0000 (UTC) Received: by mail-oa1-f71.google.com with SMTP id 586e51a60fabf-26033464bf4so5855808fac.1 for ; Mon, 15 Jul 2024 21:59:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721105952; x=1721710752; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pV555iSfBX8x4sLCC4QjrFAloFliIaAc+dHz3OjWkng=; b=Qy127eMtbog85LkYQNmdthRn3qla85fWK80CrNm44QBopwPXNFW5bRin6nJbxBZnFW ThIiCPBw5Df6DKN9p/GwDRt2LWIDzdG06de6Itmcaf+hrC/34fkRDnyQ/Nzhi6l5UXSk nzou8YocVrLnipWH5mUI2Mo5f+E1GwI85WMU9L/Yii0Ayr9H+L+qZ0fTepbs9QfzCqh/ NiA8nuAKhnQnSetVuWdAGqGbfVDprMcD37HdjWQA3ue9jALLR5bM50FpNRlx6uVfG+P7 6CiHztvuO9k1aTq6B9sGnVdRZM0C0R/eVj96kq+K7GEfOdCqn/lpUVJkYtVmhAOApMFB 6UeQ== X-Gm-Message-State: AOJu0YwMLJxEzjBJtD5yLWDwHvliLWBnhjnQwHIBbA1/awIP97Ur/lLh p47Yvq71HQ6kq6iaN1xzneeONFcgntSDnbNgungJki5QIZ/Dz+d9ikqDxVOuisbmnKobVedfs1o VRQcUyXzKPWNNUTaVNDibBhfnIjpfxvl7ZrwjnbR2U/nbm0aEH7xepVY+lpx+5gQVKBL4moW2NF neMPdPjfTf8w== X-Received: by 2002:a05:6871:b25:b0:260:a77a:f86b with SMTP id 586e51a60fabf-260bd736370mr842691fac.23.1721105952564; Mon, 15 Jul 2024 21:59:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELUJ6QVJQ+c+x5h7jbMJ2E3ZvMFdqjOtS/1FEGlxpagRHYmxxJZAQhiqdqnjzvEGbZDs4/cA== X-Received: by 2002:a05:6871:b25:b0:260:a77a:f86b with SMTP id 586e51a60fabf-260bd736370mr842677fac.23.1721105952042; Mon, 15 Jul 2024 21:59:12 -0700 (PDT) Received: from localhost.localdomain (220-135-31-21.hinet-ip.hinet.net. [220.135.31.21]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b7ecc9266sm5316321b3a.193.2024.07.15.21.59.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 21:59:10 -0700 (PDT) From: Gerald Yang To: kernel-team@lists.ubuntu.com Subject: [PATCH 1/1] net/sched: Fix UAF when resolving a clash Date: Tue, 16 Jul 2024 12:58:37 +0800 Message-ID: <20240716045845.1961853-2-gerald.yang@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240716045845.1961853-1-gerald.yang@canonical.com> References: <20240716045845.1961853-1-gerald.yang@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Chengen Du BugLink: https://bugs.launchpad.net/bugs/2073092 KASAN reports the following UAF: BUG: KASAN: slab-use-after-free in tcf_ct_flow_table_process_conn+0x12b/0x380 [act_ct] Read of size 1 at addr ffff888c07603600 by task handler130/6469 Call Trace: dump_stack_lvl+0x48/0x70 print_address_description.constprop.0+0x33/0x3d0 print_report+0xc0/0x2b0 kasan_report+0xd0/0x120 __asan_load1+0x6c/0x80 tcf_ct_flow_table_process_conn+0x12b/0x380 [act_ct] tcf_ct_act+0x886/0x1350 [act_ct] tcf_action_exec+0xf8/0x1f0 fl_classify+0x355/0x360 [cls_flower] __tcf_classify+0x1fd/0x330 tcf_classify+0x21c/0x3c0 sch_handle_ingress.constprop.0+0x2c5/0x500 __netif_receive_skb_core.constprop.0+0xb25/0x1510 __netif_receive_skb_list_core+0x220/0x4c0 netif_receive_skb_list_internal+0x446/0x620 napi_complete_done+0x157/0x3d0 gro_cell_poll+0xcf/0x100 __napi_poll+0x65/0x310 net_rx_action+0x30c/0x5c0 __do_softirq+0x14f/0x491 __irq_exit_rcu+0x82/0xc0 irq_exit_rcu+0xe/0x20 common_interrupt+0xa1/0xb0 asm_common_interrupt+0x27/0x40 Allocated by task 6469: kasan_save_stack+0x38/0x70 kasan_set_track+0x25/0x40 kasan_save_alloc_info+0x1e/0x40 __kasan_krealloc+0x133/0x190 krealloc+0xaa/0x130 nf_ct_ext_add+0xed/0x230 [nf_conntrack] tcf_ct_act+0x1095/0x1350 [act_ct] tcf_action_exec+0xf8/0x1f0 fl_classify+0x355/0x360 [cls_flower] __tcf_classify+0x1fd/0x330 tcf_classify+0x21c/0x3c0 sch_handle_ingress.constprop.0+0x2c5/0x500 __netif_receive_skb_core.constprop.0+0xb25/0x1510 __netif_receive_skb_list_core+0x220/0x4c0 netif_receive_skb_list_internal+0x446/0x620 napi_complete_done+0x157/0x3d0 gro_cell_poll+0xcf/0x100 __napi_poll+0x65/0x310 net_rx_action+0x30c/0x5c0 __do_softirq+0x14f/0x491 Freed by task 6469: kasan_save_stack+0x38/0x70 kasan_set_track+0x25/0x40 kasan_save_free_info+0x2b/0x60 ____kasan_slab_free+0x180/0x1f0 __kasan_slab_free+0x12/0x30 slab_free_freelist_hook+0xd2/0x1a0 __kmem_cache_free+0x1a2/0x2f0 kfree+0x78/0x120 nf_conntrack_free+0x74/0x130 [nf_conntrack] nf_ct_destroy+0xb2/0x140 [nf_conntrack] __nf_ct_resolve_clash+0x529/0x5d0 [nf_conntrack] nf_ct_resolve_clash+0xf6/0x490 [nf_conntrack] __nf_conntrack_confirm+0x2c6/0x770 [nf_conntrack] tcf_ct_act+0x12ad/0x1350 [act_ct] tcf_action_exec+0xf8/0x1f0 fl_classify+0x355/0x360 [cls_flower] __tcf_classify+0x1fd/0x330 tcf_classify+0x21c/0x3c0 sch_handle_ingress.constprop.0+0x2c5/0x500 __netif_receive_skb_core.constprop.0+0xb25/0x1510 __netif_receive_skb_list_core+0x220/0x4c0 netif_receive_skb_list_internal+0x446/0x620 napi_complete_done+0x157/0x3d0 gro_cell_poll+0xcf/0x100 __napi_poll+0x65/0x310 net_rx_action+0x30c/0x5c0 __do_softirq+0x14f/0x491 The ct may be dropped if a clash has been resolved but is still passed to the tcf_ct_flow_table_process_conn function for further usage. This issue can be fixed by retrieving ct from skb again after confirming conntrack. Fixes: 0cc254e5aa37 ("net/sched: act_ct: Offload connections with commit action") Co-developed-by: Gerald Yang Signed-off-by: Gerald Yang Signed-off-by: Chengen Du Link: https://patch.msgid.link/20240710053747.13223-1-chengen.du@canonical.com Signed-off-by: Paolo Abeni (cherry picked from commit 26488172b0292bed837b95a006a3f3431d1898c3) Signed-off-by: Gerald Yang --- net/sched/act_ct.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c index b4c42b257ae7..9e7f78a8132a 100644 --- a/net/sched/act_ct.c +++ b/net/sched/act_ct.c @@ -1039,6 +1039,14 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, */ if (nf_conntrack_confirm(skb) != NF_ACCEPT) goto drop; + + /* The ct may be dropped if a clash has been resolved, + * so it's necessary to retrieve it from skb again to + * prevent UAF. + */ + ct = nf_ct_get(skb, &ctinfo); + if (!ct) + skip_add = true; } if (!skip_add)