]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fileio: if we try to read a file larger than SIZE_MAX this is not a problem if a...
authorLennart Poettering <lennart@poettering.net>
Fri, 21 May 2021 14:30:52 +0000 (16:30 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 21 May 2021 19:54:19 +0000 (21:54 +0200)
i.e. 32bit userspace reading /proc/kcore on a 64bit kernel with max_size
should not needlessly fail.

src/basic/fileio.c

index 853e679aaa849f105750fea1fe5838d27466d98c..7bb1e8a8c6ec812c591713d9b2844f0884a3afe1 100644 (file)
@@ -405,12 +405,18 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
                 /* Be prepared for files from /proc which generally report a file size of 0. */
                 assert_cc(READ_FULL_BYTES_MAX < SSIZE_MAX);
                 if (st.st_size > 0) {
-                        if (st.st_size > SSIZE_MAX) /* Avoid overflow with 32-bit size_t and 64-bit off_t. */
-                                return -EFBIG;
+                        if (st.st_size > SSIZE_MAX) { /* Avoid overflow with 32-bit size_t and 64-bit off_t. */
 
-                        size = MIN((size_t) st.st_size, max_size);
-                        if (size > READ_FULL_BYTES_MAX)
-                                return -EFBIG;
+                                if (max_size == SIZE_MAX)
+                                        return -EFBIG;
+
+                                size = max_size;
+                        } else {
+                                size = MIN((size_t) st.st_size, max_size);
+
+                                if (size > READ_FULL_BYTES_MAX)
+                                        return -EFBIG;
+                        }
 
                         n_retries--;
                 } else {