2 * Yama Linux Security Module
4 * Author: Kees Cook <kees.cook@canonical.com>
6 * Copyright (C) 2010 Canonical, Ltd.
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.
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>
20 static int ptrace_scope = 1;
21 static int protected_sticky_symlinks = 1;
22 static int protected_nonaccess_hardlinks = 1;
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;
31 static LIST_HEAD(ptracer_relations);
32 static DEFINE_SPINLOCK(ptracer_relations_lock);
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
39 * Returns 0 if relationship was added, -ve on error.
41 static int yama_ptracer_add(struct task_struct *tracer,
42 struct task_struct *tracee)
45 struct ptrace_relation *added;
46 struct ptrace_relation *entry, *relation = NULL;
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) {
61 relation->tracee = tracee;
62 list_add(&relation->node, &ptracer_relations);
64 relation->tracer = tracer;
67 spin_unlock(&ptracer_relations_lock);
68 if (added && added != relation)
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
79 static void yama_ptracer_del(struct task_struct *tracer,
80 struct task_struct *tracee)
82 struct ptrace_relation *relation;
83 struct list_head *list, *safe;
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);
94 spin_unlock(&ptracer_relations_lock);
98 * yama_task_free - check for task_pid to remove from exception list
99 * @task: task being removed
101 void yama_task_free(struct task_struct *task)
103 yama_ptracer_del(task, task);
107 * yama_task_prctl - check for Yama-specific prctl operations
114 * Return 0 on success, -ve on error. -ENOSYS is returned when Yama
115 * does not handle the given option.
117 int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
118 unsigned long arg4, unsigned long arg5)
122 rc = cap_task_prctl(option, arg2, arg3, arg4, arg5);
129 yama_ptracer_del(NULL, current);
133 struct task_struct *tracer;
136 tracer = find_task_by_vpid(arg2);
138 get_task_struct(tracer);
144 rc = yama_ptracer_add(tracer, current);
145 put_task_struct(tracer);
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
159 * Returns 1 if child is a descendant of parent, 0 if not.
161 static int task_is_descendant(struct task_struct *parent,
162 struct task_struct *child)
165 struct task_struct *walker = child;
167 if (!parent || !child)
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) {
179 walker = walker->real_parent;
181 read_unlock(&tasklist_lock);
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
192 * Returns 1 if tracer has is ptracer exception ancestor for tracee.
194 static int ptracer_exception_found(struct task_struct *tracer,
195 struct task_struct *tracee)
198 struct ptrace_relation *relation;
199 struct task_struct *parent = NULL;
201 spin_lock(&ptracer_relations_lock);
202 list_for_each_entry(relation, &ptracer_relations, node)
203 if (relation->tracee == tracee) {
204 parent = relation->tracer;
207 if (task_is_descendant(parent, tracer))
209 spin_unlock(&ptracer_relations_lock);
215 * yama_ptrace_access_check - validate PTRACE_ATTACH calls
216 * @child: task that current task is attempting to PTRACE
217 * @mode: ptrace attach mode
219 * Returns 0 if following the ptrace is allowed, -ve on error.
221 int yama_ptrace_access_check(struct task_struct *child,
226 /* If standard caps disallows it, so does Yama. We should
227 * only tighten restrictions further.
229 rc = cap_ptrace_access_check(child, mode);
233 /* require ptrace target be a child of ptracer on attach */
234 if (mode == PTRACE_MODE_ATTACH &&
236 !capable(CAP_SYS_PTRACE) &&
237 !task_is_descendant(current, child) &&
238 !ptracer_exception_found(current, child))
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",
246 get_task_comm(name, current),
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
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.
267 * Returns 0 if following the symlink is allowed, -ve on error.
269 int yama_inode_follow_link(struct dentry *dentry,
270 struct nameidata *nameidata)
273 const struct inode *parent;
274 const struct inode *inode;
275 const struct cred *cred;
277 if (!protected_sticky_symlinks)
280 /* owner and follower match? */
281 cred = current_cred();
282 inode = dentry->d_inode;
283 if (cred->fsuid == inode->i_uid)
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) {
293 spin_unlock(&dentry->d_lock);
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);
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
313 * Block hardlink when all of:
314 * - fsuid does not match inode
316 * - and at least one of:
317 * - inode is not a regular file
319 * - inode is setgid and group-exec
320 * - access failure for read and write
322 * Returns 0 if successful, -ve on error.
324 int yama_path_link(struct dentry *old_dentry, struct path *new_dir,
325 struct dentry *new_dentry)
328 struct inode *inode = old_dentry->d_inode;
329 const int mode = inode->i_mode;
330 const struct cred *cred = current_cred();
332 if (!protected_nonaccess_hardlinks)
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),
351 static struct security_operations yama_ops = {
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,
365 struct ctl_path yama_sysctl_path[] = {
366 { .procname = "kernel", },
367 { .procname = "yama", },
371 static struct ctl_table yama_sysctl_table[] = {
373 .procname = "protected_sticky_symlinks",
374 .data = &protected_sticky_symlinks,
375 .maxlen = sizeof(int),
377 .proc_handler = proc_dointvec_minmax,
382 .procname = "protected_nonaccess_hardlinks",
383 .data = &protected_nonaccess_hardlinks,
384 .maxlen = sizeof(int),
386 .proc_handler = proc_dointvec_minmax,
391 .procname = "ptrace_scope",
392 .data = &ptrace_scope,
393 .maxlen = sizeof(int),
395 .proc_handler = proc_dointvec_minmax,
401 #endif /* CONFIG_SYSCTL */
403 static __init int yama_init(void)
405 printk(KERN_INFO "Yama: becoming mindful.\n");
408 if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
409 panic("Yama: sysctl registration failed.\n");
412 if (!security_module_enable(&yama_ops))
415 if (register_security(&yama_ops))
416 panic("Yama: kernel registration failed.\n");
421 security_initcall(yama_init);