UBUNTU: SAUCE: Yama: check PTRACE using thread group leader
[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         list_for_each_entry(relation, &ptracer_relations, node)
203                 if (relation->tracee == tracee) {
204                         parent = relation->tracer;
205                         break;
206                 }
207         if (task_is_descendant(parent, tracer))
208                 rc = 1;
209         spin_unlock(&ptracer_relations_lock);
210
211         return rc;
212 }
213
214 /**
215  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
216  * @child: task that current task is attempting to PTRACE
217  * @mode: ptrace attach mode
218  *
219  * Returns 0 if following the ptrace is allowed, -ve on error.
220  */
221 int yama_ptrace_access_check(struct task_struct *child,
222                                     unsigned int mode)
223 {
224         int rc;
225
226         /* If standard caps disallows it, so does Yama.  We should
227          * only tighten restrictions further.
228          */
229         rc = cap_ptrace_access_check(child, mode);
230         if (rc)
231                 return rc;
232
233         /* require ptrace target be a child of ptracer on attach */
234         if (mode == PTRACE_MODE_ATTACH &&
235             ptrace_scope &&
236             !capable(CAP_SYS_PTRACE) &&
237             !task_is_descendant(current, child) &&
238             !ptracer_exception_found(current, child))
239                 rc = -EPERM;
240
241         if (rc) {
242                 char name[sizeof(current->comm)];
243                 printk_ratelimited(KERN_INFO "ptrace of non-child"
244                         " pid %d was attempted by: %s (pid %d)\n",
245                         child->pid,
246                         get_task_comm(name, current),
247                         current->pid);
248         }
249
250         return rc;
251 }
252
253 /**
254  * yama_inode_follow_link - check for symlinks in sticky world-writeable dirs
255  * @dentry: The inode/dentry of the symlink
256  * @nameidata: The path data of the symlink
257  *
258  * In the case of the protected_sticky_symlinks sysctl being enabled,
259  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
260  * in a sticky world-writable directory.  This is to protect privileged
261  * processes from failing races against path names that may change out
262  * from under them by way of other users creating malicious symlinks.
263  * It will permit symlinks to only be followed when outside a sticky
264  * world-writable directory, or when the uid of the symlink and follower
265  * match, or when the directory owner matches the symlink's owner.
266  *
267  * Returns 0 if following the symlink is allowed, -ve on error.
268  */
269 int yama_inode_follow_link(struct dentry *dentry,
270                                   struct nameidata *nameidata)
271 {
272         int rc = 0;
273         const struct inode *parent;
274         const struct inode *inode;
275         const struct cred *cred;
276
277         if (!protected_sticky_symlinks)
278                 return 0;
279
280         /* owner and follower match? */
281         cred = current_cred();
282         inode = dentry->d_inode;
283         if (cred->fsuid == inode->i_uid)
284                 return 0;
285
286         /* check parent directory mode and owner */
287         spin_lock(&dentry->d_lock);
288         parent = dentry->d_parent->d_inode;
289         if ((parent->i_mode & (S_ISVTX|S_IWOTH)) == (S_ISVTX|S_IWOTH) &&
290             parent->i_uid != inode->i_uid) {
291                 rc = -EACCES;
292         }
293         spin_unlock(&dentry->d_lock);
294
295         if (rc) {
296                 char name[sizeof(current->comm)];
297                 printk_ratelimited(KERN_NOTICE "non-matching-uid symlink "
298                         "following attempted in sticky world-writable "
299                         "directory by %s (fsuid %d != %d)\n",
300                         get_task_comm(name, current),
301                         cred->fsuid, inode->i_uid);
302         }
303
304         return rc;
305 }
306
307 /**
308  * yama_path_link - verify that hardlinking is allowed
309  * @old_dentry: the source inode/dentry to hardlink from
310  * @new_dir: target directory
311  * @new_dentry: the target inode/dentry to hardlink to
312  *
313  * Block hardlink when all of:
314  *  - fsuid does not match inode
315  *  - not CAP_FOWNER
316  *  - and at least one of:
317  *    - inode is not a regular file
318  *    - inode is setuid
319  *    - inode is setgid and group-exec
320  *    - access failure for read and write
321  *
322  * Returns 0 if successful, -ve on error.
323  */
324 int yama_path_link(struct dentry *old_dentry, struct path *new_dir,
325                           struct dentry *new_dentry)
326 {
327         int rc = 0;
328         struct inode *inode = old_dentry->d_inode;
329         const int mode = inode->i_mode;
330         const struct cred *cred = current_cred();
331
332         if (!protected_nonaccess_hardlinks)
333                 return 0;
334
335         if (cred->fsuid != inode->i_uid &&
336             (!S_ISREG(mode) || (mode & S_ISUID) ||
337              ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) ||
338              (generic_permission(inode, MAY_READ | MAY_WRITE, NULL))) &&
339             !capable(CAP_FOWNER)) {
340                 char name[sizeof(current->comm)];
341                 printk_ratelimited(KERN_INFO "non-accessible hardlink"
342                         " creation was attempted by: %s (fsuid %d)\n",
343                         get_task_comm(name, current),
344                         cred->fsuid);
345                 rc = -EPERM;
346         }
347
348         return rc;
349 }
350
351 static struct security_operations yama_ops = {
352         .name =                 "yama",
353
354         .ptrace_access_check =  yama_ptrace_access_check,
355         .inode_follow_link =    yama_inode_follow_link,
356         .path_link =            yama_path_link,
357         .task_prctl =           yama_task_prctl,
358         .task_free =            yama_task_free,
359 };
360
361 #ifdef CONFIG_SYSCTL
362 static int zero;
363 static int one = 1;
364
365 struct ctl_path yama_sysctl_path[] = {
366         { .procname = "kernel", },
367         { .procname = "yama", },
368         { }
369 };
370
371 static struct ctl_table yama_sysctl_table[] = {
372         {
373                 .procname       = "protected_sticky_symlinks",
374                 .data           = &protected_sticky_symlinks,
375                 .maxlen         = sizeof(int),
376                 .mode           = 0644,
377                 .proc_handler   = proc_dointvec_minmax,
378                 .extra1         = &zero,
379                 .extra2         = &one,
380         },
381         {
382                 .procname       = "protected_nonaccess_hardlinks",
383                 .data           = &protected_nonaccess_hardlinks,
384                 .maxlen         = sizeof(int),
385                 .mode           = 0644,
386                 .proc_handler   = proc_dointvec_minmax,
387                 .extra1         = &zero,
388                 .extra2         = &one,
389         },
390         {
391                 .procname       = "ptrace_scope",
392                 .data           = &ptrace_scope,
393                 .maxlen         = sizeof(int),
394                 .mode           = 0644,
395                 .proc_handler   = proc_dointvec_minmax,
396                 .extra1         = &zero,
397                 .extra2         = &one,
398         },
399         { }
400 };
401 #endif /* CONFIG_SYSCTL */
402
403 static __init int yama_init(void)
404 {
405         printk(KERN_INFO "Yama: becoming mindful.\n");
406
407 #ifdef CONFIG_SYSCTL
408         if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
409                 panic("Yama: sysctl registration failed.\n");
410 #endif
411
412         if (!security_module_enable(&yama_ops))
413                 return 0;
414
415         if (register_security(&yama_ops))
416                 panic("Yama: kernel registration failed.\n");
417
418         return 0;
419 }
420
421 security_initcall(yama_init);