From fac357ac8b299af9f042d5303400da16a0244bc2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 18 Feb 2019 10:05:40 +0100 Subject: [PATCH] platform/netlink: require valid nla argument for nla_get_u64() nla_get_u64() was unlike all other nla_get_u*() implementations, in that it would allow for a missing/invalid nla argument, and return 0. Don't do this. For one, don't behave different than other getters. Also, there really is no space to report errors. Hence, the caller must make sure that the attribute is present and suitable -- like for other nla_get_*() functions. None of the callers relied on being able to pass NULL attribute. Also, inline the function and use unaligned_read_ne64(). That is our preferred way for reading unaligned data, not memcpy(). --- src/platform/nm-netlink.c | 12 ------------ src/platform/nm-netlink.h | 10 +++++++++- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/platform/nm-netlink.c b/src/platform/nm-netlink.c index eb3f1a9f82..9a2639235e 100644 --- a/src/platform/nm-netlink.c +++ b/src/platform/nm-netlink.c @@ -366,18 +366,6 @@ nlmsg_put (struct nl_msg *n, uint32_t pid, uint32_t seq, return nlh; } -uint64_t -nla_get_u64 (const struct nlattr *nla) -{ - uint64_t tmp = 0; - - if ( nla - && nla_len (nla) >= sizeof (tmp)) - memcpy (&tmp, nla_data (nla), sizeof (tmp)); - - return tmp; -} - size_t nla_strlcpy (char *dst, const struct nlattr *nla, diff --git a/src/platform/nm-netlink.h b/src/platform/nm-netlink.h index 3c2c784ad6..840e56d8e4 100644 --- a/src/platform/nm-netlink.h +++ b/src/platform/nm-netlink.h @@ -25,6 +25,8 @@ #include #include +#include "nm-utils/unaligned.h" + /*****************************************************************************/ #define NLMSGERR_ATTR_UNUSED 0 @@ -201,7 +203,13 @@ nla_get_s32 (const struct nlattr *nla) return *((const int32_t *) nla_data (nla)); } -uint64_t nla_get_u64 (const struct nlattr *nla); +static inline uint64_t +nla_get_u64 (const struct nlattr *nla) +{ + nm_assert (nla_len (nla) >= sizeof (uint64_t)); + + return unaligned_read_ne64 (nla_data (nla)); +} static inline char * nla_get_string (const struct nlattr *nla)