hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/security/tomoyo/common.c
....@@ -197,6 +197,7 @@
197197 {
198198 va_list args;
199199 const int pos = strlen(buffer);
200
+
200201 va_start(args, fmt);
201202 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
202203 va_end(args);
....@@ -214,6 +215,7 @@
214215 while (head->r.w_pos) {
215216 const char *w = head->r.w[0];
216217 size_t len = strlen(w);
218
+
217219 if (len) {
218220 if (len > head->read_user_buf_avail)
219221 len = head->read_user_buf_avail;
....@@ -279,6 +281,7 @@
279281 size_t len;
280282 size_t pos = head->r.avail;
281283 int size = head->readbuf_size - pos;
284
+
282285 if (size <= 0)
283286 return;
284287 va_start(args, fmt);
....@@ -344,13 +347,14 @@
344347 void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
345348 {
346349 unsigned int idx;
350
+
347351 for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
348352 INIT_LIST_HEAD(&ns->acl_group[idx]);
349353 for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
350354 INIT_LIST_HEAD(&ns->group_list[idx]);
351355 for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
352356 INIT_LIST_HEAD(&ns->policy_list[idx]);
353
- ns->profile_version = 20110903;
357
+ ns->profile_version = 20150505;
354358 tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
355359 list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
356360 }
....@@ -433,6 +437,7 @@
433437 u8 min_type = ptr->value_type[0];
434438 const u8 max_type = ptr->value_type[1];
435439 char buffer[128];
440
+
436441 buffer[0] = '\0';
437442 for (i = 0; i < 2; i++) {
438443 switch (min_type) {
....@@ -487,6 +492,7 @@
487492 {
488493 struct tomoyo_profile *ptr;
489494 struct tomoyo_profile *entry;
495
+
490496 if (profile >= TOMOYO_MAX_PROFILES)
491497 return NULL;
492498 ptr = ns->profile_ptr[profile];
....@@ -530,6 +536,7 @@
530536 {
531537 static struct tomoyo_profile tomoyo_null_profile;
532538 struct tomoyo_profile *ptr = ns->profile_ptr[profile];
539
+
533540 if (!ptr)
534541 ptr = &tomoyo_null_profile;
535542 return ptr;
....@@ -546,6 +553,7 @@
546553 static s8 tomoyo_find_yesno(const char *string, const char *find)
547554 {
548555 const char *cp = strstr(string, find);
556
+
549557 if (cp) {
550558 cp += strlen(find);
551559 if (!strncmp(cp, "=yes", 4))
....@@ -569,6 +577,7 @@
569577 const char *find)
570578 {
571579 const char *cp = strstr(string, find);
580
+
572581 if (cp)
573582 sscanf(cp + strlen(find), "=%u", i);
574583 }
....@@ -587,6 +596,7 @@
587596 {
588597 u8 i;
589598 u8 config;
599
+
590600 if (!strcmp(name, "CONFIG")) {
591601 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
592602 config = profile->default_config;
....@@ -595,10 +605,12 @@
595605 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
596606 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
597607 int len = 0;
608
+
598609 if (i < TOMOYO_MAX_MAC_INDEX) {
599610 const u8 c = tomoyo_index2category[i];
600611 const char *category =
601612 tomoyo_category_keywords[c];
613
+
602614 len = strlen(category);
603615 if (strncmp(name, category, len) ||
604616 name[len++] != ':' || name[len++] != ':')
....@@ -618,6 +630,7 @@
618630 config = TOMOYO_CONFIG_USE_DEFAULT;
619631 } else {
620632 u8 mode;
633
+
621634 for (mode = 0; mode < 4; mode++)
622635 if (strstr(value, tomoyo_mode[mode]))
623636 /*
....@@ -664,6 +677,7 @@
664677 unsigned int i;
665678 char *cp;
666679 struct tomoyo_profile *profile;
680
+
667681 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
668682 == 1)
669683 return 0;
....@@ -683,6 +697,7 @@
683697 const struct tomoyo_path_info *new_comment
684698 = tomoyo_get_name(cp);
685699 const struct tomoyo_path_info *old_comment;
700
+
686701 if (!new_comment)
687702 return -ENOMEM;
688703 spin_lock(&lock);
....@@ -732,6 +747,7 @@
732747 struct tomoyo_policy_namespace *ns =
733748 container_of(head->r.ns, typeof(*ns), namespace_list);
734749 const struct tomoyo_profile *profile;
750
+
735751 if (head->r.eof)
736752 return;
737753 next:
....@@ -760,6 +776,7 @@
760776 u8 i;
761777 const struct tomoyo_path_info *comment =
762778 profile->comment;
779
+
763780 tomoyo_print_namespace(head);
764781 tomoyo_io_printf(head, "%u-COMMENT=", index);
765782 tomoyo_set_string(head, comment ? comment->name : "");
....@@ -788,6 +805,7 @@
788805 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
789806 const u8 i = head->r.bit;
790807 const u8 config = profile->config[i];
808
+
791809 if (config == TOMOYO_CONFIG_USE_DEFAULT)
792810 continue;
793811 tomoyo_print_namespace(head);
....@@ -847,10 +865,10 @@
847865 struct tomoyo_acl_param param = {
848866 /* .ns = &tomoyo_kernel_namespace, */
849867 .is_delete = is_delete,
850
- .list = &tomoyo_kernel_namespace.
851
- policy_list[TOMOYO_ID_MANAGER],
868
+ .list = &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER],
852869 };
853870 int error = is_delete ? -ENOENT : -ENOMEM;
871
+
854872 if (!tomoyo_correct_domain(manager) &&
855873 !tomoyo_correct_word(manager))
856874 return -EINVAL;
....@@ -894,10 +912,10 @@
894912 {
895913 if (head->r.eof)
896914 return;
897
- list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
898
- policy_list[TOMOYO_ID_MANAGER]) {
915
+ list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER]) {
899916 struct tomoyo_manager *ptr =
900917 list_entry(head->r.acl, typeof(*ptr), head.list);
918
+
901919 if (ptr->head.is_deleted)
902920 continue;
903921 if (!tomoyo_flush(head))
....@@ -922,7 +940,7 @@
922940 const char *exe;
923941 const struct task_struct *task = current;
924942 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
925
- bool found = false;
943
+ bool found = IS_ENABLED(CONFIG_SECURITY_TOMOYO_INSECURE_BUILTIN_SETTING);
926944
927945 if (!tomoyo_policy_loaded)
928946 return true;
....@@ -933,8 +951,8 @@
933951 exe = tomoyo_get_exe();
934952 if (!exe)
935953 return false;
936
- list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
937
- policy_list[TOMOYO_ID_MANAGER], head.list) {
954
+ list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER], head.list,
955
+ srcu_read_lock_held(&tomoyo_ss)) {
938956 if (!ptr->head.is_deleted &&
939957 (!tomoyo_pathcmp(domainname, ptr->manager) ||
940958 !strcmp(exe, ptr->manager->name))) {
....@@ -945,9 +963,10 @@
945963 if (!found) { /* Reduce error messages. */
946964 static pid_t last_pid;
947965 const pid_t pid = current->pid;
966
+
948967 if (last_pid != pid) {
949
- printk(KERN_WARNING "%s ( %s ) is not permitted to "
950
- "update policies.\n", domainname->name, exe);
968
+ pr_warn("%s ( %s ) is not permitted to update policies.\n",
969
+ domainname->name, exe);
951970 last_pid = pid;
952971 }
953972 }
....@@ -974,19 +993,21 @@
974993 unsigned int pid;
975994 struct tomoyo_domain_info *domain = NULL;
976995 bool global_pid = false;
996
+
977997 if (strncmp(data, "select ", 7))
978998 return false;
979999 data += 7;
9801000 if (sscanf(data, "pid=%u", &pid) == 1 ||
9811001 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9821002 struct task_struct *p;
1003
+
9831004 rcu_read_lock();
9841005 if (global_pid)
9851006 p = find_task_by_pid_ns(pid, &init_pid_ns);
9861007 else
9871008 p = find_task_by_vpid(pid);
9881009 if (p)
989
- domain = tomoyo_real_domain(p);
1010
+ domain = tomoyo_task(p)->domain_info;
9901011 rcu_read_unlock();
9911012 } else if (!strncmp(data, "domain=", 7)) {
9921013 if (tomoyo_domain_def(data + 7))
....@@ -1004,7 +1025,7 @@
10041025 if (domain)
10051026 head->r.domain = &domain->list;
10061027 else
1007
- head->r.eof = 1;
1028
+ head->r.eof = true;
10081029 tomoyo_io_printf(head, "# select %s\n", data);
10091030 if (domain && domain->is_deleted)
10101031 tomoyo_io_printf(head, "# This is a deleted domain.\n");
....@@ -1020,10 +1041,11 @@
10201041 * Returns true if @a == @b, false otherwise.
10211042 */
10221043 static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1023
- const struct tomoyo_acl_info *b)
1044
+ const struct tomoyo_acl_info *b)
10241045 {
10251046 const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
10261047 const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1048
+
10271049 return p1->domainname == p2->domainname;
10281050 }
10291051
....@@ -1039,11 +1061,13 @@
10391061 static int tomoyo_write_task(struct tomoyo_acl_param *param)
10401062 {
10411063 int error = -EINVAL;
1064
+
10421065 if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
10431066 struct tomoyo_task_acl e = {
10441067 .head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
10451068 .domainname = tomoyo_get_domainname(param),
10461069 };
1070
+
10471071 if (e.domainname)
10481072 error = tomoyo_update_domain(&e.head, sizeof(e), param,
10491073 tomoyo_same_task_acl,
....@@ -1072,7 +1096,8 @@
10721096 if (mutex_lock_interruptible(&tomoyo_policy_lock))
10731097 return -EINTR;
10741098 /* Is there an active domain? */
1075
- list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1099
+ list_for_each_entry_rcu(domain, &tomoyo_domain_list, list,
1100
+ srcu_read_lock_held(&tomoyo_ss)) {
10761101 /* Never delete tomoyo_kernel_domain */
10771102 if (domain == &tomoyo_kernel_domain)
10781103 continue;
....@@ -1110,7 +1135,7 @@
11101135 };
11111136 static const struct {
11121137 const char *keyword;
1113
- int (*write) (struct tomoyo_acl_param *);
1138
+ int (*write)(struct tomoyo_acl_param *param);
11141139 } tomoyo_callback[5] = {
11151140 { "file ", tomoyo_write_file },
11161141 { "network inet ", tomoyo_write_inet_network },
....@@ -1151,9 +1176,11 @@
11511176 struct tomoyo_domain_info *domain = head->w.domain;
11521177 const bool is_delete = head->w.is_delete;
11531178 bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
1154
- unsigned int profile;
1179
+ unsigned int idx;
1180
+
11551181 if (*data == '<') {
11561182 int ret = 0;
1183
+
11571184 domain = NULL;
11581185 if (is_delete)
11591186 ret = tomoyo_delete_domain(data);
....@@ -1167,23 +1194,27 @@
11671194 if (!domain)
11681195 return -EINVAL;
11691196 ns = domain->ns;
1170
- if (sscanf(data, "use_profile %u", &profile) == 1
1171
- && profile < TOMOYO_MAX_PROFILES) {
1172
- if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
1173
- domain->profile = (u8) profile;
1197
+ if (sscanf(data, "use_profile %u", &idx) == 1
1198
+ && idx < TOMOYO_MAX_PROFILES) {
1199
+ if (!tomoyo_policy_loaded || ns->profile_ptr[idx])
1200
+ if (!is_delete)
1201
+ domain->profile = (u8) idx;
11741202 return 0;
11751203 }
1176
- if (sscanf(data, "use_group %u\n", &profile) == 1
1177
- && profile < TOMOYO_MAX_ACL_GROUPS) {
1204
+ if (sscanf(data, "use_group %u\n", &idx) == 1
1205
+ && idx < TOMOYO_MAX_ACL_GROUPS) {
11781206 if (!is_delete)
1179
- domain->group = (u8) profile;
1207
+ set_bit(idx, domain->group);
1208
+ else
1209
+ clear_bit(idx, domain->group);
11801210 return 0;
11811211 }
1182
- for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
1183
- const char *cp = tomoyo_dif[profile];
1212
+ for (idx = 0; idx < TOMOYO_MAX_DOMAIN_INFO_FLAGS; idx++) {
1213
+ const char *cp = tomoyo_dif[idx];
1214
+
11841215 if (strncmp(data, cp, strlen(cp) - 1))
11851216 continue;
1186
- domain->flags[profile] = !is_delete;
1217
+ domain->flags[idx] = !is_delete;
11871218 return 0;
11881219 }
11891220 return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
....@@ -1209,7 +1240,7 @@
12091240 tomoyo_set_space(head);
12101241 tomoyo_set_string(head, cond->transit->name);
12111242 }
1212
- /* fall through */
1243
+ fallthrough;
12131244 case 1:
12141245 {
12151246 const u16 condc = cond->condc;
....@@ -1225,9 +1256,11 @@
12251256 const struct tomoyo_envp *envp =
12261257 (typeof(envp)) (argv + cond->argc);
12271258 u16 skip;
1259
+
12281260 for (skip = 0; skip < head->r.cond_index; skip++) {
12291261 const u8 left = condp->left;
12301262 const u8 right = condp->right;
1263
+
12311264 condp++;
12321265 switch (left) {
12331266 case TOMOYO_ARGV_ENTRY:
....@@ -1253,6 +1286,7 @@
12531286 const u8 match = condp->equals;
12541287 const u8 left = condp->left;
12551288 const u8 right = condp->right;
1289
+
12561290 if (!tomoyo_flush(head))
12571291 return false;
12581292 condp++;
....@@ -1262,8 +1296,7 @@
12621296 case TOMOYO_ARGV_ENTRY:
12631297 tomoyo_io_printf(head,
12641298 "exec.argv[%lu]%s=\"",
1265
- argv->index, argv->
1266
- is_not ? "!" : "");
1299
+ argv->index, argv->is_not ? "!" : "");
12671300 tomoyo_set_string(head,
12681301 argv->value->name);
12691302 tomoyo_set_string(head, "\"");
....@@ -1274,12 +1307,10 @@
12741307 "exec.envp[\"");
12751308 tomoyo_set_string(head,
12761309 envp->name->name);
1277
- tomoyo_io_printf(head, "\"]%s=", envp->
1278
- is_not ? "!" : "");
1310
+ tomoyo_io_printf(head, "\"]%s=", envp->is_not ? "!" : "");
12791311 if (envp->value) {
12801312 tomoyo_set_string(head, "\"");
1281
- tomoyo_set_string(head, envp->
1282
- value->name);
1313
+ tomoyo_set_string(head, envp->value->name);
12831314 tomoyo_set_string(head, "\"");
12841315 } else {
12851316 tomoyo_set_string(head,
....@@ -1314,12 +1345,12 @@
13141345 }
13151346 }
13161347 head->r.cond_step++;
1317
- /* fall through */
1348
+ fallthrough;
13181349 case 2:
13191350 if (!tomoyo_flush(head))
13201351 break;
13211352 head->r.cond_step++;
1322
- /* fall through */
1353
+ fallthrough;
13231354 case 3:
13241355 if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
13251356 tomoyo_io_printf(head, " grant_log=%s",
....@@ -1375,6 +1406,7 @@
13751406 struct tomoyo_path_acl *ptr =
13761407 container_of(acl, typeof(*ptr), head);
13771408 const u16 perm = ptr->perm;
1409
+
13781410 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
13791411 if (!(perm & (1 << bit)))
13801412 continue;
....@@ -1395,6 +1427,7 @@
13951427 } else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
13961428 struct tomoyo_task_acl *ptr =
13971429 container_of(acl, typeof(*ptr), head);
1430
+
13981431 tomoyo_set_group(head, "task ");
13991432 tomoyo_set_string(head, "manual_domain_transition ");
14001433 tomoyo_set_string(head, ptr->domainname->name);
....@@ -1404,6 +1437,7 @@
14041437 struct tomoyo_path2_acl *ptr =
14051438 container_of(acl, typeof(*ptr), head);
14061439 const u8 perm = ptr->perm;
1440
+
14071441 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
14081442 if (!(perm & (1 << bit)))
14091443 continue;
....@@ -1424,6 +1458,7 @@
14241458 struct tomoyo_path_number_acl *ptr =
14251459 container_of(acl, typeof(*ptr), head);
14261460 const u8 perm = ptr->perm;
1461
+
14271462 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
14281463 if (!(perm & (1 << bit)))
14291464 continue;
....@@ -1444,6 +1479,7 @@
14441479 struct tomoyo_mkdev_acl *ptr =
14451480 container_of(acl, typeof(*ptr), head);
14461481 const u8 perm = ptr->perm;
1482
+
14471483 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
14481484 if (!(perm & (1 << bit)))
14491485 continue;
....@@ -1490,6 +1526,7 @@
14901526 ->name);
14911527 } else {
14921528 char buf[128];
1529
+
14931530 tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
14941531 tomoyo_io_printf(head, "%s", buf);
14951532 }
....@@ -1519,6 +1556,7 @@
15191556 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
15201557 struct tomoyo_mount_acl *ptr =
15211558 container_of(acl, typeof(*ptr), head);
1559
+
15221560 tomoyo_set_group(head, "file mount");
15231561 tomoyo_print_name_union(head, &ptr->dev_name);
15241562 tomoyo_print_name_union(head, &ptr->dir_name);
....@@ -1562,6 +1600,7 @@
15621600 list_for_each_cookie(head->r.acl, list) {
15631601 struct tomoyo_acl_info *ptr =
15641602 list_entry(head->r.acl, typeof(*ptr), list);
1603
+
15651604 if (!tomoyo_print_entry(head, ptr))
15661605 return false;
15671606 }
....@@ -1583,8 +1622,9 @@
15831622 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
15841623 struct tomoyo_domain_info *domain =
15851624 list_entry(head->r.domain, typeof(*domain), list);
1625
+ u8 i;
1626
+
15861627 switch (head->r.step) {
1587
- u8 i;
15881628 case 0:
15891629 if (domain->is_deleted &&
15901630 !head->r.print_this_domain_only)
....@@ -1594,22 +1634,33 @@
15941634 tomoyo_set_lf(head);
15951635 tomoyo_io_printf(head, "use_profile %u\n",
15961636 domain->profile);
1597
- tomoyo_io_printf(head, "use_group %u\n",
1598
- domain->group);
15991637 for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
16001638 if (domain->flags[i])
16011639 tomoyo_set_string(head, tomoyo_dif[i]);
1640
+ head->r.index = 0;
1641
+ head->r.step++;
1642
+ fallthrough;
1643
+ case 1:
1644
+ while (head->r.index < TOMOYO_MAX_ACL_GROUPS) {
1645
+ i = head->r.index++;
1646
+ if (!test_bit(i, domain->group))
1647
+ continue;
1648
+ tomoyo_io_printf(head, "use_group %u\n", i);
1649
+ if (!tomoyo_flush(head))
1650
+ return;
1651
+ }
1652
+ head->r.index = 0;
16021653 head->r.step++;
16031654 tomoyo_set_lf(head);
1604
- /* fall through */
1605
- case 1:
1655
+ fallthrough;
1656
+ case 2:
16061657 if (!tomoyo_read_domain2(head, &domain->acl_info_list))
16071658 return;
16081659 head->r.step++;
16091660 if (!tomoyo_set_lf(head))
16101661 return;
1611
- /* fall through */
1612
- case 2:
1662
+ fallthrough;
1663
+ case 3:
16131664 head->r.step = 0;
16141665 if (head->r.print_this_domain_only)
16151666 goto done;
....@@ -1660,14 +1711,15 @@
16601711 head->r.eof = true;
16611712 if (tomoyo_str_starts(&buf, "global-pid "))
16621713 global_pid = true;
1663
- pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1714
+ if (kstrtouint(buf, 10, &pid))
1715
+ return;
16641716 rcu_read_lock();
16651717 if (global_pid)
16661718 p = find_task_by_pid_ns(pid, &init_pid_ns);
16671719 else
16681720 p = find_task_by_vpid(pid);
16691721 if (p)
1670
- domain = tomoyo_real_domain(p);
1722
+ domain = tomoyo_task(p)->domain_info;
16711723 rcu_read_unlock();
16721724 if (!domain)
16731725 return;
....@@ -1710,6 +1762,7 @@
17101762 .data = head->write_buf,
17111763 };
17121764 u8 i;
1765
+
17131766 if (tomoyo_str_starts(&param.data, "aggregator "))
17141767 return tomoyo_write_aggregator(&param);
17151768 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
....@@ -1721,6 +1774,7 @@
17211774 if (tomoyo_str_starts(&param.data, "acl_group ")) {
17221775 unsigned int group;
17231776 char *data;
1777
+
17241778 group = simple_strtoul(param.data, &data, 10);
17251779 if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
17261780 return tomoyo_write_domain2
....@@ -1745,12 +1799,15 @@
17451799 struct tomoyo_policy_namespace *ns =
17461800 container_of(head->r.ns, typeof(*ns), namespace_list);
17471801 struct list_head *list = &ns->group_list[idx];
1802
+
17481803 list_for_each_cookie(head->r.group, list) {
17491804 struct tomoyo_group *group =
17501805 list_entry(head->r.group, typeof(*group), head.list);
1806
+
17511807 list_for_each_cookie(head->r.acl, &group->member_list) {
17521808 struct tomoyo_acl_head *ptr =
17531809 list_entry(head->r.acl, typeof(*ptr), list);
1810
+
17541811 if (ptr->is_deleted)
17551812 continue;
17561813 if (!tomoyo_flush(head))
....@@ -1770,10 +1827,10 @@
17701827 head)->number);
17711828 } else if (idx == TOMOYO_ADDRESS_GROUP) {
17721829 char buffer[128];
1773
-
17741830 struct tomoyo_address_group *member =
17751831 container_of(ptr, typeof(*member),
17761832 head);
1833
+
17771834 tomoyo_print_ip(buffer, sizeof(buffer),
17781835 &member->address);
17791836 tomoyo_io_printf(head, " %s", buffer);
....@@ -1801,6 +1858,7 @@
18011858 struct tomoyo_policy_namespace *ns =
18021859 container_of(head->r.ns, typeof(*ns), namespace_list);
18031860 struct list_head *list = &ns->policy_list[idx];
1861
+
18041862 list_for_each_cookie(head->r.acl, list) {
18051863 struct tomoyo_acl_head *acl =
18061864 container_of(head->r.acl, typeof(*acl), list);
....@@ -1813,6 +1871,7 @@
18131871 {
18141872 struct tomoyo_transition_control *ptr =
18151873 container_of(acl, typeof(*ptr), head);
1874
+
18161875 tomoyo_print_namespace(head);
18171876 tomoyo_set_string(head, tomoyo_transition_type
18181877 [ptr->type]);
....@@ -1828,6 +1887,7 @@
18281887 {
18291888 struct tomoyo_aggregator *ptr =
18301889 container_of(acl, typeof(*ptr), head);
1890
+
18311891 tomoyo_print_namespace(head);
18321892 tomoyo_set_string(head, "aggregator ");
18331893 tomoyo_set_string(head,
....@@ -1857,6 +1917,7 @@
18571917 {
18581918 struct tomoyo_policy_namespace *ns =
18591919 container_of(head->r.ns, typeof(*ns), namespace_list);
1920
+
18601921 if (head->r.eof)
18611922 return;
18621923 while (head->r.step < TOMOYO_MAX_POLICY &&
....@@ -1920,6 +1981,7 @@
19201981 static int tomoyo_truncate(char *str)
19211982 {
19221983 char *start = str;
1984
+
19231985 while (*(unsigned char *) str > (unsigned char) ' ')
19241986 str++;
19251987 *str = '\0';
....@@ -1942,6 +2004,7 @@
19422004 char *symlink = NULL;
19432005 char *cp = strchr(header, '\n');
19442006 int len;
2007
+
19452008 if (!cp)
19462009 return;
19472010 cp = strchr(cp + 1, '\n');
....@@ -2001,6 +2064,7 @@
20012064 static unsigned int tomoyo_serial;
20022065 struct tomoyo_query entry = { };
20032066 bool quota_exceeded = false;
2067
+
20042068 va_start(args, fmt);
20052069 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
20062070 va_end(args);
....@@ -2024,7 +2088,7 @@
20242088 /* Check max_learning_entry parameter. */
20252089 if (tomoyo_domain_quota_is_ok(r))
20262090 break;
2027
- /* fall through */
2091
+ fallthrough;
20282092 default:
20292093 return 0;
20302094 }
....@@ -2062,8 +2126,7 @@
20622126 (tomoyo_answer_wait, entry.answer ||
20632127 !atomic_read(&tomoyo_query_observers), HZ))
20642128 break;
2065
- else
2066
- entry.timer++;
2129
+ entry.timer++;
20672130 }
20682131 spin_lock(&tomoyo_query_list_lock);
20692132 list_del(&entry.list);
....@@ -2099,6 +2162,7 @@
20992162 {
21002163 struct tomoyo_query *ptr;
21012164 struct tomoyo_domain_info *domain = NULL;
2165
+
21022166 spin_lock(&tomoyo_query_list_lock);
21032167 list_for_each_entry(ptr, &tomoyo_query_list, list) {
21042168 if (ptr->serial != serial)
....@@ -2141,15 +2205,15 @@
21412205 unsigned int pos = 0;
21422206 size_t len = 0;
21432207 char *buf;
2208
+
21442209 if (head->r.w_pos)
21452210 return;
2146
- if (head->read_buf) {
2147
- kfree(head->read_buf);
2148
- head->read_buf = NULL;
2149
- }
2211
+ kfree(head->read_buf);
2212
+ head->read_buf = NULL;
21502213 spin_lock(&tomoyo_query_list_lock);
21512214 list_for_each(tmp, &tomoyo_query_list) {
21522215 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2216
+
21532217 if (pos++ != head->r.query_index)
21542218 continue;
21552219 len = ptr->query_len;
....@@ -2167,6 +2231,7 @@
21672231 spin_lock(&tomoyo_query_list_lock);
21682232 list_for_each(tmp, &tomoyo_query_list) {
21692233 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2234
+
21702235 if (pos++ != head->r.query_index)
21712236 continue;
21722237 /*
....@@ -2201,9 +2266,11 @@
22012266 struct list_head *tmp;
22022267 unsigned int serial;
22032268 unsigned int answer;
2269
+
22042270 spin_lock(&tomoyo_query_list_lock);
22052271 list_for_each(tmp, &tomoyo_query_list) {
22062272 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2273
+
22072274 ptr->timer = 0;
22082275 }
22092276 spin_unlock(&tomoyo_query_list_lock);
....@@ -2212,6 +2279,7 @@
22122279 spin_lock(&tomoyo_query_list_lock);
22132280 list_for_each(tmp, &tomoyo_query_list) {
22142281 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2282
+
22152283 if (ptr->serial != serial)
22162284 continue;
22172285 ptr->answer = answer;
....@@ -2234,7 +2302,7 @@
22342302 static void tomoyo_read_version(struct tomoyo_io_buffer *head)
22352303 {
22362304 if (!head->r.eof) {
2237
- tomoyo_io_printf(head, "2.5.0");
2305
+ tomoyo_io_printf(head, "2.6.0");
22382306 head->r.eof = true;
22392307 }
22402308 }
....@@ -2283,6 +2351,7 @@
22832351 {
22842352 u8 i;
22852353 unsigned int total = 0;
2354
+
22862355 if (head->r.eof)
22872356 return;
22882357 for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
....@@ -2291,9 +2360,9 @@
22912360 atomic_read(&tomoyo_stat_updated[i]));
22922361 if (tomoyo_stat_modified[i]) {
22932362 struct tomoyo_time stamp;
2363
+
22942364 tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2295
- tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2296
- "%02u:%02u:%02u)",
2365
+ tomoyo_io_printf(head, " (Last: %04u/%02u/%02u %02u:%02u:%02u)",
22972366 stamp.year, stamp.month, stamp.day,
22982367 stamp.hour, stamp.min, stamp.sec);
22992368 }
....@@ -2301,6 +2370,7 @@
23012370 }
23022371 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
23032372 unsigned int used = tomoyo_memory_used[i];
2373
+
23042374 total += used;
23052375 tomoyo_io_printf(head, "Memory used by %-22s %10u",
23062376 tomoyo_memory_headers[i], used);
....@@ -2325,6 +2395,7 @@
23252395 {
23262396 char *data = head->write_buf;
23272397 u8 i;
2398
+
23282399 if (tomoyo_str_starts(&data, "Memory used by "))
23292400 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
23302401 if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
....@@ -2453,6 +2524,7 @@
24532524 __poll_t tomoyo_poll_control(struct file *file, poll_table *wait)
24542525 {
24552526 struct tomoyo_io_buffer *head = file->private_data;
2527
+
24562528 if (head->poll)
24572529 return head->poll(file, wait) | EPOLLOUT | EPOLLWRNORM;
24582530 return EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM;
....@@ -2468,6 +2540,7 @@
24682540 static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
24692541 {
24702542 struct list_head *ns;
2543
+
24712544 if (head->type != TOMOYO_EXCEPTIONPOLICY &&
24722545 head->type != TOMOYO_PROFILE)
24732546 return;
....@@ -2513,7 +2586,7 @@
25132586 int idx;
25142587
25152588 if (!head->read)
2516
- return -ENOSYS;
2589
+ return -EINVAL;
25172590 if (mutex_lock_interruptible(&head->io_sem))
25182591 return -EINTR;
25192592 head->read_user_buf = buffer;
....@@ -2553,6 +2626,7 @@
25532626 head->type == TOMOYO_PROFILE) {
25542627 if (*line == '<') {
25552628 char *cp = strchr(line, ' ');
2629
+
25562630 if (cp) {
25572631 *cp++ = '\0';
25582632 head->w.ns = tomoyo_assign_namespace(line);
....@@ -2585,10 +2659,9 @@
25852659 size_t avail_len = buffer_len;
25862660 char *cp0 = head->write_buf;
25872661 int idx;
2662
+
25882663 if (!head->write)
2589
- return -ENOSYS;
2590
- if (!access_ok(VERIFY_READ, buffer, buffer_len))
2591
- return -EFAULT;
2664
+ return -EINVAL;
25922665 if (mutex_lock_interruptible(&head->io_sem))
25932666 return -EINTR;
25942667 head->read_user_buf_avail = 0;
....@@ -2596,9 +2669,11 @@
25962669 /* Read a line and dispatch it to the policy handler. */
25972670 while (avail_len > 0) {
25982671 char c;
2672
+
25992673 if (head->w.avail >= head->writebuf_size - 1) {
26002674 const int len = head->writebuf_size * 2;
26012675 char *cp = kzalloc(len, GFP_NOFS);
2676
+
26022677 if (!cp) {
26032678 error = -ENOMEM;
26042679 break;
....@@ -2635,13 +2710,13 @@
26352710 case TOMOYO_DOMAINPOLICY:
26362711 if (tomoyo_select_domain(head, cp0))
26372712 continue;
2638
- /* fall through */
2713
+ fallthrough;
26392714 case TOMOYO_EXCEPTIONPOLICY:
26402715 if (!strcmp(cp0, "select transition_only")) {
26412716 head->r.print_transition_related_only = true;
26422717 continue;
26432718 }
2644
- /* fall through */
2719
+ fallthrough;
26452720 default:
26462721 if (!tomoyo_manager()) {
26472722 error = -EPERM;
....@@ -2697,30 +2772,33 @@
26972772 {
26982773 struct tomoyo_domain_info *domain;
26992774 const int idx = tomoyo_read_lock();
2775
+
27002776 tomoyo_policy_loaded = true;
2701
- printk(KERN_INFO "TOMOYO: 2.5.0\n");
2702
- list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2777
+ pr_info("TOMOYO: 2.6.0\n");
2778
+ list_for_each_entry_rcu(domain, &tomoyo_domain_list, list,
2779
+ srcu_read_lock_held(&tomoyo_ss)) {
27032780 const u8 profile = domain->profile;
2704
- const struct tomoyo_policy_namespace *ns = domain->ns;
2705
- if (ns->profile_version != 20110903)
2706
- printk(KERN_ERR
2707
- "Profile version %u is not supported.\n",
2781
+ struct tomoyo_policy_namespace *ns = domain->ns;
2782
+
2783
+ if (ns->profile_version == 20110903) {
2784
+ pr_info_once("Converting profile version from %u to %u.\n",
2785
+ 20110903, 20150505);
2786
+ ns->profile_version = 20150505;
2787
+ }
2788
+ if (ns->profile_version != 20150505)
2789
+ pr_err("Profile version %u is not supported.\n",
27082790 ns->profile_version);
27092791 else if (!ns->profile_ptr[profile])
2710
- printk(KERN_ERR
2711
- "Profile %u (used by '%s') is not defined.\n",
2792
+ pr_err("Profile %u (used by '%s') is not defined.\n",
27122793 profile, domain->domainname->name);
27132794 else
27142795 continue;
2715
- printk(KERN_ERR
2716
- "Userland tools for TOMOYO 2.5 must be installed and "
2717
- "policy must be initialized.\n");
2718
- printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ "
2719
- "for more information.\n");
2796
+ pr_err("Userland tools for TOMOYO 2.6 must be installed and policy must be initialized.\n");
2797
+ pr_err("Please see https://tomoyo.osdn.jp/2.6/ for more information.\n");
27202798 panic("STOP!");
27212799 }
27222800 tomoyo_read_unlock(idx);
2723
- printk(KERN_INFO "Mandatory Access Control activated.\n");
2801
+ pr_info("Mandatory Access Control activated.\n");
27242802 }
27252803
27262804 /**
....@@ -2730,6 +2808,16 @@
27302808 */
27312809 void __init tomoyo_load_builtin_policy(void)
27322810 {
2811
+#ifdef CONFIG_SECURITY_TOMOYO_INSECURE_BUILTIN_SETTING
2812
+ static char tomoyo_builtin_profile[] __initdata =
2813
+ "PROFILE_VERSION=20150505\n"
2814
+ "0-CONFIG={ mode=learning grant_log=no reject_log=yes }\n";
2815
+ static char tomoyo_builtin_exception_policy[] __initdata =
2816
+ "aggregator proc:/self/exe /proc/self/exe\n";
2817
+ static char tomoyo_builtin_domain_policy[] __initdata = "";
2818
+ static char tomoyo_builtin_manager[] __initdata = "";
2819
+ static char tomoyo_builtin_stat[] __initdata = "";
2820
+#else
27332821 /*
27342822 * This include file is manually created and contains built-in policy
27352823 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
....@@ -2737,11 +2825,14 @@
27372825 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
27382826 */
27392827 #include "builtin-policy.h"
2828
+#endif
27402829 u8 i;
27412830 const int idx = tomoyo_read_lock();
2831
+
27422832 for (i = 0; i < 5; i++) {
27432833 struct tomoyo_io_buffer head = { };
27442834 char *start = "";
2835
+
27452836 switch (i) {
27462837 case 0:
27472838 start = tomoyo_builtin_profile;
....@@ -2771,6 +2862,7 @@
27712862 }
27722863 while (1) {
27732864 char *end = strchr(start, '\n');
2865
+
27742866 if (!end)
27752867 break;
27762868 *end = '\0';