Unverified Commit 86cea5d1 by Wolfgang Bumiller Committed by GitHub

Merge pull request #2549 from brauner/2018-08-19/cmd_usernsexec_fixes

cmd: lxc-usernsexec fixes + macro: move declarations + config_utils: macvlan fixes
parents 87a70c57 62a38dff
...@@ -330,6 +330,7 @@ lxc_usernsexec_SOURCES = cmd/lxc_usernsexec.c \ ...@@ -330,6 +330,7 @@ lxc_usernsexec_SOURCES = cmd/lxc_usernsexec.c \
conf.c conf.h \ conf.c conf.h \
list.h \ list.h \
log.c log.h \ log.c log.h \
macro.h \
namespace.c namespace.h \ namespace.c namespace.h \
utils.c utils.h utils.c utils.h
endif endif
......
...@@ -21,44 +21,35 @@ ...@@ -21,44 +21,35 @@
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "config.h" #include "config.h"
#include <stdio.h> #include <errno.h>
#include <stdlib.h> #include <fcntl.h>
#include <unistd.h> #include <grp.h>
#include <libgen.h>
#include <pwd.h>
#include <sched.h> #include <sched.h>
#include <sys/syscall.h>
#include <signal.h> #include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <sys/mount.h>
#include <libgen.h>
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/mount.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sched.h> #include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include "conf.h" #include "conf.h"
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "macro.h"
#include "namespace.h" #include "namespace.h"
#include "utils.h" #include "utils.h"
#ifndef MS_REC
#define MS_REC 16384
#endif
#ifndef MS_SLAVE
#define MS_SLAVE (1 << 19)
#endif
extern int lxc_log_fd; extern int lxc_log_fd;
int unshare(int flags);
static void usage(const char *name) static void usage(const char *name)
{ {
printf("usage: %s [-h] [-m <uid-maps>] -- [command [arg ..]]\n", name); printf("usage: %s [-h] [-m <uid-maps>] -- [command [arg ..]]\n", name);
...@@ -113,23 +104,13 @@ static int do_child(void *vargv) ...@@ -113,23 +104,13 @@ static int do_child(void *vargv)
char **argv = (char **)vargv; char **argv = (char **)vargv;
/* Assume we want to become root */ /* Assume we want to become root */
ret = setgid(0); ret = lxc_switch_uid_gid(0, 0);
if (ret < 0) { if (ret < 0)
CMD_SYSERROR("Failed to set gid to");
return -1;
}
ret = setuid(0);
if (ret < 0) {
CMD_SYSERROR("Failed to set uid to 0");
return -1; return -1;
}
ret = setgroups(0, NULL); ret = lxc_setgroups(0, NULL);
if (ret < 0) { if (ret < 0)
CMD_SYSERROR("Failed to clear supplementary groups");
return -1; return -1;
}
ret = unshare(CLONE_NEWNS); ret = unshare(CLONE_NEWNS);
if (ret < 0) { if (ret < 0) {
...@@ -213,23 +194,24 @@ static int parse_map(char *map) ...@@ -213,23 +194,24 @@ static int parse_map(char *map)
* only use the first one for each of uid and gid, because otherwise we're not * only use the first one for each of uid and gid, because otherwise we're not
* sure which entries the user wanted. * sure which entries the user wanted.
*/ */
static int read_default_map(char *fnam, int which, char *username) static int read_default_map(char *fnam, int which, char *user)
{ {
size_t len;
char *p1, *p2; char *p1, *p2;
FILE *fin; FILE *fin;
struct id_map *newmap; int ret = -1;
size_t sz = 0; size_t sz = 0;
char *line = NULL; char *line = NULL;
struct lxc_list *tmp = NULL; struct lxc_list *tmp = NULL;
struct id_map *newmap = NULL;
fin = fopen(fnam, "r"); fin = fopen(fnam, "r");
if (!fin) if (!fin)
return -1; return -1;
len = strlen(user);
while (getline(&line, &sz, fin) != -1) { while (getline(&line, &sz, fin) != -1) {
if (sz <= strlen(username) || if (sz <= len || strncmp(line, user, len) != 0 || line[len] != ':')
strncmp(line, username, strlen(username)) != 0 ||
line[strlen(username)] != ':')
continue; continue;
p1 = strchr(line, ':'); p1 = strchr(line, ':');
...@@ -241,34 +223,38 @@ static int read_default_map(char *fnam, int which, char *username) ...@@ -241,34 +223,38 @@ static int read_default_map(char *fnam, int which, char *username)
continue; continue;
newmap = malloc(sizeof(*newmap)); newmap = malloc(sizeof(*newmap));
if (!newmap) { if (!newmap)
fclose(fin); goto on_error;
free(line);
return -1; ret = lxc_safe_ulong(p1 + 1, &newmap->hostid);
} if (ret < 0)
goto on_error;
ret = lxc_safe_ulong(p2 + 1, &newmap->range);
if (ret < 0)
goto on_error;
newmap->hostid = atol(p1 + 1);
newmap->range = atol(p2 + 1);
newmap->nsid = 0; newmap->nsid = 0;
newmap->idtype = which; newmap->idtype = which;
ret = -1;
tmp = malloc(sizeof(*tmp)); tmp = malloc(sizeof(*tmp));
if (!tmp) { if (!tmp)
fclose(fin); goto on_error;
free(line);
free(newmap);
return -1;
}
tmp->elem = newmap; tmp->elem = newmap;
lxc_list_add_tail(&active_map, tmp); lxc_list_add_tail(&active_map, tmp);
break; break;
} }
free(line); ret = 0;
on_error:
fclose(fin); fclose(fin);
free(line);
free(newmap);
return 0; return ret;
} }
static int find_default_map(void) static int find_default_map(void)
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "lxccontainer.h" #include "lxccontainer.h"
#include "macro.h"
#include "network.h" #include "network.h"
#include "parse.h" #include "parse.h"
#include "utils.h" #include "utils.h"
...@@ -288,13 +289,12 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf) ...@@ -288,13 +289,12 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf)
TRACE("type: macvlan"); TRACE("type: macvlan");
if (netdev->priv.macvlan_attr.mode > 0) { if (netdev->priv.macvlan_attr.mode > 0) {
char *macvlan_mode; char *mode;
macvlan_mode = lxc_macvlan_flag_to_mode( mode = lxc_macvlan_flag_to_mode(
netdev->priv.macvlan_attr.mode); netdev->priv.macvlan_attr.mode);
TRACE("macvlan mode: %s", TRACE("macvlan mode: %s",
macvlan_mode ? macvlan_mode mode ? mode : "(invalid mode)");
: "(invalid mode)");
} }
break; break;
case LXC_NET_VLAN: case LXC_NET_VLAN:
...@@ -442,7 +442,7 @@ void lxc_free_networks(struct lxc_list *networks) ...@@ -442,7 +442,7 @@ void lxc_free_networks(struct lxc_list *networks)
lxc_list_init(networks); lxc_list_init(networks);
} }
static struct macvlan_mode { static struct lxc_macvlan_mode {
char *name; char *name;
int mode; int mode;
} macvlan_mode[] = { } macvlan_mode[] = {
......
...@@ -20,27 +20,13 @@ ...@@ -20,27 +20,13 @@
#ifndef __LXC_CONFILE_UTILS_H #ifndef __LXC_CONFILE_UTILS_H
#define __LXC_CONFILE_UTILS_H #define __LXC_CONFILE_UTILS_H
#include "config.h"
#include <stdbool.h> #include <stdbool.h>
#include "conf.h" #include "conf.h"
#include "confile_utils.h" #include "confile_utils.h"
#ifndef MACVLAN_MODE_PRIVATE
#define MACVLAN_MODE_PRIVATE 1
#endif
#ifndef MACVLAN_MODE_VEPA
#define MACVLAN_MODE_VEPA 2
#endif
#ifndef MACVLAN_MODE_BRIDGE
#define MACVLAN_MODE_BRIDGE 4
#endif
#ifndef MACVLAN_MODE_PASSTHRU
#define MACVLAN_MODE_PASSTHRU 8
#endif
#define strprint(str, inlen, ...) \ #define strprint(str, inlen, ...) \
do { \ do { \
if (str) \ if (str) \
......
...@@ -20,6 +20,16 @@ ...@@ -20,6 +20,16 @@
#ifndef __LXC_MACRO_H #ifndef __LXC_MACRO_H
#define __LXC_MACRO_H #define __LXC_MACRO_H
#include "config.h"
#include <asm/types.h>
#include <linux/if_link.h>
#include <linux/loop.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/mount.h>
#include <sys/socket.h>
/* Define __S_ISTYPE if missing from the C library. */ /* Define __S_ISTYPE if missing from the C library. */
#ifndef __S_ISTYPE #ifndef __S_ISTYPE
#define __S_ISTYPE(mode, mask) (((mode)&S_IFMT) == (mask)) #define __S_ISTYPE(mode, mask) (((mode)&S_IFMT) == (mask))
...@@ -187,4 +197,29 @@ extern int __build_bug_on_failed; ...@@ -187,4 +197,29 @@ extern int __build_bug_on_failed;
#define NLMSG_ERROR 0x2 #define NLMSG_ERROR 0x2
#endif #endif
#ifndef MACVLAN_MODE_PRIVATE
#define MACVLAN_MODE_PRIVATE 1
#endif
#ifndef MACVLAN_MODE_VEPA
#define MACVLAN_MODE_VEPA 2
#endif
#ifndef MACVLAN_MODE_BRIDGE
#define MACVLAN_MODE_BRIDGE 4
#endif
#ifndef MACVLAN_MODE_PASSTHRU
#define MACVLAN_MODE_PASSTHRU 8
#endif
/* mount */
#ifndef MS_REC
#define MS_REC 16384
#endif
#ifndef MS_SLAVE
#define MS_SLAVE (1 << 19)
#endif
#endif /* __LXC_MACRO_H */ #endif /* __LXC_MACRO_H */
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