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