Re: [iproute PATCH v2 7/7] lib/ll_map: Make sure im->name is NULL-terminated
flat view
From: Stephen Hemminger <stephen@networkplumber.org>
Date: 2017-08-18 16:33:37
On Thu, 17 Aug 2017 19:09:32 +0200 Phil Sutter [off-list ref] wrote:
quoted hunk ↗ jump to hunk
Signed-off-by: Phil Sutter <phil@nwl.cc> --- lib/ll_map.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)diff --git a/lib/ll_map.c b/lib/ll_map.c index 4e4556c9ac80b..4d06eb69f138a 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c@@ -120,11 +120,11 @@ int ll_remember_index(const struct sockaddr_nl *who, return 0; } - im = malloc(sizeof(*im)); + im = calloc(1, sizeof(*im)); if (im == NULL) return 0; im->index = ifi->ifi_index; - strcpy(im->name, ifname); + strncpy(im->name, ifname, IFNAMSIZ - 1); im->type = ifi->ifi_type; im->flags = ifi->ifi_flags;
This is not really necessary. kernel won't return an ifname with a length >= IFNAMSIZ. If you wanted to future proof it, why not use variable size allocation
--- a/lib/ll_map.c
+++ b/lib/ll_map.c@@ -30,7 +30,7 @@ struct ll_cache { unsigned flags; unsigned index; unsigned short type; - char name[IFNAMSIZ]; + char name[]; }; #define IDXMAP_SIZE 1024
@@ -120,7 +120,7 @@ int ll_remember_index(const struct sockaddr_nl *who, return 0; } - im = malloc(sizeof(*im)); + im = malloc(sizeof(*im) + strlen(ifname) + 1); if (im == NULL) return 0; im->index = ifi->ifi_index;