From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE2E9C04A95 for ; Wed, 28 Sep 2022 18:35:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231652AbiI1SfZ (ORCPT ); Wed, 28 Sep 2022 14:35:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230340AbiI1SfX (ORCPT ); Wed, 28 Sep 2022 14:35:23 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA0BE286FF for ; Wed, 28 Sep 2022 11:35:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AC07DB82186 for ; Wed, 28 Sep 2022 18:35:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D8094C433C1; Wed, 28 Sep 2022 18:35:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664390117; bh=ffCylb8tV986+n0BpD+n1ShDQKMVVkXi51coeGnyDqU=; h=From:To:Cc:Subject:Date:From; b=lGH55AttciAIBz6KUcmSO2k8TmloJk6KAsaIkdWsKrgjwGEpc9c9oDBhqjr3Ghsbp +M5D8axqcFqpkb1ns7GxYtJLSbWHYhKD25D0/N0os1pxodX5l+U65XxlS0WJP+m4CP +D6b6V2mH9i6Qjn2QLuUhX9JtYSDwaTdabn5wZ+YHui3qoiZFbQSTTaeuYZzL1zIrL q1Gf6w2XhlZ93Hjo6O1lrWPGu8JDfzIkSp6F7AUMQtsTa/YKnkOVQQCcWQEsfGDMtg e9L2FEpYv3ox6VhhlcvjwdMqbVmtJRhzH6HAMxvC5ELbGdZxThe/m9smW+xKQAmfao pysbNA1L6MGNw== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, Jakub Kicinski , Florent Fourcot , Nikolay Aleksandrov , Nicolas Dichtel , Johannes Berg , Pablo Neira Ayuso , Florian Westphal , Jamal Hadi Salim , Jacob Keller , Guillaume Nault , Hangbin Liu Subject: [PATCH net] genetlink: reject use of nlmsg_flags for new commands Date: Wed, 28 Sep 2022 11:35:15 -0700 Message-Id: <20220928183515.1063481-1-kuba@kernel.org> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Commit 9c5d03d36251 ("genetlink: start to validate reserved header bytes") introduced extra validation for genetlink headers. We had to gate it to only apply to new commands, to maintain bug-wards compatibility. Use this opportunity (before the new checks make it to Linus's tree) to add more conditions. Validate that Generic Netlink families do not use nlmsg_flags outside of the well-understood set. Link: https://lore.kernel.org/all/20220928073709.1b93b74a@kernel.org/ Signed-off-by: Jakub Kicinski -- CC: Florent Fourcot CC: Nikolay Aleksandrov CC: Nicolas Dichtel CC: Johannes Berg CC: Pablo Neira Ayuso CC: Florian Westphal CC: Jamal Hadi Salim CC: Jacob Keller CC: Guillaume Nault CC: Hangbin Liu --- net/netlink/genetlink.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c index 7c136de117eb..39b7c00e4cef 100644 --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -739,6 +739,36 @@ static int genl_family_rcv_msg_doit(const struct genl_family *family, return err; } +static int genl_header_check(const struct genl_family *family, + struct nlmsghdr *nlh, struct genlmsghdr *hdr, + struct netlink_ext_ack *extack) +{ + u16 flags; + + /* Only for commands added after we started validating */ + if (hdr->cmd < family->resv_start_op) + return 0; + + if (hdr->reserved) { + NL_SET_ERR_MSG(extack, "genlmsghdr.reserved field is not 0"); + return -EINVAL; + } + + /* Old netlink flags have pretty loose semantics, allow only the flags + * consumed by the core where we can enforce the meaning. + */ + flags = nlh->nlmsg_flags; + if ((flags & NLM_F_DUMP) == NLM_F_DUMP) /* DUMP is 2 bits */ + flags &= ~NLM_F_DUMP; + if (flags & ~(NLM_F_REQUEST | NLM_F_ACK | NLM_F_ECHO)) { + NL_SET_ERR_MSG(extack, + "ambiguous or reserved bits set in nlmsg_flags"); + return -EINVAL; + } + + return 0; +} + static int genl_family_rcv_msg(const struct genl_family *family, struct sk_buff *skb, struct nlmsghdr *nlh, @@ -757,7 +787,7 @@ static int genl_family_rcv_msg(const struct genl_family *family, if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) return -EINVAL; - if (hdr->cmd >= family->resv_start_op && hdr->reserved) + if (genl_header_check(family, nlh, hdr, extack)) return -EINVAL; if (genl_get_cmd(hdr->cmd, family, &op)) -- 2.37.3