Skip to content

Commit b488893

Browse files
xemulLinus Torvalds
authored andcommitted
pid namespaces: changes to show virtual ids to user
This is the largest patch in the set. Make all (I hope) the places where the pid is shown to or get from user operate on the virtual pids. The idea is: - all in-kernel data structures must store either struct pid itself or the pid's global nr, obtained with pid_nr() call; - when seeking the task from kernel code with the stored id one should use find_task_by_pid() call that works with global pids; - when showing pid's numerical value to the user the virtual one should be used, but however when one shows task's pid outside this task's namespace the global one is to be used; - when getting the pid from userspace one need to consider this as the virtual one and use appropriate task/pid-searching functions. [[email protected]: build fix] [[email protected]: nuther build fix] [[email protected]: yet nuther build fix] [[email protected]: remove unneeded casts] Signed-off-by: Pavel Emelyanov <[email protected]> Signed-off-by: Alexey Dobriyan <[email protected]> Cc: Sukadev Bhattiprolu <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Paul Menage <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3eb07c8 commit b488893

File tree

32 files changed

+241
-144
lines changed

32 files changed

+241
-144
lines changed

arch/ia64/kernel/signal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ ia64_rt_sigreturn (struct sigscratch *scr)
227227
si.si_signo = SIGSEGV;
228228
si.si_errno = 0;
229229
si.si_code = SI_KERNEL;
230-
si.si_pid = current->pid;
230+
si.si_pid = task_pid_vnr(current);
231231
si.si_uid = current->uid;
232232
si.si_addr = sc;
233233
force_sig_info(SIGSEGV, &si, current);
@@ -332,7 +332,7 @@ force_sigsegv_info (int sig, void __user *addr)
332332
si.si_signo = SIGSEGV;
333333
si.si_errno = 0;
334334
si.si_code = SI_KERNEL;
335-
si.si_pid = current->pid;
335+
si.si_pid = task_pid_vnr(current);
336336
si.si_uid = current->uid;
337337
si.si_addr = addr;
338338
force_sig_info(SIGSEGV, &si, current);

arch/parisc/kernel/signal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ sys_rt_sigreturn(struct pt_regs *regs, int in_syscall)
181181
si.si_signo = SIGSEGV;
182182
si.si_errno = 0;
183183
si.si_code = SI_KERNEL;
184-
si.si_pid = current->pid;
184+
si.si_pid = task_pid_vnr(current);
185185
si.si_uid = current->uid;
186186
si.si_addr = &frame->uc;
187187
force_sig_info(SIGSEGV, &si, current);

arch/sparc/kernel/sys_sunos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ asmlinkage int sunos_killpg(int pgrp, int sig)
866866
rcu_read_lock();
867867
ret = -EINVAL;
868868
if (pgrp > 0)
869-
ret = kill_pgrp(find_pid(pgrp), sig, 0);
869+
ret = kill_pgrp(find_vpid(pgrp), sig, 0);
870870
rcu_read_unlock();
871871

872872
return ret;

arch/sparc64/kernel/sys_sunos32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ asmlinkage int sunos_killpg(int pgrp, int sig)
831831
rcu_read_lock();
832832
ret = -EINVAL;
833833
if (pgrp > 0)
834-
ret = kill_pgrp(find_pid(pgrp), sig, 0);
834+
ret = kill_pgrp(find_vpid(pgrp), sig, 0);
835835
rcu_read_unlock();
836836

837837
return ret;

drivers/char/tty_io.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
#include <linux/selection.h>
104104

105105
#include <linux/kmod.h>
106+
#include <linux/nsproxy.h>
106107

107108
#undef TTY_DEBUG_HANGUP
108109

@@ -3107,7 +3108,7 @@ static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t
31073108
*/
31083109
if (tty == real_tty && current->signal->tty != real_tty)
31093110
return -ENOTTY;
3110-
return put_user(pid_nr(real_tty->pgrp), p);
3111+
return put_user(pid_vnr(real_tty->pgrp), p);
31113112
}
31123113

31133114
/**
@@ -3141,7 +3142,7 @@ static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t
31413142
if (pgrp_nr < 0)
31423143
return -EINVAL;
31433144
rcu_read_lock();
3144-
pgrp = find_pid(pgrp_nr);
3145+
pgrp = find_vpid(pgrp_nr);
31453146
retval = -ESRCH;
31463147
if (!pgrp)
31473148
goto out_unlock;
@@ -3178,7 +3179,7 @@ static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t _
31783179
return -ENOTTY;
31793180
if (!real_tty->session)
31803181
return -ENOTTY;
3181-
return put_user(pid_nr(real_tty->session), p);
3182+
return put_user(pid_vnr(real_tty->session), p);
31823183
}
31833184

31843185
/**

fs/binfmt_elf.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,10 +1383,10 @@ static void fill_prstatus(struct elf_prstatus *prstatus,
13831383
prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
13841384
prstatus->pr_sigpend = p->pending.signal.sig[0];
13851385
prstatus->pr_sighold = p->blocked.sig[0];
1386-
prstatus->pr_pid = p->pid;
1387-
prstatus->pr_ppid = p->parent->pid;
1388-
prstatus->pr_pgrp = task_pgrp_nr(p);
1389-
prstatus->pr_sid = task_session_nr(p);
1386+
prstatus->pr_pid = task_pid_vnr(p);
1387+
prstatus->pr_ppid = task_pid_vnr(p->parent);
1388+
prstatus->pr_pgrp = task_pgrp_vnr(p);
1389+
prstatus->pr_sid = task_session_vnr(p);
13901390
if (thread_group_leader(p)) {
13911391
/*
13921392
* This is the record for the group leader. Add in the
@@ -1429,10 +1429,10 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
14291429
psinfo->pr_psargs[i] = ' ';
14301430
psinfo->pr_psargs[len] = 0;
14311431

1432-
psinfo->pr_pid = p->pid;
1433-
psinfo->pr_ppid = p->parent->pid;
1434-
psinfo->pr_pgrp = task_pgrp_nr(p);
1435-
psinfo->pr_sid = task_session_nr(p);
1432+
psinfo->pr_pid = task_pid_vnr(p);
1433+
psinfo->pr_ppid = task_pid_vnr(p->parent);
1434+
psinfo->pr_pgrp = task_pgrp_vnr(p);
1435+
psinfo->pr_sid = task_session_vnr(p);
14361436

14371437
i = p->state ? ffz(~p->state) + 1 : 0;
14381438
psinfo->pr_state = i;

fs/binfmt_elf_fdpic.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,10 @@ static void fill_prstatus(struct elf_prstatus *prstatus,
13421342
prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
13431343
prstatus->pr_sigpend = p->pending.signal.sig[0];
13441344
prstatus->pr_sighold = p->blocked.sig[0];
1345-
prstatus->pr_pid = p->pid;
1346-
prstatus->pr_ppid = p->parent->pid;
1347-
prstatus->pr_pgrp = task_pgrp_nr(p);
1348-
prstatus->pr_sid = task_session_nr(p);
1345+
prstatus->pr_pid = task_pid_vnr(p);
1346+
prstatus->pr_ppid = task_pid_vnr(p->parent);
1347+
prstatus->pr_pgrp = task_pgrp_vnr(p);
1348+
prstatus->pr_sid = task_session_vnr(p);
13491349
if (thread_group_leader(p)) {
13501350
/*
13511351
* This is the record for the group leader. Add in the
@@ -1391,10 +1391,10 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
13911391
psinfo->pr_psargs[i] = ' ';
13921392
psinfo->pr_psargs[len] = 0;
13931393

1394-
psinfo->pr_pid = p->pid;
1395-
psinfo->pr_ppid = p->parent->pid;
1396-
psinfo->pr_pgrp = task_pgrp_nr(p);
1397-
psinfo->pr_sid = task_session_nr(p);
1394+
psinfo->pr_pid = task_pid_vnr(p);
1395+
psinfo->pr_ppid = task_pid_vnr(p->parent);
1396+
psinfo->pr_pgrp = task_pgrp_vnr(p);
1397+
psinfo->pr_sid = task_session_vnr(p);
13981398

13991399
i = p->state ? ffz(~p->state) + 1 : 0;
14001400
psinfo->pr_state = i;

fs/exec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ static int format_corename(char *corename, const char *pattern, long signr)
14331433
case 'p':
14341434
pid_in_pattern = 1;
14351435
rc = snprintf(out_ptr, out_end - out_ptr,
1436-
"%d", current->tgid);
1436+
"%d", task_tgid_vnr(current));
14371437
if (rc > out_end - out_ptr)
14381438
goto out;
14391439
out_ptr += rc;
@@ -1513,7 +1513,7 @@ static int format_corename(char *corename, const char *pattern, long signr)
15131513
if (!ispipe && !pid_in_pattern
15141514
&& (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
15151515
rc = snprintf(out_ptr, out_end - out_ptr,
1516-
".%d", current->tgid);
1516+
".%d", task_tgid_vnr(current));
15171517
if (rc > out_end - out_ptr)
15181518
goto out;
15191519
out_ptr += rc;

fs/fcntl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/ptrace.h>
1919
#include <linux/signal.h>
2020
#include <linux/rcupdate.h>
21+
#include <linux/pid_namespace.h>
2122

2223
#include <asm/poll.h>
2324
#include <asm/siginfo.h>
@@ -292,7 +293,7 @@ int f_setown(struct file *filp, unsigned long arg, int force)
292293
who = -who;
293294
}
294295
rcu_read_lock();
295-
pid = find_pid(who);
296+
pid = find_vpid(who);
296297
result = __f_setown(filp, pid, type, force);
297298
rcu_read_unlock();
298299
return result;
@@ -308,7 +309,7 @@ pid_t f_getown(struct file *filp)
308309
{
309310
pid_t pid;
310311
read_lock(&filp->f_owner.lock);
311-
pid = pid_nr(filp->f_owner.pid);
312+
pid = pid_nr_ns(filp->f_owner.pid, current->nsproxy->pid_ns);
312313
if (filp->f_owner.pid_type == PIDTYPE_PGID)
313314
pid = -pid;
314315
read_unlock(&filp->f_owner.lock);

fs/ioprio.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/capability.h>
2626
#include <linux/syscalls.h>
2727
#include <linux/security.h>
28+
#include <linux/pid_namespace.h>
2829

2930
static int set_task_ioprio(struct task_struct *task, int ioprio)
3031
{
@@ -93,15 +94,16 @@ asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
9394
if (!who)
9495
p = current;
9596
else
96-
p = find_task_by_pid(who);
97+
p = find_task_by_pid_ns(who,
98+
current->nsproxy->pid_ns);
9799
if (p)
98100
ret = set_task_ioprio(p, ioprio);
99101
break;
100102
case IOPRIO_WHO_PGRP:
101103
if (!who)
102104
pgrp = task_pgrp(current);
103105
else
104-
pgrp = find_pid(who);
106+
pgrp = find_vpid(who);
105107
do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
106108
ret = set_task_ioprio(p, ioprio);
107109
if (ret)
@@ -180,15 +182,16 @@ asmlinkage long sys_ioprio_get(int which, int who)
180182
if (!who)
181183
p = current;
182184
else
183-
p = find_task_by_pid(who);
185+
p = find_task_by_pid_ns(who,
186+
current->nsproxy->pid_ns);
184187
if (p)
185188
ret = get_task_ioprio(p);
186189
break;
187190
case IOPRIO_WHO_PGRP:
188191
if (!who)
189192
pgrp = task_pgrp(current);
190193
else
191-
pgrp = find_pid(who);
194+
pgrp = find_vpid(who);
192195
do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
193196
tmpio = get_task_ioprio(p);
194197
if (tmpio < 0)

fs/proc/array.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include <linux/cpuset.h>
7878
#include <linux/rcupdate.h>
7979
#include <linux/delayacct.h>
80+
#include <linux/pid_namespace.h>
8081

8182
#include <asm/pgtable.h>
8283
#include <asm/processor.h>
@@ -161,8 +162,15 @@ static inline char *task_state(struct task_struct *p, char *buffer)
161162
struct group_info *group_info;
162163
int g;
163164
struct fdtable *fdt = NULL;
165+
struct pid_namespace *ns;
166+
pid_t ppid, tpid;
164167

168+
ns = current->nsproxy->pid_ns;
165169
rcu_read_lock();
170+
ppid = pid_alive(p) ?
171+
task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
172+
tpid = pid_alive(p) && p->ptrace ?
173+
task_ppid_nr_ns(rcu_dereference(p->parent), ns) : 0;
166174
buffer += sprintf(buffer,
167175
"State:\t%s\n"
168176
"Tgid:\t%d\n"
@@ -172,9 +180,9 @@ static inline char *task_state(struct task_struct *p, char *buffer)
172180
"Uid:\t%d\t%d\t%d\t%d\n"
173181
"Gid:\t%d\t%d\t%d\t%d\n",
174182
get_task_state(p),
175-
p->tgid, p->pid,
176-
pid_alive(p) ? rcu_dereference(p->real_parent)->tgid : 0,
177-
pid_alive(p) && p->ptrace ? rcu_dereference(p->parent)->pid : 0,
183+
task_tgid_nr_ns(p, ns),
184+
task_pid_nr_ns(p, ns),
185+
ppid, tpid,
178186
p->uid, p->euid, p->suid, p->fsuid,
179187
p->gid, p->egid, p->sgid, p->fsgid);
180188

@@ -394,6 +402,9 @@ static int do_task_stat(struct task_struct *task, char *buffer, int whole)
394402
unsigned long rsslim = 0;
395403
char tcomm[sizeof(task->comm)];
396404
unsigned long flags;
405+
struct pid_namespace *ns;
406+
407+
ns = current->nsproxy->pid_ns;
397408

398409
state = *get_task_state(task);
399410
vsize = eip = esp = 0;
@@ -416,7 +427,7 @@ static int do_task_stat(struct task_struct *task, char *buffer, int whole)
416427
struct signal_struct *sig = task->signal;
417428

418429
if (sig->tty) {
419-
tty_pgrp = pid_nr(sig->tty->pgrp);
430+
tty_pgrp = pid_nr_ns(sig->tty->pgrp, ns);
420431
tty_nr = new_encode_dev(tty_devnum(sig->tty));
421432
}
422433

@@ -449,9 +460,9 @@ static int do_task_stat(struct task_struct *task, char *buffer, int whole)
449460
gtime += cputime_add(gtime, sig->gtime);
450461
}
451462

452-
sid = task_session_nr(task);
453-
pgid = task_pgrp_nr(task);
454-
ppid = rcu_dereference(task->real_parent)->tgid;
463+
sid = task_session_nr_ns(task, ns);
464+
pgid = task_pgrp_nr_ns(task, ns);
465+
ppid = task_ppid_nr_ns(task, ns);
455466

456467
unlock_task_sighand(task, &flags);
457468
}
@@ -483,7 +494,7 @@ static int do_task_stat(struct task_struct *task, char *buffer, int whole)
483494
res = sprintf(buffer, "%d (%s) %c %d %d %d %d %d %u %lu \
484495
%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
485496
%lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
486-
task->pid,
497+
task_pid_nr_ns(task, ns),
487498
tcomm,
488499
state,
489500
ppid,

0 commit comments

Comments
 (0)