utils: Add flatpak_parse_fd

This is meant to parse file descriptor strings passed via the command
line. It is not a security mechanism and will happily accept fds 0-3 as
well.
This commit is contained in:
Sebastian Wick
2026-02-06 17:14:49 +01:00
committed by Sebastian Wick
parent 2acdd330d8
commit 50af610ff4
3 changed files with 32 additions and 12 deletions

View File

@@ -2432,21 +2432,16 @@ option_env_fd_cb (const gchar *option_name,
GError **error)
{
FlatpakContext *context = data;
guint64 fd;
gchar *endptr;
gboolean ret;
glnx_autofd int fd = -1;
fd = g_ascii_strtoull (value, &endptr, 10);
fd = flatpak_parse_fd (value, error);
if (fd < 0)
return FALSE;
if (endptr == NULL || *endptr != '\0' || fd > G_MAXINT)
return glnx_throw (error, "Not a valid file descriptor: %s", value);
if (fd < 3)
return glnx_throw (error, "File descriptors 0, 1, 2 are reserved");
ret = flatpak_context_parse_env_fd (context, (int) fd, error);
if (fd >= 3)
close (fd);
return ret;
return flatpak_context_parse_env_fd (context, fd, error);
}
static gboolean

View File

@@ -365,6 +365,9 @@ gboolean running_under_sudo_root (void);
void flatpak_set_debugging (gboolean debugging);
gboolean flatpak_is_debugging (void);
int flatpak_parse_fd (const char *fd_string,
GError **error);
#ifdef INCLUDE_INTERNAL_TESTS
typedef void (*flatpak_test_fn) (void);
void flatpak_add_test (const char *path, flatpak_test_fn fn);

View File

@@ -2582,6 +2582,28 @@ flatpak_is_debugging (void)
return is_debugging;
}
int
flatpak_parse_fd (const char *fd_string,
GError **error)
{
guint64 parsed;
char *endptr;
int fd;
struct stat stbuf;
parsed = g_ascii_strtoull (fd_string, &endptr, 10);
if (endptr == NULL || *endptr != '\0' || parsed > G_MAXINT)
return glnx_fd_throw (error, "Not a valid file descriptor: %s", fd_string);
fd = (int) parsed;
if (!glnx_fstat (fd, &stbuf, error))
return -1;
return fd;
}
#ifdef INCLUDE_INTERNAL_TESTS
static GList *flatpak_test_paths = NULL;
static GList *flatpak_test_fns = NULL;