f082a554931a27e5e584b738ad95f909fe306aa7
[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                         struct task_struct *myself = current;
135
136                         rcu_read_lock();
137                         if (!thread_group_leader(myself))
138                                 myself = myself->group_leader;
139                         get_task_struct(myself);
140                         tracer = find_task_by_vpid(arg2);
141                         if (tracer)
142                                 get_task_struct(tracer);
143                         else
144                                 rc = -EINVAL;
145                         rcu_read_unlock();
146
147                         if (tracer) {
148                                 rc = yama_ptracer_add(tracer, myself);
149                                 put_task_struct(tracer);
150                         }
151                         put_task_struct(myself);
152                 }
153                 break;
154         }
155
156         return rc;
157 }
158
159 /**
160  * task_is_descendant - walk up a process family tree looking for a match
161  * @parent: the process to compare against while walking up from child
162  * @child: the process to start from while looking upwards for parent
163  *
164  * Returns 1 if child is a descendant of parent, 0 if not.
165  */
166 static int task_is_descendant(struct task_struct *parent,
167                               struct task_struct *child)
168 {
169         int rc = 0;
170         struct task_struct *walker = child;
171
172         if (!parent || !child)
173                 return 0;
174
175         rcu_read_lock();
176         read_lock(&tasklist_lock);
177         while (walker->pid > 0) {
178                 if (!thread_group_leader(walker))
179                         walker = walker->group_leader;
180                 if (walker == parent) {
181                         rc = 1;
182                         break;
183                 }
184                 walker = walker->real_parent;
185         }
186         read_unlock(&tasklist_lock);
187         rcu_read_unlock();
188
189         return rc;
190 }
191
192 /**
193  * ptracer_exception_found - tracer registered as exception for this tracee
194  * @tracer: the task_struct of the process attempting PTRACE
195  * @tracee: the task_struct of the process to be PTRACEd
196  *
197  * Returns 1 if tracer has is ptracer exception ancestor for tracee.
198  */
199 static int ptracer_exception_found(struct task_struct *tracer,
200                                    struct task_struct *tracee)
201 {
202         int rc = 0;
203         struct ptrace_relation *relation;
204         struct task_struct *parent = NULL;
205
206         spin_lock(&ptracer_relations_lock);
207
208         rcu_read_lock();
209         read_lock(&tasklist_lock);
210         if (!thread_group_leader(tracee))
211                 tracee = tracee->group_leader;
212         list_for_each_entry(relation, &ptracer_relations, node)
213                 if (relation->tracee == tracee) {
214                         parent = relation->tracer;
215                         break;
216                 }
217         read_unlock(&tasklist_lock);
218         rcu_read_unlock();
219
220         if (task_is_descendant(parent, tracer))
221                 rc = 1;
222         spin_unlock(&ptracer_relations_lock);
223
224         return rc;
225 }
226
227 /**
228  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
229  * @child: task that current task is attempting to PTRACE
230  * @mode: ptrace attach mode
231  *
232  * Returns 0 if following the ptrace is allowed, -ve on error.
233  */
234 int yama_ptrace_access_check(struct task_struct *child,
235                                     unsigned int mode)
236 {
237         int rc;
238
239         /* If standard caps disallows it, so does Yama.  We should
240          * only tighten restrictions further.
241          */
242         rc = cap_ptrace_access_check(child, mode);
243         if (rc)
244                 return rc;
245
246         /* require ptrace target be a child of ptracer on attach */
247         if (mode == PTRACE_MODE_ATTACH &&
248             ptrace_scope &&
249             !capable(CAP_SYS_PTRACE) &&
250             !task_is_descendant(current, child) &&
251             !ptracer_exception_found(current, child))
252                 rc = -EPERM;
253
254         if (rc) {
255                 char name[sizeof(current->comm)];
256                 printk_ratelimited(KERN_INFO "ptrace of non-child"
257                         " pid %d was attempted by: %s (pid %d)\n",
258                         child->pid,
259                         get_task_comm(name, current),
260                         current->pid);
261         }
262
263         return rc;
264 }
265
266 /**
267  * yama_inode_follow_link - check for symlinks in sticky world-writeable dirs
268  * @dentry: The inode/dentry of the symlink
269  * @nameidata: The path data of the symlink
270  *
271  * In the case of the protected_sticky_symlinks sysctl being enabled,
272  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
273  * in a sticky world-writable directory.  This is to protect privileged
274  * processes from failing races against path names that may change out
275  * from under them by way of other users creating malicious symlinks.
276  * It will permit symlinks to only be followed when outside a sticky
277  * world-writable directory, or when the uid of the symlink and follower
278  * match, or when the directory owner matches the symlink's owner.
279  *
280  * Returns 0 if following the symlink is allowed, -ve on error.
281  */
282 int yama_inode_follow_link(struct dentry *dentry,
283                                   struct nameidata *nameidata)
284 {
285         int rc = 0;
286         const struct inode *parent;
287         const struct inode *inode;
288         const struct cred *cred;
289
290         if (!protected_sticky_symlinks)
291                 return 0;
292
293         /* if inode isn't a symlink, don't try to evaluate blocking it */
294         inode = dentry->d_inode;
295         if (!S_ISLNK(inode->i_mode))
296                 return 0;
297
298         /* owner and follower match? */
299         cred = current_cred();
300         if (cred->fsuid == inode->i_uid)
301                 return 0;
302
303         /* check parent directory mode and owner */
304         spin_lock(&dentry->d_lock);
305         parent = dentry->d_parent->d_inode;
306         if ((parent->i_mode & (S_ISVTX|S_IWOTH)) == (S_ISVTX|S_IWOTH) &&
307             parent->i_uid != inode->i_uid) {
308                 rc = -EACCES;
309         }
310         spin_unlock(&dentry->d_lock);
311
312         if (rc) {
313                 char name[sizeof(current->comm)];
314                 printk_ratelimited(KERN_NOTICE "non-matching-uid symlink "
315                         "following attempted in sticky world-writable "
316                         "directory by %s (fsuid %d != %d)\n",
317                         get_task_comm(name, current),
318                         cred->fsuid, inode->i_uid);
319         }
320
321         return rc;
322 }
323
324 /**
325  * yama_path_link - verify that hardlinking is allowed
326  * @old_dentry: the source inode/dentry to hardlink from
327  * @new_dir: target directory
328  * @new_dentry: the target inode/dentry to hardlink to
329  *
330  * Block hardlink when all of:
331  *  - fsuid does not match inode
332  *  - not CAP_FOWNER
333  *  - and at least one of:
334  *    - inode is not a regular file
335  *    - inode is setuid
336  *    - inode is setgid and group-exec
337  *    - access failure for read and write
338  *
339  * Returns 0 if successful, -ve on error.
340  */
341 int yama_path_link(struct dentry *old_dentry, struct path *new_dir,
342                           struct dentry *new_dentry)
343 {
344         int rc = 0;
345         struct inode *inode = old_dentry->d_inode;
346         const int mode = inode->i_mode;
347         const struct cred *cred = current_cred();
348
349         if (!protected_nonaccess_hardlinks)
350                 return 0;
351
352         if (cred->fsuid != inode->i_uid &&
353             (!S_ISREG(mode) || (mode & S_ISUID) ||
354              ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) ||
355              (generic_permission(inode, MAY_READ | MAY_WRITE, 0, NULL))) &&
356             !capable(CAP_FOWNER)) {
357                 char name[sizeof(current->comm)];
358                 printk_ratelimited(KERN_INFO "non-accessible hardlink"
359                         " creation was attempted by: %s (fsuid %d)\n",
360                         get_task_comm(name, current),
361                         cred->fsuid);
362                 rc = -EPERM;
363         }
364
365         return rc;
366 }
367
368 static struct security_operations yama_ops = {
369         .name =                 "yama",
370
371         .ptrace_access_check =  yama_ptrace_access_check,
372         .inode_follow_link =    yama_inode_follow_link,
373         .path_link =            yama_path_link,
374         .task_prctl =           yama_task_prctl,
375         .task_free =            yama_task_free,
376 };
377
378 #ifdef CONFIG_SYSCTL
379 static int zero;
380 static int one = 1;
381
382 struct ctl_path yama_sysctl_path[] = {
383         { .procname = "kernel", },
384         { .procname = "yama", },
385         { }
386 };
387
388 static struct ctl_table yama_sysctl_table[] = {
389         {
390                 .procname       = "protected_sticky_symlinks",
391                 .data           = &protected_sticky_symlinks,
392                 .maxlen         = sizeof(int),
393                 .mode           = 0644,
394                 .proc_handler   = proc_dointvec_minmax,
395                 .extra1         = &zero,
396                 .extra2         = &one,
397         },
398         {
399                 .procname       = "protected_nonaccess_hardlinks",
400                 .data           = &protected_nonaccess_hardlinks,
401                 .maxlen         = sizeof(int),
402                 .mode           = 0644,
403                 .proc_handler   = proc_dointvec_minmax,
404                 .extra1         = &zero,
405                 .extra2         = &one,
406         },
407         {
408                 .procname       = "ptrace_scope",
409                 .data           = &ptrace_scope,
410                 .maxlen         = sizeof(int),
411                 .mode           = 0644,
412                 .proc_handler   = proc_dointvec_minmax,
413                 .extra1         = &zero,
414                 .extra2         = &one,
415         },
416         { }
417 };
418 #endif /* CONFIG_SYSCTL */
419
420 static __init int yama_init(void)
421 {
422         printk(KERN_INFO "Yama: becoming mindful.\n");
423
424 #ifdef CONFIG_SYSCTL
425         if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
426                 panic("Yama: sysctl registration failed.\n");
427 #endif
428
429         if (!security_module_enable(&yama_ops))
430                 return 0;
431
432         if (register_security(&yama_ops))
433                 panic("Yama: kernel registration failed.\n");
434
435         return 0;
436 }
437
438 security_initcall(yama_init);