Avoid running into buffer overflows with excessively long network
namespace. Fixes Gcc-8 warning about possible snprintf truncation.

Signed-off-by: Stephen Hemminger <step...@networkplumber.org>
---
 lib/namespace.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/namespace.c b/lib/namespace.c
index 682634028587..ce5683a5f4e6 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -18,7 +18,7 @@
 static void bind_etc(const char *name)
 {
        char etc_netns_path[PATH_MAX];
-       char netns_name[PATH_MAX];
+       char netns_name[2*PATH_MAX];
        char etc_name[PATH_MAX];
        struct dirent *entry;
        DIR *dir;
@@ -52,6 +52,12 @@ int netns_switch(char *name)
        unsigned long mountflags = 0;
        struct statvfs fsstat;
 
+       if (strlen(name) >= NAME_MAX) {
+               fprintf(stderr, "Network namespace name too long\"%s\"\n",
+                       name);
+               return -1;
+       }
+
        snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
        netns = open(net_path, O_RDONLY | O_CLOEXEC);
        if (netns < 0) {
-- 
2.16.2

Reply via email to