Debian Codegraph @

modules/flatpak-utils.h

raw
1 /* PipeWire */
2 /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4
5 #ifndef FLATPAK_UTILS_H
6 #define FLATPAK_UTILS_H
7
8 #define HAVE_GLIB2
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #ifdef HAVE_SYS_VFS_H
16 #include <sys/vfs.h>
17 #endif
18
19 #ifdef HAVE_GLIB2
20 #include <glib.h>
21 #endif
22
23 #include <spa/utils/cleanup.h>
24 #include <spa/utils/result.h>
25 #include <pipewire/log.h>
26
27 static int pw_check_flatpak_parse_metadata(const char *buf, size_t size, char **app_id, char **instance_id, char **devices)
28 {
29 #ifdef HAVE_GLIB2
30 /*
31 * See flatpak-metadata(5)
32 *
33 * The .flatpak-info file is in GLib key_file .ini format.
34 */
35 g_autoptr(GKeyFile) metadata = NULL;
36 char *s;
37
38 metadata = g_key_file_new();
39 if (!g_key_file_load_from_data(metadata, buf, size, G_KEY_FILE_NONE, NULL))
40 return -EINVAL;
41
42 if (app_id) {
43 s = g_key_file_get_value(metadata, "Application", "name", NULL);
44 *app_id = s ? strdup(s) : NULL;
45 g_free(s);
46 }
47
48 if (devices) {
49 s = g_key_file_get_value(metadata, "Context", "devices", NULL);
50 *devices = s ? strdup(s) : NULL;
51 g_free(s);
52 }
53
54 if (instance_id) {
55 s = g_key_file_get_value(metadata, "Instance", "instance-id", NULL);
56 *instance_id = s ? strdup(s) : NULL;
57 g_free(s);
58 }
59
60 return 0;
61 #else
62 return -ENOTSUP;
63 #endif
64 }
65
66 static int pw_check_flatpak(pid_t pid, char **app_id, char **instance_id, char **devices)
67 {
68 #if defined(__linux__)
69 char root_path[2048];
70 struct stat stat_buf;
71 int res;
72
73 if (app_id)
74 *app_id = NULL;
75 if (instance_id)
76 *instance_id = NULL;
77 if (devices)
78 *devices = NULL;
79
80 snprintf(root_path, sizeof(root_path), "/proc/%d/root", (int)pid);
81
82 spa_autoclose int root_fd = openat(AT_FDCWD, root_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY);
83 if (root_fd < 0) {
84 res = -errno;
85 pw_log_info("failed to open \"%s\": %s", root_path, spa_strerror(res));
86
87 if (res == -EACCES) {
88 /* If we can't access the root filesystem, consider not sandboxed.
89 * This should not happen but for now it is a workaround for selinux
90 * where we can't access the gnome-shell root when it connects for
91 * screen sharing.
92 */
93 return 0;
94 }
95
96 /* Not able to open the root dir shouldn't happen. Probably the app died and
97 * we're failing due to /proc/$pid not existing. In that case fail instead
98 * of treating this as privileged. */
99 return res;
100 }
101
102 spa_autoclose int info_fd = openat(root_fd, ".flatpak-info", O_RDONLY | O_CLOEXEC | O_NOCTTY);
103 if (info_fd < 0) {
104 if (errno == ENOENT) {
105 pw_log_debug("no .flatpak-info, client on the host");
106 /* No file => on the host */
107 return 0;
108 }
109 res = -errno;
110 pw_log_error("error opening .flatpak-info: %m");
111 return res;
112 }
113 if (fstat (info_fd, &stat_buf) != 0 || !S_ISREG (stat_buf.st_mode)) {
114 /* Some weird fd => failure, assume sandboxed */
115 pw_log_error("error fstat .flatpak-info: %m");
116 } else if (app_id || instance_id || devices) {
117 /* Parse the application ID if needed */
118 const size_t size = stat_buf.st_size;
119
120 if (size > 0) {
121 void *buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, info_fd, 0);
122 if (buf != MAP_FAILED) {
123 res = pw_check_flatpak_parse_metadata(buf, size, app_id, instance_id, devices);
124 munmap(buf, size);
125 } else {
126 res = -errno;
127 }
128 } else {
129 res = -EINVAL;
130 }
131
132 if (res == -EINVAL)
133 pw_log_error("PID %d .flatpak-info file is malformed",
134 (int)pid);
135 else if (res < 0)
136 pw_log_error("PID %d .flatpak-info parsing failed: %s",
137 (int)pid, spa_strerror(res));
138 }
139
140 return 1;
141 #else
142 return 0;
143 #endif
144 }
145
146 #endif /* FLATPAK_UTILS_H */