nlmsg, attr: fix false positives when validating buffer sizes
PATCH 1/1 nlmsg, attr: fix false positives when validating buffer sizes
mnl_nlmsg_ok and mnl_attr_ok both expect a signed buffer length value, len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast to int. Comparing len to the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields of struct nlmsg and struct nlattr, however, is problematic, since these fields may hold values greater than INT_MAX, in which case the casts will yield negative values and result in false positives.
Instead, assign len to an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.
--- src/attr.c | 9 +++++++-- src/nlmsg.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)
src/attr.c +7 -2
@@ -97,9 +97,14 @@ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr)*/ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len) { -return len >= (int )sizeof (struct nlattr ) && +size_t ulen = len; + +if (len <0 ) +return 0 ; + +return ulen >=sizeof (struct nlattr ) && attr->nla_len >=sizeof (struct nlattr ) && - (int )attr->nla_len <= len; + attr->nla_len <= ulen; }/**
src/nlmsg.c +7 -2
@@ -152,9 +152,14 @@ EXPORT_SYMBOL void *mnl_nlmsg_get_payload_offset(const struct nlmsghdr *nlh,*/ EXPORT_SYMBOL bool mnl_nlmsg_ok(const struct nlmsghdr *nlh, int len) { -return len >= (int )sizeof (struct nlmsghdr ) && +size_t ulen = len; + +if (len <0 ) +return 0 ; + +return ulen >=sizeof (struct nlmsghdr ) && nlh->nlmsg_len >=sizeof (struct nlmsghdr ) && - (int )nlh->nlmsg_len <= len; + nlh->nlmsg_len <= ulen; }/**
Signature · 1 line
2.40.1
Pablo Neira Ayuso
maintainerQuoted 28 lines · Jeremy Sowden
mnl_nlmsg_okandmnl_attr_okboth expect a signed buffer length value,len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast toint. Comparinglento the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields ofstruct nlmsgandstruct nlattr, however, is problematic, since these fields may hold values greater thanINT_MAX, in which case the casts will yield negative values and result in false positives.Instead, assign
lento an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.Signed-off-by Jeremy Sowden <[email protected]>--- src/attr.c | 9 +++++++-- src/nlmsg.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)src/attr.c +0
-0
src/attr.c EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr) · line 98
Quoted 2 lines
src/attr.c +0
-0@@ -97,9 +97,14 @@ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr)*/ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len)
Maybe turn this into uint32_t ?
Quoted 38 lines
src/attr.c +7
-2{ -return len >= (int )sizeof (struct nlattr ) && +size_t ulen = len; + +if (len <0 ) +return 0 ; + +return ulen >=sizeof (struct nlattr ) && attr->nla_len >=sizeof (struct nlattr ) && - (int )attr->nla_len <= len; + attr->nla_len <= ulen; }/** src/nlmsg.c +7
-2@@ -152,9 +152,14 @@ EXPORT_SYMBOL void *mnl_nlmsg_get_payload_offset(const struct nlmsghdr *nlh,*/ EXPORT_SYMBOL bool mnl_nlmsg_ok(const struct nlmsghdr *nlh, int len) { -return len >= (int )sizeof (struct nlmsghdr ) && +size_t ulen = len; + +if (len <0 ) +return 0 ; + +return ulen >=sizeof (struct nlmsghdr ) && nlh->nlmsg_len >=sizeof (struct nlmsghdr ) && - (int )nlh->nlmsg_len <= len; + nlh->nlmsg_len <= ulen; }/** Signature · 1 line
2.40.1
Pablo Neira Ayuso
maintainerQuoted 24 lines · Pablo Neira Ayuso
Quoted 23 lines · Jeremy Sowden
mnl_nlmsg_okandmnl_attr_okboth expect a signed buffer length value,len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast toint. Comparinglento the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields ofstruct nlmsgandstruct nlattr, however, is problematic, since these fields may hold values greater thanINT_MAX, in which case the casts will yield negative values and result in false positives.Instead, assign
lento an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.Signed-off-by Jeremy Sowden <[email protected]>--- src/attr.c | 9 +++++++-- src/nlmsg.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)
Quoted 9 lines
src/attr.c EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr) · line 98
Quoted 7 lines
src/attr.c +0
-0@@ -97,9 +97,14 @@ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr)*/ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len)Maybe turn this into
uint32_t?
Actually, attribute length field is 16 bits long, so it can never happen that nla_len will underflow.
Quoted 36 lines · Pablo Neira Ayuso
Quoted 25 lines · Pablo Neira Ayuso
Quoted 24 lines · Jeremy Sowden
mnl_nlmsg_okandmnl_attr_okboth expect a signed buffer length value,len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast toint. Comparinglento the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields ofstruct nlmsgandstruct nlattr, however, is problematic, since these fields may hold values greater thanINT_MAX, in which case the casts will yield negative values and result in false positives.Instead, assign
lento an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.Signed-off-by Jeremy Sowden <[email protected]>--- src/attr.c | 9 +++++++-- src/nlmsg.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)Quoted 9 lines
src/attr.c EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr) · line 98
Quoted 7 lines
src/attr.c +0
-0@@ -97,9 +97,14 @@ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr)*/ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len)Maybe turn this into
uint32_t?
Quoted 2 lines
Actually, attribute length field is 16 bits long, so it can never happen that
nla_lenwill underflow.
Oh, yeah. Sorry. I Thought I'd checked that. I think my version without the casts is still tidier. My preference would be to keep the nlattr change but amend the commit message, but if you prefer I'll drop it.
J.
Pablo Neira Ayuso
maintainerQuoted 37 lines · Jeremy Sowden
Quoted 36 lines · Pablo Neira Ayuso
Quoted 34 lines · Pablo Neira Ayuso
Quoted 29 lines · Jeremy Sowden
mnl_nlmsg_okandmnl_attr_okboth expect a signed buffer length value,len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast toint. Comparinglento the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields ofstruct nlmsgandstruct nlattr, however, is problematic, since these fields may hold values greater thanINT_MAX, in which case the casts will yield negative values and result in false positives.Instead, assign
lento an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.Signed-off-by Jeremy Sowden <[email protected]>--- src/attr.c | 9 +++++++-- src/nlmsg.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)src/attr.c +0
-0src/attr.c EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr) · line 98
Quoted 2 lines
src/attr.c +0
-0@@ -97,9 +97,14 @@ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr)*/ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len)Maybe turn this into
uint32_t?
Quoted 7 lines
Quoted 2 lines
Actually, attribute length field is 16 bits long, so it can never happen that
nla_lenwill underflow.Oh, yeah. Sorry. I Thought I'd checked that. I think my version without the casts is still tidier. My preference would be to keep the nlattr change but amend the commit message, but if you prefer I'll drop it.
I would prefer to update documentation and drop the change for nlattr.
For mnl_attr_ok(), this should be sufficient?
if (len < 0)
return false;to reject buffer larger that 2^31 (2 GBytes) or when length goes negative (malformed netlink message in the buffer).
BTW, this _trivial_ function is modeled after nlmsg_ok() in the kernel...
Quoted 6 lines · Jeremy Sowden
mnl_nlmsg_okandmnl_attr_okboth expect a signed buffer length value,len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast toint.
Did you see such compiler warning? If so, post it in commit description.
Quoted 6 lines
Comparing
lento the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields ofstruct nlmsgandstruct nlattr, however, is problematic, since these fields may hold values greater thanINT_MAX, in which case the casts will yield negative values and result in false positives.
Yes, but the early check for negative length prevents this after this patch, correct?
Quoted 3 lines
Instead, assign
lento an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.
Makes sense.
Probably I'm getting confused with this large patch description :)
Quoted 41 lines · Pablo Neira Ayuso
Quoted 40 lines · Jeremy Sowden
Quoted 38 lines · Pablo Neira Ayuso
Quoted 25 lines · Pablo Neira Ayuso
Quoted 24 lines · Jeremy Sowden
mnl_nlmsg_okandmnl_attr_okboth expect a signed buffer length value,len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast toint. Comparinglento the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields ofstruct nlmsgandstruct nlattr, however, is problematic, since these fields may hold values greater thanINT_MAX, in which case the casts will yield negative values and result in false positives.Instead, assign
lento an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.Signed-off-by Jeremy Sowden <[email protected]>--- src/attr.c | 9 +++++++-- src/nlmsg.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)Quoted 9 lines
src/attr.c EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr) · line 98
Quoted 7 lines
src/attr.c +0
-0@@ -97,9 +97,14 @@ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr)*/ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len)Maybe turn this into
uint32_t?Actually, attribute length field is 16 bits long, so it can never happen that
nla_lenwill underflow.
Quoted 6 lines
Quoted 4 lines
Oh, yeah. Sorry. I Thought I'd checked that. I think my version without the casts is still tidier. My preference would be to keep the nlattr change but amend the commit message, but if you prefer I'll drop it.
I would prefer to update documentation and drop the change for nlattr.
No problem. I'll drop it.
Quoted 29 lines
For
mnl_attr_ok(), this should be sufficient?if (len < 0) return false;to reject buffer larger that 2^31 (2 GBytes) or when length goes negative (malformed netlink message in the buffer).
BTW, this _trivial_ function is modeled after
nlmsg_ok()in the kernel...Quoted 6 lines · Jeremy Sowden
mnl_nlmsg_okandmnl_attr_okboth expect a signed buffer length value,len, against which to compare the size of the object expected to fit into the buffer, because they are intended to validate the length and it may be negative in the case of malformed messages. Comparing this signed value against unsigned operands leads to compiler warnings, so the unsigned operands are cast toint.Did you see such compiler warning? If so, post it in commit description.
Quoted 6 lines
Comparing
lento the size of the structure is fine, because the structures are only a few bytes in size. Comparing it to the length fields ofstruct nlmsgandstruct nlattr, however, is problematic, since these fields may hold values greater thanINT_MAX, in which case the casts will yield negative values and result in false positives.
Quoted 2 lines
Yes, but the early check for negative length prevents this after this patch, correct?
The problem arises if len is positive and nlh->nlmsg_len is larger than INT_MAX. In that case:
len >= (int)sizeof(struct nlmsghdr)
succeeds because len is positive, and:
(int)nlh->nlmsg_len <= len
succeeds because the cast yields a negative value, so we get a false positive.
Quoted 7 lines
Quoted 3 lines
Instead, assign
lento an unsigned local variable, check for negative values first, then use the unsigned local for the other comparisons, and remove the casts.Makes sense.
Probably I'm getting confused with this large patch description :)
I'll trim it down. :)
J.