]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cryptsetup-pkcs11: refuse keys above 16MiB size
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Dec 2019 17:39:53 +0000 (18:39 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 17 Dec 2019 17:54:00 +0000 (18:54 +0100)
src/cryptsetup/cryptsetup-pkcs11.c

index c259a766d7e63fa50c07b1aee29af92c612ca3eb..5c4d3acc97cc9a97285491ae5968578892c132b1 100644 (file)
 #include "cryptsetup-pkcs11.h"
 #include "escape.h"
 #include "fd-util.h"
+#include "format-util.h"
 #include "macro.h"
 #include "memory-util.h"
 #include "pkcs11-util.h"
 #include "stat-util.h"
 #include "strv.h"
 
+#define KEY_FILE_SIZE_MAX (16U*1024U*1024U) /* 16 MiB */
+
 static int load_key_file(
                 const char *key_file,
                 size_t key_file_size,
@@ -50,8 +53,13 @@ static int load_key_file(
 
                 if (st.st_size == 0)
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Key file is empty, refusing.");
-                if ((uint64_t) st.st_size > SIZE_MAX)
-                        return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Key file too large, refusing.");
+                if ((uint64_t) st.st_size > KEY_FILE_SIZE_MAX) {
+                        char buf1[FORMAT_BYTES_MAX], buf2[FORMAT_BYTES_MAX];
+                        return log_error_errno(SYNTHETIC_ERRNO(ERANGE),
+                                               "Key file larger (%s) than allowed maximum size (%s), refusing.",
+                                               format_bytes(buf1, sizeof(buf1), st.st_size),
+                                               format_bytes(buf2, sizeof(buf2), KEY_FILE_SIZE_MAX));
+                }
 
                 if (key_file_offset >= (uint64_t) st.st_size)
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Key file offset too large for file, refusing.");