1515/* The initial domain. */
1616struct 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 */
162205LIST_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 */
374413LIST_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 */
566600LIST_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 */
700728LIST_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