mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: JANAK DESAI <janak@us.ibm.com>
To: viro@ftp.linux.org.uk, chrisw@osdl.org, dwmw2@infradead.org,
	jamie@shareable.org, serue@us.ibm.com, mingo@elte.hu,
	linuxram@us.ibm.com, jmorris@namei.org, sds@tycho.nsa.gov,
	janak@us.ibm.com
Cc: akpm@osdl.org, linux-kernel@vger.kernel.org
Subject: [PATCH -mm 7/9] unshare system call : allow unsharing of namespace
Date: Mon, 12 Dec 2005 22:00:09 -0500	[thread overview]
Message-ID: <1134442736.14136.128.camel@hobbs.atlanta.ibm.com> (raw)


[PATCH -mm 7/9] unshare system call: allow unsharing of namespace


 fs/namespace.c            |   55
++++++++++++++++++++++++++++++----------------
 include/linux/namespace.h |    1 
 kernel/fork.c             |   16 +++++++++----
 3 files changed, 48 insertions(+), 24 deletions(-)
 
 
diff -Naurp 2.6.15-rc5-mm2+patch/fs/namespace.c
2.6.15-rc5-mm2+patch7/fs/namespace.c
--- 2.6.15-rc5-mm2+patch/fs/namespace.c	2005-12-12 18:27:20.000000000
+0000
+++ 2.6.15-rc5-mm2+patch7/fs/namespace.c	2005-12-12 22:01:00.000000000
+0000
@@ -1314,7 +1314,11 @@ dput_out:
 	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 namespace *namespace = tsk->namespace;
 	struct namespace *new_ns;
@@ -1322,19 +1326,6 @@ int copy_namespace(int flags, struct tas
 	struct fs_struct *fs = tsk->fs;
 	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);
 	if (!new_ns)
 		goto out;
@@ -1385,8 +1376,6 @@ int copy_namespace(int flags, struct tas
 	}
 	up_write(&namespace_sem);
 
-	tsk->namespace = new_ns;
-
 	if (rootmnt)
 		mntput(rootmnt);
 	if (pwdmnt)
@@ -1394,12 +1383,40 @@ int copy_namespace(int flags, struct tas
 	if (altrootmnt)
 		mntput(altrootmnt);
 
-	put_namespace(namespace);
-	return 0;
+out:
+	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);
+	if (!new_ns) {
+		err = -ENOMEM;
+		goto out;
+	}
+
+	tsk->namespace = new_ns;
 
 out:
 	put_namespace(namespace);
-	return -ENOMEM;
+	return err;
 }
 
 asmlinkage long sys_mount(char __user * dev_name, char __user *
dir_name,
diff -Naurp 2.6.15-rc5-mm2+patch/include/linux/namespace.h
2.6.15-rc5-mm2+patch7/include/linux/namespace.h
--- 2.6.15-rc5-mm2+patch/include/linux/namespace.h	2005-12-12
18:27:38.000000000 +0000
+++ 2.6.15-rc5-mm2+patch7/include/linux/namespace.h	2005-12-12
22:01:57.000000000 +0000
@@ -15,6 +15,7 @@ struct namespace {
 
 extern int copy_namespace(int, struct task_struct *);
 extern void __put_namespace(struct namespace *namespace);
+extern struct namespace *dup_namespace(struct task_struct *);
 
 static inline void put_namespace(struct namespace *namespace)
 {
diff -Naurp 2.6.15-rc5-mm2+patch/kernel/fork.c
2.6.15-rc5-mm2+patch7/kernel/fork.c
--- 2.6.15-rc5-mm2+patch/kernel/fork.c	2005-12-12 19:31:48.000000000
+0000
+++ 2.6.15-rc5-mm2+patch7/kernel/fork.c	2005-12-12 21:58:44.000000000
+0000
@@ -1392,16 +1392,22 @@ static int unshare_fs(unsigned long unsh
 }
 
 /*
- * Unsharing of namespace for tasks created without CLONE_NEWNS is not
- * supported yet
+ * Unshare the namespace structure if it is being shared
  */
 static int unshare_namespace(unsigned long unshare_flags, struct
namespace **new_nsp)
 {
-	struct namespace *ns = current->namespace;
+	struct namespace *ns = current->namespace, *new_ns;
 
 	if ((unshare_flags & CLONE_NEWNS) &&
-	    (ns && atomic_read(&ns->count) > 1))
-		return -EINVAL;
+	    (ns && atomic_read(&ns->count) > 1)) {
+		if (!capable(CAP_SYS_ADMIN))
+			return -EPERM;
+
+		new_ns = dup_namespace(current);
+		if (!new_ns)
+			return -ENOMEM;
+		*new_nsp = new_ns;
+	}
 
 	return 0;
 }



             reply	other threads:[~2005-12-13  3:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-13  3:00 JANAK DESAI [this message]
2005-12-13 13:52 ` Al Viro
2005-12-13 14:11   ` JANAK DESAI
2005-12-13 13:43 JANAK DESAI
2005-12-13 22:54 [PATCH -mm 7/9] unshare system call: " JANAK DESAI

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1134442736.14136.128.camel@hobbs.atlanta.ibm.com \
    --to=janak@us.ibm.com \
    --cc=akpm@osdl.org \
    --cc=chrisw@osdl.org \
    --cc=dwmw2@infradead.org \
    --cc=jamie@shareable.org \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxram@us.ibm.com \
    --cc=mingo@elte.hu \
    --cc=sds@tycho.nsa.gov \
    --cc=serue@us.ibm.com \
    --cc=viro@ftp.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®