From 5c809fdc07358a540571cbb223db89d27c284fd5 Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Sun, 24 Dec 2017 18:29:05 +0000 Subject: [PATCH] redirect: Add new override on getpwuid This is required for some Aspyr ports which will form their own fixed paths to directories based on getpwuid() as opposed to using the environment `HOME` variable. With this we force the home directory to be consistent under snapd and alleviate any issues with denials based on home directory lookups. Signed-off-by: Ikey Doherty --- src/redirect/main.c | 36 ++++++++++++++++++++++++++++++++++++ src/redirect/private.h | 14 ++++++++++++++ src/redirect/sym.map | 1 + 3 files changed, 51 insertions(+) diff --git a/src/redirect/main.c b/src/redirect/main.c index 97a3736..1b6c142 100644 --- a/src/redirect/main.c +++ b/src/redirect/main.c @@ -19,6 +19,13 @@ #include #include +#include "config.h" + +/* Expose getpwuid override in snapd */ +#ifdef HAVE_SNAPD_SUPPORT +#include +#endif + #include "../common/common.h" #include "../common/files.h" #include "../common/log.h" @@ -82,6 +89,9 @@ static LsiRedirectProfile *lsi_profile = NULL; static LsiSymbolBinding lsi_libc_bindings[] = { SYMBOL_BINDING(libc, open), SYMBOL_BINDING(libc, fopen64), +#ifdef HAVE_SNAPD_SUPPORT + SYMBOL_BINDING(libc, getpwuid), +#endif }; /** @@ -335,6 +345,32 @@ fallback_open: return lsi_table.fopen64(p, modes); } +#ifdef HAVE_SNAPD_SUPPORT + +#include + +_nica_public_ struct passwd *getpwuid(uid_t uid) +{ + /* Must ensure we're **really** initialised, as we might see open happen + * before the constructor.. + */ + lsi_redirect_init_tables(); + + struct passwd *ret = NULL; + const char *snap_root = getenv("SNAP_USER_COMMON"); + + /* If they're requesting our uid and SNAP_USER_COMMON is set, then + * let us override the home directory to be correct. + */ + ret = lsi_table.getpwuid(uid); + if (uid == getuid() && snap_root && *snap_root) { + ret->pw_dir = (char *)snap_root; + } + return ret; +} + +#endif + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/src/redirect/private.h b/src/redirect/private.h index f2ce981..16db6f2 100644 --- a/src/redirect/private.h +++ b/src/redirect/private.h @@ -14,6 +14,12 @@ #include #include +#include "config.h" + +#ifdef HAVE_SNAPD_SUPPORT +#include +#endif + /** * Handle definitions */ @@ -21,6 +27,10 @@ typedef int (*lsi_open_file)(const char *p, int flags, mode_t mode); typedef FILE *(*lsi_fopen64_file)(const char *p, const char *modes); +#ifdef HAVE_SNAPD_SUPPORT +typedef struct passwd *(*lsi_getpwuid)(uid_t uid); +#endif + /** * Global storage of handles for nicer organisation. */ @@ -28,6 +38,10 @@ typedef struct LsiRedirectTable { lsi_open_file open; lsi_fopen64_file fopen64; +#ifdef HAVE_SNAPD_SUPPORT + lsi_getpwuid getpwuid; +#endif + /* Allow future handle opens.. */ struct { void *libc; diff --git a/src/redirect/sym.map b/src/redirect/sym.map index b8109ff..79ec819 100644 --- a/src/redirect/sym.map +++ b/src/redirect/sym.map @@ -1,6 +1,7 @@ { global: fopen64; + getpwuid; open; local: *;