1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "conf-files.h"
26 #include "bus-internal.h"
27 #include "bus-message.h"
28 #include "bus-policy.h"
30 static void policy_item_free(PolicyItem *i) {
41 DEFINE_TRIVIAL_CLEANUP_FUNC(PolicyItem*, policy_item_free);
43 static void item_append(PolicyItem *i, PolicyItem **list) {
47 LIST_FIND_TAIL(items, *list, tail);
48 LIST_INSERT_AFTER(items, *list, tail, i);
51 static int file_load(Policy *p, const char *path) {
53 _cleanup_free_ char *c = NULL, *policy_user = NULL, *policy_group = NULL;
54 _cleanup_(policy_item_freep) PolicyItem *i = NULL;
55 void *xml_state = NULL;
67 STATE_POLICY_OTHER_ATTRIBUTE,
69 STATE_ALLOW_DENY_INTERFACE,
70 STATE_ALLOW_DENY_MEMBER,
71 STATE_ALLOW_DENY_ERROR,
72 STATE_ALLOW_DENY_PATH,
73 STATE_ALLOW_DENY_MESSAGE_TYPE,
74 STATE_ALLOW_DENY_NAME,
75 STATE_ALLOW_DENY_OTHER_ATTRIBUTE,
77 } state = STATE_OUTSIDE;
81 POLICY_CATEGORY_DEFAULT,
82 POLICY_CATEGORY_MANDATORY,
85 } policy_category = POLICY_CATEGORY_NONE;
91 r = read_full_file(path, &c, NULL);
98 log_error("Failed to load %s: %s", path, strerror(-r));
104 _cleanup_free_ char *name = NULL;
107 t = xml_tokenize(&q, &name, &xml_state, &line);
109 log_error("XML parse failure in %s: %s", path, strerror(-t));
117 if (t == XML_TAG_OPEN) {
118 if (streq(name, "busconfig"))
119 state = STATE_BUSCONFIG;
121 log_error("Unexpected tag %s at %s:%u.", name, path, line);
125 } else if (t == XML_END)
127 else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
128 log_error("Unexpected token (1) at %s:%u.", path, line);
134 case STATE_BUSCONFIG:
136 if (t == XML_TAG_OPEN) {
137 if (streq(name, "policy")) {
138 state = STATE_POLICY;
139 policy_category = POLICY_CATEGORY_NONE;
142 policy_user = policy_group = NULL;
147 } else if (t == XML_TAG_CLOSE_EMPTY ||
148 (t == XML_TAG_CLOSE && streq(name, "busconfig")))
149 state = STATE_OUTSIDE;
150 else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
151 log_error("Unexpected token (2) at %s:%u.", path, line);
159 if (t == XML_ATTRIBUTE_NAME) {
160 if (streq(name, "context"))
161 state = STATE_POLICY_CONTEXT;
162 else if (streq(name, "user"))
163 state = STATE_POLICY_USER;
164 else if (streq(name, "group"))
165 state = STATE_POLICY_GROUP;
167 if (streq(name, "at_console"))
168 log_debug("Attribute %s of <policy> tag unsupported at %s:%u, ignoring.", name, path, line);
170 log_warning("Attribute %s of <policy> tag unknown at %s:%u, ignoring.", name, path, line);
171 state = STATE_POLICY_OTHER_ATTRIBUTE;
173 } else if (t == XML_TAG_CLOSE_EMPTY ||
174 (t == XML_TAG_CLOSE && streq(name, "policy")))
175 state = STATE_BUSCONFIG;
176 else if (t == XML_TAG_OPEN) {
179 if (streq(name, "allow"))
180 it = POLICY_ITEM_ALLOW;
181 else if (streq(name, "deny"))
182 it = POLICY_ITEM_DENY;
184 log_warning("Unknown tag %s in <policy> %s:%u.", name, path, line);
189 i = new0(PolicyItem, 1);
194 state = STATE_ALLOW_DENY;
196 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
197 log_error("Unexpected token (3) at %s:%u.", path, line);
203 case STATE_POLICY_CONTEXT:
205 if (t == XML_ATTRIBUTE_VALUE) {
206 if (streq(name, "default")) {
207 policy_category = POLICY_CATEGORY_DEFAULT;
208 state = STATE_POLICY;
209 } else if (streq(name, "mandatory")) {
210 policy_category = POLICY_CATEGORY_MANDATORY;
211 state = STATE_POLICY;
213 log_error("context= parameter %s unknown for <policy> at %s:%u.", name, path, line);
217 log_error("Unexpected token (4) at %s:%u.", path, line);
223 case STATE_POLICY_USER:
225 if (t == XML_ATTRIBUTE_VALUE) {
229 policy_category = POLICY_CATEGORY_USER;
230 state = STATE_POLICY;
232 log_error("Unexpected token (5) in %s:%u.", path, line);
238 case STATE_POLICY_GROUP:
240 if (t == XML_ATTRIBUTE_VALUE) {
244 policy_category = POLICY_CATEGORY_GROUP;
245 state = STATE_POLICY;
247 log_error("Unexpected token (6) at %s:%u.", path, line);
253 case STATE_POLICY_OTHER_ATTRIBUTE:
255 if (t == XML_ATTRIBUTE_VALUE)
256 state = STATE_POLICY;
258 log_error("Unexpected token (7) in %s:%u.", path, line);
264 case STATE_ALLOW_DENY:
268 if (t == XML_ATTRIBUTE_NAME) {
271 if (startswith(name, "send_"))
272 ic = POLICY_ITEM_SEND;
273 else if (startswith(name, "receive_"))
274 ic = POLICY_ITEM_RECV;
275 else if (streq(name, "own"))
276 ic = POLICY_ITEM_OWN;
277 else if (streq(name, "own_prefix"))
278 ic = POLICY_ITEM_OWN_PREFIX;
279 else if (streq(name, "user"))
280 ic = POLICY_ITEM_USER;
281 else if (streq(name, "group"))
282 ic = POLICY_ITEM_GROUP;
283 else if (streq(name, "eavesdrop")) {
284 log_debug("Unsupported attribute %s= at %s:%u, ignoring.", name, path, line);
285 i->class = POLICY_ITEM_IGNORE;
286 state = STATE_ALLOW_DENY_OTHER_ATTRIBUTE;
289 log_error("Unknown attribute %s= at %s:%u, ignoring.", name, path, line);
290 state = STATE_ALLOW_DENY_OTHER_ATTRIBUTE;
294 if (i->class != _POLICY_ITEM_CLASS_UNSET && ic != i->class) {
295 log_error("send_ and receive_ fields mixed on same tag at %s:%u.", path, line);
301 if (ic == POLICY_ITEM_SEND || ic == POLICY_ITEM_RECV) {
304 u = strchr(name, '_');
309 if (streq(u, "interface"))
310 state = STATE_ALLOW_DENY_INTERFACE;
311 else if (streq(u, "member"))
312 state = STATE_ALLOW_DENY_MEMBER;
313 else if (streq(u, "error"))
314 state = STATE_ALLOW_DENY_ERROR;
315 else if (streq(u, "path"))
316 state = STATE_ALLOW_DENY_PATH;
317 else if (streq(u, "type"))
318 state = STATE_ALLOW_DENY_MESSAGE_TYPE;
319 else if ((streq(u, "destination") && ic == POLICY_ITEM_SEND) ||
320 (streq(u, "sender") && ic == POLICY_ITEM_RECV))
321 state = STATE_ALLOW_DENY_NAME;
323 if (streq(u, "requested_reply"))
324 log_debug("Unsupported attribute %s= at %s:%u, ignoring.", name, path, line);
326 log_error("Unknown attribute %s= at %s:%u, ignoring.", name, path, line);
327 state = STATE_ALLOW_DENY_OTHER_ATTRIBUTE;
331 state = STATE_ALLOW_DENY_NAME;
333 } else if (t == XML_TAG_CLOSE_EMPTY ||
334 (t == XML_TAG_CLOSE && streq(name, i->type == POLICY_ITEM_ALLOW ? "allow" : "deny"))) {
336 if (i->class == _POLICY_ITEM_CLASS_UNSET) {
337 log_error("Policy not set at %s:%u.", path, line);
341 if (policy_category == POLICY_CATEGORY_DEFAULT)
342 item_append(i, &p->default_items);
343 else if (policy_category == POLICY_CATEGORY_MANDATORY)
344 item_append(i, &p->mandatory_items);
345 else if (policy_category == POLICY_CATEGORY_USER) {
346 const char *u = policy_user;
348 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
350 r = hashmap_ensure_allocated(&p->user_items, NULL);
355 log_error("User policy without name");
359 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
361 log_error("Failed to resolve user %s, ignoring policy: %s", u, strerror(-r));
366 first = hashmap_get(p->user_items, UINT32_TO_PTR(i->uid));
367 item_append(i, &first);
370 r = hashmap_replace(p->user_items, UINT32_TO_PTR(i->uid), first);
372 LIST_REMOVE(items, first, i);
377 } else if (policy_category == POLICY_CATEGORY_GROUP) {
378 const char *g = policy_group;
380 assert_cc(sizeof(gid_t) == sizeof(uint32_t));
382 r = hashmap_ensure_allocated(&p->group_items, NULL);
387 log_error("Group policy without name");
391 r = get_group_creds(&g, &i->gid);
393 log_error("Failed to resolve group %s, ignoring policy: %s", g, strerror(-r));
398 first = hashmap_get(p->group_items, UINT32_TO_PTR(i->gid));
399 item_append(i, &first);
402 r = hashmap_replace(p->group_items, UINT32_TO_PTR(i->gid), first);
404 LIST_REMOVE(items, first, i);
410 state = STATE_POLICY;
413 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) {
414 log_error("Unexpected token (8) at %s:%u.", path, line);
420 case STATE_ALLOW_DENY_INTERFACE:
422 if (t == XML_ATTRIBUTE_VALUE) {
425 log_error("Duplicate interface at %s:%u.", path, line);
431 state = STATE_ALLOW_DENY;
433 log_error("Unexpected token (9) at %s:%u.", path, line);
439 case STATE_ALLOW_DENY_MEMBER:
441 if (t == XML_ATTRIBUTE_VALUE) {
444 log_error("Duplicate member in %s:%u.", path, line);
450 state = STATE_ALLOW_DENY;
452 log_error("Unexpected token (10) in %s:%u.", path, line);
458 case STATE_ALLOW_DENY_ERROR:
460 if (t == XML_ATTRIBUTE_VALUE) {
463 log_error("Duplicate error in %s:%u.", path, line);
469 state = STATE_ALLOW_DENY;
471 log_error("Unexpected token (11) in %s:%u.", path, line);
477 case STATE_ALLOW_DENY_PATH:
479 if (t == XML_ATTRIBUTE_VALUE) {
482 log_error("Duplicate path in %s:%u.", path, line);
488 state = STATE_ALLOW_DENY;
490 log_error("Unexpected token (12) in %s:%u.", path, line);
496 case STATE_ALLOW_DENY_MESSAGE_TYPE:
498 if (t == XML_ATTRIBUTE_VALUE) {
501 if (i->message_type != 0) {
502 log_error("Duplicate message type in %s:%u.", path, line);
506 r = bus_message_type_from_string(name, &i->message_type);
508 log_error("Invalid message type in %s:%u.", path, line);
512 state = STATE_ALLOW_DENY;
514 log_error("Unexpected token (13) in %s:%u.", path, line);
520 case STATE_ALLOW_DENY_NAME:
522 if (t == XML_ATTRIBUTE_VALUE) {
525 log_error("Duplicate name in %s:%u.", path, line);
530 case POLICY_ITEM_USER:
531 if (!streq(name, "*")) {
532 const char *u = name;
534 r = get_user_creds(&u, &i->uid, NULL, NULL, NULL);
536 log_error("Failed to resolve user %s: %s", name, strerror(-r));
541 case POLICY_ITEM_GROUP:
542 if (!streq(name, "*")) {
543 const char *g = name;
545 r = get_group_creds(&g, &i->gid);
547 log_error("Failed to resolve group %s: %s", name, strerror(-r));
559 state = STATE_ALLOW_DENY;
561 log_error("Unexpected token (14) in %s:%u.", path, line);
567 case STATE_ALLOW_DENY_OTHER_ATTRIBUTE:
569 if (t == XML_ATTRIBUTE_VALUE)
570 state = STATE_ALLOW_DENY;
572 log_error("Unexpected token (15) in %s:%u.", path, line);
580 if (t == XML_TAG_OPEN)
582 else if (t == XML_TAG_CLOSE || t == XML_TAG_CLOSE_EMPTY) {
585 state = STATE_BUSCONFIG;
601 struct policy_check_filter {
602 PolicyItemClass class;
603 const struct ucred *ucred;
606 const char *interface;
611 static int is_permissive(PolicyItem *i) {
615 return (i->type == POLICY_ITEM_ALLOW) ? ALLOW : DENY;
618 static int check_policy_item(PolicyItem *i, const struct policy_check_filter *filter) {
624 case POLICY_ITEM_SEND:
625 case POLICY_ITEM_RECV:
627 if (i->name && !streq_ptr(i->name, filter->name))
630 if ((i->message_type != _POLICY_ITEM_CLASS_UNSET) && (i->message_type != filter->message_type))
633 if (i->path && !streq_ptr(i->path, filter->path))
636 if (i->member && !streq_ptr(i->member, filter->member))
639 if (i->interface && !streq_ptr(i->interface, filter->interface))
642 return is_permissive(i);
644 case POLICY_ITEM_OWN:
645 assert(filter->name);
647 if (streq(i->name, "*") || streq(i->name, filter->name))
648 return is_permissive(i);
651 case POLICY_ITEM_OWN_PREFIX:
652 assert(filter->name);
654 if (streq(i->name, "*") || service_name_startswith(filter->name, i->name))
655 return is_permissive(i);
658 case POLICY_ITEM_USER:
659 assert(filter->ucred);
661 if ((streq_ptr(i->name, "*") || (i->uid_valid && i->uid == filter->ucred->uid)))
662 return is_permissive(i);
665 case POLICY_ITEM_GROUP:
666 assert(filter->ucred);
668 if ((streq_ptr(i->name, "*") || (i->gid_valid && i->gid == filter->ucred->gid)))
669 return is_permissive(i);
672 case POLICY_ITEM_IGNORE:
680 static int check_policy_items(PolicyItem *items, const struct policy_check_filter *filter) {
687 /* Check all policies in a set - a broader one might be followed by a more specific one,
688 * and the order of rules in policy definitions matters */
689 LIST_FOREACH(items, i, items) {
690 if (i->class != filter->class &&
691 IN_SET(i->class, POLICY_ITEM_OWN, POLICY_ITEM_OWN_PREFIX) != IN_SET(filter->class, POLICY_ITEM_OWN, POLICY_ITEM_OWN_PREFIX))
694 r = check_policy_item(i, filter);
702 static int policy_check(Policy *p, const struct policy_check_filter *filter) {
711 * The policy check is implemented by the following logic:
713 * 1. Check mandatory items. If the message matches any of these, it is decisive.
714 * 2. See if the passed ucred match against the user/group hashmaps. A matching entry is also decisive.
715 * 3. Consult the defaults if non of the above matched with a more specific rule.
716 * 4. If the message isn't caught be the defaults either, reject it.
719 r = check_policy_items(p->mandatory_items, filter);
724 items = hashmap_get(p->user_items, UINT32_TO_PTR(filter->ucred->uid));
726 r = check_policy_items(items, filter);
731 items = hashmap_get(p->group_items, UINT32_TO_PTR(filter->ucred->gid));
733 r = check_policy_items(items, filter);
739 return check_policy_items(p->default_items, filter);
742 bool policy_check_own(Policy *p, const struct ucred *ucred, const char *name) {
744 struct policy_check_filter filter = {
745 .class = POLICY_ITEM_OWN,
750 return policy_check(p, &filter) == ALLOW;
753 bool policy_check_hello(Policy *p, const struct ucred *ucred) {
755 struct policy_check_filter filter = {
760 filter.class = POLICY_ITEM_USER;
761 user = policy_check(p, &filter);
765 filter.class = POLICY_ITEM_GROUP;
766 group = policy_check(p, &filter);
770 return !(user == DUNNO && group == DUNNO);
773 bool policy_check_recv(Policy *p,
774 const struct ucred *ucred,
778 const char *interface,
779 const char *member) {
781 struct policy_check_filter filter = {
782 .class = POLICY_ITEM_RECV,
784 .message_type = message_type,
786 .interface = interface,
791 return policy_check(p, &filter) == ALLOW;
794 bool policy_check_send(Policy *p,
795 const struct ucred *ucred,
799 const char *interface,
800 const char *member) {
802 struct policy_check_filter filter = {
803 .class = POLICY_ITEM_SEND,
805 .message_type = message_type,
807 .interface = interface,
812 return policy_check(p, &filter) == ALLOW;
815 int policy_load(Policy *p, char **files) {
821 STRV_FOREACH(i, files) {
823 r = file_load(p, *i);
825 _cleanup_strv_free_ char **l = NULL;
828 r = conf_files_list(&l, ".conf", NULL, *i, NULL);
830 log_error("Failed to get configuration file list: %s", strerror(-r));
838 /* We ignore all errors but EISDIR, and just proceed. */
844 void policy_free(Policy *p) {
845 PolicyItem *i, *first;
850 while ((i = p->default_items)) {
851 LIST_REMOVE(items, p->default_items, i);
855 while ((i = p->mandatory_items)) {
856 LIST_REMOVE(items, p->mandatory_items, i);
860 while ((first = hashmap_steal_first(p->user_items))) {
862 while ((i = first)) {
863 LIST_REMOVE(items, first, i);
868 while ((first = hashmap_steal_first(p->group_items))) {
870 while ((i = first)) {
871 LIST_REMOVE(items, first, i);
876 hashmap_free(p->user_items);
877 hashmap_free(p->group_items);
879 p->user_items = p->group_items = NULL;
882 static void dump_items(PolicyItem *items, const char *prefix) {
892 LIST_FOREACH(items, i, items) {
894 printf("%sType: %s\n"
896 prefix, policy_item_type_to_string(i->type),
897 prefix, policy_item_class_to_string(i->class));
900 printf("%sInterface: %s\n",
901 prefix, i->interface);
904 printf("%sMember: %s\n",
908 printf("%sError: %s\n",
912 printf("%sPath: %s\n",
916 printf("%sName: %s\n",
919 if (i->message_type != 0)
920 printf("%sMessage Type: %s\n",
921 prefix, bus_message_type_to_string(i->message_type));
924 _cleanup_free_ char *user;
926 user = uid_to_name(i->uid);
928 printf("%sUser: %s (%d)\n",
929 prefix, strna(user), i->uid);
933 _cleanup_free_ char *group;
935 group = gid_to_name(i->gid);
937 printf("%sGroup: %s (%d)\n",
938 prefix, strna(group), i->gid);
943 static void dump_hashmap_items(Hashmap *h) {
948 HASHMAP_FOREACH_KEY(i, k, h, j) {
949 printf("\t%s Item for %u:\n", draw_special_char(DRAW_ARROW), PTR_TO_UINT(k));
950 dump_items(i, "\t\t");
954 void policy_dump(Policy *p) {
956 printf("%s Default Items:\n", draw_special_char(DRAW_ARROW));
957 dump_items(p->default_items, "\t");
959 printf("%s Group Items:\n", draw_special_char(DRAW_ARROW));
960 dump_hashmap_items(p->group_items);
962 printf("%s User Items:\n", draw_special_char(DRAW_ARROW));
963 dump_hashmap_items(p->user_items);
965 printf("%s Mandatory Items:\n", draw_special_char(DRAW_ARROW));
966 dump_items(p->mandatory_items, "\t");
969 static const char* const policy_item_type_table[_POLICY_ITEM_TYPE_MAX] = {
970 [_POLICY_ITEM_TYPE_UNSET] = "unset",
971 [POLICY_ITEM_ALLOW] = "allow",
972 [POLICY_ITEM_DENY] = "deny",
974 DEFINE_STRING_TABLE_LOOKUP(policy_item_type, PolicyItemType);
976 static const char* const policy_item_class_table[_POLICY_ITEM_CLASS_MAX] = {
977 [_POLICY_ITEM_CLASS_UNSET] = "unset",
978 [POLICY_ITEM_SEND] = "send",
979 [POLICY_ITEM_RECV] = "recv",
980 [POLICY_ITEM_OWN] = "own",
981 [POLICY_ITEM_OWN_PREFIX] = "own-prefix",
982 [POLICY_ITEM_USER] = "user",
983 [POLICY_ITEM_GROUP] = "group",
984 [POLICY_ITEM_IGNORE] = "ignore",
986 DEFINE_STRING_TABLE_LOOKUP(policy_item_class, PolicyItemClass);