Commit 741a2951 authored by JANAK DESAI's avatar JANAK DESAI Committed by Linus Torvalds

[PATCH] unshare system call -v5: unshare namespace

If the namespace structure is being shared, allocate a new one and copy
information from the current, shared, structure.
Signed-off-by: default avatarJanak Desai <janak@us.ibm.com>
Cc: Al Viro <viro@ftp.linux.org.uk>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Michael Kerrisk <mtk-manpages@gmx.net>
Cc: Andi Kleen <ak@muc.de>
Cc: Paul Mackerras <paulus@samba.org>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 99d1419d
...@@ -1325,27 +1325,17 @@ dput_out: ...@@ -1325,27 +1325,17 @@ dput_out:
return retval; return retval;
} }
int copy_namespace(int flags, struct task_struct *tsk) /*
* Allocate a new namespace structure and populate it with contents
* copied from the namespace of the passed in task structure.
*/
struct namespace *dup_namespace(struct task_struct *tsk, struct fs_struct *fs)
{ {
struct namespace *namespace = tsk->namespace; struct namespace *namespace = tsk->namespace;
struct namespace *new_ns; struct namespace *new_ns;
struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL; struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
struct fs_struct *fs = tsk->fs;
struct vfsmount *p, *q; struct vfsmount *p, *q;
if (!namespace)
return 0;
get_namespace(namespace);
if (!(flags & CLONE_NEWNS))
return 0;
if (!capable(CAP_SYS_ADMIN)) {
put_namespace(namespace);
return -EPERM;
}
new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL); new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
if (!new_ns) if (!new_ns)
goto out; goto out;
...@@ -1396,8 +1386,6 @@ int copy_namespace(int flags, struct task_struct *tsk) ...@@ -1396,8 +1386,6 @@ int copy_namespace(int flags, struct task_struct *tsk)
} }
up_write(&namespace_sem); up_write(&namespace_sem);
tsk->namespace = new_ns;
if (rootmnt) if (rootmnt)
mntput(rootmnt); mntput(rootmnt);
if (pwdmnt) if (pwdmnt)
...@@ -1405,12 +1393,40 @@ int copy_namespace(int flags, struct task_struct *tsk) ...@@ -1405,12 +1393,40 @@ int copy_namespace(int flags, struct task_struct *tsk)
if (altrootmnt) if (altrootmnt)
mntput(altrootmnt); mntput(altrootmnt);
put_namespace(namespace); out:
return 0; return new_ns;
}
int copy_namespace(int flags, struct task_struct *tsk)
{
struct namespace *namespace = tsk->namespace;
struct namespace *new_ns;
int err = 0;
if (!namespace)
return 0;
get_namespace(namespace);
if (!(flags & CLONE_NEWNS))
return 0;
if (!capable(CAP_SYS_ADMIN)) {
err = -EPERM;
goto out;
}
new_ns = dup_namespace(tsk, tsk->fs);
if (!new_ns) {
err = -ENOMEM;
goto out;
}
tsk->namespace = new_ns;
out: out:
put_namespace(namespace); put_namespace(namespace);
return -ENOMEM; return err;
} }
asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name, asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
......
...@@ -15,6 +15,7 @@ struct namespace { ...@@ -15,6 +15,7 @@ struct namespace {
extern int copy_namespace(int, struct task_struct *); extern int copy_namespace(int, struct task_struct *);
extern void __put_namespace(struct namespace *namespace); extern void __put_namespace(struct namespace *namespace);
extern struct namespace *dup_namespace(struct task_struct *, struct fs_struct *);
static inline void put_namespace(struct namespace *namespace) static inline void put_namespace(struct namespace *namespace)
{ {
......
...@@ -1388,16 +1388,21 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) ...@@ -1388,16 +1388,21 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
} }
/* /*
* Unsharing of namespace for tasks created without CLONE_NEWNS is not * Unshare the namespace structure if it is being shared
* supported yet
*/ */
static int unshare_namespace(unsigned long unshare_flags, struct namespace **new_nsp) static int unshare_namespace(unsigned long unshare_flags, struct namespace **new_nsp, struct fs_struct *new_fs)
{ {
struct namespace *ns = current->namespace; struct namespace *ns = current->namespace;
if ((unshare_flags & CLONE_NEWNS) && if ((unshare_flags & CLONE_NEWNS) &&
(ns && atomic_read(&ns->count) > 1)) (ns && atomic_read(&ns->count) > 1)) {
return -EINVAL; if (!capable(CAP_SYS_ADMIN))
return -EPERM;
*new_nsp = dup_namespace(current, new_fs ? new_fs : current->fs);
if (!*new_nsp)
return -ENOMEM;
}
return 0; return 0;
} }
...@@ -1482,7 +1487,7 @@ asmlinkage long sys_unshare(unsigned long unshare_flags) ...@@ -1482,7 +1487,7 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
goto bad_unshare_out; goto bad_unshare_out;
if ((err = unshare_fs(unshare_flags, &new_fs))) if ((err = unshare_fs(unshare_flags, &new_fs)))
goto bad_unshare_cleanup_thread; goto bad_unshare_cleanup_thread;
if ((err = unshare_namespace(unshare_flags, &new_ns))) if ((err = unshare_namespace(unshare_flags, &new_ns, new_fs)))
goto bad_unshare_cleanup_fs; goto bad_unshare_cleanup_fs;
if ((err = unshare_sighand(unshare_flags, &new_sigh))) if ((err = unshare_sighand(unshare_flags, &new_sigh)))
goto bad_unshare_cleanup_ns; goto bad_unshare_cleanup_ns;
......
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