From patchwork Mon Jul 10 12:08:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 786125 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3x5kWq2yQrz9s1h for ; Mon, 10 Jul 2017 22:08:39 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753790AbdGJMIh (ORCPT ); Mon, 10 Jul 2017 08:08:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43130 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753298AbdGJMIg (ORCPT ); Mon, 10 Jul 2017 08:08:36 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6812574855; Mon, 10 Jul 2017 12:08:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6812574855 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mcroce@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6812574855 Received: from dhcp-176-85.mxp.redhat.com (dhcp-176-85.mxp.redhat.com [10.32.176.85]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8240C19E02; Mon, 10 Jul 2017 12:08:34 +0000 (UTC) From: Matteo Croce To: netdev@vger.kernel.org Cc: Phil Sutter , Stephen Hemminger Subject: [PATCH] netns: avoid directory traversal (was: ip netns: Make sure netns name is sane) Date: Mon, 10 Jul 2017 14:08:31 +0200 Message-Id: <20170710120831.9355-1-mcroce@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 10 Jul 2017 12:08:35 +0000 (UTC) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Hi Phil, I noticed that your patch still leaves an uncovered scenario, the one where the namespace name is "." or "..". Calling 'ip netns del ..' will remove /var/run which is a symlink to /run on most systems causing some daemons, eg. dbus, to fail. ip netns doesn't validate input, allowing creation and deletion of files relatives to /var/run/netns. This patch denies creation or deletion of namespaces with names contaning "/" or that matches exactly "." or "..". --- ip/ipnetns.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ip/ipnetns.c b/ip/ipnetns.c index 0b0378a..4254994 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();