Skip to content

Commit bf24fb0

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Add refcounter on string data.
Add refcounter to "struct tomoyo_name_entry" and replace tomoyo_save_name() with tomoyo_get_name()/tomoyo_put_name() pair so that we can kfree() when garbage collector is added. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Acked-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent ca0b7df commit bf24fb0

5 files changed

Lines changed: 77 additions & 40 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <linux/uaccess.h>
1313
#include <linux/security.h>
1414
#include <linux/hardirq.h>
15-
#include "realpath.h"
1615
#include "common.h"
16+
#include "realpath.h"
1717
#include "tomoyo.h"
1818

1919
/* Lock for protecting policy. */
@@ -943,7 +943,9 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
943943
return -EINVAL;
944944
*cp = '\0';
945945
if (!strcmp(data, "COMMENT")) {
946-
profile->comment = tomoyo_save_name(cp + 1);
946+
const struct tomoyo_path_info *old_comment = profile->comment;
947+
profile->comment = tomoyo_get_name(cp + 1);
948+
tomoyo_put_name(old_comment);
947949
return 0;
948950
}
949951
for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
@@ -1117,7 +1119,7 @@ static int tomoyo_update_manager_entry(const char *manager,
11171119
if (!tomoyo_is_correct_path(manager, 1, -1, -1, __func__))
11181120
return -EINVAL;
11191121
}
1120-
saved_manager = tomoyo_save_name(manager);
1122+
saved_manager = tomoyo_get_name(manager);
11211123
if (!saved_manager)
11221124
return -ENOMEM;
11231125
if (!is_delete)
@@ -1132,12 +1134,14 @@ static int tomoyo_update_manager_entry(const char *manager,
11321134
}
11331135
if (!is_delete && error && tomoyo_memory_ok(entry)) {
11341136
entry->manager = saved_manager;
1137+
saved_manager = NULL;
11351138
entry->is_domain = is_domain;
11361139
list_add_tail_rcu(&entry->list, &tomoyo_policy_manager_list);
11371140
entry = NULL;
11381141
error = 0;
11391142
}
11401143
mutex_unlock(&tomoyo_policy_lock);
1144+
tomoyo_put_name(saved_manager);
11411145
kfree(entry);
11421146
return error;
11431147
}

‎security/tomoyo/domain.c‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
203203
{
204204
struct tomoyo_domain_initializer_entry *entry = NULL;
205205
struct tomoyo_domain_initializer_entry *ptr;
206-
const struct tomoyo_path_info *saved_program;
206+
const struct tomoyo_path_info *saved_program = NULL;
207207
const struct tomoyo_path_info *saved_domainname = NULL;
208208
int error = is_delete ? -ENOENT : -ENOMEM;
209209
bool is_last_name = false;
@@ -216,11 +216,11 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
216216
is_last_name = true;
217217
else if (!tomoyo_is_correct_domain(domainname, __func__))
218218
return -EINVAL;
219-
saved_domainname = tomoyo_save_name(domainname);
219+
saved_domainname = tomoyo_get_name(domainname);
220220
if (!saved_domainname)
221221
goto out;
222222
}
223-
saved_program = tomoyo_save_name(program);
223+
saved_program = tomoyo_get_name(program);
224224
if (!saved_program)
225225
goto out;
226226
if (!is_delete)
@@ -237,7 +237,9 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
237237
}
238238
if (!is_delete && error && tomoyo_memory_ok(entry)) {
239239
entry->domainname = saved_domainname;
240+
saved_domainname = NULL;
240241
entry->program = saved_program;
242+
saved_program = NULL;
241243
entry->is_not = is_not;
242244
entry->is_last_name = is_last_name;
243245
list_add_tail_rcu(&entry->list,
@@ -247,6 +249,8 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
247249
}
248250
mutex_unlock(&tomoyo_policy_lock);
249251
out:
252+
tomoyo_put_name(saved_domainname);
253+
tomoyo_put_name(saved_program);
250254
kfree(entry);
251255
return error;
252256
}
@@ -419,7 +423,7 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
419423
{
420424
struct tomoyo_domain_keeper_entry *entry = NULL;
421425
struct tomoyo_domain_keeper_entry *ptr;
422-
const struct tomoyo_path_info *saved_domainname;
426+
const struct tomoyo_path_info *saved_domainname = NULL;
423427
const struct tomoyo_path_info *saved_program = NULL;
424428
int error = is_delete ? -ENOENT : -ENOMEM;
425429
bool is_last_name = false;
@@ -432,11 +436,11 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
432436
if (program) {
433437
if (!tomoyo_is_correct_path(program, 1, -1, -1, __func__))
434438
return -EINVAL;
435-
saved_program = tomoyo_save_name(program);
439+
saved_program = tomoyo_get_name(program);
436440
if (!saved_program)
437441
goto out;
438442
}
439-
saved_domainname = tomoyo_save_name(domainname);
443+
saved_domainname = tomoyo_get_name(domainname);
440444
if (!saved_domainname)
441445
goto out;
442446
if (!is_delete)
@@ -453,7 +457,9 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
453457
}
454458
if (!is_delete && error && tomoyo_memory_ok(entry)) {
455459
entry->domainname = saved_domainname;
460+
saved_domainname = NULL;
456461
entry->program = saved_program;
462+
saved_program = NULL;
457463
entry->is_not = is_not;
458464
entry->is_last_name = is_last_name;
459465
list_add_tail_rcu(&entry->list, &tomoyo_domain_keeper_list);
@@ -462,6 +468,8 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
462468
}
463469
mutex_unlock(&tomoyo_policy_lock);
464470
out:
471+
tomoyo_put_name(saved_domainname);
472+
tomoyo_put_name(saved_program);
465473
kfree(entry);
466474
return error;
467475
}
@@ -623,8 +631,8 @@ static int tomoyo_update_alias_entry(const char *original_name,
623631
if (!tomoyo_is_correct_path(original_name, 1, -1, -1, __func__) ||
624632
!tomoyo_is_correct_path(aliased_name, 1, -1, -1, __func__))
625633
return -EINVAL; /* No patterns allowed. */
626-
saved_original_name = tomoyo_save_name(original_name);
627-
saved_aliased_name = tomoyo_save_name(aliased_name);
634+
saved_original_name = tomoyo_get_name(original_name);
635+
saved_aliased_name = tomoyo_get_name(aliased_name);
628636
if (!saved_original_name || !saved_aliased_name)
629637
goto out;
630638
if (!is_delete)
@@ -640,13 +648,17 @@ static int tomoyo_update_alias_entry(const char *original_name,
640648
}
641649
if (!is_delete && error && tomoyo_memory_ok(entry)) {
642650
entry->original_name = saved_original_name;
651+
saved_original_name = NULL;
643652
entry->aliased_name = saved_aliased_name;
653+
saved_aliased_name = NULL;
644654
list_add_tail_rcu(&entry->list, &tomoyo_alias_list);
645655
entry = NULL;
646656
error = 0;
647657
}
648658
mutex_unlock(&tomoyo_policy_lock);
649659
out:
660+
tomoyo_put_name(saved_original_name);
661+
tomoyo_put_name(saved_aliased_name);
650662
kfree(entry);
651663
return error;
652664
}
@@ -721,7 +733,7 @@ struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
721733

722734
if (!tomoyo_is_correct_domain(domainname, __func__))
723735
return NULL;
724-
saved_domainname = tomoyo_save_name(domainname);
736+
saved_domainname = tomoyo_get_name(domainname);
725737
if (!saved_domainname)
726738
return NULL;
727739
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
@@ -736,13 +748,15 @@ struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
736748
if (!found && tomoyo_memory_ok(entry)) {
737749
INIT_LIST_HEAD(&entry->acl_info_list);
738750
entry->domainname = saved_domainname;
751+
saved_domainname = NULL;
739752
entry->profile = profile;
740753
list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
741754
domain = entry;
742755
entry = NULL;
743756
found = true;
744757
}
745758
mutex_unlock(&tomoyo_policy_lock);
759+
tomoyo_put_name(saved_domainname);
746760
kfree(entry);
747761
return found ? domain : NULL;
748762
}

‎security/tomoyo/file.c‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static int tomoyo_update_globally_readable_entry(const char *filename,
222222

223223
if (!tomoyo_is_correct_path(filename, 1, 0, -1, __func__))
224224
return -EINVAL;
225-
saved_filename = tomoyo_save_name(filename);
225+
saved_filename = tomoyo_get_name(filename);
226226
if (!saved_filename)
227227
return -ENOMEM;
228228
if (!is_delete)
@@ -237,11 +237,13 @@ static int tomoyo_update_globally_readable_entry(const char *filename,
237237
}
238238
if (!is_delete && error && tomoyo_memory_ok(entry)) {
239239
entry->filename = saved_filename;
240+
saved_filename = NULL;
240241
list_add_tail_rcu(&entry->list, &tomoyo_globally_readable_list);
241242
entry = NULL;
242243
error = 0;
243244
}
244245
mutex_unlock(&tomoyo_policy_lock);
246+
tomoyo_put_name(saved_filename);
245247
kfree(entry);
246248
return error;
247249
}
@@ -365,7 +367,7 @@ static int tomoyo_update_file_pattern_entry(const char *pattern,
365367
const struct tomoyo_path_info *saved_pattern;
366368
int error = is_delete ? -ENOENT : -ENOMEM;
367369

368-
saved_pattern = tomoyo_save_name(pattern);
370+
saved_pattern = tomoyo_get_name(pattern);
369371
if (!saved_pattern)
370372
return error;
371373
if (!saved_pattern->is_patterned)
@@ -382,13 +384,15 @@ static int tomoyo_update_file_pattern_entry(const char *pattern,
382384
}
383385
if (!is_delete && error && tomoyo_memory_ok(entry)) {
384386
entry->pattern = saved_pattern;
387+
saved_pattern = NULL;
385388
list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
386389
entry = NULL;
387390
error = 0;
388391
}
389392
mutex_unlock(&tomoyo_policy_lock);
390393
out:
391394
kfree(entry);
395+
tomoyo_put_name(saved_pattern);
392396
return error;
393397
}
394398

@@ -518,7 +522,7 @@ static int tomoyo_update_no_rewrite_entry(const char *pattern,
518522

519523
if (!tomoyo_is_correct_path(pattern, 0, 0, 0, __func__))
520524
return -EINVAL;
521-
saved_pattern = tomoyo_save_name(pattern);
525+
saved_pattern = tomoyo_get_name(pattern);
522526
if (!saved_pattern)
523527
return error;
524528
if (!is_delete)
@@ -533,11 +537,13 @@ static int tomoyo_update_no_rewrite_entry(const char *pattern,
533537
}
534538
if (!is_delete && error && tomoyo_memory_ok(entry)) {
535539
entry->pattern = saved_pattern;
540+
saved_pattern = NULL;
536541
list_add_tail_rcu(&entry->list, &tomoyo_no_rewrite_list);
537542
entry = NULL;
538543
error = 0;
539544
}
540545
mutex_unlock(&tomoyo_policy_lock);
546+
tomoyo_put_name(saved_pattern);
541547
kfree(entry);
542548
return error;
543549
}
@@ -867,7 +873,7 @@ static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
867873
return -EINVAL;
868874
if (!tomoyo_is_correct_path(filename, 0, 0, 0, __func__))
869875
return -EINVAL;
870-
saved_filename = tomoyo_save_name(filename);
876+
saved_filename = tomoyo_get_name(filename);
871877
if (!saved_filename)
872878
return -ENOMEM;
873879
if (!is_delete)
@@ -913,12 +919,14 @@ static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
913919
if (perm == (1 << TOMOYO_TYPE_READ_WRITE_ACL))
914920
entry->perm |= rw_mask;
915921
entry->filename = saved_filename;
922+
saved_filename = NULL;
916923
list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
917924
entry = NULL;
918925
error = 0;
919926
}
920927
mutex_unlock(&tomoyo_policy_lock);
921928
kfree(entry);
929+
tomoyo_put_name(saved_filename);
922930
return error;
923931
}
924932

@@ -952,8 +960,8 @@ static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
952960
if (!tomoyo_is_correct_path(filename1, 0, 0, 0, __func__) ||
953961
!tomoyo_is_correct_path(filename2, 0, 0, 0, __func__))
954962
return -EINVAL;
955-
saved_filename1 = tomoyo_save_name(filename1);
956-
saved_filename2 = tomoyo_save_name(filename2);
963+
saved_filename1 = tomoyo_get_name(filename1);
964+
saved_filename2 = tomoyo_get_name(filename2);
957965
if (!saved_filename1 || !saved_filename2)
958966
goto out;
959967
if (!is_delete)
@@ -979,13 +987,17 @@ static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
979987
entry->head.type = TOMOYO_TYPE_DOUBLE_PATH_ACL;
980988
entry->perm = perm;
981989
entry->filename1 = saved_filename1;
990+
saved_filename1 = NULL;
982991
entry->filename2 = saved_filename2;
992+
saved_filename2 = NULL;
983993
list_add_tail_rcu(&entry->head.list, &domain->acl_info_list);
984994
entry = NULL;
985995
error = 0;
986996
}
987997
mutex_unlock(&tomoyo_policy_lock);
988998
out:
999+
tomoyo_put_name(saved_filename1);
1000+
tomoyo_put_name(saved_filename2);
9891001
kfree(entry);
9901002
return error;
9911003
}

‎security/tomoyo/realpath.c‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,6 @@ static unsigned int tomoyo_quota_for_savename;
253253
#define TOMOYO_HASH_BITS 8
254254
#define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
255255

256-
/*
257-
* tomoyo_name_entry is a structure which is used for linking
258-
* "struct tomoyo_path_info" into tomoyo_name_list .
259-
*
260-
* Since tomoyo_name_list manages a list of strings which are shared by
261-
* multiple processes (whereas "struct tomoyo_path_info" inside
262-
* "struct tomoyo_path_info_with_data" is not shared), a reference counter will
263-
* be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
264-
* when TOMOYO starts supporting garbage collector.
265-
*/
266-
struct tomoyo_name_entry {
267-
struct list_head list;
268-
struct tomoyo_path_info entry;
269-
};
270-
271256
/*
272257
* tomoyo_name_list is used for holding string data used by TOMOYO.
273258
* Since same string data is likely used for multiple times (e.g.
@@ -277,13 +262,13 @@ struct tomoyo_name_entry {
277262
static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
278263

279264
/**
280-
* tomoyo_save_name - Allocate permanent memory for string data.
265+
* tomoyo_get_name - Allocate permanent memory for string data.
281266
*
282267
* @name: The string to store into the permernent memory.
283268
*
284269
* Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
285270
*/
286-
const struct tomoyo_path_info *tomoyo_save_name(const char *name)
271+
const struct tomoyo_path_info *tomoyo_get_name(const char *name)
287272
{
288273
static DEFINE_MUTEX(lock);
289274
struct tomoyo_name_entry *ptr;
@@ -299,8 +284,10 @@ const struct tomoyo_path_info *tomoyo_save_name(const char *name)
299284
head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
300285
mutex_lock(&lock);
301286
list_for_each_entry(ptr, head, list) {
302-
if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
303-
goto out;
287+
if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
288+
continue;
289+
atomic_inc(&ptr->users);
290+
goto out;
304291
}
305292
ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
306293
allocated_len = ptr ? ksize(ptr) : 0;
@@ -309,7 +296,7 @@ const struct tomoyo_path_info *tomoyo_save_name(const char *name)
309296
> tomoyo_quota_for_savename)) {
310297
kfree(ptr);
311298
printk(KERN_WARNING "ERROR: Out of memory "
312-
"for tomoyo_save_name().\n");
299+
"for tomoyo_get_name().\n");
313300
if (!tomoyo_policy_loaded)
314301
panic("MAC Initialization failed.\n");
315302
ptr = NULL;
@@ -318,6 +305,7 @@ const struct tomoyo_path_info *tomoyo_save_name(const char *name)
318305
tomoyo_allocated_memory_for_savename += allocated_len;
319306
ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
320307
memmove((char *) ptr->entry.name, name, len);
308+
atomic_set(&ptr->users, 1);
321309
tomoyo_fill_path_info(&ptr->entry);
322310
list_add_tail(&ptr->list, head);
323311
out:
@@ -336,7 +324,7 @@ void __init tomoyo_realpath_init(void)
336324
for (i = 0; i < TOMOYO_MAX_HASH; i++)
337325
INIT_LIST_HEAD(&tomoyo_name_list[i]);
338326
INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
339-
tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME);
327+
tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
340328
/*
341329
* tomoyo_read_lock() is not needed because this function is
342330
* called before the first "delete" request.

0 commit comments

Comments
 (0)