diff --git a/lib/internal.h b/lib/internal.h index 5d776003..7ead1088 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -57,7 +57,7 @@ struct volume_key { char key[]; }; -struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key); +struct volume_key *crypt_alloc_volume_key(size_t keylength, const char *key); struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, unsigned keylength); void crypt_free_volume_key(struct volume_key *vk); diff --git a/lib/utils_crypt.c b/lib/utils_crypt.c index c7c98937..aaf2917e 100644 --- a/lib/utils_crypt.c +++ b/lib/utils_crypt.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -98,7 +99,7 @@ void *crypt_safe_alloc(size_t size) { struct safe_allocation *alloc; - if (!size) + if (!size || size > (SIZE_MAX - offsetof(struct safe_allocation, data))) return NULL; alloc = malloc(size + offsetof(struct safe_allocation, data)); diff --git a/lib/volumekey.c b/lib/volumekey.c index e7150aae..8b442c44 100644 --- a/lib/volumekey.c +++ b/lib/volumekey.c @@ -20,14 +20,19 @@ */ #include +#include #include #include "internal.h" -struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key) +struct volume_key *crypt_alloc_volume_key(size_t keylength, const char *key) { - struct volume_key *vk = malloc(sizeof(*vk) + keylength); + struct volume_key *vk; + if (!keylength || keylength > (SIZE_MAX - sizeof(*vk))) + return NULL; + + vk = malloc(sizeof(*vk) + keylength); if (!vk) return NULL;