File: github_apparmor_support.patch

package info (click to toggle)
dbus-broker 37-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,012 kB
  • sloc: ansic: 32,667; makefile: 146; sh: 56; python: 44
file content (353 lines) | stat: -rw-r--r-- 13,963 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
Author: Sebastian Reichel <sebastian.reichel@collabora.com>
Description: add apparmor support
Forwarded: https://github.com/bus1/dbus-broker/pull/286
--- a/src/bus/policy.c
+++ b/src/bus/policy.c
@@ -1064,7 +1064,7 @@
         size_t i;
         int r;
 
-        r = bus_apparmor_check_xmit(snapshot->apparmor, true, snapshot->seclabel, subject_seclabel,
+        r = bus_apparmor_check_send(snapshot->apparmor, snapshot->seclabel, subject_seclabel,
                                     subject, subject_id, path, interface, method);
         if (r) {
                 if (r == BUS_APPARMOR_E_DENIED)
@@ -1111,16 +1111,6 @@
                                   size_t n_fds) {
         PolicyVerdict verdict = POLICY_VERDICT_INIT;
         size_t i;
-        int r;
-
-        r = bus_apparmor_check_xmit(snapshot->apparmor, false, subject_seclabel, snapshot->seclabel,
-                                    subject, subject_id, path, interface, method);
-        if (r) {
-                if (r == BUS_APPARMOR_E_DENIED)
-                        return POLICY_E_APPARMOR_ACCESS_DENIED;
-
-                return error_fold(r);
-        }
 
         for (i = 0; i < snapshot->n_batches; ++i)
                 policy_snapshot_check_xmit(snapshot->batches[i],
--- a/src/util/apparmor-fallback.c
+++ b/src/util/apparmor-fallback.c
@@ -62,7 +62,7 @@
         return 0;
 }
 
-int bus_apparmor_check_xmit(BusAppArmorRegistry *registry, bool check_send,
+int bus_apparmor_check_send(BusAppArmorRegistry *registry,
                             const char *sender_context, const char *receiver_context,
                             NameSet *subject, uint64_t subject_id,
                             const char *path, const char *interface, const char *method) {
--- a/src/util/apparmor.c
+++ b/src/util/apparmor.c
@@ -473,26 +473,25 @@
         if (r)
                 return error_origin(-c_errno());
 
+        if (string_equal(security_mode, "complain"))
+                allow = true;
+
         if (audit)
                 bus_apparmor_log(
                         registry,
                         "apparmor=\"%s\" operation=\"dbus_bind\" "
-                        "bus=\"%s\" name=\"%s\"",
+                        "bus=\"%s\" name=\"%s\" mask=\"bind\"",
                         allow ? "ALLOWED" : "DENIED",
                         registry->bustype,
                         name
                 );
 
-        if (string_equal(security_mode, "complain"))
-                allow = true;
-
         return allow ? 0 : BUS_APPARMOR_E_DENIED;
 }
 
 /**
- * bus_apparmor_check_xmit() - check if the given transaction is allowed
+ * bus_apparmor_check_send() - check if the given transaction is allowed
  * @registry:           AppArmor registry to operate on
- * @check_send:         true if sending should be checked, false if receiving
  * @sender_context:     security context of the sender
  * @receiver_context:   security context of the receiver, or NULL
  * @subject:            List of names
@@ -511,8 +510,7 @@
  * Return: 0 if the transaction is allowed, BUS_APPARMOR_E_DENIED if it is not,
  *         or a negative error code on failure.
  */
-int bus_apparmor_check_xmit(BusAppArmorRegistry *registry,
-                            bool check_send,
+int bus_apparmor_check_send(BusAppArmorRegistry *registry,
                             const char *sender_context,
                             const char *receiver_context,
                             NameSet *subject,
@@ -524,7 +522,7 @@
         _c_cleanup_(c_freep) char *receiver_context_dup = NULL;
         char *sender_security_label, *sender_security_mode;
         char *receiver_security_label, *receiver_security_mode;
-        int r, allow, audit;
+        int r, src_allow = false, src_audit = true, dst_allow = false, dst_audit = true;
 
         if (!registry->bustype)
                 return 0;
@@ -539,46 +537,71 @@
         sender_security_label = aa_splitcon(sender_context_dup, &sender_security_mode);
         receiver_security_label = aa_splitcon(receiver_context_dup, &receiver_security_mode);
 
-        if (is_unconfined(sender_security_label, sender_security_mode))
-                return 0;
-        if (is_unconfined(receiver_security_label, receiver_security_mode))
-                return 0;
-
-        if (check_send)
+        if (is_unconfined(sender_security_label, sender_security_mode)) {
+                src_allow = true;
+                src_audit = false;
+        } else {
                 r = apparmor_message_query(true,
                                            sender_security_label,
                                            registry->bustype,
                                            receiver_security_label,
                                            subject, subject_id, path,
-                                           interface, method, &allow, &audit);
-        else
+                                           interface, method, &src_allow, &src_audit);
+
+                if (r)
+                        return error_fold(r);
+        }
+
+        if (is_unconfined(receiver_security_label, receiver_security_mode)) {
+                dst_allow = true;
+                dst_audit = false;
+        } else {
                 r = apparmor_message_query(false,
                                            receiver_security_label,
                                            registry->bustype,
                                            sender_security_label,
                                            subject, subject_id, path,
-                                           interface, method, &allow, &audit);
-        if (r)
-                return error_fold(r);
-
-        if (audit)
-                bus_apparmor_log(
-                        registry,
-                        "apparmor=\"%s\" operation=\"dbus_%s\" bus=\"%s\" "
-                        "path=\"%s\" interface=\"%s\" method=\"%s\"",
-                        allow ? "ALLOWED" : "DENIED",
-                        check_send ? "send" : "receive",
+                                           interface, method, &dst_allow, &dst_audit);
+                if (r)
+                        return error_fold(r);
+        }
+
+        if (string_equal(sender_security_mode, "complain"))
+                src_allow = 1;
+        if (string_equal(receiver_security_mode, "complain"))
+                dst_allow = 1;
+
+        if (src_audit) {
+                bus_apparmor_log(registry,
+                        "apparmor=\"%s\" operation=\"dbus_method_call\" "
+                        "bus=\"%s\" path=\"%s\" interface=\"%s\" method=\"%s\" "
+                        "mask=\"send\" label=\"%s\" peer_label=\"%s\"",
+                        src_allow ? "ALLOWED" : "DENIED",
                         registry->bustype,
                         path,
                         interface,
-                        method
+                        method,
+                        sender_security_label,
+                        receiver_security_label
                 );
+        }
 
-        if ((check_send && string_equal(sender_security_mode, "complain")) ||
-            (!check_send && string_equal(receiver_security_mode, "complain")))
-                allow = 1;
+        if (dst_audit) {
+                bus_apparmor_log(registry,
+                        "apparmor=\"%s\" operation=\"dbus_method_call\" "
+                        "bus=\"%s\" path=\"%s\" interface=\"%s\" method=\"%s\" "
+                        "mask=\"receive\" label=\"%s\" peer_label=\"%s\"",
+                        dst_allow ? "ALLOWED" : "DENIED",
+                        registry->bustype,
+                        path,
+                        interface,
+                        method,
+                        receiver_security_label,
+                        sender_security_label
+                );
+        }
 
-        return allow ? 0 : BUS_APPARMOR_E_DENIED;
+        return (src_allow && dst_allow) ? 0 : BUS_APPARMOR_E_DENIED;
 }
 
 /**
@@ -624,6 +647,9 @@
         if (r)
                 return error_origin(-c_errno());
 
+        if (string_equal(security_mode, "complain"))
+                allow = 1;
+
         if (audit)
                 bus_apparmor_log(
                         registry,
@@ -634,8 +660,5 @@
                         context
                 );
 
-        if (string_equal(security_mode, "complain"))
-                allow = 1;
-
         return allow ? 0 : BUS_APPARMOR_E_DENIED;
 }
--- a/src/util/apparmor.h
+++ b/src/util/apparmor.h
@@ -29,7 +29,7 @@
 
 int bus_apparmor_check_own(struct BusAppArmorRegistry *registry, const char *context,
                            const char *name);
-int bus_apparmor_check_xmit(BusAppArmorRegistry *registry, bool check_send,
+int bus_apparmor_check_send(BusAppArmorRegistry *registry,
                             const char *sender_context, const char *receiver_context,
                             NameSet *subject, uint64_t subject_id,
                             const char *path, const char *interface, const char *method);
--- a/src/launch/launcher.c
+++ b/src/launch/launcher.c
@@ -1156,6 +1156,55 @@
         return 0;
 }
 
+/**
+ * launcher_apparmor_check() - checks if AppArmor should be used
+ * @apparmor_mode:        bi-directional argument telling if AppArmor should be enabled
+ *
+ * This should be called with the requested apparmor mode. The
+ * function modifies the mode to either CONFIG_APPARMOR_DISABLED
+ * or CONFIG_APPARMOR_ENABLED based on platform support. If the
+ * function keeps the mode as CONFIG_APPARMOR_REQUIRED, AppArmor
+ * has been configured as mandatory but is not supported.
+ *
+ * Returns: 0 if check succeeded, or negative error code on failure.
+ */
+int launcher_apparmor_check(unsigned int *apparmor_mode) {
+        bool enabled, supported;
+        int r;
+
+        if (*apparmor_mode == CONFIG_APPARMOR_DISABLED)
+                return CONFIG_APPARMOR_DISABLED;
+
+        r = bus_apparmor_is_enabled(&enabled);
+        if (r)
+                return error_fold(r);
+
+        r = bus_apparmor_dbus_supported(&supported);
+        if (r)
+                return error_fold(r);
+
+        if (*apparmor_mode == CONFIG_APPARMOR_ENABLED) {
+                if (enabled && !supported) {
+                        fprintf(stderr, "Kernel is missing AppArmor DBus support.\n");
+                        *apparmor_mode = CONFIG_APPARMOR_DISABLED;
+                } else if (enabled) {
+                        *apparmor_mode = CONFIG_APPARMOR_ENABLED;
+                } else {
+                        *apparmor_mode = CONFIG_APPARMOR_DISABLED;
+                }
+        } else if (*apparmor_mode == CONFIG_APPARMOR_REQUIRED) {
+                if (!enabled || !supported) {
+                        fprintf(stderr, "AppArmor required, but not supported. Exiting.\n");
+                        *apparmor_mode = CONFIG_APPARMOR_REQUIRED;
+                } else {
+                        *apparmor_mode = CONFIG_APPARMOR_ENABLED;
+                }
+        }
+
+        return 0;
+}
+
+
 static int launcher_reload_config(Launcher *launcher) {
         _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
         _c_cleanup_(policy_deinit) Policy policy = POLICY_INIT(policy);
@@ -1196,29 +1245,13 @@
         if (r)
                 goto out;
 
-        switch (policy.apparmor_mode) {
-        case CONFIG_APPARMOR_ENABLED: {
-                bool enabled;
-
-                /* XXX: See comments in launcher_run() */
-
-                r = bus_apparmor_is_enabled(&enabled);
-                if (r)
-                        return error_fold(r);
-
-                if (enabled)
-                        fprintf(stderr, "AppArmor enabled, but not supported. Ignoring.\n");
-
-                policy.apparmor_mode = CONFIG_APPARMOR_DISABLED;
-                break;
-        }
-        case CONFIG_APPARMOR_REQUIRED:
-                fprintf(stderr, "AppArmor required, but not supported. Exiting.\n");
-
+        r = launcher_apparmor_check(&policy.apparmor_mode);
+        if (r < 0)
+                return error_fold(r);
+        if (policy.apparmor_mode == CONFIG_APPARMOR_REQUIRED) {
                 r = sd_event_exit(launcher->event, 0);
                 if (r < 0)
                         return error_fold(r);
-
                 return 0;
         }
 
@@ -1348,35 +1381,11 @@
         if (r)
                 return error_trace(r);
 
-        switch (policy.apparmor_mode) {
-        case CONFIG_APPARMOR_ENABLED: {
-                bool enabled, supported;
-
-                r = bus_apparmor_is_enabled(&enabled);
-                if (r)
-                        return error_fold(r);
-
-                r = bus_apparmor_dbus_supported(&supported);
-                if (r)
-                        return error_fold(r);
-
-                if (enabled && !supported)
-                        fprintf(stderr, "Kernel is missing AppArmor DBus support. Ignoring.\n");
-                else if (enabled)
-                        fprintf(stderr, "AppArmor enabled, but not supported. Ignoring.\n");
-
-                /* XXX: once the broker supports AppArmor, set this to DISABLED if and only if
-                 *      it is disabled in the kernel. */
-                policy.apparmor_mode = CONFIG_APPARMOR_DISABLED;
-                break;
-        }
-        case CONFIG_APPARMOR_REQUIRED:
-                fprintf(stderr, "AppArmor required, but not supported. Exiting.\n");
-
-                /* XXX: once the broker supports AppArmor, set this to enabled if and only
-                 *      if it is enabled in the kernel, and exit the launcher otherwise. */
+        r = launcher_apparmor_check(&policy.apparmor_mode);
+        if (r < 0)
+                return error_fold(r);
+        if (policy.apparmor_mode == CONFIG_APPARMOR_REQUIRED)
                 return 0;
-        }
 
         c_assert(launcher->fd_listen >= 0);