man-pages/man2/unshare.2

403 lines
11 KiB
Groff
Raw Normal View History

.\" Copyright (C) 2006, Janak Desai <janak@us.ibm.com>
.\" and Copyright (C) 2006, 2012 Michael Kerrisk <mtk.manpages@gmail.com>
2013-03-10 09:29:51 +00:00
.\"
.\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
2006-03-20 21:29:29 +00:00
.\" Licensed under the GPL
.\" %%%LICENSE_END
2006-03-20 21:29:29 +00:00
.\"
.\" Patch Justification:
.\" unshare system call is needed to implement, using PAM,
.\" per-security_context and/or per-user namespace to provide
.\" polyinstantiated directories. Using unshare and bind mounts, a
.\" PAM module can create private namespace with appropriate
.\" directories(based on user's security context) bind mounted on
.\" public directories such as /tmp, thus providing an instance of
.\" /tmp that is based on user's security context. Without the
.\" unshare system call, namespace separation can only be achieved
.\" by clone, which would require porting and maintaining all commands
.\" such as login, and su, that establish a user session.
.\"
.TH UNSHARE 2 2013-04-17 "Linux" "Linux Programmer's Manual"
2006-03-20 21:29:29 +00:00
.SH NAME
unshare \- disassociate parts of the process execution context
.SH SYNOPSIS
.nf
.B #include <sched.h>
.sp
.BI "int unshare(int " flags );
.fi
.sp
.in -4n
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.in
.sp
.BR unshare ():
.ad l
.RS 4
.PD 0
.TP 4
Since glibc 2.14:
_GNU_SOURCE
.TP 4
.\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
Before glibc 2.14:
_BSD_SOURCE || _SVID_SOURCE
/* _GNU_SOURCE also suffices */
.PD
.RE
.ad b
2006-03-20 21:29:29 +00:00
.SH DESCRIPTION
.BR unshare ()
2006-03-20 21:29:29 +00:00
allows a process to disassociate parts of its execution
context that are currently being shared with other processes.
Part of the execution context, such as the mount namespace, is shared
implicitly when a new process is created using
2006-03-20 21:29:29 +00:00
.BR fork (2)
or
.BR vfork (2),
2006-03-20 21:29:29 +00:00
while other parts, such as virtual memory, may be
shared by explicit request when creating a process using
2006-03-20 21:29:29 +00:00
.BR clone (2).
The main use of
2006-03-20 21:29:29 +00:00
.BR unshare ()
is to allow a process to control its
shared execution context without creating a new process.
The
.I flags
argument is a bit mask that specifies which parts of
the execution context should be unshared.
2006-03-20 21:29:29 +00:00
This argument is specified by ORing together zero or more
of the following constants:
.TP
.B CLONE_FILES
Reverse the effect of the
.BR clone (2)
.B CLONE_FILES
flag.
Unshare the file descriptor table, so that the calling process
2006-03-20 21:29:29 +00:00
no longer shares its file descriptors with any other process.
.TP
.B CLONE_FS
Reverse the effect of the
.BR clone (2)
.B CLONE_FS
2006-03-20 21:29:29 +00:00
flag.
intro.1, time.1, access.2, acct.2, alloc_hugepages.2, bind.2, chdir.2, chmod.2, chown.2, chroot.2, clone.2, close.2, execve.2, fallocate.2, fcntl.2, getdents.2, getrusage.2, getxattr.2, init_module.2, inotify_add_watch.2, ioprio_set.2, kcmp.2, link.2, listxattr.2, lseek.2, madvise.2, mkdir.2, mknod.2, mmap.2, mount.2, move_pages.2, msgctl.2, nfsservctl.2, open.2, pivot_root.2, quotactl.2, read.2, readlink.2, removexattr.2, rename.2, rmdir.2, semctl.2, setfsgid.2, setfsuid.2, setresuid.2, setuid.2, setup.2, setxattr.2, shmctl.2, splice.2, spu_create.2, stat.2, statfs.2, swapon.2, symlink.2, sync.2, sync_file_range.2, sysfs.2, truncate.2, umount.2, unlink.2, unshare.2, ustat.2, utime.2, utimensat.2, write.2, btree.3, errno.3, fexecve.3, ftw.3, futimes.3, get_nprocs_conf.3, getcwd.3, getdirentries.3, getmntent.3, glob.3, mkfifo.3, mq_open.3, readdir.3, realpath.3, recno.3, remove.3, sem_open.3, shm_open.3, statvfs.3, sysconf.3, telldir.3, tmpfile.3, cciss.4, initrd.4, pts.4, sk98lin.4, vcs.4, core.5, filesystems.5, proc.5, boot.7, bootparam.7, capabilities.7, cpuset.7, credentials.7, feature_test_macros.7, fifo.7, hier.7, inotify.7, intro.7, mq_overview.7, path_resolution.7, pipe.7, sem_overview.7, shm_overview.7, spufs.7, symlink.7, unix.7, uri.7, sync.8: Global fix: s/file system/filesystem/ Notwithstanding 24d01c530c5a3f75217543d02bf6712395e5f90c, "filesystem" is the form used by the great majority of man pages outside the man-pages project and in a number of other sources, so let's go with that. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2013-08-08 08:07:57 +00:00
Unshare filesystem attributes, so that the calling process
no longer shares its root directory
.RB ( chroot (2)),
current directory
.RB ( chdir (2)),
or umask
.RB ( umask (2))
attributes with any other process.
2006-03-20 21:29:29 +00:00
.TP
.BR CLONE_NEWIPC " (since Linux 2.6.19)
This flag has the same effect as the
.BR clone (2)
.B CLONE_NEWIPC
flag.
Unshare the System\ V IPC namespace,
so that the calling process has a private copy of the
System\ V IPC namespace which is not shared with any other process.
Specifying this flag automatically implies
.BR CLONE_SYSVSEM
as well.
Use of
.BR CLONE_NEWIPC
requires the
.BR CAP_SYS_ADMIN
capability.
.TP
.BR CLONE_NEWNET " (since Linux 2.6.24)
This flag has the same effect as the
.BR clone (2)
.B CLONE_NEWNET
flag.
Unshare the network namespace,
so that the calling process is moved into a
new network namespace which is not shared
with any previously existing process.
Use of
.BR CLONE_NEWNET
requires the
.BR CAP_SYS_ADMIN
capability.
.TP
2006-03-20 21:29:29 +00:00
.B CLONE_NEWNS
.\" These flag name are inconsistent:
.\" CLONE_NEWNS does the same thing in clone(), but CLONE_VM,
2006-03-20 21:29:29 +00:00
.\" CLONE_FS, and CLONE_FILES reverse the action of the clone()
.\" flags of the same name.
This flag has the same effect as the
2006-03-20 21:29:29 +00:00
.BR clone (2)
.B CLONE_NEWNS
flag.
Unshare the mount namespace,
so that the calling process has a private copy of
2006-03-20 21:29:29 +00:00
its namespace which is not shared with any other process.
Specifying this flag automatically implies
.B CLONE_FS
as well.
Use of
.BR CLONE_NEWNS
requires the
.BR CAP_SYS_ADMIN
capability.
.TP
.BR CLONE_NEWPID " (since Linux 3.8)"
This flag has the same effect as the
.BR clone (2)
.B CLONE_NEWPID
flag.
Unshare the PID namespace,
so that the calling process has a new PID namespace for its children
which is not shared with any previously existing process.
The calling process is
.I not
moved into the new namespace.
The first child created by the calling process will have
the process ID 1 and will assume the role of
.BR init (1)
in the new namespace.
Use of
.BR CLONE_NEWPID
requires the
.BR CAP_SYS_ADMIN
capability.
.TP
.BR CLONE_NEWUSER " (since Linux 3.8)"
This flag has the same effect as the
.BR clone (2)
.B CLONE_NEWUSER
flag.
Unshare the user namespace,
so that the calling process is moved into a new user namespace
which is not shared with any previously existing process.
like the child process created by
.BR clone (2)
with the
.B CLONE_NEWUSER
flag, the caller obtains a full set of capabilities in the new namespace.
.BR CLONE_NEWUSER
requires that the calling process is not threaded and the user ID and group ID
of the calling process are mapped user IDs and group IDs in the
user namespace of the calling process at the time of the call.
.TP
.BR CLONE_NEWUTS " (since Linux 2.6.19)"
This flag has the same effect as the
.BR clone (2)
.B CLONE_NEWUTS
flag.
Unshare the UTS IPC namespace,
so that the calling process has a private copy of the
UTS namespace which is not shared with any other process.
Use of
.BR CLONE_NEWUTS
requires the
.BR CAP_SYS_ADMIN
capability.
.TP
.BR CLONE_SYSVSEM " (since Linux 2.6.26)
.\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7
This flag reverses the effect of the
.BR clone (2)
.B CLONE_SYSVSEM
flag.
Unshare System\ V semaphore undo values,
so that the calling process has a private copy
which is not shared with any other process.
Use of
.BR CLONE_SYSVSEM
requires the
.BR CAP_SYS_ADMIN
capability.
2006-03-20 21:29:29 +00:00
.\" As at 2.6.16, the following forced implications also apply,
.\" although the relevant flags are not yet implemented.
2006-03-20 21:29:29 +00:00
.\" If CLONE_THREAD is set force CLONE_VM.
.\" If CLONE_VM is set, force CLONE_SIGHAND.
.\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared
2006-03-20 21:29:29 +00:00
.\" (i.e., current->signal->count > 1), force CLONE_THREAD.
.\"
.\" FIXME . CLONE_VM is not (yet, as at 2.6.16) implemented.
.\" .TP
.\" .B CLONE_VM
.\" Reverse the effect of the
.\" .BR clone (2)
.\" .B CLONE_VM
.\" flag.
.\" .RB ( CLONE_VM
.\" is also implicitly set by
.\" .BR vfork (2),
.\" and can be reversed using this
.\" .BR unshare ()
.\" flag.)
.\" Unshare virtual memory, so that the calling process no
.\" longer shares its virtual address space with any other process.
.PP
If
2006-03-20 21:29:29 +00:00
.I flags
is specified as zero, then
.BR unshare ()
is a no-op;
no changes are made to the calling process's execution context.
.SH RETURN VALUE
On success, zero returned.
On failure, \-1 is returned and
.I errno
2006-03-20 21:29:29 +00:00
is set to indicate the error.
.SH ERRORS
.TP
2007-08-27 07:56:52 +00:00
.B EINVAL
An invalid bit was specified in
.IR flags .
.TP
.B ENOMEM
Cannot allocate sufficient memory to copy parts of caller's
context that need to be unshared.
.TP
2006-03-20 21:29:29 +00:00
.B EPERM
The calling process did not have the required privileges for this operation.
2007-05-16 04:39:23 +00:00
.SH VERSIONS
2006-03-20 21:29:29 +00:00
The
.BR unshare ()
system call was added to Linux in kernel 2.6.16.
2007-05-18 16:30:46 +00:00
.SH CONFORMING TO
The
.BR unshare ()
2007-12-25 21:28:09 +00:00
system call is Linux-specific.
2007-05-16 04:39:23 +00:00
.SH NOTES
Not all of the process attributes that can be shared when
2006-03-20 21:29:29 +00:00
a new process is created using
.BR clone (2)
can be unshared using
.BR unshare ().
In particular, as at kernel 3.8,
.\" FIXME all of the following needs to be reviewed for the current kernel
.BR unshare ()
2006-03-20 21:29:29 +00:00
does not implement flags that reverse the effects of
.BR CLONE_SIGHAND ,
.\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
2006-03-20 21:29:29 +00:00
.\" was not specified when doing clone(); i.e., unsharing
.\" signal handlers is permitted if we are not actually
.\" sharing signal handlers. mtk
.BR CLONE_THREAD ,
or
.BR CLONE_VM .
.\" However, we can do unshare(CLONE_VM) if CLONE_VM
.\" was not specified when doing clone(); i.e., unsharing
.\" virtual memory is permitted if we are not actually
.\" sharing virtual memory. mtk
2006-03-20 21:29:29 +00:00
Such functionality may be added in the future, if required.
.\"
.\"9) Future Work
.\"--------------
.\"The current implementation of unshare does not allow unsharing of
.\"signals and signal handlers. Signals are complex to begin with and
.\"to unshare signals and/or signal handlers of a currently running
.\"process is even more complex. If in the future there is a specific
.\"need to allow unsharing of signals and/or signal handlers, it can
.\"be incrementally added to unshare without affecting legacy
.\"applications using unshare.
.\"
.SH EXAMPLE
The program below provides a simple implementation of the
.BR unshare (1)
command, which unshares one or more namespaces and executes the
command supplied in its command line arguments.
Here's an example of the use of this program,
running a shell in a new mount namespace,
and verifying that the original shell and the
new shell are in separate mount namespaces:
.in +4n
.nf
$ \fBreadlink /proc/$$/ns/mnt\fP
mnt:[4026531840]
$ \fBsudo ./unshare -m /bin/bash\fP
[sudo] password for cecilia:
# \fBreadlink /proc/$$/ns/mnt\fP
mnt:[4026532325]
.fi
.in
The differing output of the two
.BR readlink (1)
commands shows that the two shells are in different mount namespaces.
.SS Program source
\&
.nf
/* unshare.c
A simple implementation of the unshare(1) command: unshare
namespaces and execute a command.
*/
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
/* A simple error\-handling function: print an error message based
on the value in \(aqerrno\(aq and terminate the calling process */
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
} while (0)
static void
usage(char *pname)
{
fprintf(stderr, "Usage: %s [options] program [arg...]\\n", pname);
fprintf(stderr, "Options can be:\\n");
fprintf(stderr, " \-i unshare IPC namespace\\n");
fprintf(stderr, " \-m unshare mount namespace\\n");
fprintf(stderr, " \-n unshare network namespace\\n");
fprintf(stderr, " \-p unshare PID namespace\\n");
fprintf(stderr, " \-u unshare UTS namespace\\n");
fprintf(stderr, " \-U unshare user namespace\\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int flags, opt;
flags = 0;
while ((opt = getopt(argc, argv, "imnpuU")) != \-1) {
switch (opt) {
case \(aqi\(aq: flags |= CLONE_NEWIPC; break;
case \(aqm\(aq: flags |= CLONE_NEWNS; break;
case \(aqn\(aq: flags |= CLONE_NEWNET; break;
case \(aqp\(aq: flags |= CLONE_NEWPID; break;
case \(aqu\(aq: flags |= CLONE_NEWUTS; break;
case \(aqU\(aq: flags |= CLONE_NEWUSER; break;
default: usage(argv[0]);
}
}
if (optind >= argc)
usage(argv[0]);
if (unshare(flags) == \-1)
errExit("unshare");
execvp(argv[optind], &argv[optind]);
errExit("execvp");
}
.fi
2006-03-20 21:29:29 +00:00
.SH SEE ALSO
.BR unshare (1),
.BR clone (2),
.BR fork (2),
.BR kcmp (2),
2013-01-01 00:08:39 +00:00
.BR setns (2),
.BR vfork (2),
.BR namespaces (7)
.I Documentation/unshare.txt
in the Linux kernel source tree