Unverified Commit d9750081 by Stéphane Graber Committed by GitHub

Merge pull request #2299 from brauner/2018-05-01/bugfixes

coverity + code removal
parents 0f1b40e2 ccd42a31
......@@ -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 \
......
......@@ -4117,6 +4117,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;
......@@ -4200,6 +4203,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");
......
/*
* 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);
}
/*
* 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
......@@ -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;
......
......@@ -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;
......@@ -592,6 +587,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) {
......
......@@ -2417,7 +2417,7 @@ int parse_byte_size_string(const char *s, int64_t *converted)
if (!s || !strcmp(s, ""))
return -EINVAL;
end = stpncpy(dup, s, sizeof(dup));
end = stpncpy(dup, s, sizeof(dup) - 1);
if (*end != '\0')
return -EINVAL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment