Merge pull request #29400 from yuwata/network-limit-read-size
network: limit read size
This commit is contained in:
+15
-1
@@ -32,6 +32,8 @@
|
||||
|
||||
/* The maximum size of the file we'll read in one go in read_full_file() (64M). */
|
||||
#define READ_FULL_BYTES_MAX (64U*1024U*1024U - 1U)
|
||||
/* Used when a size is specified for read_full_file() with READ_FULL_FILE_UNBASE64 or _UNHEX */
|
||||
#define READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY 3
|
||||
|
||||
/* The maximum size of virtual files (i.e. procfs, sysfs, and other virtual "API" files) we'll read in one go
|
||||
* in read_virtual_file(). Note that this limit is different (and much lower) than the READ_FULL_BYTES_MAX
|
||||
@@ -572,7 +574,7 @@ int read_full_stream_full(
|
||||
size_t *ret_size) {
|
||||
|
||||
_cleanup_free_ char *buf = NULL;
|
||||
size_t n, n_next = 0, l;
|
||||
size_t n, n_next = 0, l, expected_decoded_size = size;
|
||||
int fd, r;
|
||||
|
||||
assert(f);
|
||||
@@ -583,6 +585,13 @@ int read_full_stream_full(
|
||||
if (offset != UINT64_MAX && offset > LONG_MAX) /* fseek() can only deal with "long" offsets */
|
||||
return -ERANGE;
|
||||
|
||||
if ((flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) != 0) {
|
||||
if (size <= SIZE_MAX / READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY)
|
||||
size *= READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY;
|
||||
else
|
||||
size = SIZE_MAX;
|
||||
}
|
||||
|
||||
fd = fileno(f);
|
||||
if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see
|
||||
* fmemopen()), let's optimize our buffering */
|
||||
@@ -707,6 +716,11 @@ int read_full_stream_full(
|
||||
explicit_bzero_safe(buf, n);
|
||||
free_and_replace(buf, decoded);
|
||||
n = l = decoded_size;
|
||||
|
||||
if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > expected_decoded_size) {
|
||||
r = -E2BIG;
|
||||
goto finalize;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ret_size) {
|
||||
|
||||
@@ -959,15 +959,19 @@ static int macsec_read_key_file(NetDev *netdev, SecurityAssociation *sa) {
|
||||
return 0;
|
||||
|
||||
r = read_full_file_full(
|
||||
AT_FDCWD, sa->key_file, UINT64_MAX, SIZE_MAX,
|
||||
READ_FULL_FILE_SECURE | READ_FULL_FILE_UNHEX | READ_FULL_FILE_WARN_WORLD_READABLE | READ_FULL_FILE_CONNECT_SOCKET,
|
||||
AT_FDCWD, sa->key_file, UINT64_MAX, MACSEC_KEYID_LEN,
|
||||
READ_FULL_FILE_SECURE |
|
||||
READ_FULL_FILE_UNHEX |
|
||||
READ_FULL_FILE_WARN_WORLD_READABLE |
|
||||
READ_FULL_FILE_CONNECT_SOCKET |
|
||||
READ_FULL_FILE_FAIL_WHEN_LARGER,
|
||||
NULL, (char **) &key, &key_len);
|
||||
if (r < 0)
|
||||
return log_netdev_error_errno(netdev, r,
|
||||
"Failed to read key from '%s', ignoring: %m",
|
||||
sa->key_file);
|
||||
|
||||
if (key_len != 16)
|
||||
if (key_len != MACSEC_KEYID_LEN)
|
||||
return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
|
||||
"Invalid key length (%zu bytes), ignoring: %m", key_len);
|
||||
|
||||
|
||||
@@ -1037,8 +1037,12 @@ static int wireguard_read_key_file(const char *filename, uint8_t dest[static WG_
|
||||
assert(dest);
|
||||
|
||||
r = read_full_file_full(
|
||||
AT_FDCWD, filename, UINT64_MAX, SIZE_MAX,
|
||||
READ_FULL_FILE_SECURE | READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_WARN_WORLD_READABLE | READ_FULL_FILE_CONNECT_SOCKET,
|
||||
AT_FDCWD, filename, UINT64_MAX, WG_KEY_LEN,
|
||||
READ_FULL_FILE_SECURE |
|
||||
READ_FULL_FILE_UNBASE64 |
|
||||
READ_FULL_FILE_WARN_WORLD_READABLE |
|
||||
READ_FULL_FILE_CONNECT_SOCKET |
|
||||
READ_FULL_FILE_FAIL_WHEN_LARGER,
|
||||
NULL, &key, &key_len);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user