Skip to content

Commit f4f154f

Browse files
mitaLinus Torvalds
authored andcommitted
[PATCH] fault injection: process filtering for fault-injection capabilities
This patch provides process filtering feature. The process filter allows failing only permitted processes by /proc/<pid>/make-it-fail Please see the example that demostrates how to inject slab allocation failures into module init/cleanup code in Documentation/fault-injection/fault-injection.txt Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent c17bb49 commit f4f154f

4 files changed

Lines changed: 86 additions & 1 deletion

File tree

‎fs/proc/base.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,65 @@ static struct file_operations proc_seccomp_operations = {
853853
};
854854
#endif /* CONFIG_SECCOMP */
855855

856+
#ifdef CONFIG_FAULT_INJECTION
857+
static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
858+
size_t count, loff_t *ppos)
859+
{
860+
struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
861+
char buffer[PROC_NUMBUF];
862+
size_t len;
863+
int make_it_fail;
864+
loff_t __ppos = *ppos;
865+
866+
if (!task)
867+
return -ESRCH;
868+
make_it_fail = task->make_it_fail;
869+
put_task_struct(task);
870+
871+
len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
872+
if (__ppos >= len)
873+
return 0;
874+
if (count > len-__ppos)
875+
count = len-__ppos;
876+
if (copy_to_user(buf, buffer + __ppos, count))
877+
return -EFAULT;
878+
*ppos = __ppos + count;
879+
return count;
880+
}
881+
882+
static ssize_t proc_fault_inject_write(struct file * file,
883+
const char __user * buf, size_t count, loff_t *ppos)
884+
{
885+
struct task_struct *task;
886+
char buffer[PROC_NUMBUF], *end;
887+
int make_it_fail;
888+
889+
if (!capable(CAP_SYS_RESOURCE))
890+
return -EPERM;
891+
memset(buffer, 0, sizeof(buffer));
892+
if (count > sizeof(buffer) - 1)
893+
count = sizeof(buffer) - 1;
894+
if (copy_from_user(buffer, buf, count))
895+
return -EFAULT;
896+
make_it_fail = simple_strtol(buffer, &end, 0);
897+
if (*end == '\n')
898+
end++;
899+
task = get_proc_task(file->f_dentry->d_inode);
900+
if (!task)
901+
return -ESRCH;
902+
task->make_it_fail = make_it_fail;
903+
put_task_struct(task);
904+
if (end - buffer == 0)
905+
return -EIO;
906+
return end - buffer;
907+
}
908+
909+
static struct file_operations proc_fault_inject_operations = {
910+
.read = proc_fault_inject_read,
911+
.write = proc_fault_inject_write,
912+
};
913+
#endif
914+
856915
static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
857916
{
858917
struct inode *inode = dentry->d_inode;
@@ -1793,6 +1852,9 @@ static struct pid_entry tgid_base_stuff[] = {
17931852
#ifdef CONFIG_AUDITSYSCALL
17941853
REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
17951854
#endif
1855+
#ifdef CONFIG_FAULT_INJECTION
1856+
REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1857+
#endif
17961858
};
17971859

17981860
static int proc_tgid_base_readdir(struct file * filp,
@@ -2068,6 +2130,9 @@ static struct pid_entry tid_base_stuff[] = {
20682130
#ifdef CONFIG_AUDITSYSCALL
20692131
REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
20702132
#endif
2133+
#ifdef CONFIG_FAULT_INJECTION
2134+
REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2135+
#endif
20712136
};
20722137

20732138
static int proc_tid_base_readdir(struct file * filp,

‎include/linux/fault-inject.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct fault_attr {
1717
atomic_t times;
1818
atomic_t space;
1919
unsigned long verbose;
20+
u32 task_filter;
2021

2122
unsigned long count;
2223

@@ -30,6 +31,7 @@ struct fault_attr {
3031
struct dentry *times_file;
3132
struct dentry *space_file;
3233
struct dentry *verbose_file;
34+
struct dentry *task_filter_file;
3335
} dentries;
3436

3537
#endif

‎include/linux/sched.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,9 @@ struct task_struct {
10451045
#ifdef CONFIG_TASK_DELAY_ACCT
10461046
struct task_delay_info *delays;
10471047
#endif
1048+
#ifdef CONFIG_FAULT_INJECTION
1049+
int make_it_fail;
1050+
#endif
10481051
};
10491052

10501053
static inline pid_t process_group(struct task_struct *tsk)

‎lib/fault-inject.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/types.h>
66
#include <linux/fs.h>
77
#include <linux/module.h>
8+
#include <linux/interrupt.h>
89
#include <linux/fault-inject.h>
910

1011
/*
@@ -44,13 +45,21 @@ static void fail_dump(struct fault_attr *attr)
4445

4546
#define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
4647

48+
static int fail_task(struct fault_attr *attr, struct task_struct *task)
49+
{
50+
return !in_interrupt() && task->make_it_fail;
51+
}
52+
4753
/*
4854
* This code is stolen from failmalloc-1.0
4955
* http://www.nongnu.org/failmalloc/
5056
*/
5157

5258
int should_fail(struct fault_attr *attr, ssize_t size)
5359
{
60+
if (attr->task_filter && !fail_task(attr, current))
61+
return 0;
62+
5463
if (atomic_read(&attr->times) == 0)
5564
return 0;
5665

@@ -135,6 +144,9 @@ void cleanup_fault_attr_dentries(struct fault_attr *attr)
135144
debugfs_remove(attr->dentries.verbose_file);
136145
attr->dentries.verbose_file = NULL;
137146

147+
debugfs_remove(attr->dentries.task_filter_file);
148+
attr->dentries.task_filter_file = NULL;
149+
138150
if (attr->dentries.dir)
139151
WARN_ON(!simple_empty(attr->dentries.dir));
140152

@@ -169,9 +181,12 @@ int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
169181
attr->dentries.verbose_file =
170182
debugfs_create_ul("verbose", mode, dir, &attr->verbose);
171183

184+
attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
185+
mode, dir, &attr->task_filter);
186+
172187
if (!attr->dentries.probability_file || !attr->dentries.interval_file
173188
|| !attr->dentries.times_file || !attr->dentries.space_file
174-
|| !attr->dentries.verbose_file)
189+
|| !attr->dentries.verbose_file || !attr->dentries.task_filter_file)
175190
goto fail;
176191

177192
return 0;

0 commit comments

Comments
 (0)