meta-pipewire: update to pipewire 0.3.25 and wireplumber master
[AGL/meta-agl.git] / meta-pipewire / dynamic-layers / meta-app-framework / recipes-multimedia / pipewire / pipewire / 0001-modules-add-new-access-seclabel-module.patch
1 From a949b090e9d4d11c300fb23b416a2cc69483962b Mon Sep 17 00:00:00 2001
2 From: George Kiagiadakis <george.kiagiadakis@collabora.com>
3 Date: Tue, 16 Feb 2021 17:26:20 +0200
4 Subject: [PATCH] modules: add new access-seclabel module
5
6 This module allows access control based on the security label
7 of the client. It is tailored for use with the semantics of SMACK
8
9 Upstream-Status: Inappropriate [smack specific]
10 ---
11  src/modules/meson.build              |  10 ++
12  src/modules/module-access-seclabel.c | 220 +++++++++++++++++++++++++++
13  2 files changed, 230 insertions(+)
14  create mode 100644 src/modules/module-access-seclabel.c
15
16 diff --git a/src/modules/meson.build b/src/modules/meson.build
17 index 8c9ccc85..234cff6b 100644
18 --- a/src/modules/meson.build
19 +++ b/src/modules/meson.build
20 @@ -14,6 +14,16 @@ pipewire_module_access = shared_library('pipewire-module-access', [ 'module-acce
21    dependencies : [mathlib, dl_lib, pipewire_dep],
22  )
23  
24 +pipewire_module_access_seclabel = shared_library('pipewire-module-access-seclabel',
25 +  [ 'module-access-seclabel.c' ],
26 +  c_args : pipewire_module_c_args,
27 +  include_directories : [configinc, spa_inc],
28 +  install : true,
29 +  install_dir : modules_install_dir,
30 +  install_rpath: modules_install_dir,
31 +  dependencies : [mathlib, dl_lib, pipewire_dep],
32 +)
33 +
34  pipewire_module_profiler = shared_library('pipewire-module-profiler',
35    [ 'module-profiler.c',
36      'module-profiler/protocol-native.c', ],
37 diff --git a/src/modules/module-access-seclabel.c b/src/modules/module-access-seclabel.c
38 new file mode 100644
39 index 00000000..3739f2e4
40 --- /dev/null
41 +++ b/src/modules/module-access-seclabel.c
42 @@ -0,0 +1,220 @@
43 +/* PipeWire
44 + *
45 + * Copyright © 2018 Wim Taymans
46 + * Copyright © 2021 Collabora Ltd.
47 + *   @author George Kiagiadakis <george.kiagiadakis@collabora.com>
48 + *
49 + * Permission is hereby granted, free of charge, to any person obtaining a
50 + * copy of this software and associated documentation files (the "Software"),
51 + * to deal in the Software without restriction, including without limitation
52 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
53 + * and/or sell copies of the Software, and to permit persons to whom the
54 + * Software is furnished to do so, subject to the following conditions:
55 + *
56 + * The above copyright notice and this permission notice (including the next
57 + * paragraph) shall be included in all copies or substantial portions of the
58 + * Software.
59 + *
60 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
63 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
64 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
65 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
66 + * DEALINGS IN THE SOFTWARE.
67 + */
68 +
69 +#include <string.h>
70 +#include <stdio.h>
71 +#include <errno.h>
72 +#include <sys/types.h>
73 +#include <sys/stat.h>
74 +#include <sys/vfs.h>
75 +#include <fcntl.h>
76 +#include <unistd.h>
77 +
78 +#include "config.h"
79 +
80 +#include <spa/utils/result.h>
81 +#include <spa/utils/json.h>
82 +
83 +#include <pipewire/impl.h>
84 +#include <pipewire/private.h>
85 +
86 +#define NAME "access-seclabel"
87 +
88 +#define MODULE_USAGE   "[ seclabel.allowed=<cmd-line> ] "      \
89 +                       "[ seclabel.rejected=<cmd-line> ] "     \
90 +                       "[ seclabel.restricted=<cmd-line> ] "   \
91 +
92 +static const struct spa_dict_item module_props[] = {
93 +       { PW_KEY_MODULE_AUTHOR, "George Kiagiadakis <george.kiagiadakis@collabora.com>" },
94 +       { PW_KEY_MODULE_DESCRIPTION, "Perform access check based on the security label" },
95 +       { PW_KEY_MODULE_USAGE, MODULE_USAGE },
96 +       { PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
97 +};
98 +
99 +struct impl {
100 +       struct pw_context *context;
101 +       struct pw_properties *properties;
102 +
103 +       struct spa_hook context_listener;
104 +       struct spa_hook module_listener;
105 +};
106 +
107 +static int check_label(const char *label, const char *str)
108 +{
109 +       char key[1024];
110 +       int res = 0;
111 +       struct spa_json it[2];
112 +
113 +       spa_json_init(&it[0], str, strlen(str));
114 +       if ((res = spa_json_enter_array(&it[0], &it[1])) <= 0)
115 +               goto exit;
116 +
117 +       res = 0;
118 +       while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
119 +               if (strcmp(label, key) == 0) {
120 +                       res = 1;
121 +                       break;
122 +               }
123 +       }
124 +exit:
125 +       return res;
126 +}
127 +
128 +static void
129 +context_check_access(void *data, struct pw_impl_client *client)
130 +{
131 +       struct impl *impl = data;
132 +       struct pw_permission permissions[1];
133 +       struct spa_dict_item items[2];
134 +       const struct pw_properties *props;
135 +       const char *str, *access, *label = NULL;
136 +       int res;
137 +
138 +       if ((props = pw_impl_client_get_properties(client)) != NULL) {
139 +               if ((str = pw_properties_get(props, PW_KEY_ACCESS)) != NULL) {
140 +                       pw_log_info(NAME " client %p: has already access: '%s'", client, str);
141 +                       return;
142 +               }
143 +               label = pw_properties_get(props, PW_KEY_SEC_LABEL);
144 +       }
145 +
146 +       if (!label) {
147 +               pw_log_info(NAME " client %p: has no security label", client);
148 +               return;
149 +       }
150 +
151 +       if (impl->properties && (str = pw_properties_get(impl->properties, "seclabel.allowed")) != NULL) {
152 +               res = check_label(label, str);
153 +               if (res < 0) {
154 +                       pw_log_warn(NAME" %p: client %p allowed check failed: %s",
155 +                               impl, client, spa_strerror(res));
156 +               } else if (res > 0) {
157 +                       access = "allowed";
158 +                       goto granted;
159 +               }
160 +       }
161 +
162 +       if (impl->properties && (str = pw_properties_get(impl->properties, "seclabel.rejected")) != NULL) {
163 +               res = check_label(label, str);
164 +               if (res < 0) {
165 +                       pw_log_warn(NAME" %p: client %p rejected check failed: %s",
166 +                               impl, client, spa_strerror(res));
167 +               } else if (res > 0) {
168 +                       res = -EACCES;
169 +                       access = "rejected";
170 +                       goto rejected;
171 +               }
172 +       }
173 +
174 +       if (impl->properties && (str = pw_properties_get(impl->properties, "seclabel.restricted")) != NULL) {
175 +               res = check_label(label, str);
176 +               if (res < 0) {
177 +                       pw_log_warn(NAME" %p: client %p restricted check failed: %s",
178 +                               impl, client, spa_strerror(res));
179 +               }
180 +               else if (res > 0) {
181 +                       pw_log_debug(NAME" %p: restricted client %p added", impl, client);
182 +                       access = "restricted";
183 +                       goto wait_permissions;
184 +               }
185 +       }
186 +
187 +       return;
188 +
189 +granted:
190 +       pw_log_info(NAME" %p: client %p '%s' access granted", impl, client, access);
191 +       items[0] = SPA_DICT_ITEM_INIT(PW_KEY_ACCESS, access);
192 +       pw_impl_client_update_properties(client, &SPA_DICT_INIT(items, 1));
193 +
194 +       permissions[0] = PW_PERMISSION_INIT(PW_ID_ANY, PW_PERM_ALL);
195 +       pw_impl_client_update_permissions(client, 1, permissions);
196 +       return;
197 +
198 +wait_permissions:
199 +       pw_log_info(NAME " %p: client %p wait for '%s' permissions",
200 +                       impl, client, access);
201 +       items[0] = SPA_DICT_ITEM_INIT(PW_KEY_ACCESS, access);
202 +       pw_impl_client_update_properties(client, &SPA_DICT_INIT(items, 1));
203 +       return;
204 +
205 +rejected:
206 +       pw_resource_error(pw_impl_client_get_core_resource(client), res, access);
207 +       items[0] = SPA_DICT_ITEM_INIT(PW_KEY_ACCESS, access);
208 +       pw_impl_client_update_properties(client, &SPA_DICT_INIT(items, 1));
209 +       return;
210 +}
211 +
212 +static const struct pw_context_events context_events = {
213 +       PW_VERSION_CONTEXT_EVENTS,
214 +       .check_access = context_check_access,
215 +};
216 +
217 +static void module_destroy(void *data)
218 +{
219 +       struct impl *impl = data;
220 +
221 +       spa_hook_remove(&impl->context_listener);
222 +       spa_hook_remove(&impl->module_listener);
223 +
224 +       if (impl->properties)
225 +               pw_properties_free(impl->properties);
226 +
227 +       free(impl);
228 +}
229 +
230 +static const struct pw_impl_module_events module_events = {
231 +       PW_VERSION_IMPL_MODULE_EVENTS,
232 +       .destroy = module_destroy,
233 +};
234 +
235 +SPA_EXPORT
236 +int pipewire__module_init(struct pw_impl_module *module, const char *args)
237 +{
238 +       struct pw_context *context = pw_impl_module_get_context(module);
239 +       struct pw_properties *props;
240 +       struct impl *impl;
241 +
242 +       impl = calloc(1, sizeof(struct impl));
243 +       if (impl == NULL)
244 +               return -errno;
245 +
246 +       pw_log_debug(NAME" module %p: new %s", impl, args);
247 +
248 +       if (args)
249 +               props = pw_properties_new_string(args);
250 +       else
251 +               props = NULL;
252 +
253 +       impl->context = context;
254 +       impl->properties = props;
255 +
256 +       pw_context_add_listener(context, &impl->context_listener, &context_events, impl);
257 +       pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl);
258 +
259 +       pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
260 +
261 +       return 0;
262 +}
263 -- 
264 2.30.0
265