summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-07-28 09:24:26 (GMT)
committerThomas Haller <thaller@redhat.com>2023-07-28 10:31:20 (GMT)
commitca34ad524ec7a9f0e24bb5975b178a3e70268f0f (patch)
tree5b4c5165344c68181ef852fea6678f2c887b026c
parent859b89dc567a60007151037bb2eb0cccbfca1f8c (diff)
downloadlibnl-ca34ad524ec7a9f0e24bb5975b178a3e70268f0f.zip
libnl-ca34ad524ec7a9f0e24bb5975b178a3e70268f0f.tar.gz
libnl-ca34ad524ec7a9f0e24bb5975b178a3e70268f0f.tar.bz2
lib: handle negative and zero size in nla_memcpy()
a negative count is a bug in the caller. Still, handle it better than just crashing. Maybe we should assert, but it doesn't seem best to assert against user input. Also, if count is zero, don't call memcpy(). Calling memcpy() requires that the source and destination pointers are valid, otherwise it's undefined behavior. I think if the caller tells us to copy zero bytes, we should never look at the destination pointer.
-rw-r--r--lib/attr.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/lib/attr.c b/lib/attr.c
index d919811..85a11e7 100644
--- a/lib/attr.c
+++ b/lib/attr.c
@@ -349,10 +349,13 @@ int nla_memcpy(void *dest, const struct nlattr *src, int count)
if (!src)
return 0;
-
+
minlen = _NL_MIN(count, nla_len(src));
- memcpy(dest, nla_data(src), minlen);
+ if (minlen <= 0)
+ return 0;
+
+ memcpy(dest, nla_data(src), minlen);
return minlen;
}