Skip to content

Commit 36f5e1f

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Use callback for updating entries.
Use common code for elements using "struct list_head" + "bool" structure. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 82e0f00 commit 36f5e1f

6 files changed

Lines changed: 190 additions & 252 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
469469
*/
470470
LIST_HEAD(tomoyo_policy_manager_list);
471471

472+
static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head *a,
473+
const struct tomoyo_acl_head *b)
474+
{
475+
return container_of(a, struct tomoyo_policy_manager_entry, head)
476+
->manager ==
477+
container_of(b, struct tomoyo_policy_manager_entry, head)
478+
->manager;
479+
}
480+
472481
/**
473482
* tomoyo_update_manager_entry - Add a manager entry.
474483
*
@@ -482,9 +491,8 @@ LIST_HEAD(tomoyo_policy_manager_list);
482491
static int tomoyo_update_manager_entry(const char *manager,
483492
const bool is_delete)
484493
{
485-
struct tomoyo_policy_manager_entry *ptr;
486494
struct tomoyo_policy_manager_entry e = { };
487-
int error = is_delete ? -ENOENT : -ENOMEM;
495+
int error;
488496

489497
if (tomoyo_is_domain_def(manager)) {
490498
if (!tomoyo_is_correct_domain(manager))
@@ -497,26 +505,9 @@ static int tomoyo_update_manager_entry(const char *manager,
497505
e.manager = tomoyo_get_name(manager);
498506
if (!e.manager)
499507
return -ENOMEM;
500-
if (mutex_lock_interruptible(&tomoyo_policy_lock))
501-
goto out;
502-
list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, head.list) {
503-
if (ptr->manager != e.manager)
504-
continue;
505-
ptr->head.is_deleted = is_delete;
506-
error = 0;
507-
break;
508-
}
509-
if (!is_delete && error) {
510-
struct tomoyo_policy_manager_entry *entry =
511-
tomoyo_commit_ok(&e, sizeof(e));
512-
if (entry) {
513-
list_add_tail_rcu(&entry->head.list,
514-
&tomoyo_policy_manager_list);
515-
error = 0;
516-
}
517-
}
518-
mutex_unlock(&tomoyo_policy_lock);
519-
out:
508+
error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
509+
&tomoyo_policy_manager_list,
510+
tomoyo_same_manager_entry);
520511
tomoyo_put_name(e.manager);
521512
return error;
522513
}

‎security/tomoyo/common.h‎

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,12 @@ int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
913913
bool (*merge_duplicate) (struct tomoyo_acl_info *,
914914
struct tomoyo_acl_info *,
915915
const bool));
916+
int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
917+
bool is_delete, struct list_head *list,
918+
bool (*check_duplicate) (const struct tomoyo_acl_head
919+
*,
920+
const struct tomoyo_acl_head
921+
*));
916922

917923
/********** External variable definitions. **********/
918924

@@ -1042,39 +1048,6 @@ static inline bool tomoyo_is_same_number_union
10421048
p1->max_type == p2->max_type && p1->is_group == p2->is_group;
10431049
}
10441050

1045-
static inline bool tomoyo_is_same_domain_initializer_entry
1046-
(const struct tomoyo_domain_initializer_entry *p1,
1047-
const struct tomoyo_domain_initializer_entry *p2)
1048-
{
1049-
return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1050-
&& p1->domainname == p2->domainname
1051-
&& p1->program == p2->program;
1052-
}
1053-
1054-
static inline bool tomoyo_is_same_domain_keeper_entry
1055-
(const struct tomoyo_domain_keeper_entry *p1,
1056-
const struct tomoyo_domain_keeper_entry *p2)
1057-
{
1058-
return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
1059-
&& p1->domainname == p2->domainname
1060-
&& p1->program == p2->program;
1061-
}
1062-
1063-
static inline bool tomoyo_is_same_aggregator_entry
1064-
(const struct tomoyo_aggregator_entry *p1,
1065-
const struct tomoyo_aggregator_entry *p2)
1066-
{
1067-
return p1->original_name == p2->original_name &&
1068-
p1->aggregated_name == p2->aggregated_name;
1069-
}
1070-
1071-
static inline bool tomoyo_is_same_alias_entry
1072-
(const struct tomoyo_alias_entry *p1, const struct tomoyo_alias_entry *p2)
1073-
{
1074-
return p1->original_name == p2->original_name &&
1075-
p1->aliased_name == p2->aliased_name;
1076-
}
1077-
10781051
/**
10791052
* list_for_each_cookie - iterate over a list with cookie.
10801053
* @pos: the &struct list_head to use as a loop cursor.

‎security/tomoyo/domain.c‎

Lines changed: 103 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,49 @@
1515
/* The initial domain. */
1616
struct tomoyo_domain_info tomoyo_kernel_domain;
1717

18+
/**
19+
* tomoyo_update_policy - Update an entry for exception policy.
20+
*
21+
* @new_entry: Pointer to "struct tomoyo_acl_info".
22+
* @size: Size of @new_entry in bytes.
23+
* @is_delete: True if it is a delete request.
24+
* @list: Pointer to "struct list_head".
25+
* @check_duplicate: Callback function to find duplicated entry.
26+
*
27+
* Returns 0 on success, negative value otherwise.
28+
*
29+
* Caller holds tomoyo_read_lock().
30+
*/
31+
int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
32+
bool is_delete, struct list_head *list,
33+
bool (*check_duplicate) (const struct tomoyo_acl_head
34+
*,
35+
const struct tomoyo_acl_head
36+
*))
37+
{
38+
int error = is_delete ? -ENOENT : -ENOMEM;
39+
struct tomoyo_acl_head *entry;
40+
41+
if (mutex_lock_interruptible(&tomoyo_policy_lock))
42+
return -ENOMEM;
43+
list_for_each_entry_rcu(entry, list, list) {
44+
if (!check_duplicate(entry, new_entry))
45+
continue;
46+
entry->is_deleted = is_delete;
47+
error = 0;
48+
break;
49+
}
50+
if (error && !is_delete) {
51+
entry = tomoyo_commit_ok(new_entry, size);
52+
if (entry) {
53+
list_add_tail_rcu(&entry->list, list);
54+
error = 0;
55+
}
56+
}
57+
mutex_unlock(&tomoyo_policy_lock);
58+
return error;
59+
}
60+
1861
/**
1962
* tomoyo_update_domain - Update an entry for domain policy.
2063
*
@@ -161,6 +204,20 @@ const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
161204
*/
162205
LIST_HEAD(tomoyo_domain_initializer_list);
163206

207+
static bool tomoyo_same_domain_initializer_entry(const struct tomoyo_acl_head *
208+
a,
209+
const struct tomoyo_acl_head *
210+
b)
211+
{
212+
const struct tomoyo_domain_initializer_entry *p1 =
213+
container_of(a, typeof(*p1), head);
214+
const struct tomoyo_domain_initializer_entry *p2 =
215+
container_of(b, typeof(*p2), head);
216+
return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
217+
&& p1->domainname == p2->domainname
218+
&& p1->program == p2->program;
219+
}
220+
164221
/**
165222
* tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
166223
*
@@ -178,7 +235,6 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
178235
const bool is_not,
179236
const bool is_delete)
180237
{
181-
struct tomoyo_domain_initializer_entry *ptr;
182238
struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
183239
int error = is_delete ? -ENOENT : -ENOMEM;
184240

@@ -197,26 +253,9 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
197253
e.program = tomoyo_get_name(program);
198254
if (!e.program)
199255
goto out;
200-
if (mutex_lock_interruptible(&tomoyo_policy_lock))
201-
goto out;
202-
list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
203-
head.list) {
204-
if (!tomoyo_is_same_domain_initializer_entry(ptr, &e))
205-
continue;
206-
ptr->head.is_deleted = is_delete;
207-
error = 0;
208-
break;
209-
}
210-
if (!is_delete && error) {
211-
struct tomoyo_domain_initializer_entry *entry =
212-
tomoyo_commit_ok(&e, sizeof(e));
213-
if (entry) {
214-
list_add_tail_rcu(&entry->head.list,
215-
&tomoyo_domain_initializer_list);
216-
error = 0;
217-
}
218-
}
219-
mutex_unlock(&tomoyo_policy_lock);
256+
error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
257+
&tomoyo_domain_initializer_list,
258+
tomoyo_same_domain_initializer_entry);
220259
out:
221260
tomoyo_put_name(e.domainname);
222261
tomoyo_put_name(e.program);
@@ -373,6 +412,18 @@ static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
373412
*/
374413
LIST_HEAD(tomoyo_domain_keeper_list);
375414

415+
static bool tomoyo_same_domain_keeper_entry(const struct tomoyo_acl_head *a,
416+
const struct tomoyo_acl_head *b)
417+
{
418+
const struct tomoyo_domain_keeper_entry *p1 =
419+
container_of(a, typeof(*p1), head);
420+
const struct tomoyo_domain_keeper_entry *p2 =
421+
container_of(b, typeof(*p2), head);
422+
return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
423+
&& p1->domainname == p2->domainname
424+
&& p1->program == p2->program;
425+
}
426+
376427
/**
377428
* tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
378429
*
@@ -390,7 +441,6 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
390441
const bool is_not,
391442
const bool is_delete)
392443
{
393-
struct tomoyo_domain_keeper_entry *ptr;
394444
struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
395445
int error = is_delete ? -ENOENT : -ENOMEM;
396446

@@ -409,25 +459,9 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
409459
e.domainname = tomoyo_get_name(domainname);
410460
if (!e.domainname)
411461
goto out;
412-
if (mutex_lock_interruptible(&tomoyo_policy_lock))
413-
goto out;
414-
list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, head.list) {
415-
if (!tomoyo_is_same_domain_keeper_entry(ptr, &e))
416-
continue;
417-
ptr->head.is_deleted = is_delete;
418-
error = 0;
419-
break;
420-
}
421-
if (!is_delete && error) {
422-
struct tomoyo_domain_keeper_entry *entry =
423-
tomoyo_commit_ok(&e, sizeof(e));
424-
if (entry) {
425-
list_add_tail_rcu(&entry->head.list,
426-
&tomoyo_domain_keeper_list);
427-
error = 0;
428-
}
429-
}
430-
mutex_unlock(&tomoyo_policy_lock);
462+
error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
463+
&tomoyo_domain_keeper_list,
464+
tomoyo_same_domain_keeper_entry);
431465
out:
432466
tomoyo_put_name(e.domainname);
433467
tomoyo_put_name(e.program);
@@ -565,6 +599,17 @@ static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
565599
*/
566600
LIST_HEAD(tomoyo_aggregator_list);
567601

602+
static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
603+
const struct tomoyo_acl_head *b)
604+
{
605+
const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
606+
head);
607+
const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
608+
head);
609+
return p1->original_name == p2->original_name &&
610+
p1->aggregated_name == p2->aggregated_name;
611+
}
612+
568613
/**
569614
* tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
570615
*
@@ -580,7 +625,6 @@ static int tomoyo_update_aggregator_entry(const char *original_name,
580625
const char *aggregated_name,
581626
const bool is_delete)
582627
{
583-
struct tomoyo_aggregator_entry *ptr;
584628
struct tomoyo_aggregator_entry e = { };
585629
int error = is_delete ? -ENOENT : -ENOMEM;
586630

@@ -592,25 +636,9 @@ static int tomoyo_update_aggregator_entry(const char *original_name,
592636
if (!e.original_name || !e.aggregated_name ||
593637
e.aggregated_name->is_patterned) /* No patterns allowed. */
594638
goto out;
595-
if (mutex_lock_interruptible(&tomoyo_policy_lock))
596-
goto out;
597-
list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list, head.list) {
598-
if (!tomoyo_is_same_aggregator_entry(ptr, &e))
599-
continue;
600-
ptr->head.is_deleted = is_delete;
601-
error = 0;
602-
break;
603-
}
604-
if (!is_delete && error) {
605-
struct tomoyo_aggregator_entry *entry =
606-
tomoyo_commit_ok(&e, sizeof(e));
607-
if (entry) {
608-
list_add_tail_rcu(&entry->head.list,
609-
&tomoyo_aggregator_list);
610-
error = 0;
611-
}
612-
}
613-
mutex_unlock(&tomoyo_policy_lock);
639+
error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
640+
&tomoyo_aggregator_list,
641+
tomoyo_same_aggregator_entry);
614642
out:
615643
tomoyo_put_name(e.original_name);
616644
tomoyo_put_name(e.aggregated_name);
@@ -699,6 +727,17 @@ int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
699727
*/
700728
LIST_HEAD(tomoyo_alias_list);
701729

730+
static bool tomoyo_same_alias_entry(const struct tomoyo_acl_head *a,
731+
const struct tomoyo_acl_head *b)
732+
{
733+
const struct tomoyo_alias_entry *p1 = container_of(a, typeof(*p1),
734+
head);
735+
const struct tomoyo_alias_entry *p2 = container_of(b, typeof(*p2),
736+
head);
737+
return p1->original_name == p2->original_name &&
738+
p1->aliased_name == p2->aliased_name;
739+
}
740+
702741
/**
703742
* tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
704743
*
@@ -714,7 +753,6 @@ static int tomoyo_update_alias_entry(const char *original_name,
714753
const char *aliased_name,
715754
const bool is_delete)
716755
{
717-
struct tomoyo_alias_entry *ptr;
718756
struct tomoyo_alias_entry e = { };
719757
int error = is_delete ? -ENOENT : -ENOMEM;
720758

@@ -726,25 +764,9 @@ static int tomoyo_update_alias_entry(const char *original_name,
726764
if (!e.original_name || !e.aliased_name ||
727765
e.original_name->is_patterned || e.aliased_name->is_patterned)
728766
goto out; /* No patterns allowed. */
729-
if (mutex_lock_interruptible(&tomoyo_policy_lock))
730-
goto out;
731-
list_for_each_entry_rcu(ptr, &tomoyo_alias_list, head.list) {
732-
if (!tomoyo_is_same_alias_entry(ptr, &e))
733-
continue;
734-
ptr->head.is_deleted = is_delete;
735-
error = 0;
736-
break;
737-
}
738-
if (!is_delete && error) {
739-
struct tomoyo_alias_entry *entry =
740-
tomoyo_commit_ok(&e, sizeof(e));
741-
if (entry) {
742-
list_add_tail_rcu(&entry->head.list,
743-
&tomoyo_alias_list);
744-
error = 0;
745-
}
746-
}
747-
mutex_unlock(&tomoyo_policy_lock);
767+
error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
768+
&tomoyo_alias_list,
769+
tomoyo_same_alias_entry);
748770
out:
749771
tomoyo_put_name(e.original_name);
750772
tomoyo_put_name(e.aliased_name);

0 commit comments

Comments
 (0)