UBUNTU: SAUCE: Yama: follow changes to generic_permission
[linux-flexiantxendom0-natty.git] / security / yama / yama_lsm.c
1 /*
2  * Yama Linux Security Module
3  *
4  * Author: Kees Cook <kees.cook@canonical.com>
5  *
6  * Copyright (C) 2010 Canonical, Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2, as
10  * published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/security.h>
15 #include <linux/sysctl.h>
16 #include <linux/ptrace.h>
17 #include <linux/prctl.h>
18 #include <linux/ratelimit.h>
19
20 static int ptrace_scope = 1;
21 static int protected_sticky_symlinks = 1;
22 static int protected_nonaccess_hardlinks = 1;
23
24 /* describe a PTRACE relationship for potential exception */
25 struct ptrace_relation {
26         struct task_struct *tracer;
27         struct task_struct *tracee;
28         struct list_head node;
29 };
30
31 static LIST_HEAD(ptracer_relations);
32 static DEFINE_SPINLOCK(ptracer_relations_lock);
33
34 /**
35  * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
36  * @tracer: the task_struct of the process doing the PTRACE
37  * @tracee: the task_struct of the process to be PTRACEd
38  *
39  * Returns 0 if relationship was added, -ve on error.
40  */
41 static int yama_ptracer_add(struct task_struct *tracer,
42                             struct task_struct *tracee)
43 {
44         int rc = 0;
45         struct ptrace_relation *added;
46         struct ptrace_relation *entry, *relation = NULL;
47
48         added = kmalloc(sizeof(*added), GFP_KERNEL);
49         spin_lock(&ptracer_relations_lock);
50         list_for_each_entry(entry, &ptracer_relations, node)
51                 if (entry->tracee == tracee) {
52                         relation = entry;
53                         break;
54                 }
55         if (!relation) {
56                 relation = added;
57                 if (!relation) {
58                         rc = -ENOMEM;
59                         goto unlock_out;
60                 }
61                 relation->tracee = tracee;
62                 list_add(&relation->node, &ptracer_relations);
63         }
64         relation->tracer = tracer;
65
66 unlock_out:
67         spin_unlock(&ptracer_relations_lock);
68         if (added && added != relation)
69                 kfree(added);
70
71         return rc;
72 }
73
74 /**
75  * yama_ptracer_del - remove exceptions related to the given tasks
76  * @tracer: remove any relation where tracer task matches
77  * @tracee: remove any relation where tracee task matches
78  */
79 static void yama_ptracer_del(struct task_struct *tracer,
80                              struct task_struct *tracee)
81 {
82         struct ptrace_relation *relation;
83         struct list_head *list, *safe;
84
85         spin_lock(&ptracer_relations_lock);
86         list_for_each_safe(list, safe, &ptracer_relations) {
87                 relation = list_entry(list, struct ptrace_relation, node);
88                 if (relation->tracee == tracee ||
89                     relation->tracer == tracer) {
90                         list_del(&relation->node);
91                         kfree(relation);
92                 }
93         }
94         spin_unlock(&ptracer_relations_lock);
95 }
96
97 /**
98  * yama_task_free - check for task_pid to remove from exception list
99  * @task: task being removed
100  */
101 void yama_task_free(struct task_struct *task)
102 {
103         yama_ptracer_del(task, task);
104 }
105
106 /**
107  * yama_task_prctl - check for Yama-specific prctl operations
108  * @option: operation
109  * @arg2: argument
110  * @arg3: argument
111  * @arg4: argument
112  * @arg5: argument
113  *
114  * Return 0 on success, -ve on error.  -ENOSYS is returned when Yama
115  * does not handle the given option.
116  */
117 int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
118                            unsigned long arg4, unsigned long arg5)
119 {
120         int rc;
121
122         rc = cap_task_prctl(option, arg2, arg3, arg4, arg5);
123         if (rc != -ENOSYS)
124                 return rc;
125
126         switch (option) {
127         case PR_SET_PTRACER:
128                 if (arg2 == 0) {
129                         yama_ptracer_del(NULL, current);
130                         rc = 0;
131                 }
132                 else {
133                         struct task_struct *tracer;
134
135                         rcu_read_lock();
136                         tracer = find_task_by_vpid(arg2);
137                         if (tracer)
138                                 get_task_struct(tracer);
139                         else
140                                 rc = -EINVAL;
141                         rcu_read_unlock();
142
143                         if (tracer) {
144                                 rc = yama_ptracer_add(tracer, current);
145                                 put_task_struct(tracer);
146                         }
147                 }
148                 break;
149         }
150
151         return rc;
152 }
153
154 /**
155  * task_is_descendant - walk up a process family tree looking for a match
156  * @parent: the process to compare against while walking up from child
157  * @child: the process to start from while looking upwards for parent
158  *
159  * Returns 1 if child is a descendant of parent, 0 if not.
160  */
161 static int task_is_descendant(struct task_struct *parent,
162                               struct task_struct *child)
163 {
164         int rc = 0;
165         struct task_struct *walker = child;
166
167         if (!parent || !child)
168                 return 0;
169
170         rcu_read_lock();
171         read_lock(&tasklist_lock);
172         while (walker->pid > 0) {
173                 if (!thread_group_leader(walker))
174                         walker = walker->group_leader;
175                 if (walker == parent) {
176                         rc = 1;
177                         break;
178                 }
179                 walker = walker->real_parent;
180         }
181         read_unlock(&tasklist_lock);
182         rcu_read_unlock();
183
184         return rc;
185 }
186
187 /**
188  * ptracer_exception_found - tracer registered as exception for this tracee
189  * @tracer: the task_struct of the process attempting PTRACE
190  * @tracee: the task_struct of the process to be PTRACEd
191  *
192  * Returns 1 if tracer has is ptracer exception ancestor for tracee.
193  */
194 static int ptracer_exception_found(struct task_struct *tracer,
195                                    struct task_struct *tracee)
196 {
197         int rc = 0;
198         struct ptrace_relation *relation;
199         struct task_struct *parent = NULL;
200
201         spin_lock(&ptracer_relations_lock);
202
203         rcu_read_lock();
204         read_lock(&tasklist_lock);
205         if (!thread_group_leader(tracee))
206                 tracee = tracee->group_leader;
207         list_for_each_entry(relation, &ptracer_relations, node)
208                 if (relation->tracee == tracee) {
209                         parent = relation->tracer;
210                         break;
211                 }
212         read_unlock(&tasklist_lock);
213         rcu_read_unlock();
214
215         if (task_is_descendant(parent, tracer))
216                 rc = 1;
217         spin_unlock(&ptracer_relations_lock);
218
219         return rc;
220 }
221
222 /**
223  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
224  * @child: task that current task is attempting to PTRACE
225  * @mode: ptrace attach mode
226  *
227  * Returns 0 if following the ptrace is allowed, -ve on error.
228  */
229 int yama_ptrace_access_check(struct task_struct *child,
230                                     unsigned int mode)
231 {
232         int rc;
233
234         /* If standard caps disallows it, so does Yama.  We should
235          * only tighten restrictions further.
236          */
237         rc = cap_ptrace_access_check(child, mode);
238         if (rc)
239                 return rc;
240
241         /* require ptrace target be a child of ptracer on attach */
242         if (mode == PTRACE_MODE_ATTACH &&
243             ptrace_scope &&
244             !capable(CAP_SYS_PTRACE) &&
245             !task_is_descendant(current, child) &&
246             !ptracer_exception_found(current, child))
247                 rc = -EPERM;
248
249         if (rc) {
250                 char name[sizeof(current->comm)];
251                 printk_ratelimited(KERN_INFO "ptrace of non-child"
252                         " pid %d was attempted by: %s (pid %d)\n",
253                         child->pid,
254                         get_task_comm(name, current),
255                         current->pid);
256         }
257
258         return rc;
259 }
260
261 /**
262  * yama_inode_follow_link - check for symlinks in sticky world-writeable dirs
263  * @dentry: The inode/dentry of the symlink
264  * @nameidata: The path data of the symlink
265  *
266  * In the case of the protected_sticky_symlinks sysctl being enabled,
267  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
268  * in a sticky world-writable directory.  This is to protect privileged
269  * processes from failing races against path names that may change out
270  * from under them by way of other users creating malicious symlinks.
271  * It will permit symlinks to only be followed when outside a sticky
272  * world-writable directory, or when the uid of the symlink and follower
273  * match, or when the directory owner matches the symlink's owner.
274  *
275  * Returns 0 if following the symlink is allowed, -ve on error.
276  */
277 int yama_inode_follow_link(struct dentry *dentry,
278                                   struct nameidata *nameidata)
279 {
280         int rc = 0;
281         const struct inode *parent;
282         const struct inode *inode;
283         const struct cred *cred;
284
285         if (!protected_sticky_symlinks)
286                 return 0;
287
288         /* if inode isn't a symlink, don't try to evaluate blocking it */
289         inode = dentry->d_inode;
290         if (!S_ISLNK(inode->i_mode))
291                 return 0;
292
293         /* owner and follower match? */
294         cred = current_cred();
295         if (cred->fsuid == inode->i_uid)
296                 return 0;
297
298         /* check parent directory mode and owner */
299         spin_lock(&dentry->d_lock);
300         parent = dentry->d_parent->d_inode;
301         if ((parent->i_mode & (S_ISVTX|S_IWOTH)) == (S_ISVTX|S_IWOTH) &&
302             parent->i_uid != inode->i_uid) {
303                 rc = -EACCES;
304         }
305         spin_unlock(&dentry->d_lock);
306
307         if (rc) {
308                 char name[sizeof(current->comm)];
309                 printk_ratelimited(KERN_NOTICE "non-matching-uid symlink "
310                         "following attempted in sticky world-writable "
311                         "directory by %s (fsuid %d != %d)\n",
312                         get_task_comm(name, current),
313                         cred->fsuid, inode->i_uid);
314         }
315
316         return rc;
317 }
318
319 /**
320  * yama_path_link - verify that hardlinking is allowed
321  * @old_dentry: the source inode/dentry to hardlink from
322  * @new_dir: target directory
323  * @new_dentry: the target inode/dentry to hardlink to
324  *
325  * Block hardlink when all of:
326  *  - fsuid does not match inode
327  *  - not CAP_FOWNER
328  *  - and at least one of:
329  *    - inode is not a regular file
330  *    - inode is setuid
331  *    - inode is setgid and group-exec
332  *    - access failure for read and write
333  *
334  * Returns 0 if successful, -ve on error.
335  */
336 int yama_path_link(struct dentry *old_dentry, struct path *new_dir,
337                           struct dentry *new_dentry)
338 {
339         int rc = 0;
340         struct inode *inode = old_dentry->d_inode;
341         const int mode = inode->i_mode;
342         const struct cred *cred = current_cred();
343
344         if (!protected_nonaccess_hardlinks)
345                 return 0;
346
347         if (cred->fsuid != inode->i_uid &&
348             (!S_ISREG(mode) || (mode & S_ISUID) ||
349              ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) ||
350              (generic_permission(inode, MAY_READ | MAY_WRITE, 0, NULL))) &&
351             !capable(CAP_FOWNER)) {
352                 char name[sizeof(current->comm)];
353                 printk_ratelimited(KERN_INFO "non-accessible hardlink"
354                         " creation was attempted by: %s (fsuid %d)\n",
355                         get_task_comm(name, current),
356                         cred->fsuid);
357                 rc = -EPERM;
358         }
359
360         return rc;
361 }
362
363 static struct security_operations yama_ops = {
364         .name =                 "yama",
365
366         .ptrace_access_check =  yama_ptrace_access_check,
367         .inode_follow_link =    yama_inode_follow_link,
368         .path_link =            yama_path_link,
369         .task_prctl =           yama_task_prctl,
370         .task_free =            yama_task_free,
371 };
372
373 #ifdef CONFIG_SYSCTL
374 static int zero;
375 static int one = 1;
376
377 struct ctl_path yama_sysctl_path[] = {
378         { .procname = "kernel", },
379         { .procname = "yama", },
380         { }
381 };
382
383 static struct ctl_table yama_sysctl_table[] = {
384         {
385                 .procname       = "protected_sticky_symlinks",
386                 .data           = &protected_sticky_symlinks,
387                 .maxlen         = sizeof(int),
388                 .mode           = 0644,
389                 .proc_handler   = proc_dointvec_minmax,
390                 .extra1         = &zero,
391                 .extra2         = &one,
392         },
393         {
394                 .procname       = "protected_nonaccess_hardlinks",
395                 .data           = &protected_nonaccess_hardlinks,
396                 .maxlen         = sizeof(int),
397                 .mode           = 0644,
398                 .proc_handler   = proc_dointvec_minmax,
399                 .extra1         = &zero,
400                 .extra2         = &one,
401         },
402         {
403                 .procname       = "ptrace_scope",
404                 .data           = &ptrace_scope,
405                 .maxlen         = sizeof(int),
406                 .mode           = 0644,
407                 .proc_handler   = proc_dointvec_minmax,
408                 .extra1         = &zero,
409                 .extra2         = &one,
410         },
411         { }
412 };
413 #endif /* CONFIG_SYSCTL */
414
415 static __init int yama_init(void)
416 {
417         printk(KERN_INFO "Yama: becoming mindful.\n");
418
419 #ifdef CONFIG_SYSCTL
420         if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
421                 panic("Yama: sysctl registration failed.\n");
422 #endif
423
424         if (!security_module_enable(&yama_ops))
425                 return 0;
426
427         if (register_security(&yama_ops))
428                 panic("Yama: kernel registration failed.\n");
429
430         return 0;
431 }
432
433 security_initcall(yama_init);