All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated
@ 2024-02-16  1:52 Stephen Hemminger
  2024-02-19 12:56 ` Simon Horman
  2024-02-19 19:13 ` Jakub Kicinski
  0 siblings, 2 replies; 3+ messages in thread
From: Stephen Hemminger @ 2024-02-16  1:52 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

Lots of code in network schedulers has the pattern:
	if (!nla) {
		NL_SET_ERR_MSG_MOD(extack, "missing attributes");
		return -EINVAL;
	}

	err = nla_parse_nested_deprecated(tb, TCA_CSUM_MAX, nla, csum_policy,
					  NULL);
	if (err < 0)
		return err;

The check for nla being NULL can be moved into nla_parse_nested_deprecated().
Which simplifies lots of places.

This is safer and won't break other places since:
	err = nla_parse_nested_deprecated(tb, TCA_XXXX, NULL, some_policy, NULL);
would have crashed kernel because nla_parse_deprecated derefences the nla
argument before calling __nla_parse.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 include/net/netlink.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/net/netlink.h b/include/net/netlink.h
index c19ff921b661..05d137283ab0 100644
--- a/include/net/netlink.h
+++ b/include/net/netlink.h
@@ -1322,6 +1322,11 @@ static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
 					      const struct nla_policy *policy,
 					      struct netlink_ext_ack *extack)
 {
+	if (!nla) {
+		NL_SET_ERR_MSG_MOD(extack, "missing attributes");
+		return -EINVAL;
+	}
+
 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
 			   NL_VALIDATE_LIBERAL, extack);
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated
  2024-02-16  1:52 [RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated Stephen Hemminger
@ 2024-02-19 12:56 ` Simon Horman
  2024-02-19 19:13 ` Jakub Kicinski
  1 sibling, 0 replies; 3+ messages in thread
From: Simon Horman @ 2024-02-19 12:56 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

On Thu, Feb 15, 2024 at 05:52:48PM -0800, Stephen Hemminger wrote:
> Lots of code in network schedulers has the pattern:
> 	if (!nla) {
> 		NL_SET_ERR_MSG_MOD(extack, "missing attributes");
> 		return -EINVAL;
> 	}
> 
> 	err = nla_parse_nested_deprecated(tb, TCA_CSUM_MAX, nla, csum_policy,
> 					  NULL);
> 	if (err < 0)
> 		return err;
> 
> The check for nla being NULL can be moved into nla_parse_nested_deprecated().
> Which simplifies lots of places.
> 
> This is safer and won't break other places since:
> 	err = nla_parse_nested_deprecated(tb, TCA_XXXX, NULL, some_policy, NULL);
> would have crashed kernel because nla_parse_deprecated derefences the nla
> argument before calling __nla_parse.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

Hi Stephen,

this seems like a good approach to me.
Would you also plan to update the schedules at some point?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated
  2024-02-16  1:52 [RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated Stephen Hemminger
  2024-02-19 12:56 ` Simon Horman
@ 2024-02-19 19:13 ` Jakub Kicinski
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2024-02-19 19:13 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

On Thu, 15 Feb 2024 17:52:48 -0800 Stephen Hemminger wrote:
> Lots of code in network schedulers has the pattern:
> 	if (!nla) {
> 		NL_SET_ERR_MSG_MOD(extack, "missing attributes");
> 		return -EINVAL;
> 	}
> 
> 	err = nla_parse_nested_deprecated(tb, TCA_CSUM_MAX, nla, csum_policy,
> 					  NULL);
> 	if (err < 0)
> 		return err;
> 
> The check for nla being NULL can be moved into nla_parse_nested_deprecated().
> Which simplifies lots of places.

If it's mainly TC which has this problem the fix belongs in TC,
not in the generic code.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-19 19:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-16  1:52 [RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated Stephen Hemminger
2024-02-19 12:56 ` Simon Horman
2024-02-19 19:13 ` Jakub Kicinski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.