tools: ynl: check for overflow of constructed messages

Donald points out that we don't check for overflows.
Stash the length of the message on nlmsg_pid (nlmsg_seq would
do as well). This allows the attribute helpers to remain
self-contained (no extra arguments). Also let the put
helpers continue to return nothing. The error is checked
only in (newly introduced) ynl_msg_end().

Reviewed-by: Donald Hunter <donald.hunter@gmail.com>
Link: https://lore.kernel.org/r/20240305185000.964773-1-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski 2024-03-05 10:50:00 -08:00
parent e3afe5dd3a
commit 15d2540e0d
3 changed files with 68 additions and 4 deletions

View file

@ -135,6 +135,8 @@ int ynl_error_parse(struct ynl_parse_arg *yarg, const char *msg);
/* Netlink message handling helpers */
#define YNL_MSG_OVERFLOW 1
static inline struct nlmsghdr *ynl_nlmsg_put_header(void *buf)
{
struct nlmsghdr *nlh = buf;
@ -239,11 +241,29 @@ ynl_attr_first(const void *start, size_t len, size_t skip)
return ynl_attr_if_good(start + len, attr);
}
static inline bool
__ynl_attr_put_overflow(struct nlmsghdr *nlh, size_t size)
{
bool o;
/* ynl_msg_start() stashed buffer length in nlmsg_pid. */
o = nlh->nlmsg_len + NLA_HDRLEN + NLMSG_ALIGN(size) > nlh->nlmsg_pid;
if (o)
/* YNL_MSG_OVERFLOW is < NLMSG_HDRLEN, all subsequent checks
* are guaranteed to fail.
*/
nlh->nlmsg_pid = YNL_MSG_OVERFLOW;
return o;
}
static inline struct nlattr *
ynl_attr_nest_start(struct nlmsghdr *nlh, unsigned int attr_type)
{
struct nlattr *attr;
if (__ynl_attr_put_overflow(nlh, 0))
return ynl_nlmsg_end_addr(nlh) - NLA_HDRLEN;
attr = ynl_nlmsg_end_addr(nlh);
attr->nla_type = attr_type | NLA_F_NESTED;
nlh->nlmsg_len += NLA_HDRLEN;
@ -263,6 +283,9 @@ ynl_attr_put(struct nlmsghdr *nlh, unsigned int attr_type,
{
struct nlattr *attr;
if (__ynl_attr_put_overflow(nlh, size))
return;
attr = ynl_nlmsg_end_addr(nlh);
attr->nla_type = attr_type;
attr->nla_len = NLA_HDRLEN + size;
@ -276,14 +299,17 @@ static inline void
ynl_attr_put_str(struct nlmsghdr *nlh, unsigned int attr_type, const char *str)
{
struct nlattr *attr;
const char *end;
size_t len;
len = strlen(str);
if (__ynl_attr_put_overflow(nlh, len))
return;
attr = ynl_nlmsg_end_addr(nlh);
attr->nla_type = attr_type;
end = stpcpy(ynl_attr_data(attr), str);
attr->nla_len =
NLA_HDRLEN + NLA_ALIGN(end - (char *)ynl_attr_data(attr));
strcpy(ynl_attr_data(attr), str);
attr->nla_len = NLA_HDRLEN + NLA_ALIGN(len);
nlh->nlmsg_len += NLMSG_ALIGN(attr->nla_len);
}

View file

@ -404,9 +404,33 @@ struct nlmsghdr *ynl_msg_start(struct ynl_sock *ys, __u32 id, __u16 flags)
nlh->nlmsg_flags = flags;
nlh->nlmsg_seq = ++ys->seq;
/* This is a local YNL hack for length checking, we put the buffer
* length in nlmsg_pid, since messages sent to the kernel always use
* PID 0. Message needs to be terminated with ynl_msg_end().
*/
nlh->nlmsg_pid = YNL_SOCKET_BUFFER_SIZE;
return nlh;
}
static int ynl_msg_end(struct ynl_sock *ys, struct nlmsghdr *nlh)
{
/* We stash buffer length in nlmsg_pid. */
if (nlh->nlmsg_pid == 0) {
yerr(ys, YNL_ERROR_INPUT_INVALID,
"Unknown input buffer length");
return -EINVAL;
}
if (nlh->nlmsg_pid == YNL_MSG_OVERFLOW) {
yerr(ys, YNL_ERROR_INPUT_TOO_BIG,
"Constructred message longer than internal buffer");
return -EMSGSIZE;
}
nlh->nlmsg_pid = 0;
return 0;
}
struct nlmsghdr *
ynl_gemsg_start(struct ynl_sock *ys, __u32 id, __u16 flags,
__u8 cmd, __u8 version)
@ -607,6 +631,10 @@ static int ynl_sock_read_family(struct ynl_sock *ys, const char *family_name)
nlh = ynl_gemsg_start_req(ys, GENL_ID_CTRL, CTRL_CMD_GETFAMILY, 1);
ynl_attr_put_str(nlh, CTRL_ATTR_FAMILY_NAME, family_name);
err = ynl_msg_end(ys, nlh);
if (err < 0)
return err;
err = send(ys->socket, nlh, nlh->nlmsg_len, 0);
if (err < 0) {
perr(ys, "failed to request socket family info");
@ -868,6 +896,10 @@ int ynl_exec(struct ynl_sock *ys, struct nlmsghdr *req_nlh,
{
int err;
err = ynl_msg_end(ys, req_nlh);
if (err < 0)
return err;
err = send(ys->socket, req_nlh, req_nlh->nlmsg_len, 0);
if (err < 0)
return err;
@ -921,6 +953,10 @@ int ynl_exec_dump(struct ynl_sock *ys, struct nlmsghdr *req_nlh,
{
int err;
err = ynl_msg_end(ys, req_nlh);
if (err < 0)
return err;
err = send(ys->socket, req_nlh, req_nlh->nlmsg_len, 0);
if (err < 0)
return err;

View file

@ -20,6 +20,8 @@ enum ynl_error_code {
YNL_ERROR_ATTR_INVALID,
YNL_ERROR_UNKNOWN_NTF,
YNL_ERROR_INV_RESP,
YNL_ERROR_INPUT_INVALID,
YNL_ERROR_INPUT_TOO_BIG,
};
/**