summaryrefslogtreecommitdiffstats
path: root/crypto/userspace/ncr-limits.c
blob: 472f5cee33b7361c1f3a42b07ed5d336e1ddeb97 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * 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/cryptodev.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/ncr.h>
#include <linux/highmem.h>
#include <linux/random.h>
#include <asm/atomic.h>
#include <linux/version.h>
#include <linux/file.h>
#include <linux/cred.h>
#include "ncr_int.h"

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

static 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;
	pid_t pid;
	limits_type_t type;
	atomic_t cnt;
};

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

static struct limit_st 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);
}

void ncr_limits_deinit(void)
{
struct limit_process_item_st* pitem, *ptmp;
struct limit_user_item_st* uitem, *utmp;

	down(&limits.users.sem);
	list_for_each_entry_safe(uitem, utmp, &limits.users.list, list) {
		list_del(&uitem->list);
		kfree(uitem);
	}
	up(&limits.users.sem);
	
	down(&limits.processes.sem);
	list_for_each_entry_safe(pitem, ptmp, &limits.processes.list, list) {
		list_del(&pitem->list);
		kfree(pitem);
	}
	up(&limits.processes.sem);

}

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

	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( sizeof(*uitem), GFP_KERNEL);
		if (uitem == NULL) {
			err();
			up(&limits.users.sem);
			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 == pid && pitem->type == type) {
			add = 0;
			if (atomic_add_unless(&pitem->cnt, 1, max_per_process[type])==0) {
				err();
				up(&limits.processes.sem);

				ret = -EPERM;
				goto restore_user;
			}
		}
	}
	

	if (add) {
		pitem = kmalloc(sizeof(*pitem), GFP_KERNEL);
		if (uitem == NULL) {
			err();
			up(&limits.processes.sem);
			ret = -ENOMEM;
			goto restore_user;
		}
		pitem->pid = pid;
		pitem->type = type;
		atomic_set(&pitem->cnt, 1);

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

	return 0;

restore_user:
	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);
	return ret;
}

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

	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 == pid && pitem->type == type) {
			atomic_dec(&pitem->cnt);
		}
	}
	up(&limits.processes.sem);

	return;
}