| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
/* This gets logged as part of the (more verbose) protocol logging */ |
| 9 |
#ifdef G_LOG_DOMAIN |
| 10 |
#undef G_LOG_DOMAIN |
| 11 |
#endif |
| 12 |
#define G_LOG_DOMAIN "cockpit-protocol" |
| 13 |
|
| 14 |
#include "cockpitmemfdread.h" |
| 15 |
|
| 16 |
#include "cockpitjson.h" |
| 17 |
|
| 18 |
#include <errno.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <inttypes.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
gchar * |
| 25 |
cockpit_memfd_read (int fd, |
| 26 |
GError **error) |
| 27 |
{ |
| 28 |
int seals = fcntl (fd, F_GET_SEALS); |
| 29 |
if (seals == -1) |
| 30 |
{ |
| 31 |
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), |
| 32 |
"could not query seals on fd %d: not memfd?: %m", fd); |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
|
| 36 |
const guint expected_seals = F_SEAL_WRITE | F_SEAL_GROW | F_SEAL_SHRINK; |
| 37 |
if ((seals & expected_seals) != expected_seals) |
| 38 |
{ |
| 39 |
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, |
| 40 |
"memfd fd %d has incorrect seals set: %u (instead of %u)\n", |
| 41 |
fd, seals & expected_seals, expected_seals); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
|
| 45 |
struct stat buf; |
| 46 |
if (fstat (fd, &buf) != 0) |
| 47 |
{ |
| 48 |
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), |
| 49 |
"Failed to stat memfd %d: %m", fd); |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
if (buf.st_size < 1) |
| 54 |
{ |
| 55 |
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, |
| 56 |
"memfd %d must not be empty", fd); |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
|
| 60 |
/* This number is completely arbitrary: it's much larger than anything |
| 61 |
* we're ever going to receive, but it's much smaller than any value |
| 62 |
* that would ever cause us problems (with integer overflow, or malloc |
| 63 |
* failing). |
| 64 |
*/ |
| 65 |
if (buf.st_size > 10000) |
| 66 |
{ |
| 67 |
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, |
| 68 |
"memfd %d is unreasonably large (%"PRId64" bytes)", fd, (gint64) buf.st_size); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
g_autofree gchar *content = g_malloc (buf.st_size + 1); |
| 73 |
gssize s = pread (fd, content, buf.st_size + 1, 0); |
| 74 |
if (s == -1) |
| 75 |
{ |
| 76 |
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), |
| 77 |
"failed to read memfd %d: %m", fd); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
else if (s != buf.st_size) |
| 81 |
{ |
| 82 |
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, |
| 83 |
"memfd %d changed size from %zu to %zu bytes", fd, (gssize) buf.st_size, s); |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
|
| 87 |
for (gint i = 0; i < buf.st_size; i++) |
| 88 |
if (((signed char) content[i]) <= 0) |
| 89 |
{ |
| 90 |
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, |
| 91 |
"memfd %d contains %s character", fd, content[i] ? "non-ASCII" : "nul"); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
content[buf.st_size] = '\0'; |
| 96 |
|
| 97 |
return g_steal_pointer (&content); |
| 98 |
} |
| 99 |
|
| 100 |
JsonObject * |
| 101 |
cockpit_memfd_read_json (gint fd, |
| 102 |
GError **error) |
| 103 |
{ |
| 104 |
g_autofree gchar *content = cockpit_memfd_read (fd, error); |
| 105 |
|
| 106 |
if (content == NULL) |
| 107 |
return NULL; |
| 108 |
|
| 109 |
return cockpit_json_parse_object (content, -1, error); |
| 110 |
} |
| 111 |
|
| 112 |
JsonObject * |
| 113 |
cockpit_memfd_read_json_from_control_messages (CockpitControlMessages *ccm, |
| 114 |
GError **error) |
| 115 |
{ |
| 116 |
if (ccm->n_messages == 0) |
| 117 |
return NULL; |
| 118 |
|
| 119 |
gint peeked_fd = cockpit_control_messages_peek_single_fd (ccm, error); |
| 120 |
|
| 121 |
if (peeked_fd == -1) |
| 122 |
return NULL; |
| 123 |
|
| 124 |
return cockpit_memfd_read_json (peeked_fd, error); |
| 125 |
} |