From patchwork Tue Jul 25 05:57:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Ma X-Patchwork-Id: 125348 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp2278474vqg; Mon, 24 Jul 2023 23:47:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlF8ZWl4SMi0tGp+oIK0dCyD9UfMAyzL06TGRL5mukz487yfar+kMQa/59fiMU09uh2bS7hm X-Received: by 2002:a05:6402:1295:b0:51e:4e77:3347 with SMTP id w21-20020a056402129500b0051e4e773347mr8564931edv.41.1690267654756; Mon, 24 Jul 2023 23:47:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690267654; cv=none; d=google.com; s=arc-20160816; b=n+SfgTMsr16pB3wocDpmTtXjh2INxTG6YXuiHF7bSYRil9vOPGd6yF1bsjKlskCcKq 9tlMQ1oYHNWrP+q8Etw3STI31cJB5x62y8fJxTyrPr4TDgdSz2bKZZLHlXzMP5wTMlQp solE/hhsG6LJlJ70PqvWyKYhnHRuBWLo/2Q6iInY+iASs86MyKGm71X9niu4AjTt3ujg EysRMOs3IhBFURjzvR/7dU+CBhCwKP4RDQcyJEYKACeNVAag0QdIM0IIHwD/k8U7TnsI hoK9M4FxvMmyYKbMdw8T87oSDQkW958w/QjSAlYq/Uzc72pK88W1j4CUbqNRBbabpr8Y csOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=qiJl0C4H11tn5mnA5JLbWK/uTuelu2OFjFAo+gIC3Hc=; fh=KlB3vWSu0lIFrdWbXRXV7nsdWp8gJ5u2+BS0EOffnVo=; b=y4emnaoe3kSMbmdfaPhhTZjXLg9rXb9a7NwhA+KK7VGINqoSbMk0R/fbce25+uifqG oczo72pJNK6joRbBrKhBJtVrY7FlcKFejgU3zIKcDTRctsVtxqwotKwjnnSv5E0jSyKa kLsVFIqWaAkQmZ09GfgcfVHZencigqS2FZxAxDs6RbX+LhT51I9o6lpnvg+DDg/Yen5Q suO4M8Y5XuWPrH/k+hidgx9qOliFJTkIbA+tGJ48Gi5y0MJRmkLMQuuVVfIA0WGfmSOY g3rfMq0Pr2kYNt9sOoxfvDpMB+sVbp8TdkDzYwU4YAJuWDk0JYYVGxl7RHYQ2hIDLby7 +fxQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e8-20020a056402148800b005222afcd97asi2883801edv.620.2023.07.24.23.47.11; Mon, 24 Jul 2023 23:47:34 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231839AbjGYF5l (ORCPT + 99 others); Tue, 25 Jul 2023 01:57:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229780AbjGYF5j (ORCPT ); Tue, 25 Jul 2023 01:57:39 -0400 X-Greylist: delayed 554 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 24 Jul 2023 22:57:35 PDT Received: from sgoci-sdnproxy-4.icoremail.net (sgoci-sdnproxy-4.icoremail.net [129.150.39.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9DF34E69; Mon, 24 Jul 2023 22:57:35 -0700 (PDT) Received: from localhost.localdomain (unknown [183.128.129.228]) by mail-app2 (Coremail) with SMTP id by_KCgD3TRg0ZL9kpBKFCg--.13835S4; Tue, 25 Jul 2023 13:57:08 +0800 (CST) From: Lin Ma To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, razor@blackwall.org, idosch@nvidia.com, lucien.xin@gmail.com, liuhangbin@gmail.com, edwin.peer@broadcom.com, jiri@resnulli.us, md.fahad.iqbal.polash@intel.com, anirudh.venkataramanan@intel.com, jeffrey.t.kirsher@intel.com, neerav.parikh@intel.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Lin Ma Subject: [PATCH v2] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length Date: Tue, 25 Jul 2023 13:57:06 +0800 Message-Id: <20230725055706.498774-1-linma@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: by_KCgD3TRg0ZL9kpBKFCg--.13835S4 X-Coremail-Antispam: 1UD129KBjvJXoW7Zr4rAw15KF4Dtr45WrWkXrb_yoW8uF4fp3 W8Ka47JF4DXrn2vFsrZF17Xa4fZFZ3KFW5Gr42ywn2yr1jqFyUCryqkrn09ry3CFsaqa45 tr4DCFyYvw1DWFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvK14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2 Y2ka0xkIwI1lc2xSY4AK67AK6r4rMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r 1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CE b7AF67AKxVW8ZVWrXwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0x vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAI cVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxh VjvjDU0xZFpf9x0JU9189UUUUU= X-CM-SenderInfo: qtrwiiyqvtljo62m3hxhgxhubq/ X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,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: 1772358321103315862 X-GMAIL-MSGID: 1772374096382281447 There are totally 9 ndo_bridge_setlink handlers in the current kernel, which are 1) bnxt_bridge_setlink, 2) be_ndo_bridge_setlink 3) i40e_ndo_bridge_setlink 4) ice_bridge_setlink 5) ixgbe_ndo_bridge_setlink 6) mlx5e_bridge_setlink 7) nfp_net_bridge_setlink 8) qeth_l2_bridge_setlink 9) br_setlink. By investigating the code, we find that 1-7 parse and use nlattr IFLA_BRIDGE_MODE but 3 and 4 forget to do the nla_len check. This can lead to an out-of-attribute read and allow a malformed nlattr (e.g., length 0) to be viewed as a 2 byte integer. To avoid such issues, also for other ndo_bridge_setlink handlers in the future. This patch adds the nla_len check in rtnl_bridge_setlink and does an early error return if length mismatches. To make it works, the break is removed from the parsing for IFLA_BRIDGE_FLAGS to make sure this nla_for_each_nested iterates every attribute. Fixes: b1edc14a3fbf ("ice: Implement ice_bridge_getlink and ice_bridge_setlink") Fixes: 51616018dd1b ("i40e: Add support for getlink, setlink ndo ops") Suggested-by: Jakub Kicinski Signed-off-by: Lin Ma Acked-by: Nikolay Aleksandrov --- V1 -> V2: removes the break in parsing for IFLA_BRIDGE_FLAGS suggested by Hangbin Liu net/core/rtnetlink.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 3ad4e030846d..aef25aa5cf1d 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -5140,13 +5140,17 @@ static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); if (br_spec) { nla_for_each_nested(attr, br_spec, rem) { - if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { + if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !have_flags) { if (nla_len(attr) < sizeof(flags)) return -EINVAL; have_flags = true; flags = nla_get_u16(attr); - break; + } + + if (nla_type(attr) == IFLA_BRIDGE_MODE) { + if (nla_len(attr) < sizeof(u16)) + return -EINVAL; } } }