[lxc-devel] [lxc/master] coverity + code removal

brauner on Github lxc-bot at linuxcontainers.org
Tue May 1 15:42:34 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180501/27644d83/attachment.bin>
-------------- next part --------------
From 7e880483dd79d6a6b6e19ef446e7aafc704ac40f Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 1 May 2018 16:59:19 +0200
Subject: [PATCH 1/4] genl: remove

These files have never been used and as such have no dependencies in the
codebase whatsoever. So remove them. If we need them we can simply pull them
out of the git history.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/Makefile.am |   1 -
 src/lxc/genl.c      | 150 ----------------------------------------------------
 src/lxc/genl.h      | 121 ------------------------------------------
 3 files changed, 272 deletions(-)
 delete mode 100644 src/lxc/genl.c
 delete mode 100644 src/lxc/genl.h

diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index 0662d83da..f894b7925 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -120,7 +120,6 @@ liblxc_la_SOURCES = \
 	network.c network.h \
 	nl.c nl.h \
 	rtnl.c rtnl.h \
-	genl.c genl.h \
 	\
 	caps.c caps.h \
 	lxcseccomp.h \
diff --git a/src/lxc/genl.c b/src/lxc/genl.c
deleted file mode 100644
index 6afaf40ab..000000000
--- a/src/lxc/genl.c
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- * lxc: linux Container library
- *
- * (C) Copyright IBM Corp. 2007, 2008
- *
- * Authors:
- * Daniel Lezcano <daniel.lezcano at free.fr>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-#include <string.h>
-#include <stdio.h>
-#include <sys/socket.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <linux/genetlink.h>
-#include <linux/rtnetlink.h>
-
-#include "nl.h"
-#include "genl.h"
-
-static int genetlink_resolve_family(const char *family)
-{
-	struct nl_handler handler;
-	struct nlattr *attr;
-	struct genlmsg *request, *reply;
-	struct genlmsghdr *genlmsghdr;
-
-	int len, ret;
-
-	request = genlmsg_alloc(GENLMSG_GOOD_SIZE);
-	if (!request)
-		return -ENOMEM;
-
-	reply = genlmsg_alloc(GENLMSG_GOOD_SIZE);
-	if (!reply) {
-		genlmsg_free(request);
-		return -ENOMEM;
-	}
-
-	request->nlmsghdr.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
-	request->nlmsghdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
-	request->nlmsghdr.nlmsg_type = GENL_ID_CTRL;
-
-	genlmsghdr = NLMSG_DATA(&request->nlmsghdr);
-	genlmsghdr->cmd = CTRL_CMD_GETFAMILY;
-
-	ret = netlink_open(&handler, NETLINK_GENERIC);
-	if (ret)
-		goto out;
-
-	ret = nla_put_string((struct nlmsg *)&request->nlmsghdr,
-			     CTRL_ATTR_FAMILY_NAME, family);
-	if (ret)
-		goto out_close;
-
-	ret = netlink_transaction(&handler, (struct nlmsg *)&request->nlmsghdr,
-				  (struct nlmsg *)&reply->nlmsghdr);
-	if (ret < 0)
-		goto out_close;
-
-	genlmsghdr = NLMSG_DATA(&reply->nlmsghdr);
-	len = reply->nlmsghdr.nlmsg_len;
-
-	ret = -ENOMSG;
-	if (reply->nlmsghdr.nlmsg_type !=  GENL_ID_CTRL)
-		goto out_close;
-
-	if (genlmsghdr->cmd != CTRL_CMD_NEWFAMILY)
-		goto out_close;
-
-	ret = -EMSGSIZE;
-	len -= NLMSG_LENGTH(GENL_HDRLEN);
-	if (len < 0)
-		goto out_close;
-
-	attr = (struct nlattr *)GENLMSG_DATA(reply);
-	attr = (struct nlattr *)((char *)attr + NLA_ALIGN(attr->nla_len));
-
-	ret = -ENOMSG;
-	if (attr->nla_type != CTRL_ATTR_FAMILY_ID)
-		goto out_close;
-
-	ret =  *(__u16 *) NLA_DATA(attr);
-out_close:
-	netlink_close(&handler);
-out:
-	genlmsg_free(request);
-	genlmsg_free(reply);
-	return ret;
-}
-
-extern int genetlink_open(struct genl_handler *handler, const char *family)
-{
-	int ret;
-	handler->family = genetlink_resolve_family(family);
-	if (handler->family < 0)
-		return handler->family;
-
-	ret = netlink_open(&handler->nlh, NETLINK_GENERIC);
-
-	return ret;
-}
-
-extern int genetlink_close(struct genl_handler *handler)
-{
-	return netlink_close(&handler->nlh);
-}
-
-extern int genetlink_rcv(struct genl_handler *handler, struct genlmsg *genlmsg)
-{
-	return netlink_rcv(&handler->nlh, (struct nlmsg *)&genlmsg->nlmsghdr);
-}
-
-extern int genetlink_send(struct genl_handler *handler, struct genlmsg *genlmsg)
-{
-
-	return netlink_send(&handler->nlh, (struct nlmsg *)&genlmsg->nlmsghdr);
-}
-
-extern int genetlink_transaction(struct genl_handler *handler,
-			  struct genlmsg *request, struct genlmsg *answer)
-{
-	return netlink_transaction(&handler->nlh, (struct nlmsg *)&request->nlmsghdr,
-				   (struct nlmsg *)&answer->nlmsghdr);
-}
-
-extern struct genlmsg *genlmsg_alloc(size_t size)
-{
-	size_t len = NLMSG_LENGTH(GENL_HDRLEN) + size;
-	return  (struct genlmsg *)nlmsg_alloc(len);
-}
-
-extern void genlmsg_free(struct genlmsg *genlmsg)
-{
-	free(genlmsg);
-}
diff --git a/src/lxc/genl.h b/src/lxc/genl.h
deleted file mode 100644
index a83b7a7a9..000000000
--- a/src/lxc/genl.h
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * lxc: linux Container library
- *
- * (C) Copyright IBM Corp. 2007, 2008
- *
- * Authors:
- * Daniel Lezcano <daniel.lezcano at free.fr>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-#ifndef __LXC_GENL_H
-#define __LXC_GENL_H
-
-/*
- * Use this as a good size to allocate generic netlink messages
- */
-#define GENLMSG_GOOD_SIZE NLMSG_GOOD_SIZE
-#define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
-
-/*
- * struct genl_handler : the structure which store the netlink handler
- *  and the family number resulting of the auto-generating id family
- *  for the generic netlink protocol
- *
- * @nlh: the netlink socket handler
- * @family: the generic netlink family assigned number
- */
-struct genl_handler
-{
-	struct nl_handler nlh;
-	int family;
-};
-
-/*
- * struct genlmsg : the struct containing the generic netlink message
- *  format
- *
- * @nlmsghdr: a netlink message header
- * @genlmsghdr: a generic netlink message header pointer
- *
- */
-/* __attribute__ ((aligned(4))); */
-struct genlmsg {
-	struct nlmsghdr nlmsghdr;
-	struct genlmsghdr genlmsghdr;
-};
-
-static inline int genetlink_len(const struct genlmsg *genlmsg)
-{
-	return ((genlmsg->nlmsghdr.nlmsg_len) - GENL_HDRLEN - NLMSG_HDRLEN);
-}
-
-/*
- * genetlink_open : resolve family number id and open a generic netlink socket
- *
- * @handler: a struct genl_handler pointer
- * @family: the family name of the generic netlink protocol
- *
- * Returns 0 on success, < 0 otherwise
- */
-int genetlink_open(struct genl_handler *handler, const char *family);
-
-/*
- * genetlink_close : close a generic netlink socket
- *
- * @handler: the handler of the socket to be closed
- *
- * Returns 0 on success, < 0 otherwise
- */
-int genetlink_close(struct genl_handler *handler);
-
-/*
- * genetlink_rcv : receive a generic netlink socket, it is up
- *  to the caller to manage the allocation of the generic netlink message
- *
- * @handler: the handler of the generic netlink socket
- * @genlmsg: the pointer to a generic netlink message pre-allocated
- *
- * Returns 0 on success, < 0 otherwise
- */
-int genetlink_rcv(struct genl_handler *handler, struct genlmsg *genlmsg);
-
-/*
- * genetlink_send : send a generic netlink socket, it is up
- *  to the caller to manage the allocation of the generic netlink message
- *
- * @handler: the handler of the generic netlink socket
- * @genlmsg: the pointer to a generic netlink message pre-allocated
- *
- * Returns 0 on success, < 0 otherwise
- */
-int genetlink_send(struct genl_handler *handler, struct genlmsg *genlmsg);
-
-struct genlmsg *genlmsg_alloc(size_t size);
-
-void genlmsg_free(struct genlmsg *genlmsg);
-
-/*
- * genetlink_transaction : send and receive a generic netlink message in one shot
- *
- * @handler: the handler of the generic netlink socket
- * @request: a generic netlink message containing the request to be sent
- * @answer: a pre-allocated generic netlink message to receive the response
- *
- * Returns 0 on success, < 0 otherwise
- */
-int genetlink_transaction(struct genl_handler *handler,
-			  struct genlmsg *request, struct genlmsg *answer);
-#endif

From 8291d2924c61150f47ef51f29e609b61ca5a0899 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 1 May 2018 17:17:03 +0200
Subject: [PATCH 2/4] coverity: #1248104

Argument cannot be negative

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

diff --git a/src/lxc/network.c b/src/lxc/network.c
index e31066cb9..cbffe8ef6 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -592,6 +592,8 @@ static char *is_wlan(const char *ifname)
 	fseek(f, 0, SEEK_END);
 	physlen = ftell(f);
 	fseek(f, 0, SEEK_SET);
+	if (physlen < 0)
+		goto bad;
 
 	physname = malloc(physlen + 1);
 	if (!physname) {

From 41302e6220d8537620416d7118d7f4cab00eb121 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 1 May 2018 17:18:26 +0200
Subject: [PATCH 3/4] coverity: #1248105

Time of check time of use

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/network.c | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/src/lxc/network.c b/src/lxc/network.c
index cbffe8ef6..04944811c 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -571,7 +571,6 @@ static char *is_wlan(const char *ifname)
 	size_t len;
 	char *path;
 	FILE *f;
-	struct stat sb;
 	char *physname = NULL;
 
 	len = strlen(ifname) + strlen(PHYSNAME) - 1;
@@ -580,10 +579,6 @@ static char *is_wlan(const char *ifname)
 	if (ret < 0 || (size_t)ret >= len)
 		goto bad;
 
-	ret = stat(path, &sb);
-	if (ret)
-		goto bad;
-
 	f = fopen(path, "r");
 	if (!f)
 		goto bad;

From db78d760086929117eb32c1dc4b36e4154d2eb6e Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 1 May 2018 17:27:51 +0200
Subject: [PATCH 4/4] coverity: #1425744

Dereference after null check

userns_exec_{1,full} are called from functions that might not have a conf.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.c         | 6 ++++++
 src/lxc/lxccontainer.c | 1 +
 2 files changed, 7 insertions(+)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 48c4b4ecc..800013a52 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -4109,6 +4109,9 @@ int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data,
 	int ret = -1, status = -1;
 	char c = '1';
 
+	if (!conf)
+		return -EINVAL;
+
 	idmap = get_minimal_idmap(conf);
 	if (!idmap)
 		return -1;
@@ -4192,6 +4195,9 @@ int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data,
 	struct id_map *container_root_uid = NULL, *container_root_gid = NULL,
 		      *host_uid_map = NULL, *host_gid_map = NULL;
 
+	if (!conf)
+		return -EINVAL;
+
 	ret = pipe(p);
 	if (ret < 0) {
 		SYSERROR("opening pipe");
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 30efeaff8..b396a6d5a 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -2955,6 +2955,7 @@ static bool do_lxcapi_destroy(struct lxc_container *c)
 {
 	if (!c || !lxcapi_is_defined(c))
 		return false;
+
 	if (has_snapshots(c)) {
 		ERROR("Container %s has snapshots;  not removing", c->name);
 		return false;


More information about the lxc-devel mailing list