Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp736858rwb; Wed, 26 Jul 2023 01:52:33 -0700 (PDT) X-Google-Smtp-Source: APBJJlFzKhtyebG4ZqhZiW7ZCkJx3sTEh2sWiJ/PkmfvQG25vgI+kRZZ12CdmjwIIKfv4+fs/Uts X-Received: by 2002:a05:6a20:748e:b0:134:d4d3:f0aa with SMTP id p14-20020a056a20748e00b00134d4d3f0aamr1139920pzd.2.1690361553399; Wed, 26 Jul 2023 01:52:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690361553; cv=none; d=google.com; s=arc-20160816; b=GpkU6am67L6s4miZkvhHNW7Jw+/hpPBHmsHTKnbeS4whABSo8xGfLAhUtSnKy8OBWY WAvPBfM95lz81zS60rc4UETpdsaERAr0nbbsWLGWhkwkcVBvwmBwKNCke7zaGa/1oA6J ly/lXabBUwc6imeednBnYqDwPC/M/bFyuMufhg9BDkXG4KM7gsxiJoxT8T68yXaQvsFy EwMRoh+Y5XhgP2h+soNqL2X5dyJfK0O0Ag2eZ7RC1zSQqsUtJKz/2wiNUzNBOjIk0tR9 NvRU9qwkR2Mc+MyY7CaTIowEIKf7dyA3mDrVdfbii0t4wC8D2E+BA9U6ixm48KoeWZsq Gt+w== 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=3/pmzLUFc4Rpi4i2YW+o6hL7Ke5McTxlGPLGmTvULrM=; fh=KlB3vWSu0lIFrdWbXRXV7nsdWp8gJ5u2+BS0EOffnVo=; b=NnEjm9fiSPJ8ZbZuD2K4FToWumTJIH8/eIbPfz7FEGBtN5/5NvUBm1RcHqouCC8eJI gXxmmHXi783yuE8Soxc9mANZr+TPChOTZAQov/9Ve1QMuazdwry//S/MqmUiq65Hmav8 waqPilFqnaFHh8NiCfQVpivlcRPZjDYIkv8GpyhaH4zGLifgOk/ACjIGMjc5GD/8EO7C nwgHh3eWs0LrP5C8JUBW7NSNxxg9X2/D+EC0/pmFuXrsWsL9oemAz5NJKt62pWkI2+LO eXI8vTFsCdBYf1Tg9u3fku9yiITbsFaZYIDMwfQad6jqc0cfTcNdu5bbkagQJEIJ6MAe xBsg== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv188-20020a632ec5000000b0055c1f8b4613si11585638pgb.723.2023.07.26.01.52.20; Wed, 26 Jul 2023 01:52:33 -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 S231183AbjGZIHm (ORCPT + 99 others); Wed, 26 Jul 2023 04:07:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230462AbjGZIHR (ORCPT ); Wed, 26 Jul 2023 04:07:17 -0400 Received: from zg8tmja2lje4os4yms4ymjma.icoremail.net (zg8tmja2lje4os4yms4ymjma.icoremail.net [206.189.21.223]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 758F4449C; Wed, 26 Jul 2023 00:59:01 -0700 (PDT) Received: from localhost.localdomain (unknown [125.120.144.234]) by mail-app2 (Coremail) with SMTP id by_KCgDHr37t0MBk3WGYCg--.38569S4; Wed, 26 Jul 2023 15:53:18 +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 net v3] rtnetlink: let rtnl_bridge_setlink checks IFLA_BRIDGE_MODE length Date: Wed, 26 Jul 2023 15:53:14 +0800 Message-Id: <20230726075314.1059224-1-linma@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: by_KCgDHr37t0MBk3WGYCg--.38569S4 X-Coremail-Antispam: 1UD129KBjvJXoW7Zr4rAw15KF4Dtr45WrWkXrb_yoW8tF1xp3 W8Ka47GF1DXrn2vFsrZ3W7Za4fZFZ3KrW5Gr42ywn2yr1jqFyUCr9Fkrn09ry7CFsaqFy5 tr4DCFyYvw1DXFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvC14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2 Y2ka0xkIwI1lc2xSY4AK67AK6r4xMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r 1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CE b7AF67AKxVW8ZVWrXwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0x vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAI cVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kf nxnUUI43ZEXa7VUbsmitUUUUU== X-CM-SenderInfo: qtrwiiyqvtljo62m3hxhgxhubq/ Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 V2 -> V3: add net subject prefix and Acked-by tag 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; } } } -- 2.17.1