Skip to contentSkip to conversation

genetlink: reject use of nlmsg_flags for new commands

PATCH Superseded Jakub Kicinski 6 participants 7 messages Sep 28, 2022 – Sep 29, 2022

Subsystems (2)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 genetlink: reject use of nlmsg_flags for new commands

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.

--
CC: Florent Fourcot <[email protected]>
CC: Nikolay Aleksandrov <[email protected]>
CC: Nicolas Dichtel <[email protected]>
CC: Johannes Berg <[email protected]>
CC: Pablo Neira Ayuso <[email protected]>
CC: Florian Westphal <[email protected]>
CC: Jamal Hadi Salim <[email protected]>
CC: Jacob Keller <[email protected]>
CC: Guillaume Nault <[email protected]>
CC: Hangbin Liu <[email protected]>

---
 net/netlink/genetlink.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
net/netlink/genetlink.c +31 -1
index 7c136de117eb..39b7c00e4cef 100644
@@ -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))
Signature · 1 line
2.37.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jakub Kicinski

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.

Makes sense.

Thanks for doing this :)

johannes

Acked-by on v1 1/1
#2 lore raw
In reply to Jakub Kicinski
Quoted 22 lines · Jakub Kicinski

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.

--
CC: Florent Fourcot <[email protected]>
CC: Nikolay Aleksandrov <[email protected]>
CC: Nicolas Dichtel <[email protected]>
CC: Johannes Berg <[email protected]>
CC: Pablo Neira Ayuso <[email protected]>
CC: Florian Westphal <[email protected]>
CC: Jamal Hadi Salim <[email protected]>
CC: Jacob Keller <[email protected]>
CC: Guillaume Nault <[email protected]>
CC: Hangbin Liu <[email protected]>

Quoted 4 lines
---
 net/netlink/genetlink.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

Thanks!

Reviewed-by on v1 1/1
#3 lore raw
In reply to Jakub Kicinski

Le 28/09/2022 à 20:35, Jakub Kicinski a écrit :

Quoted 11 lines

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.

Quoted 11 lines

--
CC: Florent Fourcot <[email protected]>
CC: Nikolay Aleksandrov <[email protected]>
CC: Nicolas Dichtel <[email protected]>
CC: Johannes Berg <[email protected]>
CC: Pablo Neira Ayuso <[email protected]>
CC: Florian Westphal <[email protected]>
CC: Jamal Hadi Salim <[email protected]>
CC: Jacob Keller <[email protected]>
CC: Guillaume Nault <[email protected]>
CC: Hangbin Liu <[email protected]>

Thanks!

author maintainer
#4 lore raw
In reply to Jakub Kicinski
Quoted 1 line · On Wed, 28 Sep 2022 11:35:15 -0700 Jakub Kicinski wrote:

[PATCH net] genetlink: reject use of nlmsg_flags for new commands

I managed to mis-type the subject, should have been net-next, obviously. I'll resend tomorrow.

Reviewed-by on v1 1/1
#5 lore raw
In reply to Jakub Kicinski
Quoted 8 lines · Jakub Kicinski

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.

Reviewed-by on v1 1/1
#6 lore raw
In reply to Jakub Kicinski
Quoted 62 lines

-----Original Message-----
From: Jakub Kicinski <[email protected]>
Sent: Wednesday, September 28, 2022 11:35 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; Jakub Kicinski <[email protected]>; Florent Fourcot <[email protected]>; Nikolay Aleksandrov <[email protected]>; Nicolas Dichtel <[email protected]>; Johannes Berg <[email protected]>; Pablo Neira Ayuso <[email protected]>; Florian Westphal <[email protected]>; Jamal Hadi Salim <[email protected]>; Keller, Jacob E <[email protected]>; Guillaume Nault <[email protected]>; Hangbin Liu <[email protected]>
Subject: [PATCH net] genetlink: reject use of nlmsg_flags for new commands

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.

--
CC: Florent Fourcot <[email protected]>
CC: Nikolay Aleksandrov <[email protected]>
CC: Nicolas Dichtel <[email protected]>
CC: Johannes Berg <[email protected]>
CC: Pablo Neira Ayuso <[email protected]>
CC: Florian Westphal <[email protected]>
CC: Jamal Hadi Salim <[email protected]>
CC: Jacob Keller <[email protected]>
CC: Guillaume Nault <[email protected]>
CC: Hangbin Liu <[email protected]>

---
 net/netlink/genetlink.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
net/netlink/genetlink.c +11 -0
index 7c136de117eb..39b7c00e4cef 100644
@@ -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) {

net/netlink/genetlink.c static int genl_family_rcv_msg_doit(const struct · line 763

Quoted 10 lines
net/netlink/genetlink.c +10 -0
+		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;

Ok so this check is making sure that flags has BOTH NLM_F_ROOT and NLM_F_MATCH or neither, but not only one of them?

That's why it can't go in the following section, because we want to allow NLM_F_DUMP but not NLM_F_ROOT or NLM_F_MATCH on its own.

Ok.

Quoted 21 lines
net/netlink/genetlink.c +9 -0
+	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,
net/netlink/genetlink.c +1 -1
 			       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;
 
Quoted 3 lines

if (genl_get_cmd(hdr->cmd, family, &op))
--
2.37.3

Looks good to me!