summaryrefslogtreecommitdiffstats
path: root/ncr-limits.c
blob: c65b7b333d7f9fa94c7fe2fc94b143eb4879aae1 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * New driver for /dev/crypto device (aka CryptoDev)

 * Copyright (c) 2010 Nikos Mavrogiannopoulos <nmav@gnutls.org>
 *
 * This file is part of linux cryptodev.
 *
 * cryptodev is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * cryptodev is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/types.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/random.h>
#include "cryptodev.h"
#include <asm/atomic.h>
#include <linux/version.h>
#include "ncr.h"
#include "ncr_int.h"

/* arbitrary now */
unsigned int max_per_user[] = {
	[LIMIT_TYPE_KEY] = 128,
	[LIMIT_TYPE_DATA] = 128,
};

unsigned int max_per_process[] = {
	[LIMIT_TYPE_KEY] = 64,
	[LIMIT_TYPE_DATA] = 64,
};

struct limit_user_item_st {
	struct list_head list;
	uid_t uid;
	limits_type_t type;
	atomic_t cnt;
};

struct limit_process_item_st {
	struct list_head list;
	struct pid * pid;
	limits_type_t type;
	atomic_t cnt;
};

struct limit_st {
	struct list_sem_st users;
	struct list_sem_st processes;
} limits;

void ncr_limits_init(void)
{
	init_MUTEX(&limits.users.sem);
	INIT_LIST_HEAD(&limits.users.list);

	init_MUTEX(&limits.processes.sem);
	INIT_LIST_HEAD(&limits.processes.list);
}

int ncr_limits_add_and_check(struct file *filp, limits_type_t type)
{
struct limit_process_item_st* pitem;
struct limit_user_item_st* uitem;
uid_t uid;
int add = 1;

/* FIXME: is this uid ok?
 */
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
	uid = filp->f_uid;
#else
	uid = filp->f_cred->fsuid;
#endif

	down(&limits.users.sem);
	list_for_each_entry(uitem, &limits.users.list, list) {
		if (uitem->uid == uid && uitem->type == type) {
			add = 0;
			if (atomic_add_unless(&uitem->cnt, 1, max_per_user[type])==0) {
				err();
				up(&limits.users.sem);
				return -EPERM;
			}
		}
	}

	if (add) {
		uitem = kmalloc(GFP_KERNEL, sizeof(*uitem));
		if (uitem == NULL) {
			err();
			return -ENOMEM;
		}
		uitem->uid = uid;
		uitem->type = type;
		atomic_set(&uitem->cnt, 1);

		list_add(&uitem->list, &limits.users.list);
	}
	up(&limits.users.sem);

	add = 1;
	/* check process limits */
	down(&limits.processes.sem);
	list_for_each_entry(pitem, &limits.processes.list, list) {
		if (pitem->pid == filp->f_owner.pid && pitem->type == type) {
			add = 0;
			if (atomic_add_unless(&pitem->cnt, 1, max_per_process[type])==0) {
				err();
				up(&limits.processes.sem);
				return -EPERM;
			}
		}
	}
	

	if (add) {
		pitem = kmalloc(GFP_KERNEL, sizeof(*pitem));
		if (uitem == NULL) {
			err();
			return -ENOMEM;
		}
		pitem->pid = filp->f_owner.pid;
		pitem->type = type;
		atomic_set(&pitem->cnt, 1);

		list_add(&pitem->list, &limits.processes.list);
	}
	up(&limits.processes.sem);

	return 0;
}

void ncr_limits_remove(struct file *filp, limits_type_t type)
{
struct limit_process_item_st* pitem;
struct limit_user_item_st* uitem;
uid_t uid;

/* FIXME: is this uid ok?
 */
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
	uid = filp->f_uid;
#else
	uid = filp->f_cred->fsuid;
#endif

	down(&limits.users.sem);
	list_for_each_entry(uitem, &limits.users.list, list) {
		if (uitem->uid == uid && uitem->type == type) {
			atomic_dec(&uitem->cnt);
		}
	}
	up(&limits.users.sem);

	/* check process limits */
	down(&limits.processes.sem);
	list_for_each_entry(pitem, &limits.processes.list, list) {
		if (pitem->pid == filp->f_owner.pid && pitem->type == type) {
			atomic_dec(&pitem->cnt);
		}
	}
		up(&limits.processes.sem);

	return;
}