blob: c5c84dfb5b3e3e0cf488a38846d4897b19fd53d0 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * In-kernel key management code. Includes functions to parse and
4 * write authentication token-related packets with the underlying
5 * file.
6 *
7 * Copyright (C) 2004-2006 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -080010 * Trevor S. Highland <trevor.highland@gmail.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
Herbert Xu3095e8e2016-01-25 10:29:33 +080028#include <crypto/hash.h>
29#include <crypto/skcipher.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070030#include <linux/string.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070031#include <linux/pagemap.h>
32#include <linux/key.h>
33#include <linux/random.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070034#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070036#include "ecryptfs_kernel.h"
37
38/**
39 * request_key returned an error instead of a valid key address;
40 * determine the type of error, make appropriate log entries, and
41 * return an error code.
42 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -070043static int process_request_key_err(long err_code)
Michael Halcrow237fead2006-10-04 02:16:22 -070044{
45 int rc = 0;
46
47 switch (err_code) {
Eric Sandeen982363c2008-07-23 21:30:04 -070048 case -ENOKEY:
Michael Halcrow237fead2006-10-04 02:16:22 -070049 ecryptfs_printk(KERN_WARNING, "No key\n");
50 rc = -ENOENT;
51 break;
Eric Sandeen982363c2008-07-23 21:30:04 -070052 case -EKEYEXPIRED:
Michael Halcrow237fead2006-10-04 02:16:22 -070053 ecryptfs_printk(KERN_WARNING, "Key expired\n");
54 rc = -ETIME;
55 break;
Eric Sandeen982363c2008-07-23 21:30:04 -070056 case -EKEYREVOKED:
Michael Halcrow237fead2006-10-04 02:16:22 -070057 ecryptfs_printk(KERN_WARNING, "Key revoked\n");
58 rc = -EINVAL;
59 break;
60 default:
61 ecryptfs_printk(KERN_WARNING, "Unknown error code: "
Joe Perches888d57b2010-11-10 15:46:16 -080062 "[0x%.16lx]\n", err_code);
Michael Halcrow237fead2006-10-04 02:16:22 -070063 rc = -EINVAL;
64 }
65 return rc;
66}
67
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +010068static int process_find_global_auth_tok_for_sig_err(int err_code)
69{
70 int rc = err_code;
71
72 switch (err_code) {
73 case -ENOENT:
74 ecryptfs_printk(KERN_WARNING, "Missing auth tok\n");
75 break;
76 case -EINVAL:
77 ecryptfs_printk(KERN_WARNING, "Invalid auth tok\n");
78 break;
79 default:
80 rc = process_request_key_err(err_code);
81 break;
82 }
83 return rc;
84}
85
Michael Halcrow237fead2006-10-04 02:16:22 -070086/**
Michael Halcrowf66e8832008-04-29 00:59:51 -070087 * ecryptfs_parse_packet_length
Michael Halcrow237fead2006-10-04 02:16:22 -070088 * @data: Pointer to memory containing length at offset
89 * @size: This function writes the decoded size to this memory
90 * address; zero on error
91 * @length_size: The number of bytes occupied by the encoded length
92 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -070093 * Returns zero on success; non-zero on error
Michael Halcrow237fead2006-10-04 02:16:22 -070094 */
Michael Halcrowf66e8832008-04-29 00:59:51 -070095int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
96 size_t *length_size)
Michael Halcrow237fead2006-10-04 02:16:22 -070097{
98 int rc = 0;
99
100 (*length_size) = 0;
101 (*size) = 0;
102 if (data[0] < 192) {
103 /* One-byte length */
Tyler Hicks831115a2014-10-23 10:16:06 -0400104 (*size) = data[0];
Michael Halcrow237fead2006-10-04 02:16:22 -0700105 (*length_size) = 1;
106 } else if (data[0] < 224) {
107 /* Two-byte length */
Tyler Hicks831115a2014-10-23 10:16:06 -0400108 (*size) = (data[0] - 192) * 256;
109 (*size) += data[1] + 192;
Michael Halcrow237fead2006-10-04 02:16:22 -0700110 (*length_size) = 2;
111 } else if (data[0] == 255) {
Tyler Hicks48399c02012-01-14 16:46:46 +0100112 /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */
Michael Halcrow237fead2006-10-04 02:16:22 -0700113 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
114 "supported\n");
115 rc = -EINVAL;
116 goto out;
117 } else {
118 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
119 rc = -EINVAL;
120 goto out;
121 }
122out:
123 return rc;
124}
125
126/**
Michael Halcrowf66e8832008-04-29 00:59:51 -0700127 * ecryptfs_write_packet_length
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700128 * @dest: The byte array target into which to write the length. Must
Tyler Hicks48399c02012-01-14 16:46:46 +0100129 * have at least ECRYPTFS_MAX_PKT_LEN_SIZE bytes allocated.
Michael Halcrow237fead2006-10-04 02:16:22 -0700130 * @size: The length to write.
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700131 * @packet_size_length: The number of bytes used to encode the packet
132 * length is written to this address.
Michael Halcrow237fead2006-10-04 02:16:22 -0700133 *
134 * Returns zero on success; non-zero on error.
135 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700136int ecryptfs_write_packet_length(char *dest, size_t size,
137 size_t *packet_size_length)
Michael Halcrow237fead2006-10-04 02:16:22 -0700138{
139 int rc = 0;
140
141 if (size < 192) {
142 dest[0] = size;
143 (*packet_size_length) = 1;
144 } else if (size < 65536) {
145 dest[0] = (((size - 192) / 256) + 192);
146 dest[1] = ((size - 192) % 256);
147 (*packet_size_length) = 2;
148 } else {
Tyler Hicks48399c02012-01-14 16:46:46 +0100149 /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */
Michael Halcrow237fead2006-10-04 02:16:22 -0700150 rc = -EINVAL;
151 ecryptfs_printk(KERN_WARNING,
Tyler Hicksf24b3882010-11-15 17:36:38 -0600152 "Unsupported packet size: [%zd]\n", size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700153 }
154 return rc;
155}
156
Michael Halcrowdddfa462007-02-12 00:53:44 -0800157static int
158write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
159 char **packet, size_t *packet_len)
160{
161 size_t i = 0;
162 size_t data_len;
163 size_t packet_size_len;
164 char *message;
165 int rc;
166
167 /*
168 * ***** TAG 64 Packet Format *****
169 * | Content Type | 1 byte |
170 * | Key Identifier Size | 1 or 2 bytes |
171 * | Key Identifier | arbitrary |
172 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
173 * | Encrypted File Encryption Key | arbitrary |
174 */
175 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
176 + session_key->encrypted_key_size);
177 *packet = kmalloc(data_len, GFP_KERNEL);
178 message = *packet;
179 if (!message) {
180 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
181 rc = -ENOMEM;
182 goto out;
183 }
184 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700185 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
186 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800187 if (rc) {
188 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
189 "header; cannot generate packet length\n");
190 goto out;
191 }
192 i += packet_size_len;
193 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
194 i += ECRYPTFS_SIG_SIZE_HEX;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700195 rc = ecryptfs_write_packet_length(&message[i],
196 session_key->encrypted_key_size,
197 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800198 if (rc) {
199 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
200 "header; cannot generate packet length\n");
201 goto out;
202 }
203 i += packet_size_len;
204 memcpy(&message[i], session_key->encrypted_key,
205 session_key->encrypted_key_size);
206 i += session_key->encrypted_key_size;
207 *packet_len = i;
208out:
209 return rc;
210}
211
212static int
Trevor Highland19e66a62008-02-06 01:38:36 -0800213parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
Michael Halcrowdddfa462007-02-12 00:53:44 -0800214 struct ecryptfs_message *msg)
215{
216 size_t i = 0;
217 char *data;
218 size_t data_len;
219 size_t m_size;
220 size_t message_len;
221 u16 checksum = 0;
222 u16 expected_checksum = 0;
223 int rc;
224
225 /*
226 * ***** TAG 65 Packet Format *****
227 * | Content Type | 1 byte |
228 * | Status Indicator | 1 byte |
229 * | File Encryption Key Size | 1 or 2 bytes |
230 * | File Encryption Key | arbitrary |
231 */
232 message_len = msg->data_len;
233 data = msg->data;
234 if (message_len < 4) {
235 rc = -EIO;
236 goto out;
237 }
238 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
239 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
240 rc = -EIO;
241 goto out;
242 }
243 if (data[i++]) {
244 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
245 "[%d]\n", data[i-1]);
246 rc = -EIO;
247 goto out;
248 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700249 rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800250 if (rc) {
251 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
252 "rc = [%d]\n", rc);
253 goto out;
254 }
255 i += data_len;
256 if (message_len < (i + m_size)) {
Tyler Hicks624ae522008-10-15 22:02:51 -0700257 ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd "
258 "is shorter than expected\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800259 rc = -EIO;
260 goto out;
261 }
262 if (m_size < 3) {
263 ecryptfs_printk(KERN_ERR,
264 "The decrypted key is not long enough to "
265 "include a cipher code and checksum\n");
266 rc = -EIO;
267 goto out;
268 }
269 *cipher_code = data[i++];
270 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
271 session_key->decrypted_key_size = m_size - 3;
272 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
273 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
274 "the maximum key size [%d]\n",
275 session_key->decrypted_key_size,
276 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
277 rc = -EIO;
278 goto out;
279 }
280 memcpy(session_key->decrypted_key, &data[i],
281 session_key->decrypted_key_size);
282 i += session_key->decrypted_key_size;
283 expected_checksum += (unsigned char)(data[i++]) << 8;
284 expected_checksum += (unsigned char)(data[i++]);
285 for (i = 0; i < session_key->decrypted_key_size; i++)
286 checksum += session_key->decrypted_key[i];
287 if (expected_checksum != checksum) {
288 ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
289 "encryption key; expected [%x]; calculated "
290 "[%x]\n", expected_checksum, checksum);
291 rc = -EIO;
292 }
293out:
294 return rc;
295}
296
297
298static int
Trevor Highland19e66a62008-02-06 01:38:36 -0800299write_tag_66_packet(char *signature, u8 cipher_code,
Michael Halcrowdddfa462007-02-12 00:53:44 -0800300 struct ecryptfs_crypt_stat *crypt_stat, char **packet,
301 size_t *packet_len)
302{
303 size_t i = 0;
304 size_t j;
305 size_t data_len;
306 size_t checksum = 0;
307 size_t packet_size_len;
308 char *message;
309 int rc;
310
311 /*
312 * ***** TAG 66 Packet Format *****
313 * | Content Type | 1 byte |
314 * | Key Identifier Size | 1 or 2 bytes |
315 * | Key Identifier | arbitrary |
316 * | File Encryption Key Size | 1 or 2 bytes |
317 * | File Encryption Key | arbitrary |
318 */
319 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
320 *packet = kmalloc(data_len, GFP_KERNEL);
321 message = *packet;
322 if (!message) {
323 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
324 rc = -ENOMEM;
325 goto out;
326 }
327 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700328 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
329 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800330 if (rc) {
331 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
332 "header; cannot generate packet length\n");
333 goto out;
334 }
335 i += packet_size_len;
336 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
337 i += ECRYPTFS_SIG_SIZE_HEX;
338 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700339 rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
340 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800341 if (rc) {
342 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
343 "header; cannot generate packet length\n");
344 goto out;
345 }
346 i += packet_size_len;
347 message[i++] = cipher_code;
348 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
349 i += crypt_stat->key_size;
350 for (j = 0; j < crypt_stat->key_size; j++)
351 checksum += crypt_stat->key[j];
352 message[i++] = (checksum / 256) % 256;
353 message[i++] = (checksum % 256);
354 *packet_len = i;
355out:
356 return rc;
357}
358
359static int
360parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
361 struct ecryptfs_message *msg)
362{
363 size_t i = 0;
364 char *data;
365 size_t data_len;
366 size_t message_len;
367 int rc;
368
369 /*
370 * ***** TAG 65 Packet Format *****
371 * | Content Type | 1 byte |
372 * | Status Indicator | 1 byte |
373 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
374 * | Encrypted File Encryption Key | arbitrary |
375 */
376 message_len = msg->data_len;
377 data = msg->data;
378 /* verify that everything through the encrypted FEK size is present */
379 if (message_len < 4) {
380 rc = -EIO;
Michael Halcrowdf261c52009-01-06 14:42:02 -0800381 printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable "
Michael Halcrowf66e8832008-04-29 00:59:51 -0700382 "message length is [%d]\n", __func__, message_len, 4);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800383 goto out;
384 }
385 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800386 rc = -EIO;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700387 printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n",
388 __func__);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800389 goto out;
390 }
391 if (data[i++]) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800392 rc = -EIO;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700393 printk(KERN_ERR "%s: Status indicator has non zero "
394 "value [%d]\n", __func__, data[i-1]);
395
Michael Halcrowdddfa462007-02-12 00:53:44 -0800396 goto out;
397 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700398 rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
399 &data_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800400 if (rc) {
401 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
402 "rc = [%d]\n", rc);
403 goto out;
404 }
405 i += data_len;
406 if (message_len < (i + key_rec->enc_key_size)) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800407 rc = -EIO;
Michael Halcrowdf261c52009-01-06 14:42:02 -0800408 printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n",
Michael Halcrowf66e8832008-04-29 00:59:51 -0700409 __func__, message_len, (i + key_rec->enc_key_size));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800410 goto out;
411 }
412 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800413 rc = -EIO;
Michael Halcrowdf261c52009-01-06 14:42:02 -0800414 printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than "
Michael Halcrowf66e8832008-04-29 00:59:51 -0700415 "the maximum key size [%d]\n", __func__,
416 key_rec->enc_key_size,
417 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800418 goto out;
419 }
420 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
421out:
422 return rc;
423}
424
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100425/**
426 * ecryptfs_verify_version
427 * @version: The version number to confirm
428 *
429 * Returns zero on good version; non-zero otherwise
430 */
431static int ecryptfs_verify_version(u16 version)
432{
433 int rc = 0;
434 unsigned char major;
435 unsigned char minor;
436
437 major = ((version >> 8) & 0xFF);
438 minor = (version & 0xFF);
439 if (major != ECRYPTFS_VERSION_MAJOR) {
440 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
441 "Expected [%d]; got [%d]\n",
442 ECRYPTFS_VERSION_MAJOR, major);
443 rc = -EINVAL;
444 goto out;
445 }
446 if (minor != ECRYPTFS_VERSION_MINOR) {
447 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
448 "Expected [%d]; got [%d]\n",
449 ECRYPTFS_VERSION_MINOR, minor);
450 rc = -EINVAL;
451 goto out;
452 }
453out:
454 return rc;
455}
456
457/**
458 * ecryptfs_verify_auth_tok_from_key
459 * @auth_tok_key: key containing the authentication token
460 * @auth_tok: authentication token
461 *
462 * Returns zero on valid auth tok; -EINVAL otherwise
463 */
464static int
465ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key,
466 struct ecryptfs_auth_tok **auth_tok)
467{
468 int rc = 0;
469
470 (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key);
471 if (ecryptfs_verify_version((*auth_tok)->version)) {
472 printk(KERN_ERR "Data structure version mismatch. Userspace "
473 "tools must match eCryptfs kernel module with major "
474 "version [%d] and minor version [%d]\n",
475 ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR);
476 rc = -EINVAL;
477 goto out;
478 }
479 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
480 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
481 printk(KERN_ERR "Invalid auth_tok structure "
482 "returned from key query\n");
483 rc = -EINVAL;
484 goto out;
485 }
486out:
487 return rc;
488}
489
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700490static int
Michael Halcrow9c79f342009-01-06 14:41:57 -0800491ecryptfs_find_global_auth_tok_for_sig(
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100492 struct key **auth_tok_key,
493 struct ecryptfs_auth_tok **auth_tok,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800494 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
495{
496 struct ecryptfs_global_auth_tok *walker;
497 int rc = 0;
498
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100499 (*auth_tok_key) = NULL;
500 (*auth_tok) = NULL;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800501 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
502 list_for_each_entry(walker,
503 &mount_crypt_stat->global_auth_tok_list,
504 mount_crypt_stat_list) {
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100505 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX))
506 continue;
507
508 if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) {
509 rc = -EINVAL;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800510 goto out;
511 }
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100512
513 rc = key_validate(walker->global_auth_tok_key);
514 if (rc) {
515 if (rc == -EKEYEXPIRED)
516 goto out;
517 goto out_invalid_auth_tok;
518 }
519
Roberto Sassub5695d02011-03-21 16:00:55 +0100520 down_write(&(walker->global_auth_tok_key->sem));
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100521 rc = ecryptfs_verify_auth_tok_from_key(
522 walker->global_auth_tok_key, auth_tok);
523 if (rc)
Roberto Sassub5695d02011-03-21 16:00:55 +0100524 goto out_invalid_auth_tok_unlock;
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100525
526 (*auth_tok_key) = walker->global_auth_tok_key;
527 key_get(*auth_tok_key);
528 goto out;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800529 }
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100530 rc = -ENOENT;
531 goto out;
Roberto Sassub5695d02011-03-21 16:00:55 +0100532out_invalid_auth_tok_unlock:
533 up_write(&(walker->global_auth_tok_key->sem));
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100534out_invalid_auth_tok:
535 printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig);
536 walker->flags |= ECRYPTFS_AUTH_TOK_INVALID;
537 key_put(walker->global_auth_tok_key);
538 walker->global_auth_tok_key = NULL;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800539out:
540 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
541 return rc;
542}
543
544/**
545 * ecryptfs_find_auth_tok_for_sig
546 * @auth_tok: Set to the matching auth_tok; NULL if not found
547 * @crypt_stat: inode crypt_stat crypto context
548 * @sig: Sig of auth_tok to find
549 *
550 * For now, this function simply looks at the registered auth_tok's
551 * linked off the mount_crypt_stat, so all the auth_toks that can be
552 * used must be registered at mount time. This function could
553 * potentially try a lot harder to find auth_tok's (e.g., by calling
554 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
555 * that static registration of auth_tok's will no longer be necessary.
556 *
557 * Returns zero on no error; non-zero on error
558 */
559static int
560ecryptfs_find_auth_tok_for_sig(
Roberto Sassuaee683b2010-10-06 18:31:06 +0200561 struct key **auth_tok_key,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800562 struct ecryptfs_auth_tok **auth_tok,
563 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
564 char *sig)
565{
Michael Halcrow9c79f342009-01-06 14:41:57 -0800566 int rc = 0;
567
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100568 rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok,
569 mount_crypt_stat, sig);
570 if (rc == -ENOENT) {
Roberto Sassuf16feb52010-10-06 18:31:32 +0200571 /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the
572 * mount_crypt_stat structure, we prevent to use auth toks that
573 * are not inserted through the ecryptfs_add_global_auth_tok
574 * function.
575 */
576 if (mount_crypt_stat->flags
577 & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY)
578 return -EINVAL;
579
Roberto Sassuaee683b2010-10-06 18:31:06 +0200580 rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800581 sig);
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +0100582 }
Michael Halcrow9c79f342009-01-06 14:41:57 -0800583 return rc;
584}
585
586/**
587 * write_tag_70_packet can gobble a lot of stack space. We stuff most
588 * of the function's parameters in a kmalloc'd struct to help reduce
589 * eCryptfs' overall stack usage.
590 */
591struct ecryptfs_write_tag_70_packet_silly_stack {
592 u8 cipher_code;
593 size_t max_packet_size;
594 size_t packet_size_len;
595 size_t block_aligned_filename_size;
596 size_t block_size;
597 size_t i;
598 size_t j;
599 size_t num_rand_bytes;
600 struct mutex *tfm_mutex;
601 char *block_aligned_filename;
602 struct ecryptfs_auth_tok *auth_tok;
Tyler Hicks8d08dab2011-05-17 00:50:33 -0500603 struct scatterlist src_sg[2];
604 struct scatterlist dst_sg[2];
Herbert Xu3095e8e2016-01-25 10:29:33 +0800605 struct crypto_skcipher *skcipher_tfm;
606 struct skcipher_request *skcipher_req;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800607 char iv[ECRYPTFS_MAX_IV_BYTES];
608 char hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
609 char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
Herbert Xu3095e8e2016-01-25 10:29:33 +0800610 struct crypto_shash *hash_tfm;
611 struct shash_desc *hash_desc;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800612};
613
614/**
615 * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK
616 * @filename: NULL-terminated filename string
617 *
618 * This is the simplest mechanism for achieving filename encryption in
619 * eCryptfs. It encrypts the given filename with the mount-wide
620 * filename encryption key (FNEK) and stores it in a packet to @dest,
621 * which the callee will encode and write directly into the dentry
622 * name.
623 */
624int
625ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
626 size_t *packet_size,
627 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
628 char *filename, size_t filename_size)
629{
630 struct ecryptfs_write_tag_70_packet_silly_stack *s;
Roberto Sassuaee683b2010-10-06 18:31:06 +0200631 struct key *auth_tok_key = NULL;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800632 int rc = 0;
633
Herbert Xu3095e8e2016-01-25 10:29:33 +0800634 s = kzalloc(sizeof(*s), GFP_KERNEL);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800635 if (!s) {
636 printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
Michael Halcrowdf261c52009-01-06 14:42:02 -0800637 "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
Julia Lawallf137f152010-08-11 12:11:41 +0200638 rc = -ENOMEM;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800639 goto out;
640 }
Michael Halcrow9c79f342009-01-06 14:41:57 -0800641 (*packet_size) = 0;
Roberto Sassu950983f2011-03-21 16:00:54 +0100642 rc = ecryptfs_find_auth_tok_for_sig(
643 &auth_tok_key,
644 &s->auth_tok, mount_crypt_stat,
645 mount_crypt_stat->global_default_fnek_sig);
646 if (rc) {
647 printk(KERN_ERR "%s: Error attempting to find auth tok for "
648 "fnek sig [%s]; rc = [%d]\n", __func__,
649 mount_crypt_stat->global_default_fnek_sig, rc);
650 goto out;
651 }
Michael Halcrow9c79f342009-01-06 14:41:57 -0800652 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
Herbert Xu3095e8e2016-01-25 10:29:33 +0800653 &s->skcipher_tfm,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800654 &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
655 if (unlikely(rc)) {
656 printk(KERN_ERR "Internal error whilst attempting to get "
657 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
658 mount_crypt_stat->global_default_fn_cipher_name, rc);
659 goto out;
660 }
661 mutex_lock(s->tfm_mutex);
Herbert Xu3095e8e2016-01-25 10:29:33 +0800662 s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800663 /* Plus one for the \0 separator between the random prefix
664 * and the plaintext filename */
665 s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1);
666 s->block_aligned_filename_size = (s->num_rand_bytes + filename_size);
667 if ((s->block_aligned_filename_size % s->block_size) != 0) {
668 s->num_rand_bytes += (s->block_size
669 - (s->block_aligned_filename_size
670 % s->block_size));
671 s->block_aligned_filename_size = (s->num_rand_bytes
672 + filename_size);
673 }
674 /* Octet 0: Tag 70 identifier
675 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
676 * and block-aligned encrypted filename size)
677 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
678 * Octet N2-N3: Cipher identifier (1 octet)
679 * Octets N3-N4: Block-aligned encrypted filename
680 * - Consists of a minimum number of random characters, a \0
681 * separator, and then the filename */
Tyler Hicks4a266202011-11-05 13:45:08 -0400682 s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE
Michael Halcrow9c79f342009-01-06 14:41:57 -0800683 + s->block_aligned_filename_size);
684 if (dest == NULL) {
685 (*packet_size) = s->max_packet_size;
686 goto out_unlock;
687 }
688 if (s->max_packet_size > (*remaining_bytes)) {
Michael Halcrowa8f12862009-01-06 14:42:03 -0800689 printk(KERN_WARNING "%s: Require [%zd] bytes to write; only "
690 "[%zd] available\n", __func__, s->max_packet_size,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800691 (*remaining_bytes));
692 rc = -EINVAL;
693 goto out_unlock;
694 }
Herbert Xu3095e8e2016-01-25 10:29:33 +0800695
696 s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL);
697 if (!s->skcipher_req) {
698 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
699 "skcipher_request_alloc for %s\n", __func__,
700 crypto_skcipher_driver_name(s->skcipher_tfm));
701 rc = -ENOMEM;
702 goto out_unlock;
703 }
704
705 skcipher_request_set_callback(s->skcipher_req,
706 CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
707
Michael Halcrow9c79f342009-01-06 14:41:57 -0800708 s->block_aligned_filename = kzalloc(s->block_aligned_filename_size,
709 GFP_KERNEL);
710 if (!s->block_aligned_filename) {
711 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
Michael Halcrowdf261c52009-01-06 14:42:02 -0800712 "kzalloc [%zd] bytes\n", __func__,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800713 s->block_aligned_filename_size);
714 rc = -ENOMEM;
715 goto out_unlock;
716 }
Michael Halcrow9c79f342009-01-06 14:41:57 -0800717 dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE;
718 rc = ecryptfs_write_packet_length(&dest[s->i],
719 (ECRYPTFS_SIG_SIZE
720 + 1 /* Cipher code */
721 + s->block_aligned_filename_size),
722 &s->packet_size_len);
723 if (rc) {
724 printk(KERN_ERR "%s: Error generating tag 70 packet "
725 "header; cannot generate packet length; rc = [%d]\n",
726 __func__, rc);
727 goto out_free_unlock;
728 }
729 s->i += s->packet_size_len;
730 ecryptfs_from_hex(&dest[s->i],
731 mount_crypt_stat->global_default_fnek_sig,
732 ECRYPTFS_SIG_SIZE);
733 s->i += ECRYPTFS_SIG_SIZE;
734 s->cipher_code = ecryptfs_code_for_cipher_string(
735 mount_crypt_stat->global_default_fn_cipher_name,
736 mount_crypt_stat->global_default_fn_cipher_key_bytes);
737 if (s->cipher_code == 0) {
738 printk(KERN_WARNING "%s: Unable to generate code for "
Michael Halcrowa8f12862009-01-06 14:42:03 -0800739 "cipher [%s] with key bytes [%zd]\n", __func__,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800740 mount_crypt_stat->global_default_fn_cipher_name,
741 mount_crypt_stat->global_default_fn_cipher_key_bytes);
742 rc = -EINVAL;
743 goto out_free_unlock;
744 }
745 dest[s->i++] = s->cipher_code;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800746 /* TODO: Support other key modules than passphrase for
747 * filename encryption */
Tyler Hicksdf6ad332009-08-21 04:27:46 -0500748 if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
749 rc = -EOPNOTSUPP;
750 printk(KERN_INFO "%s: Filename encryption only supports "
751 "password tokens\n", __func__);
752 goto out_free_unlock;
753 }
Herbert Xu3095e8e2016-01-25 10:29:33 +0800754 s->hash_tfm = crypto_alloc_shash(ECRYPTFS_TAG_70_DIGEST, 0, 0);
755 if (IS_ERR(s->hash_tfm)) {
756 rc = PTR_ERR(s->hash_tfm);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800757 printk(KERN_ERR "%s: Error attempting to "
758 "allocate hash crypto context; rc = [%d]\n",
759 __func__, rc);
760 goto out_free_unlock;
761 }
Herbert Xu3095e8e2016-01-25 10:29:33 +0800762
763 s->hash_desc = kmalloc(sizeof(*s->hash_desc) +
764 crypto_shash_descsize(s->hash_tfm), GFP_KERNEL);
765 if (!s->hash_desc) {
766 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
767 "kmalloc [%zd] bytes\n", __func__,
768 sizeof(*s->hash_desc) +
769 crypto_shash_descsize(s->hash_tfm));
770 rc = -ENOMEM;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800771 goto out_release_free_unlock;
772 }
Herbert Xu3095e8e2016-01-25 10:29:33 +0800773
774 s->hash_desc->tfm = s->hash_tfm;
775 s->hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
776
777 rc = crypto_shash_digest(s->hash_desc,
778 (u8 *)s->auth_tok->token.password.session_key_encryption_key,
779 s->auth_tok->token.password.session_key_encryption_key_bytes,
780 s->hash);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800781 if (rc) {
782 printk(KERN_ERR
Herbert Xu3095e8e2016-01-25 10:29:33 +0800783 "%s: Error computing crypto hash; rc = [%d]\n",
Michael Halcrow9c79f342009-01-06 14:41:57 -0800784 __func__, rc);
785 goto out_release_free_unlock;
786 }
787 for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) {
788 s->block_aligned_filename[s->j] =
789 s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)];
790 if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)
791 == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) {
Herbert Xu3095e8e2016-01-25 10:29:33 +0800792 rc = crypto_shash_digest(s->hash_desc, (u8 *)s->hash,
793 ECRYPTFS_TAG_70_DIGEST_SIZE,
794 s->tmp_hash);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800795 if (rc) {
796 printk(KERN_ERR
Herbert Xu3095e8e2016-01-25 10:29:33 +0800797 "%s: Error computing crypto hash; "
Michael Halcrow9c79f342009-01-06 14:41:57 -0800798 "rc = [%d]\n", __func__, rc);
799 goto out_release_free_unlock;
800 }
801 memcpy(s->hash, s->tmp_hash,
802 ECRYPTFS_TAG_70_DIGEST_SIZE);
803 }
804 if (s->block_aligned_filename[s->j] == '\0')
805 s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL;
806 }
807 memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename,
808 filename_size);
809 rc = virt_to_scatterlist(s->block_aligned_filename,
Tyler Hicks8d08dab2011-05-17 00:50:33 -0500810 s->block_aligned_filename_size, s->src_sg, 2);
811 if (rc < 1) {
Michael Halcrow9c79f342009-01-06 14:41:57 -0800812 printk(KERN_ERR "%s: Internal error whilst attempting to "
Tyler Hicks8d08dab2011-05-17 00:50:33 -0500813 "convert filename memory to scatterlist; rc = [%d]. "
Michael Halcrowa8f12862009-01-06 14:42:03 -0800814 "block_aligned_filename_size = [%zd]\n", __func__, rc,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800815 s->block_aligned_filename_size);
816 goto out_release_free_unlock;
817 }
818 rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size,
Tyler Hicks8d08dab2011-05-17 00:50:33 -0500819 s->dst_sg, 2);
820 if (rc < 1) {
Michael Halcrow9c79f342009-01-06 14:41:57 -0800821 printk(KERN_ERR "%s: Internal error whilst attempting to "
822 "convert encrypted filename memory to scatterlist; "
Tyler Hicks8d08dab2011-05-17 00:50:33 -0500823 "rc = [%d]. block_aligned_filename_size = [%zd]\n",
824 __func__, rc, s->block_aligned_filename_size);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800825 goto out_release_free_unlock;
826 }
827 /* The characters in the first block effectively do the job
828 * of the IV here, so we just use 0's for the IV. Note the
829 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
830 * >= ECRYPTFS_MAX_IV_BYTES. */
Herbert Xu3095e8e2016-01-25 10:29:33 +0800831 rc = crypto_skcipher_setkey(
832 s->skcipher_tfm,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800833 s->auth_tok->token.password.session_key_encryption_key,
834 mount_crypt_stat->global_default_fn_cipher_key_bytes);
835 if (rc < 0) {
836 printk(KERN_ERR "%s: Error setting key for crypto context; "
837 "rc = [%d]. s->auth_tok->token.password.session_key_"
838 "encryption_key = [0x%p]; mount_crypt_stat->"
Michael Halcrowdf261c52009-01-06 14:42:02 -0800839 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800840 rc,
841 s->auth_tok->token.password.session_key_encryption_key,
842 mount_crypt_stat->global_default_fn_cipher_key_bytes);
843 goto out_release_free_unlock;
844 }
Herbert Xu3095e8e2016-01-25 10:29:33 +0800845 skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg,
846 s->block_aligned_filename_size, s->iv);
847 rc = crypto_skcipher_encrypt(s->skcipher_req);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800848 if (rc) {
849 printk(KERN_ERR "%s: Error attempting to encrypt filename; "
850 "rc = [%d]\n", __func__, rc);
851 goto out_release_free_unlock;
852 }
853 s->i += s->block_aligned_filename_size;
854 (*packet_size) = s->i;
855 (*remaining_bytes) -= (*packet_size);
856out_release_free_unlock:
Herbert Xu3095e8e2016-01-25 10:29:33 +0800857 crypto_free_shash(s->hash_tfm);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800858out_free_unlock:
Johannes Weiner00fcf2c2009-03-31 15:24:42 -0700859 kzfree(s->block_aligned_filename);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800860out_unlock:
861 mutex_unlock(s->tfm_mutex);
862out:
Roberto Sassub5695d02011-03-21 16:00:55 +0100863 if (auth_tok_key) {
864 up_write(&(auth_tok_key->sem));
Roberto Sassuaee683b2010-10-06 18:31:06 +0200865 key_put(auth_tok_key);
Roberto Sassub5695d02011-03-21 16:00:55 +0100866 }
Herbert Xu3095e8e2016-01-25 10:29:33 +0800867 skcipher_request_free(s->skcipher_req);
868 kzfree(s->hash_desc);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800869 kfree(s);
870 return rc;
871}
872
873struct ecryptfs_parse_tag_70_packet_silly_stack {
874 u8 cipher_code;
875 size_t max_packet_size;
876 size_t packet_size_len;
877 size_t parsed_tag_70_packet_size;
878 size_t block_aligned_filename_size;
879 size_t block_size;
880 size_t i;
881 struct mutex *tfm_mutex;
882 char *decrypted_filename;
883 struct ecryptfs_auth_tok *auth_tok;
Tyler Hicks8d08dab2011-05-17 00:50:33 -0500884 struct scatterlist src_sg[2];
885 struct scatterlist dst_sg[2];
Herbert Xu3095e8e2016-01-25 10:29:33 +0800886 struct crypto_skcipher *skcipher_tfm;
887 struct skcipher_request *skcipher_req;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800888 char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
889 char iv[ECRYPTFS_MAX_IV_BYTES];
Colin Ian King2a559a82015-02-23 11:34:10 +0000890 char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
Michael Halcrow9c79f342009-01-06 14:41:57 -0800891};
892
893/**
894 * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet
895 * @filename: This function kmalloc's the memory for the filename
Michael Halcrow7d8bc2b2009-01-06 14:42:04 -0800896 * @filename_size: This function sets this to the amount of memory
897 * kmalloc'd for the filename
898 * @packet_size: This function sets this to the the number of octets
899 * in the packet parsed
900 * @mount_crypt_stat: The mount-wide cryptographic context
901 * @data: The memory location containing the start of the tag 70
902 * packet
903 * @max_packet_size: The maximum legal size of the packet to be parsed
904 * from @data
905 *
906 * Returns zero on success; non-zero otherwise
Michael Halcrow9c79f342009-01-06 14:41:57 -0800907 */
908int
909ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
910 size_t *packet_size,
911 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
912 char *data, size_t max_packet_size)
913{
914 struct ecryptfs_parse_tag_70_packet_silly_stack *s;
Roberto Sassuaee683b2010-10-06 18:31:06 +0200915 struct key *auth_tok_key = NULL;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800916 int rc = 0;
917
918 (*packet_size) = 0;
919 (*filename_size) = 0;
920 (*filename) = NULL;
Herbert Xu3095e8e2016-01-25 10:29:33 +0800921 s = kzalloc(sizeof(*s), GFP_KERNEL);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800922 if (!s) {
923 printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
Michael Halcrowa8f12862009-01-06 14:42:03 -0800924 "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
Julia Lawallf137f152010-08-11 12:11:41 +0200925 rc = -ENOMEM;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800926 goto out;
927 }
Tyler Hicks4a266202011-11-05 13:45:08 -0400928 if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) {
Michael Halcrowdf261c52009-01-06 14:42:02 -0800929 printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
Michael Halcrow9c79f342009-01-06 14:41:57 -0800930 "at least [%d]\n", __func__, max_packet_size,
Tyler Hicks4a266202011-11-05 13:45:08 -0400931 ECRYPTFS_TAG_70_MIN_METADATA_SIZE);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800932 rc = -EINVAL;
933 goto out;
934 }
935 /* Octet 0: Tag 70 identifier
936 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
937 * and block-aligned encrypted filename size)
938 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
939 * Octet N2-N3: Cipher identifier (1 octet)
940 * Octets N3-N4: Block-aligned encrypted filename
941 * - Consists of a minimum number of random numbers, a \0
942 * separator, and then the filename */
943 if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) {
944 printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be "
945 "tag [0x%.2x]\n", __func__,
946 data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE);
947 rc = -EINVAL;
948 goto out;
949 }
950 rc = ecryptfs_parse_packet_length(&data[(*packet_size)],
951 &s->parsed_tag_70_packet_size,
952 &s->packet_size_len);
953 if (rc) {
954 printk(KERN_WARNING "%s: Error parsing packet length; "
955 "rc = [%d]\n", __func__, rc);
956 goto out;
957 }
958 s->block_aligned_filename_size = (s->parsed_tag_70_packet_size
959 - ECRYPTFS_SIG_SIZE - 1);
960 if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size)
961 > max_packet_size) {
Michael Halcrowa8f12862009-01-06 14:42:03 -0800962 printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet "
963 "size is [%zd]\n", __func__, max_packet_size,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800964 (1 + s->packet_size_len + 1
965 + s->block_aligned_filename_size));
966 rc = -EINVAL;
967 goto out;
968 }
969 (*packet_size) += s->packet_size_len;
970 ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)],
971 ECRYPTFS_SIG_SIZE);
972 s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
973 (*packet_size) += ECRYPTFS_SIG_SIZE;
974 s->cipher_code = data[(*packet_size)++];
975 rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
976 if (rc) {
977 printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n",
978 __func__, s->cipher_code);
979 goto out;
980 }
Roberto Sassu950983f2011-03-21 16:00:54 +0100981 rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
982 &s->auth_tok, mount_crypt_stat,
983 s->fnek_sig_hex);
984 if (rc) {
985 printk(KERN_ERR "%s: Error attempting to find auth tok for "
986 "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
987 rc);
988 goto out;
989 }
Herbert Xu3095e8e2016-01-25 10:29:33 +0800990 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm,
Michael Halcrow9c79f342009-01-06 14:41:57 -0800991 &s->tfm_mutex,
992 s->cipher_string);
993 if (unlikely(rc)) {
994 printk(KERN_ERR "Internal error whilst attempting to get "
995 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
996 s->cipher_string, rc);
997 goto out;
998 }
999 mutex_lock(s->tfm_mutex);
1000 rc = virt_to_scatterlist(&data[(*packet_size)],
Tyler Hicks8d08dab2011-05-17 00:50:33 -05001001 s->block_aligned_filename_size, s->src_sg, 2);
1002 if (rc < 1) {
Michael Halcrow9c79f342009-01-06 14:41:57 -08001003 printk(KERN_ERR "%s: Internal error whilst attempting to "
1004 "convert encrypted filename memory to scatterlist; "
Tyler Hicks8d08dab2011-05-17 00:50:33 -05001005 "rc = [%d]. block_aligned_filename_size = [%zd]\n",
1006 __func__, rc, s->block_aligned_filename_size);
Michael Halcrow9c79f342009-01-06 14:41:57 -08001007 goto out_unlock;
1008 }
1009 (*packet_size) += s->block_aligned_filename_size;
1010 s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
1011 GFP_KERNEL);
1012 if (!s->decrypted_filename) {
1013 printk(KERN_ERR "%s: Out of memory whilst attempting to "
Michael Halcrowa8f12862009-01-06 14:42:03 -08001014 "kmalloc [%zd] bytes\n", __func__,
Michael Halcrow9c79f342009-01-06 14:41:57 -08001015 s->block_aligned_filename_size);
1016 rc = -ENOMEM;
1017 goto out_unlock;
1018 }
1019 rc = virt_to_scatterlist(s->decrypted_filename,
Tyler Hicks8d08dab2011-05-17 00:50:33 -05001020 s->block_aligned_filename_size, s->dst_sg, 2);
1021 if (rc < 1) {
Michael Halcrow9c79f342009-01-06 14:41:57 -08001022 printk(KERN_ERR "%s: Internal error whilst attempting to "
1023 "convert decrypted filename memory to scatterlist; "
Tyler Hicks8d08dab2011-05-17 00:50:33 -05001024 "rc = [%d]. block_aligned_filename_size = [%zd]\n",
1025 __func__, rc, s->block_aligned_filename_size);
Michael Halcrow9c79f342009-01-06 14:41:57 -08001026 goto out_free_unlock;
1027 }
Herbert Xu3095e8e2016-01-25 10:29:33 +08001028
1029 s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL);
1030 if (!s->skcipher_req) {
1031 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
1032 "skcipher_request_alloc for %s\n", __func__,
1033 crypto_skcipher_driver_name(s->skcipher_tfm));
1034 rc = -ENOMEM;
1035 goto out_free_unlock;
1036 }
1037
1038 skcipher_request_set_callback(s->skcipher_req,
1039 CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
1040
Michael Halcrow9c79f342009-01-06 14:41:57 -08001041 /* The characters in the first block effectively do the job of
1042 * the IV here, so we just use 0's for the IV. Note the
1043 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
1044 * >= ECRYPTFS_MAX_IV_BYTES. */
Michael Halcrow9c79f342009-01-06 14:41:57 -08001045 /* TODO: Support other key modules than passphrase for
1046 * filename encryption */
Tyler Hicksdf6ad332009-08-21 04:27:46 -05001047 if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
1048 rc = -EOPNOTSUPP;
1049 printk(KERN_INFO "%s: Filename encryption only supports "
1050 "password tokens\n", __func__);
1051 goto out_free_unlock;
1052 }
Herbert Xu3095e8e2016-01-25 10:29:33 +08001053 rc = crypto_skcipher_setkey(
1054 s->skcipher_tfm,
Michael Halcrow9c79f342009-01-06 14:41:57 -08001055 s->auth_tok->token.password.session_key_encryption_key,
1056 mount_crypt_stat->global_default_fn_cipher_key_bytes);
1057 if (rc < 0) {
1058 printk(KERN_ERR "%s: Error setting key for crypto context; "
1059 "rc = [%d]. s->auth_tok->token.password.session_key_"
1060 "encryption_key = [0x%p]; mount_crypt_stat->"
Michael Halcrowdf261c52009-01-06 14:42:02 -08001061 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
Michael Halcrow9c79f342009-01-06 14:41:57 -08001062 rc,
1063 s->auth_tok->token.password.session_key_encryption_key,
1064 mount_crypt_stat->global_default_fn_cipher_key_bytes);
1065 goto out_free_unlock;
1066 }
Herbert Xu3095e8e2016-01-25 10:29:33 +08001067 skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg,
1068 s->block_aligned_filename_size, s->iv);
1069 rc = crypto_skcipher_decrypt(s->skcipher_req);
Michael Halcrow9c79f342009-01-06 14:41:57 -08001070 if (rc) {
1071 printk(KERN_ERR "%s: Error attempting to decrypt filename; "
1072 "rc = [%d]\n", __func__, rc);
1073 goto out_free_unlock;
1074 }
Michael Halcrow9c79f342009-01-06 14:41:57 -08001075 while (s->decrypted_filename[s->i] != '\0'
1076 && s->i < s->block_aligned_filename_size)
1077 s->i++;
1078 if (s->i == s->block_aligned_filename_size) {
1079 printk(KERN_WARNING "%s: Invalid tag 70 packet; could not "
1080 "find valid separator between random characters and "
1081 "the filename\n", __func__);
1082 rc = -EINVAL;
1083 goto out_free_unlock;
1084 }
1085 s->i++;
1086 (*filename_size) = (s->block_aligned_filename_size - s->i);
1087 if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) {
Michael Halcrowdf261c52009-01-06 14:42:02 -08001088 printk(KERN_WARNING "%s: Filename size is [%zd], which is "
Michael Halcrow9c79f342009-01-06 14:41:57 -08001089 "invalid\n", __func__, (*filename_size));
1090 rc = -EINVAL;
1091 goto out_free_unlock;
1092 }
1093 (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
1094 if (!(*filename)) {
1095 printk(KERN_ERR "%s: Out of memory whilst attempting to "
Michael Halcrowa8f12862009-01-06 14:42:03 -08001096 "kmalloc [%zd] bytes\n", __func__,
Michael Halcrow9c79f342009-01-06 14:41:57 -08001097 ((*filename_size) + 1));
1098 rc = -ENOMEM;
1099 goto out_free_unlock;
1100 }
1101 memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size));
1102 (*filename)[(*filename_size)] = '\0';
1103out_free_unlock:
1104 kfree(s->decrypted_filename);
1105out_unlock:
1106 mutex_unlock(s->tfm_mutex);
1107out:
1108 if (rc) {
1109 (*packet_size) = 0;
1110 (*filename_size) = 0;
1111 (*filename) = NULL;
1112 }
Roberto Sassub5695d02011-03-21 16:00:55 +01001113 if (auth_tok_key) {
1114 up_write(&(auth_tok_key->sem));
Roberto Sassuaee683b2010-10-06 18:31:06 +02001115 key_put(auth_tok_key);
Roberto Sassub5695d02011-03-21 16:00:55 +01001116 }
Herbert Xu3095e8e2016-01-25 10:29:33 +08001117 skcipher_request_free(s->skcipher_req);
Michael Halcrow9c79f342009-01-06 14:41:57 -08001118 kfree(s);
1119 return rc;
1120}
1121
1122static int
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001123ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
1124{
1125 int rc = 0;
1126
1127 (*sig) = NULL;
1128 switch (auth_tok->token_type) {
1129 case ECRYPTFS_PASSWORD:
1130 (*sig) = auth_tok->token.password.signature;
1131 break;
1132 case ECRYPTFS_PRIVATE_KEY:
1133 (*sig) = auth_tok->token.private_key.signature;
1134 break;
1135 default:
1136 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
1137 auth_tok->token_type);
1138 rc = -EINVAL;
1139 }
1140 return rc;
1141}
1142
Michael Halcrowdddfa462007-02-12 00:53:44 -08001143/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001144 * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
1145 * @auth_tok: The key authentication token used to decrypt the session key
1146 * @crypt_stat: The cryptographic context
Michael Halcrowdddfa462007-02-12 00:53:44 -08001147 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001148 * Returns zero on success; non-zero error otherwise.
Michael Halcrowdddfa462007-02-12 00:53:44 -08001149 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001150static int
1151decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1152 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrowdddfa462007-02-12 00:53:44 -08001153{
Trevor Highland19e66a62008-02-06 01:38:36 -08001154 u8 cipher_code = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001155 struct ecryptfs_msg_ctx *msg_ctx;
1156 struct ecryptfs_message *msg = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001157 char *auth_tok_sig;
Geyslan G. Bem3edc8372013-10-11 16:49:16 -03001158 char *payload = NULL;
Simon Quefa519962013-01-17 11:18:20 -08001159 size_t payload_len = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001160 int rc;
1161
Michael Halcrow5dda6992007-10-16 01:28:06 -07001162 rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
1163 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001164 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
1165 auth_tok->token_type);
1166 goto out;
1167 }
1168 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
Tyler Hicks624ae522008-10-15 22:02:51 -07001169 &payload, &payload_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001170 if (rc) {
Michael Halcrowf66e8832008-04-29 00:59:51 -07001171 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -08001172 goto out;
1173 }
Tyler Hicks624ae522008-10-15 22:02:51 -07001174 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001175 if (rc) {
Tyler Hicks624ae522008-10-15 22:02:51 -07001176 ecryptfs_printk(KERN_ERR, "Error sending message to "
Kees Cook290502b2013-02-28 00:39:37 -08001177 "ecryptfsd: %d\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001178 goto out;
1179 }
1180 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1181 if (rc) {
1182 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
1183 "from the user space daemon\n");
1184 rc = -EIO;
1185 goto out;
1186 }
1187 rc = parse_tag_65_packet(&(auth_tok->session_key),
1188 &cipher_code, msg);
1189 if (rc) {
1190 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
1191 rc);
1192 goto out;
1193 }
1194 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1195 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1196 auth_tok->session_key.decrypted_key_size);
1197 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
1198 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
1199 if (rc) {
1200 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
1201 cipher_code)
1202 goto out;
1203 }
1204 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1205 if (ecryptfs_verbosity > 0) {
1206 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
1207 ecryptfs_dump_hex(crypt_stat->key,
1208 crypt_stat->key_size);
1209 }
1210out:
Tim Gardner3a467412013-02-12 10:56:54 -07001211 kfree(msg);
Geyslan G. Bem3edc8372013-10-11 16:49:16 -03001212 kfree(payload);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001213 return rc;
1214}
1215
1216static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
1217{
Michael Halcrowdddfa462007-02-12 00:53:44 -08001218 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrowe0869cc2007-10-16 01:27:55 -07001219 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001220
Michael Halcrowe0869cc2007-10-16 01:27:55 -07001221 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
1222 auth_tok_list_head, list) {
1223 list_del(&auth_tok_list_item->list);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001224 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1225 auth_tok_list_item);
1226 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001227}
1228
1229struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
1230
Michael Halcrowdddfa462007-02-12 00:53:44 -08001231/**
1232 * parse_tag_1_packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001233 * @crypt_stat: The cryptographic context to modify based on packet contents
Michael Halcrowdddfa462007-02-12 00:53:44 -08001234 * @data: The raw bytes of the packet.
1235 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001236 * a new authentication token will be placed at the
1237 * end of this list for this packet.
Michael Halcrowdddfa462007-02-12 00:53:44 -08001238 * @new_auth_tok: Pointer to a pointer to memory that this function
1239 * allocates; sets the memory address of the pointer to
1240 * NULL on error. This object is added to the
1241 * auth_tok_list.
1242 * @packet_size: This function writes the size of the parsed packet
1243 * into this memory location; zero on error.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001244 * @max_packet_size: The maximum allowable packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -08001245 *
1246 * Returns zero on success; non-zero on error.
1247 */
1248static int
1249parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
1250 unsigned char *data, struct list_head *auth_tok_list,
1251 struct ecryptfs_auth_tok **new_auth_tok,
1252 size_t *packet_size, size_t max_packet_size)
1253{
1254 size_t body_size;
1255 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1256 size_t length_size;
1257 int rc = 0;
1258
1259 (*packet_size) = 0;
1260 (*new_auth_tok) = NULL;
Michael Halcrow132181792007-10-16 01:27:56 -07001261 /**
1262 * This format is inspired by OpenPGP; see RFC 2440
1263 * packet tag 1
1264 *
1265 * Tag 1 identifier (1 byte)
1266 * Max Tag 1 packet size (max 3 bytes)
1267 * Version (1 byte)
1268 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
1269 * Cipher identifier (1 byte)
1270 * Encrypted key size (arbitrary)
1271 *
1272 * 12 bytes minimum packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -08001273 */
Michael Halcrow132181792007-10-16 01:27:56 -07001274 if (unlikely(max_packet_size < 12)) {
1275 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -08001276 rc = -EINVAL;
1277 goto out;
1278 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001279 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
Michael Halcrow132181792007-10-16 01:27:56 -07001280 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
1281 ECRYPTFS_TAG_1_PACKET_TYPE);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001282 rc = -EINVAL;
1283 goto out;
1284 }
1285 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1286 * at end of function upon failure */
1287 auth_tok_list_item =
Michael Halcrow132181792007-10-16 01:27:56 -07001288 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
1289 GFP_KERNEL);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001290 if (!auth_tok_list_item) {
Michael Halcrow132181792007-10-16 01:27:56 -07001291 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -08001292 rc = -ENOMEM;
1293 goto out;
1294 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001295 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrowf66e8832008-04-29 00:59:51 -07001296 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
1297 &length_size);
Michael Halcrow5dda6992007-10-16 01:28:06 -07001298 if (rc) {
Michael Halcrow132181792007-10-16 01:27:56 -07001299 printk(KERN_WARNING "Error parsing packet length; "
1300 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001301 goto out_free;
1302 }
Michael Halcrow132181792007-10-16 01:27:56 -07001303 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07001304 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001305 rc = -EINVAL;
1306 goto out_free;
1307 }
1308 (*packet_size) += length_size;
1309 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrow132181792007-10-16 01:27:56 -07001310 printk(KERN_WARNING "Packet size exceeds max\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -08001311 rc = -EINVAL;
1312 goto out_free;
1313 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001314 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrow132181792007-10-16 01:27:56 -07001315 printk(KERN_WARNING "Unknown version number [%d]\n",
1316 data[(*packet_size) - 1]);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001317 rc = -EINVAL;
1318 goto out_free;
1319 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001320 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
1321 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
1322 *packet_size += ECRYPTFS_SIG_SIZE;
1323 /* This byte is skipped because the kernel does not need to
1324 * know which public key encryption algorithm was used */
1325 (*packet_size)++;
1326 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrow132181792007-10-16 01:27:56 -07001327 body_size - (ECRYPTFS_SIG_SIZE + 2);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001328 if ((*new_auth_tok)->session_key.encrypted_key_size
1329 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrow132181792007-10-16 01:27:56 -07001330 printk(KERN_WARNING "Tag 1 packet contains key larger "
1331 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
Michael Halcrowdddfa462007-02-12 00:53:44 -08001332 rc = -EINVAL;
1333 goto out;
1334 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001335 memcpy((*new_auth_tok)->session_key.encrypted_key,
Michael Halcrow132181792007-10-16 01:27:56 -07001336 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
Michael Halcrowdddfa462007-02-12 00:53:44 -08001337 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
1338 (*new_auth_tok)->session_key.flags &=
1339 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1340 (*new_auth_tok)->session_key.flags |=
1341 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
1342 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
Michael Halcrow132181792007-10-16 01:27:56 -07001343 (*new_auth_tok)->flags = 0;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001344 (*new_auth_tok)->session_key.flags &=
1345 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1346 (*new_auth_tok)->session_key.flags &=
1347 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001348 list_add(&auth_tok_list_item->list, auth_tok_list);
1349 goto out;
1350out_free:
1351 (*new_auth_tok) = NULL;
1352 memset(auth_tok_list_item, 0,
1353 sizeof(struct ecryptfs_auth_tok_list_item));
1354 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1355 auth_tok_list_item);
1356out:
1357 if (rc)
1358 (*packet_size) = 0;
1359 return rc;
1360}
1361
Michael Halcrow237fead2006-10-04 02:16:22 -07001362/**
1363 * parse_tag_3_packet
1364 * @crypt_stat: The cryptographic context to modify based on packet
1365 * contents.
1366 * @data: The raw bytes of the packet.
1367 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
1368 * a new authentication token will be placed at the end
1369 * of this list for this packet.
1370 * @new_auth_tok: Pointer to a pointer to memory that this function
1371 * allocates; sets the memory address of the pointer to
1372 * NULL on error. This object is added to the
1373 * auth_tok_list.
1374 * @packet_size: This function writes the size of the parsed packet
1375 * into this memory location; zero on error.
1376 * @max_packet_size: maximum number of bytes to parse
1377 *
1378 * Returns zero on success; non-zero on error.
1379 */
1380static int
1381parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
1382 unsigned char *data, struct list_head *auth_tok_list,
1383 struct ecryptfs_auth_tok **new_auth_tok,
1384 size_t *packet_size, size_t max_packet_size)
1385{
Michael Halcrow237fead2006-10-04 02:16:22 -07001386 size_t body_size;
1387 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1388 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001389 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001390
1391 (*packet_size) = 0;
1392 (*new_auth_tok) = NULL;
Michael Halcrowc59becf2007-10-16 01:27:56 -07001393 /**
1394 *This format is inspired by OpenPGP; see RFC 2440
1395 * packet tag 3
1396 *
1397 * Tag 3 identifier (1 byte)
1398 * Max Tag 3 packet size (max 3 bytes)
1399 * Version (1 byte)
1400 * Cipher code (1 byte)
1401 * S2K specifier (1 byte)
1402 * Hash identifier (1 byte)
1403 * Salt (ECRYPTFS_SALT_SIZE)
1404 * Hash iterations (1 byte)
1405 * Encrypted key (arbitrary)
1406 *
1407 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
Michael Halcrow237fead2006-10-04 02:16:22 -07001408 */
Michael Halcrowc59becf2007-10-16 01:27:56 -07001409 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
1410 printk(KERN_ERR "Max packet size too large\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001411 rc = -EINVAL;
1412 goto out;
1413 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001414 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
Michael Halcrowc59becf2007-10-16 01:27:56 -07001415 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
1416 ECRYPTFS_TAG_3_PACKET_TYPE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001417 rc = -EINVAL;
1418 goto out;
1419 }
1420 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
1421 * at end of function upon failure */
1422 auth_tok_list_item =
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001423 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -07001424 if (!auth_tok_list_item) {
Michael Halcrowc59becf2007-10-16 01:27:56 -07001425 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001426 rc = -ENOMEM;
1427 goto out;
1428 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001429 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrowf66e8832008-04-29 00:59:51 -07001430 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
1431 &length_size);
Michael Halcrow5dda6992007-10-16 01:28:06 -07001432 if (rc) {
Michael Halcrowc59becf2007-10-16 01:27:56 -07001433 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
1434 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001435 goto out_free;
1436 }
Michael Halcrowc59becf2007-10-16 01:27:56 -07001437 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07001438 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001439 rc = -EINVAL;
1440 goto out_free;
1441 }
1442 (*packet_size) += length_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001443 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -07001444 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001445 rc = -EINVAL;
1446 goto out_free;
1447 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001448 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrowc59becf2007-10-16 01:27:56 -07001449 (body_size - (ECRYPTFS_SALT_SIZE + 5));
Ramon de Carvalho Vallef151cd22009-07-28 13:58:22 -05001450 if ((*new_auth_tok)->session_key.encrypted_key_size
1451 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
1452 printk(KERN_WARNING "Tag 3 packet contains key larger "
1453 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n");
1454 rc = -EINVAL;
1455 goto out_free;
1456 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001457 if (unlikely(data[(*packet_size)++] != 0x04)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -07001458 printk(KERN_WARNING "Unknown version number [%d]\n",
1459 data[(*packet_size) - 1]);
Michael Halcrow237fead2006-10-04 02:16:22 -07001460 rc = -EINVAL;
1461 goto out_free;
1462 }
Tyler Hicksb0105ea2009-08-11 00:36:32 -05001463 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
1464 (u16)data[(*packet_size)]);
1465 if (rc)
1466 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001467 /* A little extra work to differentiate among the AES key
1468 * sizes; see RFC2440 */
1469 switch(data[(*packet_size)++]) {
1470 case RFC2440_CIPHER_AES_192:
1471 crypt_stat->key_size = 24;
1472 break;
1473 default:
1474 crypt_stat->key_size =
1475 (*new_auth_tok)->session_key.encrypted_key_size;
1476 }
Tyler Hicksb0105ea2009-08-11 00:36:32 -05001477 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1478 if (rc)
1479 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001480 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -07001481 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001482 rc = -ENOSYS;
1483 goto out_free;
1484 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001485 /* TODO: finish the hash mapping */
Michael Halcrow237fead2006-10-04 02:16:22 -07001486 switch (data[(*packet_size)++]) {
1487 case 0x01: /* See RFC2440 for these numbers and their mappings */
1488 /* Choose MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -07001489 memcpy((*new_auth_tok)->token.password.salt,
1490 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
1491 (*packet_size) += ECRYPTFS_SALT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001492 /* This conversion was taken straight from RFC2440 */
Michael Halcrow237fead2006-10-04 02:16:22 -07001493 (*new_auth_tok)->token.password.hash_iterations =
1494 ((u32) 16 + (data[(*packet_size)] & 15))
1495 << ((data[(*packet_size)] >> 4) + 6);
1496 (*packet_size)++;
Michael Halcrowc59becf2007-10-16 01:27:56 -07001497 /* Friendly reminder:
1498 * (*new_auth_tok)->session_key.encrypted_key_size =
1499 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
Michael Halcrow237fead2006-10-04 02:16:22 -07001500 memcpy((*new_auth_tok)->session_key.encrypted_key,
1501 &data[(*packet_size)],
1502 (*new_auth_tok)->session_key.encrypted_key_size);
1503 (*packet_size) +=
1504 (*new_auth_tok)->session_key.encrypted_key_size;
1505 (*new_auth_tok)->session_key.flags &=
1506 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1507 (*new_auth_tok)->session_key.flags |=
1508 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
Michael Halcrowc59becf2007-10-16 01:27:56 -07001509 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -07001510 break;
1511 default:
1512 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
1513 "[%d]\n", data[(*packet_size) - 1]);
1514 rc = -ENOSYS;
1515 goto out_free;
1516 }
1517 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
1518 /* TODO: Parametarize; we might actually want userspace to
1519 * decrypt the session key. */
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001520 (*new_auth_tok)->session_key.flags &=
1521 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
1522 (*new_auth_tok)->session_key.flags &=
1523 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -07001524 list_add(&auth_tok_list_item->list, auth_tok_list);
1525 goto out;
1526out_free:
1527 (*new_auth_tok) = NULL;
1528 memset(auth_tok_list_item, 0,
1529 sizeof(struct ecryptfs_auth_tok_list_item));
1530 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
1531 auth_tok_list_item);
1532out:
1533 if (rc)
1534 (*packet_size) = 0;
1535 return rc;
1536}
1537
1538/**
1539 * parse_tag_11_packet
1540 * @data: The raw bytes of the packet
1541 * @contents: This function writes the data contents of the literal
1542 * packet into this memory location
1543 * @max_contents_bytes: The maximum number of bytes that this function
1544 * is allowed to write into contents
1545 * @tag_11_contents_size: This function writes the size of the parsed
1546 * contents into this memory location; zero on
1547 * error
1548 * @packet_size: This function writes the size of the parsed packet
1549 * into this memory location; zero on error
1550 * @max_packet_size: maximum number of bytes to parse
1551 *
1552 * Returns zero on success; non-zero on error.
1553 */
1554static int
1555parse_tag_11_packet(unsigned char *data, unsigned char *contents,
1556 size_t max_contents_bytes, size_t *tag_11_contents_size,
1557 size_t *packet_size, size_t max_packet_size)
1558{
Michael Halcrow237fead2006-10-04 02:16:22 -07001559 size_t body_size;
1560 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001561 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001562
1563 (*packet_size) = 0;
1564 (*tag_11_contents_size) = 0;
Michael Halcrowf6481042007-10-16 01:27:57 -07001565 /* This format is inspired by OpenPGP; see RFC 2440
1566 * packet tag 11
1567 *
1568 * Tag 11 identifier (1 byte)
1569 * Max Tag 11 packet size (max 3 bytes)
1570 * Binary format specifier (1 byte)
1571 * Filename length (1 byte)
1572 * Filename ("_CONSOLE") (8 bytes)
1573 * Modification date (4 bytes)
1574 * Literal data (arbitrary)
1575 *
1576 * We need at least 16 bytes of data for the packet to even be
1577 * valid.
Michael Halcrow237fead2006-10-04 02:16:22 -07001578 */
Michael Halcrowf6481042007-10-16 01:27:57 -07001579 if (max_packet_size < 16) {
1580 printk(KERN_ERR "Maximum packet size too small\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001581 rc = -EINVAL;
1582 goto out;
1583 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001584 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
Michael Halcrowf6481042007-10-16 01:27:57 -07001585 printk(KERN_WARNING "Invalid tag 11 packet format\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001586 rc = -EINVAL;
1587 goto out;
1588 }
Michael Halcrowf66e8832008-04-29 00:59:51 -07001589 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
1590 &length_size);
Michael Halcrow5dda6992007-10-16 01:28:06 -07001591 if (rc) {
Michael Halcrowf6481042007-10-16 01:27:57 -07001592 printk(KERN_WARNING "Invalid tag 11 packet format\n");
1593 goto out;
1594 }
1595 if (body_size < 14) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07001596 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowf6481042007-10-16 01:27:57 -07001597 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001598 goto out;
1599 }
1600 (*packet_size) += length_size;
Michael Halcrowf6481042007-10-16 01:27:57 -07001601 (*tag_11_contents_size) = (body_size - 14);
Michael Halcrow237fead2006-10-04 02:16:22 -07001602 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
Michael Halcrowf6481042007-10-16 01:27:57 -07001603 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001604 rc = -EINVAL;
1605 goto out;
1606 }
Tyler Hicks6352a292009-07-28 13:57:01 -05001607 if (unlikely((*tag_11_contents_size) > max_contents_bytes)) {
1608 printk(KERN_ERR "Literal data section in tag 11 packet exceeds "
1609 "expected size\n");
1610 rc = -EINVAL;
1611 goto out;
1612 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001613 if (data[(*packet_size)++] != 0x62) {
Michael Halcrowf6481042007-10-16 01:27:57 -07001614 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001615 rc = -EINVAL;
1616 goto out;
1617 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001618 if (data[(*packet_size)++] != 0x08) {
Michael Halcrowf6481042007-10-16 01:27:57 -07001619 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001620 rc = -EINVAL;
1621 goto out;
1622 }
Michael Halcrowf6481042007-10-16 01:27:57 -07001623 (*packet_size) += 12; /* Ignore filename and modification date */
Michael Halcrow237fead2006-10-04 02:16:22 -07001624 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
1625 (*packet_size) += (*tag_11_contents_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001626out:
1627 if (rc) {
1628 (*packet_size) = 0;
1629 (*tag_11_contents_size) = 0;
1630 }
1631 return rc;
1632}
1633
Michael Halcrowf4aad162007-10-16 01:27:53 -07001634int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
1635 struct ecryptfs_auth_tok **auth_tok,
1636 char *sig)
1637{
1638 int rc = 0;
1639
1640 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
1641 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
Roberto Sassu1252cc32011-06-27 13:45:45 +02001642 (*auth_tok_key) = ecryptfs_get_encrypted_key(sig);
1643 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
1644 printk(KERN_ERR "Could not find key with description: [%s]\n",
1645 sig);
1646 rc = process_request_key_err(PTR_ERR(*auth_tok_key));
1647 (*auth_tok_key) = NULL;
1648 goto out;
1649 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001650 }
Roberto Sassub5695d02011-03-21 16:00:55 +01001651 down_write(&(*auth_tok_key)->sem);
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +01001652 rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok);
Roberto Sassuaee683b2010-10-06 18:31:06 +02001653 if (rc) {
Roberto Sassub5695d02011-03-21 16:00:55 +01001654 up_write(&(*auth_tok_key)->sem);
Roberto Sassuaee683b2010-10-06 18:31:06 +02001655 key_put(*auth_tok_key);
1656 (*auth_tok_key) = NULL;
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +01001657 goto out;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001658 }
1659out:
1660 return rc;
1661}
1662
1663/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001664 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
1665 * @auth_tok: The passphrase authentication token to use to encrypt the FEK
1666 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001667 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001668 * Returns zero on success; non-zero error otherwise
Michael Halcrow237fead2006-10-04 02:16:22 -07001669 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001670static int
1671decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1672 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -07001673{
Michael Halcrowac97b9f2008-11-19 15:36:28 -08001674 struct scatterlist dst_sg[2];
1675 struct scatterlist src_sg[2];
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001676 struct mutex *tfm_mutex;
Herbert Xu3095e8e2016-01-25 10:29:33 +08001677 struct crypto_skcipher *tfm;
1678 struct skcipher_request *req = NULL;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001679 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001680
Michael Halcrowf4aad162007-10-16 01:27:53 -07001681 if (unlikely(ecryptfs_verbosity > 0)) {
1682 ecryptfs_printk(
1683 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1684 auth_tok->token.password.session_key_encryption_key_bytes);
1685 ecryptfs_dump_hex(
1686 auth_tok->token.password.session_key_encryption_key,
1687 auth_tok->token.password.session_key_encryption_key_bytes);
Michael Halcrow237fead2006-10-04 02:16:22 -07001688 }
Herbert Xu3095e8e2016-01-25 10:29:33 +08001689 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001690 crypt_stat->cipher);
1691 if (unlikely(rc)) {
1692 printk(KERN_ERR "Internal error whilst attempting to get "
1693 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1694 crypt_stat->cipher, rc);
1695 goto out;
1696 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001697 rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1698 auth_tok->session_key.encrypted_key_size,
Michael Halcrowac97b9f2008-11-19 15:36:28 -08001699 src_sg, 2);
1700 if (rc < 1 || rc > 2) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001701 printk(KERN_ERR "Internal error whilst attempting to convert "
1702 "auth_tok->session_key.encrypted_key to scatterlist; "
1703 "expected rc = 1; got rc = [%d]. "
1704 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1705 auth_tok->session_key.encrypted_key_size);
1706 goto out;
1707 }
1708 auth_tok->session_key.decrypted_key_size =
1709 auth_tok->session_key.encrypted_key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001710 rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1711 auth_tok->session_key.decrypted_key_size,
Michael Halcrowac97b9f2008-11-19 15:36:28 -08001712 dst_sg, 2);
1713 if (rc < 1 || rc > 2) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001714 printk(KERN_ERR "Internal error whilst attempting to convert "
1715 "auth_tok->session_key.decrypted_key to scatterlist; "
1716 "expected rc = 1; got rc = [%d]\n", rc);
1717 goto out;
1718 }
1719 mutex_lock(tfm_mutex);
Herbert Xu3095e8e2016-01-25 10:29:33 +08001720 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1721 if (!req) {
1722 mutex_unlock(tfm_mutex);
1723 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
1724 "skcipher_request_alloc for %s\n", __func__,
1725 crypto_skcipher_driver_name(tfm));
1726 rc = -ENOMEM;
1727 goto out;
1728 }
1729
1730 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
1731 NULL, NULL);
1732 rc = crypto_skcipher_setkey(
1733 tfm, auth_tok->token.password.session_key_encryption_key,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001734 crypt_stat->key_size);
1735 if (unlikely(rc < 0)) {
1736 mutex_unlock(tfm_mutex);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001737 printk(KERN_ERR "Error setting key for crypto context\n");
1738 rc = -EINVAL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001739 goto out;
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001740 }
Herbert Xu3095e8e2016-01-25 10:29:33 +08001741 skcipher_request_set_crypt(req, src_sg, dst_sg,
1742 auth_tok->session_key.encrypted_key_size,
1743 NULL);
1744 rc = crypto_skcipher_decrypt(req);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001745 mutex_unlock(tfm_mutex);
1746 if (unlikely(rc)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -08001747 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001748 goto out;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001749 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001750 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1751 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1752 auth_tok->session_key.decrypted_key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001753 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001754 if (unlikely(ecryptfs_verbosity > 0)) {
Tyler Hicksf24b3882010-11-15 17:36:38 -06001755 ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n",
Michael Halcrowf4aad162007-10-16 01:27:53 -07001756 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001757 ecryptfs_dump_hex(crypt_stat->key,
1758 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001759 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001760out:
Herbert Xu3095e8e2016-01-25 10:29:33 +08001761 skcipher_request_free(req);
Michael Halcrow237fead2006-10-04 02:16:22 -07001762 return rc;
1763}
1764
1765/**
1766 * ecryptfs_parse_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001767 * @crypt_stat: The cryptographic context
1768 * @src: Virtual address of region of memory containing the packets
1769 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
Michael Halcrow237fead2006-10-04 02:16:22 -07001770 *
1771 * Get crypt_stat to have the file's session key if the requisite key
1772 * is available to decrypt the session key.
1773 *
1774 * Returns Zero if a valid authentication token was retrieved and
1775 * processed; negative value for file not encrypted or for error
1776 * conditions.
1777 */
1778int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1779 unsigned char *src,
1780 struct dentry *ecryptfs_dentry)
1781{
1782 size_t i = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001783 size_t found_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001784 size_t next_packet_is_auth_tok_packet;
Michael Halcrow237fead2006-10-04 02:16:22 -07001785 struct list_head auth_tok_list;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001786 struct ecryptfs_auth_tok *matching_auth_tok;
1787 struct ecryptfs_auth_tok *candidate_auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001788 char *candidate_auth_tok_sig;
Michael Halcrow237fead2006-10-04 02:16:22 -07001789 size_t packet_size;
1790 struct ecryptfs_auth_tok *new_auth_tok;
1791 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001792 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrow237fead2006-10-04 02:16:22 -07001793 size_t tag_11_contents_size;
1794 size_t tag_11_packet_size;
Roberto Sassuaee683b2010-10-06 18:31:06 +02001795 struct key *auth_tok_key = NULL;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001796 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001797
1798 INIT_LIST_HEAD(&auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001799 /* Parse the header to find as many packets as we can; these will be
Michael Halcrow237fead2006-10-04 02:16:22 -07001800 * added the our &auth_tok_list */
1801 next_packet_is_auth_tok_packet = 1;
1802 while (next_packet_is_auth_tok_packet) {
1803 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1804
1805 switch (src[i]) {
1806 case ECRYPTFS_TAG_3_PACKET_TYPE:
1807 rc = parse_tag_3_packet(crypt_stat,
1808 (unsigned char *)&src[i],
1809 &auth_tok_list, &new_auth_tok,
1810 &packet_size, max_packet_size);
1811 if (rc) {
1812 ecryptfs_printk(KERN_ERR, "Error parsing "
1813 "tag 3 packet\n");
1814 rc = -EIO;
1815 goto out_wipe_list;
1816 }
1817 i += packet_size;
1818 rc = parse_tag_11_packet((unsigned char *)&src[i],
1819 sig_tmp_space,
1820 ECRYPTFS_SIG_SIZE,
1821 &tag_11_contents_size,
1822 &tag_11_packet_size,
1823 max_packet_size);
1824 if (rc) {
1825 ecryptfs_printk(KERN_ERR, "No valid "
1826 "(ecryptfs-specific) literal "
1827 "packet containing "
1828 "authentication token "
1829 "signature found after "
1830 "tag 3 packet\n");
1831 rc = -EIO;
1832 goto out_wipe_list;
1833 }
1834 i += tag_11_packet_size;
1835 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1836 ecryptfs_printk(KERN_ERR, "Expected "
1837 "signature of size [%d]; "
Tyler Hicksf24b3882010-11-15 17:36:38 -06001838 "read size [%zd]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -07001839 ECRYPTFS_SIG_SIZE,
1840 tag_11_contents_size);
1841 rc = -EIO;
1842 goto out_wipe_list;
1843 }
1844 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1845 sig_tmp_space, tag_11_contents_size);
1846 new_auth_tok->token.password.signature[
1847 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001848 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrow237fead2006-10-04 02:16:22 -07001849 break;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001850 case ECRYPTFS_TAG_1_PACKET_TYPE:
1851 rc = parse_tag_1_packet(crypt_stat,
1852 (unsigned char *)&src[i],
1853 &auth_tok_list, &new_auth_tok,
1854 &packet_size, max_packet_size);
1855 if (rc) {
1856 ecryptfs_printk(KERN_ERR, "Error parsing "
1857 "tag 1 packet\n");
1858 rc = -EIO;
1859 goto out_wipe_list;
1860 }
1861 i += packet_size;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001862 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001863 break;
Michael Halcrow237fead2006-10-04 02:16:22 -07001864 case ECRYPTFS_TAG_11_PACKET_TYPE:
1865 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1866 "(Tag 11 not allowed by itself)\n");
1867 rc = -EIO;
1868 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001869 default:
Tyler Hicksf24b3882010-11-15 17:36:38 -06001870 ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] "
1871 "of the file header; hex value of "
Michael Halcrow237fead2006-10-04 02:16:22 -07001872 "character is [0x%.2x]\n", i, src[i]);
1873 next_packet_is_auth_tok_packet = 0;
1874 }
1875 }
1876 if (list_empty(&auth_tok_list)) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001877 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1878 "eCryptfs file; this is not supported in this version "
1879 "of the eCryptfs kernel module\n");
1880 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001881 goto out;
1882 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001883 /* auth_tok_list contains the set of authentication tokens
1884 * parsed from the metadata. We need to find a matching
1885 * authentication token that has the secret component(s)
1886 * necessary to decrypt the EFEK in the auth_tok parsed from
1887 * the metadata. There may be several potential matches, but
1888 * just one will be sufficient to decrypt to get the FEK. */
1889find_next_matching_auth_tok:
1890 found_auth_tok = 0;
1891 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001892 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1893 if (unlikely(ecryptfs_verbosity > 0)) {
1894 ecryptfs_printk(KERN_DEBUG,
1895 "Considering cadidate auth tok:\n");
1896 ecryptfs_dump_auth_tok(candidate_auth_tok);
1897 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001898 rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1899 candidate_auth_tok);
1900 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001901 printk(KERN_ERR
1902 "Unrecognized candidate auth tok type: [%d]\n",
1903 candidate_auth_tok->token_type);
1904 rc = -EINVAL;
1905 goto out_wipe_list;
1906 }
Roberto Sassu39fac852010-10-06 18:31:15 +02001907 rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
Roberto Sassuaee683b2010-10-06 18:31:06 +02001908 &matching_auth_tok,
Michael Halcrow9c79f342009-01-06 14:41:57 -08001909 crypt_stat->mount_crypt_stat,
Michael Halcrow5dda6992007-10-16 01:28:06 -07001910 candidate_auth_tok_sig);
Roberto Sassu39fac852010-10-06 18:31:15 +02001911 if (!rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001912 found_auth_tok = 1;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001913 goto found_matching_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001914 }
1915 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001916 if (!found_auth_tok) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001917 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1918 "authentication token\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001919 rc = -EIO;
1920 goto out_wipe_list;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001921 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001922found_matching_auth_tok:
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001923 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Michael Halcrowdddfa462007-02-12 00:53:44 -08001924 memcpy(&(candidate_auth_tok->token.private_key),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001925 &(matching_auth_tok->token.private_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001926 sizeof(struct ecryptfs_private_key));
Tyler Hicksb2987a52011-07-26 19:47:08 -05001927 up_write(&(auth_tok_key->sem));
1928 key_put(auth_tok_key);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001929 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001930 crypt_stat);
1931 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001932 memcpy(&(candidate_auth_tok->token.password),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001933 &(matching_auth_tok->token.password),
Michael Halcrow237fead2006-10-04 02:16:22 -07001934 sizeof(struct ecryptfs_password));
Tyler Hicksb2987a52011-07-26 19:47:08 -05001935 up_write(&(auth_tok_key->sem));
1936 key_put(auth_tok_key);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001937 rc = decrypt_passphrase_encrypted_session_key(
1938 candidate_auth_tok, crypt_stat);
Tyler Hicksb2987a52011-07-26 19:47:08 -05001939 } else {
1940 up_write(&(auth_tok_key->sem));
1941 key_put(auth_tok_key);
1942 rc = -EINVAL;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001943 }
1944 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001945 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1946
1947 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1948 "session key for authentication token with sig "
1949 "[%.*s]; rc = [%d]. Removing auth tok "
1950 "candidate from the list and searching for "
Joe Perches888d57b2010-11-10 15:46:16 -08001951 "the next match.\n", ECRYPTFS_SIG_SIZE_HEX,
1952 candidate_auth_tok_sig, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001953 list_for_each_entry_safe(auth_tok_list_item,
1954 auth_tok_list_item_tmp,
1955 &auth_tok_list, list) {
1956 if (candidate_auth_tok
1957 == &auth_tok_list_item->auth_tok) {
1958 list_del(&auth_tok_list_item->list);
1959 kmem_cache_free(
1960 ecryptfs_auth_tok_list_item_cache,
1961 auth_tok_list_item);
1962 goto find_next_matching_auth_tok;
1963 }
1964 }
1965 BUG();
Michael Halcrowdddfa462007-02-12 00:53:44 -08001966 }
1967 rc = ecryptfs_compute_root_iv(crypt_stat);
1968 if (rc) {
1969 ecryptfs_printk(KERN_ERR, "Error computing "
1970 "the root IV\n");
1971 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001972 }
1973 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1974 if (rc) {
1975 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1976 "context for cipher [%s]; rc = [%d]\n",
1977 crypt_stat->cipher, rc);
1978 }
1979out_wipe_list:
1980 wipe_auth_tok_list(&auth_tok_list);
1981out:
1982 return rc;
1983}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001984
Michael Halcrowdddfa462007-02-12 00:53:44 -08001985static int
Tyler Hicksb2987a52011-07-26 19:47:08 -05001986pki_encrypt_session_key(struct key *auth_tok_key,
1987 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001988 struct ecryptfs_crypt_stat *crypt_stat,
1989 struct ecryptfs_key_record *key_rec)
1990{
1991 struct ecryptfs_msg_ctx *msg_ctx = NULL;
Tyler Hicks624ae522008-10-15 22:02:51 -07001992 char *payload = NULL;
Tyler Hicks99b373f2011-08-05 04:15:19 -05001993 size_t payload_len = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001994 struct ecryptfs_message *msg;
1995 int rc;
1996
1997 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
Michael Halcrow9c79f342009-01-06 14:41:57 -08001998 ecryptfs_code_for_cipher_string(
1999 crypt_stat->cipher,
2000 crypt_stat->key_size),
Tyler Hicks624ae522008-10-15 22:02:51 -07002001 crypt_stat, &payload, &payload_len);
Tyler Hicksb2987a52011-07-26 19:47:08 -05002002 up_write(&(auth_tok_key->sem));
2003 key_put(auth_tok_key);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002004 if (rc) {
2005 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
2006 goto out;
2007 }
Tyler Hicks624ae522008-10-15 22:02:51 -07002008 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002009 if (rc) {
Tyler Hicks624ae522008-10-15 22:02:51 -07002010 ecryptfs_printk(KERN_ERR, "Error sending message to "
Kees Cook290502b2013-02-28 00:39:37 -08002011 "ecryptfsd: %d\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002012 goto out;
2013 }
2014 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
2015 if (rc) {
2016 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
2017 "from the user space daemon\n");
2018 rc = -EIO;
2019 goto out;
2020 }
2021 rc = parse_tag_67_packet(key_rec, msg);
2022 if (rc)
2023 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
2024 kfree(msg);
2025out:
Tyler Hicks624ae522008-10-15 22:02:51 -07002026 kfree(payload);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002027 return rc;
2028}
2029/**
2030 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
2031 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07002032 * @remaining_bytes: Maximum number of bytes that can be writtn
Tyler Hicksb2987a52011-07-26 19:47:08 -05002033 * @auth_tok_key: The authentication token key to unlock and put when done with
2034 * @auth_tok
Michael Halcrow22e78fa2007-10-16 01:28:02 -07002035 * @auth_tok: The authentication token used for generating the tag 1 packet
2036 * @crypt_stat: The cryptographic context
2037 * @key_rec: The key record struct for the tag 1 packet
Michael Halcrowdddfa462007-02-12 00:53:44 -08002038 * @packet_size: This function will write the number of bytes that end
2039 * up constituting the packet; set to zero on error
2040 *
2041 * Returns zero on success; non-zero on error.
2042 */
2043static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07002044write_tag_1_packet(char *dest, size_t *remaining_bytes,
Tyler Hicksb2987a52011-07-26 19:47:08 -05002045 struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08002046 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdddfa462007-02-12 00:53:44 -08002047 struct ecryptfs_key_record *key_rec, size_t *packet_size)
2048{
2049 size_t i;
2050 size_t encrypted_session_key_valid = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002051 size_t packet_size_length;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002052 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002053 int rc = 0;
2054
2055 (*packet_size) = 0;
2056 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
2057 ECRYPTFS_SIG_SIZE);
2058 encrypted_session_key_valid = 0;
2059 for (i = 0; i < crypt_stat->key_size; i++)
2060 encrypted_session_key_valid |=
2061 auth_tok->session_key.encrypted_key[i];
2062 if (encrypted_session_key_valid) {
2063 memcpy(key_rec->enc_key,
2064 auth_tok->session_key.encrypted_key,
2065 auth_tok->session_key.encrypted_key_size);
Tyler Hicksb2987a52011-07-26 19:47:08 -05002066 up_write(&(auth_tok_key->sem));
2067 key_put(auth_tok_key);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002068 goto encrypted_session_key_set;
2069 }
2070 if (auth_tok->session_key.encrypted_key_size == 0)
2071 auth_tok->session_key.encrypted_key_size =
2072 auth_tok->token.private_key.key_size;
Tyler Hicksb2987a52011-07-26 19:47:08 -05002073 rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat,
2074 key_rec);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002075 if (rc) {
Michael Halcrowf66e8832008-04-29 00:59:51 -07002076 printk(KERN_ERR "Failed to encrypt session key via a key "
2077 "module; rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002078 goto out;
2079 }
2080 if (ecryptfs_verbosity > 0) {
2081 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
2082 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
2083 }
2084encrypted_session_key_set:
Michael Halcrowf4aad162007-10-16 01:27:53 -07002085 /* This format is inspired by OpenPGP; see RFC 2440
2086 * packet tag 1 */
2087 max_packet_size = (1 /* Tag 1 identifier */
2088 + 3 /* Max Tag 1 packet size */
2089 + 1 /* Version */
2090 + ECRYPTFS_SIG_SIZE /* Key identifier */
2091 + 1 /* Cipher identifier */
2092 + key_rec->enc_key_size); /* Encrypted key size */
2093 if (max_packet_size > (*remaining_bytes)) {
2094 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07002095 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrowf4aad162007-10-16 01:27:53 -07002096 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrowdddfa462007-02-12 00:53:44 -08002097 rc = -EINVAL;
2098 goto out;
2099 }
2100 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -07002101 rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
2102 (max_packet_size - 4),
2103 &packet_size_length);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002104 if (rc) {
2105 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
2106 "header; cannot generate packet length\n");
2107 goto out;
2108 }
2109 (*packet_size) += packet_size_length;
2110 dest[(*packet_size)++] = 0x03; /* version 3 */
2111 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
2112 (*packet_size) += ECRYPTFS_SIG_SIZE;
2113 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
2114 memcpy(&dest[(*packet_size)], key_rec->enc_key,
2115 key_rec->enc_key_size);
2116 (*packet_size) += key_rec->enc_key_size;
2117out:
2118 if (rc)
2119 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002120 else
2121 (*remaining_bytes) -= (*packet_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002122 return rc;
2123}
Michael Halcrow237fead2006-10-04 02:16:22 -07002124
2125/**
2126 * write_tag_11_packet
2127 * @dest: Target into which Tag 11 packet is to be written
Michael Halcrow22e78fa2007-10-16 01:28:02 -07002128 * @remaining_bytes: Maximum packet length
Michael Halcrow237fead2006-10-04 02:16:22 -07002129 * @contents: Byte array of contents to copy in
2130 * @contents_length: Number of bytes in contents
2131 * @packet_length: Length of the Tag 11 packet written; zero on error
2132 *
2133 * Returns zero on success; non-zero on error.
2134 */
2135static int
Andrew Morton81acbcd2007-10-16 01:27:59 -07002136write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
Michael Halcrow146a4602007-10-16 01:27:58 -07002137 size_t contents_length, size_t *packet_length)
Michael Halcrow237fead2006-10-04 02:16:22 -07002138{
Michael Halcrow237fead2006-10-04 02:16:22 -07002139 size_t packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07002140 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002141 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07002142
2143 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07002144 /* This format is inspired by OpenPGP; see RFC 2440
2145 * packet tag 11 */
2146 max_packet_size = (1 /* Tag 11 identifier */
2147 + 3 /* Max Tag 11 packet size */
2148 + 1 /* Binary format specifier */
2149 + 1 /* Filename length */
2150 + 8 /* Filename ("_CONSOLE") */
2151 + 4 /* Modification date */
2152 + contents_length); /* Literal data */
2153 if (max_packet_size > (*remaining_bytes)) {
2154 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07002155 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrow146a4602007-10-16 01:27:58 -07002156 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrow237fead2006-10-04 02:16:22 -07002157 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07002158 goto out;
2159 }
Michael Halcrow237fead2006-10-04 02:16:22 -07002160 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -07002161 rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
2162 (max_packet_size - 4),
2163 &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07002164 if (rc) {
Michael Halcrow146a4602007-10-16 01:27:58 -07002165 printk(KERN_ERR "Error generating tag 11 packet header; cannot "
2166 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07002167 goto out;
2168 }
2169 (*packet_length) += packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07002170 dest[(*packet_length)++] = 0x62; /* binary data format specifier */
Michael Halcrow237fead2006-10-04 02:16:22 -07002171 dest[(*packet_length)++] = 8;
2172 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
2173 (*packet_length) += 8;
Michael Halcrow237fead2006-10-04 02:16:22 -07002174 memset(&dest[(*packet_length)], 0x00, 4);
2175 (*packet_length) += 4;
Michael Halcrow237fead2006-10-04 02:16:22 -07002176 memcpy(&dest[(*packet_length)], contents, contents_length);
2177 (*packet_length) += contents_length;
2178 out:
2179 if (rc)
2180 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07002181 else
2182 (*remaining_bytes) -= (*packet_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07002183 return rc;
2184}
2185
2186/**
2187 * write_tag_3_packet
2188 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07002189 * @remaining_bytes: Maximum number of bytes that can be written
Michael Halcrow237fead2006-10-04 02:16:22 -07002190 * @auth_tok: Authentication token
2191 * @crypt_stat: The cryptographic context
2192 * @key_rec: encrypted key
2193 * @packet_size: This function will write the number of bytes that end
2194 * up constituting the packet; set to zero on error
2195 *
2196 * Returns zero on success; non-zero on error.
2197 */
2198static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07002199write_tag_3_packet(char *dest, size_t *remaining_bytes,
2200 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrow237fead2006-10-04 02:16:22 -07002201 struct ecryptfs_crypt_stat *crypt_stat,
2202 struct ecryptfs_key_record *key_rec, size_t *packet_size)
2203{
Michael Halcrow237fead2006-10-04 02:16:22 -07002204 size_t i;
Michael Halcrow237fead2006-10-04 02:16:22 -07002205 size_t encrypted_session_key_valid = 0;
2206 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrowac97b9f2008-11-19 15:36:28 -08002207 struct scatterlist dst_sg[2];
2208 struct scatterlist src_sg[2];
Michael Halcrow237fead2006-10-04 02:16:22 -07002209 struct mutex *tfm_mutex = NULL;
Trevor Highland19e66a62008-02-06 01:38:36 -08002210 u8 cipher_code;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002211 size_t packet_size_length;
2212 size_t max_packet_size;
2213 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2214 crypt_stat->mount_crypt_stat;
Herbert Xu3095e8e2016-01-25 10:29:33 +08002215 struct crypto_skcipher *tfm;
2216 struct skcipher_request *req;
Michael Halcrow8bba0662006-10-30 22:07:18 -08002217 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07002218
2219 (*packet_size) = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002220 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
Michael Halcrow237fead2006-10-04 02:16:22 -07002221 ECRYPTFS_SIG_SIZE);
Herbert Xu3095e8e2016-01-25 10:29:33 +08002222 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
Michael Halcrowf4aad162007-10-16 01:27:53 -07002223 crypt_stat->cipher);
2224 if (unlikely(rc)) {
2225 printk(KERN_ERR "Internal error whilst attempting to get "
2226 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
2227 crypt_stat->cipher, rc);
2228 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -07002229 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07002230 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07002231 printk(KERN_WARNING "No key size specified at mount; "
Herbert Xu3095e8e2016-01-25 10:29:33 +08002232 "defaulting to [%d]\n",
2233 crypto_skcipher_default_keysize(tfm));
Michael Halcrowf4aad162007-10-16 01:27:53 -07002234 mount_crypt_stat->global_default_cipher_key_size =
Herbert Xu3095e8e2016-01-25 10:29:33 +08002235 crypto_skcipher_default_keysize(tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -07002236 }
2237 if (crypt_stat->key_size == 0)
2238 crypt_stat->key_size =
2239 mount_crypt_stat->global_default_cipher_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07002240 if (auth_tok->session_key.encrypted_key_size == 0)
2241 auth_tok->session_key.encrypted_key_size =
2242 crypt_stat->key_size;
2243 if (crypt_stat->key_size == 24
2244 && strcmp("aes", crypt_stat->cipher) == 0) {
2245 memset((crypt_stat->key + 24), 0, 8);
2246 auth_tok->session_key.encrypted_key_size = 32;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002247 } else
2248 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002249 key_rec->enc_key_size =
Michael Halcrow237fead2006-10-04 02:16:22 -07002250 auth_tok->session_key.encrypted_key_size;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002251 encrypted_session_key_valid = 0;
2252 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
2253 encrypted_session_key_valid |=
2254 auth_tok->session_key.encrypted_key[i];
2255 if (encrypted_session_key_valid) {
2256 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
2257 "using auth_tok->session_key.encrypted_key, "
Tyler Hicksf24b3882010-11-15 17:36:38 -06002258 "where key_rec->enc_key_size = [%zd]\n",
Michael Halcrowf4aad162007-10-16 01:27:53 -07002259 key_rec->enc_key_size);
2260 memcpy(key_rec->enc_key,
2261 auth_tok->session_key.encrypted_key,
2262 key_rec->enc_key_size);
2263 goto encrypted_session_key_set;
2264 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08002265 if (auth_tok->token.password.flags &
2266 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
Michael Halcrow237fead2006-10-04 02:16:22 -07002267 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
2268 "session key encryption key of size [%d]\n",
2269 auth_tok->token.password.
2270 session_key_encryption_key_bytes);
2271 memcpy(session_key_encryption_key,
2272 auth_tok->token.password.session_key_encryption_key,
2273 crypt_stat->key_size);
2274 ecryptfs_printk(KERN_DEBUG,
Jean Delvaredf2e3012011-07-16 18:10:35 +02002275 "Cached session key encryption key:\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07002276 if (ecryptfs_verbosity > 0)
2277 ecryptfs_dump_hex(session_key_encryption_key, 16);
2278 }
2279 if (unlikely(ecryptfs_verbosity > 0)) {
2280 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
2281 ecryptfs_dump_hex(session_key_encryption_key, 16);
2282 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07002283 rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
Michael Halcrowac97b9f2008-11-19 15:36:28 -08002284 src_sg, 2);
2285 if (rc < 1 || rc > 2) {
Michael Halcrow237fead2006-10-04 02:16:22 -07002286 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07002287 "for crypt_stat session key; expected rc = 1; "
Tyler Hicksf24b3882010-11-15 17:36:38 -06002288 "got rc = [%d]. key_rec->enc_key_size = [%zd]\n",
Michael Halcrowf4aad162007-10-16 01:27:53 -07002289 rc, key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07002290 rc = -ENOMEM;
2291 goto out;
2292 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07002293 rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
Michael Halcrowac97b9f2008-11-19 15:36:28 -08002294 dst_sg, 2);
2295 if (rc < 1 || rc > 2) {
Michael Halcrow237fead2006-10-04 02:16:22 -07002296 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07002297 "for crypt_stat encrypted session key; "
2298 "expected rc = 1; got rc = [%d]. "
Tyler Hicksf24b3882010-11-15 17:36:38 -06002299 "key_rec->enc_key_size = [%zd]\n", rc,
Michael Halcrowf4aad162007-10-16 01:27:53 -07002300 key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07002301 rc = -ENOMEM;
2302 goto out;
2303 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07002304 mutex_lock(tfm_mutex);
Herbert Xu3095e8e2016-01-25 10:29:33 +08002305 rc = crypto_skcipher_setkey(tfm, session_key_encryption_key,
2306 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07002307 if (rc < 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07002308 mutex_unlock(tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07002309 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
Michael Halcrow8bba0662006-10-30 22:07:18 -08002310 "context; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07002311 goto out;
2312 }
Herbert Xu3095e8e2016-01-25 10:29:33 +08002313
2314 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2315 if (!req) {
2316 mutex_unlock(tfm_mutex);
2317 ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst "
2318 "attempting to skcipher_request_alloc for "
2319 "%s\n", crypto_skcipher_driver_name(tfm));
2320 rc = -ENOMEM;
2321 goto out;
2322 }
2323
2324 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
2325 NULL, NULL);
2326
Michael Halcrow237fead2006-10-04 02:16:22 -07002327 rc = 0;
Tyler Hicksf24b3882010-11-15 17:36:38 -06002328 ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n",
Michael Halcrow237fead2006-10-04 02:16:22 -07002329 crypt_stat->key_size);
Herbert Xu3095e8e2016-01-25 10:29:33 +08002330 skcipher_request_set_crypt(req, src_sg, dst_sg,
2331 (*key_rec).enc_key_size, NULL);
2332 rc = crypto_skcipher_encrypt(req);
Michael Halcrowf4aad162007-10-16 01:27:53 -07002333 mutex_unlock(tfm_mutex);
Herbert Xu3095e8e2016-01-25 10:29:33 +08002334 skcipher_request_free(req);
Michael Halcrow8bba0662006-10-30 22:07:18 -08002335 if (rc) {
2336 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
2337 goto out;
2338 }
Michael Halcrow237fead2006-10-04 02:16:22 -07002339 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
Michael Halcrowf4aad162007-10-16 01:27:53 -07002340 if (ecryptfs_verbosity > 0) {
Tyler Hicksf24b3882010-11-15 17:36:38 -06002341 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n",
Michael Halcrowf4aad162007-10-16 01:27:53 -07002342 key_rec->enc_key_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002343 ecryptfs_dump_hex(key_rec->enc_key,
2344 key_rec->enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07002345 }
Michael Halcrow237fead2006-10-04 02:16:22 -07002346encrypted_session_key_set:
Michael Halcrow237fead2006-10-04 02:16:22 -07002347 /* This format is inspired by OpenPGP; see RFC 2440
2348 * packet tag 3 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07002349 max_packet_size = (1 /* Tag 3 identifier */
2350 + 3 /* Max Tag 3 packet size */
2351 + 1 /* Version */
2352 + 1 /* Cipher code */
2353 + 1 /* S2K specifier */
2354 + 1 /* Hash identifier */
2355 + ECRYPTFS_SALT_SIZE /* Salt */
2356 + 1 /* Hash iterations */
2357 + key_rec->enc_key_size); /* Encrypted key size */
2358 if (max_packet_size > (*remaining_bytes)) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07002359 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
2360 "there are only [%td] available\n", max_packet_size,
Michael Halcrowf4aad162007-10-16 01:27:53 -07002361 (*remaining_bytes));
2362 rc = -EINVAL;
2363 goto out;
2364 }
Michael Halcrow237fead2006-10-04 02:16:22 -07002365 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002366 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
2367 * to get the number of octets in the actual Tag 3 packet */
Michael Halcrowf66e8832008-04-29 00:59:51 -07002368 rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
2369 (max_packet_size - 4),
2370 &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07002371 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07002372 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
2373 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07002374 goto out;
2375 }
2376 (*packet_size) += packet_size_length;
2377 dest[(*packet_size)++] = 0x04; /* version 4 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07002378 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
2379 * specified with strings */
Michael Halcrow9c79f342009-01-06 14:41:57 -08002380 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher,
2381 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07002382 if (cipher_code == 0) {
2383 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
2384 "cipher [%s]\n", crypt_stat->cipher);
2385 rc = -EINVAL;
2386 goto out;
2387 }
2388 dest[(*packet_size)++] = cipher_code;
2389 dest[(*packet_size)++] = 0x03; /* S2K */
2390 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
2391 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
2392 ECRYPTFS_SALT_SIZE);
2393 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
2394 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
Michael Halcrowdddfa462007-02-12 00:53:44 -08002395 memcpy(&dest[(*packet_size)], key_rec->enc_key,
2396 key_rec->enc_key_size);
2397 (*packet_size) += key_rec->enc_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07002398out:
Michael Halcrow237fead2006-10-04 02:16:22 -07002399 if (rc)
2400 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002401 else
2402 (*remaining_bytes) -= (*packet_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07002403 return rc;
2404}
2405
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002406struct kmem_cache *ecryptfs_key_record_cache;
2407
Michael Halcrow237fead2006-10-04 02:16:22 -07002408/**
2409 * ecryptfs_generate_key_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07002410 * @dest_base: Virtual address from which to write the key record set
Michael Halcrow237fead2006-10-04 02:16:22 -07002411 * @crypt_stat: The cryptographic context from which the
2412 * authentication tokens will be retrieved
2413 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
2414 * for the global parameters
2415 * @len: The amount written
2416 * @max: The maximum amount of data allowed to be written
2417 *
2418 * Generates a key packet set and writes it to the virtual address
2419 * passed in.
2420 *
2421 * Returns zero on success; non-zero on error.
2422 */
2423int
2424ecryptfs_generate_key_packet_set(char *dest_base,
2425 struct ecryptfs_crypt_stat *crypt_stat,
2426 struct dentry *ecryptfs_dentry, size_t *len,
2427 size_t max)
2428{
Michael Halcrow237fead2006-10-04 02:16:22 -07002429 struct ecryptfs_auth_tok *auth_tok;
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +01002430 struct key *auth_tok_key = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07002431 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2432 &ecryptfs_superblock_to_private(
2433 ecryptfs_dentry->d_sb)->mount_crypt_stat;
2434 size_t written;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002435 struct ecryptfs_key_record *key_rec;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002436 struct ecryptfs_key_sig *key_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002437 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07002438
2439 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002440 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002441 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
2442 if (!key_rec) {
2443 rc = -ENOMEM;
2444 goto out;
2445 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07002446 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
2447 crypt_stat_list) {
2448 memset(key_rec, 0, sizeof(*key_rec));
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +01002449 rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key,
2450 &auth_tok,
Michael Halcrowf4aad162007-10-16 01:27:53 -07002451 mount_crypt_stat,
2452 key_sig->keysig);
2453 if (rc) {
Roberto Sassu0e1fc5e2011-03-21 16:00:53 +01002454 printk(KERN_WARNING "Unable to retrieve auth tok with "
2455 "sig = [%s]\n", key_sig->keysig);
2456 rc = process_find_global_auth_tok_for_sig_err(rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07002457 goto out_free;
2458 }
Michael Halcrow237fead2006-10-04 02:16:22 -07002459 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
2460 rc = write_tag_3_packet((dest_base + (*len)),
Michael Halcrowf4aad162007-10-16 01:27:53 -07002461 &max, auth_tok,
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002462 crypt_stat, key_rec,
Michael Halcrow237fead2006-10-04 02:16:22 -07002463 &written);
Tyler Hicksb2987a52011-07-26 19:47:08 -05002464 up_write(&(auth_tok_key->sem));
2465 key_put(auth_tok_key);
Michael Halcrow237fead2006-10-04 02:16:22 -07002466 if (rc) {
2467 ecryptfs_printk(KERN_WARNING, "Error "
2468 "writing tag 3 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002469 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07002470 }
2471 (*len) += written;
2472 /* Write auth tok signature packet */
Michael Halcrowf4aad162007-10-16 01:27:53 -07002473 rc = write_tag_11_packet((dest_base + (*len)), &max,
2474 key_rec->sig,
2475 ECRYPTFS_SIG_SIZE, &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07002476 if (rc) {
2477 ecryptfs_printk(KERN_ERR, "Error writing "
2478 "auth tok signature packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002479 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07002480 }
2481 (*len) += written;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002482 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Tyler Hicksb2987a52011-07-26 19:47:08 -05002483 rc = write_tag_1_packet(dest_base + (*len), &max,
2484 auth_tok_key, auth_tok,
Michael Halcrowf4aad162007-10-16 01:27:53 -07002485 crypt_stat, key_rec, &written);
Michael Halcrowdddfa462007-02-12 00:53:44 -08002486 if (rc) {
2487 ecryptfs_printk(KERN_WARNING, "Error "
2488 "writing tag 1 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002489 goto out_free;
Michael Halcrowdddfa462007-02-12 00:53:44 -08002490 }
2491 (*len) += written;
Michael Halcrow237fead2006-10-04 02:16:22 -07002492 } else {
Tyler Hicksb2987a52011-07-26 19:47:08 -05002493 up_write(&(auth_tok_key->sem));
2494 key_put(auth_tok_key);
Michael Halcrow237fead2006-10-04 02:16:22 -07002495 ecryptfs_printk(KERN_WARNING, "Unsupported "
2496 "authentication token type\n");
2497 rc = -EINVAL;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002498 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07002499 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07002500 }
2501 if (likely(max > 0)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07002502 dest_base[(*len)] = 0x00;
2503 } else {
2504 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
2505 rc = -EIO;
2506 }
Michael Halcroweb95e7f2007-02-16 01:28:40 -08002507out_free:
2508 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
Michael Halcrow237fead2006-10-04 02:16:22 -07002509out:
2510 if (rc)
2511 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002512 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07002513 return rc;
2514}
Michael Halcrowf4aad162007-10-16 01:27:53 -07002515
2516struct kmem_cache *ecryptfs_key_sig_cache;
2517
2518int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
2519{
2520 struct ecryptfs_key_sig *new_key_sig;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002521
2522 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
2523 if (!new_key_sig) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07002524 printk(KERN_ERR
2525 "Error allocating from ecryptfs_key_sig_cache\n");
Roland Dreieraa061172009-07-01 15:48:18 -07002526 return -ENOMEM;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002527 }
2528 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
Roberto Sassu7762e232011-03-21 16:00:52 +01002529 new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
Roland Dreieraa061172009-07-01 15:48:18 -07002530 /* Caller must hold keysig_list_mutex */
Michael Halcrowf4aad162007-10-16 01:27:53 -07002531 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
Roland Dreieraa061172009-07-01 15:48:18 -07002532
2533 return 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002534}
2535
2536struct kmem_cache *ecryptfs_global_auth_tok_cache;
2537
2538int
2539ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
Tyler Hicks84814d62009-03-13 13:51:59 -07002540 char *sig, u32 global_auth_tok_flags)
Michael Halcrowf4aad162007-10-16 01:27:53 -07002541{
2542 struct ecryptfs_global_auth_tok *new_auth_tok;
2543 int rc = 0;
2544
Eric Sandeen459e2162007-12-17 16:19:52 -08002545 new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
Michael Halcrowf4aad162007-10-16 01:27:53 -07002546 GFP_KERNEL);
2547 if (!new_auth_tok) {
2548 rc = -ENOMEM;
2549 printk(KERN_ERR "Error allocating from "
2550 "ecryptfs_global_auth_tok_cache\n");
2551 goto out;
2552 }
2553 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
Tyler Hicks84814d62009-03-13 13:51:59 -07002554 new_auth_tok->flags = global_auth_tok_flags;
Michael Halcrowf4aad162007-10-16 01:27:53 -07002555 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
2556 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
2557 list_add(&new_auth_tok->mount_crypt_stat_list,
2558 &mount_crypt_stat->global_auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07002559 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
2560out:
2561 return rc;
2562}
2563