#ifndef STRACE_NETLINK_H
#define STRACE_NETLINK_H
+#include <stdbool.h>
#include <sys/socket.h>
#include <linux/netlink.h>
# define NLA_TYPE_MASK ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
#endif
+static inline bool
+is_nlmsg_ok(const struct nlmsghdr *const nlh, const ssize_t len)
+{
+ return len >= (ssize_t) sizeof(*nlh)
+ && nlh->nlmsg_len >= sizeof(*nlh)
+ && (size_t) len >= nlh->nlmsg_len;
+}
+
#endif /* !STRACE_NETLINK_H */
}
const struct nlmsghdr *h = &hdr_buf.hdr;
- if (!NLMSG_OK(h, ret))
+ if (!is_nlmsg_ok(h, ret))
return false;
- for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
+ for (; is_nlmsg_ok(h, ret); h = NLMSG_NEXT(h, ret)) {
if (h->nlmsg_type != expected_msg_type)
return false;
const int rc = parser(NLMSG_DATA(h),
perror_msg_and_skip("recvmsg");
struct nlmsghdr *h = &hdr_buf.hdr;
- if (!NLMSG_OK(h, ret))
- error_msg_and_skip("!NLMSG_OK");
+ if (!is_nlmsg_ok(h, ret))
+ error_msg_and_skip("!is_nlmsg_ok");
if (h->nlmsg_type == NLMSG_ERROR) {
const struct nlmsgerr *err = NLMSG_DATA(h);
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
perror_msg_and_skip("recvmsg");
struct nlmsghdr *h = &hdr_buf.hdr;
- if (!NLMSG_OK(h, ret))
- error_msg_and_skip("!NLMSG_OK");
+ if (!is_nlmsg_ok(h, ret))
+ error_msg_and_skip("!is_nlmsg_ok");
if (h->nlmsg_type == NLMSG_ERROR) {
const struct nlmsgerr *err = NLMSG_DATA(h);
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
perror_msg_and_skip("recvmsg");
struct nlmsghdr *h = &hdr_buf.hdr;
- if (!NLMSG_OK(h, ret))
- error_msg_and_skip("!NLMSG_OK");
+ if (!is_nlmsg_ok(h, ret))
+ error_msg_and_skip("!is_nlmsg_ok");
if (h->nlmsg_type == NLMSG_ERROR) {
const struct nlmsgerr *err = NLMSG_DATA(h);
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))