4e3424e879a62c947c88bc1848d83a781a807c53
[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_cvirt.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      task_t *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         task_t *g, *target;
101         int ret = -EPERM;
102         int found = 0;
103
104         do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
105                 target = g;
106                 while_each_thread(g, target) {
107                         if (!security_capset_check(target, effective,
108                                                         inheritable,
109                                                         permitted)) {
110                                 security_capset_set(target, effective,
111                                                         inheritable,
112                                                         permitted);
113                                 ret = 0;
114                         }
115                         found = 1;
116                 }
117         } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
118
119         if (!found)
120              ret = 0;
121         return ret;
122 }
123
124 /*
125  * cap_set_all - set capabilities for all processes other than init
126  * and self.  We call this holding task_capability_lock and tasklist_lock.
127  */
128 static inline int cap_set_all(kernel_cap_t *effective,
129                                kernel_cap_t *inheritable,
130                                kernel_cap_t *permitted)
131 {
132      task_t *g, *target;
133      int ret = -EPERM;
134      int found = 0;
135
136      do_each_thread(g, target) {
137              if (target == current || target->pid == 1)
138                      continue;
139              found = 1;
140              if (security_capset_check(target, effective, inheritable,
141                                                 permitted))
142                      continue;
143              ret = 0;
144              security_capset_set(target, effective, inheritable, permitted);
145      } while_each_thread(g, target);
146
147      if (!found)
148              ret = 0;
149      return ret;
150 }
151
152 /**
153  * sys_capset - set capabilities for a process or a group of processes
154  * @header: pointer to struct that contains capability version and
155  *      target pid data
156  * @data: pointer to struct that contains the effective, permitted,
157  *      and inheritable capabilities
158  *
159  * Set capabilities for a given process, all processes, or all
160  * processes in a given process group.
161  *
162  * The restrictions on setting capabilities are specified as:
163  *
164  * [pid is for the 'target' task.  'current' is the calling task.]
165  *
166  * I: any raised capabilities must be a subset of the (old current) permitted
167  * P: any raised capabilities must be a subset of the (old current) permitted
168  * E: must be set to a subset of (new target) permitted
169  *
170  * Returns 0 on success and < 0 on error.
171  */
172 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
173 {
174      kernel_cap_t inheritable, permitted, effective;
175      __u32 version;
176      task_t *target;
177      int ret;
178      pid_t pid;
179
180      if (get_user(version, &header->version))
181              return -EFAULT; 
182
183      if (version != _LINUX_CAPABILITY_VERSION) {
184              if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
185                      return -EFAULT; 
186              return -EINVAL;
187      }
188
189      if (get_user(pid, &header->pid))
190              return -EFAULT; 
191
192      if (pid && pid != current->pid && !capable(CAP_SETPCAP))
193              return -EPERM;
194
195      if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
196          copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
197          copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
198              return -EFAULT; 
199
200      spin_lock(&task_capability_lock);
201      read_lock(&tasklist_lock);
202
203      if (pid > 0 && pid != current->pid) {
204           target = find_task_by_pid(pid);
205           if (!target) {
206                ret = -ESRCH;
207                goto out;
208           }
209      } else
210                target = current;
211
212      ret = 0;
213
214      /* having verified that the proposed changes are legal,
215            we now put them into effect. */
216      if (pid < 0) {
217              if (pid == -1)  /* all procs other than current and init */
218                      ret = cap_set_all(&effective, &inheritable, &permitted);
219
220              else            /* all procs in process group */
221                      ret = cap_set_pg(-pid, &effective, &inheritable,
222                                                         &permitted);
223      } else {
224              ret = security_capset_check(target, &effective, &inheritable,
225                                                         &permitted);
226              if (!ret)
227                      security_capset_set(target, &effective, &inheritable,
228                                                         &permitted);
229      }
230
231 out:
232      read_unlock(&tasklist_lock);
233      spin_unlock(&task_capability_lock);
234
235      return ret;
236 }
237
238 int __capable(struct task_struct *t, int cap)
239 {
240         if (security_capable(t, cap) == 0) {
241                 t->flags |= PF_SUPERPRIV;
242                 return 1;
243         }
244         return 0;
245 }
246 EXPORT_SYMBOL(__capable);
247
248 int capable(int cap)
249 {
250         /* here for now so we don't require task locking */
251         if (vx_check_bit(VXC_CAP_MASK, cap) && !vx_mcaps(1L << cap))
252                 return 0;
253         return __capable(current, cap);
254 }
255 EXPORT_SYMBOL(capable);
256