lxc_usernsexec.c revision 13bc2fd2370f8e3778d81f58ac8dda5746550a4f
/*
* (C) Copyright IBM Corp. 2008
* (C) Copyright Canonical, Inc 2010-2013
*
* Authors:
* Serge Hallyn <serge.hallyn@ubuntu.com>
* (Once upon a time, this was based on nsexec from the IBM
* container tools)
*
* 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 "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <fcntl.h>
#include <sched.h>
#include <pwd.h>
#include <grp.h>
#include "conf.h"
#include "namespace.h"
#include "utils.h"
#ifndef MS_REC
#define MS_REC 16384
#endif
#ifndef MS_SLAVE
#endif
{
printf("\n");
printf(" -h this message\n");
printf("\n");
printf(" -m <uid-maps> uid maps to use\n");
printf("\n");
printf(" uid-maps: [u|g|b]:ns_id:host_id:range\n");
printf(" [u|g|b]: map user id, group id, or both\n");
printf(" ns_id: the base id in the new namespace\n");
printf(" host_id: the base id in the parent namespace\n");
printf(" range: how many ids to map\n");
printf(" Note: This program uses newuidmap(2) and newgidmap(2).\n");
printf(" calling user permission to use the mapped ranges\n");
exit(1);
}
if (fd == -1) {
return;
}
flags &= ~O_NONBLOCK;
return;
}
for (i = 0; i < fd; i++)
close(i);
for (i = 0; i < 3; i++)
if (fd != i)
if (fd >= 3)
}
// Code copy end
{
// Assume we want to become root
if (setgid(0) < 0) {
perror("setgid");
return -1;
}
if (setuid(0) < 0) {
perror("setuid");
return -1;
}
perror("setgroups");
return -1;
}
if (unshare(CLONE_NEWNS) < 0) {
perror("unshare CLONE_NEWNS");
return -1;
}
if (detect_shared_rootfs()) {
printf("Failed to make / rslave");
return -1;
}
}
perror("execvpe");
return -1;
}
static struct lxc_list active_map;
/*
* given a string like "b:0:100000:10", map both uids and gids
* 0-10 to 100000 to 100010
*/
{
int ret;
int i;
char which;
if (!map)
return -1;
if (ret != 4)
return -1;
return -1;
for (i = 0; i < 2; i++) {
continue;
if (!newmap)
return -1;
if (types[i] == 'u')
else
if (!tmp) {
return -1;
}
}
return 0;
}
/*
* This is called if the user did not pass any uid ranges in
* through -m flags. It's called once to get the default uid
* map, and once for the default gid map.
* allowed map. We only use the first one for each of uid and
* gid, because otherwise we're not sure which entries the user
* wanted.
*/
{
if (!fin)
return -1;
continue;
if (!p1)
continue;
if (!p2)
continue;
if (!newmap) {
return -1;
}
if (!tmp) {
return -1;
}
break;
}
if (line)
return 0;
}
#define subuidfile "/etc/subuid"
#define subgidfile "/etc/subgid"
static int find_default_map(void)
{
if (!p)
return -1;
return -1;
return -1;
return 0;
}
{
int c;
char ttyname[256];
int status;
int ret;
int pid;
char buf[1];
if (ret < 0) {
perror("readlink on fd 0");
exit(1);
}
switch (c) {
case 'h':
default:
}
};
if (lxc_list_empty(&active_map)) {
if (find_default_map()) {
exit(1);
}
}
if (argc < 1) {
argv = default_args;
argc = 1;
}
perror("pipe");
exit(1);
}
// Child.
if (ret < 0) {
perror("unshare");
return 1;
}
buf[0] = '1';
perror("write pipe");
exit(1);
}
perror("read pipe");
exit(1);
}
if (buf[0] != '1') {
exit(1);
}
}
perror("read pipe");
exit(1);
}
buf[0] = '1';
ret = 0;
}
perror("write to pipe");
exit(1);
}
exit(1);
}
}