Skip to content

Commit ca0b7df

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Reduce lines by using common path for addition and deletion.
Since the codes for adding an entry and removing an entry are similar, we can save some lines by using "if (is_delete) { ... } else { ... }" branches. 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 8007f10 commit ca0b7df

3 files changed

Lines changed: 183 additions & 226 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,10 +1103,10 @@ static LIST_HEAD(tomoyo_policy_manager_list);
11031103
static int tomoyo_update_manager_entry(const char *manager,
11041104
const bool is_delete)
11051105
{
1106-
struct tomoyo_policy_manager_entry *new_entry;
1106+
struct tomoyo_policy_manager_entry *entry = NULL;
11071107
struct tomoyo_policy_manager_entry *ptr;
11081108
const struct tomoyo_path_info *saved_manager;
1109-
int error = -ENOMEM;
1109+
int error = is_delete ? -ENOENT : -ENOMEM;
11101110
bool is_domain = false;
11111111

11121112
if (tomoyo_is_domain_def(manager)) {
@@ -1120,29 +1120,25 @@ static int tomoyo_update_manager_entry(const char *manager,
11201120
saved_manager = tomoyo_save_name(manager);
11211121
if (!saved_manager)
11221122
return -ENOMEM;
1123-
new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL);
1123+
if (!is_delete)
1124+
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
11241125
mutex_lock(&tomoyo_policy_lock);
11251126
list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
11261127
if (ptr->manager != saved_manager)
11271128
continue;
11281129
ptr->is_deleted = is_delete;
11291130
error = 0;
1130-
goto out;
1131+
break;
11311132
}
1132-
if (is_delete) {
1133-
error = -ENOENT;
1134-
goto out;
1133+
if (!is_delete && error && tomoyo_memory_ok(entry)) {
1134+
entry->manager = saved_manager;
1135+
entry->is_domain = is_domain;
1136+
list_add_tail_rcu(&entry->list, &tomoyo_policy_manager_list);
1137+
entry = NULL;
1138+
error = 0;
11351139
}
1136-
if (!tomoyo_memory_ok(new_entry))
1137-
goto out;
1138-
new_entry->manager = saved_manager;
1139-
new_entry->is_domain = is_domain;
1140-
list_add_tail_rcu(&new_entry->list, &tomoyo_policy_manager_list);
1141-
new_entry = NULL;
1142-
error = 0;
1143-
out:
11441140
mutex_unlock(&tomoyo_policy_lock);
1145-
kfree(new_entry);
1141+
kfree(entry);
11461142
return error;
11471143
}
11481144

‎security/tomoyo/domain.c‎

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
201201
const bool is_not,
202202
const bool is_delete)
203203
{
204-
struct tomoyo_domain_initializer_entry *new_entry;
204+
struct tomoyo_domain_initializer_entry *entry = NULL;
205205
struct tomoyo_domain_initializer_entry *ptr;
206206
const struct tomoyo_path_info *saved_program;
207207
const struct tomoyo_path_info *saved_domainname = NULL;
208-
int error = -ENOMEM;
208+
int error = is_delete ? -ENOENT : -ENOMEM;
209209
bool is_last_name = false;
210210

211211
if (!tomoyo_is_correct_path(program, 1, -1, -1, __func__))
@@ -218,12 +218,13 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
218218
return -EINVAL;
219219
saved_domainname = tomoyo_save_name(domainname);
220220
if (!saved_domainname)
221-
return -ENOMEM;
221+
goto out;
222222
}
223223
saved_program = tomoyo_save_name(program);
224224
if (!saved_program)
225-
return -ENOMEM;
226-
new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL);
225+
goto out;
226+
if (!is_delete)
227+
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
227228
mutex_lock(&tomoyo_policy_lock);
228229
list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) {
229230
if (ptr->is_not != is_not ||
@@ -232,24 +233,21 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
232233
continue;
233234
ptr->is_deleted = is_delete;
234235
error = 0;
235-
goto out;
236+
break;
236237
}
237-
if (is_delete) {
238-
error = -ENOENT;
239-
goto out;
238+
if (!is_delete && error && tomoyo_memory_ok(entry)) {
239+
entry->domainname = saved_domainname;
240+
entry->program = saved_program;
241+
entry->is_not = is_not;
242+
entry->is_last_name = is_last_name;
243+
list_add_tail_rcu(&entry->list,
244+
&tomoyo_domain_initializer_list);
245+
entry = NULL;
246+
error = 0;
240247
}
241-
if (!tomoyo_memory_ok(new_entry))
242-
goto out;
243-
new_entry->domainname = saved_domainname;
244-
new_entry->program = saved_program;
245-
new_entry->is_not = is_not;
246-
new_entry->is_last_name = is_last_name;
247-
list_add_tail_rcu(&new_entry->list, &tomoyo_domain_initializer_list);
248-
new_entry = NULL;
249-
error = 0;
250-
out:
251248
mutex_unlock(&tomoyo_policy_lock);
252-
kfree(new_entry);
249+
out:
250+
kfree(entry);
253251
return error;
254252
}
255253

@@ -419,11 +417,11 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
419417
const bool is_not,
420418
const bool is_delete)
421419
{
422-
struct tomoyo_domain_keeper_entry *new_entry;
420+
struct tomoyo_domain_keeper_entry *entry = NULL;
423421
struct tomoyo_domain_keeper_entry *ptr;
424422
const struct tomoyo_path_info *saved_domainname;
425423
const struct tomoyo_path_info *saved_program = NULL;
426-
int error = -ENOMEM;
424+
int error = is_delete ? -ENOENT : -ENOMEM;
427425
bool is_last_name = false;
428426

429427
if (!tomoyo_is_domain_def(domainname) &&
@@ -436,12 +434,13 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
436434
return -EINVAL;
437435
saved_program = tomoyo_save_name(program);
438436
if (!saved_program)
439-
return -ENOMEM;
437+
goto out;
440438
}
441439
saved_domainname = tomoyo_save_name(domainname);
442440
if (!saved_domainname)
443-
return -ENOMEM;
444-
new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL);
441+
goto out;
442+
if (!is_delete)
443+
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
445444
mutex_lock(&tomoyo_policy_lock);
446445
list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
447446
if (ptr->is_not != is_not ||
@@ -450,24 +449,20 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
450449
continue;
451450
ptr->is_deleted = is_delete;
452451
error = 0;
453-
goto out;
452+
break;
454453
}
455-
if (is_delete) {
456-
error = -ENOENT;
457-
goto out;
454+
if (!is_delete && error && tomoyo_memory_ok(entry)) {
455+
entry->domainname = saved_domainname;
456+
entry->program = saved_program;
457+
entry->is_not = is_not;
458+
entry->is_last_name = is_last_name;
459+
list_add_tail_rcu(&entry->list, &tomoyo_domain_keeper_list);
460+
entry = NULL;
461+
error = 0;
458462
}
459-
if (!tomoyo_memory_ok(new_entry))
460-
goto out;
461-
new_entry->domainname = saved_domainname;
462-
new_entry->program = saved_program;
463-
new_entry->is_not = is_not;
464-
new_entry->is_last_name = is_last_name;
465-
list_add_tail_rcu(&new_entry->list, &tomoyo_domain_keeper_list);
466-
new_entry = NULL;
467-
error = 0;
468-
out:
469463
mutex_unlock(&tomoyo_policy_lock);
470-
kfree(new_entry);
464+
out:
465+
kfree(entry);
471466
return error;
472467
}
473468

@@ -619,43 +614,40 @@ static int tomoyo_update_alias_entry(const char *original_name,
619614
const char *aliased_name,
620615
const bool is_delete)
621616
{
622-
struct tomoyo_alias_entry *new_entry;
617+
struct tomoyo_alias_entry *entry = NULL;
623618
struct tomoyo_alias_entry *ptr;
624619
const struct tomoyo_path_info *saved_original_name;
625620
const struct tomoyo_path_info *saved_aliased_name;
626-
int error = -ENOMEM;
621+
int error = is_delete ? -ENOENT : -ENOMEM;
627622

628623
if (!tomoyo_is_correct_path(original_name, 1, -1, -1, __func__) ||
629624
!tomoyo_is_correct_path(aliased_name, 1, -1, -1, __func__))
630625
return -EINVAL; /* No patterns allowed. */
631626
saved_original_name = tomoyo_save_name(original_name);
632627
saved_aliased_name = tomoyo_save_name(aliased_name);
633628
if (!saved_original_name || !saved_aliased_name)
634-
return -ENOMEM;
635-
new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL);
629+
goto out;
630+
if (!is_delete)
631+
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
636632
mutex_lock(&tomoyo_policy_lock);
637633
list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
638634
if (ptr->original_name != saved_original_name ||
639635
ptr->aliased_name != saved_aliased_name)
640636
continue;
641637
ptr->is_deleted = is_delete;
642638
error = 0;
643-
goto out;
639+
break;
644640
}
645-
if (is_delete) {
646-
error = -ENOENT;
647-
goto out;
641+
if (!is_delete && error && tomoyo_memory_ok(entry)) {
642+
entry->original_name = saved_original_name;
643+
entry->aliased_name = saved_aliased_name;
644+
list_add_tail_rcu(&entry->list, &tomoyo_alias_list);
645+
entry = NULL;
646+
error = 0;
648647
}
649-
if (!tomoyo_memory_ok(new_entry))
650-
goto out;
651-
new_entry->original_name = saved_original_name;
652-
new_entry->aliased_name = saved_aliased_name;
653-
list_add_tail_rcu(&new_entry->list, &tomoyo_alias_list);
654-
new_entry = NULL;
655-
error = 0;
656-
out:
657648
mutex_unlock(&tomoyo_policy_lock);
658-
kfree(new_entry);
649+
out:
650+
kfree(entry);
659651
return error;
660652
}
661653

@@ -722,32 +714,37 @@ struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
722714
domainname,
723715
const u8 profile)
724716
{
717+
struct tomoyo_domain_info *entry;
725718
struct tomoyo_domain_info *domain;
726719
const struct tomoyo_path_info *saved_domainname;
720+
bool found = false;
727721

728-
mutex_lock(&tomoyo_policy_lock);
729-
domain = tomoyo_find_domain(domainname);
730-
if (domain)
731-
goto out;
732722
if (!tomoyo_is_correct_domain(domainname, __func__))
733-
goto out;
723+
return NULL;
734724
saved_domainname = tomoyo_save_name(domainname);
735725
if (!saved_domainname)
736-
goto out;
737-
domain = kmalloc(sizeof(*domain), GFP_KERNEL);
738-
if (tomoyo_memory_ok(domain)) {
739-
INIT_LIST_HEAD(&domain->acl_info_list);
740-
domain->domainname = saved_domainname;
741-
domain->profile = profile;
742-
list_add_tail_rcu(&domain->list, &tomoyo_domain_list);
743-
} else {
744-
kfree(domain);
745-
domain = NULL;
726+
return NULL;
727+
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
728+
mutex_lock(&tomoyo_policy_lock);
729+
list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
730+
if (domain->is_deleted ||
731+
tomoyo_pathcmp(saved_domainname, domain->domainname))
732+
continue;
733+
found = true;
734+
break;
735+
}
736+
if (!found && tomoyo_memory_ok(entry)) {
737+
INIT_LIST_HEAD(&entry->acl_info_list);
738+
entry->domainname = saved_domainname;
739+
entry->profile = profile;
740+
list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
741+
domain = entry;
742+
entry = NULL;
743+
found = true;
746744
}
747-
748-
out:
749745
mutex_unlock(&tomoyo_policy_lock);
750-
return domain;
746+
kfree(entry);
747+
return found ? domain : NULL;
751748
}
752749

753750
/**

0 commit comments

Comments
 (0)