1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* policy.c Bus security policy
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-hash.h>
30 #include <dbus/dbus-internals.h>
33 bus_policy_rule_new (BusPolicyRuleType type,
38 rule = dbus_new0 (BusPolicyRule, 1);
48 case BUS_POLICY_RULE_USER:
49 rule->d.user.uid = DBUS_UID_UNSET;
51 case BUS_POLICY_RULE_GROUP:
52 rule->d.group.gid = DBUS_GID_UNSET;
54 case BUS_POLICY_RULE_SEND:
55 rule->d.send.message_type = DBUS_MESSAGE_TYPE_INVALID;
57 case BUS_POLICY_RULE_RECEIVE:
58 rule->d.receive.message_type = DBUS_MESSAGE_TYPE_INVALID;
59 /* allow rules default to TRUE (only requested replies allowed)
60 * deny rules default to FALSE (only unrequested replies denied)
62 rule->d.receive.requested_reply = rule->allow;
64 case BUS_POLICY_RULE_OWN:
72 bus_policy_rule_ref (BusPolicyRule *rule)
74 _dbus_assert (rule->refcount > 0);
80 bus_policy_rule_unref (BusPolicyRule *rule)
82 _dbus_assert (rule->refcount > 0);
86 if (rule->refcount == 0)
90 case BUS_POLICY_RULE_SEND:
91 dbus_free (rule->d.send.path);
92 dbus_free (rule->d.send.interface);
93 dbus_free (rule->d.send.member);
94 dbus_free (rule->d.send.error);
95 dbus_free (rule->d.send.destination);
97 case BUS_POLICY_RULE_RECEIVE:
98 dbus_free (rule->d.receive.path);
99 dbus_free (rule->d.receive.interface);
100 dbus_free (rule->d.receive.member);
101 dbus_free (rule->d.receive.error);
102 dbus_free (rule->d.receive.origin);
104 case BUS_POLICY_RULE_OWN:
105 dbus_free (rule->d.own.service_name);
107 case BUS_POLICY_RULE_USER:
109 case BUS_POLICY_RULE_GROUP:
121 DBusList *default_rules; /**< Default policy rules */
122 DBusList *mandatory_rules; /**< Mandatory policy rules */
123 DBusHashTable *rules_by_uid; /**< per-UID policy rules */
124 DBusHashTable *rules_by_gid; /**< per-GID policy rules */
128 free_rule_func (void *data,
131 BusPolicyRule *rule = data;
133 bus_policy_rule_unref (rule);
137 free_rule_list_func (void *data)
139 DBusList **list = data;
141 if (list == NULL) /* DBusHashTable is on crack */
144 _dbus_list_foreach (list, free_rule_func, NULL);
146 _dbus_list_clear (list);
152 bus_policy_new (void)
156 policy = dbus_new0 (BusPolicy, 1);
160 policy->refcount = 1;
162 policy->rules_by_uid = _dbus_hash_table_new (DBUS_HASH_ULONG,
164 free_rule_list_func);
165 if (policy->rules_by_uid == NULL)
168 policy->rules_by_gid = _dbus_hash_table_new (DBUS_HASH_ULONG,
170 free_rule_list_func);
171 if (policy->rules_by_gid == NULL)
177 bus_policy_unref (policy);
182 bus_policy_ref (BusPolicy *policy)
184 _dbus_assert (policy->refcount > 0);
186 policy->refcount += 1;
190 bus_policy_unref (BusPolicy *policy)
192 _dbus_assert (policy->refcount > 0);
194 policy->refcount -= 1;
196 if (policy->refcount == 0)
198 _dbus_list_foreach (&policy->default_rules, free_rule_func, NULL);
199 _dbus_list_clear (&policy->default_rules);
201 _dbus_list_foreach (&policy->mandatory_rules, free_rule_func, NULL);
202 _dbus_list_clear (&policy->mandatory_rules);
204 if (policy->rules_by_uid)
206 _dbus_hash_table_unref (policy->rules_by_uid);
207 policy->rules_by_uid = NULL;
210 if (policy->rules_by_gid)
212 _dbus_hash_table_unref (policy->rules_by_gid);
213 policy->rules_by_gid = NULL;
221 add_list_to_client (DBusList **list,
222 BusClientPolicy *client)
226 link = _dbus_list_get_first_link (list);
229 BusPolicyRule *rule = link->data;
230 link = _dbus_list_get_next_link (list, link);
234 case BUS_POLICY_RULE_USER:
235 case BUS_POLICY_RULE_GROUP:
236 /* These aren't per-connection policies */
239 case BUS_POLICY_RULE_OWN:
240 case BUS_POLICY_RULE_SEND:
241 case BUS_POLICY_RULE_RECEIVE:
242 /* These are per-connection */
243 if (!bus_client_policy_append_rule (client, rule))
253 bus_policy_create_client_policy (BusPolicy *policy,
254 DBusConnection *connection,
257 BusClientPolicy *client;
260 _dbus_assert (dbus_connection_get_is_authenticated (connection));
261 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
263 client = bus_client_policy_new ();
267 if (!add_list_to_client (&policy->default_rules,
271 /* we avoid the overhead of looking up user's groups
272 * if we don't have any group rules anyway
274 if (_dbus_hash_table_get_n_entries (policy->rules_by_gid) > 0)
276 unsigned long *groups;
280 if (!bus_connection_get_groups (connection, &groups, &n_groups, error))
288 list = _dbus_hash_table_lookup_ulong (policy->rules_by_gid,
293 if (!add_list_to_client (list, client))
306 if (!dbus_connection_get_unix_user (connection, &uid))
308 dbus_set_error (error, DBUS_ERROR_FAILED,
309 "No user ID known for connection, cannot determine security policy\n");
313 if (_dbus_hash_table_get_n_entries (policy->rules_by_uid) > 0)
317 list = _dbus_hash_table_lookup_ulong (policy->rules_by_uid,
322 if (!add_list_to_client (list, client))
327 if (!add_list_to_client (&policy->mandatory_rules,
331 bus_client_policy_optimize (client);
338 _DBUS_ASSERT_ERROR_IS_SET (error);
340 bus_client_policy_unref (client);
345 list_allows_user (dbus_bool_t def,
348 const unsigned long *group_ids,
356 link = _dbus_list_get_first_link (list);
359 BusPolicyRule *rule = link->data;
360 link = _dbus_list_get_next_link (list, link);
362 if (rule->type == BUS_POLICY_RULE_USER)
364 _dbus_verbose ("List %p user rule uid="DBUS_UID_FORMAT"\n",
365 list, rule->d.user.uid);
367 if (rule->d.user.uid == DBUS_UID_UNSET)
369 else if (rule->d.user.uid != uid)
372 else if (rule->type == BUS_POLICY_RULE_GROUP)
374 _dbus_verbose ("List %p group rule uid="DBUS_UID_FORMAT"\n",
375 list, rule->d.user.uid);
377 if (rule->d.group.gid == DBUS_GID_UNSET)
384 while (i < n_group_ids)
386 if (rule->d.group.gid == group_ids[i])
391 if (i == n_group_ids)
398 allowed = rule->allow;
405 bus_policy_allow_user (BusPolicy *policy,
406 DBusUserDatabase *user_database,
410 unsigned long *group_ids;
413 /* On OOM or error we always reject the user */
414 if (!_dbus_user_database_get_groups (user_database,
415 uid, &group_ids, &n_group_ids, NULL))
417 _dbus_verbose ("Did not get any groups for UID %lu\n",
424 allowed = list_allows_user (allowed,
425 &policy->default_rules,
427 group_ids, n_group_ids);
429 allowed = list_allows_user (allowed,
430 &policy->mandatory_rules,
432 group_ids, n_group_ids);
434 dbus_free (group_ids);
436 _dbus_verbose ("UID %lu allowed = %d\n", uid, allowed);
442 bus_policy_append_default_rule (BusPolicy *policy,
445 if (!_dbus_list_append (&policy->default_rules, rule))
448 bus_policy_rule_ref (rule);
454 bus_policy_append_mandatory_rule (BusPolicy *policy,
457 if (!_dbus_list_append (&policy->mandatory_rules, rule))
460 bus_policy_rule_ref (rule);
466 get_list (DBusHashTable *hash,
471 list = _dbus_hash_table_lookup_ulong (hash, key);
475 list = dbus_new0 (DBusList*, 1);
479 if (!_dbus_hash_table_insert_ulong (hash, key, list))
490 bus_policy_append_user_rule (BusPolicy *policy,
496 list = get_list (policy->rules_by_uid, uid);
501 if (!_dbus_list_append (list, rule))
504 bus_policy_rule_ref (rule);
510 bus_policy_append_group_rule (BusPolicy *policy,
516 list = get_list (policy->rules_by_gid, gid);
521 if (!_dbus_list_append (list, rule))
524 bus_policy_rule_ref (rule);
530 append_copy_of_policy_list (DBusList **list,
531 DBusList **to_append)
538 /* Preallocate all our links */
539 link = _dbus_list_get_first_link (to_append);
542 if (!_dbus_list_append (&tmp_list, link->data))
544 _dbus_list_clear (&tmp_list);
548 link = _dbus_list_get_next_link (to_append, link);
551 /* Now append them */
552 while ((link = _dbus_list_pop_first_link (&tmp_list)))
554 bus_policy_rule_ref (link->data);
555 _dbus_list_append_link (list, link);
562 merge_id_hash (DBusHashTable *dest,
563 DBusHashTable *to_absorb)
567 _dbus_hash_iter_init (to_absorb, &iter);
568 while (_dbus_hash_iter_next (&iter))
570 unsigned long id = _dbus_hash_iter_get_ulong_key (&iter);
571 DBusList **list = _dbus_hash_iter_get_value (&iter);
572 DBusList **target = get_list (dest, id);
577 if (!append_copy_of_policy_list (target, list))
585 bus_policy_merge (BusPolicy *policy,
586 BusPolicy *to_absorb)
588 /* Not properly atomic, but as used for configuration files
589 * we don't rely on it.
591 if (!append_copy_of_policy_list (&policy->default_rules,
592 &to_absorb->default_rules))
595 if (!append_copy_of_policy_list (&policy->mandatory_rules,
596 &to_absorb->mandatory_rules))
599 if (!merge_id_hash (policy->rules_by_uid,
600 to_absorb->rules_by_uid))
603 if (!merge_id_hash (policy->rules_by_gid,
604 to_absorb->rules_by_gid))
610 struct BusClientPolicy
618 bus_client_policy_new (void)
620 BusClientPolicy *policy;
622 policy = dbus_new0 (BusClientPolicy, 1);
626 policy->refcount = 1;
632 bus_client_policy_ref (BusClientPolicy *policy)
634 _dbus_assert (policy->refcount > 0);
636 policy->refcount += 1;
640 rule_unref_foreach (void *data,
643 BusPolicyRule *rule = data;
645 bus_policy_rule_unref (rule);
649 bus_client_policy_unref (BusClientPolicy *policy)
651 _dbus_assert (policy->refcount > 0);
653 policy->refcount -= 1;
655 if (policy->refcount == 0)
657 _dbus_list_foreach (&policy->rules,
661 _dbus_list_clear (&policy->rules);
668 remove_rules_by_type_up_to (BusClientPolicy *policy,
669 BusPolicyRuleType type,
674 link = _dbus_list_get_first_link (&policy->rules);
675 while (link != up_to)
677 BusPolicyRule *rule = link->data;
678 DBusList *next = _dbus_list_get_next_link (&policy->rules, link);
680 if (rule->type == type)
682 _dbus_list_remove_link (&policy->rules, link);
683 bus_policy_rule_unref (rule);
691 bus_client_policy_optimize (BusClientPolicy *policy)
695 /* The idea here is that if we have:
697 * <allow send_interface="foo.bar"/>
698 * <deny send_interface="*"/>
700 * (for example) the deny will always override the allow. So we
701 * delete the allow. Ditto for deny followed by allow, etc. This is
702 * a dumb thing to put in a config file, but the <include> feature
703 * of files allows for an "inheritance and override" pattern where
704 * it could make sense. If an included file wants to "start over"
705 * with a blanket deny, no point keeping the rules from the parent
709 _dbus_verbose ("Optimizing policy with %d rules\n",
710 _dbus_list_get_length (&policy->rules));
712 link = _dbus_list_get_first_link (&policy->rules);
717 dbus_bool_t remove_preceding;
719 next = _dbus_list_get_next_link (&policy->rules, link);
722 remove_preceding = FALSE;
724 _dbus_assert (rule != NULL);
728 case BUS_POLICY_RULE_SEND:
730 rule->d.send.message_type == DBUS_MESSAGE_TYPE_INVALID &&
731 rule->d.send.path == NULL &&
732 rule->d.send.interface == NULL &&
733 rule->d.send.member == NULL &&
734 rule->d.send.error == NULL &&
735 rule->d.send.destination == NULL;
737 case BUS_POLICY_RULE_RECEIVE:
739 rule->d.receive.message_type == DBUS_MESSAGE_TYPE_INVALID &&
740 rule->d.receive.path == NULL &&
741 rule->d.receive.interface == NULL &&
742 rule->d.receive.member == NULL &&
743 rule->d.receive.error == NULL &&
744 rule->d.receive.origin == NULL;
746 case BUS_POLICY_RULE_OWN:
748 rule->d.own.service_name == NULL;
750 case BUS_POLICY_RULE_USER:
751 case BUS_POLICY_RULE_GROUP:
752 _dbus_assert_not_reached ("invalid rule");
756 if (remove_preceding)
757 remove_rules_by_type_up_to (policy, rule->type,
763 _dbus_verbose ("After optimization, policy has %d rules\n",
764 _dbus_list_get_length (&policy->rules));
768 bus_client_policy_append_rule (BusClientPolicy *policy,
771 _dbus_verbose ("Appending rule %p with type %d to policy %p\n",
772 rule, rule->type, policy);
774 if (!_dbus_list_append (&policy->rules, rule))
777 bus_policy_rule_ref (rule);
783 bus_client_policy_check_can_send (BusClientPolicy *policy,
784 BusRegistry *registry,
785 DBusConnection *receiver,
786 DBusMessage *message)
791 /* policy->rules is in the order the rules appeared
792 * in the config file, i.e. last rule that applies wins
795 _dbus_verbose (" (policy) checking send rules\n");
798 link = _dbus_list_get_first_link (&policy->rules);
801 BusPolicyRule *rule = link->data;
803 link = _dbus_list_get_next_link (&policy->rules, link);
805 /* Rule is skipped if it specifies a different
806 * message name from the message, or a different
807 * destination from the message
810 if (rule->type != BUS_POLICY_RULE_SEND)
812 _dbus_verbose (" (policy) skipping non-send rule\n");
816 if (rule->d.send.message_type != DBUS_MESSAGE_TYPE_INVALID)
818 if (dbus_message_get_type (message) != rule->d.send.message_type)
820 _dbus_verbose (" (policy) skipping rule for different message type\n");
825 if (rule->d.send.path != NULL)
827 if (dbus_message_get_path (message) != NULL &&
828 strcmp (dbus_message_get_path (message),
829 rule->d.send.path) != 0)
831 _dbus_verbose (" (policy) skipping rule for different path\n");
836 if (rule->d.send.interface != NULL)
838 if (dbus_message_get_interface (message) != NULL &&
839 strcmp (dbus_message_get_interface (message),
840 rule->d.send.interface) != 0)
842 _dbus_verbose (" (policy) skipping rule for different interface\n");
847 if (rule->d.send.member != NULL)
849 if (dbus_message_get_member (message) != NULL &&
850 strcmp (dbus_message_get_member (message),
851 rule->d.send.member) != 0)
853 _dbus_verbose (" (policy) skipping rule for different member\n");
858 if (rule->d.send.error != NULL)
860 if (dbus_message_get_error_name (message) != NULL &&
861 strcmp (dbus_message_get_error_name (message),
862 rule->d.send.error) != 0)
864 _dbus_verbose (" (policy) skipping rule for different error name\n");
869 if (rule->d.send.destination != NULL)
871 /* receiver can be NULL for messages that are sent to the
872 * message bus itself, we check the strings in that case as
873 * built-in services don't have a DBusConnection but messages
874 * to them have a destination service name.
876 if (receiver == NULL)
878 if (!dbus_message_has_destination (message,
879 rule->d.send.destination))
881 _dbus_verbose (" (policy) skipping rule because message dest is not %s\n",
882 rule->d.send.destination);
891 _dbus_string_init_const (&str, rule->d.send.destination);
893 service = bus_registry_lookup (registry, &str);
896 _dbus_verbose (" (policy) skipping rule because dest %s doesn't exist\n",
897 rule->d.send.destination);
901 if (!bus_service_has_owner (service, receiver))
903 _dbus_verbose (" (policy) skipping rule because dest %s isn't owned by receiver\n",
904 rule->d.send.destination);
911 allowed = rule->allow;
913 _dbus_verbose (" (policy) used rule, allow now = %d\n",
920 /* See docs on what the args mean on bus_context_check_security_policy()
924 bus_client_policy_check_can_receive (BusClientPolicy *policy,
925 BusRegistry *registry,
926 dbus_bool_t requested_reply,
927 DBusConnection *sender,
928 DBusConnection *addressed_recipient,
929 DBusConnection *proposed_recipient,
930 DBusMessage *message)
934 dbus_bool_t eavesdropping;
937 addressed_recipient != proposed_recipient &&
938 dbus_message_get_destination (message) != NULL;
940 /* policy->rules is in the order the rules appeared
941 * in the config file, i.e. last rule that applies wins
944 _dbus_verbose (" (policy) checking receive rules, eavesdropping = %d\n", eavesdropping);
947 link = _dbus_list_get_first_link (&policy->rules);
950 BusPolicyRule *rule = link->data;
952 link = _dbus_list_get_next_link (&policy->rules, link);
954 if (rule->type != BUS_POLICY_RULE_RECEIVE)
956 _dbus_verbose (" (policy) skipping non-receive rule\n");
960 if (rule->d.receive.message_type != DBUS_MESSAGE_TYPE_INVALID)
962 if (dbus_message_get_type (message) != rule->d.receive.message_type)
964 _dbus_verbose (" (policy) skipping rule for different message type\n");
969 /* for allow, eavesdrop=false means the rule doesn't apply when
970 * eavesdropping. eavesdrop=true means always allow.
972 if (eavesdropping && rule->allow && !rule->d.receive.eavesdrop)
974 _dbus_verbose (" (policy) skipping allow rule since it doesn't apply to eavesdropping\n");
978 /* for deny, eavesdrop=true means the rule applies only when
979 * eavesdropping; eavesdrop=false means always deny.
981 if (!eavesdropping && !rule->allow && rule->d.receive.eavesdrop)
983 _dbus_verbose (" (policy) skipping deny rule since it only applies to eavesdropping\n");
987 /* If it's a reply, the requested_reply flag kicks in */
988 if (dbus_message_get_reply_serial (message) != 0)
990 /* for allow, requested_reply=true means the rule applies
991 * only when reply was requested. requested_reply=false means
994 if (!requested_reply && rule->allow && rule->d.receive.requested_reply)
996 _dbus_verbose (" (policy) skipping allow rule since it only applies to requested replies\n");
1000 /* for deny, requested_reply=false means the rule applies only
1001 * when the reply was not requested. requested_reply=true means the
1002 * rule always applies.
1004 if (requested_reply && !rule->allow && !rule->d.receive.requested_reply)
1006 _dbus_verbose (" (policy) skipping deny rule since it only applies to unrequested replies\n");
1011 if (rule->d.receive.path != NULL)
1013 if (dbus_message_get_path (message) != NULL &&
1014 strcmp (dbus_message_get_path (message),
1015 rule->d.receive.path) != 0)
1017 _dbus_verbose (" (policy) skipping rule for different path\n");
1022 if (rule->d.receive.interface != NULL)
1024 if (dbus_message_get_interface (message) != NULL &&
1025 strcmp (dbus_message_get_interface (message),
1026 rule->d.receive.interface) != 0)
1028 _dbus_verbose (" (policy) skipping rule for different interface\n");
1033 if (rule->d.receive.member != NULL)
1035 if (dbus_message_get_member (message) != NULL &&
1036 strcmp (dbus_message_get_member (message),
1037 rule->d.receive.member) != 0)
1039 _dbus_verbose (" (policy) skipping rule for different member\n");
1044 if (rule->d.receive.error != NULL)
1046 if (dbus_message_get_error_name (message) != NULL &&
1047 strcmp (dbus_message_get_error_name (message),
1048 rule->d.receive.error) != 0)
1050 _dbus_verbose (" (policy) skipping rule for different error name\n");
1055 if (rule->d.receive.origin != NULL)
1057 /* sender can be NULL for messages that originate from the
1058 * message bus itself, we check the strings in that case as
1059 * built-in services don't have a DBusConnection but will
1060 * still set the sender on their messages.
1064 if (!dbus_message_has_sender (message,
1065 rule->d.receive.origin))
1067 _dbus_verbose (" (policy) skipping rule because message sender is not %s\n",
1068 rule->d.receive.origin);
1074 BusService *service;
1077 _dbus_string_init_const (&str, rule->d.receive.origin);
1079 service = bus_registry_lookup (registry, &str);
1081 if (service == NULL)
1083 _dbus_verbose (" (policy) skipping rule because origin %s doesn't exist\n",
1084 rule->d.receive.origin);
1088 if (!bus_service_has_owner (service, sender))
1090 _dbus_verbose (" (policy) skipping rule because origin %s isn't owned by sender\n",
1091 rule->d.receive.origin);
1098 allowed = rule->allow;
1100 _dbus_verbose (" (policy) used rule, allow now = %d\n",
1108 bus_client_policy_check_can_own (BusClientPolicy *policy,
1109 DBusConnection *connection,
1110 const DBusString *service_name)
1113 dbus_bool_t allowed;
1115 /* policy->rules is in the order the rules appeared
1116 * in the config file, i.e. last rule that applies wins
1120 link = _dbus_list_get_first_link (&policy->rules);
1121 while (link != NULL)
1123 BusPolicyRule *rule = link->data;
1125 link = _dbus_list_get_next_link (&policy->rules, link);
1127 /* Rule is skipped if it specifies a different service name from
1131 if (rule->type != BUS_POLICY_RULE_OWN)
1134 if (rule->d.own.service_name != NULL)
1136 if (!_dbus_string_equal_c_str (service_name,
1137 rule->d.own.service_name))
1142 allowed = rule->allow;
1148 #ifdef DBUS_BUILD_TESTS
1151 bus_policy_test (const DBusString *test_data_dir)
1153 /* This doesn't do anything for now because I decided to do it in
1154 * dispatch.c instead by having some of the clients in dispatch.c
1155 * have particular policies applied to them.
1161 #endif /* DBUS_BUILD_TESTS */