Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an overlayfs on /etc to keep monitored files updated #5528

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
overlayfs on /etc POC
  • Loading branch information
swick committed Sep 22, 2023
commit 28da8f50de16bd2399ba0a6338b921c4b515070b
6 changes: 6 additions & 0 deletions common/flatpak-bwrap-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ gboolean flatpak_bwrap_add_args_data (FlatpakBwrap *bwrap,
gssize content_size,
const char *path,
GError **error);
gboolean flatpak_bwrap_add_args_file (FlatpakBwrap *bwrap,
const char *name,
const char *content,
gssize content_size,
const char *path,
GError **error);
void flatpak_bwrap_add_bind_arg (FlatpakBwrap *bwrap,
const char *type,
const char *src,
Expand Down
17 changes: 17 additions & 0 deletions common/flatpak-bwrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ flatpak_bwrap_add_args_data (FlatpakBwrap *bwrap,
return TRUE;
}

gboolean
flatpak_bwrap_add_args_file (FlatpakBwrap *bwrap,
const char *name,
const char *content,
gssize content_size,
const char *path,
GError **error)
{
g_auto(GLnxTmpfile) args_tmpf = { 0, };

if (!flatpak_buffer_to_sealed_memfd_or_tmpfile (&args_tmpf, name, content, content_size, error))
return FALSE;

flatpak_bwrap_add_args_data_fd (bwrap, "--file", g_steal_fd (&args_tmpf.fd), path);
return TRUE;
}

/* This resolves the target here rather than in bwrap, because it may
* not resolve in bwrap setup due to absolute symlinks conflicting
* with /newroot root. For example, dest could be inside
Expand Down
68 changes: 15 additions & 53 deletions common/flatpak-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@ static void
add_tzdata_args (FlatpakBwrap *bwrap,
GFile *runtime_files)
{
#if 0
g_autofree char *raw_timezone = flatpak_get_timezone ();
g_autofree char *timezone_content = g_strdup_printf ("%s\n", raw_timezone);
g_autofree char *localtime_content = g_strconcat ("../usr/share/zoneinfo/", raw_timezone, NULL);
Expand Down Expand Up @@ -1562,6 +1563,7 @@ add_tzdata_args (FlatpakBwrap *bwrap,
flatpak_bwrap_add_args_data (bwrap, "timezone",
timezone_content, -1, "/etc/timezone",
NULL);
#endif
}

static void
Expand All @@ -1588,14 +1590,15 @@ add_monitor_path_args (gboolean use_session_helper,
&session_data,
NULL, NULL))
{
const char *overlay = "/run/etc-overlay:/etc";

flatpak_bwrap_add_args (bwrap, "--dir", "/run/etc-overlay", NULL);

if (g_variant_lookup (session_data, "path", "s", &monitor_path))
flatpak_bwrap_add_args (bwrap,
"--ro-bind", monitor_path, "/run/host/monitor",
"--symlink", "/run/host/monitor/resolv.conf", "/etc/resolv.conf",
"--symlink", "/run/host/monitor/host.conf", "/etc/host.conf",
"--symlink", "/run/host/monitor/hosts", "/etc/hosts",
"--symlink", "/run/host/monitor/gai.conf", "/etc/gai.conf",
NULL);
{
flatpak_bwrap_add_args (bwrap, "--ro-bind", monitor_path, "/run/host/monitor", NULL);
overlay = "/run/etc-overlay:/run/host/monitor:/etc";
}

if (g_variant_lookup (session_data, "pkcs11-socket", "s", &pkcs11_socket_path))
{
Expand All @@ -1604,9 +1607,9 @@ add_monitor_path_args (gboolean use_session_helper,
"# This overrides the runtime p11-kit-trusted module with a client one talking to the trust module on the host\n"
"module: p11-kit-client.so\n";

if (flatpak_bwrap_add_args_data (bwrap, "p11-kit-trust.module",
if (flatpak_bwrap_add_args_file (bwrap, "p11-kit-trust.module",
trusted_module_contents, -1,
"/etc/pkcs11/modules/p11-kit-trust.module", NULL))
"/run/etc-overlay/pkcs11/modules/p11-kit-trust.module", NULL))
{
flatpak_bwrap_add_args (bwrap,
"--ro-bind", pkcs11_socket_path, sandbox_pkcs11_socket_path,
Expand All @@ -1615,6 +1618,8 @@ add_monitor_path_args (gboolean use_session_helper,
flatpak_bwrap_add_runtime_dir_member (bwrap, "p11-kit");
}
}

flatpak_bwrap_add_args (bwrap, "--overlay", overlay, "/etc", NULL);
}
else
{
Expand Down Expand Up @@ -2219,50 +2224,7 @@ flatpak_run_setup_base_argv (FlatpakBwrap *bwrap,
(flags & FLATPAK_RUN_FLAG_WRITABLE_ETC) == 0 &&
g_file_query_exists (etc, NULL))
{
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
struct dirent *dent;
gboolean inited;

inited = glnx_dirfd_iterator_init_at (AT_FDCWD, flatpak_file_get_path_cached (etc), FALSE, &dfd_iter, NULL);

while (inited)
{
g_autofree char *src = NULL;
g_autofree char *dest = NULL;

if (!glnx_dirfd_iterator_next_dent_ensure_dtype (&dfd_iter, &dent, NULL, NULL) || dent == NULL)
break;

if (strcmp (dent->d_name, "passwd") == 0 ||
strcmp (dent->d_name, "group") == 0 ||
strcmp (dent->d_name, "machine-id") == 0 ||
strcmp (dent->d_name, "resolv.conf") == 0 ||
strcmp (dent->d_name, "host.conf") == 0 ||
strcmp (dent->d_name, "hosts") == 0 ||
strcmp (dent->d_name, "gai.conf") == 0 ||
strcmp (dent->d_name, "localtime") == 0 ||
strcmp (dent->d_name, "timezone") == 0 ||
strcmp (dent->d_name, "pkcs11") == 0)
continue;

src = g_build_filename (flatpak_file_get_path_cached (etc), dent->d_name, NULL);
dest = g_build_filename ("/etc", dent->d_name, NULL);
if (dent->d_type == DT_LNK)
{
g_autofree char *target = NULL;

target = glnx_readlinkat_malloc (dfd_iter.fd, dent->d_name,
NULL, error);
if (target == NULL)
return FALSE;

flatpak_bwrap_add_args (bwrap, "--symlink", target, dest, NULL);
}
else
{
flatpak_bwrap_add_args (bwrap, "--ro-bind", src, dest, NULL);
}
}
flatpak_bwrap_add_args (bwrap, "--ro-bind", flatpak_file_get_path_cached (etc), "/etc", NULL);
}

if (app_id_dir != NULL)
Expand Down