]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
lib/namespace: don't leak fd in error case
authorPhil Sutter <phil@nwl.cc>
Thu, 6 Aug 2015 12:24:35 +0000 (14:24 +0200)
committerStephen Hemminger <shemming@brocade.com>
Wed, 12 Aug 2015 16:23:47 +0000 (09:23 -0700)
Signed-off-by: Phil Sutter <phil@nwl.cc>
lib/namespace.c

index a61feb6aea350a696c9a132e1e59ed89f7b1836d..8197165259ccf0d052d7e3212a70eefe7a784cc0 100644 (file)
@@ -58,32 +58,35 @@ int netns_switch(char *name)
        if (setns(netns, CLONE_NEWNET) < 0) {
                fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
                        name, strerror(errno));
-               return -1;
+               goto fail_close;
        }
 
        if (unshare(CLONE_NEWNS) < 0) {
                fprintf(stderr, "unshare failed: %s\n", strerror(errno));
-               return -1;
+               goto fail_close;
        }
        /* Don't let any mounts propagate back to the parent */
        if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
                fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
                        strerror(errno));
-               return -1;
+               goto fail_close;
        }
        /* Mount a version of /sys that describes the network namespace */
        if (umount2("/sys", MNT_DETACH) < 0) {
                fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
-               return -1;
+               goto fail_close;
        }
        if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
                fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
-               return -1;
+               goto fail_close;
        }
 
        /* Setup bind mounts for config files in /etc */
        bind_etc(name);
        return 0;
+fail_close:
+       close(netns);
+       return -1;
 }
 
 int netns_get_fd(const char *name)