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