summaryrefslogtreecommitdiff
path: root/us_manager/pf/proc_filters.c
blob: 43004a0b19fa544c24c5693e5327a3882f64eb03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/mm_types.h>
#include <linux/fs.h>
#include "proc_filters.h"
#include <sspt/sspt.h>

static int check_dentry(struct task_struct *task, struct dentry *dentry)
{
	struct vm_area_struct *vma;
	struct mm_struct *mm = task->mm;

	if (mm == NULL) {
		return 0;
	}

	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		if (check_vma(vma) && vma->vm_file->f_dentry == dentry) {
			return 1;
		}
	}

	return 0;
}

static struct task_struct *call_by_dentry(struct proc_filter *self,
					 struct task_struct *task)
{
	struct dentry *dentry = (struct dentry *)self->data;

	if (!dentry || check_dentry(task, dentry))
		return task;

	return NULL;
}

static struct task_struct *call_by_tgid(struct proc_filter *self,
				       struct task_struct *task)
{
	pid_t tgid = (pid_t)self->data;

	if (task->tgid == tgid)
		return task;

	return NULL;
}

static struct proc_filter *create_pf(void)
{
	struct proc_filter *pf = kmalloc(sizeof(*pf), GFP_KERNEL);

	return pf;
}

struct proc_filter *create_pf_by_dentry(struct dentry *dentry)
{
	struct proc_filter *pf = create_pf();

	pf->call = &call_by_dentry;
	pf->data = (void *)dentry;

	return pf;
}
struct proc_filter *create_pf_by_tgid(pid_t tgid)
{
	struct proc_filter *pf = create_pf();

	pf->call = &call_by_tgid;
	pf->data = (void *)tgid;

	return pf;
}

void free_pf(struct proc_filter *pf)
{
	kfree(pf);
}

int check_pf_by_dentry(struct proc_filter *filter, struct dentry *dentry)
{
	return filter->data == (void *)dentry &&
	       filter->call == &call_by_dentry;
}

int check_pf_by_tgid(struct proc_filter *filter, pid_t tgid)
{
	return filter->data == (void *)tgid && filter->call == &call_by_tgid;
}