From 3a2f7db477288c181c5450a22e089dd2dbaf637c Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Wed, 16 Jan 2019 13:05:34 -0800 Subject: [PATCH] cryptfs: check for errors in create_encrypted_random_key() When generating the key and salt we weren't checking for an error opening or reading from /dev/urandom. Switch to the helper function ReadRandomBytes() and start checking for errors. Test: Booted device with FDE. As a extra sanity check I also temporarily added log messages that dump the key and salt, and I verified they still appear random. Change-Id: I01ccee4f1f9910bf9508c8f02a918157393b0e68 --- cryptfs.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cryptfs.cpp b/cryptfs.cpp index d661952..ce01f1f 100644 --- a/cryptfs.cpp +++ b/cryptfs.cpp @@ -1398,14 +1398,15 @@ static int decrypt_master_key(const char* passwd, unsigned char* decrypted_maste static int create_encrypted_random_key(const char* passwd, unsigned char* master_key, unsigned char* salt, struct crypt_mnt_ftr* crypt_ftr) { - int fd; unsigned char key_buf[MAX_KEY_LEN]; - /* Get some random bits for a key */ - fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); - read(fd, key_buf, sizeof(key_buf)); - read(fd, salt, SALT_LEN); - close(fd); + /* Get some random bits for a key and salt */ + if (android::vold::ReadRandomBytes(sizeof(key_buf), reinterpret_cast(key_buf)) != 0) { + return -1; + } + if (android::vold::ReadRandomBytes(SALT_LEN, reinterpret_cast(salt)) != 0) { + return -1; + } /* Now encrypt it with the password */ return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);