diff options
| author | Jakub Kicinski <kuba@kernel.org> | 2025-04-15 18:28:57 +0300 |
|---|---|---|
| committer | Jakub Kicinski <kuba@kernel.org> | 2025-04-15 18:28:57 +0300 |
| commit | 2f8cc49d869cbfa6f72068774b3de420536c21ac (patch) | |
| tree | 0c732021c626771e27b8f53a49e269f288d13de8 /include | |
| parent | 0418711f60bbc6c35ba21fc78c6e3bc913036d2f (diff) | |
| parent | 8ff953036110bf1804b054b730300c8377bdde5e (diff) | |
| download | linux-2f8cc49d869cbfa6f72068774b3de420536c21ac.tar.xz | |
Merge branch 'net-introduce-nlmsg_payload-helper'
Breno Leitao says:
====================
net: Introduce nlmsg_payload helper
In the current codebase, there are multiple instances where the
structure size is checked before assigning it to a Netlink message. This
check is crucial for ensuring that the structure is correctly mapped
onto the Netlink message, providing a layer of security.
To streamline this process, Jakub Kicinski suggested creating a helper
function, `nlmsg_payload`, which verifies if the structure fits within
the message. If it does, the function returns the data; otherwise, it
returns NULL. This approach simplifies the code and reduces redundancy.
This patchset introduces the `nlmsg_payload` helper and updates several
parts of the code to use it. Further updates will follow in subsequent
patchsets.
v1: https://lore.kernel.org/20250411-nlmsg-v1-0-ddd4e065cb15@debian.org
====================
Link: https://patch.msgid.link/20250414-nlmsg-v2-0-3d90cb42c6af@debian.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Diffstat (limited to 'include')
| -rw-r--r-- | include/net/netlink.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/include/net/netlink.h b/include/net/netlink.h index 29e0db940382..82e07e272290 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -612,6 +612,22 @@ static inline int nlmsg_len(const struct nlmsghdr *nlh) } /** + * nlmsg_payload - message payload if the data fits in the len + * @nlh: netlink message header + * @len: struct length + * + * Returns: The netlink message payload/data if the length is sufficient, + * otherwise NULL. + */ +static inline void *nlmsg_payload(const struct nlmsghdr *nlh, size_t len) +{ + if (nlh->nlmsg_len < nlmsg_msg_size(len)) + return NULL; + + return nlmsg_data(nlh); +} + +/** * nlmsg_attrdata - head of attributes data * @nlh: netlink message header * @hdrlen: length of family specific header |
