Skip to content

Commit b2e0d98

Browse files
committed
userns: Implement unshare of the user namespace
- Add CLONE_THREAD to the unshare flags if CLONE_NEWUSER is selected As changing user namespaces is only valid if all there is only a single thread. - Restore the code to add CLONE_VM if CLONE_THREAD is selected and the code to addCLONE_SIGHAND if CLONE_VM is selected. Making the constraints in the code clear. Acked-by: Serge Hallyn <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent cde1975 commit b2e0d98

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

include/linux/nsproxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void exit_task_namespaces(struct task_struct *tsk);
6767
void switch_task_namespaces(struct task_struct *tsk, struct nsproxy *new);
6868
void free_nsproxy(struct nsproxy *ns);
6969
int unshare_nsproxy_namespaces(unsigned long, struct nsproxy **,
70-
struct fs_struct *);
70+
struct cred *, struct fs_struct *);
7171
int __init nsproxy_cache_init(void);
7272

7373
static inline void put_nsproxy(struct nsproxy *ns)

include/linux/user_namespace.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
3939
}
4040

4141
extern int create_user_ns(struct cred *new);
42+
extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
4243
extern void free_user_ns(struct kref *kref);
4344

4445
static inline void put_user_ns(struct user_namespace *ns)
@@ -66,6 +67,14 @@ static inline int create_user_ns(struct cred *new)
6667
return -EINVAL;
6768
}
6869

70+
static inline int unshare_userns(unsigned long unshare_flags,
71+
struct cred **new_cred)
72+
{
73+
if (unshare_flags & CLONE_NEWUSER)
74+
return -EINVAL;
75+
return 0;
76+
}
77+
6978
static inline void put_user_ns(struct user_namespace *ns)
7079
{
7180
}

kernel/fork.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ static int check_unshare_flags(unsigned long unshare_flags)
16871687
if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
16881688
CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
16891689
CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
1690-
CLONE_NEWPID))
1690+
CLONE_NEWUSER|CLONE_NEWPID))
16911691
return -EINVAL;
16921692
/*
16931693
* Not implemented, but pretend it works if there is nothing to
@@ -1754,10 +1754,16 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
17541754
{
17551755
struct fs_struct *fs, *new_fs = NULL;
17561756
struct files_struct *fd, *new_fd = NULL;
1757+
struct cred *new_cred = NULL;
17571758
struct nsproxy *new_nsproxy = NULL;
17581759
int do_sysvsem = 0;
17591760
int err;
17601761

1762+
/*
1763+
* If unsharing a user namespace must also unshare the thread.
1764+
*/
1765+
if (unshare_flags & CLONE_NEWUSER)
1766+
unshare_flags |= CLONE_THREAD;
17611767
/*
17621768
* If unsharing a pid namespace must also unshare the thread.
17631769
*/
@@ -1795,11 +1801,15 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
17951801
err = unshare_fd(unshare_flags, &new_fd);
17961802
if (err)
17971803
goto bad_unshare_cleanup_fs;
1798-
err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, new_fs);
1804+
err = unshare_userns(unshare_flags, &new_cred);
17991805
if (err)
18001806
goto bad_unshare_cleanup_fd;
1807+
err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
1808+
new_cred, new_fs);
1809+
if (err)
1810+
goto bad_unshare_cleanup_cred;
18011811

1802-
if (new_fs || new_fd || do_sysvsem || new_nsproxy) {
1812+
if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
18031813
if (do_sysvsem) {
18041814
/*
18051815
* CLONE_SYSVSEM is equivalent to sys_exit().
@@ -1832,11 +1842,20 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
18321842
}
18331843

18341844
task_unlock(current);
1845+
1846+
if (new_cred) {
1847+
/* Install the new user namespace */
1848+
commit_creds(new_cred);
1849+
new_cred = NULL;
1850+
}
18351851
}
18361852

18371853
if (new_nsproxy)
18381854
put_nsproxy(new_nsproxy);
18391855

1856+
bad_unshare_cleanup_cred:
1857+
if (new_cred)
1858+
put_cred(new_cred);
18401859
bad_unshare_cleanup_fd:
18411860
if (new_fd)
18421861
put_files_struct(new_fd);

kernel/nsproxy.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void free_nsproxy(struct nsproxy *ns)
186186
* On success, returns the new nsproxy.
187187
*/
188188
int unshare_nsproxy_namespaces(unsigned long unshare_flags,
189-
struct nsproxy **new_nsp, struct fs_struct *new_fs)
189+
struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs)
190190
{
191191
struct user_namespace *user_ns;
192192
int err = 0;
@@ -195,12 +195,12 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags,
195195
CLONE_NEWNET | CLONE_NEWPID)))
196196
return 0;
197197

198-
if (!nsown_capable(CAP_SYS_ADMIN))
198+
user_ns = new_cred ? new_cred->user_ns : current_user_ns();
199+
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
199200
return -EPERM;
200201

201-
user_ns = current_user_ns();
202202
*new_nsp = create_new_namespaces(unshare_flags, current, user_ns,
203-
new_fs ? new_fs : current->fs);
203+
new_fs ? new_fs : current->fs);
204204
if (IS_ERR(*new_nsp)) {
205205
err = PTR_ERR(*new_nsp);
206206
goto out;

kernel/user_namespace.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ int create_user_ns(struct cred *new)
8282
return 0;
8383
}
8484

85+
int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
86+
{
87+
struct cred *cred;
88+
89+
if (!(unshare_flags & CLONE_NEWUSER))
90+
return 0;
91+
92+
cred = prepare_creds();
93+
if (!cred)
94+
return -ENOMEM;
95+
96+
*new_cred = cred;
97+
return create_user_ns(cred);
98+
}
99+
85100
void free_user_ns(struct kref *kref)
86101
{
87102
struct user_namespace *parent, *ns =

0 commit comments

Comments
 (0)