Debian Codegraph @

src/validate-icon.c

raw ◀ ▶
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 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors:
18 * Matthias Clasen <[email protected]>
19 * Julian Sparber <[email protected]>
20 */
21
22 /* The canonical copy of this file is in:
23 * - https://github.com/flatpak/flatpak at icon-validator/validate-icon.c
24 * Known copies of this file are in:
25 * - https://github.com/flatpak/xdg-desktop-portal at src/validate-icon.c
26 */
27
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib/gstdio.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #include "xdp-utils.h"
35
36 #ifdef __FreeBSD__
37 #define execvpe exect
38 #endif
39
40 #define ICON_VALIDATOR_GROUP "Icon Validator"
41
42 typedef struct
43 {
44 const char *name;
45 size_t max_icon_size;
46 size_t max_svg_icon_size;
47 size_t max_file_size;
48 } XdpValidatorRuleset;
49
50 static const XdpValidatorRuleset rulesets[] =
51 {
52 {
53 .name = "desktop",
54 .max_icon_size = 512,
55 .max_svg_icon_size = 4096,
56 .max_file_size = 1024 * 1024 * 4 /* 4MB */,
57 },
58 {
59 .name = "notification",
60 .max_icon_size = 512,
61 .max_svg_icon_size = 4096,
62 .max_file_size = 1024 * 1024 * 4 /* 4MB */,
63 },
64 };
65
66 static const XdpValidatorRuleset *ruleset = NULL;
67 static gboolean opt_sandbox;
68 static char *opt_path = NULL;
69 static int opt_fd = -1;
70
71 static gboolean
72 option_validator_cb (const gchar *option_name,
73 const gchar *value,
74 gpointer data,
75 GError **error)
76 {
77 for (size_t i = 0; i < G_N_ELEMENTS (rulesets); i++)
78 {
79 if (g_strcmp0 (value, rulesets[i].name) == 0)
80 {
81 ruleset = &rulesets[i];
82 return TRUE;
83 }
84 }
85
86 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
87 "Invalid ruleset '%s'. Accepted values are: desktop, notification",
88 value);
89 return FALSE;
90 }
91
92 static GOptionEntry entries[] = {
93 { "sandbox", 0, 0, G_OPTION_ARG_NONE, &opt_sandbox, "Run in a sandbox", NULL },
94 { "path", 0, 0, G_OPTION_ARG_FILENAME, &opt_path, "Read icon data from given file path. Required to be from a trusted source.", "PATH" },
95 { "fd", 0, 0, G_OPTION_ARG_INT, &opt_fd, "Read icon data from given file descriptor. Required to be from a trusted source or to be sealed", "FD" },
96 { "ruleset", 0, 0, G_OPTION_ARG_CALLBACK, &option_validator_cb, "The icon validator ruleset to apply. Accepted values: desktop, notification", "RULESET" },
97 { NULL }
98 };
99
100 static int
101 validate_icon (int input_fd)
102 {
103 const char *allowed_formats[] = { "png", "jpeg", "svg", NULL };
104 g_autoptr(GBytes) bytes = NULL;
105 g_autoptr(GError) error = NULL;
106 GdkPixbufFormat *format;
107 g_autoptr(GKeyFile) key_file = NULL;
108 g_autofree char *key_file_data = NULL;
109 g_autoptr(GdkPixbufLoader) loader = NULL;
110 g_autoptr(GMappedFile) mapped = NULL;
111 int max_size, width, height;
112 g_autofree char *name = NULL;
113 GdkPixbuf *pixbuf;
114
115 g_assert (ruleset != NULL);
116
117 /* Ensure that we read from the beginning of the file */
118 lseek (input_fd, 0, SEEK_SET);
119
120 mapped = g_mapped_file_new_from_fd (input_fd, FALSE, &error);
121 if (!mapped)
122 {
123 g_printerr ("Failed to create mapped file for image: %s\n", error->message);
124 return 1;
125 }
126
127 bytes = g_mapped_file_get_bytes (mapped);
128
129 if (g_bytes_get_size (bytes) == 0)
130 {
131 g_printerr ("Image is 0 bytes\n");
132 return 1;
133 }
134
135 if (g_bytes_get_size (bytes) > ruleset->max_file_size)
136 {
137 g_printerr ("Image is bigger then the allowed size\n");
138 return 1;
139 }
140
141 loader = gdk_pixbuf_loader_new ();
142
143 if (!gdk_pixbuf_loader_write_bytes (loader, bytes, &error))
144 {
145 g_printerr ("Failed to load image: %s\n", error->message);
146 gdk_pixbuf_loader_close (loader, NULL);
147 return 1;
148 }
149
150 if (!gdk_pixbuf_loader_close (loader, &error))
151 {
152 g_printerr ("Failed to load image: %s\n", error->message);
153 return 1;
154 }
155
156 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
157 if (!pixbuf)
158 {
159 g_printerr ("Failed to load image: %s\n", error->message);
160 return 1;
161 }
162
163 format = gdk_pixbuf_loader_get_format (loader);
164 if (!format)
165 {
166 g_printerr ("Image format not recognized\n");
167 return 1;
168 }
169
170 name = gdk_pixbuf_format_get_name (format);
171 if (!g_strv_contains (allowed_formats, name))
172 {
173 g_printerr ("Image format %s not accepted\n", name);
174 return 1;
175 }
176
177 width = gdk_pixbuf_get_width (pixbuf);
178 height = gdk_pixbuf_get_height (pixbuf);
179
180 if (width != height)
181 {
182 g_printerr ("Expected a square image but got: %dx%d\n", width, height);
183 return 1;
184 }
185
186 /* Sanity check for vector files */
187 max_size = g_str_equal (name, "svg") ? ruleset->max_svg_icon_size : ruleset->max_icon_size;
188
189 /* The icon is a square so we only need to check one side */
190 if (width > max_size)
191 {
192 g_printerr ("Image too large (%dx%d). Max. size %dx%d\n", width, height, max_size, max_size);
193 return 1;
194 }
195
196 /* Print the format and size for consumption by (at least) the dynamic
197 * launcher portal. Use a GKeyFile so the output can be easily extended
198 * in the future in a backwards compatible way.
199 */
200 key_file = g_key_file_new ();
201 g_key_file_set_string (key_file, ICON_VALIDATOR_GROUP, "format", name);
202 g_key_file_set_integer (key_file, ICON_VALIDATOR_GROUP, "width", width);
203 key_file_data = g_key_file_to_data (key_file, NULL, NULL);
204 g_print ("%s", key_file_data);
205
206 return 0;
207 }
208
209 #ifdef HELPER
210
211 G_GNUC_NULL_TERMINATED
212 static void
213 add_args (GPtrArray *argv_array, ...)
214 {
215 va_list args;
216 const char *arg;
217
218 va_start (args, argv_array);
219 while ((arg = va_arg (args, const gchar *)))
220 g_ptr_array_add (argv_array, g_strdup (arg));
221 va_end (args);
222 }
223
224 static gboolean
225 path_is_usrmerged (const char *dir)
226 {
227 /* does /dir point to /usr/dir? */
228 g_autofree char *target = NULL;
229 GStatBuf stat_buf_src, stat_buf_target;
230
231 if (g_stat (dir, &stat_buf_src) < 0)
232 return FALSE;
233
234 target = g_strdup_printf ("/usr/%s", dir);
235
236 if (g_stat (target, &stat_buf_target) < 0)
237 return FALSE;
238
239 return (stat_buf_src.st_dev == stat_buf_target.st_dev) &&
240 (stat_buf_src.st_ino == stat_buf_target.st_ino);
241 }
242
243 const char *
244 flatpak_get_bwrap (void)
245 {
246 const char *e = g_getenv ("FLATPAK_BWRAP");
247
248 if (e != NULL)
249 return e;
250
251 return HELPER;
252 }
253
254 static int
255 rerun_in_sandbox (int input_fd)
256 {
257 const char * const usrmerged_dirs[] = { "bin", "lib32", "lib64", "lib", "sbin" };
258 g_autoptr(GPtrArray) args = g_ptr_array_new_with_free_func (g_free);
259 g_autofree char* arg_input_fd = NULL;
260 char validate_icon[PATH_MAX + 1];
261 ssize_t symlink_size;
262
263 g_assert (ruleset != NULL);
264
265 symlink_size = readlink ("/proc/self/exe", validate_icon, sizeof (validate_icon) - 1);
266 if (symlink_size < 0 || (size_t) symlink_size >= sizeof (validate_icon))
267 {
268 g_printerr ("Error: failed to read /proc/self/exe\n");
269 return 1;
270 }
271
272 validate_icon[symlink_size] = 0;
273
274 add_args (args,
275 flatpak_get_bwrap (),
276 "--unshare-ipc",
277 "--unshare-net",
278 "--unshare-pid",
279 "--tmpfs", "/tmp",
280 "--proc", "/proc",
281 "--dev", "/dev",
282 "--ro-bind", "/usr", "/usr",
283 "--ro-bind-try", "/etc/ld.so.cache", "/etc/ld.so.cache",
284 "--ro-bind", validate_icon, validate_icon,
285 NULL);
286
287 /* These directories might be symlinks into /usr/... */
288 for (size_t i = 0; i < G_N_ELEMENTS (usrmerged_dirs); i++)
289 {
290 g_autofree char *absolute_dir = g_strdup_printf ("/%s", usrmerged_dirs[i]);
291
292 if (!g_file_test (absolute_dir, G_FILE_TEST_EXISTS))
293 continue;
294
295 if (path_is_usrmerged (absolute_dir))
296 {
297 g_autofree char *symlink_target = g_strdup_printf ("/usr/%s", absolute_dir);
298
299 add_args (args,
300 "--symlink", symlink_target, absolute_dir,
301 NULL);
302 }
303 else
304 {
305 add_args (args,
306 "--ro-bind", absolute_dir, absolute_dir,
307 NULL);
308 }
309 }
310
311 add_args (args,
312 "--chdir", "/",
313 "--setenv", "GIO_USE_VFS", "local",
314 "--unsetenv", "TMPDIR",
315 "--die-with-parent",
316 NULL);
317
318 if (g_getenv ("G_MESSAGES_DEBUG"))
319 add_args (args, "--setenv", "G_MESSAGES_DEBUG", g_getenv ("G_MESSAGES_DEBUG"), NULL);
320 if (g_getenv ("G_MESSAGES_PREFIXED"))
321 add_args (args, "--setenv", "G_MESSAGES_PREFIXED", g_getenv ("G_MESSAGES_PREFIXED"), NULL);
322
323 arg_input_fd = g_strdup_printf ("%d", input_fd);
324 add_args (args,
325 validate_icon,
326 "--fd", arg_input_fd,
327 "--ruleset", ruleset->name,
328 NULL);
329 g_ptr_array_add (args, NULL);
330
331 execvpe (flatpak_get_bwrap (), (char **) args->pdata, NULL);
332 /* If we get here, then execvpe() failed. */
333 g_printerr ("Icon validation: execvpe %s: %s\n", flatpak_get_bwrap (), g_strerror (errno));
334 return 1;
335 }
336 #endif
337
338 int
339 main (int argc, char *argv[])
340 {
341 g_autoptr(GOptionContext) context = NULL;
342 g_autoptr(GError) error = NULL;
343 g_autofd int fd_path = -1;
344
345 context = g_option_context_new (NULL);
346 g_option_context_add_main_entries (context, entries, NULL);
347 if (!g_option_context_parse (context, &argc, &argv, &error))
348 {
349 g_printerr ("Error: %s\n", error->message);
350 return 1;
351 }
352
353 if (ruleset == NULL)
354 {
355 g_printerr ("Error: A ruleset must be given with --ruleset\n");
356 return 1;
357 }
358
359 if (opt_path != NULL && opt_fd != -1)
360 {
361 g_printerr ("Error: Only --path or --fd can be given\n");
362 return 1;
363 }
364
365 if (opt_path)
366 {
367 opt_fd = fd_path = g_open (opt_path, O_RDONLY, 0);
368 if (opt_fd == -1)
369 {
370 g_printerr ("Error: Couldn't open file\n");
371 return 1;
372 }
373 }
374 else if (opt_fd == -1)
375 {
376 g_autofree char *help = NULL;
377
378 help = g_option_context_get_help (context, TRUE, NULL);
379 g_printerr ("Error: Either --path or --fd needs to be given\n\n%s", help);
380 return 1;
381 }
382
383 #ifdef HELPER
384 if (opt_sandbox)
385 return rerun_in_sandbox (opt_fd);
386 else
387 #endif
388 return validate_icon (opt_fd);
389 }