lib: return error on Netlink attribute length overflow

Netlink attribute length is defined as u16. It's possible to exceed nla_len when
creating nested attributes. Storing incorrect length due to overflow will cause
a reader to read only a part of nested attribute or skip it entirely.

As a solution cancel the addition of a nested attribute when nla_len size is
exceeded.

Signed-off-by: Przemyslaw Szczerbik <przemek.szczerbik@gmail.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>

http://lists.infradead.org/pipermail/libnl/2016-May/002131.html
This commit is contained in:
Przemyslaw Szczerbik 2016-05-30 23:26:00 +02:00 committed by Thomas Haller
parent 838f43faee
commit 424b3b6d0d
3 changed files with 8 additions and 5 deletions

View File

@ -50,8 +50,9 @@ extern "C" {
#define NLE_NODEV 31
#define NLE_IMMUTABLE 32
#define NLE_DUMP_INTR 33
#define NLE_ATTRSIZE 34
#define NLE_MAX NLE_DUMP_INTR
#define NLE_MAX NLE_ATTRSIZE
extern const char * nl_geterror(int);
extern void nl_perror(int, const char *);

View File

@ -912,7 +912,7 @@ struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
*
* Corrects the container attribute header to include the appeneded attributes.
*
* @return 0
* @return 0 on success or a negative error code.
*/
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
{
@ -920,14 +920,15 @@ int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
len = (void *) nlmsg_tail(msg->nm_nlh) - (void *) start;
if (len == NLA_HDRLEN) {
if (len == NLA_HDRLEN || len > USHRT_MAX) {
/*
* Kernel can't handle empty nested attributes, trim the
* Max nlattr size exceeded or empty nested attribute, trim the
* attribute header again
*/
nla_nest_cancel(msg, start);
return 0;
/* Return error only if nlattr size was exceeded */
return (len == NLA_HDRLEN) ? 0 : -NLE_ATTRSIZE;
}
start->nla_len = len;

View File

@ -47,6 +47,7 @@ static const char *errmsg[NLE_MAX+1] = {
[NLE_NODEV] = "No such device",
[NLE_IMMUTABLE] = "Immutable attribute",
[NLE_DUMP_INTR] = "Dump inconsistency detected, interrupted",
[NLE_ATTRSIZE] = "Attribute max length exceeded",
};
/**