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