Debian Codegraph @

src/validate-sound.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright © 2018 Red Hat, Inc
3 * Copyright © 2024 GNOME Foundation Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Authors:
19 * Matthias Clasen <[email protected]>
20 * Julian Sparber <[email protected]>
21 */
22
23 /* This is based on src/validate-icon.c */
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <gst/gst.h>
28 #include <gst/pbutils/pbutils.h>
29 #include <glib/gstdio.h>
30 #include <unistd.h>
31
32 #ifdef __FreeBSD__
33 #define execvpe exect
34 #endif
35
36 #define SOUND_VALIDATOR_GROUP "Sound Validator"
37
38 static int
39 validate_sound (int input_fd)
40 {
41 g_autoptr(GKeyFile) key_file = NULL;
42 g_autofree char *key_file_data = NULL;
43 g_autoptr(GError) error = NULL;
44 g_autoptr(GstDiscoverer) discoverer = NULL;
45 g_autoptr(GstDiscovererInfo) info = NULL;
46 g_autoptr(GstDiscovererStreamInfo) audio_info = NULL;
47 g_autoptr(GstDiscovererStreamInfo) stream_info = NULL;
48 g_autoptr(GstDiscovererStreamInfo) stream_next = NULL;
49 GstDiscovererResult result;
50 const gchar *format = NULL;
51 g_autofree gchar *uri = NULL;
52
53 gst_init (NULL, NULL);
54
55 discoverer = gst_discoverer_new (GST_SECOND, &error);
56
57 if (!discoverer)
58 {
59 g_printerr ("validate-sound: Failed to create gstreamer discoverer: %s\n", error->message);
60 return 1;
61 }
62
63 uri = g_strdup_printf ("file:///proc/self/fd/%d", input_fd);
64 info = gst_discoverer_discover_uri (discoverer, uri, &error);
65 result = gst_discoverer_info_get_result (info);
66
67 switch (result)
68 {
69 case GST_DISCOVERER_URI_INVALID:
70 g_assert_not_reached ();
71 return 1;
72 case GST_DISCOVERER_ERROR:
73 g_printerr ("validate-sound: Couldn't discover media type: %s\n", error->message);
74 return 1;
75 case GST_DISCOVERER_TIMEOUT:
76 g_printerr ("validate-sound: Couldn't discover media type: Timeout\n");
77 return 1;
78 case GST_DISCOVERER_BUSY:
79 g_printerr ("validate-sound: Couldn't discover media type: Busy\n");
80 return 1;
81 case GST_DISCOVERER_MISSING_PLUGINS:
82 {
83 g_autofree char *str = NULL;
84
85 str = g_strjoinv ("\n",
86 (char **) gst_discoverer_info_get_missing_elements_installer_details (info));
87
88 g_printerr ("validate-sound: Couldn't discover media type: Missing plugins: %s\n", str);
89 return 1;
90 }
91 case GST_DISCOVERER_OK:
92 break;
93 }
94
95 stream_info = gst_discoverer_info_get_stream_info (info);
96 if (!stream_info)
97 {
98 g_printerr ("validate-sound: Contains a invalid stream\n");
99 return 1;
100 }
101
102 stream_next = gst_discoverer_stream_info_get_next (stream_info);
103 if (stream_next)
104 {
105 g_printerr ("validate-sound: Only a single stream is allowed\n");
106 return 1;
107 }
108
109 if (GST_IS_DISCOVERER_CONTAINER_INFO (stream_info))
110 {
111 g_autoptr(GList) streams = NULL;
112 g_autoptr(GstCaps) container_caps = NULL;
113 GstStructure *structure = NULL;
114
115 container_caps = gst_discoverer_stream_info_get_caps (stream_info);
116 structure = gst_caps_get_structure (container_caps, 0);
117
118 if (!gst_caps_is_fixed (container_caps) || gst_caps_get_size (container_caps) != 1)
119 {
120 g_printerr ("validate-sound: The media format is to complex\n");
121 return 1;
122 }
123
124 if (!gst_structure_has_name (structure, "audio/ogg"))
125 {
126 g_printerr ("validate-sound: Unsupported container format\n");
127 return 1;
128 }
129
130 streams = gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO (stream_info));
131
132 if (streams->next)
133 {
134 g_printerr ("validate-sound: Only a single stream is allowed\n");
135 return 1;
136 }
137
138 audio_info = gst_discoverer_stream_info_ref (streams->data);
139 }
140 else
141 {
142 audio_info = g_steal_pointer (&stream_info);
143 }
144
145 if (GST_IS_DISCOVERER_AUDIO_INFO (audio_info))
146 {
147 g_autoptr(GstCaps) caps = NULL;
148 GstStructure *structure = NULL;
149
150 caps = gst_discoverer_stream_info_get_caps (audio_info);
151 structure = gst_caps_get_structure (caps, 0);
152
153 if (!gst_caps_is_fixed (caps) || gst_caps_get_size (caps) != 1)
154 {
155 g_printerr ("validate-sound: Media format is to complex\n");
156 return 1;
157 }
158
159 if (gst_structure_has_name (structure, "audio/x-wav"))
160 {
161 format = "wav/pcm";
162 }
163 else if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "audio/x-vorbis"))
164 {
165 format = "ogg/vorbis";
166 }
167 else if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "audio/x-opus"))
168 {
169 format = "ogg/opus";
170 }
171 }
172
173 if (format == NULL)
174 {
175 g_printerr ("validate-sound: Unsupported sound format\n");
176 return 1;
177 }
178
179 key_file = g_key_file_new ();
180 g_key_file_set_string (key_file, SOUND_VALIDATOR_GROUP, "format", format);
181 key_file_data = g_key_file_to_data (key_file, NULL, NULL);
182 g_print ("%s", key_file_data);
183
184 close (input_fd);
185
186 return 0;
187 }
188
189 #ifdef HELPER
190
191 G_GNUC_NULL_TERMINATED
192 static void
193 add_args (GPtrArray *argv_array, ...)
194 {
195 va_list args;
196 const char *arg;
197
198 va_start (args, argv_array);
199 while ((arg = va_arg (args, const gchar *)))
200 g_ptr_array_add (argv_array, g_strdup (arg));
201 va_end (args);
202 }
203
204 static gboolean
205 path_is_usrmerged (const char *dir)
206 {
207 /* does /dir point to /usr/dir? */
208 g_autofree char *target = NULL;
209 GStatBuf stat_buf_src, stat_buf_target;
210
211 if (g_stat (dir, &stat_buf_src) < 0)
212 return FALSE;
213
214 target = g_strdup_printf ("/usr/%s", dir);
215
216 if (g_stat (target, &stat_buf_target) < 0)
217 return FALSE;
218
219 return (stat_buf_src.st_dev == stat_buf_target.st_dev) &&
220 (stat_buf_src.st_ino == stat_buf_target.st_ino);
221 }
222
223 const char *
224 flatpak_get_bwrap (void)
225 {
226 const char *e = g_getenv ("FLATPAK_BWRAP");
227
228 if (e != NULL)
229 return e;
230
231 return HELPER;
232 }
233
234 static int
235 rerun_in_sandbox (int input_fd)
236 {
237 const char * const usrmerged_dirs[] = { "bin", "lib32", "lib64", "lib", "sbin" };
238 int i;
239 g_autoptr(GPtrArray) args = g_ptr_array_new_with_free_func (g_free);
240 char validate_sound[PATH_MAX + 1];
241 ssize_t symlink_size;
242 g_autofree char* arg_input_fd = NULL;
243
244 symlink_size = readlink ("/proc/self/exe", validate_sound, sizeof (validate_sound) - 1);
245 if (symlink_size < 0 || (size_t) symlink_size >= sizeof (validate_sound))
246 {
247 g_printerr ("Error: failed to read /proc/self/exe\n");
248 return 1;
249 }
250
251 validate_sound[symlink_size] = 0;
252
253 add_args (args,
254 flatpak_get_bwrap (),
255 "--unshare-ipc",
256 "--unshare-net",
257 "--unshare-pid",
258 "--tmpfs", "/tmp",
259 "--proc", "/proc",
260 "--dev", "/dev",
261 "--ro-bind", "/usr", "/usr",
262 "--ro-bind-try", "/etc/ld.so.cache", "/etc/ld.so.cache",
263 "--ro-bind", validate_sound, validate_sound,
264 NULL);
265
266 /* These directories might be symlinks into /usr/... */
267 for (i = 0; i < G_N_ELEMENTS (usrmerged_dirs); i++)
268 {
269 g_autofree char *absolute_dir = g_strdup_printf ("/%s", usrmerged_dirs[i]);
270
271 if (!g_file_test (absolute_dir, G_FILE_TEST_EXISTS))
272 continue;
273
274 if (path_is_usrmerged (absolute_dir))
275 {
276 g_autofree char *symlink_target = g_strdup_printf ("/usr/%s", absolute_dir);
277
278 add_args (args,
279 "--symlink", symlink_target, absolute_dir,
280 NULL);
281 }
282 else
283 {
284 add_args (args,
285 "--ro-bind", absolute_dir, absolute_dir,
286 NULL);
287 }
288 }
289
290 add_args (args,
291 "--chdir", "/",
292 "--setenv", "GIO_USE_VFS", "local",
293 "--unsetenv", "TMPDIR",
294 "--die-with-parent",
295 NULL);
296
297 if (g_getenv ("G_MESSAGES_DEBUG"))
298 add_args (args, "--setenv", "G_MESSAGES_DEBUG", g_getenv ("G_MESSAGES_DEBUG"), NULL);
299 if (g_getenv ("G_MESSAGES_PREFIXED"))
300 add_args (args, "--setenv", "G_MESSAGES_PREFIXED", g_getenv ("G_MESSAGES_PREFIXED"), NULL);
301
302
303 arg_input_fd = g_strdup_printf ("%d", input_fd);
304 add_args (args, validate_sound, "--fd", arg_input_fd, NULL);
305 g_ptr_array_add (args, NULL);
306
307 execvpe (flatpak_get_bwrap (), (char **) args->pdata, NULL);
308 /* If we get here, then execvpe() failed. */
309 g_printerr ("Sound validation: execvpe %s: %s\n", flatpak_get_bwrap (), g_strerror (errno));
310 return 1;
311 }
312 #endif
313
314 static gboolean opt_sandbox;
315 static gchar *opt_path = NULL;
316 static gint opt_fd = -1;
317
318 static GOptionEntry entries[] = {
319 { "sandbox", 0, 0, G_OPTION_ARG_NONE, &opt_sandbox, "Run in a sandbox", NULL },
320 { "path", 0, 0, G_OPTION_ARG_FILENAME, &opt_path, "Read sound data from given file path", "PATH" },
321 { "fd", 0, 0, G_OPTION_ARG_INT, &opt_fd, "Read sound data from given file descriptor", "FD" },
322 { NULL }
323 };
324
325 int
326 main (int argc, char *argv[])
327 {
328 g_autoptr(GOptionContext) context = NULL;
329 g_autoptr(GError) error = NULL;
330
331 context = g_option_context_new (NULL);
332 g_option_context_add_main_entries (context, entries, NULL);
333 if (!g_option_context_parse (context, &argc, &argv, &error))
334 {
335 g_printerr ("Error: %s\n", error->message);
336 return 1;
337 }
338
339 if (opt_path != NULL && opt_fd != -1)
340 {
341 g_printerr ("Error: Only --path or --fd can be given\n");
342 return 1;
343 }
344
345 if (opt_path)
346 {
347 opt_fd = g_open (opt_path, O_RDONLY, 0);
348 if (opt_fd == -1)
349 {
350 g_printerr ("Error: Couldn't open file\n");
351 return 1;
352 }
353 }
354 else if (opt_fd == -1)
355 {
356 g_autofree char *help = NULL;
357
358 help = g_option_context_get_help (context, TRUE, NULL);
359 g_printerr ("Error: Either --path or --fd needs to be given\n\n%s", help);
360 return 1;
361 }
362
363 #ifdef HELPER
364 if (opt_sandbox)
365 return rerun_in_sandbox (opt_fd);
366 else
367 #endif
368 return validate_sound (opt_fd);
369 }