diff mbox series

[RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated

Message ID 20240216015257.10020-1-stephen@networkplumber.org (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series [RFC] netlink: check for NULL attribute in nla_parse_nested_deprecated | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 3786 this patch: 3786
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 3 maintainers not CCed: pabeni@redhat.com kuba@kernel.org edumazet@google.com
netdev/build_clang success Errors and warnings before: 1017 this patch: 1017
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 4002 this patch: 4002
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 11 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Stephen Hemminger Feb. 16, 2024, 1:52 a.m. UTC
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(+)

Comments

Simon Horman Feb. 19, 2024, 12:56 p.m. UTC | #1
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?
Jakub Kicinski Feb. 19, 2024, 7:13 p.m. UTC | #2
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.
diff mbox series

Patch

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);
 }