X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbus-proxyd%2Fbus-policy.c;h=3cfe9befb2ffc70876264bcafec8403591b762d2;hp=2234e7af3a6619a5f103af1484f07ddefa6fe0aa;hb=78f9b196ab9671ceb625cd2abf90629ed201c24f;hpb=2e2b36084a98f9071fd178c6540ce57b2f577f8d diff --git a/src/bus-proxyd/bus-policy.c b/src/bus-proxyd/bus-policy.c index 2234e7af3..3cfe9befb 100644 --- a/src/bus-proxyd/bus-policy.c +++ b/src/bus-proxyd/bus-policy.c @@ -24,6 +24,7 @@ #include "strv.h" #include "conf-files.h" #include "bus-internal.h" +#include "bus-message.h" #include "bus-policy.h" static void policy_item_free(PolicyItem *i) { @@ -39,6 +40,14 @@ static void policy_item_free(PolicyItem *i) { DEFINE_TRIVIAL_CLEANUP_FUNC(PolicyItem*, policy_item_free); +static void item_append(PolicyItem *i, PolicyItem **list) { + + PolicyItem *tail; + + LIST_FIND_TAIL(items, *list, tail); + LIST_INSERT_AFTER(items, *list, tail, i); +} + static int file_load(Policy *p, const char *path) { _cleanup_free_ char *c = NULL, *policy_user = NULL, *policy_group = NULL; @@ -155,7 +164,10 @@ static int file_load(Policy *p, const char *path) { else if (streq(name, "group")) state = STATE_POLICY_GROUP; else { - log_warning("Attribute %s of tag unknown at %s:%u, ignoring.", name, path, line); + if (streq(name, "at_console")) + log_debug("Attribute %s of tag unsupported at %s:%u, ignoring.", name, path, line); + else + log_warning("Attribute %s of tag unknown at %s:%u, ignoring.", name, path, line); state = STATE_POLICY_OTHER_ATTRIBUTE; } } else if (t == XML_TAG_CLOSE_EMPTY || @@ -268,7 +280,12 @@ static int file_load(Policy *p, const char *path) { ic = POLICY_ITEM_USER; else if (streq(name, "group")) ic = POLICY_ITEM_GROUP; - else { + else if (streq(name, "eavesdrop")) { + log_debug("Unsupported attribute %s= at %s:%u, ignoring.", name, path, line); + i->class = POLICY_ITEM_IGNORE; + state = STATE_ALLOW_DENY_OTHER_ATTRIBUTE; + break; + } else { log_error("Unknown attribute %s= at %s:%u, ignoring.", name, path, line); state = STATE_ALLOW_DENY_OTHER_ATTRIBUTE; break; @@ -303,7 +320,10 @@ static int file_load(Policy *p, const char *path) { (streq(u, "sender") && ic == POLICY_ITEM_RECV)) state = STATE_ALLOW_DENY_NAME; else { - log_error("Unknown attribute %s= at %s:%u, ignoring.", name, path, line); + if (streq(u, "requested_reply")) + log_debug("Unsupported attribute %s= at %s:%u, ignoring.", name, path, line); + else + log_error("Unknown attribute %s= at %s:%u, ignoring.", name, path, line); state = STATE_ALLOW_DENY_OTHER_ATTRIBUTE; break; } @@ -319,42 +339,71 @@ static int file_load(Policy *p, const char *path) { } if (policy_category == POLICY_CATEGORY_DEFAULT) - LIST_PREPEND(items, p->default_items, i); + item_append(i, &p->default_items); else if (policy_category == POLICY_CATEGORY_MANDATORY) - LIST_PREPEND(items, p->default_items, i); + item_append(i, &p->mandatory_items); else if (policy_category == POLICY_CATEGORY_USER) { - PolicyItem *first; + const char *u = policy_user; assert_cc(sizeof(uid_t) == sizeof(uint32_t)); - r = hashmap_ensure_allocated(&p->user_items, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&p->user_items, NULL); if (r < 0) return log_oom(); - first = hashmap_get(p->user_items, UINT32_TO_PTR(i->uid)); - LIST_PREPEND(items, first, i); + if (!u) { + log_error("User policy without name"); + return -EINVAL; + } - r = hashmap_replace(p->user_items, UINT32_TO_PTR(i->uid), first); + r = get_user_creds(&u, &i->uid, NULL, NULL, NULL); if (r < 0) { - LIST_REMOVE(items, first, i); - return log_oom(); + log_error("Failed to resolve user %s, ignoring policy: %s", u, strerror(-r)); + free(i); + } else { + PolicyItem *first; + + first = hashmap_get(p->user_items, UINT32_TO_PTR(i->uid)); + item_append(i, &first); + i->uid_valid = true; + + r = hashmap_replace(p->user_items, UINT32_TO_PTR(i->uid), first); + if (r < 0) { + LIST_REMOVE(items, first, i); + return log_oom(); + } } + } else if (policy_category == POLICY_CATEGORY_GROUP) { - PolicyItem *first; + const char *g = policy_group; assert_cc(sizeof(gid_t) == sizeof(uint32_t)); - r = hashmap_ensure_allocated(&p->group_items, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&p->group_items, NULL); if (r < 0) return log_oom(); - first = hashmap_get(p->group_items, UINT32_TO_PTR(i->gid)); - LIST_PREPEND(items, first, i); + if (!g) { + log_error("Group policy without name"); + return -EINVAL; + } - r = hashmap_replace(p->group_items, UINT32_TO_PTR(i->gid), first); + r = get_group_creds(&g, &i->gid); if (r < 0) { - LIST_REMOVE(items, first, i); - return log_oom(); + log_error("Failed to resolve group %s, ignoring policy: %s", g, strerror(-r)); + free(i); + } else { + PolicyItem *first; + + first = hashmap_get(p->group_items, UINT32_TO_PTR(i->gid)); + item_append(i, &first); + i->gid_valid = true; + + r = hashmap_replace(p->group_items, UINT32_TO_PTR(i->gid), first); + if (r < 0) { + LIST_REMOVE(items, first, i); + return log_oom(); + } } } @@ -477,8 +526,36 @@ static int file_load(Policy *p, const char *path) { return -EINVAL; } + switch (i->class) { + case POLICY_ITEM_USER: + if (!streq(name, "*")) { + const char *u = name; + + r = get_user_creds(&u, &i->uid, NULL, NULL, NULL); + if (r < 0) + log_error("Failed to resolve user %s: %s", name, strerror(-r)); + else + i->uid_valid = true; + } + break; + case POLICY_ITEM_GROUP: + if (!streq(name, "*")) { + const char *g = name; + + r = get_group_creds(&g, &i->gid); + if (r < 0) + log_error("Failed to resolve group %s: %s", name, strerror(-r)); + else + i->gid_valid = true; + } + break; + default: + break; + } + i->name = name; name = NULL; + state = STATE_ALLOW_DENY; } else { log_error("Unexpected token (14) in %s:%u.", path, line); @@ -515,6 +592,294 @@ static int file_load(Policy *p, const char *path) { } } +enum { + DENY, + ALLOW, + DUNNO, +}; + +static const char *verdict_to_string(int v) { + switch (v) { + + case DENY: + return "DENY"; + case ALLOW: + return "ALLOW"; + case DUNNO: + return "DUNNO"; + } + + return NULL; +} + +struct policy_check_filter { + PolicyItemClass class; + uid_t uid; + gid_t gid; + int message_type; + const char *name; + const char *interface; + const char *path; + const char *member; +}; + +static int is_permissive(PolicyItem *i) { + + assert(i); + + return (i->type == POLICY_ITEM_ALLOW) ? ALLOW : DENY; +} + +static int check_policy_item(PolicyItem *i, const struct policy_check_filter *filter) { + + assert(i); + assert(filter); + + switch (i->class) { + case POLICY_ITEM_SEND: + case POLICY_ITEM_RECV: + + if (i->name && !streq_ptr(i->name, filter->name)) + break; + + if ((i->message_type != 0) && (i->message_type != filter->message_type)) + break; + + if (i->path && !streq_ptr(i->path, filter->path)) + break; + + if (i->member && !streq_ptr(i->member, filter->member)) + break; + + if (i->interface && !streq_ptr(i->interface, filter->interface)) + break; + + return is_permissive(i); + + case POLICY_ITEM_OWN: + assert(filter->name); + + if (streq(i->name, "*") || streq(i->name, filter->name)) + return is_permissive(i); + break; + + case POLICY_ITEM_OWN_PREFIX: + assert(filter->name); + + if (streq(i->name, "*") || service_name_startswith(filter->name, i->name)) + return is_permissive(i); + break; + + case POLICY_ITEM_USER: + if (filter->uid != (uid_t) -1) + if ((streq_ptr(i->name, "*") || (i->uid_valid && i->uid == filter->uid))) + return is_permissive(i); + break; + + case POLICY_ITEM_GROUP: + if (filter->gid != (gid_t) -1) + if ((streq_ptr(i->name, "*") || (i->gid_valid && i->gid == filter->gid))) + return is_permissive(i); + break; + + case POLICY_ITEM_IGNORE: + default: + break; + } + + return DUNNO; +} + +static int check_policy_items(PolicyItem *items, const struct policy_check_filter *filter) { + + PolicyItem *i; + int verdict = DUNNO; + + assert(filter); + + /* Check all policies in a set - a broader one might be followed by a more specific one, + * and the order of rules in policy definitions matters */ + LIST_FOREACH(items, i, items) { + int v; + + if (i->class != filter->class && + !(i->class == POLICY_ITEM_OWN_PREFIX && filter->class == POLICY_ITEM_OWN)) + continue; + + v = check_policy_item(i, filter); + if (v != DUNNO) + verdict = v; + } + + return verdict; +} + +static int policy_check(Policy *p, const struct policy_check_filter *filter) { + + PolicyItem *items; + int verdict, v; + + assert(p); + assert(filter); + + assert(IN_SET(filter->class, POLICY_ITEM_SEND, POLICY_ITEM_RECV, POLICY_ITEM_OWN, POLICY_ITEM_USER, POLICY_ITEM_GROUP)); + + /* + * The policy check is implemented by the following logic: + * + * 1. Check default items + * 2. Check group items + * 3. Check user items + * 4. Check mandatory items + * + * Later rules override earlier rules. + */ + + verdict = check_policy_items(p->default_items, filter); + + if (filter->gid != (gid_t) -1) { + items = hashmap_get(p->group_items, UINT32_TO_PTR(filter->gid)); + if (items) { + v = check_policy_items(items, filter); + if (v != DUNNO) + verdict = v; + } + } + + if (filter->uid != (uid_t) -1) { + items = hashmap_get(p->user_items, UINT32_TO_PTR(filter->uid)); + if (items) { + v = check_policy_items(items, filter); + if (v != DUNNO) + verdict = v; + } + } + + v = check_policy_items(p->mandatory_items, filter); + if (v != DUNNO) + verdict = v; + + return verdict; +} + +bool policy_check_own(Policy *p, uid_t uid, gid_t gid, const char *name) { + + struct policy_check_filter filter = { + .class = POLICY_ITEM_OWN, + .uid = uid, + .gid = gid, + .name = name, + }; + + int verdict; + + assert(p); + assert(name); + + verdict = policy_check(p, &filter); + + log_full(LOG_AUTH | (verdict != ALLOW ? LOG_WARNING : LOG_DEBUG), + "Ownership permission check for uid=" UID_FMT " gid=" GID_FMT" name=%s: %s", + uid, gid, strna(name), strna(verdict_to_string(verdict))); + + return verdict == ALLOW; +} + +bool policy_check_hello(Policy *p, uid_t uid, gid_t gid) { + + struct policy_check_filter filter = { + .uid = uid, + .gid = gid, + }; + int verdict; + + assert(p); + + filter.class = POLICY_ITEM_USER; + verdict = policy_check(p, &filter); + + if (verdict != DENY) { + int v; + + filter.class = POLICY_ITEM_GROUP; + v = policy_check(p, &filter); + if (v != DUNNO) + verdict = v; + } + + log_full(LOG_AUTH | (verdict != ALLOW ? LOG_WARNING : LOG_DEBUG), + "Hello permission check for uid=" UID_FMT " gid=" GID_FMT": %s", + uid, gid, strna(verdict_to_string(verdict))); + + return verdict == ALLOW; +} + +bool policy_check_recv(Policy *p, + uid_t uid, + gid_t gid, + int message_type, + const char *name, + const char *path, + const char *interface, + const char *member) { + + struct policy_check_filter filter = { + .class = POLICY_ITEM_RECV, + .uid = uid, + .gid = gid, + .message_type = message_type, + .name = name, + .interface = interface, + .path = path, + .member = member, + }; + + int verdict; + + assert(p); + + verdict = policy_check(p, &filter); + + log_full(LOG_AUTH | (verdict != ALLOW ? LOG_WARNING : LOG_DEBUG), + "Recieve permission check for uid=" UID_FMT " gid=" GID_FMT" message=%s name=%s interface=%s path=%s member=%s: %s", + uid, gid, bus_message_type_to_string(message_type), strna(name), strna(path), strna(interface), strna(member), strna(verdict_to_string(verdict))); + + return verdict == ALLOW; +} + +bool policy_check_send(Policy *p, + uid_t uid, + gid_t gid, + int message_type, + const char *name, + const char *path, + const char *interface, + const char *member) { + + struct policy_check_filter filter = { + .class = POLICY_ITEM_SEND, + .uid = uid, + .gid = gid, + .message_type = message_type, + .name = name, + .interface = interface, + .path = path, + .member = member, + }; + + int verdict; + + assert(p); + + verdict = policy_check(p, &filter); + + log_full(LOG_AUTH | (verdict != ALLOW ? LOG_WARNING : LOG_DEBUG), + "Send permission check for uid=" UID_FMT " gid=" GID_FMT" message=%s name=%s interface=%s path=%s member=%s: %s", + uid, gid, bus_message_type_to_string(message_type), strna(name), strna(path), strna(interface), strna(member), strna(verdict_to_string(verdict))); + + return verdict == ALLOW; +} + int policy_load(Policy *p, char **files) { char **i; int r; @@ -582,61 +947,65 @@ void policy_free(Policy *p) { p->user_items = p->group_items = NULL; } -static void dump_items(PolicyItem *i) { +static void dump_items(PolicyItem *items, const char *prefix) { + + PolicyItem *i; - if (!i) + if (!items) return; - printf("Type: %s\n" - "Class: %s\n", - policy_item_type_to_string(i->type), - policy_item_class_to_string(i->class)); + if (!prefix) + prefix = ""; - if (i->interface) - printf("Interface: %s\n", - i->interface); + LIST_FOREACH(items, i, items) { - if (i->member) - printf("Member: %s\n", - i->member); + printf("%sType: %s\n" + "%sClass: %s\n", + prefix, policy_item_type_to_string(i->type), + prefix, policy_item_class_to_string(i->class)); - if (i->error) - printf("Error: %s\n", - i->error); + if (i->interface) + printf("%sInterface: %s\n", + prefix, i->interface); - if (i->path) - printf("Path: %s\n", - i->path); + if (i->member) + printf("%sMember: %s\n", + prefix, i->member); - if (i->name) - printf("Name: %s\n", - i->name); + if (i->error) + printf("%sError: %s\n", + prefix, i->error); - if (i->message_type != 0) - printf("Message Type: %s\n", - bus_message_type_to_string(i->message_type)); + if (i->path) + printf("%sPath: %s\n", + prefix, i->path); - if (i->uid_valid) { - _cleanup_free_ char *user; + if (i->name) + printf("%sName: %s\n", + prefix, i->name); - user = uid_to_name(i->uid); + if (i->message_type != 0) + printf("%sMessage Type: %s\n", + prefix, bus_message_type_to_string(i->message_type)); - printf("User: %s\n", - strna(user)); - } + if (i->uid_valid) { + _cleanup_free_ char *user; - if (i->gid_valid) { - _cleanup_free_ char *group; + user = uid_to_name(i->uid); - group = gid_to_name(i->gid); + printf("%sUser: %s (%d)\n", + prefix, strna(user), i->uid); + } - printf("Group: %s\n", - strna(group)); - } + if (i->gid_valid) { + _cleanup_free_ char *group; - if (i->items_next) { - printf("--\n"); - dump_items(i->items_next); + group = gid_to_name(i->gid); + + printf("%sGroup: %s (%d)\n", + prefix, strna(group), i->gid); + } + printf("%s-\n", prefix); } } @@ -646,25 +1015,24 @@ static void dump_hashmap_items(Hashmap *h) { void *k; HASHMAP_FOREACH_KEY(i, k, h, j) { - printf("Item for %u:\n", PTR_TO_UINT(k)); - dump_items(i); + printf("\t%s Item for %u:\n", draw_special_char(DRAW_ARROW), PTR_TO_UINT(k)); + dump_items(i, "\t\t"); } } -noreturn void policy_dump(Policy *p) { +void policy_dump(Policy *p) { printf("%s Default Items:\n", draw_special_char(DRAW_ARROW)); - dump_items(p->default_items); - - printf("%s Mandatory Items:\n", draw_special_char(DRAW_ARROW)); - dump_items(p->mandatory_items); + dump_items(p->default_items, "\t"); printf("%s Group Items:\n", draw_special_char(DRAW_ARROW)); dump_hashmap_items(p->group_items); printf("%s User Items:\n", draw_special_char(DRAW_ARROW)); dump_hashmap_items(p->user_items); - exit(0); + + printf("%s Mandatory Items:\n", draw_special_char(DRAW_ARROW)); + dump_items(p->mandatory_items, "\t"); } static const char* const policy_item_type_table[_POLICY_ITEM_TYPE_MAX] = { @@ -682,5 +1050,6 @@ static const char* const policy_item_class_table[_POLICY_ITEM_CLASS_MAX] = { [POLICY_ITEM_OWN_PREFIX] = "own-prefix", [POLICY_ITEM_USER] = "user", [POLICY_ITEM_GROUP] = "group", + [POLICY_ITEM_IGNORE] = "ignore", }; DEFINE_STRING_TABLE_LOOKUP(policy_item_class, PolicyItemClass);