mirror of
https://github.com/flatpak/flatpak.git
synced 2026-04-23 16:39:02 -04:00
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:
committed by
Sebastian Wick
parent
2acdd330d8
commit
50af610ff4
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user