Skip to content

Commit b0e7759

Browse files
hallyntorvalds
authored andcommitted
userns: user namespaces: convert several capable() calls
CAP_IPC_OWNER and CAP_IPC_LOCK can be checked against current_user_ns(), because the resource comes from current's own ipc namespace. setuid/setgid are to uids in own namespace, so again checks can be against current_user_ns(). Changelog: Jan 11: Use task_ns_capable() in place of sched_capable(). Jan 11: Use nsown_capable() as suggested by Bastian Blank. Jan 11: Clarify (hopefully) some logic in futex and sched.c Feb 15: use ns_capable for ipc, not nsown_capable Feb 23: let copy_ipcs handle setting ipc_ns->user_ns Feb 23: pass ns down rather than taking it from current [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com> Acked-by: "Eric W. Biederman" <ebiederm@xmission.com> Acked-by: Daniel Lezcano <daniel.lezcano@free.fr> Acked-by: David Howells <dhowells@redhat.com> Cc: James Morris <jmorris@namei.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b515498 commit b0e7759

13 files changed

Lines changed: 75 additions & 45 deletions

File tree

‎include/linux/ipc_namespace.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/idr.h>
66
#include <linux/rwsem.h>
77
#include <linux/notifier.h>
8+
#include <linux/nsproxy.h>
89

910
/*
1011
* ipc namespace events
@@ -93,7 +94,7 @@ static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
9394

9495
#if defined(CONFIG_IPC_NS)
9596
extern struct ipc_namespace *copy_ipcs(unsigned long flags,
96-
struct ipc_namespace *ns);
97+
struct task_struct *tsk);
9798
static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
9899
{
99100
if (ns)
@@ -104,12 +105,12 @@ static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
104105
extern void put_ipc_ns(struct ipc_namespace *ns);
105106
#else
106107
static inline struct ipc_namespace *copy_ipcs(unsigned long flags,
107-
struct ipc_namespace *ns)
108+
struct task_struct *tsk)
108109
{
109110
if (flags & CLONE_NEWIPC)
110111
return ERR_PTR(-EINVAL);
111112

112-
return ns;
113+
return tsk->nsproxy->ipc_ns;
113114
}
114115

115116
static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)

‎ipc/msg.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
421421
return -EFAULT;
422422
}
423423

424-
ipcp = ipcctl_pre_down(&msg_ids(ns), msqid, cmd,
424+
ipcp = ipcctl_pre_down(ns, &msg_ids(ns), msqid, cmd,
425425
&msqid64.msg_perm, msqid64.msg_qbytes);
426426
if (IS_ERR(ipcp))
427427
return PTR_ERR(ipcp);
@@ -539,7 +539,7 @@ SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
539539
success_return = 0;
540540
}
541541
err = -EACCES;
542-
if (ipcperms(&msq->q_perm, S_IRUGO))
542+
if (ipcperms(ns, &msq->q_perm, S_IRUGO))
543543
goto out_unlock;
544544

545545
err = security_msg_queue_msgctl(msq, cmd);
@@ -664,7 +664,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
664664
struct msg_sender s;
665665

666666
err = -EACCES;
667-
if (ipcperms(&msq->q_perm, S_IWUGO))
667+
if (ipcperms(ns, &msq->q_perm, S_IWUGO))
668668
goto out_unlock_free;
669669

670670
err = security_msg_queue_msgsnd(msq, msg, msgflg);
@@ -774,7 +774,7 @@ long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
774774
struct list_head *tmp;
775775

776776
msg = ERR_PTR(-EACCES);
777-
if (ipcperms(&msq->q_perm, S_IRUGO))
777+
if (ipcperms(ns, &msq->q_perm, S_IRUGO))
778778
goto out_unlock;
779779

780780
msg = ERR_PTR(-EAGAIN);

‎ipc/namespace.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
#include "util.h"
1717

18-
static struct ipc_namespace *create_ipc_ns(struct ipc_namespace *old_ns)
18+
static struct ipc_namespace *create_ipc_ns(struct task_struct *tsk,
19+
struct ipc_namespace *old_ns)
1920
{
2021
struct ipc_namespace *ns;
2122
int err;
@@ -44,17 +45,19 @@ static struct ipc_namespace *create_ipc_ns(struct ipc_namespace *old_ns)
4445
ipcns_notify(IPCNS_CREATED);
4546
register_ipcns_notifier(ns);
4647

47-
ns->user_ns = old_ns->user_ns;
48-
get_user_ns(ns->user_ns);
48+
ns->user_ns = get_user_ns(task_cred_xxx(tsk, user)->user_ns);
4949

5050
return ns;
5151
}
5252

53-
struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
53+
struct ipc_namespace *copy_ipcs(unsigned long flags,
54+
struct task_struct *tsk)
5455
{
56+
struct ipc_namespace *ns = tsk->nsproxy->ipc_ns;
57+
5558
if (!(flags & CLONE_NEWIPC))
5659
return get_ipc_ns(ns);
57-
return create_ipc_ns(ns);
60+
return create_ipc_ns(tsk, ns);
5861
}
5962

6063
/*

‎ipc/sem.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
817817
}
818818

819819
err = -EACCES;
820-
if (ipcperms (&sma->sem_perm, S_IRUGO))
820+
if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
821821
goto out_unlock;
822822

823823
err = security_sem_semctl(sma, cmd);
@@ -862,7 +862,8 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
862862
nsems = sma->sem_nsems;
863863

864864
err = -EACCES;
865-
if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
865+
if (ipcperms(ns, &sma->sem_perm,
866+
(cmd == SETVAL || cmd == SETALL) ? S_IWUGO : S_IRUGO))
866867
goto out_unlock;
867868

868869
err = security_sem_semctl(sma, cmd);
@@ -1047,7 +1048,8 @@ static int semctl_down(struct ipc_namespace *ns, int semid,
10471048
return -EFAULT;
10481049
}
10491050

1050-
ipcp = ipcctl_pre_down(&sem_ids(ns), semid, cmd, &semid64.sem_perm, 0);
1051+
ipcp = ipcctl_pre_down(ns, &sem_ids(ns), semid, cmd,
1052+
&semid64.sem_perm, 0);
10511053
if (IS_ERR(ipcp))
10521054
return PTR_ERR(ipcp);
10531055

@@ -1386,7 +1388,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
13861388
goto out_unlock_free;
13871389

13881390
error = -EACCES;
1389-
if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1391+
if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
13901392
goto out_unlock_free;
13911393

13921394
error = security_sem_semop(sma, sops, nsops, alter);

‎ipc/shm.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
623623
return -EFAULT;
624624
}
625625

626-
ipcp = ipcctl_pre_down(&shm_ids(ns), shmid, cmd, &shmid64.shm_perm, 0);
626+
ipcp = ipcctl_pre_down(ns, &shm_ids(ns), shmid, cmd,
627+
&shmid64.shm_perm, 0);
627628
if (IS_ERR(ipcp))
628629
return PTR_ERR(ipcp);
629630

@@ -737,7 +738,7 @@ SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
737738
result = 0;
738739
}
739740
err = -EACCES;
740-
if (ipcperms (&shp->shm_perm, S_IRUGO))
741+
if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
741742
goto out_unlock;
742743
err = security_shm_shmctl(shp, cmd);
743744
if (err)
@@ -773,7 +774,7 @@ SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
773774

774775
audit_ipc_obj(&(shp->shm_perm));
775776

776-
if (!capable(CAP_IPC_LOCK)) {
777+
if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
777778
uid_t euid = current_euid();
778779
err = -EPERM;
779780
if (euid != shp->shm_perm.uid &&
@@ -888,7 +889,7 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
888889
}
889890

890891
err = -EACCES;
891-
if (ipcperms(&shp->shm_perm, acc_mode))
892+
if (ipcperms(ns, &shp->shm_perm, acc_mode))
892893
goto out_unlock;
893894

894895
err = security_shm_shmat(shp, shmaddr, shmflg);

‎ipc/util.c‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,14 @@ static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
329329
*
330330
* It is called with ipc_ids.rw_mutex and ipcp->lock held.
331331
*/
332-
static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
333-
struct ipc_params *params)
332+
static int ipc_check_perms(struct ipc_namespace *ns,
333+
struct kern_ipc_perm *ipcp,
334+
struct ipc_ops *ops,
335+
struct ipc_params *params)
334336
{
335337
int err;
336338

337-
if (ipcperms(ipcp, params->flg))
339+
if (ipcperms(ns, ipcp, params->flg))
338340
err = -EACCES;
339341
else {
340342
err = ops->associate(ipcp, params->flg);
@@ -396,7 +398,7 @@ static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
396398
* ipc_check_perms returns the IPC id on
397399
* success
398400
*/
399-
err = ipc_check_perms(ipcp, ops, params);
401+
err = ipc_check_perms(ns, ipcp, ops, params);
400402
}
401403
ipc_unlock(ipcp);
402404
}
@@ -610,10 +612,12 @@ void ipc_rcu_putref(void *ptr)
610612
*
611613
* Check user, group, other permissions for access
612614
* to ipc resources. return 0 if allowed
615+
*
616+
* @flag will most probably be 0 or S_...UGO from <linux/stat.h>
613617
*/
614618

615-
int ipcperms (struct kern_ipc_perm *ipcp, short flag)
616-
{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
619+
int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
620+
{
617621
uid_t euid = current_euid();
618622
int requested_mode, granted_mode;
619623

@@ -627,7 +631,7 @@ int ipcperms (struct kern_ipc_perm *ipcp, short flag)
627631
granted_mode >>= 3;
628632
/* is there some bit set in requested_mode but not in granted_mode? */
629633
if ((requested_mode & ~granted_mode & 0007) &&
630-
!capable(CAP_IPC_OWNER))
634+
!ns_capable(ns->user_ns, CAP_IPC_OWNER))
631635
return -1;
632636

633637
return security_ipc_permission(ipcp, flag);
@@ -765,6 +769,7 @@ void ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
765769

766770
/**
767771
* ipcctl_pre_down - retrieve an ipc and check permissions for some IPC_XXX cmd
772+
* @ids: the ipc namespace
768773
* @ids: the table of ids where to look for the ipc
769774
* @id: the id of the ipc to retrieve
770775
* @cmd: the cmd to check
@@ -779,7 +784,8 @@ void ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
779784
* - returns the ipc with both ipc and rw_mutex locks held in case of success
780785
* or an err-code without any lock held otherwise.
781786
*/
782-
struct kern_ipc_perm *ipcctl_pre_down(struct ipc_ids *ids, int id, int cmd,
787+
struct kern_ipc_perm *ipcctl_pre_down(struct ipc_namespace *ns,
788+
struct ipc_ids *ids, int id, int cmd,
783789
struct ipc64_perm *perm, int extra_perm)
784790
{
785791
struct kern_ipc_perm *ipcp;
@@ -799,8 +805,8 @@ struct kern_ipc_perm *ipcctl_pre_down(struct ipc_ids *ids, int id, int cmd,
799805
perm->gid, perm->mode);
800806

801807
euid = current_euid();
802-
if (euid == ipcp->cuid ||
803-
euid == ipcp->uid || capable(CAP_SYS_ADMIN))
808+
if (euid == ipcp->cuid || euid == ipcp->uid ||
809+
ns_capable(ns->user_ns, CAP_SYS_ADMIN))
804810
return ipcp;
805811

806812
err = -EPERM;

‎ipc/util.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ int ipc_get_maxid(struct ipc_ids *);
103103
void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
104104

105105
/* must be called with ipcp locked */
106-
int ipcperms(struct kern_ipc_perm *ipcp, short flg);
106+
int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
107107

108108
/* for rare, potentially huge allocations.
109109
* both function can sleep
@@ -126,7 +126,8 @@ struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
126126
void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
127127
void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
128128
void ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
129-
struct kern_ipc_perm *ipcctl_pre_down(struct ipc_ids *ids, int id, int cmd,
129+
struct kern_ipc_perm *ipcctl_pre_down(struct ipc_namespace *ns,
130+
struct ipc_ids *ids, int id, int cmd,
130131
struct ipc64_perm *perm, int extra_perm);
131132

132133
#ifndef __ARCH_WANT_IPC_PARSE_VERSION

‎kernel/futex.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,10 +2418,19 @@ SYSCALL_DEFINE3(get_robust_list, int, pid,
24182418
goto err_unlock;
24192419
ret = -EPERM;
24202420
pcred = __task_cred(p);
2421+
/* If victim is in different user_ns, then uids are not
2422+
comparable, so we must have CAP_SYS_PTRACE */
2423+
if (cred->user->user_ns != pcred->user->user_ns) {
2424+
if (!ns_capable(pcred->user->user_ns, CAP_SYS_PTRACE))
2425+
goto err_unlock;
2426+
goto ok;
2427+
}
2428+
/* If victim is in same user_ns, then uids are comparable */
24212429
if (cred->euid != pcred->euid &&
24222430
cred->euid != pcred->uid &&
2423-
!capable(CAP_SYS_PTRACE))
2431+
!ns_capable(pcred->user->user_ns, CAP_SYS_PTRACE))
24242432
goto err_unlock;
2433+
ok:
24252434
head = p->robust_list;
24262435
rcu_read_unlock();
24272436
}

‎kernel/futex_compat.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,19 @@ compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
153153
goto err_unlock;
154154
ret = -EPERM;
155155
pcred = __task_cred(p);
156+
/* If victim is in different user_ns, then uids are not
157+
comparable, so we must have CAP_SYS_PTRACE */
158+
if (cred->user->user_ns != pcred->user->user_ns) {
159+
if (!ns_capable(pcred->user->user_ns, CAP_SYS_PTRACE))
160+
goto err_unlock;
161+
goto ok;
162+
}
163+
/* If victim is in same user_ns, then uids are comparable */
156164
if (cred->euid != pcred->euid &&
157165
cred->euid != pcred->uid &&
158-
!capable(CAP_SYS_PTRACE))
166+
!ns_capable(pcred->user->user_ns, CAP_SYS_PTRACE))
159167
goto err_unlock;
168+
ok:
160169
head = p->compat_robust_list;
161170
rcu_read_unlock();
162171
}

‎kernel/groups.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
233233
struct group_info *group_info;
234234
int retval;
235235

236-
if (!capable(CAP_SETGID))
236+
if (!nsown_capable(CAP_SETGID))
237237
return -EPERM;
238238
if ((unsigned)gidsetsize > NGROUPS_MAX)
239239
return -EINVAL;

0 commit comments

Comments
 (0)