diff options
author | Thomas Haller <thaller@redhat.com> | 2020-04-06 10:54:35 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-04-10 10:44:57 +0200 |
commit | 3e1e63e57db4cb3d44da35d75c8c9ab46e3e82df (patch) | |
tree | 21f6b8dfcb1ddcefa59cbed6ef4644058e49b827 /shared | |
parent | 277925c36a9623d9b3c1203abbc6ab6f44e54973 (diff) | |
download | NetworkManager-3e1e63e57db4cb3d44da35d75c8c9ab46e3e82df.tar.gz |
cli/polkit: make parsing polkit-agent-helper-1 protocol more conforming
- in io_watch_have_data(), ensure that we handle incomplete lines
that don't yet have a newline by waiting for more data. That means,
if the current content of the in_buffer does not have a newline, we
wait longer.
- in io_watch_have_data(), implement (and ignore) certain commands
instead of failing the request.
- in io_watch_have_data(), no longer g_compress() the entire line.
"polkitagenthelper-pam.c" never backslash escapes the command, it
only escapes the arguments. Of course, there should be no difference
in practice, except that we don't want to handle escape sequences
in the commands.
- in io_watch_have_data(), compare SUCCESS/FAILURE literally.
"polkitagenthelper-pam.c" never appends any trailing garbage to these
commands, and we shouldn't handle that (although "polkitagentsession.c"
does).
- when io_watch_have_data() completes with success, we cannot destroy
AuthRequest right away. It probably still has data pending that we first
need to write to the polkit helper. Wait longer, and let io_watch_can_write()
complete the request.
- ensure we always answer the GDBusMethodInvocation. Otherwise, it gets
leaked.
- use NMStrBuf instead of GString.
Diffstat (limited to 'shared')
-rw-r--r-- | shared/nm-glib-aux/nm-io-utils.c | 44 | ||||
-rw-r--r-- | shared/nm-glib-aux/nm-io-utils.h | 4 |
2 files changed, 32 insertions, 16 deletions
diff --git a/shared/nm-glib-aux/nm-io-utils.c b/shared/nm-glib-aux/nm-io-utils.c index 9620c688df..776c63e111 100644 --- a/shared/nm-glib-aux/nm-io-utils.c +++ b/shared/nm-glib-aux/nm-io-utils.c @@ -11,6 +11,7 @@ #include <sys/stat.h> #include <fcntl.h> +#include "nm-str-buf.h" #include "nm-shared-utils.h" #include "nm-secret-utils.h" #include "nm-errno.h" @@ -418,27 +419,40 @@ nm_utils_file_stat (const char *filename, struct stat *out_st) * @fd: the fd to read from. * @out_string: (out): output string where read bytes will be stored. * - * Returns: <0 on failure, which is -(errno) - * 0 on EOF or if the call would block (if the fd is nonblocking), - * >0 on success, which is the number of bytes read */ -ssize_t -nm_utils_fd_read (int fd, GString *out_string) + * Returns: <0 on failure, which is -(errno). + * 0 on EOF. + * >0 on success, which is the number of bytes read. */ +gssize +nm_utils_fd_read (int fd, NMStrBuf *out_string) { - size_t start_len; - ssize_t n_read; + gsize buf_available; + gssize n_read; + int errsv; g_return_val_if_fail (fd >= 0, -1); g_return_val_if_fail (out_string, -1); - start_len = out_string->len; - g_string_set_size (out_string, start_len + 1024); - - n_read = read (fd, &out_string->str[start_len], 1024); - if (n_read < 0) - return -NM_ERRNO_NATIVE (errno); + /* If the buffer size is 0, we allocate NM_UTILS_GET_NEXT_REALLOC_SIZE_1000 (1000 bytes) + * the first time. Afterwards, the buffer grows exponentially. + * + * Note that with @buf_available, we always would read as much buffer as we actually + * have reserved. */ + nm_str_buf_maybe_expand (out_string, NM_UTILS_GET_NEXT_REALLOC_SIZE_1000, FALSE); + + buf_available = out_string->allocated - out_string->len; + + n_read = read (fd, + &((nm_str_buf_get_str_unsafe (out_string))[out_string->len]), + buf_available); + if (n_read < 0) { + errsv = errno; + return -NM_ERRNO_NATIVE (errsv); + } - if (n_read > 0) - g_string_set_size (out_string, start_len + (gsize) n_read); + if (n_read > 0) { + nm_assert ((gsize) n_read <= buf_available); + nm_str_buf_set_size (out_string, out_string->len + (gsize) n_read, TRUE, FALSE); + } return n_read; } diff --git a/shared/nm-glib-aux/nm-io-utils.h b/shared/nm-glib-aux/nm-io-utils.h index dd2f499d6f..31326fc79a 100644 --- a/shared/nm-glib-aux/nm-io-utils.h +++ b/shared/nm-glib-aux/nm-io-utils.h @@ -47,7 +47,9 @@ gboolean nm_utils_file_set_contents (const char *filename, int *out_errsv, GError **error); -ssize_t nm_utils_fd_read (int fd, GString *out_string); +struct _NMStrBuf; + +gssize nm_utils_fd_read (int fd, struct _NMStrBuf *out_string); struct stat; |