vserver 2.0 rc7
[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/mm.h>
11 #include <linux/module.h>
12 #include <linux/security.h>
13 #include <linux/syscalls.h>
14 #include <linux/vs_cvirt.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  */
38 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
39 {
40      int ret = 0;
41      pid_t pid;
42      __u32 version;
43      task_t *target;
44      struct __user_cap_data_struct data;
45
46      if (get_user(version, &header->version))
47              return -EFAULT;
48
49      if (version != _LINUX_CAPABILITY_VERSION) {
50              if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
51                      return -EFAULT; 
52              return -EINVAL;
53      }
54
55      if (get_user(pid, &header->pid))
56              return -EFAULT;
57
58      if (pid < 0) 
59              return -EINVAL;
60
61      spin_lock(&task_capability_lock);
62      read_lock(&tasklist_lock); 
63
64      if (pid && pid != current->pid) {
65              target = find_task_by_pid(pid);
66              if (!target) {
67                   ret = -ESRCH;
68                   goto out;
69              }
70      } else
71              target = current;
72
73      ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
74
75 out:
76      read_unlock(&tasklist_lock); 
77      spin_unlock(&task_capability_lock);
78
79      if (!ret && copy_to_user(dataptr, &data, sizeof data))
80           return -EFAULT; 
81
82      return ret;
83 }
84
85 /*
86  * cap_set_pg - set capabilities for all processes in a given process
87  * group.  We call this holding task_capability_lock and tasklist_lock.
88  */
89 static inline int cap_set_pg(int pgrp, kernel_cap_t *effective,
90                               kernel_cap_t *inheritable,
91                               kernel_cap_t *permitted)
92 {
93         task_t *g, *target;
94         int ret = -EPERM;
95         int found = 0;
96
97         do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
98                 target = g;
99                 while_each_thread(g, target) {
100                         if (!security_capset_check(target, effective,
101                                                         inheritable,
102                                                         permitted)) {
103                                 security_capset_set(target, effective,
104                                                         inheritable,
105                                                         permitted);
106                                 ret = 0;
107                         }
108                         found = 1;
109                 }
110         } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
111
112         if (!found)
113              ret = 0;
114         return ret;
115 }
116
117 /*
118  * cap_set_all - set capabilities for all processes other than init
119  * and self.  We call this holding task_capability_lock and tasklist_lock.
120  */
121 static inline int cap_set_all(kernel_cap_t *effective,
122                                kernel_cap_t *inheritable,
123                                kernel_cap_t *permitted)
124 {
125      task_t *g, *target;
126      int ret = -EPERM;
127      int found = 0;
128
129      do_each_thread(g, target) {
130              if (target == current || target->pid == 1)
131                      continue;
132              found = 1;
133              if (security_capset_check(target, effective, inheritable,
134                                                 permitted))
135                      continue;
136              ret = 0;
137              security_capset_set(target, effective, inheritable, permitted);
138      } while_each_thread(g, target);
139
140      if (!found)
141              ret = 0;
142      return ret;
143 }
144
145 /*
146  * sys_capset - set capabilities for a given process, all processes, or all
147  * processes in a given process group.
148  *
149  * The restrictions on setting capabilities are specified as:
150  *
151  * [pid is for the 'target' task.  'current' is the calling task.]
152  *
153  * I: any raised capabilities must be a subset of the (old current) permitted
154  * P: any raised capabilities must be a subset of the (old current) permitted
155  * E: must be set to a subset of (new target) permitted
156  */
157 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
158 {
159      kernel_cap_t inheritable, permitted, effective;
160      __u32 version;
161      task_t *target;
162      int ret;
163      pid_t pid;
164
165      if (get_user(version, &header->version))
166              return -EFAULT; 
167
168      if (version != _LINUX_CAPABILITY_VERSION) {
169              if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
170                      return -EFAULT; 
171              return -EINVAL;
172      }
173
174      if (get_user(pid, &header->pid))
175              return -EFAULT; 
176
177      if (pid && pid != current->pid && !capable(CAP_SETPCAP))
178              return -EPERM;
179
180      if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
181          copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
182          copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
183              return -EFAULT; 
184
185      spin_lock(&task_capability_lock);
186      read_lock(&tasklist_lock);
187
188      if (pid > 0 && pid != current->pid) {
189           target = find_task_by_pid(pid);
190           if (!target) {
191                ret = -ESRCH;
192                goto out;
193           }
194      } else
195                target = current;
196
197      ret = 0;
198
199      /* having verified that the proposed changes are legal,
200            we now put them into effect. */
201      if (pid < 0) {
202              if (pid == -1)  /* all procs other than current and init */
203                      ret = cap_set_all(&effective, &inheritable, &permitted);
204
205              else            /* all procs in process group */
206                      ret = cap_set_pg(-pid, &effective, &inheritable,
207                                                         &permitted);
208      } else {
209              ret = security_capset_check(target, &effective, &inheritable,
210                                                         &permitted);
211              if (!ret)
212                      security_capset_set(target, &effective, &inheritable,
213                                                         &permitted);
214      }
215
216 out:
217      read_unlock(&tasklist_lock);
218      spin_unlock(&task_capability_lock);
219
220      return ret;
221 }