2018-10-27 17:33:21 +01:00
|
|
|
/* PipeWire
|
|
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2018 Wim Taymans
|
2018-10-27 17:33:21 +01:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2018-10-27 17:33:21 +01:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2018-10-27 17:33:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
2019-01-14 12:58:23 +01:00
|
|
|
#include <pipewire/pipewire.h>
|
2018-10-27 17:33:21 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-10 13:12:22 +02:00
|
|
|
static int check_cmdline(struct pw_client *client, int pid, const char *str)
|
2018-10-27 17:33:21 +01:00
|
|
|
{
|
|
|
|
|
char path[2048];
|
|
|
|
|
int fd;
|
|
|
|
|
|
2019-05-10 13:12:22 +02:00
|
|
|
sprintf(path, "/proc/%u/cmdline", pid);
|
2018-10-27 17:33:21 +01:00
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
2019-02-13 13:22:07 +02:00
|
|
|
if (read(fd, path, 1024) <= 0) {
|
|
|
|
|
close(fd);
|
2018-10-27 17:33:21 +01:00
|
|
|
return -EIO;
|
2019-02-13 13:22:07 +02:00
|
|
|
}
|
2018-10-27 17:33:21 +01:00
|
|
|
|
2019-02-13 13:22:07 +02:00
|
|
|
if (strcmp(path, str) == 0) {
|
|
|
|
|
close(fd);
|
2018-10-27 17:33:21 +01:00
|
|
|
return 1;
|
2019-02-13 13:22:07 +02:00
|
|
|
}
|
2018-10-27 17:33:21 +01:00
|
|
|
|
2019-02-13 13:22:07 +02:00
|
|
|
close(fd);
|
2018-10-27 17:33:21 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 13:12:22 +02:00
|
|
|
static int check_flatpak(struct pw_client *client, int pid)
|
2018-10-27 17:33:21 +01:00
|
|
|
{
|
|
|
|
|
char root_path[2048];
|
|
|
|
|
int root_fd, info_fd, res;
|
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
2019-05-10 13:12:22 +02:00
|
|
|
sprintf(root_path, "/proc/%u/root", pid);
|
2018-10-27 17:33:21 +01:00
|
|
|
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
|
2018-10-29 08:46:09 +00:00
|
|
|
core_check_access(void *data, struct pw_client *client)
|
2018-10-27 17:33:21 +01:00
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
2018-11-05 15:02:08 +01:00
|
|
|
struct pw_permission permissions[1];
|
2018-10-29 08:46:09 +00:00
|
|
|
struct spa_dict_item items[2];
|
2019-05-10 13:12:22 +02:00
|
|
|
const struct pw_properties *props;
|
2018-10-29 08:46:09 +00:00
|
|
|
const char *str;
|
2019-05-10 13:12:22 +02:00
|
|
|
int pid, res;
|
2018-10-27 17:33:21 +01:00
|
|
|
|
2019-05-10 13:12:22 +02:00
|
|
|
pid = -EINVAL;
|
|
|
|
|
if ((props = pw_client_get_properties(client)) != NULL) {
|
|
|
|
|
if ((str = pw_properties_get(props, PW_CLIENT_PROP_UCRED_PID)) != NULL)
|
|
|
|
|
pid = atoi(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pid < 0) {
|
2018-10-29 08:46:09 +00:00
|
|
|
pw_log_info("no trusted pid found, assuming not sandboxed\n");
|
|
|
|
|
goto granted;
|
|
|
|
|
} else {
|
2019-05-10 13:12:22 +02:00
|
|
|
pw_log_info("client has trusted pid %d", pid);
|
2018-10-29 08:46:09 +00:00
|
|
|
}
|
2018-10-27 17:33:21 +01:00
|
|
|
|
2018-10-29 08:46:09 +00:00
|
|
|
if (impl->properties && (str = pw_properties_get(impl->properties, "blacklisted")) != NULL) {
|
2019-05-10 13:12:22 +02:00
|
|
|
res = check_cmdline(client, pid, str);
|
2018-10-29 08:46:09 +00:00
|
|
|
if (res == 0)
|
|
|
|
|
goto granted;
|
|
|
|
|
if (res > 0)
|
2018-11-05 15:02:08 +01:00
|
|
|
res = -EACCES;
|
|
|
|
|
items[0] = SPA_DICT_ITEM_INIT("pipewire.access", "blacklisted");
|
2018-10-29 08:46:09 +00:00
|
|
|
goto blacklisted;
|
2018-10-27 17:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
2018-10-29 08:46:09 +00:00
|
|
|
if (impl->properties && (str = pw_properties_get(impl->properties, "restricted")) != NULL) {
|
2019-05-10 13:12:22 +02:00
|
|
|
res = check_cmdline(client, pid, str);
|
2018-10-29 08:46:09 +00:00
|
|
|
if (res == 0)
|
|
|
|
|
goto granted;
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
pw_log_warn("module %p: client %p restricted check failed: %s",
|
2018-10-27 17:33:21 +01:00
|
|
|
impl, client, spa_strerror(res));
|
2018-10-29 08:46:09 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2018-10-27 17:33:21 +01:00
|
|
|
}
|
2018-10-29 08:46:09 +00:00
|
|
|
|
2019-05-10 13:12:22 +02:00
|
|
|
res = check_flatpak(client, pid);
|
2018-10-29 08:46:09 +00:00
|
|
|
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;
|
2018-10-27 17:33:21 +01:00
|
|
|
}
|
2018-10-29 08:46:09 +00:00
|
|
|
|
|
|
|
|
granted:
|
|
|
|
|
pw_log_debug("module %p: client %p access granted", impl, client);
|
2018-11-05 15:02:08 +01:00
|
|
|
permissions[0] = PW_PERMISSION_INIT(-1, PW_PERM_RWX);
|
|
|
|
|
pw_client_update_permissions(client, 1, permissions);
|
2018-10-29 08:46:09 +00:00
|
|
|
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));
|
2018-10-27 17:33:21 +01:00
|
|
|
pw_client_set_busy(client, true);
|
2018-10-29 08:46:09 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
blacklisted:
|
2019-02-14 16:53:42 +01:00
|
|
|
pw_resource_error(pw_client_get_core_resource(client), res, "blacklisted");
|
2018-10-29 08:46:09 +00:00
|
|
|
pw_client_update_properties(client, &SPA_DICT_INIT(items, 1));
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
2018-10-27 17:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_core_events core_events = {
|
|
|
|
|
PW_VERSION_CORE_EVENTS,
|
2018-10-29 08:46:09 +00:00
|
|
|
.check_access = core_check_access,
|
2018-10-27 17:33:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-06 13:24:41 +01:00
|
|
|
SPA_EXPORT
|
2018-10-29 08:46:09 +00:00
|
|
|
int pipewire__module_init(struct pw_module *module, const char *args)
|
2018-10-27 17:33:21 +01:00
|
|
|
{
|
|
|
|
|
struct pw_core *core = pw_module_get_core(module);
|
2018-10-29 08:46:09 +00:00
|
|
|
struct pw_properties *props;
|
2018-10-27 17:33:21 +01:00
|
|
|
struct impl *impl;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
2018-10-29 08:46:09 +00:00
|
|
|
pw_log_debug("module %p: new %s", impl, args);
|
|
|
|
|
|
|
|
|
|
if (args)
|
|
|
|
|
props = pw_properties_new_string(args);
|
|
|
|
|
else
|
|
|
|
|
props = NULL;
|
2018-10-27 17:33:21 +01:00
|
|
|
|
|
|
|
|
impl->core = core;
|
2018-10-29 08:46:09 +00:00
|
|
|
impl->properties = props;
|
2018-10-27 17:33:21 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|