From patchwork Wed Aug 2 21:23:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rishabh Bhatnagar X-Patchwork-Id: 130164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp747966vqx; Wed, 2 Aug 2023 14:53:46 -0700 (PDT) X-Google-Smtp-Source: APBJJlELWCNtIxy6Rh4GkS/58B5NBpy1rvHndPjvyDrVquJU2R0enDcHwhNg9o2cz5h3Gb5iVePx X-Received: by 2002:a17:906:51d6:b0:99c:5711:3187 with SMTP id v22-20020a17090651d600b0099c57113187mr897524ejk.6.1691013225929; Wed, 02 Aug 2023 14:53:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691013225; cv=none; d=google.com; s=arc-20160816; b=1H6uSieWqIISB1QZlMTsLScC28LWRemgKDD1I4s10NjOt96HA/fNKv5dxdcumyqa6e ZoeLxuIL07It1tBlVGX8Lg4X2FtAn7n/Hg6IP4fLMDX+oVaF0Eh+2fHFKqgVmEo1Dgl6 e076fUxOP0J45+oQEEjVYH7qaXRttFRyFDQjfuNi0McYhENeNZxUw6Xn5FfK3W5zR+zU /RkjBPxP2p3jFbzy+4aVbG8Kz80hYNRyIvf8UolAxJA1OatwUrFujSe3N835OTtMkVpZ dklxBnmnizdawrz3X7tEgVKHltwSk1RfRU1lo5EBeTC7f/7ePeRZeqNFe9moyj2wLN29 ZhXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=UGL2vJ6MLNe12v+8D3skuK9/dM3RwPLrOS4RD+wo23g=; fh=VSOiUP4Jf+JnnqbJFY/KTfM2TgEILGh5Bt1rk96WVUA=; b=xmcVBGDDn8H7H3dDhqyR/oZ83q/lE8y33kpISqglxHiQAg6MiNR8KR/KPmnyiL8hkI Xq0D7eGE8Ovuo+67ZtoIn4uN2RWgbLTzs0tAjC31BxI2KqfUcOh5P4KiIA59hsPpPbn4 LuqTfbnWp5lD/RrqxmB+Otk4q4bKMfL7mPrMYB2xDlS2mniv/RJAmK9vPnKIcP5/uK14 Ibec2Xi8Cig6R3Q7nn+ZRwGNapaifY69EyXe9PeeGzWiQp8HKFMyuZznvGvxwhW/gRgq mwHPxL1qZg/5dtdM86Z87qOkfzZ4LMDFKPysYa+uqiouOIRQ1bmQ72dKgWYRxL9T/8qb zybg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=Ku1U123V; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rn3-20020a170906d92300b0098e47bbff3csi10983153ejb.297.2023.08.02.14.53.22; Wed, 02 Aug 2023 14:53:45 -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=@amazon.com header.s=amazon201209 header.b=Ku1U123V; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231405AbjHBVYK (ORCPT + 99 others); Wed, 2 Aug 2023 17:24:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbjHBVYI (ORCPT ); Wed, 2 Aug 2023 17:24:08 -0400 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11E8CAC; Wed, 2 Aug 2023 14:24:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1691011447; x=1722547447; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=UGL2vJ6MLNe12v+8D3skuK9/dM3RwPLrOS4RD+wo23g=; b=Ku1U123VDtKWrc6vTmdiowKBuBCaZ6iXvCNNQwZ/Mrai77BDapiNpnAU HbH59f27LawLohhjOoa1Sy9BiF+RW4W/io0sxVSjL99OcG1rpMyNNZB4Y 6hyyad8Q56DFXB+SvaJBYTNhoD9app/sEMVNWxFmC/1TmPUShaILEU41T 0=; X-IronPort-AV: E=Sophos;i="6.01,250,1684800000"; d="scan'208";a="343285625" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-245b69b1.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Aug 2023 21:24:06 +0000 Received: from EX19MTAUWC001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-iad-1e-m6i4x-245b69b1.us-east-1.amazon.com (Postfix) with ESMTPS id 0E8A4344A1E; Wed, 2 Aug 2023 21:24:04 +0000 (UTC) Received: from EX19D002UWC002.ant.amazon.com (10.13.138.166) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 2 Aug 2023 21:24:04 +0000 Received: from EX19MTAUEA001.ant.amazon.com (10.252.134.203) by EX19D002UWC002.ant.amazon.com (10.13.138.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 2 Aug 2023 21:24:04 +0000 Received: from dev-dsk-risbhat-2b-8bdc64cd.us-west-2.amazon.com (10.189.73.169) by mail-relay.amazon.com (10.252.134.102) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 2 Aug 2023 21:24:03 +0000 Received: by dev-dsk-risbhat-2b-8bdc64cd.us-west-2.amazon.com (Postfix, from userid 22673075) id 85C4FE4C; Wed, 2 Aug 2023 21:24:03 +0000 (UTC) From: Rishabh Bhatnagar To: , CC: , , , , Eric Dumazet , Jamal Hadi Salim , Rishabh Bhatnagar Subject: [PATCH 4.19] net/sched: cls_u32: Fix reference counter leak leading to overflow Date: Wed, 2 Aug 2023 21:23:55 +0000 Message-ID: <20230802212355.27982-1-risbhat@amazon.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773154789118765444 X-GMAIL-MSGID: 1773155884226468511 From: Lee Jones Upstream commit 04c55383fa5689357bcdd2c8036725a55ed632bc. In the event of a failure in tcf_change_indev(), u32_set_parms() will immediately return without decrementing the recently incremented reference counter. If this happens enough times, the counter will rollover and the reference freed, leading to a double free which can be used to do 'bad things'. In order to prevent this, move the point of possible failure above the point where the reference counter is incremented. Also save any meaningful return values to be applied to the return data at the appropriate point in time. This issue was caught with KASAN. Fixes: 705c7091262d ("net: sched: cls_u32: no need to call tcf_exts_change for newly allocated struct") Suggested-by: Eric Dumazet Signed-off-by: Lee Jones Reviewed-by: Eric Dumazet Acked-by: Jamal Hadi Salim Signed-off-by: David S. Miller Signed-off-by: Rishabh Bhatnagar --- net/sched/cls_u32.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c index d30256ac3537..ee8ef606a8e9 100644 --- a/net/sched/cls_u32.c +++ b/net/sched/cls_u32.c @@ -778,11 +778,22 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp, struct netlink_ext_ack *extack) { int err; +#ifdef CONFIG_NET_CLS_IND + int ifindex = -1; +#endif err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr, extack); if (err < 0) return err; +#ifdef CONFIG_NET_CLS_IND + if (tb[TCA_U32_INDEV]) { + ifindex = tcf_change_indev(net, tb[TCA_U32_INDEV], extack); + if (ifindex < 0) + return -EINVAL; + } +#endif + if (tb[TCA_U32_LINK]) { u32 handle = nla_get_u32(tb[TCA_U32_LINK]); struct tc_u_hnode *ht_down = NULL, *ht_old; @@ -814,13 +825,8 @@ static int u32_set_parms(struct net *net, struct tcf_proto *tp, } #ifdef CONFIG_NET_CLS_IND - if (tb[TCA_U32_INDEV]) { - int ret; - ret = tcf_change_indev(net, tb[TCA_U32_INDEV], extack); - if (ret < 0) - return -EINVAL; - n->ifindex = ret; - } + if (ifindex >= 0) + n->ifindex = ifindex; #endif return 0; }