Debian Codegraph @

src/bus/policy.h

raw
1 #pragma once
2
3 /*
4 * D-Bus Policy
5 */
6
7 #include <c-dvar.h>
8 #include <c-list.h>
9 #include <c-rbtree.h>
10 #include <c-stdaux.h>
11 #include <stdlib.h>
12 #include "dbus/protocol.h"
13 #include "util/ref.h"
14
15 typedef struct BusAppArmorRegistry BusAppArmorRegistry;
16 typedef struct BusSELinuxRegistry BusSELinuxRegistry;
17 typedef struct NameSet NameSet;
18 typedef struct PolicyBatch PolicyBatch;
19 typedef struct PolicyBatchName PolicyBatchName;
20 typedef struct PolicyRegistry PolicyRegistry;
21 typedef struct PolicyRegistryNode PolicyRegistryNode;
22 typedef struct PolicyRegistryNodeIndex PolicyRegistryNodeIndex;
23 typedef struct PolicySnapshot PolicySnapshot;
24 typedef struct PolicyVerdict PolicyVerdict;
25 typedef struct PolicyXmit PolicyXmit;
26
27 enum {
28 _POLICY_E_SUCCESS,
29
30 POLICY_E_INVALID,
31 POLICY_E_ACCESS_DENIED,
32 POLICY_E_SELINUX_ACCESS_DENIED,
33 POLICY_E_APPARMOR_ACCESS_DENIED,
34 };
35
36 struct PolicyVerdict {
37 bool verdict;
38 uint64_t priority;
39 };
40
41 #define POLICY_VERDICT_INIT {}
42 #define POLICY_VERDICT_INIT_WITH(_v, _p) { .verdict = (_v), .priority = (_p) }
43
44 struct PolicyXmit {
45 CList batch_link;
46 PolicyVerdict verdict;
47 unsigned int type;
48 unsigned int broadcast;
49 char *path;
50 char *interface;
51 char *member;
52 uint64_t min_fds;
53 uint64_t max_fds;
54 };
55
56 #define POLICY_XMIT_NULL(_x) { \
57 .batch_link = C_LIST_INIT((_x).batch_link), \
58 .verdict = POLICY_VERDICT_INIT, \
59 .type = DBUS_MESSAGE_TYPE_INVALID, \
60 .max_fds = UINT64_MAX, \
61 }
62
63 struct PolicyBatchName {
64 PolicyBatch *batch;
65 CRBNode batch_node;
66 PolicyVerdict own_verdict;
67 PolicyVerdict own_prefix_verdict;
68 CList send_unindexed;
69 CList recv_unindexed;
70 char name[];
71 };
72
73 #define POLICY_BATCH_NAME_NULL(_x) { \
74 .batch_node = C_RBNODE_INIT((_x).batch_node), \
75 .own_verdict = POLICY_VERDICT_INIT, \
76 .own_prefix_verdict = POLICY_VERDICT_INIT, \
77 .send_unindexed = C_LIST_INIT((_x).send_unindexed), \
78 .recv_unindexed = C_LIST_INIT((_x).recv_unindexed), \
79 }
80
81 struct PolicyBatch {
82 _Atomic unsigned long n_refs;
83 PolicyVerdict connect_verdict;
84 CRBTree name_tree;
85 };
86
87 #define POLICY_BATCH_NULL(_x) { \
88 .n_refs = REF_INIT, \
89 .connect_verdict = POLICY_VERDICT_INIT, \
90 .name_tree = C_RBTREE_INIT, \
91 }
92
93 struct PolicyRegistryNodeIndex {
94 uint32_t uidgid_start;
95 uint32_t uidgid_end;
96 };
97
98 #define POLICY_REGISTRY_NODE_INDEX_NULL { \
99 .uidgid_start = (uint32_t)-1, \
100 .uidgid_end = (uint32_t)-1, \
101 }
102
103 struct PolicyRegistryNode {
104 PolicyRegistryNodeIndex index;
105 CRBTree *registry_tree;
106 CRBNode registry_node;
107 PolicyBatch *batch;
108 };
109
110 #define POLICY_REGISTRY_NODE_NULL(_x) { \
111 .index = POLICY_REGISTRY_NODE_INDEX_NULL, \
112 .registry_node = C_RBNODE_INIT((_x).registry_node), \
113 }
114
115 struct PolicyRegistry {
116 BusAppArmorRegistry *apparmor;
117 BusSELinuxRegistry *selinux;
118 PolicyBatch *default_batch;
119 CRBTree uid_range_tree;
120 CRBTree uid_tree;
121 CRBTree gid_tree;
122 };
123
124 #define POLICY_REGISTRY_NULL { \
125 .uid_range_tree = C_RBTREE_INIT, \
126 .uid_tree = C_RBTREE_INIT, \
127 .gid_tree = C_RBTREE_INIT, \
128 }
129
130 struct PolicySnapshot {
131 BusAppArmorRegistry *apparmor;
132 BusSELinuxRegistry *selinux;
133 char *seclabel;
134 size_t n_batches;
135 PolicyBatch *batches[];
136 };
137
138 #define POLICY_SNAPSHOT_NULL {}
139
140 /* batches */
141
142 int policy_batch_new(PolicyBatch **batchp);
143 void policy_batch_free(_Atomic unsigned long *n_refs, void *userdata);
144
145 /* registry */
146
147 int policy_registry_new(PolicyRegistry **registryp, const char *fallback_seclabel);
148 PolicyRegistry *policy_registry_free(PolicyRegistry *registry);
149
150 int policy_registry_import(PolicyRegistry *registry, CDVar *v);
151
152 C_DEFINE_CLEANUP(PolicyRegistry *, policy_registry_free);
153
154 /* snapshots */
155
156 int policy_snapshot_new(PolicySnapshot **snapshotp,
157 PolicyRegistry *registry,
158 const char *context,
159 uint32_t uid,
160 const uint32_t *gids,
161 size_t n_gids);
162 PolicySnapshot *policy_snapshot_free(PolicySnapshot *snapshot);
163
164 int policy_snapshot_dup(PolicySnapshot *snapshot, PolicySnapshot **newp);
165
166 int policy_snapshot_check_connect(PolicySnapshot *snapshot);
167 int policy_snapshot_check_own(PolicySnapshot *snapshot, const char *name);
168 int policy_snapshot_check_send(PolicySnapshot *snapshot,
169 const char *subject_seclabel,
170 NameSet *subject,
171 uint64_t subject_id,
172 const char *interface,
173 const char *method,
174 const char *path,
175 unsigned int type,
176 bool broadcast,
177 size_t n_fds);
178 int policy_snapshot_check_receive(PolicySnapshot *snapshot,
179 const char *subject_seclabel,
180 NameSet *subject,
181 uint64_t subject_id,
182 const char *interface,
183 const char *method,
184 const char *path,
185 unsigned int type,
186 bool broadcast,
187 size_t n_fds);
188
189 C_DEFINE_CLEANUP(PolicySnapshot *, policy_snapshot_free);
190
191 /* inline helpers */
192
193 static inline PolicyBatch *policy_batch_ref(PolicyBatch *batch) {
194 if (batch)
195 ref_inc(&batch->n_refs);
196 return batch;
197 }
198
199 static inline PolicyBatch *policy_batch_unref(PolicyBatch *batch) {
200 if (batch)
201 ref_dec(&batch->n_refs, policy_batch_free, NULL);
202 return NULL;
203 }
204
205 C_DEFINE_CLEANUP(PolicyBatch *, policy_batch_unref);