1237fead6SMichael Halcrow /** 2237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3237fead6SMichael Halcrow * In-kernel key management code. Includes functions to parse and 4237fead6SMichael Halcrow * write authentication token-related packets with the underlying 5237fead6SMichael Halcrow * file. 6237fead6SMichael Halcrow * 7237fead6SMichael Halcrow * Copyright (C) 2004-2006 International Business Machines Corp. 8237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com> 9237fead6SMichael Halcrow * Michael C. Thompson <mcthomps@us.ibm.com> 10dddfa461SMichael Halcrow * Trevor S. Highland <trevor.highland@gmail.com> 11237fead6SMichael Halcrow * 12237fead6SMichael Halcrow * This program is free software; you can redistribute it and/or 13237fead6SMichael Halcrow * modify it under the terms of the GNU General Public License as 14237fead6SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 15237fead6SMichael Halcrow * License, or (at your option) any later version. 16237fead6SMichael Halcrow * 17237fead6SMichael Halcrow * This program is distributed in the hope that it will be useful, but 18237fead6SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 19237fead6SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20237fead6SMichael Halcrow * General Public License for more details. 21237fead6SMichael Halcrow * 22237fead6SMichael Halcrow * You should have received a copy of the GNU General Public License 23237fead6SMichael Halcrow * along with this program; if not, write to the Free Software 24237fead6SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 25237fead6SMichael Halcrow * 02111-1307, USA. 26237fead6SMichael Halcrow */ 27237fead6SMichael Halcrow 283095e8e3SHerbert Xu #include <crypto/hash.h> 293095e8e3SHerbert Xu #include <crypto/skcipher.h> 30237fead6SMichael Halcrow #include <linux/string.h> 31237fead6SMichael Halcrow #include <linux/pagemap.h> 32237fead6SMichael Halcrow #include <linux/key.h> 33237fead6SMichael Halcrow #include <linux/random.h> 34237fead6SMichael Halcrow #include <linux/scatterlist.h> 355a0e3ad6STejun Heo #include <linux/slab.h> 36237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 37237fead6SMichael Halcrow 38237fead6SMichael Halcrow /** 39237fead6SMichael Halcrow * request_key returned an error instead of a valid key address; 40237fead6SMichael Halcrow * determine the type of error, make appropriate log entries, and 41237fead6SMichael Halcrow * return an error code. 42237fead6SMichael Halcrow */ 43cd9d67dfSMichael Halcrow static int process_request_key_err(long err_code) 44237fead6SMichael Halcrow { 45237fead6SMichael Halcrow int rc = 0; 46237fead6SMichael Halcrow 47237fead6SMichael Halcrow switch (err_code) { 48982363c9SEric Sandeen case -ENOKEY: 49237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "No key\n"); 50237fead6SMichael Halcrow rc = -ENOENT; 51237fead6SMichael Halcrow break; 52982363c9SEric Sandeen case -EKEYEXPIRED: 53237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Key expired\n"); 54237fead6SMichael Halcrow rc = -ETIME; 55237fead6SMichael Halcrow break; 56982363c9SEric Sandeen case -EKEYREVOKED: 57237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Key revoked\n"); 58237fead6SMichael Halcrow rc = -EINVAL; 59237fead6SMichael Halcrow break; 60237fead6SMichael Halcrow default: 61237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unknown error code: " 62888d57bbSJoe Perches "[0x%.16lx]\n", err_code); 63237fead6SMichael Halcrow rc = -EINVAL; 64237fead6SMichael Halcrow } 65237fead6SMichael Halcrow return rc; 66237fead6SMichael Halcrow } 67237fead6SMichael Halcrow 680e1fc5efSRoberto Sassu static int process_find_global_auth_tok_for_sig_err(int err_code) 690e1fc5efSRoberto Sassu { 700e1fc5efSRoberto Sassu int rc = err_code; 710e1fc5efSRoberto Sassu 720e1fc5efSRoberto Sassu switch (err_code) { 730e1fc5efSRoberto Sassu case -ENOENT: 740e1fc5efSRoberto Sassu ecryptfs_printk(KERN_WARNING, "Missing auth tok\n"); 750e1fc5efSRoberto Sassu break; 760e1fc5efSRoberto Sassu case -EINVAL: 770e1fc5efSRoberto Sassu ecryptfs_printk(KERN_WARNING, "Invalid auth tok\n"); 780e1fc5efSRoberto Sassu break; 790e1fc5efSRoberto Sassu default: 800e1fc5efSRoberto Sassu rc = process_request_key_err(err_code); 810e1fc5efSRoberto Sassu break; 820e1fc5efSRoberto Sassu } 830e1fc5efSRoberto Sassu return rc; 840e1fc5efSRoberto Sassu } 850e1fc5efSRoberto Sassu 86237fead6SMichael Halcrow /** 87f66e883eSMichael Halcrow * ecryptfs_parse_packet_length 88237fead6SMichael Halcrow * @data: Pointer to memory containing length at offset 89237fead6SMichael Halcrow * @size: This function writes the decoded size to this memory 90237fead6SMichael Halcrow * address; zero on error 91237fead6SMichael Halcrow * @length_size: The number of bytes occupied by the encoded length 92237fead6SMichael Halcrow * 9322e78fafSMichael Halcrow * Returns zero on success; non-zero on error 94237fead6SMichael Halcrow */ 95f66e883eSMichael Halcrow int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, 96237fead6SMichael Halcrow size_t *length_size) 97237fead6SMichael Halcrow { 98237fead6SMichael Halcrow int rc = 0; 99237fead6SMichael Halcrow 100237fead6SMichael Halcrow (*length_size) = 0; 101237fead6SMichael Halcrow (*size) = 0; 102237fead6SMichael Halcrow if (data[0] < 192) { 103237fead6SMichael Halcrow /* One-byte length */ 104831115afSTyler Hicks (*size) = data[0]; 105237fead6SMichael Halcrow (*length_size) = 1; 106237fead6SMichael Halcrow } else if (data[0] < 224) { 107237fead6SMichael Halcrow /* Two-byte length */ 108831115afSTyler Hicks (*size) = (data[0] - 192) * 256; 109831115afSTyler Hicks (*size) += data[1] + 192; 110237fead6SMichael Halcrow (*length_size) = 2; 111237fead6SMichael Halcrow } else if (data[0] == 255) { 11248399c0bSTyler Hicks /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ 113237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Five-byte packet length not " 114237fead6SMichael Halcrow "supported\n"); 115237fead6SMichael Halcrow rc = -EINVAL; 116237fead6SMichael Halcrow goto out; 117237fead6SMichael Halcrow } else { 118237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing packet length\n"); 119237fead6SMichael Halcrow rc = -EINVAL; 120237fead6SMichael Halcrow goto out; 121237fead6SMichael Halcrow } 122237fead6SMichael Halcrow out: 123237fead6SMichael Halcrow return rc; 124237fead6SMichael Halcrow } 125237fead6SMichael Halcrow 126237fead6SMichael Halcrow /** 127f66e883eSMichael Halcrow * ecryptfs_write_packet_length 12822e78fafSMichael Halcrow * @dest: The byte array target into which to write the length. Must 12948399c0bSTyler Hicks * have at least ECRYPTFS_MAX_PKT_LEN_SIZE bytes allocated. 130237fead6SMichael Halcrow * @size: The length to write. 13122e78fafSMichael Halcrow * @packet_size_length: The number of bytes used to encode the packet 13222e78fafSMichael Halcrow * length is written to this address. 133237fead6SMichael Halcrow * 134237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 135237fead6SMichael Halcrow */ 136f66e883eSMichael Halcrow int ecryptfs_write_packet_length(char *dest, size_t size, 137237fead6SMichael Halcrow size_t *packet_size_length) 138237fead6SMichael Halcrow { 139237fead6SMichael Halcrow int rc = 0; 140237fead6SMichael Halcrow 141237fead6SMichael Halcrow if (size < 192) { 142237fead6SMichael Halcrow dest[0] = size; 143237fead6SMichael Halcrow (*packet_size_length) = 1; 144237fead6SMichael Halcrow } else if (size < 65536) { 145237fead6SMichael Halcrow dest[0] = (((size - 192) / 256) + 192); 146237fead6SMichael Halcrow dest[1] = ((size - 192) % 256); 147237fead6SMichael Halcrow (*packet_size_length) = 2; 148237fead6SMichael Halcrow } else { 14948399c0bSTyler Hicks /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ 150237fead6SMichael Halcrow rc = -EINVAL; 151237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, 152f24b3887STyler Hicks "Unsupported packet size: [%zd]\n", size); 153237fead6SMichael Halcrow } 154237fead6SMichael Halcrow return rc; 155237fead6SMichael Halcrow } 156237fead6SMichael Halcrow 157dddfa461SMichael Halcrow static int 158dddfa461SMichael Halcrow write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key, 159dddfa461SMichael Halcrow char **packet, size_t *packet_len) 160dddfa461SMichael Halcrow { 161dddfa461SMichael Halcrow size_t i = 0; 162dddfa461SMichael Halcrow size_t data_len; 163dddfa461SMichael Halcrow size_t packet_size_len; 164dddfa461SMichael Halcrow char *message; 165dddfa461SMichael Halcrow int rc; 166dddfa461SMichael Halcrow 167dddfa461SMichael Halcrow /* 168dddfa461SMichael Halcrow * ***** TAG 64 Packet Format ***** 169dddfa461SMichael Halcrow * | Content Type | 1 byte | 170dddfa461SMichael Halcrow * | Key Identifier Size | 1 or 2 bytes | 171dddfa461SMichael Halcrow * | Key Identifier | arbitrary | 172dddfa461SMichael Halcrow * | Encrypted File Encryption Key Size | 1 or 2 bytes | 173dddfa461SMichael Halcrow * | Encrypted File Encryption Key | arbitrary | 174dddfa461SMichael Halcrow */ 175dddfa461SMichael Halcrow data_len = (5 + ECRYPTFS_SIG_SIZE_HEX 176dddfa461SMichael Halcrow + session_key->encrypted_key_size); 177dddfa461SMichael Halcrow *packet = kmalloc(data_len, GFP_KERNEL); 178dddfa461SMichael Halcrow message = *packet; 179dddfa461SMichael Halcrow if (!message) { 180dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 181dddfa461SMichael Halcrow rc = -ENOMEM; 182dddfa461SMichael Halcrow goto out; 183dddfa461SMichael Halcrow } 184dddfa461SMichael Halcrow message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE; 185f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 186dddfa461SMichael Halcrow &packet_size_len); 187dddfa461SMichael Halcrow if (rc) { 188dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 189dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 190dddfa461SMichael Halcrow goto out; 191dddfa461SMichael Halcrow } 192dddfa461SMichael Halcrow i += packet_size_len; 193dddfa461SMichael Halcrow memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 194dddfa461SMichael Halcrow i += ECRYPTFS_SIG_SIZE_HEX; 195f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], 196f66e883eSMichael Halcrow session_key->encrypted_key_size, 197dddfa461SMichael Halcrow &packet_size_len); 198dddfa461SMichael Halcrow if (rc) { 199dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 200dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 201dddfa461SMichael Halcrow goto out; 202dddfa461SMichael Halcrow } 203dddfa461SMichael Halcrow i += packet_size_len; 204dddfa461SMichael Halcrow memcpy(&message[i], session_key->encrypted_key, 205dddfa461SMichael Halcrow session_key->encrypted_key_size); 206dddfa461SMichael Halcrow i += session_key->encrypted_key_size; 207dddfa461SMichael Halcrow *packet_len = i; 208dddfa461SMichael Halcrow out: 209dddfa461SMichael Halcrow return rc; 210dddfa461SMichael Halcrow } 211dddfa461SMichael Halcrow 212dddfa461SMichael Halcrow static int 21319e66a67STrevor Highland parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code, 214dddfa461SMichael Halcrow struct ecryptfs_message *msg) 215dddfa461SMichael Halcrow { 216dddfa461SMichael Halcrow size_t i = 0; 217dddfa461SMichael Halcrow char *data; 218dddfa461SMichael Halcrow size_t data_len; 219dddfa461SMichael Halcrow size_t m_size; 220dddfa461SMichael Halcrow size_t message_len; 221dddfa461SMichael Halcrow u16 checksum = 0; 222dddfa461SMichael Halcrow u16 expected_checksum = 0; 223dddfa461SMichael Halcrow int rc; 224dddfa461SMichael Halcrow 225dddfa461SMichael Halcrow /* 226dddfa461SMichael Halcrow * ***** TAG 65 Packet Format ***** 227dddfa461SMichael Halcrow * | Content Type | 1 byte | 228dddfa461SMichael Halcrow * | Status Indicator | 1 byte | 229dddfa461SMichael Halcrow * | File Encryption Key Size | 1 or 2 bytes | 230dddfa461SMichael Halcrow * | File Encryption Key | arbitrary | 231dddfa461SMichael Halcrow */ 232dddfa461SMichael Halcrow message_len = msg->data_len; 233dddfa461SMichael Halcrow data = msg->data; 234dddfa461SMichael Halcrow if (message_len < 4) { 235dddfa461SMichael Halcrow rc = -EIO; 236dddfa461SMichael Halcrow goto out; 237dddfa461SMichael Halcrow } 238dddfa461SMichael Halcrow if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) { 239dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n"); 240dddfa461SMichael Halcrow rc = -EIO; 241dddfa461SMichael Halcrow goto out; 242dddfa461SMichael Halcrow } 243dddfa461SMichael Halcrow if (data[i++]) { 244dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value " 245dddfa461SMichael Halcrow "[%d]\n", data[i-1]); 246dddfa461SMichael Halcrow rc = -EIO; 247dddfa461SMichael Halcrow goto out; 248dddfa461SMichael Halcrow } 249f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len); 250dddfa461SMichael Halcrow if (rc) { 251dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 252dddfa461SMichael Halcrow "rc = [%d]\n", rc); 253dddfa461SMichael Halcrow goto out; 254dddfa461SMichael Halcrow } 255dddfa461SMichael Halcrow i += data_len; 256dddfa461SMichael Halcrow if (message_len < (i + m_size)) { 257624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd " 258624ae528STyler Hicks "is shorter than expected\n"); 259dddfa461SMichael Halcrow rc = -EIO; 260dddfa461SMichael Halcrow goto out; 261dddfa461SMichael Halcrow } 262dddfa461SMichael Halcrow if (m_size < 3) { 263dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, 264dddfa461SMichael Halcrow "The decrypted key is not long enough to " 265dddfa461SMichael Halcrow "include a cipher code and checksum\n"); 266dddfa461SMichael Halcrow rc = -EIO; 267dddfa461SMichael Halcrow goto out; 268dddfa461SMichael Halcrow } 269dddfa461SMichael Halcrow *cipher_code = data[i++]; 270dddfa461SMichael Halcrow /* The decrypted key includes 1 byte cipher code and 2 byte checksum */ 271dddfa461SMichael Halcrow session_key->decrypted_key_size = m_size - 3; 272dddfa461SMichael Halcrow if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) { 273dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "key_size [%d] larger than " 274dddfa461SMichael Halcrow "the maximum key size [%d]\n", 275dddfa461SMichael Halcrow session_key->decrypted_key_size, 276dddfa461SMichael Halcrow ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 277dddfa461SMichael Halcrow rc = -EIO; 278dddfa461SMichael Halcrow goto out; 279dddfa461SMichael Halcrow } 280dddfa461SMichael Halcrow memcpy(session_key->decrypted_key, &data[i], 281dddfa461SMichael Halcrow session_key->decrypted_key_size); 282dddfa461SMichael Halcrow i += session_key->decrypted_key_size; 283dddfa461SMichael Halcrow expected_checksum += (unsigned char)(data[i++]) << 8; 284dddfa461SMichael Halcrow expected_checksum += (unsigned char)(data[i++]); 285dddfa461SMichael Halcrow for (i = 0; i < session_key->decrypted_key_size; i++) 286dddfa461SMichael Halcrow checksum += session_key->decrypted_key[i]; 287dddfa461SMichael Halcrow if (expected_checksum != checksum) { 288dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Invalid checksum for file " 289dddfa461SMichael Halcrow "encryption key; expected [%x]; calculated " 290dddfa461SMichael Halcrow "[%x]\n", expected_checksum, checksum); 291dddfa461SMichael Halcrow rc = -EIO; 292dddfa461SMichael Halcrow } 293dddfa461SMichael Halcrow out: 294dddfa461SMichael Halcrow return rc; 295dddfa461SMichael Halcrow } 296dddfa461SMichael Halcrow 297dddfa461SMichael Halcrow 298dddfa461SMichael Halcrow static int 29919e66a67STrevor Highland write_tag_66_packet(char *signature, u8 cipher_code, 300dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, char **packet, 301dddfa461SMichael Halcrow size_t *packet_len) 302dddfa461SMichael Halcrow { 303dddfa461SMichael Halcrow size_t i = 0; 304dddfa461SMichael Halcrow size_t j; 305dddfa461SMichael Halcrow size_t data_len; 306dddfa461SMichael Halcrow size_t checksum = 0; 307dddfa461SMichael Halcrow size_t packet_size_len; 308dddfa461SMichael Halcrow char *message; 309dddfa461SMichael Halcrow int rc; 310dddfa461SMichael Halcrow 311dddfa461SMichael Halcrow /* 312dddfa461SMichael Halcrow * ***** TAG 66 Packet Format ***** 313dddfa461SMichael Halcrow * | Content Type | 1 byte | 314dddfa461SMichael Halcrow * | Key Identifier Size | 1 or 2 bytes | 315dddfa461SMichael Halcrow * | Key Identifier | arbitrary | 316dddfa461SMichael Halcrow * | File Encryption Key Size | 1 or 2 bytes | 317dddfa461SMichael Halcrow * | File Encryption Key | arbitrary | 318dddfa461SMichael Halcrow */ 319dddfa461SMichael Halcrow data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); 320dddfa461SMichael Halcrow *packet = kmalloc(data_len, GFP_KERNEL); 321dddfa461SMichael Halcrow message = *packet; 322dddfa461SMichael Halcrow if (!message) { 323dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 324dddfa461SMichael Halcrow rc = -ENOMEM; 325dddfa461SMichael Halcrow goto out; 326dddfa461SMichael Halcrow } 327dddfa461SMichael Halcrow message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE; 328f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 329dddfa461SMichael Halcrow &packet_size_len); 330dddfa461SMichael Halcrow if (rc) { 331dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 332dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 333dddfa461SMichael Halcrow goto out; 334dddfa461SMichael Halcrow } 335dddfa461SMichael Halcrow i += packet_size_len; 336dddfa461SMichael Halcrow memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 337dddfa461SMichael Halcrow i += ECRYPTFS_SIG_SIZE_HEX; 338dddfa461SMichael Halcrow /* The encrypted key includes 1 byte cipher code and 2 byte checksum */ 339f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3, 340dddfa461SMichael Halcrow &packet_size_len); 341dddfa461SMichael Halcrow if (rc) { 342dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 343dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 344dddfa461SMichael Halcrow goto out; 345dddfa461SMichael Halcrow } 346dddfa461SMichael Halcrow i += packet_size_len; 347dddfa461SMichael Halcrow message[i++] = cipher_code; 348dddfa461SMichael Halcrow memcpy(&message[i], crypt_stat->key, crypt_stat->key_size); 349dddfa461SMichael Halcrow i += crypt_stat->key_size; 350dddfa461SMichael Halcrow for (j = 0; j < crypt_stat->key_size; j++) 351dddfa461SMichael Halcrow checksum += crypt_stat->key[j]; 352dddfa461SMichael Halcrow message[i++] = (checksum / 256) % 256; 353dddfa461SMichael Halcrow message[i++] = (checksum % 256); 354dddfa461SMichael Halcrow *packet_len = i; 355dddfa461SMichael Halcrow out: 356dddfa461SMichael Halcrow return rc; 357dddfa461SMichael Halcrow } 358dddfa461SMichael Halcrow 359dddfa461SMichael Halcrow static int 360dddfa461SMichael Halcrow parse_tag_67_packet(struct ecryptfs_key_record *key_rec, 361dddfa461SMichael Halcrow struct ecryptfs_message *msg) 362dddfa461SMichael Halcrow { 363dddfa461SMichael Halcrow size_t i = 0; 364dddfa461SMichael Halcrow char *data; 365dddfa461SMichael Halcrow size_t data_len; 366dddfa461SMichael Halcrow size_t message_len; 367dddfa461SMichael Halcrow int rc; 368dddfa461SMichael Halcrow 369dddfa461SMichael Halcrow /* 370dddfa461SMichael Halcrow * ***** TAG 65 Packet Format ***** 371dddfa461SMichael Halcrow * | Content Type | 1 byte | 372dddfa461SMichael Halcrow * | Status Indicator | 1 byte | 373dddfa461SMichael Halcrow * | Encrypted File Encryption Key Size | 1 or 2 bytes | 374dddfa461SMichael Halcrow * | Encrypted File Encryption Key | arbitrary | 375dddfa461SMichael Halcrow */ 376dddfa461SMichael Halcrow message_len = msg->data_len; 377dddfa461SMichael Halcrow data = msg->data; 378dddfa461SMichael Halcrow /* verify that everything through the encrypted FEK size is present */ 379dddfa461SMichael Halcrow if (message_len < 4) { 380dddfa461SMichael Halcrow rc = -EIO; 381df261c52SMichael Halcrow printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable " 382f66e883eSMichael Halcrow "message length is [%d]\n", __func__, message_len, 4); 383dddfa461SMichael Halcrow goto out; 384dddfa461SMichael Halcrow } 385dddfa461SMichael Halcrow if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { 386dddfa461SMichael Halcrow rc = -EIO; 387f66e883eSMichael Halcrow printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n", 388f66e883eSMichael Halcrow __func__); 389dddfa461SMichael Halcrow goto out; 390dddfa461SMichael Halcrow } 391dddfa461SMichael Halcrow if (data[i++]) { 392dddfa461SMichael Halcrow rc = -EIO; 393f66e883eSMichael Halcrow printk(KERN_ERR "%s: Status indicator has non zero " 394f66e883eSMichael Halcrow "value [%d]\n", __func__, data[i-1]); 395f66e883eSMichael Halcrow 396dddfa461SMichael Halcrow goto out; 397dddfa461SMichael Halcrow } 398f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size, 399f66e883eSMichael Halcrow &data_len); 400dddfa461SMichael Halcrow if (rc) { 401dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 402dddfa461SMichael Halcrow "rc = [%d]\n", rc); 403dddfa461SMichael Halcrow goto out; 404dddfa461SMichael Halcrow } 405dddfa461SMichael Halcrow i += data_len; 406dddfa461SMichael Halcrow if (message_len < (i + key_rec->enc_key_size)) { 407dddfa461SMichael Halcrow rc = -EIO; 408df261c52SMichael Halcrow printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n", 409f66e883eSMichael Halcrow __func__, message_len, (i + key_rec->enc_key_size)); 410dddfa461SMichael Halcrow goto out; 411dddfa461SMichael Halcrow } 412dddfa461SMichael Halcrow if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 413f66e883eSMichael Halcrow rc = -EIO; 414df261c52SMichael Halcrow printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than " 415f66e883eSMichael Halcrow "the maximum key size [%d]\n", __func__, 416dddfa461SMichael Halcrow key_rec->enc_key_size, 417dddfa461SMichael Halcrow ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 418dddfa461SMichael Halcrow goto out; 419dddfa461SMichael Halcrow } 420dddfa461SMichael Halcrow memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); 421dddfa461SMichael Halcrow out: 422dddfa461SMichael Halcrow return rc; 423dddfa461SMichael Halcrow } 424dddfa461SMichael Halcrow 4250e1fc5efSRoberto Sassu /** 4260e1fc5efSRoberto Sassu * ecryptfs_verify_version 4270e1fc5efSRoberto Sassu * @version: The version number to confirm 4280e1fc5efSRoberto Sassu * 4290e1fc5efSRoberto Sassu * Returns zero on good version; non-zero otherwise 4300e1fc5efSRoberto Sassu */ 4310e1fc5efSRoberto Sassu static int ecryptfs_verify_version(u16 version) 4320e1fc5efSRoberto Sassu { 4330e1fc5efSRoberto Sassu int rc = 0; 4340e1fc5efSRoberto Sassu unsigned char major; 4350e1fc5efSRoberto Sassu unsigned char minor; 4360e1fc5efSRoberto Sassu 4370e1fc5efSRoberto Sassu major = ((version >> 8) & 0xFF); 4380e1fc5efSRoberto Sassu minor = (version & 0xFF); 4390e1fc5efSRoberto Sassu if (major != ECRYPTFS_VERSION_MAJOR) { 4400e1fc5efSRoberto Sassu ecryptfs_printk(KERN_ERR, "Major version number mismatch. " 4410e1fc5efSRoberto Sassu "Expected [%d]; got [%d]\n", 4420e1fc5efSRoberto Sassu ECRYPTFS_VERSION_MAJOR, major); 4430e1fc5efSRoberto Sassu rc = -EINVAL; 4440e1fc5efSRoberto Sassu goto out; 4450e1fc5efSRoberto Sassu } 4460e1fc5efSRoberto Sassu if (minor != ECRYPTFS_VERSION_MINOR) { 4470e1fc5efSRoberto Sassu ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " 4480e1fc5efSRoberto Sassu "Expected [%d]; got [%d]\n", 4490e1fc5efSRoberto Sassu ECRYPTFS_VERSION_MINOR, minor); 4500e1fc5efSRoberto Sassu rc = -EINVAL; 4510e1fc5efSRoberto Sassu goto out; 4520e1fc5efSRoberto Sassu } 4530e1fc5efSRoberto Sassu out: 4540e1fc5efSRoberto Sassu return rc; 4550e1fc5efSRoberto Sassu } 4560e1fc5efSRoberto Sassu 4570e1fc5efSRoberto Sassu /** 4580e1fc5efSRoberto Sassu * ecryptfs_verify_auth_tok_from_key 4590e1fc5efSRoberto Sassu * @auth_tok_key: key containing the authentication token 4600e1fc5efSRoberto Sassu * @auth_tok: authentication token 4610e1fc5efSRoberto Sassu * 462f66665c0SEric Biggers * Returns zero on valid auth tok; -EINVAL if the payload is invalid; or 463f66665c0SEric Biggers * -EKEYREVOKED if the key was revoked before we acquired its semaphore. 4640e1fc5efSRoberto Sassu */ 4650e1fc5efSRoberto Sassu static int 4660e1fc5efSRoberto Sassu ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key, 4670e1fc5efSRoberto Sassu struct ecryptfs_auth_tok **auth_tok) 4680e1fc5efSRoberto Sassu { 4690e1fc5efSRoberto Sassu int rc = 0; 4700e1fc5efSRoberto Sassu 4710e1fc5efSRoberto Sassu (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key); 472f66665c0SEric Biggers if (IS_ERR(*auth_tok)) { 473f66665c0SEric Biggers rc = PTR_ERR(*auth_tok); 474f66665c0SEric Biggers *auth_tok = NULL; 475f66665c0SEric Biggers goto out; 476f66665c0SEric Biggers } 477f66665c0SEric Biggers 4780e1fc5efSRoberto Sassu if (ecryptfs_verify_version((*auth_tok)->version)) { 4790e1fc5efSRoberto Sassu printk(KERN_ERR "Data structure version mismatch. Userspace " 4800e1fc5efSRoberto Sassu "tools must match eCryptfs kernel module with major " 4810e1fc5efSRoberto Sassu "version [%d] and minor version [%d]\n", 4820e1fc5efSRoberto Sassu ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR); 4830e1fc5efSRoberto Sassu rc = -EINVAL; 4840e1fc5efSRoberto Sassu goto out; 4850e1fc5efSRoberto Sassu } 4860e1fc5efSRoberto Sassu if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD 4870e1fc5efSRoberto Sassu && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { 4880e1fc5efSRoberto Sassu printk(KERN_ERR "Invalid auth_tok structure " 4890e1fc5efSRoberto Sassu "returned from key query\n"); 4900e1fc5efSRoberto Sassu rc = -EINVAL; 4910e1fc5efSRoberto Sassu goto out; 4920e1fc5efSRoberto Sassu } 4930e1fc5efSRoberto Sassu out: 4940e1fc5efSRoberto Sassu return rc; 4950e1fc5efSRoberto Sassu } 4960e1fc5efSRoberto Sassu 497cd9d67dfSMichael Halcrow static int 4989c79f34fSMichael Halcrow ecryptfs_find_global_auth_tok_for_sig( 4990e1fc5efSRoberto Sassu struct key **auth_tok_key, 5000e1fc5efSRoberto Sassu struct ecryptfs_auth_tok **auth_tok, 5019c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) 5029c79f34fSMichael Halcrow { 5039c79f34fSMichael Halcrow struct ecryptfs_global_auth_tok *walker; 5049c79f34fSMichael Halcrow int rc = 0; 5059c79f34fSMichael Halcrow 5060e1fc5efSRoberto Sassu (*auth_tok_key) = NULL; 5070e1fc5efSRoberto Sassu (*auth_tok) = NULL; 5089c79f34fSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 5099c79f34fSMichael Halcrow list_for_each_entry(walker, 5109c79f34fSMichael Halcrow &mount_crypt_stat->global_auth_tok_list, 5119c79f34fSMichael Halcrow mount_crypt_stat_list) { 5120e1fc5efSRoberto Sassu if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX)) 5130e1fc5efSRoberto Sassu continue; 5140e1fc5efSRoberto Sassu 5150e1fc5efSRoberto Sassu if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) { 5160e1fc5efSRoberto Sassu rc = -EINVAL; 5179c79f34fSMichael Halcrow goto out; 5189c79f34fSMichael Halcrow } 5190e1fc5efSRoberto Sassu 5200e1fc5efSRoberto Sassu rc = key_validate(walker->global_auth_tok_key); 5210e1fc5efSRoberto Sassu if (rc) { 5220e1fc5efSRoberto Sassu if (rc == -EKEYEXPIRED) 5230e1fc5efSRoberto Sassu goto out; 5240e1fc5efSRoberto Sassu goto out_invalid_auth_tok; 5259c79f34fSMichael Halcrow } 5260e1fc5efSRoberto Sassu 527b5695d04SRoberto Sassu down_write(&(walker->global_auth_tok_key->sem)); 5280e1fc5efSRoberto Sassu rc = ecryptfs_verify_auth_tok_from_key( 5290e1fc5efSRoberto Sassu walker->global_auth_tok_key, auth_tok); 5300e1fc5efSRoberto Sassu if (rc) 531b5695d04SRoberto Sassu goto out_invalid_auth_tok_unlock; 5320e1fc5efSRoberto Sassu 5330e1fc5efSRoberto Sassu (*auth_tok_key) = walker->global_auth_tok_key; 5340e1fc5efSRoberto Sassu key_get(*auth_tok_key); 5350e1fc5efSRoberto Sassu goto out; 5360e1fc5efSRoberto Sassu } 5370e1fc5efSRoberto Sassu rc = -ENOENT; 5380e1fc5efSRoberto Sassu goto out; 539b5695d04SRoberto Sassu out_invalid_auth_tok_unlock: 540b5695d04SRoberto Sassu up_write(&(walker->global_auth_tok_key->sem)); 5410e1fc5efSRoberto Sassu out_invalid_auth_tok: 5420e1fc5efSRoberto Sassu printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig); 5430e1fc5efSRoberto Sassu walker->flags |= ECRYPTFS_AUTH_TOK_INVALID; 5440e1fc5efSRoberto Sassu key_put(walker->global_auth_tok_key); 5450e1fc5efSRoberto Sassu walker->global_auth_tok_key = NULL; 5469c79f34fSMichael Halcrow out: 5479c79f34fSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 5489c79f34fSMichael Halcrow return rc; 5499c79f34fSMichael Halcrow } 5509c79f34fSMichael Halcrow 5519c79f34fSMichael Halcrow /** 5529c79f34fSMichael Halcrow * ecryptfs_find_auth_tok_for_sig 5539c79f34fSMichael Halcrow * @auth_tok: Set to the matching auth_tok; NULL if not found 5549c79f34fSMichael Halcrow * @crypt_stat: inode crypt_stat crypto context 5559c79f34fSMichael Halcrow * @sig: Sig of auth_tok to find 5569c79f34fSMichael Halcrow * 5579c79f34fSMichael Halcrow * For now, this function simply looks at the registered auth_tok's 5589c79f34fSMichael Halcrow * linked off the mount_crypt_stat, so all the auth_toks that can be 5599c79f34fSMichael Halcrow * used must be registered at mount time. This function could 5609c79f34fSMichael Halcrow * potentially try a lot harder to find auth_tok's (e.g., by calling 5619c79f34fSMichael Halcrow * out to ecryptfsd to dynamically retrieve an auth_tok object) so 5629c79f34fSMichael Halcrow * that static registration of auth_tok's will no longer be necessary. 5639c79f34fSMichael Halcrow * 5649c79f34fSMichael Halcrow * Returns zero on no error; non-zero on error 5659c79f34fSMichael Halcrow */ 5669c79f34fSMichael Halcrow static int 5679c79f34fSMichael Halcrow ecryptfs_find_auth_tok_for_sig( 568aee683b9SRoberto Sassu struct key **auth_tok_key, 5699c79f34fSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 5709c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 5719c79f34fSMichael Halcrow char *sig) 5729c79f34fSMichael Halcrow { 5739c79f34fSMichael Halcrow int rc = 0; 5749c79f34fSMichael Halcrow 5750e1fc5efSRoberto Sassu rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok, 5760e1fc5efSRoberto Sassu mount_crypt_stat, sig); 5770e1fc5efSRoberto Sassu if (rc == -ENOENT) { 578f16feb51SRoberto Sassu /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the 579f16feb51SRoberto Sassu * mount_crypt_stat structure, we prevent to use auth toks that 580f16feb51SRoberto Sassu * are not inserted through the ecryptfs_add_global_auth_tok 581f16feb51SRoberto Sassu * function. 582f16feb51SRoberto Sassu */ 583f16feb51SRoberto Sassu if (mount_crypt_stat->flags 584f16feb51SRoberto Sassu & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) 585f16feb51SRoberto Sassu return -EINVAL; 586f16feb51SRoberto Sassu 587aee683b9SRoberto Sassu rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok, 5889c79f34fSMichael Halcrow sig); 5890e1fc5efSRoberto Sassu } 5909c79f34fSMichael Halcrow return rc; 5919c79f34fSMichael Halcrow } 5929c79f34fSMichael Halcrow 5939c79f34fSMichael Halcrow /** 5949c79f34fSMichael Halcrow * write_tag_70_packet can gobble a lot of stack space. We stuff most 5959c79f34fSMichael Halcrow * of the function's parameters in a kmalloc'd struct to help reduce 5969c79f34fSMichael Halcrow * eCryptfs' overall stack usage. 5979c79f34fSMichael Halcrow */ 5989c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack { 5999c79f34fSMichael Halcrow u8 cipher_code; 6009c79f34fSMichael Halcrow size_t max_packet_size; 6019c79f34fSMichael Halcrow size_t packet_size_len; 6029c79f34fSMichael Halcrow size_t block_aligned_filename_size; 6039c79f34fSMichael Halcrow size_t block_size; 6049c79f34fSMichael Halcrow size_t i; 6059c79f34fSMichael Halcrow size_t j; 6069c79f34fSMichael Halcrow size_t num_rand_bytes; 6079c79f34fSMichael Halcrow struct mutex *tfm_mutex; 6089c79f34fSMichael Halcrow char *block_aligned_filename; 6099c79f34fSMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 6108d08dab7STyler Hicks struct scatterlist src_sg[2]; 6118d08dab7STyler Hicks struct scatterlist dst_sg[2]; 6123095e8e3SHerbert Xu struct crypto_skcipher *skcipher_tfm; 6133095e8e3SHerbert Xu struct skcipher_request *skcipher_req; 6149c79f34fSMichael Halcrow char iv[ECRYPTFS_MAX_IV_BYTES]; 6159c79f34fSMichael Halcrow char hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 6169c79f34fSMichael Halcrow char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 6173095e8e3SHerbert Xu struct crypto_shash *hash_tfm; 6183095e8e3SHerbert Xu struct shash_desc *hash_desc; 6199c79f34fSMichael Halcrow }; 6209c79f34fSMichael Halcrow 6219c79f34fSMichael Halcrow /** 6229c79f34fSMichael Halcrow * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK 6239c79f34fSMichael Halcrow * @filename: NULL-terminated filename string 6249c79f34fSMichael Halcrow * 6259c79f34fSMichael Halcrow * This is the simplest mechanism for achieving filename encryption in 6269c79f34fSMichael Halcrow * eCryptfs. It encrypts the given filename with the mount-wide 6279c79f34fSMichael Halcrow * filename encryption key (FNEK) and stores it in a packet to @dest, 6289c79f34fSMichael Halcrow * which the callee will encode and write directly into the dentry 6299c79f34fSMichael Halcrow * name. 6309c79f34fSMichael Halcrow */ 6319c79f34fSMichael Halcrow int 6329c79f34fSMichael Halcrow ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, 6339c79f34fSMichael Halcrow size_t *packet_size, 6349c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 6359c79f34fSMichael Halcrow char *filename, size_t filename_size) 6369c79f34fSMichael Halcrow { 6379c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack *s; 638aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 6399c79f34fSMichael Halcrow int rc = 0; 6409c79f34fSMichael Halcrow 6413095e8e3SHerbert Xu s = kzalloc(sizeof(*s), GFP_KERNEL); 6429c79f34fSMichael Halcrow if (!s) { 6439c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " 644df261c52SMichael Halcrow "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); 645d1558f4eSHerbert Xu return -ENOMEM; 6469c79f34fSMichael Halcrow } 6479c79f34fSMichael Halcrow (*packet_size) = 0; 648950983fcSRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig( 649950983fcSRoberto Sassu &auth_tok_key, 650950983fcSRoberto Sassu &s->auth_tok, mount_crypt_stat, 651950983fcSRoberto Sassu mount_crypt_stat->global_default_fnek_sig); 652950983fcSRoberto Sassu if (rc) { 653950983fcSRoberto Sassu printk(KERN_ERR "%s: Error attempting to find auth tok for " 654950983fcSRoberto Sassu "fnek sig [%s]; rc = [%d]\n", __func__, 655950983fcSRoberto Sassu mount_crypt_stat->global_default_fnek_sig, rc); 656950983fcSRoberto Sassu goto out; 657950983fcSRoberto Sassu } 6589c79f34fSMichael Halcrow rc = ecryptfs_get_tfm_and_mutex_for_cipher_name( 6593095e8e3SHerbert Xu &s->skcipher_tfm, 6609c79f34fSMichael Halcrow &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name); 6619c79f34fSMichael Halcrow if (unlikely(rc)) { 6629c79f34fSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 6639c79f34fSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 6649c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, rc); 6659c79f34fSMichael Halcrow goto out; 6669c79f34fSMichael Halcrow } 6679c79f34fSMichael Halcrow mutex_lock(s->tfm_mutex); 6683095e8e3SHerbert Xu s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm); 6699c79f34fSMichael Halcrow /* Plus one for the \0 separator between the random prefix 6709c79f34fSMichael Halcrow * and the plaintext filename */ 6719c79f34fSMichael Halcrow s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1); 6729c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->num_rand_bytes + filename_size); 6739c79f34fSMichael Halcrow if ((s->block_aligned_filename_size % s->block_size) != 0) { 6749c79f34fSMichael Halcrow s->num_rand_bytes += (s->block_size 6759c79f34fSMichael Halcrow - (s->block_aligned_filename_size 6769c79f34fSMichael Halcrow % s->block_size)); 6779c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->num_rand_bytes 6789c79f34fSMichael Halcrow + filename_size); 6799c79f34fSMichael Halcrow } 6809c79f34fSMichael Halcrow /* Octet 0: Tag 70 identifier 6819c79f34fSMichael Halcrow * Octets 1-N1: Tag 70 packet size (includes cipher identifier 6829c79f34fSMichael Halcrow * and block-aligned encrypted filename size) 6839c79f34fSMichael Halcrow * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 6849c79f34fSMichael Halcrow * Octet N2-N3: Cipher identifier (1 octet) 6859c79f34fSMichael Halcrow * Octets N3-N4: Block-aligned encrypted filename 6869c79f34fSMichael Halcrow * - Consists of a minimum number of random characters, a \0 6879c79f34fSMichael Halcrow * separator, and then the filename */ 6884a26620dSTyler Hicks s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE 6899c79f34fSMichael Halcrow + s->block_aligned_filename_size); 6909c79f34fSMichael Halcrow if (dest == NULL) { 6919c79f34fSMichael Halcrow (*packet_size) = s->max_packet_size; 6929c79f34fSMichael Halcrow goto out_unlock; 6939c79f34fSMichael Halcrow } 6949c79f34fSMichael Halcrow if (s->max_packet_size > (*remaining_bytes)) { 695a8f12864SMichael Halcrow printk(KERN_WARNING "%s: Require [%zd] bytes to write; only " 696a8f12864SMichael Halcrow "[%zd] available\n", __func__, s->max_packet_size, 6979c79f34fSMichael Halcrow (*remaining_bytes)); 6989c79f34fSMichael Halcrow rc = -EINVAL; 6999c79f34fSMichael Halcrow goto out_unlock; 7009c79f34fSMichael Halcrow } 7013095e8e3SHerbert Xu 7023095e8e3SHerbert Xu s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 7033095e8e3SHerbert Xu if (!s->skcipher_req) { 7043095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 7053095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 7063095e8e3SHerbert Xu crypto_skcipher_driver_name(s->skcipher_tfm)); 7073095e8e3SHerbert Xu rc = -ENOMEM; 7083095e8e3SHerbert Xu goto out_unlock; 7093095e8e3SHerbert Xu } 7103095e8e3SHerbert Xu 7113095e8e3SHerbert Xu skcipher_request_set_callback(s->skcipher_req, 7123095e8e3SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 7133095e8e3SHerbert Xu 7149c79f34fSMichael Halcrow s->block_aligned_filename = kzalloc(s->block_aligned_filename_size, 7159c79f34fSMichael Halcrow GFP_KERNEL); 7169c79f34fSMichael Halcrow if (!s->block_aligned_filename) { 7179c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 718df261c52SMichael Halcrow "kzalloc [%zd] bytes\n", __func__, 7199c79f34fSMichael Halcrow s->block_aligned_filename_size); 7209c79f34fSMichael Halcrow rc = -ENOMEM; 7219c79f34fSMichael Halcrow goto out_unlock; 7229c79f34fSMichael Halcrow } 7239c79f34fSMichael Halcrow dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE; 7249c79f34fSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[s->i], 7259c79f34fSMichael Halcrow (ECRYPTFS_SIG_SIZE 7269c79f34fSMichael Halcrow + 1 /* Cipher code */ 7279c79f34fSMichael Halcrow + s->block_aligned_filename_size), 7289c79f34fSMichael Halcrow &s->packet_size_len); 7299c79f34fSMichael Halcrow if (rc) { 7309c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error generating tag 70 packet " 7319c79f34fSMichael Halcrow "header; cannot generate packet length; rc = [%d]\n", 7329c79f34fSMichael Halcrow __func__, rc); 7339c79f34fSMichael Halcrow goto out_free_unlock; 7349c79f34fSMichael Halcrow } 7359c79f34fSMichael Halcrow s->i += s->packet_size_len; 7369c79f34fSMichael Halcrow ecryptfs_from_hex(&dest[s->i], 7379c79f34fSMichael Halcrow mount_crypt_stat->global_default_fnek_sig, 7389c79f34fSMichael Halcrow ECRYPTFS_SIG_SIZE); 7399c79f34fSMichael Halcrow s->i += ECRYPTFS_SIG_SIZE; 7409c79f34fSMichael Halcrow s->cipher_code = ecryptfs_code_for_cipher_string( 7419c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, 7429c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 7439c79f34fSMichael Halcrow if (s->cipher_code == 0) { 7449c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Unable to generate code for " 745a8f12864SMichael Halcrow "cipher [%s] with key bytes [%zd]\n", __func__, 7469c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, 7479c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 7489c79f34fSMichael Halcrow rc = -EINVAL; 7499c79f34fSMichael Halcrow goto out_free_unlock; 7509c79f34fSMichael Halcrow } 7519c79f34fSMichael Halcrow dest[s->i++] = s->cipher_code; 7529c79f34fSMichael Halcrow /* TODO: Support other key modules than passphrase for 7539c79f34fSMichael Halcrow * filename encryption */ 754df6ad33bSTyler Hicks if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 755df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 756df6ad33bSTyler Hicks printk(KERN_INFO "%s: Filename encryption only supports " 757df6ad33bSTyler Hicks "password tokens\n", __func__); 758df6ad33bSTyler Hicks goto out_free_unlock; 759df6ad33bSTyler Hicks } 7603095e8e3SHerbert Xu s->hash_tfm = crypto_alloc_shash(ECRYPTFS_TAG_70_DIGEST, 0, 0); 7613095e8e3SHerbert Xu if (IS_ERR(s->hash_tfm)) { 7623095e8e3SHerbert Xu rc = PTR_ERR(s->hash_tfm); 7639c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to " 7649c79f34fSMichael Halcrow "allocate hash crypto context; rc = [%d]\n", 7659c79f34fSMichael Halcrow __func__, rc); 7669c79f34fSMichael Halcrow goto out_free_unlock; 7679c79f34fSMichael Halcrow } 7683095e8e3SHerbert Xu 7693095e8e3SHerbert Xu s->hash_desc = kmalloc(sizeof(*s->hash_desc) + 7703095e8e3SHerbert Xu crypto_shash_descsize(s->hash_tfm), GFP_KERNEL); 7713095e8e3SHerbert Xu if (!s->hash_desc) { 7723095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 7733095e8e3SHerbert Xu "kmalloc [%zd] bytes\n", __func__, 7743095e8e3SHerbert Xu sizeof(*s->hash_desc) + 7753095e8e3SHerbert Xu crypto_shash_descsize(s->hash_tfm)); 7763095e8e3SHerbert Xu rc = -ENOMEM; 7779c79f34fSMichael Halcrow goto out_release_free_unlock; 7789c79f34fSMichael Halcrow } 7793095e8e3SHerbert Xu 7803095e8e3SHerbert Xu s->hash_desc->tfm = s->hash_tfm; 7813095e8e3SHerbert Xu s->hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; 7823095e8e3SHerbert Xu 7833095e8e3SHerbert Xu rc = crypto_shash_digest(s->hash_desc, 7843095e8e3SHerbert Xu (u8 *)s->auth_tok->token.password.session_key_encryption_key, 7853095e8e3SHerbert Xu s->auth_tok->token.password.session_key_encryption_key_bytes, 7863095e8e3SHerbert Xu s->hash); 7879c79f34fSMichael Halcrow if (rc) { 7889c79f34fSMichael Halcrow printk(KERN_ERR 7893095e8e3SHerbert Xu "%s: Error computing crypto hash; rc = [%d]\n", 7909c79f34fSMichael Halcrow __func__, rc); 7919c79f34fSMichael Halcrow goto out_release_free_unlock; 7929c79f34fSMichael Halcrow } 7939c79f34fSMichael Halcrow for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) { 7949c79f34fSMichael Halcrow s->block_aligned_filename[s->j] = 7959c79f34fSMichael Halcrow s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)]; 7969c79f34fSMichael Halcrow if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE) 7979c79f34fSMichael Halcrow == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) { 7983095e8e3SHerbert Xu rc = crypto_shash_digest(s->hash_desc, (u8 *)s->hash, 7993095e8e3SHerbert Xu ECRYPTFS_TAG_70_DIGEST_SIZE, 8003095e8e3SHerbert Xu s->tmp_hash); 8019c79f34fSMichael Halcrow if (rc) { 8029c79f34fSMichael Halcrow printk(KERN_ERR 8033095e8e3SHerbert Xu "%s: Error computing crypto hash; " 8049c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 8059c79f34fSMichael Halcrow goto out_release_free_unlock; 8069c79f34fSMichael Halcrow } 8079c79f34fSMichael Halcrow memcpy(s->hash, s->tmp_hash, 8089c79f34fSMichael Halcrow ECRYPTFS_TAG_70_DIGEST_SIZE); 8099c79f34fSMichael Halcrow } 8109c79f34fSMichael Halcrow if (s->block_aligned_filename[s->j] == '\0') 8119c79f34fSMichael Halcrow s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL; 8129c79f34fSMichael Halcrow } 8139c79f34fSMichael Halcrow memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename, 8149c79f34fSMichael Halcrow filename_size); 8159c79f34fSMichael Halcrow rc = virt_to_scatterlist(s->block_aligned_filename, 8168d08dab7STyler Hicks s->block_aligned_filename_size, s->src_sg, 2); 8178d08dab7STyler Hicks if (rc < 1) { 8189c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 8198d08dab7STyler Hicks "convert filename memory to scatterlist; rc = [%d]. " 820a8f12864SMichael Halcrow "block_aligned_filename_size = [%zd]\n", __func__, rc, 8219c79f34fSMichael Halcrow s->block_aligned_filename_size); 8229c79f34fSMichael Halcrow goto out_release_free_unlock; 8239c79f34fSMichael Halcrow } 8249c79f34fSMichael Halcrow rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size, 8258d08dab7STyler Hicks s->dst_sg, 2); 8268d08dab7STyler Hicks if (rc < 1) { 8279c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 8289c79f34fSMichael Halcrow "convert encrypted filename memory to scatterlist; " 8298d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 8308d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 8319c79f34fSMichael Halcrow goto out_release_free_unlock; 8329c79f34fSMichael Halcrow } 8339c79f34fSMichael Halcrow /* The characters in the first block effectively do the job 8349c79f34fSMichael Halcrow * of the IV here, so we just use 0's for the IV. Note the 8359c79f34fSMichael Halcrow * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 8369c79f34fSMichael Halcrow * >= ECRYPTFS_MAX_IV_BYTES. */ 8373095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 8383095e8e3SHerbert Xu s->skcipher_tfm, 8399c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 8409c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 8419c79f34fSMichael Halcrow if (rc < 0) { 8429c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error setting key for crypto context; " 8439c79f34fSMichael Halcrow "rc = [%d]. s->auth_tok->token.password.session_key_" 8449c79f34fSMichael Halcrow "encryption_key = [0x%p]; mount_crypt_stat->" 845df261c52SMichael Halcrow "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 8469c79f34fSMichael Halcrow rc, 8479c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 8489c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 8499c79f34fSMichael Halcrow goto out_release_free_unlock; 8509c79f34fSMichael Halcrow } 8513095e8e3SHerbert Xu skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 8523095e8e3SHerbert Xu s->block_aligned_filename_size, s->iv); 8533095e8e3SHerbert Xu rc = crypto_skcipher_encrypt(s->skcipher_req); 8549c79f34fSMichael Halcrow if (rc) { 8559c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to encrypt filename; " 8569c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 8579c79f34fSMichael Halcrow goto out_release_free_unlock; 8589c79f34fSMichael Halcrow } 8599c79f34fSMichael Halcrow s->i += s->block_aligned_filename_size; 8609c79f34fSMichael Halcrow (*packet_size) = s->i; 8619c79f34fSMichael Halcrow (*remaining_bytes) -= (*packet_size); 8629c79f34fSMichael Halcrow out_release_free_unlock: 8633095e8e3SHerbert Xu crypto_free_shash(s->hash_tfm); 8649c79f34fSMichael Halcrow out_free_unlock: 86500fcf2cbSJohannes Weiner kzfree(s->block_aligned_filename); 8669c79f34fSMichael Halcrow out_unlock: 8679c79f34fSMichael Halcrow mutex_unlock(s->tfm_mutex); 8689c79f34fSMichael Halcrow out: 869b5695d04SRoberto Sassu if (auth_tok_key) { 870b5695d04SRoberto Sassu up_write(&(auth_tok_key->sem)); 871aee683b9SRoberto Sassu key_put(auth_tok_key); 872b5695d04SRoberto Sassu } 8733095e8e3SHerbert Xu skcipher_request_free(s->skcipher_req); 8743095e8e3SHerbert Xu kzfree(s->hash_desc); 8759c79f34fSMichael Halcrow kfree(s); 8769c79f34fSMichael Halcrow return rc; 8779c79f34fSMichael Halcrow } 8789c79f34fSMichael Halcrow 8799c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack { 8809c79f34fSMichael Halcrow u8 cipher_code; 8819c79f34fSMichael Halcrow size_t max_packet_size; 8829c79f34fSMichael Halcrow size_t packet_size_len; 8839c79f34fSMichael Halcrow size_t parsed_tag_70_packet_size; 8849c79f34fSMichael Halcrow size_t block_aligned_filename_size; 8859c79f34fSMichael Halcrow size_t block_size; 8869c79f34fSMichael Halcrow size_t i; 8879c79f34fSMichael Halcrow struct mutex *tfm_mutex; 8889c79f34fSMichael Halcrow char *decrypted_filename; 8899c79f34fSMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 8908d08dab7STyler Hicks struct scatterlist src_sg[2]; 8918d08dab7STyler Hicks struct scatterlist dst_sg[2]; 8923095e8e3SHerbert Xu struct crypto_skcipher *skcipher_tfm; 8933095e8e3SHerbert Xu struct skcipher_request *skcipher_req; 8949c79f34fSMichael Halcrow char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1]; 8959c79f34fSMichael Halcrow char iv[ECRYPTFS_MAX_IV_BYTES]; 8962a559a8bSColin Ian King char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1]; 8979c79f34fSMichael Halcrow }; 8989c79f34fSMichael Halcrow 8999c79f34fSMichael Halcrow /** 9009c79f34fSMichael Halcrow * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet 9019c79f34fSMichael Halcrow * @filename: This function kmalloc's the memory for the filename 9027d8bc2beSMichael Halcrow * @filename_size: This function sets this to the amount of memory 9037d8bc2beSMichael Halcrow * kmalloc'd for the filename 9047d8bc2beSMichael Halcrow * @packet_size: This function sets this to the the number of octets 9057d8bc2beSMichael Halcrow * in the packet parsed 9067d8bc2beSMichael Halcrow * @mount_crypt_stat: The mount-wide cryptographic context 9077d8bc2beSMichael Halcrow * @data: The memory location containing the start of the tag 70 9087d8bc2beSMichael Halcrow * packet 9097d8bc2beSMichael Halcrow * @max_packet_size: The maximum legal size of the packet to be parsed 9107d8bc2beSMichael Halcrow * from @data 9117d8bc2beSMichael Halcrow * 9127d8bc2beSMichael Halcrow * Returns zero on success; non-zero otherwise 9139c79f34fSMichael Halcrow */ 9149c79f34fSMichael Halcrow int 9159c79f34fSMichael Halcrow ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, 9169c79f34fSMichael Halcrow size_t *packet_size, 9179c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 9189c79f34fSMichael Halcrow char *data, size_t max_packet_size) 9199c79f34fSMichael Halcrow { 9209c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack *s; 921aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 9229c79f34fSMichael Halcrow int rc = 0; 9239c79f34fSMichael Halcrow 9249c79f34fSMichael Halcrow (*packet_size) = 0; 9259c79f34fSMichael Halcrow (*filename_size) = 0; 9269c79f34fSMichael Halcrow (*filename) = NULL; 9273095e8e3SHerbert Xu s = kzalloc(sizeof(*s), GFP_KERNEL); 9289c79f34fSMichael Halcrow if (!s) { 9299c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " 930a8f12864SMichael Halcrow "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); 931d1558f4eSHerbert Xu return -ENOMEM; 9329c79f34fSMichael Halcrow } 9334a26620dSTyler Hicks if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) { 934df261c52SMichael Halcrow printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be " 9359c79f34fSMichael Halcrow "at least [%d]\n", __func__, max_packet_size, 9364a26620dSTyler Hicks ECRYPTFS_TAG_70_MIN_METADATA_SIZE); 9379c79f34fSMichael Halcrow rc = -EINVAL; 9389c79f34fSMichael Halcrow goto out; 9399c79f34fSMichael Halcrow } 9409c79f34fSMichael Halcrow /* Octet 0: Tag 70 identifier 9419c79f34fSMichael Halcrow * Octets 1-N1: Tag 70 packet size (includes cipher identifier 9429c79f34fSMichael Halcrow * and block-aligned encrypted filename size) 9439c79f34fSMichael Halcrow * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 9449c79f34fSMichael Halcrow * Octet N2-N3: Cipher identifier (1 octet) 9459c79f34fSMichael Halcrow * Octets N3-N4: Block-aligned encrypted filename 9469c79f34fSMichael Halcrow * - Consists of a minimum number of random numbers, a \0 9479c79f34fSMichael Halcrow * separator, and then the filename */ 9489c79f34fSMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) { 9499c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be " 9509c79f34fSMichael Halcrow "tag [0x%.2x]\n", __func__, 9519c79f34fSMichael Halcrow data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE); 9529c79f34fSMichael Halcrow rc = -EINVAL; 9539c79f34fSMichael Halcrow goto out; 9549c79f34fSMichael Halcrow } 9559c79f34fSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], 9569c79f34fSMichael Halcrow &s->parsed_tag_70_packet_size, 9579c79f34fSMichael Halcrow &s->packet_size_len); 9589c79f34fSMichael Halcrow if (rc) { 9599c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Error parsing packet length; " 9609c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 9619c79f34fSMichael Halcrow goto out; 9629c79f34fSMichael Halcrow } 9639c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->parsed_tag_70_packet_size 9649c79f34fSMichael Halcrow - ECRYPTFS_SIG_SIZE - 1); 9659c79f34fSMichael Halcrow if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size) 9669c79f34fSMichael Halcrow > max_packet_size) { 967a8f12864SMichael Halcrow printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet " 968a8f12864SMichael Halcrow "size is [%zd]\n", __func__, max_packet_size, 9699c79f34fSMichael Halcrow (1 + s->packet_size_len + 1 9709c79f34fSMichael Halcrow + s->block_aligned_filename_size)); 9719c79f34fSMichael Halcrow rc = -EINVAL; 9729c79f34fSMichael Halcrow goto out; 9739c79f34fSMichael Halcrow } 9749c79f34fSMichael Halcrow (*packet_size) += s->packet_size_len; 9759c79f34fSMichael Halcrow ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)], 9769c79f34fSMichael Halcrow ECRYPTFS_SIG_SIZE); 9779c79f34fSMichael Halcrow s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 9789c79f34fSMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 9799c79f34fSMichael Halcrow s->cipher_code = data[(*packet_size)++]; 9809c79f34fSMichael Halcrow rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code); 9819c79f34fSMichael Halcrow if (rc) { 9829c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n", 9839c79f34fSMichael Halcrow __func__, s->cipher_code); 9849c79f34fSMichael Halcrow goto out; 9859c79f34fSMichael Halcrow } 986950983fcSRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 987950983fcSRoberto Sassu &s->auth_tok, mount_crypt_stat, 988950983fcSRoberto Sassu s->fnek_sig_hex); 989950983fcSRoberto Sassu if (rc) { 990950983fcSRoberto Sassu printk(KERN_ERR "%s: Error attempting to find auth tok for " 991950983fcSRoberto Sassu "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex, 992950983fcSRoberto Sassu rc); 993950983fcSRoberto Sassu goto out; 994950983fcSRoberto Sassu } 9953095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm, 9969c79f34fSMichael Halcrow &s->tfm_mutex, 9979c79f34fSMichael Halcrow s->cipher_string); 9989c79f34fSMichael Halcrow if (unlikely(rc)) { 9999c79f34fSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 10009c79f34fSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 10019c79f34fSMichael Halcrow s->cipher_string, rc); 10029c79f34fSMichael Halcrow goto out; 10039c79f34fSMichael Halcrow } 10049c79f34fSMichael Halcrow mutex_lock(s->tfm_mutex); 10059c79f34fSMichael Halcrow rc = virt_to_scatterlist(&data[(*packet_size)], 10068d08dab7STyler Hicks s->block_aligned_filename_size, s->src_sg, 2); 10078d08dab7STyler Hicks if (rc < 1) { 10089c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 10099c79f34fSMichael Halcrow "convert encrypted filename memory to scatterlist; " 10108d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 10118d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 10129c79f34fSMichael Halcrow goto out_unlock; 10139c79f34fSMichael Halcrow } 10149c79f34fSMichael Halcrow (*packet_size) += s->block_aligned_filename_size; 10159c79f34fSMichael Halcrow s->decrypted_filename = kmalloc(s->block_aligned_filename_size, 10169c79f34fSMichael Halcrow GFP_KERNEL); 10179c79f34fSMichael Halcrow if (!s->decrypted_filename) { 10189c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst attempting to " 1019a8f12864SMichael Halcrow "kmalloc [%zd] bytes\n", __func__, 10209c79f34fSMichael Halcrow s->block_aligned_filename_size); 10219c79f34fSMichael Halcrow rc = -ENOMEM; 10229c79f34fSMichael Halcrow goto out_unlock; 10239c79f34fSMichael Halcrow } 10249c79f34fSMichael Halcrow rc = virt_to_scatterlist(s->decrypted_filename, 10258d08dab7STyler Hicks s->block_aligned_filename_size, s->dst_sg, 2); 10268d08dab7STyler Hicks if (rc < 1) { 10279c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 10289c79f34fSMichael Halcrow "convert decrypted filename memory to scatterlist; " 10298d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 10308d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 10319c79f34fSMichael Halcrow goto out_free_unlock; 10329c79f34fSMichael Halcrow } 10333095e8e3SHerbert Xu 10343095e8e3SHerbert Xu s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 10353095e8e3SHerbert Xu if (!s->skcipher_req) { 10363095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 10373095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 10383095e8e3SHerbert Xu crypto_skcipher_driver_name(s->skcipher_tfm)); 10393095e8e3SHerbert Xu rc = -ENOMEM; 10403095e8e3SHerbert Xu goto out_free_unlock; 10413095e8e3SHerbert Xu } 10423095e8e3SHerbert Xu 10433095e8e3SHerbert Xu skcipher_request_set_callback(s->skcipher_req, 10443095e8e3SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 10453095e8e3SHerbert Xu 10469c79f34fSMichael Halcrow /* The characters in the first block effectively do the job of 10479c79f34fSMichael Halcrow * the IV here, so we just use 0's for the IV. Note the 10489c79f34fSMichael Halcrow * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 10499c79f34fSMichael Halcrow * >= ECRYPTFS_MAX_IV_BYTES. */ 10509c79f34fSMichael Halcrow /* TODO: Support other key modules than passphrase for 10519c79f34fSMichael Halcrow * filename encryption */ 1052df6ad33bSTyler Hicks if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 1053df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 1054df6ad33bSTyler Hicks printk(KERN_INFO "%s: Filename encryption only supports " 1055df6ad33bSTyler Hicks "password tokens\n", __func__); 1056df6ad33bSTyler Hicks goto out_free_unlock; 1057df6ad33bSTyler Hicks } 10583095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 10593095e8e3SHerbert Xu s->skcipher_tfm, 10609c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 10619c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 10629c79f34fSMichael Halcrow if (rc < 0) { 10639c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error setting key for crypto context; " 10649c79f34fSMichael Halcrow "rc = [%d]. s->auth_tok->token.password.session_key_" 10659c79f34fSMichael Halcrow "encryption_key = [0x%p]; mount_crypt_stat->" 1066df261c52SMichael Halcrow "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 10679c79f34fSMichael Halcrow rc, 10689c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 10699c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 10709c79f34fSMichael Halcrow goto out_free_unlock; 10719c79f34fSMichael Halcrow } 10723095e8e3SHerbert Xu skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 10733095e8e3SHerbert Xu s->block_aligned_filename_size, s->iv); 10743095e8e3SHerbert Xu rc = crypto_skcipher_decrypt(s->skcipher_req); 10759c79f34fSMichael Halcrow if (rc) { 10769c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to decrypt filename; " 10779c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 10789c79f34fSMichael Halcrow goto out_free_unlock; 10799c79f34fSMichael Halcrow } 10809c79f34fSMichael Halcrow while (s->decrypted_filename[s->i] != '\0' 10819c79f34fSMichael Halcrow && s->i < s->block_aligned_filename_size) 10829c79f34fSMichael Halcrow s->i++; 10839c79f34fSMichael Halcrow if (s->i == s->block_aligned_filename_size) { 10849c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Invalid tag 70 packet; could not " 10859c79f34fSMichael Halcrow "find valid separator between random characters and " 10869c79f34fSMichael Halcrow "the filename\n", __func__); 10879c79f34fSMichael Halcrow rc = -EINVAL; 10889c79f34fSMichael Halcrow goto out_free_unlock; 10899c79f34fSMichael Halcrow } 10909c79f34fSMichael Halcrow s->i++; 10919c79f34fSMichael Halcrow (*filename_size) = (s->block_aligned_filename_size - s->i); 10929c79f34fSMichael Halcrow if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) { 1093df261c52SMichael Halcrow printk(KERN_WARNING "%s: Filename size is [%zd], which is " 10949c79f34fSMichael Halcrow "invalid\n", __func__, (*filename_size)); 10959c79f34fSMichael Halcrow rc = -EINVAL; 10969c79f34fSMichael Halcrow goto out_free_unlock; 10979c79f34fSMichael Halcrow } 10989c79f34fSMichael Halcrow (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL); 10999c79f34fSMichael Halcrow if (!(*filename)) { 11009c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst attempting to " 1101a8f12864SMichael Halcrow "kmalloc [%zd] bytes\n", __func__, 11029c79f34fSMichael Halcrow ((*filename_size) + 1)); 11039c79f34fSMichael Halcrow rc = -ENOMEM; 11049c79f34fSMichael Halcrow goto out_free_unlock; 11059c79f34fSMichael Halcrow } 11069c79f34fSMichael Halcrow memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size)); 11079c79f34fSMichael Halcrow (*filename)[(*filename_size)] = '\0'; 11089c79f34fSMichael Halcrow out_free_unlock: 11099c79f34fSMichael Halcrow kfree(s->decrypted_filename); 11109c79f34fSMichael Halcrow out_unlock: 11119c79f34fSMichael Halcrow mutex_unlock(s->tfm_mutex); 11129c79f34fSMichael Halcrow out: 11139c79f34fSMichael Halcrow if (rc) { 11149c79f34fSMichael Halcrow (*packet_size) = 0; 11159c79f34fSMichael Halcrow (*filename_size) = 0; 11169c79f34fSMichael Halcrow (*filename) = NULL; 11179c79f34fSMichael Halcrow } 1118b5695d04SRoberto Sassu if (auth_tok_key) { 1119b5695d04SRoberto Sassu up_write(&(auth_tok_key->sem)); 1120aee683b9SRoberto Sassu key_put(auth_tok_key); 1121b5695d04SRoberto Sassu } 11223095e8e3SHerbert Xu skcipher_request_free(s->skcipher_req); 11239c79f34fSMichael Halcrow kfree(s); 11249c79f34fSMichael Halcrow return rc; 11259c79f34fSMichael Halcrow } 11269c79f34fSMichael Halcrow 11279c79f34fSMichael Halcrow static int 1128cd9d67dfSMichael Halcrow ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) 1129cd9d67dfSMichael Halcrow { 1130cd9d67dfSMichael Halcrow int rc = 0; 1131cd9d67dfSMichael Halcrow 1132cd9d67dfSMichael Halcrow (*sig) = NULL; 1133cd9d67dfSMichael Halcrow switch (auth_tok->token_type) { 1134cd9d67dfSMichael Halcrow case ECRYPTFS_PASSWORD: 1135cd9d67dfSMichael Halcrow (*sig) = auth_tok->token.password.signature; 1136cd9d67dfSMichael Halcrow break; 1137cd9d67dfSMichael Halcrow case ECRYPTFS_PRIVATE_KEY: 1138cd9d67dfSMichael Halcrow (*sig) = auth_tok->token.private_key.signature; 1139cd9d67dfSMichael Halcrow break; 1140cd9d67dfSMichael Halcrow default: 1141cd9d67dfSMichael Halcrow printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", 1142cd9d67dfSMichael Halcrow auth_tok->token_type); 1143cd9d67dfSMichael Halcrow rc = -EINVAL; 1144cd9d67dfSMichael Halcrow } 1145cd9d67dfSMichael Halcrow return rc; 1146cd9d67dfSMichael Halcrow } 1147cd9d67dfSMichael Halcrow 1148dddfa461SMichael Halcrow /** 114922e78fafSMichael Halcrow * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. 115022e78fafSMichael Halcrow * @auth_tok: The key authentication token used to decrypt the session key 115122e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 1152dddfa461SMichael Halcrow * 115322e78fafSMichael Halcrow * Returns zero on success; non-zero error otherwise. 1154dddfa461SMichael Halcrow */ 1155f4aad16aSMichael Halcrow static int 1156f4aad16aSMichael Halcrow decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1157dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1158dddfa461SMichael Halcrow { 115919e66a67STrevor Highland u8 cipher_code = 0; 1160dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx; 1161dddfa461SMichael Halcrow struct ecryptfs_message *msg = NULL; 1162f4aad16aSMichael Halcrow char *auth_tok_sig; 11633edc8376SGeyslan G. Bem char *payload = NULL; 1164fa519964SSimon Que size_t payload_len = 0; 1165dddfa461SMichael Halcrow int rc; 1166dddfa461SMichael Halcrow 11675dda6992SMichael Halcrow rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); 11685dda6992SMichael Halcrow if (rc) { 1169f4aad16aSMichael Halcrow printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", 1170f4aad16aSMichael Halcrow auth_tok->token_type); 1171f4aad16aSMichael Halcrow goto out; 1172f4aad16aSMichael Halcrow } 1173f4aad16aSMichael Halcrow rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), 1174624ae528STyler Hicks &payload, &payload_len); 1175dddfa461SMichael Halcrow if (rc) { 1176f66e883eSMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); 1177dddfa461SMichael Halcrow goto out; 1178dddfa461SMichael Halcrow } 1179624ae528STyler Hicks rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1180dddfa461SMichael Halcrow if (rc) { 1181624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "Error sending message to " 1182290502beSKees Cook "ecryptfsd: %d\n", rc); 1183dddfa461SMichael Halcrow goto out; 1184dddfa461SMichael Halcrow } 1185dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1186dddfa461SMichael Halcrow if (rc) { 1187dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " 1188dddfa461SMichael Halcrow "from the user space daemon\n"); 1189dddfa461SMichael Halcrow rc = -EIO; 1190dddfa461SMichael Halcrow goto out; 1191dddfa461SMichael Halcrow } 1192dddfa461SMichael Halcrow rc = parse_tag_65_packet(&(auth_tok->session_key), 1193dddfa461SMichael Halcrow &cipher_code, msg); 1194dddfa461SMichael Halcrow if (rc) { 1195dddfa461SMichael Halcrow printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", 1196dddfa461SMichael Halcrow rc); 1197dddfa461SMichael Halcrow goto out; 1198dddfa461SMichael Halcrow } 1199dddfa461SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1200dddfa461SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1201dddfa461SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1202dddfa461SMichael Halcrow crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; 1203dddfa461SMichael Halcrow rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code); 1204dddfa461SMichael Halcrow if (rc) { 1205dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", 1206dddfa461SMichael Halcrow cipher_code) 1207dddfa461SMichael Halcrow goto out; 1208dddfa461SMichael Halcrow } 1209dddfa461SMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1210dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 1211dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); 1212dddfa461SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1213dddfa461SMichael Halcrow crypt_stat->key_size); 1214dddfa461SMichael Halcrow } 1215dddfa461SMichael Halcrow out: 1216dddfa461SMichael Halcrow kfree(msg); 12173edc8376SGeyslan G. Bem kfree(payload); 1218dddfa461SMichael Halcrow return rc; 1219dddfa461SMichael Halcrow } 1220dddfa461SMichael Halcrow 1221dddfa461SMichael Halcrow static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) 1222dddfa461SMichael Halcrow { 1223dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1224e0869cc1SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1225dddfa461SMichael Halcrow 1226e0869cc1SMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, 1227e0869cc1SMichael Halcrow auth_tok_list_head, list) { 1228e0869cc1SMichael Halcrow list_del(&auth_tok_list_item->list); 1229dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1230dddfa461SMichael Halcrow auth_tok_list_item); 1231dddfa461SMichael Halcrow } 1232dddfa461SMichael Halcrow } 1233dddfa461SMichael Halcrow 1234dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 1235dddfa461SMichael Halcrow 1236dddfa461SMichael Halcrow /** 1237dddfa461SMichael Halcrow * parse_tag_1_packet 123822e78fafSMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet contents 1239dddfa461SMichael Halcrow * @data: The raw bytes of the packet. 1240dddfa461SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 124122e78fafSMichael Halcrow * a new authentication token will be placed at the 124222e78fafSMichael Halcrow * end of this list for this packet. 1243dddfa461SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 1244dddfa461SMichael Halcrow * allocates; sets the memory address of the pointer to 1245dddfa461SMichael Halcrow * NULL on error. This object is added to the 1246dddfa461SMichael Halcrow * auth_tok_list. 1247dddfa461SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1248dddfa461SMichael Halcrow * into this memory location; zero on error. 124922e78fafSMichael Halcrow * @max_packet_size: The maximum allowable packet size 1250dddfa461SMichael Halcrow * 1251dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 1252dddfa461SMichael Halcrow */ 1253dddfa461SMichael Halcrow static int 1254dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, 1255dddfa461SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 1256dddfa461SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 1257dddfa461SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1258dddfa461SMichael Halcrow { 1259dddfa461SMichael Halcrow size_t body_size; 1260dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1261dddfa461SMichael Halcrow size_t length_size; 1262dddfa461SMichael Halcrow int rc = 0; 1263dddfa461SMichael Halcrow 1264dddfa461SMichael Halcrow (*packet_size) = 0; 1265dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 126613218179SMichael Halcrow /** 126713218179SMichael Halcrow * This format is inspired by OpenPGP; see RFC 2440 126813218179SMichael Halcrow * packet tag 1 126913218179SMichael Halcrow * 127013218179SMichael Halcrow * Tag 1 identifier (1 byte) 127113218179SMichael Halcrow * Max Tag 1 packet size (max 3 bytes) 127213218179SMichael Halcrow * Version (1 byte) 127313218179SMichael Halcrow * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) 127413218179SMichael Halcrow * Cipher identifier (1 byte) 127513218179SMichael Halcrow * Encrypted key size (arbitrary) 127613218179SMichael Halcrow * 127713218179SMichael Halcrow * 12 bytes minimum packet size 1278dddfa461SMichael Halcrow */ 127913218179SMichael Halcrow if (unlikely(max_packet_size < 12)) { 128013218179SMichael Halcrow printk(KERN_ERR "Invalid max packet size; must be >=12\n"); 1281dddfa461SMichael Halcrow rc = -EINVAL; 1282dddfa461SMichael Halcrow goto out; 1283dddfa461SMichael Halcrow } 1284dddfa461SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { 128513218179SMichael Halcrow printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", 1286dddfa461SMichael Halcrow ECRYPTFS_TAG_1_PACKET_TYPE); 1287dddfa461SMichael Halcrow rc = -EINVAL; 1288dddfa461SMichael Halcrow goto out; 1289dddfa461SMichael Halcrow } 1290dddfa461SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1291dddfa461SMichael Halcrow * at end of function upon failure */ 1292dddfa461SMichael Halcrow auth_tok_list_item = 129313218179SMichael Halcrow kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, 1294dddfa461SMichael Halcrow GFP_KERNEL); 1295dddfa461SMichael Halcrow if (!auth_tok_list_item) { 129613218179SMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 1297dddfa461SMichael Halcrow rc = -ENOMEM; 1298dddfa461SMichael Halcrow goto out; 1299dddfa461SMichael Halcrow } 1300dddfa461SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1301f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 13025dda6992SMichael Halcrow &length_size); 13035dda6992SMichael Halcrow if (rc) { 130413218179SMichael Halcrow printk(KERN_WARNING "Error parsing packet length; " 1305dddfa461SMichael Halcrow "rc = [%d]\n", rc); 1306dddfa461SMichael Halcrow goto out_free; 1307dddfa461SMichael Halcrow } 130813218179SMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { 130981acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1310dddfa461SMichael Halcrow rc = -EINVAL; 1311dddfa461SMichael Halcrow goto out_free; 1312dddfa461SMichael Halcrow } 1313dddfa461SMichael Halcrow (*packet_size) += length_size; 1314dddfa461SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 131513218179SMichael Halcrow printk(KERN_WARNING "Packet size exceeds max\n"); 1316dddfa461SMichael Halcrow rc = -EINVAL; 1317dddfa461SMichael Halcrow goto out_free; 1318dddfa461SMichael Halcrow } 1319dddfa461SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 132013218179SMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 132113218179SMichael Halcrow data[(*packet_size) - 1]); 1322dddfa461SMichael Halcrow rc = -EINVAL; 1323dddfa461SMichael Halcrow goto out_free; 1324dddfa461SMichael Halcrow } 1325dddfa461SMichael Halcrow ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, 1326dddfa461SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SIG_SIZE); 1327dddfa461SMichael Halcrow *packet_size += ECRYPTFS_SIG_SIZE; 1328dddfa461SMichael Halcrow /* This byte is skipped because the kernel does not need to 1329dddfa461SMichael Halcrow * know which public key encryption algorithm was used */ 1330dddfa461SMichael Halcrow (*packet_size)++; 1331dddfa461SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 133213218179SMichael Halcrow body_size - (ECRYPTFS_SIG_SIZE + 2); 1333dddfa461SMichael Halcrow if ((*new_auth_tok)->session_key.encrypted_key_size 1334dddfa461SMichael Halcrow > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 133513218179SMichael Halcrow printk(KERN_WARNING "Tag 1 packet contains key larger " 1336*0996b67dSColin Ian King "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); 1337dddfa461SMichael Halcrow rc = -EINVAL; 1338dddfa461SMichael Halcrow goto out; 1339dddfa461SMichael Halcrow } 1340dddfa461SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 134113218179SMichael Halcrow &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); 1342dddfa461SMichael Halcrow (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; 1343dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags &= 1344dddfa461SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1345dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags |= 1346dddfa461SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1347dddfa461SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; 134813218179SMichael Halcrow (*new_auth_tok)->flags = 0; 1349e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1350e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1351e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1352e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1353dddfa461SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 1354dddfa461SMichael Halcrow goto out; 1355dddfa461SMichael Halcrow out_free: 1356dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 1357dddfa461SMichael Halcrow memset(auth_tok_list_item, 0, 1358dddfa461SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 1359dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1360dddfa461SMichael Halcrow auth_tok_list_item); 1361dddfa461SMichael Halcrow out: 1362dddfa461SMichael Halcrow if (rc) 1363dddfa461SMichael Halcrow (*packet_size) = 0; 1364dddfa461SMichael Halcrow return rc; 1365dddfa461SMichael Halcrow } 1366dddfa461SMichael Halcrow 1367237fead6SMichael Halcrow /** 1368237fead6SMichael Halcrow * parse_tag_3_packet 1369237fead6SMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet 1370237fead6SMichael Halcrow * contents. 1371237fead6SMichael Halcrow * @data: The raw bytes of the packet. 1372237fead6SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 1373237fead6SMichael Halcrow * a new authentication token will be placed at the end 1374237fead6SMichael Halcrow * of this list for this packet. 1375237fead6SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 1376237fead6SMichael Halcrow * allocates; sets the memory address of the pointer to 1377237fead6SMichael Halcrow * NULL on error. This object is added to the 1378237fead6SMichael Halcrow * auth_tok_list. 1379237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1380237fead6SMichael Halcrow * into this memory location; zero on error. 1381237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 1382237fead6SMichael Halcrow * 1383237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1384237fead6SMichael Halcrow */ 1385237fead6SMichael Halcrow static int 1386237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, 1387237fead6SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 1388237fead6SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 1389237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1390237fead6SMichael Halcrow { 1391237fead6SMichael Halcrow size_t body_size; 1392237fead6SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1393237fead6SMichael Halcrow size_t length_size; 1394dddfa461SMichael Halcrow int rc = 0; 1395237fead6SMichael Halcrow 1396237fead6SMichael Halcrow (*packet_size) = 0; 1397237fead6SMichael Halcrow (*new_auth_tok) = NULL; 1398c59becfcSMichael Halcrow /** 1399c59becfcSMichael Halcrow *This format is inspired by OpenPGP; see RFC 2440 1400c59becfcSMichael Halcrow * packet tag 3 1401c59becfcSMichael Halcrow * 1402c59becfcSMichael Halcrow * Tag 3 identifier (1 byte) 1403c59becfcSMichael Halcrow * Max Tag 3 packet size (max 3 bytes) 1404c59becfcSMichael Halcrow * Version (1 byte) 1405c59becfcSMichael Halcrow * Cipher code (1 byte) 1406c59becfcSMichael Halcrow * S2K specifier (1 byte) 1407c59becfcSMichael Halcrow * Hash identifier (1 byte) 1408c59becfcSMichael Halcrow * Salt (ECRYPTFS_SALT_SIZE) 1409c59becfcSMichael Halcrow * Hash iterations (1 byte) 1410c59becfcSMichael Halcrow * Encrypted key (arbitrary) 1411c59becfcSMichael Halcrow * 1412c59becfcSMichael Halcrow * (ECRYPTFS_SALT_SIZE + 7) minimum packet size 1413237fead6SMichael Halcrow */ 1414c59becfcSMichael Halcrow if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { 1415c59becfcSMichael Halcrow printk(KERN_ERR "Max packet size too large\n"); 1416237fead6SMichael Halcrow rc = -EINVAL; 1417237fead6SMichael Halcrow goto out; 1418237fead6SMichael Halcrow } 1419237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { 1420c59becfcSMichael Halcrow printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", 1421237fead6SMichael Halcrow ECRYPTFS_TAG_3_PACKET_TYPE); 1422237fead6SMichael Halcrow rc = -EINVAL; 1423237fead6SMichael Halcrow goto out; 1424237fead6SMichael Halcrow } 1425237fead6SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1426237fead6SMichael Halcrow * at end of function upon failure */ 1427237fead6SMichael Halcrow auth_tok_list_item = 1428c3762229SRobert P. J. Day kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); 1429237fead6SMichael Halcrow if (!auth_tok_list_item) { 1430c59becfcSMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 1431237fead6SMichael Halcrow rc = -ENOMEM; 1432237fead6SMichael Halcrow goto out; 1433237fead6SMichael Halcrow } 1434237fead6SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1435f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 14365dda6992SMichael Halcrow &length_size); 14375dda6992SMichael Halcrow if (rc) { 1438c59becfcSMichael Halcrow printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", 1439c59becfcSMichael Halcrow rc); 1440237fead6SMichael Halcrow goto out_free; 1441237fead6SMichael Halcrow } 1442c59becfcSMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { 144381acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1444237fead6SMichael Halcrow rc = -EINVAL; 1445237fead6SMichael Halcrow goto out_free; 1446237fead6SMichael Halcrow } 1447237fead6SMichael Halcrow (*packet_size) += length_size; 1448237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 1449c59becfcSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 1450237fead6SMichael Halcrow rc = -EINVAL; 1451237fead6SMichael Halcrow goto out_free; 1452237fead6SMichael Halcrow } 1453237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 1454c59becfcSMichael Halcrow (body_size - (ECRYPTFS_SALT_SIZE + 5)); 1455f151cd2cSRamon de Carvalho Valle if ((*new_auth_tok)->session_key.encrypted_key_size 1456f151cd2cSRamon de Carvalho Valle > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 1457f151cd2cSRamon de Carvalho Valle printk(KERN_WARNING "Tag 3 packet contains key larger " 1458f151cd2cSRamon de Carvalho Valle "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); 1459f151cd2cSRamon de Carvalho Valle rc = -EINVAL; 1460f151cd2cSRamon de Carvalho Valle goto out_free; 1461f151cd2cSRamon de Carvalho Valle } 1462237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x04)) { 1463c59becfcSMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 1464c59becfcSMichael Halcrow data[(*packet_size) - 1]); 1465237fead6SMichael Halcrow rc = -EINVAL; 1466237fead6SMichael Halcrow goto out_free; 1467237fead6SMichael Halcrow } 1468b0105eaeSTyler Hicks rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, 1469237fead6SMichael Halcrow (u16)data[(*packet_size)]); 1470b0105eaeSTyler Hicks if (rc) 1471b0105eaeSTyler Hicks goto out_free; 1472237fead6SMichael Halcrow /* A little extra work to differentiate among the AES key 1473237fead6SMichael Halcrow * sizes; see RFC2440 */ 1474237fead6SMichael Halcrow switch(data[(*packet_size)++]) { 1475237fead6SMichael Halcrow case RFC2440_CIPHER_AES_192: 1476237fead6SMichael Halcrow crypt_stat->key_size = 24; 1477237fead6SMichael Halcrow break; 1478237fead6SMichael Halcrow default: 1479237fead6SMichael Halcrow crypt_stat->key_size = 1480237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 1481237fead6SMichael Halcrow } 1482b0105eaeSTyler Hicks rc = ecryptfs_init_crypt_ctx(crypt_stat); 1483b0105eaeSTyler Hicks if (rc) 1484b0105eaeSTyler Hicks goto out_free; 1485237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 1486c59becfcSMichael Halcrow printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); 1487237fead6SMichael Halcrow rc = -ENOSYS; 1488237fead6SMichael Halcrow goto out_free; 1489237fead6SMichael Halcrow } 1490237fead6SMichael Halcrow /* TODO: finish the hash mapping */ 1491237fead6SMichael Halcrow switch (data[(*packet_size)++]) { 1492237fead6SMichael Halcrow case 0x01: /* See RFC2440 for these numbers and their mappings */ 1493237fead6SMichael Halcrow /* Choose MD5 */ 1494237fead6SMichael Halcrow memcpy((*new_auth_tok)->token.password.salt, 1495237fead6SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SALT_SIZE); 1496237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; 1497237fead6SMichael Halcrow /* This conversion was taken straight from RFC2440 */ 1498237fead6SMichael Halcrow (*new_auth_tok)->token.password.hash_iterations = 1499237fead6SMichael Halcrow ((u32) 16 + (data[(*packet_size)] & 15)) 1500237fead6SMichael Halcrow << ((data[(*packet_size)] >> 4) + 6); 1501237fead6SMichael Halcrow (*packet_size)++; 1502c59becfcSMichael Halcrow /* Friendly reminder: 1503c59becfcSMichael Halcrow * (*new_auth_tok)->session_key.encrypted_key_size = 1504c59becfcSMichael Halcrow * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ 1505237fead6SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 1506237fead6SMichael Halcrow &data[(*packet_size)], 1507237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size); 1508237fead6SMichael Halcrow (*packet_size) += 1509237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 1510237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags &= 1511237fead6SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1512237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags |= 1513237fead6SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1514c59becfcSMichael Halcrow (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ 1515237fead6SMichael Halcrow break; 1516237fead6SMichael Halcrow default: 1517237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " 1518237fead6SMichael Halcrow "[%d]\n", data[(*packet_size) - 1]); 1519237fead6SMichael Halcrow rc = -ENOSYS; 1520237fead6SMichael Halcrow goto out_free; 1521237fead6SMichael Halcrow } 1522237fead6SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; 1523237fead6SMichael Halcrow /* TODO: Parametarize; we might actually want userspace to 1524237fead6SMichael Halcrow * decrypt the session key. */ 1525e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1526e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1527e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1528e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1529237fead6SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 1530237fead6SMichael Halcrow goto out; 1531237fead6SMichael Halcrow out_free: 1532237fead6SMichael Halcrow (*new_auth_tok) = NULL; 1533237fead6SMichael Halcrow memset(auth_tok_list_item, 0, 1534237fead6SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 1535237fead6SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1536237fead6SMichael Halcrow auth_tok_list_item); 1537237fead6SMichael Halcrow out: 1538237fead6SMichael Halcrow if (rc) 1539237fead6SMichael Halcrow (*packet_size) = 0; 1540237fead6SMichael Halcrow return rc; 1541237fead6SMichael Halcrow } 1542237fead6SMichael Halcrow 1543237fead6SMichael Halcrow /** 1544237fead6SMichael Halcrow * parse_tag_11_packet 1545237fead6SMichael Halcrow * @data: The raw bytes of the packet 1546237fead6SMichael Halcrow * @contents: This function writes the data contents of the literal 1547237fead6SMichael Halcrow * packet into this memory location 1548237fead6SMichael Halcrow * @max_contents_bytes: The maximum number of bytes that this function 1549237fead6SMichael Halcrow * is allowed to write into contents 1550237fead6SMichael Halcrow * @tag_11_contents_size: This function writes the size of the parsed 1551237fead6SMichael Halcrow * contents into this memory location; zero on 1552237fead6SMichael Halcrow * error 1553237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1554237fead6SMichael Halcrow * into this memory location; zero on error 1555237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 1556237fead6SMichael Halcrow * 1557237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1558237fead6SMichael Halcrow */ 1559237fead6SMichael Halcrow static int 1560237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents, 1561237fead6SMichael Halcrow size_t max_contents_bytes, size_t *tag_11_contents_size, 1562237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1563237fead6SMichael Halcrow { 1564237fead6SMichael Halcrow size_t body_size; 1565237fead6SMichael Halcrow size_t length_size; 1566dddfa461SMichael Halcrow int rc = 0; 1567237fead6SMichael Halcrow 1568237fead6SMichael Halcrow (*packet_size) = 0; 1569237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 1570f648104aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1571f648104aSMichael Halcrow * packet tag 11 1572f648104aSMichael Halcrow * 1573f648104aSMichael Halcrow * Tag 11 identifier (1 byte) 1574f648104aSMichael Halcrow * Max Tag 11 packet size (max 3 bytes) 1575f648104aSMichael Halcrow * Binary format specifier (1 byte) 1576f648104aSMichael Halcrow * Filename length (1 byte) 1577f648104aSMichael Halcrow * Filename ("_CONSOLE") (8 bytes) 1578f648104aSMichael Halcrow * Modification date (4 bytes) 1579f648104aSMichael Halcrow * Literal data (arbitrary) 1580f648104aSMichael Halcrow * 1581f648104aSMichael Halcrow * We need at least 16 bytes of data for the packet to even be 1582f648104aSMichael Halcrow * valid. 1583237fead6SMichael Halcrow */ 1584f648104aSMichael Halcrow if (max_packet_size < 16) { 1585f648104aSMichael Halcrow printk(KERN_ERR "Maximum packet size too small\n"); 1586237fead6SMichael Halcrow rc = -EINVAL; 1587237fead6SMichael Halcrow goto out; 1588237fead6SMichael Halcrow } 1589237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { 1590f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1591237fead6SMichael Halcrow rc = -EINVAL; 1592237fead6SMichael Halcrow goto out; 1593237fead6SMichael Halcrow } 1594f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 15955dda6992SMichael Halcrow &length_size); 15965dda6992SMichael Halcrow if (rc) { 1597f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1598f648104aSMichael Halcrow goto out; 1599f648104aSMichael Halcrow } 1600f648104aSMichael Halcrow if (body_size < 14) { 160181acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1602f648104aSMichael Halcrow rc = -EINVAL; 1603237fead6SMichael Halcrow goto out; 1604237fead6SMichael Halcrow } 1605237fead6SMichael Halcrow (*packet_size) += length_size; 1606f648104aSMichael Halcrow (*tag_11_contents_size) = (body_size - 14); 1607237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { 1608f648104aSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 1609237fead6SMichael Halcrow rc = -EINVAL; 1610237fead6SMichael Halcrow goto out; 1611237fead6SMichael Halcrow } 16126352a293STyler Hicks if (unlikely((*tag_11_contents_size) > max_contents_bytes)) { 16136352a293STyler Hicks printk(KERN_ERR "Literal data section in tag 11 packet exceeds " 16146352a293STyler Hicks "expected size\n"); 16156352a293STyler Hicks rc = -EINVAL; 16166352a293STyler Hicks goto out; 16176352a293STyler Hicks } 1618237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x62) { 1619f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 1620237fead6SMichael Halcrow rc = -EINVAL; 1621237fead6SMichael Halcrow goto out; 1622237fead6SMichael Halcrow } 1623237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x08) { 1624f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 1625237fead6SMichael Halcrow rc = -EINVAL; 1626237fead6SMichael Halcrow goto out; 1627237fead6SMichael Halcrow } 1628f648104aSMichael Halcrow (*packet_size) += 12; /* Ignore filename and modification date */ 1629237fead6SMichael Halcrow memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); 1630237fead6SMichael Halcrow (*packet_size) += (*tag_11_contents_size); 1631237fead6SMichael Halcrow out: 1632237fead6SMichael Halcrow if (rc) { 1633237fead6SMichael Halcrow (*packet_size) = 0; 1634237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 1635237fead6SMichael Halcrow } 1636237fead6SMichael Halcrow return rc; 1637237fead6SMichael Halcrow } 1638237fead6SMichael Halcrow 1639f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 1640f4aad16aSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 1641f4aad16aSMichael Halcrow char *sig) 1642f4aad16aSMichael Halcrow { 1643f4aad16aSMichael Halcrow int rc = 0; 1644f4aad16aSMichael Halcrow 1645f4aad16aSMichael Halcrow (*auth_tok_key) = request_key(&key_type_user, sig, NULL); 1646f4aad16aSMichael Halcrow if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 16471252cc3bSRoberto Sassu (*auth_tok_key) = ecryptfs_get_encrypted_key(sig); 16481252cc3bSRoberto Sassu if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 1649f4aad16aSMichael Halcrow printk(KERN_ERR "Could not find key with description: [%s]\n", 1650f4aad16aSMichael Halcrow sig); 1651982363c9SEric Sandeen rc = process_request_key_err(PTR_ERR(*auth_tok_key)); 16521821df04SRoberto Sassu (*auth_tok_key) = NULL; 1653f4aad16aSMichael Halcrow goto out; 1654f4aad16aSMichael Halcrow } 16551252cc3bSRoberto Sassu } 1656b5695d04SRoberto Sassu down_write(&(*auth_tok_key)->sem); 16570e1fc5efSRoberto Sassu rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok); 1658aee683b9SRoberto Sassu if (rc) { 1659b5695d04SRoberto Sassu up_write(&(*auth_tok_key)->sem); 1660aee683b9SRoberto Sassu key_put(*auth_tok_key); 1661aee683b9SRoberto Sassu (*auth_tok_key) = NULL; 16620e1fc5efSRoberto Sassu goto out; 1663f4aad16aSMichael Halcrow } 1664f4aad16aSMichael Halcrow out: 1665f4aad16aSMichael Halcrow return rc; 1666f4aad16aSMichael Halcrow } 1667f4aad16aSMichael Halcrow 1668f4aad16aSMichael Halcrow /** 166922e78fafSMichael Halcrow * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. 167022e78fafSMichael Halcrow * @auth_tok: The passphrase authentication token to use to encrypt the FEK 167122e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 1672237fead6SMichael Halcrow * 167322e78fafSMichael Halcrow * Returns zero on success; non-zero error otherwise 1674237fead6SMichael Halcrow */ 1675f4aad16aSMichael Halcrow static int 1676f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1677237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1678237fead6SMichael Halcrow { 1679ac97b9f9SMichael Halcrow struct scatterlist dst_sg[2]; 1680ac97b9f9SMichael Halcrow struct scatterlist src_sg[2]; 1681dd8e2902SMichael Halcrow struct mutex *tfm_mutex; 16823095e8e3SHerbert Xu struct crypto_skcipher *tfm; 16833095e8e3SHerbert Xu struct skcipher_request *req = NULL; 16848bba066fSMichael Halcrow int rc = 0; 1685237fead6SMichael Halcrow 1686f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1687f4aad16aSMichael Halcrow ecryptfs_printk( 1688f4aad16aSMichael Halcrow KERN_DEBUG, "Session key encryption key (size [%d]):\n", 1689f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1690f4aad16aSMichael Halcrow ecryptfs_dump_hex( 1691f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key, 1692f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1693f4aad16aSMichael Halcrow } 16943095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 1695f4aad16aSMichael Halcrow crypt_stat->cipher); 1696f4aad16aSMichael Halcrow if (unlikely(rc)) { 1697f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 1698f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1699f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 1700237fead6SMichael Halcrow goto out; 1701237fead6SMichael Halcrow } 17025dda6992SMichael Halcrow rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, 1703f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size, 1704ac97b9f9SMichael Halcrow src_sg, 2); 1705ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 1706f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1707f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key to scatterlist; " 1708f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 1709f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, 1710f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size); 1711f4aad16aSMichael Halcrow goto out; 1712237fead6SMichael Halcrow } 1713f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size = 1714f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size; 17155dda6992SMichael Halcrow rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, 1716f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size, 1717ac97b9f9SMichael Halcrow dst_sg, 2); 1718ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 1719f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1720f4aad16aSMichael Halcrow "auth_tok->session_key.decrypted_key to scatterlist; " 1721f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]\n", rc); 1722f4aad16aSMichael Halcrow goto out; 1723f4aad16aSMichael Halcrow } 1724237fead6SMichael Halcrow mutex_lock(tfm_mutex); 17253095e8e3SHerbert Xu req = skcipher_request_alloc(tfm, GFP_KERNEL); 17263095e8e3SHerbert Xu if (!req) { 17273095e8e3SHerbert Xu mutex_unlock(tfm_mutex); 17283095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 17293095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 17303095e8e3SHerbert Xu crypto_skcipher_driver_name(tfm)); 17313095e8e3SHerbert Xu rc = -ENOMEM; 17323095e8e3SHerbert Xu goto out; 17333095e8e3SHerbert Xu } 17343095e8e3SHerbert Xu 17353095e8e3SHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 17363095e8e3SHerbert Xu NULL, NULL); 17373095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 17383095e8e3SHerbert Xu tfm, auth_tok->token.password.session_key_encryption_key, 1739237fead6SMichael Halcrow crypt_stat->key_size); 1740f4aad16aSMichael Halcrow if (unlikely(rc < 0)) { 1741f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1742e5d9cbdeSMichael Halcrow printk(KERN_ERR "Error setting key for crypto context\n"); 1743e5d9cbdeSMichael Halcrow rc = -EINVAL; 1744f4aad16aSMichael Halcrow goto out; 1745e5d9cbdeSMichael Halcrow } 17463095e8e3SHerbert Xu skcipher_request_set_crypt(req, src_sg, dst_sg, 17473095e8e3SHerbert Xu auth_tok->session_key.encrypted_key_size, 17483095e8e3SHerbert Xu NULL); 17493095e8e3SHerbert Xu rc = crypto_skcipher_decrypt(req); 1750f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1751f4aad16aSMichael Halcrow if (unlikely(rc)) { 17528bba066fSMichael Halcrow printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); 1753f4aad16aSMichael Halcrow goto out; 17548bba066fSMichael Halcrow } 1755237fead6SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1756237fead6SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1757237fead6SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1758e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1759f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1760f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n", 1761f4aad16aSMichael Halcrow crypt_stat->key_size); 1762237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1763237fead6SMichael Halcrow crypt_stat->key_size); 1764f4aad16aSMichael Halcrow } 1765237fead6SMichael Halcrow out: 17663095e8e3SHerbert Xu skcipher_request_free(req); 1767237fead6SMichael Halcrow return rc; 1768237fead6SMichael Halcrow } 1769237fead6SMichael Halcrow 1770237fead6SMichael Halcrow /** 1771237fead6SMichael Halcrow * ecryptfs_parse_packet_set 177222e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 177322e78fafSMichael Halcrow * @src: Virtual address of region of memory containing the packets 177422e78fafSMichael Halcrow * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set 1775237fead6SMichael Halcrow * 1776237fead6SMichael Halcrow * Get crypt_stat to have the file's session key if the requisite key 1777237fead6SMichael Halcrow * is available to decrypt the session key. 1778237fead6SMichael Halcrow * 1779237fead6SMichael Halcrow * Returns Zero if a valid authentication token was retrieved and 1780237fead6SMichael Halcrow * processed; negative value for file not encrypted or for error 1781237fead6SMichael Halcrow * conditions. 1782237fead6SMichael Halcrow */ 1783237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 1784237fead6SMichael Halcrow unsigned char *src, 1785237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1786237fead6SMichael Halcrow { 1787237fead6SMichael Halcrow size_t i = 0; 1788f4aad16aSMichael Halcrow size_t found_auth_tok; 1789237fead6SMichael Halcrow size_t next_packet_is_auth_tok_packet; 1790237fead6SMichael Halcrow struct list_head auth_tok_list; 1791dd8e2902SMichael Halcrow struct ecryptfs_auth_tok *matching_auth_tok; 1792dd8e2902SMichael Halcrow struct ecryptfs_auth_tok *candidate_auth_tok; 1793f4aad16aSMichael Halcrow char *candidate_auth_tok_sig; 1794237fead6SMichael Halcrow size_t packet_size; 1795237fead6SMichael Halcrow struct ecryptfs_auth_tok *new_auth_tok; 1796237fead6SMichael Halcrow unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; 1797f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1798237fead6SMichael Halcrow size_t tag_11_contents_size; 1799237fead6SMichael Halcrow size_t tag_11_packet_size; 1800aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 1801dddfa461SMichael Halcrow int rc = 0; 1802237fead6SMichael Halcrow 1803237fead6SMichael Halcrow INIT_LIST_HEAD(&auth_tok_list); 1804f4aad16aSMichael Halcrow /* Parse the header to find as many packets as we can; these will be 1805237fead6SMichael Halcrow * added the our &auth_tok_list */ 1806237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 1; 1807237fead6SMichael Halcrow while (next_packet_is_auth_tok_packet) { 180809cbfeafSKirill A. Shutemov size_t max_packet_size = ((PAGE_SIZE - 8) - i); 1809237fead6SMichael Halcrow 1810237fead6SMichael Halcrow switch (src[i]) { 1811237fead6SMichael Halcrow case ECRYPTFS_TAG_3_PACKET_TYPE: 1812237fead6SMichael Halcrow rc = parse_tag_3_packet(crypt_stat, 1813237fead6SMichael Halcrow (unsigned char *)&src[i], 1814237fead6SMichael Halcrow &auth_tok_list, &new_auth_tok, 1815237fead6SMichael Halcrow &packet_size, max_packet_size); 1816237fead6SMichael Halcrow if (rc) { 1817237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1818237fead6SMichael Halcrow "tag 3 packet\n"); 1819237fead6SMichael Halcrow rc = -EIO; 1820237fead6SMichael Halcrow goto out_wipe_list; 1821237fead6SMichael Halcrow } 1822237fead6SMichael Halcrow i += packet_size; 1823237fead6SMichael Halcrow rc = parse_tag_11_packet((unsigned char *)&src[i], 1824237fead6SMichael Halcrow sig_tmp_space, 1825237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1826237fead6SMichael Halcrow &tag_11_contents_size, 1827237fead6SMichael Halcrow &tag_11_packet_size, 1828237fead6SMichael Halcrow max_packet_size); 1829237fead6SMichael Halcrow if (rc) { 1830237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "No valid " 1831237fead6SMichael Halcrow "(ecryptfs-specific) literal " 1832237fead6SMichael Halcrow "packet containing " 1833237fead6SMichael Halcrow "authentication token " 1834237fead6SMichael Halcrow "signature found after " 1835237fead6SMichael Halcrow "tag 3 packet\n"); 1836237fead6SMichael Halcrow rc = -EIO; 1837237fead6SMichael Halcrow goto out_wipe_list; 1838237fead6SMichael Halcrow } 1839237fead6SMichael Halcrow i += tag_11_packet_size; 1840237fead6SMichael Halcrow if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { 1841237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Expected " 1842237fead6SMichael Halcrow "signature of size [%d]; " 1843f24b3887STyler Hicks "read size [%zd]\n", 1844237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1845237fead6SMichael Halcrow tag_11_contents_size); 1846237fead6SMichael Halcrow rc = -EIO; 1847237fead6SMichael Halcrow goto out_wipe_list; 1848237fead6SMichael Halcrow } 1849237fead6SMichael Halcrow ecryptfs_to_hex(new_auth_tok->token.password.signature, 1850237fead6SMichael Halcrow sig_tmp_space, tag_11_contents_size); 1851237fead6SMichael Halcrow new_auth_tok->token.password.signature[ 1852237fead6SMichael Halcrow ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; 1853e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1854237fead6SMichael Halcrow break; 1855dddfa461SMichael Halcrow case ECRYPTFS_TAG_1_PACKET_TYPE: 1856dddfa461SMichael Halcrow rc = parse_tag_1_packet(crypt_stat, 1857dddfa461SMichael Halcrow (unsigned char *)&src[i], 1858dddfa461SMichael Halcrow &auth_tok_list, &new_auth_tok, 1859dddfa461SMichael Halcrow &packet_size, max_packet_size); 1860dddfa461SMichael Halcrow if (rc) { 1861dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1862dddfa461SMichael Halcrow "tag 1 packet\n"); 1863dddfa461SMichael Halcrow rc = -EIO; 1864dddfa461SMichael Halcrow goto out_wipe_list; 1865dddfa461SMichael Halcrow } 1866dddfa461SMichael Halcrow i += packet_size; 1867e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1868dddfa461SMichael Halcrow break; 1869237fead6SMichael Halcrow case ECRYPTFS_TAG_11_PACKET_TYPE: 1870237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid packet set " 1871237fead6SMichael Halcrow "(Tag 11 not allowed by itself)\n"); 1872237fead6SMichael Halcrow rc = -EIO; 1873237fead6SMichael Halcrow goto out_wipe_list; 1874237fead6SMichael Halcrow default: 1875f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] " 1876f24b3887STyler Hicks "of the file header; hex value of " 1877237fead6SMichael Halcrow "character is [0x%.2x]\n", i, src[i]); 1878237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 0; 1879237fead6SMichael Halcrow } 1880237fead6SMichael Halcrow } 1881237fead6SMichael Halcrow if (list_empty(&auth_tok_list)) { 1882f4aad16aSMichael Halcrow printk(KERN_ERR "The lower file appears to be a non-encrypted " 1883f4aad16aSMichael Halcrow "eCryptfs file; this is not supported in this version " 1884f4aad16aSMichael Halcrow "of the eCryptfs kernel module\n"); 1885f4aad16aSMichael Halcrow rc = -EINVAL; 1886237fead6SMichael Halcrow goto out; 1887237fead6SMichael Halcrow } 1888f4aad16aSMichael Halcrow /* auth_tok_list contains the set of authentication tokens 1889f4aad16aSMichael Halcrow * parsed from the metadata. We need to find a matching 1890f4aad16aSMichael Halcrow * authentication token that has the secret component(s) 1891f4aad16aSMichael Halcrow * necessary to decrypt the EFEK in the auth_tok parsed from 1892f4aad16aSMichael Halcrow * the metadata. There may be several potential matches, but 1893f4aad16aSMichael Halcrow * just one will be sufficient to decrypt to get the FEK. */ 1894f4aad16aSMichael Halcrow find_next_matching_auth_tok: 1895f4aad16aSMichael Halcrow found_auth_tok = 0; 1896f4aad16aSMichael Halcrow list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { 1897237fead6SMichael Halcrow candidate_auth_tok = &auth_tok_list_item->auth_tok; 1898237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1899237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1900237fead6SMichael Halcrow "Considering cadidate auth tok:\n"); 1901237fead6SMichael Halcrow ecryptfs_dump_auth_tok(candidate_auth_tok); 1902237fead6SMichael Halcrow } 19035dda6992SMichael Halcrow rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, 19045dda6992SMichael Halcrow candidate_auth_tok); 19055dda6992SMichael Halcrow if (rc) { 1906f4aad16aSMichael Halcrow printk(KERN_ERR 1907f4aad16aSMichael Halcrow "Unrecognized candidate auth tok type: [%d]\n", 1908f4aad16aSMichael Halcrow candidate_auth_tok->token_type); 1909f4aad16aSMichael Halcrow rc = -EINVAL; 1910f4aad16aSMichael Halcrow goto out_wipe_list; 1911f4aad16aSMichael Halcrow } 191239fac853SRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 1913aee683b9SRoberto Sassu &matching_auth_tok, 19149c79f34fSMichael Halcrow crypt_stat->mount_crypt_stat, 19155dda6992SMichael Halcrow candidate_auth_tok_sig); 191639fac853SRoberto Sassu if (!rc) { 1917237fead6SMichael Halcrow found_auth_tok = 1; 1918f4aad16aSMichael Halcrow goto found_matching_auth_tok; 1919237fead6SMichael Halcrow } 1920237fead6SMichael Halcrow } 1921237fead6SMichael Halcrow if (!found_auth_tok) { 1922f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Could not find a usable " 1923f4aad16aSMichael Halcrow "authentication token\n"); 1924237fead6SMichael Halcrow rc = -EIO; 1925237fead6SMichael Halcrow goto out_wipe_list; 1926dddfa461SMichael Halcrow } 1927f4aad16aSMichael Halcrow found_matching_auth_tok: 1928e2bd99ecSMichael Halcrow if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1929dddfa461SMichael Halcrow memcpy(&(candidate_auth_tok->token.private_key), 1930f4aad16aSMichael Halcrow &(matching_auth_tok->token.private_key), 1931dddfa461SMichael Halcrow sizeof(struct ecryptfs_private_key)); 1932b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1933b2987a5eSTyler Hicks key_put(auth_tok_key); 1934f4aad16aSMichael Halcrow rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, 1935dddfa461SMichael Halcrow crypt_stat); 1936dddfa461SMichael Halcrow } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { 1937237fead6SMichael Halcrow memcpy(&(candidate_auth_tok->token.password), 1938f4aad16aSMichael Halcrow &(matching_auth_tok->token.password), 1939237fead6SMichael Halcrow sizeof(struct ecryptfs_password)); 1940b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1941b2987a5eSTyler Hicks key_put(auth_tok_key); 1942f4aad16aSMichael Halcrow rc = decrypt_passphrase_encrypted_session_key( 1943f4aad16aSMichael Halcrow candidate_auth_tok, crypt_stat); 1944b2987a5eSTyler Hicks } else { 1945b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1946b2987a5eSTyler Hicks key_put(auth_tok_key); 1947b2987a5eSTyler Hicks rc = -EINVAL; 1948dddfa461SMichael Halcrow } 1949237fead6SMichael Halcrow if (rc) { 1950f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1951f4aad16aSMichael Halcrow 1952f4aad16aSMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error decrypting the " 1953f4aad16aSMichael Halcrow "session key for authentication token with sig " 1954f4aad16aSMichael Halcrow "[%.*s]; rc = [%d]. Removing auth tok " 1955f4aad16aSMichael Halcrow "candidate from the list and searching for " 1956888d57bbSJoe Perches "the next match.\n", ECRYPTFS_SIG_SIZE_HEX, 1957888d57bbSJoe Perches candidate_auth_tok_sig, rc); 1958f4aad16aSMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, 1959f4aad16aSMichael Halcrow auth_tok_list_item_tmp, 1960f4aad16aSMichael Halcrow &auth_tok_list, list) { 1961f4aad16aSMichael Halcrow if (candidate_auth_tok 1962f4aad16aSMichael Halcrow == &auth_tok_list_item->auth_tok) { 1963f4aad16aSMichael Halcrow list_del(&auth_tok_list_item->list); 1964f4aad16aSMichael Halcrow kmem_cache_free( 1965f4aad16aSMichael Halcrow ecryptfs_auth_tok_list_item_cache, 1966f4aad16aSMichael Halcrow auth_tok_list_item); 1967f4aad16aSMichael Halcrow goto find_next_matching_auth_tok; 1968f4aad16aSMichael Halcrow } 1969f4aad16aSMichael Halcrow } 1970f4aad16aSMichael Halcrow BUG(); 1971237fead6SMichael Halcrow } 1972237fead6SMichael Halcrow rc = ecryptfs_compute_root_iv(crypt_stat); 1973237fead6SMichael Halcrow if (rc) { 1974237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error computing " 1975237fead6SMichael Halcrow "the root IV\n"); 1976237fead6SMichael Halcrow goto out_wipe_list; 1977237fead6SMichael Halcrow } 1978237fead6SMichael Halcrow rc = ecryptfs_init_crypt_ctx(crypt_stat); 1979237fead6SMichael Halcrow if (rc) { 1980237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error initializing crypto " 1981237fead6SMichael Halcrow "context for cipher [%s]; rc = [%d]\n", 1982237fead6SMichael Halcrow crypt_stat->cipher, rc); 1983237fead6SMichael Halcrow } 1984237fead6SMichael Halcrow out_wipe_list: 1985237fead6SMichael Halcrow wipe_auth_tok_list(&auth_tok_list); 1986237fead6SMichael Halcrow out: 1987237fead6SMichael Halcrow return rc; 1988237fead6SMichael Halcrow } 1989f4aad16aSMichael Halcrow 1990dddfa461SMichael Halcrow static int 1991b2987a5eSTyler Hicks pki_encrypt_session_key(struct key *auth_tok_key, 1992b2987a5eSTyler Hicks struct ecryptfs_auth_tok *auth_tok, 1993dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1994dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec) 1995dddfa461SMichael Halcrow { 1996dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx = NULL; 1997624ae528STyler Hicks char *payload = NULL; 199899b373ffSTyler Hicks size_t payload_len = 0; 1999dddfa461SMichael Halcrow struct ecryptfs_message *msg; 2000dddfa461SMichael Halcrow int rc; 2001dddfa461SMichael Halcrow 2002dddfa461SMichael Halcrow rc = write_tag_66_packet(auth_tok->token.private_key.signature, 20039c79f34fSMichael Halcrow ecryptfs_code_for_cipher_string( 20049c79f34fSMichael Halcrow crypt_stat->cipher, 20059c79f34fSMichael Halcrow crypt_stat->key_size), 2006624ae528STyler Hicks crypt_stat, &payload, &payload_len); 2007b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2008b2987a5eSTyler Hicks key_put(auth_tok_key); 2009dddfa461SMichael Halcrow if (rc) { 2010dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); 2011dddfa461SMichael Halcrow goto out; 2012dddfa461SMichael Halcrow } 2013624ae528STyler Hicks rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 2014dddfa461SMichael Halcrow if (rc) { 2015624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "Error sending message to " 2016290502beSKees Cook "ecryptfsd: %d\n", rc); 2017dddfa461SMichael Halcrow goto out; 2018dddfa461SMichael Halcrow } 2019dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 2020dddfa461SMichael Halcrow if (rc) { 2021dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " 2022dddfa461SMichael Halcrow "from the user space daemon\n"); 2023dddfa461SMichael Halcrow rc = -EIO; 2024dddfa461SMichael Halcrow goto out; 2025dddfa461SMichael Halcrow } 2026dddfa461SMichael Halcrow rc = parse_tag_67_packet(key_rec, msg); 2027dddfa461SMichael Halcrow if (rc) 2028dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); 2029dddfa461SMichael Halcrow kfree(msg); 2030dddfa461SMichael Halcrow out: 2031624ae528STyler Hicks kfree(payload); 2032dddfa461SMichael Halcrow return rc; 2033dddfa461SMichael Halcrow } 2034dddfa461SMichael Halcrow /** 2035dddfa461SMichael Halcrow * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet 2036dddfa461SMichael Halcrow * @dest: Buffer into which to write the packet 203722e78fafSMichael Halcrow * @remaining_bytes: Maximum number of bytes that can be writtn 2038b2987a5eSTyler Hicks * @auth_tok_key: The authentication token key to unlock and put when done with 2039b2987a5eSTyler Hicks * @auth_tok 204022e78fafSMichael Halcrow * @auth_tok: The authentication token used for generating the tag 1 packet 204122e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 204222e78fafSMichael Halcrow * @key_rec: The key record struct for the tag 1 packet 2043dddfa461SMichael Halcrow * @packet_size: This function will write the number of bytes that end 2044dddfa461SMichael Halcrow * up constituting the packet; set to zero on error 2045dddfa461SMichael Halcrow * 2046dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 2047dddfa461SMichael Halcrow */ 2048dddfa461SMichael Halcrow static int 2049f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes, 2050b2987a5eSTyler Hicks struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok, 2051dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2052dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 2053dddfa461SMichael Halcrow { 2054dddfa461SMichael Halcrow size_t i; 2055dddfa461SMichael Halcrow size_t encrypted_session_key_valid = 0; 2056dddfa461SMichael Halcrow size_t packet_size_length; 2057f4aad16aSMichael Halcrow size_t max_packet_size; 2058dddfa461SMichael Halcrow int rc = 0; 2059dddfa461SMichael Halcrow 2060dddfa461SMichael Halcrow (*packet_size) = 0; 2061dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, 2062dddfa461SMichael Halcrow ECRYPTFS_SIG_SIZE); 2063dddfa461SMichael Halcrow encrypted_session_key_valid = 0; 2064dddfa461SMichael Halcrow for (i = 0; i < crypt_stat->key_size; i++) 2065dddfa461SMichael Halcrow encrypted_session_key_valid |= 2066dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key[i]; 2067dddfa461SMichael Halcrow if (encrypted_session_key_valid) { 2068dddfa461SMichael Halcrow memcpy(key_rec->enc_key, 2069dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key, 2070dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size); 2071b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2072b2987a5eSTyler Hicks key_put(auth_tok_key); 2073dddfa461SMichael Halcrow goto encrypted_session_key_set; 2074dddfa461SMichael Halcrow } 2075dddfa461SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 2076dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size = 2077dddfa461SMichael Halcrow auth_tok->token.private_key.key_size; 2078b2987a5eSTyler Hicks rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat, 2079b2987a5eSTyler Hicks key_rec); 2080dddfa461SMichael Halcrow if (rc) { 2081f66e883eSMichael Halcrow printk(KERN_ERR "Failed to encrypt session key via a key " 2082f66e883eSMichael Halcrow "module; rc = [%d]\n", rc); 2083dddfa461SMichael Halcrow goto out; 2084dddfa461SMichael Halcrow } 2085dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 2086dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); 2087dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); 2088dddfa461SMichael Halcrow } 2089dddfa461SMichael Halcrow encrypted_session_key_set: 2090f4aad16aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2091f4aad16aSMichael Halcrow * packet tag 1 */ 2092f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 1 identifier */ 2093f4aad16aSMichael Halcrow + 3 /* Max Tag 1 packet size */ 2094f4aad16aSMichael Halcrow + 1 /* Version */ 2095f4aad16aSMichael Halcrow + ECRYPTFS_SIG_SIZE /* Key identifier */ 2096f4aad16aSMichael Halcrow + 1 /* Cipher identifier */ 2097f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 2098f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 2099f4aad16aSMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 210081acbcd6SAndrew Morton "need up to [%td] bytes, but there are only [%td] " 2101f4aad16aSMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 2102dddfa461SMichael Halcrow rc = -EINVAL; 2103dddfa461SMichael Halcrow goto out; 2104dddfa461SMichael Halcrow } 2105dddfa461SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; 2106f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2107f66e883eSMichael Halcrow (max_packet_size - 4), 2108dddfa461SMichael Halcrow &packet_size_length); 2109dddfa461SMichael Halcrow if (rc) { 2110dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " 2111dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 2112dddfa461SMichael Halcrow goto out; 2113dddfa461SMichael Halcrow } 2114dddfa461SMichael Halcrow (*packet_size) += packet_size_length; 2115dddfa461SMichael Halcrow dest[(*packet_size)++] = 0x03; /* version 3 */ 2116dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); 2117dddfa461SMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 2118dddfa461SMichael Halcrow dest[(*packet_size)++] = RFC2440_CIPHER_RSA; 2119dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 2120dddfa461SMichael Halcrow key_rec->enc_key_size); 2121dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 2122dddfa461SMichael Halcrow out: 2123dddfa461SMichael Halcrow if (rc) 2124dddfa461SMichael Halcrow (*packet_size) = 0; 2125f4aad16aSMichael Halcrow else 2126f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 2127dddfa461SMichael Halcrow return rc; 2128dddfa461SMichael Halcrow } 2129237fead6SMichael Halcrow 2130237fead6SMichael Halcrow /** 2131237fead6SMichael Halcrow * write_tag_11_packet 2132237fead6SMichael Halcrow * @dest: Target into which Tag 11 packet is to be written 213322e78fafSMichael Halcrow * @remaining_bytes: Maximum packet length 2134237fead6SMichael Halcrow * @contents: Byte array of contents to copy in 2135237fead6SMichael Halcrow * @contents_length: Number of bytes in contents 2136237fead6SMichael Halcrow * @packet_length: Length of the Tag 11 packet written; zero on error 2137237fead6SMichael Halcrow * 2138237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2139237fead6SMichael Halcrow */ 2140237fead6SMichael Halcrow static int 214181acbcd6SAndrew Morton write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, 2142146a4606SMichael Halcrow size_t contents_length, size_t *packet_length) 2143237fead6SMichael Halcrow { 2144237fead6SMichael Halcrow size_t packet_size_length; 2145146a4606SMichael Halcrow size_t max_packet_size; 2146dddfa461SMichael Halcrow int rc = 0; 2147237fead6SMichael Halcrow 2148237fead6SMichael Halcrow (*packet_length) = 0; 2149146a4606SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2150146a4606SMichael Halcrow * packet tag 11 */ 2151146a4606SMichael Halcrow max_packet_size = (1 /* Tag 11 identifier */ 2152146a4606SMichael Halcrow + 3 /* Max Tag 11 packet size */ 2153146a4606SMichael Halcrow + 1 /* Binary format specifier */ 2154146a4606SMichael Halcrow + 1 /* Filename length */ 2155146a4606SMichael Halcrow + 8 /* Filename ("_CONSOLE") */ 2156146a4606SMichael Halcrow + 4 /* Modification date */ 2157146a4606SMichael Halcrow + contents_length); /* Literal data */ 2158146a4606SMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 2159146a4606SMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 216081acbcd6SAndrew Morton "need up to [%td] bytes, but there are only [%td] " 2161146a4606SMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 2162237fead6SMichael Halcrow rc = -EINVAL; 2163237fead6SMichael Halcrow goto out; 2164237fead6SMichael Halcrow } 2165237fead6SMichael Halcrow dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; 2166f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_length)], 2167f66e883eSMichael Halcrow (max_packet_size - 4), 2168f66e883eSMichael Halcrow &packet_size_length); 2169237fead6SMichael Halcrow if (rc) { 2170146a4606SMichael Halcrow printk(KERN_ERR "Error generating tag 11 packet header; cannot " 2171146a4606SMichael Halcrow "generate packet length. rc = [%d]\n", rc); 2172237fead6SMichael Halcrow goto out; 2173237fead6SMichael Halcrow } 2174237fead6SMichael Halcrow (*packet_length) += packet_size_length; 2175146a4606SMichael Halcrow dest[(*packet_length)++] = 0x62; /* binary data format specifier */ 2176237fead6SMichael Halcrow dest[(*packet_length)++] = 8; 2177237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], "_CONSOLE", 8); 2178237fead6SMichael Halcrow (*packet_length) += 8; 2179237fead6SMichael Halcrow memset(&dest[(*packet_length)], 0x00, 4); 2180237fead6SMichael Halcrow (*packet_length) += 4; 2181237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], contents, contents_length); 2182237fead6SMichael Halcrow (*packet_length) += contents_length; 2183237fead6SMichael Halcrow out: 2184237fead6SMichael Halcrow if (rc) 2185237fead6SMichael Halcrow (*packet_length) = 0; 2186146a4606SMichael Halcrow else 2187146a4606SMichael Halcrow (*remaining_bytes) -= (*packet_length); 2188237fead6SMichael Halcrow return rc; 2189237fead6SMichael Halcrow } 2190237fead6SMichael Halcrow 2191237fead6SMichael Halcrow /** 2192237fead6SMichael Halcrow * write_tag_3_packet 2193237fead6SMichael Halcrow * @dest: Buffer into which to write the packet 219422e78fafSMichael Halcrow * @remaining_bytes: Maximum number of bytes that can be written 2195237fead6SMichael Halcrow * @auth_tok: Authentication token 2196237fead6SMichael Halcrow * @crypt_stat: The cryptographic context 2197237fead6SMichael Halcrow * @key_rec: encrypted key 2198237fead6SMichael Halcrow * @packet_size: This function will write the number of bytes that end 2199237fead6SMichael Halcrow * up constituting the packet; set to zero on error 2200237fead6SMichael Halcrow * 2201237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2202237fead6SMichael Halcrow */ 2203237fead6SMichael Halcrow static int 2204f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes, 2205f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *auth_tok, 2206237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2207237fead6SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 2208237fead6SMichael Halcrow { 2209237fead6SMichael Halcrow size_t i; 2210237fead6SMichael Halcrow size_t encrypted_session_key_valid = 0; 2211237fead6SMichael Halcrow char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 2212ac97b9f9SMichael Halcrow struct scatterlist dst_sg[2]; 2213ac97b9f9SMichael Halcrow struct scatterlist src_sg[2]; 2214237fead6SMichael Halcrow struct mutex *tfm_mutex = NULL; 221519e66a67STrevor Highland u8 cipher_code; 2216f4aad16aSMichael Halcrow size_t packet_size_length; 2217f4aad16aSMichael Halcrow size_t max_packet_size; 2218f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2219f4aad16aSMichael Halcrow crypt_stat->mount_crypt_stat; 22203095e8e3SHerbert Xu struct crypto_skcipher *tfm; 22213095e8e3SHerbert Xu struct skcipher_request *req; 22228bba066fSMichael Halcrow int rc = 0; 2223237fead6SMichael Halcrow 2224237fead6SMichael Halcrow (*packet_size) = 0; 2225dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, 2226237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE); 22273095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 2228f4aad16aSMichael Halcrow crypt_stat->cipher); 2229f4aad16aSMichael Halcrow if (unlikely(rc)) { 2230f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 2231f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 2232f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 2233f4aad16aSMichael Halcrow goto out; 2234237fead6SMichael Halcrow } 2235f4aad16aSMichael Halcrow if (mount_crypt_stat->global_default_cipher_key_size == 0) { 2236f4aad16aSMichael Halcrow printk(KERN_WARNING "No key size specified at mount; " 22373095e8e3SHerbert Xu "defaulting to [%d]\n", 22383095e8e3SHerbert Xu crypto_skcipher_default_keysize(tfm)); 2239f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size = 22403095e8e3SHerbert Xu crypto_skcipher_default_keysize(tfm); 2241f4aad16aSMichael Halcrow } 2242f4aad16aSMichael Halcrow if (crypt_stat->key_size == 0) 2243f4aad16aSMichael Halcrow crypt_stat->key_size = 2244f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size; 2245237fead6SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 2246237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 2247237fead6SMichael Halcrow crypt_stat->key_size; 2248237fead6SMichael Halcrow if (crypt_stat->key_size == 24 2249237fead6SMichael Halcrow && strcmp("aes", crypt_stat->cipher) == 0) { 2250237fead6SMichael Halcrow memset((crypt_stat->key + 24), 0, 8); 2251237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 32; 2252f4aad16aSMichael Halcrow } else 2253f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; 2254dddfa461SMichael Halcrow key_rec->enc_key_size = 2255237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size; 2256f4aad16aSMichael Halcrow encrypted_session_key_valid = 0; 2257f4aad16aSMichael Halcrow for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) 2258f4aad16aSMichael Halcrow encrypted_session_key_valid |= 2259f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key[i]; 2260f4aad16aSMichael Halcrow if (encrypted_session_key_valid) { 2261f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " 2262f4aad16aSMichael Halcrow "using auth_tok->session_key.encrypted_key, " 2263f24b3887STyler Hicks "where key_rec->enc_key_size = [%zd]\n", 2264f4aad16aSMichael Halcrow key_rec->enc_key_size); 2265f4aad16aSMichael Halcrow memcpy(key_rec->enc_key, 2266f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key, 2267f4aad16aSMichael Halcrow key_rec->enc_key_size); 2268f4aad16aSMichael Halcrow goto encrypted_session_key_set; 2269f4aad16aSMichael Halcrow } 2270dddfa461SMichael Halcrow if (auth_tok->token.password.flags & 2271dddfa461SMichael Halcrow ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { 2272237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Using previously generated " 2273237fead6SMichael Halcrow "session key encryption key of size [%d]\n", 2274237fead6SMichael Halcrow auth_tok->token.password. 2275237fead6SMichael Halcrow session_key_encryption_key_bytes); 2276237fead6SMichael Halcrow memcpy(session_key_encryption_key, 2277237fead6SMichael Halcrow auth_tok->token.password.session_key_encryption_key, 2278237fead6SMichael Halcrow crypt_stat->key_size); 2279237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 2280df2e301fSJean Delvare "Cached session key encryption key:\n"); 2281237fead6SMichael Halcrow if (ecryptfs_verbosity > 0) 2282237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 2283237fead6SMichael Halcrow } 2284237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 2285237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); 2286237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 2287237fead6SMichael Halcrow } 22885dda6992SMichael Halcrow rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, 2289ac97b9f9SMichael Halcrow src_sg, 2); 2290ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 2291237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2292f4aad16aSMichael Halcrow "for crypt_stat session key; expected rc = 1; " 2293f24b3887STyler Hicks "got rc = [%d]. key_rec->enc_key_size = [%zd]\n", 2294f4aad16aSMichael Halcrow rc, key_rec->enc_key_size); 2295237fead6SMichael Halcrow rc = -ENOMEM; 2296237fead6SMichael Halcrow goto out; 2297237fead6SMichael Halcrow } 22985dda6992SMichael Halcrow rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, 2299ac97b9f9SMichael Halcrow dst_sg, 2); 2300ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 2301237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2302f4aad16aSMichael Halcrow "for crypt_stat encrypted session key; " 2303f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 2304f24b3887STyler Hicks "key_rec->enc_key_size = [%zd]\n", rc, 2305f4aad16aSMichael Halcrow key_rec->enc_key_size); 2306237fead6SMichael Halcrow rc = -ENOMEM; 2307237fead6SMichael Halcrow goto out; 2308237fead6SMichael Halcrow } 2309237fead6SMichael Halcrow mutex_lock(tfm_mutex); 23103095e8e3SHerbert Xu rc = crypto_skcipher_setkey(tfm, session_key_encryption_key, 2311237fead6SMichael Halcrow crypt_stat->key_size); 2312237fead6SMichael Halcrow if (rc < 0) { 2313237fead6SMichael Halcrow mutex_unlock(tfm_mutex); 2314237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error setting key for crypto " 23158bba066fSMichael Halcrow "context; rc = [%d]\n", rc); 2316237fead6SMichael Halcrow goto out; 2317237fead6SMichael Halcrow } 23183095e8e3SHerbert Xu 23193095e8e3SHerbert Xu req = skcipher_request_alloc(tfm, GFP_KERNEL); 23203095e8e3SHerbert Xu if (!req) { 23213095e8e3SHerbert Xu mutex_unlock(tfm_mutex); 23223095e8e3SHerbert Xu ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst " 23233095e8e3SHerbert Xu "attempting to skcipher_request_alloc for " 23243095e8e3SHerbert Xu "%s\n", crypto_skcipher_driver_name(tfm)); 23253095e8e3SHerbert Xu rc = -ENOMEM; 23263095e8e3SHerbert Xu goto out; 23273095e8e3SHerbert Xu } 23283095e8e3SHerbert Xu 23293095e8e3SHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 23303095e8e3SHerbert Xu NULL, NULL); 23313095e8e3SHerbert Xu 2332237fead6SMichael Halcrow rc = 0; 2333f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n", 2334237fead6SMichael Halcrow crypt_stat->key_size); 23353095e8e3SHerbert Xu skcipher_request_set_crypt(req, src_sg, dst_sg, 23363095e8e3SHerbert Xu (*key_rec).enc_key_size, NULL); 23373095e8e3SHerbert Xu rc = crypto_skcipher_encrypt(req); 2338f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 23393095e8e3SHerbert Xu skcipher_request_free(req); 23408bba066fSMichael Halcrow if (rc) { 23418bba066fSMichael Halcrow printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); 23428bba066fSMichael Halcrow goto out; 23438bba066fSMichael Halcrow } 2344237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); 2345f4aad16aSMichael Halcrow if (ecryptfs_verbosity > 0) { 2346f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n", 2347f4aad16aSMichael Halcrow key_rec->enc_key_size); 2348dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, 2349dddfa461SMichael Halcrow key_rec->enc_key_size); 2350f4aad16aSMichael Halcrow } 2351237fead6SMichael Halcrow encrypted_session_key_set: 2352237fead6SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2353237fead6SMichael Halcrow * packet tag 3 */ 2354f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 3 identifier */ 2355f4aad16aSMichael Halcrow + 3 /* Max Tag 3 packet size */ 2356f4aad16aSMichael Halcrow + 1 /* Version */ 2357f4aad16aSMichael Halcrow + 1 /* Cipher code */ 2358f4aad16aSMichael Halcrow + 1 /* S2K specifier */ 2359f4aad16aSMichael Halcrow + 1 /* Hash identifier */ 2360f4aad16aSMichael Halcrow + ECRYPTFS_SALT_SIZE /* Salt */ 2361f4aad16aSMichael Halcrow + 1 /* Hash iterations */ 2362f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 2363f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 236481acbcd6SAndrew Morton printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " 236581acbcd6SAndrew Morton "there are only [%td] available\n", max_packet_size, 2366f4aad16aSMichael Halcrow (*remaining_bytes)); 2367f4aad16aSMichael Halcrow rc = -EINVAL; 2368f4aad16aSMichael Halcrow goto out; 2369f4aad16aSMichael Halcrow } 2370237fead6SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; 2371f4aad16aSMichael Halcrow /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) 2372f4aad16aSMichael Halcrow * to get the number of octets in the actual Tag 3 packet */ 2373f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2374f66e883eSMichael Halcrow (max_packet_size - 4), 2375237fead6SMichael Halcrow &packet_size_length); 2376237fead6SMichael Halcrow if (rc) { 2377f4aad16aSMichael Halcrow printk(KERN_ERR "Error generating tag 3 packet header; cannot " 2378f4aad16aSMichael Halcrow "generate packet length. rc = [%d]\n", rc); 2379237fead6SMichael Halcrow goto out; 2380237fead6SMichael Halcrow } 2381237fead6SMichael Halcrow (*packet_size) += packet_size_length; 2382237fead6SMichael Halcrow dest[(*packet_size)++] = 0x04; /* version 4 */ 2383f4aad16aSMichael Halcrow /* TODO: Break from RFC2440 so that arbitrary ciphers can be 2384f4aad16aSMichael Halcrow * specified with strings */ 23859c79f34fSMichael Halcrow cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher, 23869c79f34fSMichael Halcrow crypt_stat->key_size); 2387237fead6SMichael Halcrow if (cipher_code == 0) { 2388237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unable to generate code for " 2389237fead6SMichael Halcrow "cipher [%s]\n", crypt_stat->cipher); 2390237fead6SMichael Halcrow rc = -EINVAL; 2391237fead6SMichael Halcrow goto out; 2392237fead6SMichael Halcrow } 2393237fead6SMichael Halcrow dest[(*packet_size)++] = cipher_code; 2394237fead6SMichael Halcrow dest[(*packet_size)++] = 0x03; /* S2K */ 2395237fead6SMichael Halcrow dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ 2396237fead6SMichael Halcrow memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, 2397237fead6SMichael Halcrow ECRYPTFS_SALT_SIZE); 2398237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ 2399237fead6SMichael Halcrow dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ 2400dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 2401dddfa461SMichael Halcrow key_rec->enc_key_size); 2402dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 2403237fead6SMichael Halcrow out: 2404237fead6SMichael Halcrow if (rc) 2405237fead6SMichael Halcrow (*packet_size) = 0; 2406f4aad16aSMichael Halcrow else 2407f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 2408237fead6SMichael Halcrow return rc; 2409237fead6SMichael Halcrow } 2410237fead6SMichael Halcrow 2411eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache; 2412eb95e7ffSMichael Halcrow 2413237fead6SMichael Halcrow /** 2414237fead6SMichael Halcrow * ecryptfs_generate_key_packet_set 241522e78fafSMichael Halcrow * @dest_base: Virtual address from which to write the key record set 2416237fead6SMichael Halcrow * @crypt_stat: The cryptographic context from which the 2417237fead6SMichael Halcrow * authentication tokens will be retrieved 2418237fead6SMichael Halcrow * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat 2419237fead6SMichael Halcrow * for the global parameters 2420237fead6SMichael Halcrow * @len: The amount written 2421237fead6SMichael Halcrow * @max: The maximum amount of data allowed to be written 2422237fead6SMichael Halcrow * 2423237fead6SMichael Halcrow * Generates a key packet set and writes it to the virtual address 2424237fead6SMichael Halcrow * passed in. 2425237fead6SMichael Halcrow * 2426237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2427237fead6SMichael Halcrow */ 2428237fead6SMichael Halcrow int 2429237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base, 2430237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2431237fead6SMichael Halcrow struct dentry *ecryptfs_dentry, size_t *len, 2432237fead6SMichael Halcrow size_t max) 2433237fead6SMichael Halcrow { 2434237fead6SMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 24350e1fc5efSRoberto Sassu struct key *auth_tok_key = NULL; 2436237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2437237fead6SMichael Halcrow &ecryptfs_superblock_to_private( 2438237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 2439237fead6SMichael Halcrow size_t written; 2440eb95e7ffSMichael Halcrow struct ecryptfs_key_record *key_rec; 2441f4aad16aSMichael Halcrow struct ecryptfs_key_sig *key_sig; 2442dddfa461SMichael Halcrow int rc = 0; 2443237fead6SMichael Halcrow 2444237fead6SMichael Halcrow (*len) = 0; 2445f4aad16aSMichael Halcrow mutex_lock(&crypt_stat->keysig_list_mutex); 2446eb95e7ffSMichael Halcrow key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); 2447eb95e7ffSMichael Halcrow if (!key_rec) { 2448eb95e7ffSMichael Halcrow rc = -ENOMEM; 2449eb95e7ffSMichael Halcrow goto out; 2450eb95e7ffSMichael Halcrow } 2451f4aad16aSMichael Halcrow list_for_each_entry(key_sig, &crypt_stat->keysig_list, 2452f4aad16aSMichael Halcrow crypt_stat_list) { 2453f4aad16aSMichael Halcrow memset(key_rec, 0, sizeof(*key_rec)); 24540e1fc5efSRoberto Sassu rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key, 24550e1fc5efSRoberto Sassu &auth_tok, 2456f4aad16aSMichael Halcrow mount_crypt_stat, 2457f4aad16aSMichael Halcrow key_sig->keysig); 2458f4aad16aSMichael Halcrow if (rc) { 24590e1fc5efSRoberto Sassu printk(KERN_WARNING "Unable to retrieve auth tok with " 24600e1fc5efSRoberto Sassu "sig = [%s]\n", key_sig->keysig); 24610e1fc5efSRoberto Sassu rc = process_find_global_auth_tok_for_sig_err(rc); 2462f4aad16aSMichael Halcrow goto out_free; 2463f4aad16aSMichael Halcrow } 2464237fead6SMichael Halcrow if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 2465237fead6SMichael Halcrow rc = write_tag_3_packet((dest_base + (*len)), 2466f4aad16aSMichael Halcrow &max, auth_tok, 2467eb95e7ffSMichael Halcrow crypt_stat, key_rec, 2468237fead6SMichael Halcrow &written); 2469b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2470b2987a5eSTyler Hicks key_put(auth_tok_key); 2471237fead6SMichael Halcrow if (rc) { 2472237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 2473237fead6SMichael Halcrow "writing tag 3 packet\n"); 2474eb95e7ffSMichael Halcrow goto out_free; 2475237fead6SMichael Halcrow } 2476237fead6SMichael Halcrow (*len) += written; 2477237fead6SMichael Halcrow /* Write auth tok signature packet */ 2478f4aad16aSMichael Halcrow rc = write_tag_11_packet((dest_base + (*len)), &max, 2479f4aad16aSMichael Halcrow key_rec->sig, 2480f4aad16aSMichael Halcrow ECRYPTFS_SIG_SIZE, &written); 2481237fead6SMichael Halcrow if (rc) { 2482237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing " 2483237fead6SMichael Halcrow "auth tok signature packet\n"); 2484eb95e7ffSMichael Halcrow goto out_free; 2485237fead6SMichael Halcrow } 2486237fead6SMichael Halcrow (*len) += written; 2487dddfa461SMichael Halcrow } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 2488b2987a5eSTyler Hicks rc = write_tag_1_packet(dest_base + (*len), &max, 2489b2987a5eSTyler Hicks auth_tok_key, auth_tok, 2490f4aad16aSMichael Halcrow crypt_stat, key_rec, &written); 2491dddfa461SMichael Halcrow if (rc) { 2492dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 2493dddfa461SMichael Halcrow "writing tag 1 packet\n"); 2494eb95e7ffSMichael Halcrow goto out_free; 2495dddfa461SMichael Halcrow } 2496dddfa461SMichael Halcrow (*len) += written; 2497237fead6SMichael Halcrow } else { 2498b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2499b2987a5eSTyler Hicks key_put(auth_tok_key); 2500237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unsupported " 2501237fead6SMichael Halcrow "authentication token type\n"); 2502237fead6SMichael Halcrow rc = -EINVAL; 2503eb95e7ffSMichael Halcrow goto out_free; 2504237fead6SMichael Halcrow } 2505f4aad16aSMichael Halcrow } 2506f4aad16aSMichael Halcrow if (likely(max > 0)) { 2507237fead6SMichael Halcrow dest_base[(*len)] = 0x00; 2508237fead6SMichael Halcrow } else { 2509237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 2510237fead6SMichael Halcrow rc = -EIO; 2511237fead6SMichael Halcrow } 2512eb95e7ffSMichael Halcrow out_free: 2513eb95e7ffSMichael Halcrow kmem_cache_free(ecryptfs_key_record_cache, key_rec); 2514237fead6SMichael Halcrow out: 2515237fead6SMichael Halcrow if (rc) 2516237fead6SMichael Halcrow (*len) = 0; 2517f4aad16aSMichael Halcrow mutex_unlock(&crypt_stat->keysig_list_mutex); 2518237fead6SMichael Halcrow return rc; 2519237fead6SMichael Halcrow } 2520f4aad16aSMichael Halcrow 2521f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache; 2522f4aad16aSMichael Halcrow 2523f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) 2524f4aad16aSMichael Halcrow { 2525f4aad16aSMichael Halcrow struct ecryptfs_key_sig *new_key_sig; 2526f4aad16aSMichael Halcrow 2527f4aad16aSMichael Halcrow new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); 2528f4aad16aSMichael Halcrow if (!new_key_sig) { 2529f4aad16aSMichael Halcrow printk(KERN_ERR 2530f4aad16aSMichael Halcrow "Error allocating from ecryptfs_key_sig_cache\n"); 2531aa06117fSRoland Dreier return -ENOMEM; 2532f4aad16aSMichael Halcrow } 2533f4aad16aSMichael Halcrow memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); 25347762e230SRoberto Sassu new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2535aa06117fSRoland Dreier /* Caller must hold keysig_list_mutex */ 2536f4aad16aSMichael Halcrow list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); 2537aa06117fSRoland Dreier 2538aa06117fSRoland Dreier return 0; 2539f4aad16aSMichael Halcrow } 2540f4aad16aSMichael Halcrow 2541f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache; 2542f4aad16aSMichael Halcrow 2543f4aad16aSMichael Halcrow int 2544f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 254584814d64STyler Hicks char *sig, u32 global_auth_tok_flags) 2546f4aad16aSMichael Halcrow { 2547f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *new_auth_tok; 2548f4aad16aSMichael Halcrow int rc = 0; 2549f4aad16aSMichael Halcrow 2550459e2164SEric Sandeen new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, 2551f4aad16aSMichael Halcrow GFP_KERNEL); 2552f4aad16aSMichael Halcrow if (!new_auth_tok) { 2553f4aad16aSMichael Halcrow rc = -ENOMEM; 2554f4aad16aSMichael Halcrow printk(KERN_ERR "Error allocating from " 2555f4aad16aSMichael Halcrow "ecryptfs_global_auth_tok_cache\n"); 2556f4aad16aSMichael Halcrow goto out; 2557f4aad16aSMichael Halcrow } 2558f4aad16aSMichael Halcrow memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); 255984814d64STyler Hicks new_auth_tok->flags = global_auth_tok_flags; 2560f4aad16aSMichael Halcrow new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2561f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 2562f4aad16aSMichael Halcrow list_add(&new_auth_tok->mount_crypt_stat_list, 2563f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list); 2564f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 2565f4aad16aSMichael Halcrow out: 2566f4aad16aSMichael Halcrow return rc; 2567f4aad16aSMichael Halcrow } 2568f4aad16aSMichael Halcrow 2569