07a1ec114da9bbd56a7e37270a9c0d872c3b92f2
[AGL/meta-agl.git] / meta-pipewire / recipes-multimedia / pipewire / pipewire / 0005-module-access-add-same-sec-label-mode.patch
1 From 19fad1a4fa8bdc4f02aac4e169e7ff9cab18bdcd Mon Sep 17 00:00:00 2001
2 From: George Kiagiadakis <george.kiagiadakis@collabora.com>
3 Date: Tue, 19 Nov 2019 17:09:07 +0200
4 Subject: [PATCH] module-access: add same-sec-label-mode
5
6 This is a mode where the access module allows all clients that have
7 the same security label as the pipewire daemon, and every other
8 client is put on the restricted state.
9
10 In systems that use SMACK security labels, such as AGL, this allows
11 the session manager (which is spawned by pipewire, inheriting the
12 same smack label) to have full access to all objects, while every
13 other client is restricted and the session manager must decide
14 what to do with it
15
16 Note that while this option is configurable, there is no loss of
17 security if this option is not set in the configuration. Clients
18 that don't have the same security context will be considered to
19 be flatpak clients because pipewire will not be able to open
20 /proc/pid/cmdline. This however results in some unwanted error
21 messages that may be confusing.
22
23 Upstream-Status: Inappropriate [agl/smack specific]
24 ---
25  src/modules/module-access.c | 45 ++++++++++++++++++++++++++++++++++++-
26  1 file changed, 44 insertions(+), 1 deletion(-)
27
28 diff --git a/src/modules/module-access.c b/src/modules/module-access.c
29 index 09dafa43..f75306d9 100644
30 --- a/src/modules/module-access.c
31 +++ b/src/modules/module-access.c
32 @@ -50,6 +50,30 @@ struct impl {
33         struct spa_hook module_listener;
34  };
35  
36 +static int check_seclabel(const char *str)
37 +{
38 +       char attr[1024];
39 +       int fd, len;
40 +
41 +       fd = open("/proc/self/attr/current", O_RDONLY);
42 +       if (fd < 0)
43 +               return -errno;
44 +
45 +       if ((len = read(fd, attr, 1024)) <= 0) {
46 +               close(fd);
47 +               return -EIO;
48 +       }
49 +       attr[len] = '\0';
50 +
51 +       if (strcmp(attr, str) == 0) {
52 +               close(fd);
53 +               return 1;
54 +       }
55 +
56 +       close(fd);
57 +       return 0;
58 +}
59 +
60  static int check_cmdline(struct pw_client *client, int pid, const char *str)
61  {
62         char path[2048];
63 @@ -121,8 +145,27 @@ core_check_access(void *data, struct pw_client *client)
64         const char *str;
65         int pid, res;
66  
67 +       props = pw_client_get_properties(client);
68 +
69 +       if (impl->properties &&
70 +           (str = pw_properties_get(impl->properties, "same-sec-label-mode")) != NULL &&
71 +           strcmp(str, "1") == 0) {
72 +               if (props && (str = pw_properties_get(props, PW_KEY_SEC_LABEL)) != NULL) {
73 +                       res = check_seclabel(str);
74 +                       if (res == 1)
75 +                               goto granted;
76 +                       else if (res < 0)
77 +                               pw_log_warn("module %p: client %p seclabel check failed: %s",
78 +                                       impl, client, spa_strerror(res));
79 +               }
80 +               pw_log_debug("module %p: seclabel restricted client %p added",
81 +                       impl, client);
82 +               items[0] = SPA_DICT_ITEM_INIT(PW_KEY_ACCESS, "restricted");
83 +               goto wait_permissions;
84 +       }
85 +
86         pid = -EINVAL;
87 -       if ((props = pw_client_get_properties(client)) != NULL) {
88 +       if (props != NULL) {
89                 if ((str = pw_properties_get(props, PW_KEY_SEC_PID)) != NULL)
90                         pid = atoi(str);
91         }
92 -- 
93 2.24.0
94