[lxc-devel] [lxc/master] legacy confile: fix legacy network parser

brauner on Github lxc-bot at linuxcontainers.org
Sat Dec 9 16:31:47 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 831 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171209/451ea367/attachment.bin>
-------------- next part --------------
From 3142235f57d83f4d957357d823f2b519a7ed8eef Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Dec 2017 17:27:34 +0100
Subject: [PATCH] legacy confile: fix legacy network parser

This fixes a bug introduced by:

commit 94f0035bf636ba853451d59c129e26d94850c04d
Author: Christian Brauner <christian.brauner at ubuntu.com>
Date:   Thu Dec 7 15:07:26 2017 +0100

    coverity: #1425924

    remove logically dead condition

    Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>

Coverity's bug analysis is correct but my fix wasn't.

This commit fixes a bunch of other bugs I just spotted as well.

This unblocks #2009.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/confile_legacy.c | 55 +++++++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 22 deletions(-)

diff --git a/src/lxc/confile_legacy.c b/src/lxc/confile_legacy.c
index 8563d7c5c..d94a5eb50 100644
--- a/src/lxc/confile_legacy.c
+++ b/src/lxc/confile_legacy.c
@@ -182,7 +182,7 @@ int set_config_network_legacy_type(const char *key, const char *value,
 	}
 
 	lxc_list_init(list);
-	list->elem = netdev;
+	lxc_list_add_elem(list, netdev);
 
 	/* We maintain a negative count for legacy networks. */
 	netdev->idx = -1;
@@ -327,10 +327,18 @@ static struct lxc_netdev *network_netdev(const char *key, const char *value,
 		return NULL;
 	}
 
-	if (get_network_netdev_idx(key + 12) == EINVAL)
-		netdev = lxc_list_last_elem(network);
-	else
+	if (get_network_netdev_idx(key + 12) == EINVAL) {
+		struct lxc_list *it;
+		struct lxc_netdev *found;
+
+		lxc_list_for_each(it, network) {
+			found = it->elem;
+			if (found->idx == -1)
+				netdev = found;
+		}
+	} else {
 		netdev = get_netdev_from_key(key + 12, network);
+	}
 
 	if (!netdev) {
 		ERROR("no network device defined for '%s' = '%s' option", key,
@@ -568,7 +576,7 @@ int set_config_network_legacy_ipv4(const char *key, const char *value,
 	}
 
 	lxc_list_init(list);
-	list->elem = inetdev;
+	lxc_list_add_elem(list, inetdev);
 
 	addr = strdup(value);
 	if (!addr) {
@@ -590,7 +598,7 @@ int set_config_network_legacy_ipv4(const char *key, const char *value,
 		prefix = slash + 1;
 	}
 
-	if (!inet_pton(AF_INET, addr, &inetdev->addr)) {
+	if (inet_pton(AF_INET, addr, &inetdev->addr) != 1) {
 		SYSERROR("invalid ipv4 address: %s", value);
 		free(inetdev);
 		free(addr);
@@ -598,7 +606,7 @@ int set_config_network_legacy_ipv4(const char *key, const char *value,
 		return -1;
 	}
 
-	if (bcast && !inet_pton(AF_INET, bcast, &inetdev->bcast)) {
+	if (bcast && inet_pton(AF_INET, bcast, &inetdev->bcast) != 1) {
 		SYSERROR("invalid ipv4 broadcast address: %s", value);
 		free(inetdev);
 		free(list);
@@ -655,7 +663,7 @@ int set_config_network_legacy_ipv4_gateway(const char *key, const char *value,
 			return -1;
 		}
 
-		if (!inet_pton(AF_INET, value, gw)) {
+		if (inet_pton(AF_INET, value, gw) != 1) {
 			SYSERROR("invalid ipv4 gateway address: %s", value);
 			free(gw);
 			return -1;
@@ -721,7 +729,7 @@ int set_config_network_legacy_ipv6(const char *key, const char *value,
 		}
 	}
 
-	if (!inet_pton(AF_INET6, valdup, &inet6dev->addr)) {
+	if (inet_pton(AF_INET6, valdup, &inet6dev->addr) != 1) {
 		SYSERROR("invalid ipv6 address: %s", valdup);
 		free(list);
 		free(inet6dev);
@@ -761,7 +769,7 @@ int set_config_network_legacy_ipv6_gateway(const char *key, const char *value,
 			return -1;
 		}
 
-		if (!inet_pton(AF_INET6, value, gw)) {
+		if (inet_pton(AF_INET6, value, gw) != 1) {
 			SYSERROR("invalid ipv6 gateway address: %s", value);
 			free(gw);
 			return -1;
@@ -838,19 +846,18 @@ int get_config_network_legacy_item(const char *key, char *retv, int inlen,
 	else
 		memset(retv, 0, inlen);
 
-	if (!strncmp(key, "lxc.network.", 12))
-		key += 12;
-	else
+	if (strncmp(key, "lxc.network.", 12) != 0)
 		return -1;
 
-	p1 = strchr(key, '.');
+	p1 = strchr(key + 12, '.');
 	if (!p1 || *(p1 + 1) == '\0')
 		return -1;
 	p1++;
 
-	netdev = get_netdev_from_key(key, &c->network);
+	netdev = network_netdev(key, "", &c->network);
 	if (!netdev)
 		return -1;
+
 	if (strcmp(p1, "name") == 0) {
 		if (netdev->name[0] != '\0')
 			strprint(retv, inlen, "%s", netdev->name);
@@ -958,9 +965,16 @@ static int lxc_clear_nic(struct lxc_conf *c, const char *key)
 		return -1;
 	}
 
-	if ((idx = get_network_netdev_idx(key)) == EINVAL)
-		netdev = lxc_list_last_elem(&c->network);
-	else {
+	if ((idx = get_network_netdev_idx(key)) == EINVAL) {
+		struct lxc_list *it;
+		struct lxc_netdev *found;
+
+		lxc_list_for_each(it, &c->network) {
+			found = it->elem;
+			if (found->idx == -1)
+				netdev = found;
+		}
+	} else {
 		lxc_list_for_each(it, &c->network) {
 			netdev = it->elem;
 			if (idx == netdev->idx)
@@ -972,10 +986,7 @@ static int lxc_clear_nic(struct lxc_conf *c, const char *key)
 		return -1;
 
 	p1 = strchr(key, '.');
-	if (!p1 || *(p1+1) == '\0')
-		return -1;
-
-	if (it) {
+	if (!p1 && it) {
 		lxc_remove_nic(it);
 	} else if (strcmp(p1, ".ipv4") == 0) {
 		struct lxc_list *it2,*next;


More information about the lxc-devel mailing list