Newer
Older
/* pressure-vessel-wrap — run a program in a container that protects $HOME,
* optionally using a Flatpak-style runtime.
*
* Contains code taken from Flatpak.
*
* Copyright © 2014-2019 Red Hat, Inc
* Copyright © 2017-2019 Collabora Ltd.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "subprojects/libglnx/config.h"
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <sysexits.h>
/* Include these before steam-runtime-tools.h so that their backport of
* G_DEFINE_AUTOPTR_CLEANUP_FUNC will be visible to it */
#include "glib-backports.h"
#include "libglnx.h"
#include <steam-runtime-tools/steam-runtime-tools.h>
#include "bwrap.h"
#include "flatpak-bwrap-private.h"
#include "flatpak-run-private.h"
#include "flatpak-utils-private.h"
/*
* Supported Debian-style multiarch tuples
*/
static const char * const multiarch_tuples[] =
{
"x86_64-linux-gnu",
/*
* Directories other than /usr/lib that we must search for loadable
* modules, in the same order as multiarch_tuples
*/
static const char * const libquals[] =
{
"lib64",
"lib32"
};
static gchar *
find_executable_dir (GError **error)
{
g_autofree gchar *target = glnx_readlinkat_malloc (-1, "/proc/self/exe",
NULL, error);
if (target == NULL)
return glnx_prefix_error_null (error, "Unable to resolve /proc/self/exe");
return g_path_get_dirname (target);
}
static void
search_path_append (GString *search_path,
const gchar *item)
{
if (item == NULL || item[0] == '\0')
return;
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
if (search_path->len != 0)
g_string_append (search_path, ":");
g_string_append (search_path, item);
}
static gchar *
find_bwrap (const char *tools_dir)
{
static const char * const flatpak_libexecdirs[] =
{
"/usr/local/libexec",
"/usr/libexec",
"/usr/lib/flatpak"
};
const char *tmp;
g_autofree gchar *candidate = NULL;
g_autofree gchar *dirname = NULL;
gsize i;
g_return_val_if_fail (tools_dir != NULL, NULL);
tmp = g_getenv ("BWRAP");
if (tmp != NULL)
return g_strdup (tmp);
candidate = g_find_program_in_path ("bwrap");
if (candidate != NULL)
return g_steal_pointer (&candidate);
for (i = 0; i < G_N_ELEMENTS (flatpak_libexecdirs); i++)
{
candidate = g_build_filename (flatpak_libexecdirs[i],
"flatpak-bwrap", NULL);
if (g_file_test (candidate, G_FILE_TEST_IS_EXECUTABLE))
return g_steal_pointer (&candidate);
else
g_clear_pointer (&candidate, g_free);
}
candidate = g_build_filename (tools_dir, "bwrap", NULL);
if (g_file_test (candidate, G_FILE_TEST_IS_EXECUTABLE))
return g_steal_pointer (&candidate);
else
g_clear_pointer (&candidate, g_free);
return NULL;
}
static gchar *
check_bwrap (const char *tools_dir)
{
g_autoptr(GError) local_error = NULL;
GError **error = &local_error;
g_autofree gchar *bwrap_executable = find_bwrap (tools_dir);
const char *bwrap_test_argv[] =
{
NULL,
"--bind", "/", "/",
"true",
NULL
};
g_return_val_if_fail (tools_dir != NULL, NULL);
if (bwrap_executable == NULL)
{
g_warning ("Cannot find bwrap");
}
else
{
int exit_status;
g_autofree gchar *child_stdout = NULL;
g_autofree gchar *child_stderr = NULL;
bwrap_test_argv[0] = bwrap_executable;
if (!g_spawn_sync (NULL, /* cwd */
(gchar **) bwrap_test_argv,
NULL, /* environ */
G_SPAWN_DEFAULT,
NULL, NULL, /* child setup */
&child_stdout,
&child_stderr,
&exit_status,
error))
{
g_warning ("Cannot run bwrap: %s", local_error->message);
g_clear_error (&local_error);
}
else if (exit_status != 0)
{
g_warning ("Cannot run bwrap: exit status %d", exit_status);
if (child_stdout != NULL && child_stdout[0] != '\0')
g_warning ("Output:\n%s", child_stdout);
if (child_stderr != NULL && child_stderr[0] != '\0')
g_warning ("Diagnostic output:\n%s", child_stderr);
}
else
{
return g_steal_pointer (&bwrap_executable);
}
}
return NULL;
}
static gchar *
capture_output (const char * const * argv,
GError **error)
{
gsize len;
gint exit_status;
g_autofree gchar *output = NULL;
g_autofree gchar *errors = NULL;
gsize i;
g_autoptr(GString) command = g_string_new ("");
for (i = 0; argv[i] != NULL; i++)
{
g_autofree gchar *quoted = g_shell_quote (argv[i]);
g_string_append_printf (command, " %s", quoted);
}
g_debug ("run:%s", command->str);
if (!g_spawn_sync (NULL, /* cwd */
(char **) argv,
NULL, /* env */
G_SPAWN_SEARCH_PATH,
NULL, NULL, /* child setup */
&output,
&errors,
&exit_status,
error))
return NULL;
g_printerr ("%s", errors);
if (!g_spawn_check_exit_status (exit_status, error))
return NULL;
len = strlen (output);
/* Emulate shell $() */
if (len > 0 && output[len - 1] == '\n')
output[len - 1] = '\0';
g_debug ("-> %s", output);
return g_steal_pointer (&output);
}
static gboolean
try_bind_dri (FlatpakBwrap *bwrap,
FlatpakBwrap *mount_runtime_on_scratch,
const char *overrides,
const char *scratch,
const char *tool_path,
const char *libdir,
const char *libdir_on_host,
GError **error)
{
g_autofree gchar *dri = g_build_filename (libdir, "dri", NULL);
g_autofree gchar *s2tc = g_build_filename (libdir, "libtxc_dxtn.so", NULL);
/* mount_runtime_on_scratch can't own any fds, because if it did,
* flatpak_bwrap_append_bwrap() would steal them. */
g_return_val_if_fail (mount_runtime_on_scratch->fds == NULL
|| mount_runtime_on_scratch->fds->len == 0,
FALSE);
if (g_file_test (dri, G_FILE_TEST_IS_DIR))
{
g_autoptr(FlatpakBwrap) temp_bwrap = NULL;
g_autofree gchar *expr = NULL;
g_autofree gchar *host_dri = NULL;
g_autofree gchar *dest_dri = NULL;
expr = g_strdup_printf ("only-dependencies:if-exists:path-match:%s/dri/*.so",
libdir);
temp_bwrap = flatpak_bwrap_new (NULL);
g_warn_if_fail (mount_runtime_on_scratch->fds == NULL
|| mount_runtime_on_scratch->fds->len == 0);
flatpak_bwrap_append_bwrap (temp_bwrap, mount_runtime_on_scratch);
flatpak_bwrap_add_args (temp_bwrap,
tool_path,
"--container", scratch,
"--link-target", "/run/host",
"--dest", libdir_on_host,
"--provider", "/",
expr,
NULL);
flatpak_bwrap_finish (temp_bwrap);
if (!pv_bwrap_run_sync (temp_bwrap, NULL, error))
return FALSE;
g_clear_pointer (&temp_bwrap, flatpak_bwrap_free);
host_dri = g_build_filename ("/run/host", libdir, "dri", NULL);
dest_dri = g_build_filename (libdir_on_host, "dri", NULL);
temp_bwrap = flatpak_bwrap_new (NULL);
flatpak_bwrap_add_args (temp_bwrap,
bwrap->argv->pdata[0],
"--ro-bind", "/", "/",
"--tmpfs", "/run",
"--ro-bind", "/", "/run/host",
"--bind", overrides, overrides,
"sh", "-c",
"ln -fns \"$1\"/* \"$2\"",
"sh", /* $0 */
host_dri,
dest_dri,
NULL);
flatpak_bwrap_finish (temp_bwrap);
if (!pv_bwrap_run_sync (temp_bwrap, NULL, error))
return FALSE;
}
if (g_file_test (s2tc, G_FILE_TEST_EXISTS))
{
g_autoptr(FlatpakBwrap) temp_bwrap = NULL;
g_autofree gchar *expr = NULL;
expr = g_strdup_printf ("path-match:%s", s2tc);
temp_bwrap = flatpak_bwrap_new (NULL);
g_warn_if_fail (mount_runtime_on_scratch->fds == NULL
|| mount_runtime_on_scratch->fds->len == 0);
flatpak_bwrap_append_bwrap (temp_bwrap, mount_runtime_on_scratch);
flatpak_bwrap_add_args (temp_bwrap,
tool_path,
"--container", scratch,
"--link-target", "/run/host",
"--dest", libdir_on_host,
"--provider", "/",
expr,
NULL);
flatpak_bwrap_finish (temp_bwrap);
if (!pv_bwrap_run_sync (temp_bwrap, NULL, error))
return FALSE;
}
return TRUE;
}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Try to make sure we have all the locales we need, by running
* the helper from steam-runtime-tools in the container. If this
* fails, it isn't fatal - carry on anyway.
*
* @bwrap must be set up to have the same libc that we will be using
* for the container.
*/
static void
ensure_locales (gboolean on_host,
const char *tools_dir,
FlatpakBwrap *bwrap,
const char *overrides)
{
g_autoptr(GError) local_error = NULL;
g_autoptr(FlatpakBwrap) run_locale_gen = NULL;
g_autofree gchar *locale_gen = NULL;
g_autofree gchar *locales = g_build_filename (overrides, "locales", NULL);
g_autoptr(GDir) dir = NULL;
int exit_status;
/* bwrap can't own any fds yet, because if it did,
* flatpak_bwrap_append_bwrap() would steal them. */
g_return_if_fail (bwrap->fds == NULL || bwrap->fds->len == 0);
g_mkdir (locales, 0700);
run_locale_gen = flatpak_bwrap_new (NULL);
if (on_host)
{
locale_gen = g_build_filename (tools_dir,
"pressure-vessel-locale-gen",
NULL);
flatpak_bwrap_add_args (run_locale_gen,
bwrap->argv->pdata[0],
"--ro-bind", "/", "/",
NULL);
pv_bwrap_add_api_filesystems (run_locale_gen);
flatpak_bwrap_add_args (run_locale_gen,
"--bind", locales, locales,
"--chdir", locales,
locale_gen,
"--verbose",
NULL);
}
else
{
locale_gen = g_build_filename ("/run/host/tools",
"pressure-vessel-locale-gen",
NULL);
flatpak_bwrap_append_bwrap (run_locale_gen, bwrap);
pv_bwrap_copy_tree (run_locale_gen, overrides, "/overrides");
if (!flatpak_bwrap_bundle_args (run_locale_gen, 1, -1, FALSE,
&local_error))
{
g_warning ("Unable to set up locale-gen command: %s",
local_error->message);
g_clear_error (&local_error);
}
flatpak_bwrap_add_args (run_locale_gen,
"--ro-bind", tools_dir, "/run/host/tools",
"--bind", locales, "/overrides/locales",
"--chdir", "/overrides/locales",
locale_gen,
"--verbose",
NULL);
}
flatpak_bwrap_finish (run_locale_gen);
/* locale-gen exits 72 (EX_OSFILE) if it had to correct for
* missing locales at OS level. This is not an error. */
if (!pv_bwrap_run_sync (run_locale_gen, &exit_status, &local_error))
{
if (exit_status == EX_OSFILE)
g_debug ("pressure-vessel-locale-gen created missing locales");
else
g_warning ("Unable to generate locales: %s", local_error->message);
g_clear_error (&local_error);
}
else
{
g_debug ("No locales generated");
}
dir = g_dir_open (locales, 0, NULL);
/* If the directory is not empty, make it the container's LOCPATH */
if (dir != NULL && g_dir_read_name (dir) != NULL)
{
g_autoptr(GString) locpath = NULL;
g_debug ("%s is non-empty", locales);
locpath = g_string_new ("/overrides/locales");
search_path_append (locpath, g_getenv ("LOCPATH"));
flatpak_bwrap_add_args (bwrap,
"--setenv", "LOCPATH", locpath->str,
NULL);
}
else
{
g_debug ("%s is empty", locales);
}
}
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
static gboolean
bind_runtime (FlatpakBwrap *bwrap,
const char *tools_dir,
const char *runtime,
const char *overrides,
const char *scratch,
GError **error)
{
static const char * const bind_mutable[] =
{
"etc",
"var/cache",
"var/lib"
};
static const char * const dont_bind[] =
{
"/etc/group",
"/etc/passwd",
"/etc/host.conf",
"/etc/hosts",
"/etc/localtime",
"/etc/machine-id",
"/etc/resolv.conf",
"/var/lib/dbus",
"/var/lib/dhcp",
"/var/lib/sudo",
"/var/lib/urandom",
NULL
};
g_autofree gchar *xrd = g_strdup_printf ("/run/user/%ld", (long) geteuid ());
g_autofree gchar *usr = NULL;
gsize i, j;
const gchar *member;
g_autoptr(GString) dri_path = g_string_new ("");
gboolean any_architecture_works = FALSE;
gboolean any_libc_from_host = FALSE;
gboolean all_libc_from_host = TRUE;
g_autofree gchar *localedef = NULL;
g_return_val_if_fail (tools_dir != NULL, FALSE);
g_return_val_if_fail (runtime != NULL, FALSE);
g_return_val_if_fail (overrides != NULL, FALSE);
g_return_val_if_fail (scratch != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
usr = g_build_filename (runtime, "usr", NULL);
if (!pv_bwrap_bind_usr (bwrap, runtime, "/", error))
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
flatpak_bwrap_add_args (bwrap,
"--setenv", "XDG_RUNTIME_DIR", xrd,
"--tmpfs", "/run",
"--tmpfs", "/tmp",
"--tmpfs", "/var",
"--symlink", "../run", "/var/run",
NULL);
for (i = 0; i < G_N_ELEMENTS (bind_mutable); i++)
{
g_autofree gchar *path = g_build_filename (runtime, bind_mutable[i],
NULL);
g_autoptr(GDir) dir = NULL;
dir = g_dir_open (path, 0, NULL);
if (dir == NULL)
continue;
for (member = g_dir_read_name (dir);
member != NULL;
member = g_dir_read_name (dir))
{
g_autofree gchar *dest = g_build_filename ("/", bind_mutable[i],
member, NULL);
g_autofree gchar *full = NULL;
g_autofree gchar *target = NULL;
if (g_strv_contains (dont_bind, dest))
continue;
full = g_build_filename (runtime, bind_mutable[i], member, NULL);
target = glnx_readlinkat_malloc (-1, full, NULL, NULL);
if (target != NULL)
flatpak_bwrap_add_args (bwrap, "--symlink", target, dest, NULL);
else
flatpak_bwrap_add_args (bwrap, "--ro-bind", full, dest, NULL);
}
}
if (g_file_test ("/etc/machine-id", G_FILE_TEST_EXISTS))
{
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/etc/machine-id", "/etc/machine-id",
"--symlink", "/etc/machine-id",
"/var/lib/dbus/machine-id",
NULL);
}
else if (g_file_test ("/var/lib/dbus/machine-id", G_FILE_TEST_EXISTS))
{
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/var/lib/dbus/machine-id",
"/etc/machine-id",
"--symlink", "/etc/machine-id",
"/var/lib/dbus/machine-id",
NULL);
}
if (g_file_test ("/etc/resolv.conf", G_FILE_TEST_EXISTS))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/etc/resolv.conf", "/etc/resolv.conf",
NULL);
if (g_file_test ("/etc/host.conf", G_FILE_TEST_EXISTS))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/etc/host.conf", "/etc/host.conf",
NULL);
if (g_file_test ("/etc/hosts", G_FILE_TEST_EXISTS))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/etc/hosts", "/etc/hosts",
NULL);
/* TODO: Synthesize a passwd with only the user and nobody,
* like Flatpak does? */
if (g_file_test ("/etc/passwd", G_FILE_TEST_EXISTS))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/etc/passwd", "/etc/passwd",
NULL);
if (g_file_test ("/etc/group", G_FILE_TEST_EXISTS))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/etc/group", "/etc/group",
NULL);
g_assert (multiarch_tuples[G_N_ELEMENTS (multiarch_tuples) - 1] == NULL);
for (i = 0; i < G_N_ELEMENTS (multiarch_tuples) - 1; i++)
{
g_autofree gchar *tool = g_strdup_printf ("%s-capsule-capture-libs",
multiarch_tuples[i]);
g_autofree gchar *tool_path = NULL;
g_autofree gchar *ld_so = NULL;
const gchar *argv[] = { NULL, "--print-ld.so", NULL };
g_debug ("Checking for %s libraries...", multiarch_tuples[i]);
tool_path = g_build_filename (tools_dir, tool, NULL);
argv[0] = tool_path;
/* This has the side-effect of testing whether we can run binaries
* for this architecture on the host system. */
ld_so = capture_output (argv, NULL);
if (ld_so != NULL)
{
g_auto(GStrv) dirs = NULL;
g_autoptr(FlatpakBwrap) mount_runtime_on_scratch = NULL;
g_autoptr(FlatpakBwrap) temp_bwrap = NULL;
g_autofree gchar *libdir_in_container = g_build_filename ("/overrides",
"lib",
multiarch_tuples[i],
NULL);
g_autofree gchar *libdir_on_host = g_build_filename (overrides, "lib",
multiarch_tuples[i],
NULL);
g_autofree gchar *this_dri_path_on_host = g_build_filename (libdir_on_host,
"dri", NULL);
g_autofree gchar *this_dri_path_in_container = g_build_filename (libdir_in_container,
"dri", NULL);
g_autofree gchar *libc = NULL;
g_autofree gchar *ld_so_in_runtime = NULL;
const gchar *libqual = NULL;
temp_bwrap = flatpak_bwrap_new (NULL);
flatpak_bwrap_add_args (temp_bwrap,
bwrap->argv->pdata[0],
NULL);
if (!pv_bwrap_bind_usr (temp_bwrap, runtime, "/", error))
flatpak_bwrap_add_args (temp_bwrap,
"readlink", "-e", ld_so,
NULL);
flatpak_bwrap_finish (temp_bwrap);
ld_so_in_runtime = capture_output ((const char * const *) temp_bwrap->argv->pdata,
NULL);
g_clear_pointer (&temp_bwrap, flatpak_bwrap_free);
if (ld_so_in_runtime == NULL)
{
g_debug ("Container does not have %s so it cannot run "
"%s binaries", ld_so, multiarch_tuples[i]);
continue;
}
any_architecture_works = TRUE;
g_debug ("Container path: %s -> %s", ld_so, ld_so_in_runtime);
search_path_append (dri_path, this_dri_path_in_container);
g_mkdir_with_parents (libdir_on_host, 0755);
g_mkdir_with_parents (this_dri_path_on_host, 0755);
mount_runtime_on_scratch = flatpak_bwrap_new (NULL);
flatpak_bwrap_add_args (mount_runtime_on_scratch,
bwrap->argv->pdata[0],
"--ro-bind", "/", "/",
"--bind", overrides, overrides,
"--tmpfs", scratch,
NULL);
if (!pv_bwrap_bind_usr (mount_runtime_on_scratch, runtime, scratch,
temp_bwrap = flatpak_bwrap_new (NULL);
/* mount_runtime_on_scratch can't own any fds, because if it did,
* flatpak_bwrap_append_bwrap() would steal them. */
g_warn_if_fail (mount_runtime_on_scratch->fds == NULL
|| mount_runtime_on_scratch->fds->len == 0);
flatpak_bwrap_append_bwrap (temp_bwrap, mount_runtime_on_scratch);
flatpak_bwrap_add_args (temp_bwrap,
tool_path,
"--container", scratch,
"--link-target", "/run/host",
"--dest", libdir_on_host,
"--provider", "/",
"gl:",
NULL);
flatpak_bwrap_finish (temp_bwrap);
if (!pv_bwrap_run_sync (temp_bwrap, NULL, error))
g_clear_pointer (&temp_bwrap, flatpak_bwrap_free);
libc = g_build_filename (libdir_on_host, "libc.so.6", NULL);
/* If we are going to use the host system's libc6 (likely)
* then we have to use its ld.so too. */
if (g_file_test (libc, G_FILE_TEST_IS_SYMLINK))
{
g_autofree gchar *ld_so_in_host = NULL;
g_debug ("Making host ld.so visible in container");
ld_so_in_host = flatpak_canonicalize_filename (ld_so);
g_debug ("Host path: %s -> %s", ld_so, ld_so_in_host);
g_clear_pointer (&temp_bwrap, flatpak_bwrap_free);
temp_bwrap = flatpak_bwrap_new (NULL);
flatpak_bwrap_add_args (temp_bwrap,
bwrap->argv->pdata[0],
NULL);
if (!pv_bwrap_bind_usr (temp_bwrap, runtime, "/", error))
flatpak_bwrap_add_args (temp_bwrap,
"readlink", "-f", ld_so,
NULL);
flatpak_bwrap_finish (temp_bwrap);
g_debug ("Container path: %s -> %s", ld_so, ld_so_in_runtime);
flatpak_bwrap_add_args (bwrap,
"--ro-bind", ld_so_in_host,
ld_so_in_runtime,
/* Collect miscellaneous libraries that libc might dlopen.
* At the moment this is just libidn2. */
temp_bwrap = flatpak_bwrap_new (NULL);
g_warn_if_fail (mount_runtime_on_scratch->fds == NULL
|| mount_runtime_on_scratch->fds->len == 0);
flatpak_bwrap_append_bwrap (temp_bwrap, mount_runtime_on_scratch);
flatpak_bwrap_add_args (temp_bwrap,
tool_path,
"--container", scratch,
"--link-target", "/run/host",
"--dest", libdir_on_host,
"--provider", "/",
"if-exists:libidn2.so.0",
NULL);
flatpak_bwrap_finish (temp_bwrap);
if (!pv_bwrap_run_sync (temp_bwrap, NULL, error))
g_clear_pointer (&temp_bwrap, flatpak_bwrap_free);
any_libc_from_host = TRUE;
}
else
{
all_libc_from_host = FALSE;
/* /lib32 or /lib64 */
g_assert (i < G_N_ELEMENTS (libquals));
libqual = libquals[i];
dirs = g_new0 (gchar *, 7);
dirs[0] = g_build_filename ("/lib", multiarch_tuples[i], NULL);
dirs[1] = g_build_filename ("/usr", "lib", multiarch_tuples[i], NULL);
dirs[2] = g_strdup ("/lib");
dirs[3] = g_strdup ("/usr/lib");
dirs[4] = g_build_filename ("/", libqual, NULL);
dirs[5] = g_build_filename ("/usr", libqual, NULL);
for (j = 0; j < 6; j++)
if (!try_bind_dri (bwrap, mount_runtime_on_scratch,
overrides, scratch, tool_path, dirs[j],
libdir_on_host, error))
}
}
else
{
g_debug ("Cannot determine ld.so for %s", multiarch_tuples[i]);
}
}
if (!any_architecture_works)
{
GString *archs = g_string_new ("");
g_assert (multiarch_tuples[G_N_ELEMENTS (multiarch_tuples) - 1] == NULL);
for (i = 0; i < G_N_ELEMENTS (multiarch_tuples) - 1; i++)
{
if (archs->len > 0)
g_string_append (archs, ", ");
g_string_append (archs, multiarch_tuples[i]);
}
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"None of the supported CPU architectures are common to "
"the host system and the container (tried: %s)",
archs->str);
g_string_free (archs, TRUE);
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
if (any_libc_from_host && !all_libc_from_host)
{
/*
* This shouldn't happen. It would mean that there exist at least
* two architectures (let's say aaa and bbb) for which we have:
* host libc6:aaa < container libc6 < host libc6:bbb
* (we know that the container's libc6:aaa and libc6:bbb are
* constrained to be the same version because that's how multiarch
* works).
*
* If the host system locales work OK with both the aaa and bbb
* versions, let's assume they will also work with the intermediate
* version from the container...
*/
g_warning ("Using glibc from host system for some but not all "
"architectures! Arbitrarily using host locales.");
}
if (any_libc_from_host)
{
g_debug ("Making host locale data visible in container");
if (g_file_test ("/usr/lib/locale", G_FILE_TEST_EXISTS))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/usr/lib/locale",
"/usr/lib/locale",
NULL);
if (g_file_test ("/usr/share/i18n", G_FILE_TEST_EXISTS))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/usr/share/i18n",
"/usr/share/i18n",
NULL);
localedef = g_find_program_in_path ("localedef");
if (localedef == NULL)
{
g_warning ("Cannot find localedef in PATH");
}
else
{
g_autofree gchar *target = g_build_filename ("/run/host",
localedef, NULL);
flatpak_bwrap_add_args (bwrap,
"--symlink", target,
"/overrides/bin/localedef",
NULL);
}
}
else
{
g_debug ("Using included locale data from container");
}
if (!pv_bwrap_bind_usr (bwrap, "/", "/run/host", error))
ensure_locales (any_libc_from_host, tools_dir, bwrap, overrides);
if (dri_path->len != 0)
flatpak_bwrap_add_args (bwrap,
"--setenv", "LIBGL_DRIVERS_PATH", dri_path->str,
NULL);
else
flatpak_bwrap_add_args (bwrap,
"--unsetenv", "LIBGL_DRIVERS_PATH",
NULL);
/* These can add data fds to @bwrap, so they must come last - after
* other functions stop using @bwrap as a basis for their own bwrap
* invocations with flatpak_bwrap_append_bwrap().
* Otherwise, when flatpak_bwrap_append_bwrap() calls
* flatpak_bwrap_steal_fds(), it will make the original FlatpakBwrap
* unusable. */
flatpak_run_add_wayland_args (bwrap);
flatpak_run_add_x11_args (bwrap, TRUE);
flatpak_run_add_pulseaudio_args (bwrap);
flatpak_run_add_session_dbus_args (bwrap);
flatpak_run_add_system_dbus_args (bwrap);
pv_bwrap_copy_tree (bwrap, overrides, "/overrides");
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/* /etc/localtime and /etc/resolv.conf can not exist (or be symlinks to
* non-existing targets), in which case we don't want to attempt to create
* bogus symlinks or bind mounts, as that will cause flatpak run to fail.
*/
if (g_file_test ("/etc/localtime", G_FILE_TEST_EXISTS))
{
g_autofree char *localtime = NULL;
gboolean is_reachable = FALSE;
g_autofree char *timezone = flatpak_get_timezone ();
g_autofree char *timezone_content = g_strdup_printf ("%s\n", timezone);
localtime = glnx_readlinkat_malloc (-1, "/etc/localtime", NULL, NULL);
if (localtime != NULL)
{
g_autoptr(GFile) base_file = NULL;
g_autoptr(GFile) target_file = NULL;
g_autofree char *target_canonical = NULL;
base_file = g_file_new_for_path ("/etc");
target_file = g_file_resolve_relative_path (base_file, localtime);
target_canonical = g_file_get_path (target_file);
is_reachable = g_str_has_prefix (target_canonical, "/usr/");
}
if (is_reachable)
{
flatpak_bwrap_add_args (bwrap,
"--symlink", localtime, "/etc/localtime",
NULL);
}
else
{
flatpak_bwrap_add_args (bwrap,
"--ro-bind", "/etc/localtime", "/etc/localtime",
NULL);
}
flatpak_bwrap_add_args_data (bwrap, "timezone",
timezone_content, -1, "/etc/timezone",
NULL);
}
/* Order matters here: root, steam and steambeta are or might be symlinks
* to the root of the Steam installation, so we want to bind-mount their
* targets before we deal with the rest. */
static const char * const steam_api_subdirs[] =
{
"root", "steam", "steambeta", "bin", "bin32", "bin64", "sdk32", "sdk64",
};
static gboolean
use_fake_home (FlatpakBwrap *bwrap,
const gchar *fake_home,
GError **error)
{
const gchar *real_home = g_get_home_dir ();
g_autofree gchar *cache = g_build_filename (fake_home, ".cache", NULL);
g_autofree gchar *cache2 = g_build_filename (fake_home, "cache", NULL);
g_autofree gchar *tmp = g_build_filename (cache, "tmp", NULL);
g_autofree gchar *config = g_build_filename (fake_home, ".config", NULL);
g_autofree gchar *config2 = g_build_filename (fake_home, "config", NULL);
g_autofree gchar *local = g_build_filename (fake_home, ".local", NULL);
g_autofree gchar *data = g_build_filename (local, "share", NULL);
g_autofree gchar *data2 = g_build_filename (fake_home, "data", NULL);
g_autofree gchar *steam_pid = NULL;
g_autofree gchar *steam_pipe = NULL;
g_autoptr(GHashTable) mounted = NULL;
gsize i;
g_return_val_if_fail (bwrap != NULL, FALSE);
g_return_val_if_fail (fake_home != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
g_mkdir_with_parents (fake_home, 0700);
g_mkdir_with_parents (cache, 0700);
g_mkdir_with_parents (tmp, 0700);
g_mkdir_with_parents (config, 0700);
g_mkdir_with_parents (local, 0700);
g_mkdir_with_parents (data, 0700);
if (!g_file_test (cache2, G_FILE_TEST_EXISTS))
{
g_unlink (cache2);
if (symlink (".cache", cache2) != 0)
return glnx_throw_errno_prefix (error,
"Unable to create symlink %s -> .cache",
cache2);
}
if (!g_file_test (config2, G_FILE_TEST_EXISTS))
{
g_unlink (config2);
if (symlink (".config", config2) != 0)
return glnx_throw_errno_prefix (error,
"Unable to create symlink %s -> .config",
config2);
}
if (!g_file_test (data2, G_FILE_TEST_EXISTS))
{
g_unlink (data2);
if (symlink (".local/share", data2) != 0)
return glnx_throw_errno_prefix (error,
"Unable to create symlink %s -> .local/share",
data2);