ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <asm/uaccess.h>
14
15 unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
16 kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
17
18 EXPORT_SYMBOL(securebits);
19 EXPORT_SYMBOL(cap_bset);
20
21 /*
22  * This global lock protects task->cap_* for all tasks including current.
23  * Locking rule: acquire this prior to tasklist_lock.
24  */
25 spinlock_t task_capability_lock = SPIN_LOCK_UNLOCKED;
26
27 /*
28  * For sys_getproccap() and sys_setproccap(), any of the three
29  * capability set pointers may be NULL -- indicating that that set is
30  * uninteresting and/or not to be changed.
31  */
32
33 /*
34  * sys_capget - get the capabilities of a given process.
35  */
36 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
37 {
38      int ret = 0;
39      pid_t pid;
40      __u32 version;
41      task_t *target;
42      struct __user_cap_data_struct data;
43
44      if (get_user(version, &header->version))
45              return -EFAULT;
46
47      if (version != _LINUX_CAPABILITY_VERSION) {
48              if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
49                      return -EFAULT; 
50              return -EINVAL;
51      }
52
53      if (get_user(pid, &header->pid))
54              return -EFAULT;
55
56      if (pid < 0) 
57              return -EINVAL;
58
59      spin_lock(&task_capability_lock);
60      read_lock(&tasklist_lock); 
61
62      if (pid && pid != current->pid) {
63              target = find_task_by_pid(pid);
64              if (!target) {
65                   ret = -ESRCH;
66                   goto out;
67              }
68      } else
69              target = current;
70
71      ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
72
73 out:
74      read_unlock(&tasklist_lock); 
75      spin_unlock(&task_capability_lock);
76
77      if (!ret && copy_to_user(dataptr, &data, sizeof data))
78           return -EFAULT; 
79
80      return ret;
81 }
82
83 /*
84  * cap_set_pg - set capabilities for all processes in a given process
85  * group.  We call this holding task_capability_lock and tasklist_lock.
86  */
87 static inline void cap_set_pg(int pgrp, kernel_cap_t *effective,
88                               kernel_cap_t *inheritable,
89                               kernel_cap_t *permitted)
90 {
91         task_t *g, *target;
92         struct list_head *l;
93         struct pid *pid;
94
95         for_each_task_pid(pgrp, PIDTYPE_PGID, g, l, pid) {
96                 target = g;
97                 while_each_thread(g, target)
98                         security_capset_set(target, effective, inheritable, permitted);
99         }
100 }
101
102 /*
103  * cap_set_all - set capabilities for all processes other than init
104  * and self.  We call this holding task_capability_lock and tasklist_lock.
105  */
106 static inline void cap_set_all(kernel_cap_t *effective,
107                                kernel_cap_t *inheritable,
108                                kernel_cap_t *permitted)
109 {
110      task_t *g, *target;
111
112      do_each_thread(g, target) {
113              if (target == current || target->pid == 1)
114                      continue;
115              security_capset_set(target, effective, inheritable, permitted);
116      } while_each_thread(g, target);
117 }
118
119 /*
120  * sys_capset - set capabilities for a given process, all processes, or all
121  * processes in a given process group.
122  *
123  * The restrictions on setting capabilities are specified as:
124  *
125  * [pid is for the 'target' task.  'current' is the calling task.]
126  *
127  * I: any raised capabilities must be a subset of the (old current) permitted
128  * P: any raised capabilities must be a subset of the (old current) permitted
129  * E: must be set to a subset of (new target) permitted
130  */
131 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
132 {
133      kernel_cap_t inheritable, permitted, effective;
134      __u32 version;
135      task_t *target;
136      int ret;
137      pid_t pid;
138
139      if (get_user(version, &header->version))
140              return -EFAULT; 
141
142      if (version != _LINUX_CAPABILITY_VERSION) {
143              if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
144                      return -EFAULT; 
145              return -EINVAL;
146      }
147
148      if (get_user(pid, &header->pid))
149              return -EFAULT; 
150
151      if (pid && !capable(CAP_SETPCAP))
152              return -EPERM;
153
154      if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
155          copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
156          copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
157              return -EFAULT; 
158
159      spin_lock(&task_capability_lock);
160      read_lock(&tasklist_lock);
161
162      if (pid > 0 && pid != current->pid) {
163           target = find_task_by_pid(pid);
164           if (!target) {
165                ret = -ESRCH;
166                goto out;
167           }
168      } else
169                target = current;
170
171      ret = -EPERM;
172
173      if (security_capset_check(target, &effective, &inheritable, &permitted))
174              goto out;
175
176      if (!cap_issubset(inheritable, cap_combine(target->cap_inheritable,
177                        current->cap_permitted)))
178              goto out;
179
180      /* verify restrictions on target's new Permitted set */
181      if (!cap_issubset(permitted, cap_combine(target->cap_permitted,
182                        current->cap_permitted)))
183              goto out;
184
185      /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
186      if (!cap_issubset(effective, permitted))
187              goto out;
188
189      ret = 0;
190
191      /* having verified that the proposed changes are legal,
192            we now put them into effect. */
193      if (pid < 0) {
194              if (pid == -1)  /* all procs other than current and init */
195                      cap_set_all(&effective, &inheritable, &permitted);
196
197              else            /* all procs in process group */
198                      cap_set_pg(-pid, &effective, &inheritable, &permitted);
199      } else {
200              security_capset_set(target, &effective, &inheritable, &permitted);
201      }
202
203 out:
204      read_unlock(&tasklist_lock);
205      spin_unlock(&task_capability_lock);
206
207      return ret;
208 }