12 |
|
|
13 |
#include "internal.h" |
#include "internal.h" |
14 |
|
|
15 |
static atomic_t ccs_non_string_memory_size; |
static atomic_t ccs_policy_memory_size; |
16 |
static unsigned int ccs_quota_for_non_string; |
static unsigned int ccs_quota_for_policy; |
17 |
|
|
18 |
/** |
/** |
19 |
* ccs_memory_ok - Check memory quota. |
* ccs_memory_ok - Check memory quota. |
26 |
bool ccs_memory_ok(const void *ptr, const unsigned int size) |
bool ccs_memory_ok(const void *ptr, const unsigned int size) |
27 |
{ |
{ |
28 |
size_t s = ccs_round2(size); |
size_t s = ccs_round2(size); |
29 |
atomic_add(s, &ccs_non_string_memory_size); |
atomic_add(s, &ccs_policy_memory_size); |
30 |
if (ptr && (!ccs_quota_for_non_string || |
if (ptr && (!ccs_quota_for_policy || |
31 |
atomic_read(&ccs_non_string_memory_size) |
atomic_read(&ccs_policy_memory_size) |
32 |
<= ccs_quota_for_non_string)) |
<= ccs_quota_for_policy)) |
33 |
return true; |
return true; |
34 |
atomic_sub(s, &ccs_non_string_memory_size); |
atomic_sub(s, &ccs_policy_memory_size); |
35 |
printk(KERN_WARNING "ERROR: Out of memory. (%s)\n", __func__); |
printk(KERN_WARNING "ERROR: Out of memory. (%s)\n", __func__); |
36 |
if (!ccs_policy_loaded) |
if (!ccs_policy_loaded) |
37 |
panic("MAC Initialization failed.\n"); |
panic("MAC Initialization failed.\n"); |
66 |
*/ |
*/ |
67 |
void ccs_memory_free(const void *ptr, size_t size) |
void ccs_memory_free(const void *ptr, size_t size) |
68 |
{ |
{ |
69 |
atomic_sub(ccs_round2(size), &ccs_non_string_memory_size); |
atomic_sub(ccs_round2(size), &ccs_policy_memory_size); |
70 |
kfree(ptr); |
kfree(ptr); |
71 |
} |
} |
72 |
|
|
184 |
ccs_memory_free(cond, cond->size); |
ccs_memory_free(cond, cond->size); |
185 |
} |
} |
186 |
|
|
|
static unsigned int ccs_string_memory_size; |
|
|
static unsigned int ccs_quota_for_string; |
|
|
|
|
187 |
#define CCS_MAX_HASH 256 |
#define CCS_MAX_HASH 256 |
188 |
|
|
189 |
/* Structure for string data. */ |
/* Structure for string data. */ |
230 |
} |
} |
231 |
allocated_len = ccs_round2(sizeof(*ptr) + len); |
allocated_len = ccs_round2(sizeof(*ptr) + len); |
232 |
ptr = kzalloc(allocated_len, GFP_KERNEL); |
ptr = kzalloc(allocated_len, GFP_KERNEL); |
233 |
if (!ptr) |
if (!ptr || (ccs_quota_for_policy && |
234 |
allocated_len = 0; |
atomic_read(&ccs_policy_memory_size) + allocated_len |
235 |
ccs_string_memory_size += allocated_len; |
> ccs_quota_for_policy)) { |
|
if (!allocated_len || |
|
|
(ccs_quota_for_string && |
|
|
ccs_string_memory_size > ccs_quota_for_string)) { |
|
|
ccs_string_memory_size -= allocated_len; |
|
236 |
kfree(ptr); |
kfree(ptr); |
237 |
ptr = NULL; |
ptr = NULL; |
238 |
printk(KERN_WARNING "ERROR: Out of memory. (%s)\n", __func__); |
printk(KERN_WARNING "ERROR: Out of memory. (%s)\n", __func__); |
240 |
panic("MAC Initialization failed.\n"); |
panic("MAC Initialization failed.\n"); |
241 |
goto out; |
goto out; |
242 |
} |
} |
243 |
|
atomic_add(allocated_len, &ccs_policy_memory_size); |
244 |
ptr->entry.name = ((char *) ptr) + sizeof(*ptr); |
ptr->entry.name = ((char *) ptr) + sizeof(*ptr); |
245 |
memmove((char *) ptr->entry.name, name, len); |
memmove((char *) ptr->entry.name, name, len); |
246 |
atomic_set(&ptr->users, 1); |
atomic_set(&ptr->users, 1); |
269 |
mutex_lock(&ccs_name_list_lock); |
mutex_lock(&ccs_name_list_lock); |
270 |
if (atomic_dec_and_test(&ptr->users)) { |
if (atomic_dec_and_test(&ptr->users)) { |
271 |
list_del(&ptr->list); |
list_del(&ptr->list); |
|
ccs_string_memory_size -= ptr->size; |
|
272 |
can_delete = true; |
can_delete = true; |
273 |
} |
} |
274 |
mutex_unlock(&ccs_name_list_lock); |
mutex_unlock(&ccs_name_list_lock); |
275 |
/***** EXCLUSIVE SECTION END *****/ |
/***** EXCLUSIVE SECTION END *****/ |
276 |
if (can_delete) |
if (can_delete) { |
277 |
|
atomic_sub(ptr->size, &ccs_policy_memory_size); |
278 |
kfree(ptr); |
kfree(ptr); |
279 |
|
} |
280 |
} |
} |
281 |
|
|
282 |
struct srcu_struct ccs_ss; |
struct srcu_struct ccs_ss; |
345 |
int ccs_read_memory_counter(struct ccs_io_buffer *head) |
int ccs_read_memory_counter(struct ccs_io_buffer *head) |
346 |
{ |
{ |
347 |
if (!head->read_eof) { |
if (!head->read_eof) { |
348 |
const unsigned int string = ccs_string_memory_size; |
const unsigned int usage[3] = { |
349 |
const unsigned int nonstring |
atomic_read(&ccs_policy_memory_size), |
350 |
= atomic_read(&ccs_non_string_memory_size); |
ccs_audit_log_memory_size, |
351 |
const unsigned int audit_log = ccs_audit_log_memory_size; |
ccs_query_memory_size |
352 |
const unsigned int query = ccs_query_memory_size; |
}; |
353 |
char buffer[64]; |
const unsigned int quota[3] = { |
354 |
memset(buffer, 0, sizeof(buffer)); |
ccs_quota_for_policy, |
355 |
if (ccs_quota_for_string) |
ccs_quota_for_audit_log, |
356 |
snprintf(buffer, sizeof(buffer) - 1, |
ccs_quota_for_query |
357 |
" (Quota: %10u)", ccs_quota_for_string); |
}; |
358 |
else |
static const char *header[4] = { |
359 |
buffer[0] = '\0'; |
"Policy: ", |
360 |
ccs_io_printf(head, "Policy (string): %10u%s\n", |
"Audit logs: ", |
361 |
string, buffer); |
"Query lists:", |
362 |
if (ccs_quota_for_non_string) |
"Total: " |
363 |
snprintf(buffer, sizeof(buffer) - 1, |
}; |
364 |
" (Quota: %10u)", ccs_quota_for_non_string); |
unsigned int total = 0; |
365 |
else |
int i; |
366 |
buffer[0] = '\0'; |
for (i = 0; i < 3; i++) { |
367 |
ccs_io_printf(head, "Policy (non-string): %10u%s\n", |
total += usage[i]; |
368 |
nonstring, buffer); |
ccs_io_printf(head, "%s %10u", header[i], usage[i]); |
369 |
if (ccs_quota_for_audit_log) |
if (quota[i]) |
370 |
snprintf(buffer, sizeof(buffer) - 1, |
ccs_io_printf(head, " (Quota: %10u)", |
371 |
" (Quota: %10u)", ccs_quota_for_audit_log); |
quota[i]); |
372 |
else |
ccs_io_printf(head, "\n"); |
373 |
buffer[0] = '\0'; |
} |
374 |
ccs_io_printf(head, "Audit logs: %10u%s\n", |
ccs_io_printf(head, "%s %10u\n", header[3], total); |
|
audit_log, buffer); |
|
|
if (ccs_quota_for_query) |
|
|
snprintf(buffer, sizeof(buffer) - 1, |
|
|
" (Quota: %10u)", ccs_quota_for_query); |
|
|
else |
|
|
buffer[0] = '\0'; |
|
|
ccs_io_printf(head, "Interactive enforcement: %10u%s\n", |
|
|
query, buffer); |
|
|
ccs_io_printf(head, "Total: %10u\n", |
|
|
string + nonstring + audit_log + query); |
|
375 |
head->read_eof = true; |
head->read_eof = true; |
376 |
} |
} |
377 |
return 0; |
return 0; |
388 |
{ |
{ |
389 |
char *data = head->write_buf; |
char *data = head->write_buf; |
390 |
unsigned int size; |
unsigned int size; |
391 |
if (sscanf(data, "Policy (string): %u", &size) == 1) |
if (sscanf(data, "Policy: %u", &size) == 1) |
392 |
ccs_quota_for_string = size; |
ccs_quota_for_policy = size; |
|
else if (sscanf(data, "Policy (non-string): %u", &size) == 1) |
|
|
ccs_quota_for_non_string = size; |
|
393 |
else if (sscanf(data, "Audit logs: %u", &size) == 1) |
else if (sscanf(data, "Audit logs: %u", &size) == 1) |
394 |
ccs_quota_for_audit_log = size; |
ccs_quota_for_audit_log = size; |
395 |
else if (sscanf(data, "Interactive enforcement: %u", &size) == 1) |
else if (sscanf(data, "Query lists: %u", &size) == 1) |
396 |
ccs_quota_for_query = size; |
ccs_quota_for_query = size; |
397 |
return 0; |
return 0; |
398 |
} |
} |