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
|
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "config.h"
#include "pipewire/core.h"
#include "pipewire/interfaces.h"
#include "pipewire/link.h"
#include "pipewire/log.h"
#include "pipewire/module.h"
#include "pipewire/properties.h"
#include "pipewire/utils.h"
static const struct spa_dict_item module_props[] = {
{ PW_MODULE_PROP_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
{ PW_MODULE_PROP_DESCRIPTION, "Perform access check" },
{ PW_MODULE_PROP_VERSION, PACKAGE_VERSION },
};
struct impl {
struct pw_core *core;
struct pw_properties *properties;
struct spa_hook core_listener;
struct spa_hook module_listener;
};
static int check_cmdline(struct pw_client *client, const struct ucred *ucred, const char *str)
{
char path[2048];
int fd;
sprintf(path, "/proc/%u/cmdline", ucred->pid);
fd = open(path, O_RDONLY);
if (fd < 0)
return -errno;
if (read(fd, path, 1024) <= 0)
return -EIO;
if (strcmp(path, str) == 0)
return 1;
return 0;
}
static int check_flatpak(struct pw_client *client, const struct ucred *ucred)
{
char root_path[2048];
int root_fd, info_fd, res;
struct stat stat_buf;
sprintf(root_path, "/proc/%u/root", ucred->pid);
root_fd = openat (AT_FDCWD, root_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY);
if (root_fd == -1) {
/* Not able to open the root dir shouldn't happen. Probably the app died and
* we're failing due to /proc/$pid not existing. In that case fail instead
* of treating this as privileged. */
res = -errno;
pw_log_error("failed to open \"%s\": %m", root_path);
return res;
}
info_fd = openat (root_fd, ".flatpak-info", O_RDONLY | O_CLOEXEC | O_NOCTTY);
close (root_fd);
if (info_fd == -1) {
if (errno == ENOENT) {
pw_log_debug("no .flatpak-info, client on the host");
/* No file => on the host */
return 0;
}
res = -errno;
pw_log_error("error opening .flatpak-info: %m");
return res;
}
if (fstat (info_fd, &stat_buf) != 0 || !S_ISREG (stat_buf.st_mode)) {
/* Some weird fd => failure, assume sandboxed */
close(info_fd);
pw_log_error("error fstat .flatpak-info: %m");
}
return 1;
}
static void
core_check_access(void *data, struct pw_client *client)
{
struct impl *impl = data;
const struct ucred *ucred;
struct spa_dict_item items[2];
const char *str;
int res;
ucred = pw_client_get_ucred(client);
if (!ucred) {
pw_log_info("no trusted pid found, assuming not sandboxed\n");
goto granted;
} else {
pw_log_info("client has trusted pid %d", ucred->pid);
}
if (impl->properties && (str = pw_properties_get(impl->properties, "blacklisted")) != NULL) {
res = check_cmdline(client, ucred, str);
if (res == 0)
goto granted;
if (res > 0)
res = EACCES;
goto blacklisted;
}
if (impl->properties && (str = pw_properties_get(impl->properties, "restricted")) != NULL) {
res = check_cmdline(client, ucred, str);
if (res == 0)
goto granted;
if (res < 0) {
pw_log_warn("module %p: client %p restricted check failed: %s",
impl, client, spa_strerror(res));
}
else if (res > 0) {
pw_log_debug("module %p: restricted client %p added", impl, client);
}
items[0] = SPA_DICT_ITEM_INIT("pipewire.access", "restricted");
goto wait_permissions;
}
res = check_flatpak(client, ucred);
if (res != 0) {
if (res < 0) {
pw_log_warn("module %p: client %p sandbox check failed: %s",
impl, client, spa_strerror(res));
}
else if (res > 0) {
pw_log_debug("module %p: sandboxed client %p added", impl, client);
}
items[0] = SPA_DICT_ITEM_INIT("pipewire.access", "flatpak");
goto wait_permissions;
}
granted:
pw_log_debug("module %p: client %p access granted", impl, client);
pw_client_set_permissions(client, PW_PERM_RWX);
return;
wait_permissions:
pw_log_debug("module %p: client %p wait for permissions", impl, client);
pw_client_update_properties(client, &SPA_DICT_INIT(items, 1));
pw_client_set_busy(client, true);
return;
blacklisted:
items[0] = SPA_DICT_ITEM_INIT("pipewire.access", "blacklisted");
pw_resource_error(pw_client_get_core_resource(client), 0, res, "blacklisted");
pw_client_update_properties(client, &SPA_DICT_INIT(items, 1));
return;
}
static const struct pw_core_events core_events = {
PW_VERSION_CORE_EVENTS,
.check_access = core_check_access,
};
static void module_destroy(void *data)
{
struct impl *impl = data;
spa_hook_remove(&impl->core_listener);
spa_hook_remove(&impl->module_listener);
if (impl->properties)
pw_properties_free(impl->properties);
free(impl);
}
static const struct pw_module_events module_events = {
PW_VERSION_MODULE_EVENTS,
.destroy = module_destroy,
};
int pipewire__module_init(struct pw_module *module, const char *args)
{
struct pw_core *core = pw_module_get_core(module);
struct pw_properties *props;
struct impl *impl;
impl = calloc(1, sizeof(struct impl));
if (impl == NULL)
return -ENOMEM;
pw_log_debug("module %p: new %s", impl, args);
if (args)
props = pw_properties_new_string(args);
else
props = NULL;
impl->core = core;
impl->properties = props;
pw_core_add_listener(core, &impl->core_listener, &core_events, impl);
pw_module_add_listener(module, &impl->module_listener, &module_events, impl);
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
return 0;
}
|