Skip to contentSkip to conversation

net_sched: add max len check for TCA_KIND

PATCH Applied Cong Wang 5 participants 9 messages Sep 18, 2019 – Oct 4, 2019

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 net_sched: add max len check for TCA_KIND

The TCA_KIND attribute is of NLA_STRING which does not check the NUL char. KMSAN reported an uninit-value of TCA_KIND which is likely caused by the lack of NUL.

Change it to NLA_NUL_STRING and add a max len too.

Fixes 8b4c3cdd9dd8 ("net: sched: Add policy validation for tc attributes")
Reported-and-tested-by [email protected]
Cc David Ahern <[email protected]>
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
---
 net/sched/sch_api.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/sched/sch_api.c +2 -1
index 1047825d9f48..81d58b280612 100644
@@ -1390,7 +1390,8 @@ check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
 }
 
 const struct nla_policy rtm_tca_policy[TCA_MAX + 1] = {
-	[TCA_KIND]		= { .type = NLA_STRING },
+	[TCA_KIND]		= { .type = NLA_NUL_STRING,
+				    .len = IFNAMSIZ - 1 },
 	[TCA_RATE]		= { .type = NLA_BINARY,
 				    .len = sizeof(struct tc_estimator) },
 	[TCA_STAB]		= { .type = NLA_NESTED },
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Cong Wang

The TCA_KIND attribute is of NLA_STRING which does not check the NUL char. KMSAN reported an uninit-value of TCA_KIND which is likely caused by the lack of NUL.

Change it to NLA_NUL_STRING and add a max len too.

Fixes 8b4c3cdd9dd8 ("net: sched: Add policy validation for tc attributes")

The commit referenced here did not introduce the ability to go beyond memory boundaries with string comparisons. Rather, it was not complete solution for attribute validation. I say that wrt to the fix getting propagated to the correct stable releases.

Quoted 1 line
Reported-and-tested-by [email protected]

What is the actual sysbot report?

Quoted 14 lines
Cc David Ahern <[email protected]>
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
---
 net/sched/sch_api.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/sched/sch_api.c +0 -0
index 1047825d9f48..81d58b280612 100644

net/sched/sch_api.c check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w) · line 1397

Quoted 9 lines
net/sched/sch_api.c +2 -1
@@ -1390,7 +1390,8 @@ check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
 }
 
 const struct nla_policy rtm_tca_policy[TCA_MAX + 1] = {
-	[TCA_KIND]		= { .type = NLA_STRING },
+	[TCA_KIND]		= { .type = NLA_NUL_STRING,
+				    .len = IFNAMSIZ - 1 },
 	[TCA_RATE]		= { .type = NLA_BINARY,
 				    .len = sizeof(struct tc_estimator) },
 	[TCA_STAB]		= { .type = NLA_NESTED },

This is a better policy so for that:

author
#2 lore raw
Quoted 6 lines · David Ahern
Quoted 4 lines · Cong Wang

The TCA_KIND attribute is of NLA_STRING which does not check the NUL char. KMSAN reported an uninit-value of TCA_KIND which is likely caused by the lack of NUL.

Quoted 8 lines
Quoted 3 lines

Change it to NLA_NUL_STRING and add a max len too.

Fixes 8b4c3cdd9dd8 ("net: sched: Add policy validation for tc attributes")

The commit referenced here did not introduce the ability to go beyond memory boundaries with string comparisons. Rather, it was not complete solution for attribute validation. I say that wrt to the fix getting propagated to the correct stable releases.

I think this patch should be backported to wherever commit 8b4c3cdd9dd8 goes, this is why I picked it as Fixes.

Quoted 4 lines
Quoted 2 lines
Reported-and-tested-by [email protected]

What is the actual sysbot report?

https://marc.info/?l=linux-kernel&m=156862916112881&w=2

#3 lore raw
Quoted 10 lines · Cong Wang
Quoted 9 lines · David Ahern
Quoted 7 lines · Cong Wang

The TCA_KIND attribute is of NLA_STRING which does not check the NUL char. KMSAN reported an uninit-value of TCA_KIND which is likely caused by the lack of NUL.

Change it to NLA_NUL_STRING and add a max len too.

Fixes 8b4c3cdd9dd8 ("net: sched: Add policy validation for tc attributes")
Quoted 7 lines
Quoted 4 lines

The commit referenced here did not introduce the ability to go beyond memory boundaries with string comparisons. Rather, it was not complete solution for attribute validation. I say that wrt to the fix getting propagated to the correct stable releases.

I think this patch should be backported to wherever commit 8b4c3cdd9dd8 goes, this is why I picked it as Fixes.

Applied, queued for 4.14+, thanks!

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Reported-and-tested-by [email protected]

What is the actual sysbot report?

https://marc.info/?l=linux-kernel&m=156862916112881&w=2

#4 lore raw
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · Cong Wang
Quoted 13 lines · David Ahern
Quoted 7 lines · Cong Wang

The TCA_KIND attribute is of NLA_STRING which does not check the NUL char. KMSAN reported an uninit-value of TCA_KIND which is likely caused by the lack of NUL.

Change it to NLA_NUL_STRING and add a max len too.

Fixes 8b4c3cdd9dd8 ("net: sched: Add policy validation for tc attributes")

The commit referenced here did not introduce the ability to go beyond memory boundaries with string comparisons. Rather, it was not complete solution for attribute validation. I say that wrt to the fix getting propagated to the correct stable releases.

Quoted 4 lines
Quoted 2 lines

I think this patch should be backported to wherever commit 8b4c3cdd9dd8 goes, this is why I picked it as Fixes.

Applied, queued for 4.14+, thanks!

Ahm, this breaks some user applications.

I'm getting "Attribute failed policy validation" extack error while adding ingress qdisc on an app using libmnl, because it just doesn't pack the null byte there if it uses mnl_attr_put_str():
https://git.netfilter.org/libmnl/tree/src/attr.c#n481
Unless it uses mnl_attr_put_strz() instead.

Though not sure who's to blame here, as one could argue that the app should have been using the latter in the first place, but well.. it worked and produced the right results.

Ditto for 199ce850ce11 ("net_sched: add policy validation for action attributes") on TCA_ACT_KIND.

#5 lore raw
Quoted 11 lines · Marcelo Ricardo Leitner
Quoted 1 line · Jakub Kicinski

Applied, queued for 4.14+, thanks!

Ahm, this breaks some user applications.

I'm getting "Attribute failed policy validation" extack error while adding ingress qdisc on an app using libmnl, because it just doesn't pack the null byte there if it uses mnl_attr_put_str():
https://git.netfilter.org/libmnl/tree/src/attr.c#n481
Unless it uses mnl_attr_put_strz() instead.

Quoted 6 lines

Though not sure who's to blame here, as one could argue that the app should have been using the latter in the first place, but well.. it worked and produced the right results.

Ditto for 199ce850ce11 ("net_sched: add policy validation for action attributes") on TCA_ACT_KIND.

Thanks for the report Marcelo! This netlink validation stuff is always super risky I figured better find out if something breaks sooner than later, hence the backport.

So if I'm understanding this would be the fix?

net/sched/act_api.c +1 -2
index 2558f00f6b3e..bcc1178ce50d 100644
@@ -832,8 +832,7 @@ static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
 }
 
 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
-       [TCA_ACT_KIND]          = { .type = NLA_NUL_STRING,
-                                   .len = IFNAMSIZ - 1 },
+       [TCA_ACT_KIND]          = { .type = NLA_STRING },
        [TCA_ACT_INDEX]         = { .type = NLA_U32 },
        [TCA_ACT_COOKIE]        = { .type = NLA_BINARY,
                                    .len = TC_COOKIE_MAX_SIZE },
net/sched/sch_api.c +1 -2
index 81d58b280612..1047825d9f48 100644
@@ -1390,8 +1390,7 @@ check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
 }
 
 const struct nla_policy rtm_tca_policy[TCA_MAX + 1] = {
-       [TCA_KIND]              = { .type = NLA_NUL_STRING,
-                                   .len = IFNAMSIZ - 1 },
+       [TCA_KIND]              = { .type = NLA_STRING },
        [TCA_RATE]              = { .type = NLA_BINARY,
                                    .len = sizeof(struct tc_estimator) },
        [TCA_STAB]              = { .type = NLA_NESTED },

Cong, are you planning to take a look? With this we have to find a different way to deal with the KMSAN report you mentioned :(

author
#6 lore raw
Quoted 17 lines · Jakub Kicinski
Quoted 15 lines · Marcelo Ricardo Leitner
Quoted 1 line · Jakub Kicinski

Applied, queued for 4.14+, thanks!

Ahm, this breaks some user applications.

I'm getting "Attribute failed policy validation" extack error while adding ingress qdisc on an app using libmnl, because it just doesn't pack the null byte there if it uses mnl_attr_put_str():
https://git.netfilter.org/libmnl/tree/src/attr.c#n481
Unless it uses mnl_attr_put_strz() instead.

Though not sure who's to blame here, as one could argue that the app should have been using the latter in the first place, but well.. it worked and produced the right results.

Quoted 8 lines
Quoted 2 lines

Ditto for 199ce850ce11 ("net_sched: add policy validation for action attributes") on TCA_ACT_KIND.

Thanks for the report Marcelo! This netlink validation stuff is always super risky I figured better find out if something breaks sooner than later, hence the backport.

So if I'm understanding this would be the fix?

Of course not, you just break KMSAN again. Please read the original report.

I will send a patch to use nla_strlcpy() instead, I think it will make everyone happy here.

Thanks.

#7 lore raw
Quoted 20 lines · Cong Wang
Quoted 19 lines · Jakub Kicinski
Quoted 17 lines · Marcelo Ricardo Leitner
Quoted 1 line · Jakub Kicinski

Applied, queued for 4.14+, thanks!

Ahm, this breaks some user applications.

I'm getting "Attribute failed policy validation" extack error while adding ingress qdisc on an app using libmnl, because it just doesn't pack the null byte there if it uses mnl_attr_put_str():
https://git.netfilter.org/libmnl/tree/src/attr.c#n481
Unless it uses mnl_attr_put_strz() instead.

Though not sure who's to blame here, as one could argue that the app should have been using the latter in the first place, but well.. it worked and produced the right results.

Ditto for 199ce850ce11 ("net_sched: add policy validation for action attributes") on TCA_ACT_KIND.

Quoted 8 lines
Quoted 5 lines

Thanks for the report Marcelo! This netlink validation stuff is always super risky I figured better find out if something breaks sooner than later, hence the backport.

So if I'm understanding this would be the fix?

Of course not, you just break KMSAN again. Please read the original report.

The fix for the regression. I'm establishing the rest of 199ce850ce11 ("net_sched: add policy validation for action attributes") is fine.

I mentioned this brings back the problem KMSAN reported in the part of the email you cut off. str*cpy is the obvious answer for reimplementing that fix.

Quoted 1 line

I will send a patch

Please do.

maintainer Acked-by on v1 1/1
#8 lore raw
In reply to Cong Wang
Quoted 8 lines · Thu, Sep 19, 2019 at 01:24:12AM CEST, [email protected] wrote:

The TCA_KIND attribute is of NLA_STRING which does not check the NUL char. KMSAN reported an uninit-value of TCA_KIND which is likely caused by the lack of NUL.

Change it to NLA_NUL_STRING and add a max len too.

Fixes 8b4c3cdd9dd8 ("net: sched: Add policy validation for tc attributes")
Reported-and-tested-by [email protected]