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