From patchwork Wed Jul 19 22:36:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 791251 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xCX2G6fxjz9t2r for ; Thu, 20 Jul 2017 08:36:38 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932689AbdGSWgg (ORCPT ); Wed, 19 Jul 2017 18:36:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33248 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751911AbdGSWgf (ORCPT ); Wed, 19 Jul 2017 18:36:35 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 852377A163; Wed, 19 Jul 2017 22:36:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 852377A163 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mcroce@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 852377A163 Received: from lanhost330.redhat.com (ovpn-116-21.ams2.redhat.com [10.36.116.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BA45A17CF9; Wed, 19 Jul 2017 22:36:33 +0000 (UTC) From: Matteo Croce To: Phil Sutter Cc: netdev@vger.kernel.org Subject: [PATCH v2] netns: avoid directory traversal (was: ip netns: Make sure netns name is sane) Date: Thu, 20 Jul 2017 00:36:32 +0200 Message-Id: <20170719223632.4124-1-mcroce@redhat.com> In-Reply-To: <20170718171230.084c804e@xeon-e3> References: <20170718171230.084c804e@xeon-e3> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 19 Jul 2017 22:36:35 +0000 (UTC) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org v2: reword commit message ip netns keeps track of created namespaces with bind mounts named /var/run/netns/. No input sanitization is done, allowing creation and deletion of files relatives to /var/run/netns or, if the path is non existent or invalid, allows to create "untracked" namespaces (invisible to the tool). This commit denies creation or deletion of namespaces with names contaning "/" or matching exactly "." or "..". Signed-off-by: Matteo Croce --- ip/ipnetns.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ip/ipnetns.c b/ip/ipnetns.c index 0b0378ab..42549944 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -766,6 +766,11 @@ static int netns_monitor(int argc, char **argv) return 0; } +static int invalid_name(const char *name) +{ + return strchr(name, '/') || !strcmp(name, ".") || !strcmp(name, ".."); +} + int do_netns(int argc, char **argv) { netns_nsid_socket_init(); @@ -775,6 +780,11 @@ int do_netns(int argc, char **argv) return netns_list(0, NULL); } + if (argc > 1 && invalid_name(argv[1])) { + fprintf(stderr, "Invalid netns name \"%s\"\n", argv[1]); + exit(-1); + } + if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) || (matches(*argv, "lst") == 0)) { netns_map_init();