From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg0-f66.google.com ([74.125.83.66]:40299 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967720AbeCSQ4t (ORCPT ); Mon, 19 Mar 2018 12:56:49 -0400 Received: by mail-pg0-f66.google.com with SMTP id g8so7123024pgv.7 for ; Mon, 19 Mar 2018 09:56:48 -0700 (PDT) From: Stephen Hemminger To: netdev@vger.kernel.org Cc: Stephen Hemminger Subject: [PATCH iproute2 5/5] namespace: limit length of network namespace Date: Mon, 19 Mar 2018 09:56:38 -0700 Message-Id: <20180319165638.30166-6-stephen@networkplumber.org> In-Reply-To: <20180319165638.30166-1-stephen@networkplumber.org> References: <20180319165638.30166-1-stephen@networkplumber.org> Sender: netdev-owner@vger.kernel.org List-ID: Avoid running into buffer overflows with excessively long network namespace. Fixes Gcc-8 warning about possible snprintf truncation. Signed-off-by: Stephen Hemminger --- 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