[PATCH] pid: replace do/while_each_task_pid with do/while_each_pid_task
[linux-2.6.git] / kernel / capability.c
1 /*
2  * linux/kernel/capability.c
3  *
4  * Copyright (C) 1997  Andrew Main <zefram@fysh.org>
5  *
6  * Integrated into 2.1.97+,  Andrew G. Morgan <morgan@transmeta.com>
7  * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
8  */ 
9
10 #include <linux/capability.h>
11 #include <linux/mm.h>
12 #include <linux/module.h>
13 #include <linux/security.h>
14 #include <linux/syscalls.h>
15 #include <asm/uaccess.h>
16
17 unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
18 kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
19
20 EXPORT_SYMBOL(securebits);
21 EXPORT_SYMBOL(cap_bset);
22
23 /*
24  * This lock protects task->cap_* for all tasks including current.
25  * Locking rule: acquire this prior to tasklist_lock.
26  */
27 static DEFINE_SPINLOCK(task_capability_lock);
28
29 /*
30  * For sys_getproccap() and sys_setproccap(), any of the three
31  * capability set pointers may be NULL -- indicating that that set is
32  * uninteresting and/or not to be changed.
33  */
34
35 /**
36  * sys_capget - get the capabilities of a given process.
37  * @header: pointer to struct that contains capability version and
38  *      target pid data
39  * @dataptr: pointer to struct that contains the effective, permitted,
40  *      and inheritable capabilities that are returned
41  *
42  * Returns 0 on success and < 0 on error.
43  */
44 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
45 {
46      int ret = 0;
47      pid_t pid;
48      __u32 version;
49      struct task_struct *target;
50      struct __user_cap_data_struct data;
51
52      if (get_user(version, &header->version))
53              return -EFAULT;
54
55      if (version != _LINUX_CAPABILITY_VERSION) {
56              if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
57                      return -EFAULT; 
58              return -EINVAL;
59      }
60
61      if (get_user(pid, &header->pid))
62              return -EFAULT;
63
64      if (pid < 0) 
65              return -EINVAL;
66
67      spin_lock(&task_capability_lock);
68      read_lock(&tasklist_lock); 
69
70      if (pid && pid != current->pid) {
71              target = find_task_by_pid(pid);
72              if (!target) {
73                   ret = -ESRCH;
74                   goto out;
75              }
76      } else
77              target = current;
78
79      ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
80
81 out:
82      read_unlock(&tasklist_lock); 
83      spin_unlock(&task_capability_lock);
84
85      if (!ret && copy_to_user(dataptr, &data, sizeof data))
86           return -EFAULT; 
87
88      return ret;
89 }
90
91 /*
92  * cap_set_pg - set capabilities for all processes in a given process
93  * group.  We call this holding task_capability_lock and tasklist_lock.
94  */
95 static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
96                               kernel_cap_t *inheritable,
97                               kernel_cap_t *permitted)
98 {
99         struct task_struct *g, *target;
100         int ret = -EPERM;
101         int found = 0;
102         struct pid *pgrp;
103
104         pgrp = find_pid(pgrp_nr);
105         do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
106                 target = g;
107                 while_each_thread(g, target) {
108                         if (!security_capset_check(target, effective,
109                                                         inheritable,
110                                                         permitted)) {
111                                 security_capset_set(target, effective,
112                                                         inheritable,
113                                                         permitted);
114                                 ret = 0;
115                         }
116                         found = 1;
117                 }
118         } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
119
120         if (!found)
121              ret = 0;
122         return ret;
123 }
124
125 /*
126  * cap_set_all - set capabilities for all processes other than init
127  * and self.  We call this holding task_capability_lock and tasklist_lock.
128  */
129 static inline int cap_set_all(kernel_cap_t *effective,
130                                kernel_cap_t *inheritable,
131                                kernel_cap_t *permitted)
132 {
133      struct task_struct *g, *target;
134      int ret = -EPERM;
135      int found = 0;
136
137      do_each_thread(g, target) {
138              if (target == current || is_init(target))
139                      continue;
140              found = 1;
141              if (security_capset_check(target, effective, inheritable,
142                                                 permitted))
143                      continue;
144              ret = 0;
145              security_capset_set(target, effective, inheritable, permitted);
146      } while_each_thread(g, target);
147
148      if (!found)
149              ret = 0;
150      return ret;
151 }
152
153 /**
154  * sys_capset - set capabilities for a process or a group of processes
155  * @header: pointer to struct that contains capability version and
156  *      target pid data
157  * @data: pointer to struct that contains the effective, permitted,
158  *      and inheritable capabilities
159  *
160  * Set capabilities for a given process, all processes, or all
161  * processes in a given process group.
162  *
163  * The restrictions on setting capabilities are specified as:
164  *
165  * [pid is for the 'target' task.  'current' is the calling task.]
166  *
167  * I: any raised capabilities must be a subset of the (old current) permitted
168  * P: any raised capabilities must be a subset of the (old current) permitted
169  * E: must be set to a subset of (new target) permitted
170  *
171  * Returns 0 on success and < 0 on error.
172  */
173 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
174 {
175      kernel_cap_t inheritable, permitted, effective;
176      __u32 version;
177      struct task_struct *target;
178      int ret;
179      pid_t pid;
180
181      if (get_user(version, &header->version))
182              return -EFAULT; 
183
184      if (version != _LINUX_CAPABILITY_VERSION) {
185              if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
186                      return -EFAULT; 
187              return -EINVAL;
188      }
189
190      if (get_user(pid, &header->pid))
191              return -EFAULT; 
192
193      if (pid && pid != current->pid && !capable(CAP_SETPCAP))
194              return -EPERM;
195
196      if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
197          copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
198          copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
199              return -EFAULT; 
200
201      spin_lock(&task_capability_lock);
202      read_lock(&tasklist_lock);
203
204      if (pid > 0 && pid != current->pid) {
205           target = find_task_by_pid(pid);
206           if (!target) {
207                ret = -ESRCH;
208                goto out;
209           }
210      } else
211                target = current;
212
213      ret = 0;
214
215      /* having verified that the proposed changes are legal,
216            we now put them into effect. */
217      if (pid < 0) {
218              if (pid == -1)  /* all procs other than current and init */
219                      ret = cap_set_all(&effective, &inheritable, &permitted);
220
221              else            /* all procs in process group */
222                      ret = cap_set_pg(-pid, &effective, &inheritable,
223                                                         &permitted);
224      } else {
225              ret = security_capset_check(target, &effective, &inheritable,
226                                                         &permitted);
227              if (!ret)
228                      security_capset_set(target, &effective, &inheritable,
229                                                         &permitted);
230      }
231
232 out:
233      read_unlock(&tasklist_lock);
234      spin_unlock(&task_capability_lock);
235
236      return ret;
237 }
238
239 int __capable(struct task_struct *t, int cap)
240 {
241         if (security_capable(t, cap) == 0) {
242                 t->flags |= PF_SUPERPRIV;
243                 return 1;
244         }
245         return 0;
246 }
247 EXPORT_SYMBOL(__capable);
248
249 int capable(int cap)
250 {
251         return __capable(current, cap);
252 }
253 EXPORT_SYMBOL(capable);