Commit b60ed720 by Serge Hallyn

A few changes for unprivileged lxc-start

When doing reboot test, must add clone_newuser to clone flags, else we can't clone(CLONE_NEWPID). If we don't have caps at lxc-start, don't refuse to start. Drop the lxc_caps_check() function altogether as it is unused now. Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent b113383b
...@@ -227,42 +227,4 @@ int lxc_caps_last_cap(void) ...@@ -227,42 +227,4 @@ int lxc_caps_last_cap(void)
return last_cap; return last_cap;
} }
/*
* check if we have the caps needed to start a container. returns 1 on
* success, 0 on error. (I'd prefer this be a bool, but am afraid that
* might fail to build on some distros).
*/
int lxc_caps_check(void)
{
uid_t uid = getuid();
cap_t caps;
cap_flag_value_t value;
int i, ret;
cap_value_t needed_caps[] = { CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_SETUID, CAP_SETGID };
#define NUMCAPS ((int) (sizeof(needed_caps) / sizeof(cap_t)))
if (!uid)
return 1;
caps = cap_get_proc();
if (!caps) {
ERROR("failed to cap_get_proc: %m");
return 0;
}
for (i=0; i<NUMCAPS; i++) {
ret = cap_get_flag(caps, needed_caps[i], CAP_EFFECTIVE, &value);
if (ret) {
ERROR("Failed to cap_get_flag: %m");
return 0;
}
if (!value) {
return 0;
}
}
return 1;
}
#endif #endif
...@@ -30,7 +30,6 @@ extern int lxc_caps_reset(void); ...@@ -30,7 +30,6 @@ extern int lxc_caps_reset(void);
extern int lxc_caps_down(void); extern int lxc_caps_down(void);
extern int lxc_caps_up(void); extern int lxc_caps_up(void);
extern int lxc_caps_init(void); extern int lxc_caps_init(void);
extern int lxc_caps_check(void);
extern int lxc_caps_last_cap(void); extern int lxc_caps_last_cap(void);
#else #else
...@@ -46,9 +45,6 @@ static inline int lxc_caps_up(void) { ...@@ -46,9 +45,6 @@ static inline int lxc_caps_up(void) {
static inline int lxc_caps_init(void) { static inline int lxc_caps_init(void) {
return 0; return 0;
} }
static inline int lxc_caps_check(void) {
return 1;
}
static inline int lxc_caps_last_cap(void) { static inline int lxc_caps_last_cap(void) {
return 0; return 0;
......
...@@ -227,14 +227,6 @@ int main(int argc, char *argv[]) ...@@ -227,14 +227,6 @@ int main(int argc, char *argv[])
} }
if (my_args.daemonize) { if (my_args.daemonize) {
/* do an early check for needed privs, since otherwise the
* user won't see the error */
if (!lxc_caps_check()) {
ERROR("Not running with sufficient privilege");
goto out;
}
if (daemon(0, 0)) { if (daemon(0, 0)) {
SYSERROR("failed to daemonize '%s'", my_args.name); SYSERROR("failed to daemonize '%s'", my_args.name);
goto out; goto out;
......
...@@ -253,17 +253,10 @@ out_sigfd: ...@@ -253,17 +253,10 @@ out_sigfd:
return -1; return -1;
} }
extern int lxc_caps_check(void);
struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath) struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath)
{ {
struct lxc_handler *handler; struct lxc_handler *handler;
if (!lxc_caps_check()) {
ERROR("Not running with sufficient privilege");
return NULL;
}
handler = malloc(sizeof(*handler)); handler = malloc(sizeof(*handler));
if (!handler) if (!handler)
return NULL; return NULL;
...@@ -417,10 +410,10 @@ static int container_reboot_supported(void *arg) ...@@ -417,10 +410,10 @@ static int container_reboot_supported(void *arg)
return 0; return 0;
} }
static int must_drop_cap_sys_boot(void) static int must_drop_cap_sys_boot(struct lxc_conf *conf)
{ {
FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
int ret, cmd, v; int ret, cmd, v, flags;
long stack_size = 4096; long stack_size = 4096;
void *stack = alloca(stack_size); void *stack = alloca(stack_size);
int status; int status;
...@@ -439,11 +432,15 @@ static int must_drop_cap_sys_boot(void) ...@@ -439,11 +432,15 @@ static int must_drop_cap_sys_boot(void)
} }
cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
flags = CLONE_NEWPID | SIGCHLD;
if (!lxc_list_empty(&conf->id_map))
flags |= CLONE_NEWUSER;
#ifdef __ia64__ #ifdef __ia64__
pid = __clone2(container_reboot_supported, stack, stack_size, CLONE_NEWPID | SIGCHLD, &cmd); pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd);
#else #else
stack += stack_size; stack += stack_size;
pid = clone(container_reboot_supported, stack, CLONE_NEWPID | SIGCHLD, &cmd); pid = clone(container_reboot_supported, stack, flags, &cmd);
#endif #endif
if (pid < 0) { if (pid < 0) {
SYSERROR("failed to clone\n"); SYSERROR("failed to clone\n");
...@@ -668,6 +665,9 @@ int lxc_spawn(struct lxc_handler *handler) ...@@ -668,6 +665,9 @@ int lxc_spawn(struct lxc_handler *handler)
curcgroup = alloca(len); curcgroup = alloca(len);
if (lxc_curcgroup(curcgroup, len) <= 1) if (lxc_curcgroup(curcgroup, len) <= 1)
curcgroup = NULL; curcgroup = NULL;
FILE *f = fopen("/tmp/a", "a");
fprintf(f, "curcgroup is %s\n", curcgroup);
fclose(f);
} }
if ((handler->cgroup = lxc_cgroup_path_create(curcgroup, name)) == NULL) if ((handler->cgroup = lxc_cgroup_path_create(curcgroup, name)) == NULL)
goto out_delete_net; goto out_delete_net;
...@@ -776,7 +776,7 @@ int __lxc_start(const char *name, struct lxc_conf *conf, ...@@ -776,7 +776,7 @@ int __lxc_start(const char *name, struct lxc_conf *conf,
handler->ops = ops; handler->ops = ops;
handler->data = data; handler->data = data;
if (must_drop_cap_sys_boot()) { if (must_drop_cap_sys_boot(handler->conf)) {
#if HAVE_SYS_CAPABILITY_H #if HAVE_SYS_CAPABILITY_H
DEBUG("Dropping cap_sys_boot\n"); DEBUG("Dropping cap_sys_boot\n");
#else #else
......
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