Blame view

fs/ecryptfs/keystore.c 80.3 KB
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1
2
3
4
5
6
7
8
9
  /**
   * eCryptfs: Linux filesystem encryption layer
   * In-kernel key management code.  Includes functions to parse and
   * write authentication token-related packets with the underlying
   * file.
   *
   * Copyright (C) 2004-2006 International Business Machines Corp.
   *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
   *              Michael C. Thompson <mcthomps@us.ibm.com>
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
10
   *              Trevor S. Highland <trevor.highland@gmail.com>
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation; either version 2 of the
   * License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   * 02111-1307, USA.
   */
3095e8e36   Herbert Xu   eCryptfs: Use skc...
27
28
  #include <crypto/hash.h>
  #include <crypto/skcipher.h>
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
29
  #include <linux/string.h>
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
30
31
32
  #include <linux/pagemap.h>
  #include <linux/key.h>
  #include <linux/random.h>
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
33
  #include <linux/scatterlist.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
34
  #include <linux/slab.h>
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
35
36
37
38
39
40
41
  #include "ecryptfs_kernel.h"
  
  /**
   * request_key returned an error instead of a valid key address;
   * determine the type of error, make appropriate log entries, and
   * return an error code.
   */
cd9d67dfd   Michael Halcrow   eCryptfs: make ne...
42
  static int process_request_key_err(long err_code)
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
43
44
45
46
  {
  	int rc = 0;
  
  	switch (err_code) {
982363c97   Eric Sandeen   ecryptfs: propaga...
47
  	case -ENOKEY:
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
48
49
50
51
  		ecryptfs_printk(KERN_WARNING, "No key
  ");
  		rc = -ENOENT;
  		break;
982363c97   Eric Sandeen   ecryptfs: propaga...
52
  	case -EKEYEXPIRED:
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
53
54
55
56
  		ecryptfs_printk(KERN_WARNING, "Key expired
  ");
  		rc = -ETIME;
  		break;
982363c97   Eric Sandeen   ecryptfs: propaga...
57
  	case -EKEYREVOKED:
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
58
59
60
61
62
63
  		ecryptfs_printk(KERN_WARNING, "Key revoked
  ");
  		rc = -EINVAL;
  		break;
  	default:
  		ecryptfs_printk(KERN_WARNING, "Unknown error code: "
888d57bbc   Joe Perches   fs/ecryptfs: Add ...
64
65
  				"[0x%.16lx]
  ", err_code);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
66
67
68
69
  		rc = -EINVAL;
  	}
  	return rc;
  }
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  static int process_find_global_auth_tok_for_sig_err(int err_code)
  {
  	int rc = err_code;
  
  	switch (err_code) {
  	case -ENOENT:
  		ecryptfs_printk(KERN_WARNING, "Missing auth tok
  ");
  		break;
  	case -EINVAL:
  		ecryptfs_printk(KERN_WARNING, "Invalid auth tok
  ");
  		break;
  	default:
  		rc = process_request_key_err(err_code);
  		break;
  	}
  	return rc;
  }
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
89
  /**
f66e883eb   Michael Halcrow   eCryptfs: integra...
90
   * ecryptfs_parse_packet_length
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
91
92
93
94
95
   * @data: Pointer to memory containing length at offset
   * @size: This function writes the decoded size to this memory
   *        address; zero on error
   * @length_size: The number of bytes occupied by the encoded length
   *
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
96
   * Returns zero on success; non-zero on error
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
97
   */
f66e883eb   Michael Halcrow   eCryptfs: integra...
98
99
  int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
  				 size_t *length_size)
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
100
101
102
103
104
105
106
  {
  	int rc = 0;
  
  	(*length_size) = 0;
  	(*size) = 0;
  	if (data[0] < 192) {
  		/* One-byte length */
831115af5   Tyler Hicks   eCryptfs: Remove ...
107
  		(*size) = data[0];
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
108
109
110
  		(*length_size) = 1;
  	} else if (data[0] < 224) {
  		/* Two-byte length */
831115af5   Tyler Hicks   eCryptfs: Remove ...
111
112
  		(*size) = (data[0] - 192) * 256;
  		(*size) += data[1] + 192;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
113
114
  		(*length_size) = 2;
  	} else if (data[0] == 255) {
48399c0b0   Tyler Hicks   eCryptfs: Replace...
115
  		/* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  		ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
  				"supported
  ");
  		rc = -EINVAL;
  		goto out;
  	} else {
  		ecryptfs_printk(KERN_ERR, "Error parsing packet length
  ");
  		rc = -EINVAL;
  		goto out;
  	}
  out:
  	return rc;
  }
  
  /**
f66e883eb   Michael Halcrow   eCryptfs: integra...
132
   * ecryptfs_write_packet_length
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
133
   * @dest: The byte array target into which to write the length. Must
48399c0b0   Tyler Hicks   eCryptfs: Replace...
134
   *        have at least ECRYPTFS_MAX_PKT_LEN_SIZE bytes allocated.
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
135
   * @size: The length to write.
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
136
137
   * @packet_size_length: The number of bytes used to encode the packet
   *                      length is written to this address.
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
138
139
140
   *
   * Returns zero on success; non-zero on error.
   */
f66e883eb   Michael Halcrow   eCryptfs: integra...
141
142
  int ecryptfs_write_packet_length(char *dest, size_t size,
  				 size_t *packet_size_length)
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
143
144
145
146
147
148
149
150
151
152
153
  {
  	int rc = 0;
  
  	if (size < 192) {
  		dest[0] = size;
  		(*packet_size_length) = 1;
  	} else if (size < 65536) {
  		dest[0] = (((size - 192) / 256) + 192);
  		dest[1] = ((size - 192) % 256);
  		(*packet_size_length) = 2;
  	} else {
48399c0b0   Tyler Hicks   eCryptfs: Replace...
154
  		/* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
155
156
  		rc = -EINVAL;
  		ecryptfs_printk(KERN_WARNING,
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
157
158
  				"Unsupported packet size: [%zd]
  ", size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
159
160
161
  	}
  	return rc;
  }
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  static int
  write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
  		    char **packet, size_t *packet_len)
  {
  	size_t i = 0;
  	size_t data_len;
  	size_t packet_size_len;
  	char *message;
  	int rc;
  
  	/*
  	 *              ***** TAG 64 Packet Format *****
  	 *    | Content Type                       | 1 byte       |
  	 *    | Key Identifier Size                | 1 or 2 bytes |
  	 *    | Key Identifier                     | arbitrary    |
  	 *    | Encrypted File Encryption Key Size | 1 or 2 bytes |
  	 *    | Encrypted File Encryption Key      | arbitrary    |
  	 */
  	data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
  		    + session_key->encrypted_key_size);
  	*packet = kmalloc(data_len, GFP_KERNEL);
  	message = *packet;
  	if (!message) {
  		ecryptfs_printk(KERN_ERR, "Unable to allocate memory
  ");
  		rc = -ENOMEM;
  		goto out;
  	}
  	message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
f66e883eb   Michael Halcrow   eCryptfs: integra...
191
192
  	rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
  					  &packet_size_len);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
193
194
195
196
197
198
199
200
201
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
  				"header; cannot generate packet length
  ");
  		goto out;
  	}
  	i += packet_size_len;
  	memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
  	i += ECRYPTFS_SIG_SIZE_HEX;
f66e883eb   Michael Halcrow   eCryptfs: integra...
202
203
204
  	rc = ecryptfs_write_packet_length(&message[i],
  					  session_key->encrypted_key_size,
  					  &packet_size_len);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
  				"header; cannot generate packet length
  ");
  		goto out;
  	}
  	i += packet_size_len;
  	memcpy(&message[i], session_key->encrypted_key,
  	       session_key->encrypted_key_size);
  	i += session_key->encrypted_key_size;
  	*packet_len = i;
  out:
  	return rc;
  }
  
  static int
19e66a67e   Trevor Highland   eCryptfs: change ...
221
  parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  		    struct ecryptfs_message *msg)
  {
  	size_t i = 0;
  	char *data;
  	size_t data_len;
  	size_t m_size;
  	size_t message_len;
  	u16 checksum = 0;
  	u16 expected_checksum = 0;
  	int rc;
  
  	/*
  	 *              ***** TAG 65 Packet Format *****
  	 *         | Content Type             | 1 byte       |
  	 *         | Status Indicator         | 1 byte       |
  	 *         | File Encryption Key Size | 1 or 2 bytes |
  	 *         | File Encryption Key      | arbitrary    |
  	 */
  	message_len = msg->data_len;
  	data = msg->data;
  	if (message_len < 4) {
  		rc = -EIO;
  		goto out;
  	}
  	if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
  		ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65
  ");
  		rc = -EIO;
  		goto out;
  	}
  	if (data[i++]) {
  		ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
  				"[%d]
  ", data[i-1]);
  		rc = -EIO;
  		goto out;
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
259
  	rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
260
261
262
263
264
265
266
267
  	if (rc) {
  		ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  				"rc = [%d]
  ", rc);
  		goto out;
  	}
  	i += data_len;
  	if (message_len < (i + m_size)) {
624ae5284   Tyler Hicks   eCryptfs: remove ...
268
269
270
  		ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd "
  				"is shorter than expected
  ");
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  		rc = -EIO;
  		goto out;
  	}
  	if (m_size < 3) {
  		ecryptfs_printk(KERN_ERR,
  				"The decrypted key is not long enough to "
  				"include a cipher code and checksum
  ");
  		rc = -EIO;
  		goto out;
  	}
  	*cipher_code = data[i++];
  	/* The decrypted key includes 1 byte cipher code and 2 byte checksum */
  	session_key->decrypted_key_size = m_size - 3;
  	if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
  		ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
  				"the maximum key size [%d]
  ",
  				session_key->decrypted_key_size,
  				ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
  		rc = -EIO;
  		goto out;
  	}
  	memcpy(session_key->decrypted_key, &data[i],
  	       session_key->decrypted_key_size);
  	i += session_key->decrypted_key_size;
  	expected_checksum += (unsigned char)(data[i++]) << 8;
  	expected_checksum += (unsigned char)(data[i++]);
  	for (i = 0; i < session_key->decrypted_key_size; i++)
  		checksum += session_key->decrypted_key[i];
  	if (expected_checksum != checksum) {
  		ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
  				"encryption  key; expected [%x]; calculated "
  				"[%x]
  ", expected_checksum, checksum);
  		rc = -EIO;
  	}
  out:
  	return rc;
  }
  
  
  static int
19e66a67e   Trevor Highland   eCryptfs: change ...
314
  write_tag_66_packet(char *signature, u8 cipher_code,
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  		    struct ecryptfs_crypt_stat *crypt_stat, char **packet,
  		    size_t *packet_len)
  {
  	size_t i = 0;
  	size_t j;
  	size_t data_len;
  	size_t checksum = 0;
  	size_t packet_size_len;
  	char *message;
  	int rc;
  
  	/*
  	 *              ***** TAG 66 Packet Format *****
  	 *         | Content Type             | 1 byte       |
  	 *         | Key Identifier Size      | 1 or 2 bytes |
  	 *         | Key Identifier           | arbitrary    |
  	 *         | File Encryption Key Size | 1 or 2 bytes |
  	 *         | File Encryption Key      | arbitrary    |
  	 */
  	data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
  	*packet = kmalloc(data_len, GFP_KERNEL);
  	message = *packet;
  	if (!message) {
  		ecryptfs_printk(KERN_ERR, "Unable to allocate memory
  ");
  		rc = -ENOMEM;
  		goto out;
  	}
  	message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
f66e883eb   Michael Halcrow   eCryptfs: integra...
344
345
  	rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
  					  &packet_size_len);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
346
347
348
349
350
351
352
353
354
355
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
  				"header; cannot generate packet length
  ");
  		goto out;
  	}
  	i += packet_size_len;
  	memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
  	i += ECRYPTFS_SIG_SIZE_HEX;
  	/* The encrypted key includes 1 byte cipher code and 2 byte checksum */
f66e883eb   Michael Halcrow   eCryptfs: integra...
356
357
  	rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
  					  &packet_size_len);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
  				"header; cannot generate packet length
  ");
  		goto out;
  	}
  	i += packet_size_len;
  	message[i++] = cipher_code;
  	memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
  	i += crypt_stat->key_size;
  	for (j = 0; j < crypt_stat->key_size; j++)
  		checksum += crypt_stat->key[j];
  	message[i++] = (checksum / 256) % 256;
  	message[i++] = (checksum % 256);
  	*packet_len = i;
  out:
  	return rc;
  }
  
  static int
  parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
  		    struct ecryptfs_message *msg)
  {
  	size_t i = 0;
  	char *data;
  	size_t data_len;
  	size_t message_len;
  	int rc;
  
  	/*
  	 *              ***** TAG 65 Packet Format *****
  	 *    | Content Type                       | 1 byte       |
  	 *    | Status Indicator                   | 1 byte       |
  	 *    | Encrypted File Encryption Key Size | 1 or 2 bytes |
  	 *    | Encrypted File Encryption Key      | arbitrary    |
  	 */
  	message_len = msg->data_len;
  	data = msg->data;
  	/* verify that everything through the encrypted FEK size is present */
  	if (message_len < 4) {
  		rc = -EIO;
df261c52a   Michael Halcrow   eCryptfs: Replace...
399
  		printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable "
f66e883eb   Michael Halcrow   eCryptfs: integra...
400
401
  		       "message length is [%d]
  ", __func__, message_len, 4);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
402
403
404
  		goto out;
  	}
  	if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
405
  		rc = -EIO;
f66e883eb   Michael Halcrow   eCryptfs: integra...
406
407
408
  		printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67
  ",
  		       __func__);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
409
410
411
  		goto out;
  	}
  	if (data[i++]) {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
412
  		rc = -EIO;
f66e883eb   Michael Halcrow   eCryptfs: integra...
413
414
415
  		printk(KERN_ERR "%s: Status indicator has non zero "
  		       "value [%d]
  ", __func__, data[i-1]);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
416
417
  		goto out;
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
418
419
  	rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
  					  &data_len);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
420
421
422
423
424
425
426
427
  	if (rc) {
  		ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  				"rc = [%d]
  ", rc);
  		goto out;
  	}
  	i += data_len;
  	if (message_len < (i + key_rec->enc_key_size)) {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
428
  		rc = -EIO;
df261c52a   Michael Halcrow   eCryptfs: Replace...
429
430
  		printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]
  ",
f66e883eb   Michael Halcrow   eCryptfs: integra...
431
  		       __func__, message_len, (i + key_rec->enc_key_size));
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
432
433
434
  		goto out;
  	}
  	if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
435
  		rc = -EIO;
df261c52a   Michael Halcrow   eCryptfs: Replace...
436
  		printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than "
f66e883eb   Michael Halcrow   eCryptfs: integra...
437
438
439
440
  		       "the maximum key size [%d]
  ", __func__,
  		       key_rec->enc_key_size,
  		       ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
441
442
443
444
445
446
  		goto out;
  	}
  	memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
  out:
  	return rc;
  }
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  /**
   * ecryptfs_verify_version
   * @version: The version number to confirm
   *
   * Returns zero on good version; non-zero otherwise
   */
  static int ecryptfs_verify_version(u16 version)
  {
  	int rc = 0;
  	unsigned char major;
  	unsigned char minor;
  
  	major = ((version >> 8) & 0xFF);
  	minor = (version & 0xFF);
  	if (major != ECRYPTFS_VERSION_MAJOR) {
  		ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
  				"Expected [%d]; got [%d]
  ",
  				ECRYPTFS_VERSION_MAJOR, major);
  		rc = -EINVAL;
  		goto out;
  	}
  	if (minor != ECRYPTFS_VERSION_MINOR) {
  		ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
  				"Expected [%d]; got [%d]
  ",
  				ECRYPTFS_VERSION_MINOR, minor);
  		rc = -EINVAL;
  		goto out;
  	}
  out:
  	return rc;
  }
  
  /**
   * ecryptfs_verify_auth_tok_from_key
   * @auth_tok_key: key containing the authentication token
   * @auth_tok: authentication token
   *
f66665c09   Eric Biggers   ecryptfs: fix der...
486
487
   * Returns zero on valid auth tok; -EINVAL if the payload is invalid; or
   * -EKEYREVOKED if the key was revoked before we acquired its semaphore.
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
488
489
490
491
492
493
494
495
   */
  static int
  ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key,
  				  struct ecryptfs_auth_tok **auth_tok)
  {
  	int rc = 0;
  
  	(*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key);
f66665c09   Eric Biggers   ecryptfs: fix der...
496
497
498
499
500
  	if (IS_ERR(*auth_tok)) {
  		rc = PTR_ERR(*auth_tok);
  		*auth_tok = NULL;
  		goto out;
  	}
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  	if (ecryptfs_verify_version((*auth_tok)->version)) {
  		printk(KERN_ERR "Data structure version mismatch. Userspace "
  		       "tools must match eCryptfs kernel module with major "
  		       "version [%d] and minor version [%d]
  ",
  		       ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR);
  		rc = -EINVAL;
  		goto out;
  	}
  	if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
  	    && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
  		printk(KERN_ERR "Invalid auth_tok structure "
  		       "returned from key query
  ");
  		rc = -EINVAL;
  		goto out;
  	}
  out:
  	return rc;
  }
cd9d67dfd   Michael Halcrow   eCryptfs: make ne...
521
  static int
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
522
  ecryptfs_find_global_auth_tok_for_sig(
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
523
524
  	struct key **auth_tok_key,
  	struct ecryptfs_auth_tok **auth_tok,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
525
526
527
528
  	struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
  {
  	struct ecryptfs_global_auth_tok *walker;
  	int rc = 0;
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
529
530
  	(*auth_tok_key) = NULL;
  	(*auth_tok) = NULL;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
531
532
533
534
  	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  	list_for_each_entry(walker,
  			    &mount_crypt_stat->global_auth_tok_list,
  			    mount_crypt_stat_list) {
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
535
536
537
538
539
  		if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX))
  			continue;
  
  		if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) {
  			rc = -EINVAL;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
540
541
  			goto out;
  		}
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
542
543
544
545
546
547
548
  
  		rc = key_validate(walker->global_auth_tok_key);
  		if (rc) {
  			if (rc == -EKEYEXPIRED)
  				goto out;
  			goto out_invalid_auth_tok;
  		}
b5695d046   Roberto Sassu   eCryptfs: write l...
549
  		down_write(&(walker->global_auth_tok_key->sem));
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
550
551
552
  		rc = ecryptfs_verify_auth_tok_from_key(
  				walker->global_auth_tok_key, auth_tok);
  		if (rc)
b5695d046   Roberto Sassu   eCryptfs: write l...
553
  			goto out_invalid_auth_tok_unlock;
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
554
555
556
557
  
  		(*auth_tok_key) = walker->global_auth_tok_key;
  		key_get(*auth_tok_key);
  		goto out;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
558
  	}
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
559
560
  	rc = -ENOENT;
  	goto out;
b5695d046   Roberto Sassu   eCryptfs: write l...
561
562
  out_invalid_auth_tok_unlock:
  	up_write(&(walker->global_auth_tok_key->sem));
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
563
564
565
566
567
568
  out_invalid_auth_tok:
  	printk(KERN_WARNING "Invalidating auth tok with sig = [%s]
  ", sig);
  	walker->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  	key_put(walker->global_auth_tok_key);
  	walker->global_auth_tok_key = NULL;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
  out:
  	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  	return rc;
  }
  
  /**
   * ecryptfs_find_auth_tok_for_sig
   * @auth_tok: Set to the matching auth_tok; NULL if not found
   * @crypt_stat: inode crypt_stat crypto context
   * @sig: Sig of auth_tok to find
   *
   * For now, this function simply looks at the registered auth_tok's
   * linked off the mount_crypt_stat, so all the auth_toks that can be
   * used must be registered at mount time. This function could
   * potentially try a lot harder to find auth_tok's (e.g., by calling
   * out to ecryptfsd to dynamically retrieve an auth_tok object) so
   * that static registration of auth_tok's will no longer be necessary.
   *
   * Returns zero on no error; non-zero on error
   */
  static int
  ecryptfs_find_auth_tok_for_sig(
aee683b9e   Roberto Sassu   ecryptfs: release...
591
  	struct key **auth_tok_key,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
592
593
594
595
  	struct ecryptfs_auth_tok **auth_tok,
  	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  	char *sig)
  {
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
596
  	int rc = 0;
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
597
598
599
  	rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok,
  						   mount_crypt_stat, sig);
  	if (rc == -ENOENT) {
f16feb511   Roberto Sassu   ecryptfs: added e...
600
601
602
603
604
605
606
607
  		/* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the
  		 * mount_crypt_stat structure, we prevent to use auth toks that
  		 * are not inserted through the ecryptfs_add_global_auth_tok
  		 * function.
  		 */
  		if (mount_crypt_stat->flags
  				& ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY)
  			return -EINVAL;
aee683b9e   Roberto Sassu   ecryptfs: release...
608
  		rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
609
  						       sig);
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
610
  	}
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  	return rc;
  }
  
  /**
   * write_tag_70_packet can gobble a lot of stack space. We stuff most
   * of the function's parameters in a kmalloc'd struct to help reduce
   * eCryptfs' overall stack usage.
   */
  struct ecryptfs_write_tag_70_packet_silly_stack {
  	u8 cipher_code;
  	size_t max_packet_size;
  	size_t packet_size_len;
  	size_t block_aligned_filename_size;
  	size_t block_size;
  	size_t i;
  	size_t j;
  	size_t num_rand_bytes;
  	struct mutex *tfm_mutex;
  	char *block_aligned_filename;
  	struct ecryptfs_auth_tok *auth_tok;
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
631
632
  	struct scatterlist src_sg[2];
  	struct scatterlist dst_sg[2];
3095e8e36   Herbert Xu   eCryptfs: Use skc...
633
634
  	struct crypto_skcipher *skcipher_tfm;
  	struct skcipher_request *skcipher_req;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
635
636
637
  	char iv[ECRYPTFS_MAX_IV_BYTES];
  	char hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
  	char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
3095e8e36   Herbert Xu   eCryptfs: Use skc...
638
639
  	struct crypto_shash *hash_tfm;
  	struct shash_desc *hash_desc;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  };
  
  /**
   * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK
   * @filename: NULL-terminated filename string
   *
   * This is the simplest mechanism for achieving filename encryption in
   * eCryptfs. It encrypts the given filename with the mount-wide
   * filename encryption key (FNEK) and stores it in a packet to @dest,
   * which the callee will encode and write directly into the dentry
   * name.
   */
  int
  ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
  			     size_t *packet_size,
  			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  			     char *filename, size_t filename_size)
  {
  	struct ecryptfs_write_tag_70_packet_silly_stack *s;
aee683b9e   Roberto Sassu   ecryptfs: release...
659
  	struct key *auth_tok_key = NULL;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
660
  	int rc = 0;
3095e8e36   Herbert Xu   eCryptfs: Use skc...
661
  	s = kzalloc(sizeof(*s), GFP_KERNEL);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
662
663
  	if (!s) {
  		printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
df261c52a   Michael Halcrow   eCryptfs: Replace...
664
665
  		       "[%zd] bytes of kernel memory
  ", __func__, sizeof(*s));
d1558f4e9   Herbert Xu   eCryptfs: Use skc...
666
  		return -ENOMEM;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
667
  	}
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
668
  	(*packet_size) = 0;
950983fc0   Roberto Sassu   eCryptfs: move ec...
669
670
671
672
673
674
675
676
677
678
679
  	rc = ecryptfs_find_auth_tok_for_sig(
  		&auth_tok_key,
  		&s->auth_tok, mount_crypt_stat,
  		mount_crypt_stat->global_default_fnek_sig);
  	if (rc) {
  		printk(KERN_ERR "%s: Error attempting to find auth tok for "
  		       "fnek sig [%s]; rc = [%d]
  ", __func__,
  		       mount_crypt_stat->global_default_fnek_sig, rc);
  		goto out;
  	}
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
680
  	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
3095e8e36   Herbert Xu   eCryptfs: Use skc...
681
  		&s->skcipher_tfm,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
682
683
684
685
686
687
688
689
690
  		&s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
  	if (unlikely(rc)) {
  		printk(KERN_ERR "Internal error whilst attempting to get "
  		       "tfm and mutex for cipher name [%s]; rc = [%d]
  ",
  		       mount_crypt_stat->global_default_fn_cipher_name, rc);
  		goto out;
  	}
  	mutex_lock(s->tfm_mutex);
3095e8e36   Herbert Xu   eCryptfs: Use skc...
691
  	s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
  	/* Plus one for the \0 separator between the random prefix
  	 * and the plaintext filename */
  	s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1);
  	s->block_aligned_filename_size = (s->num_rand_bytes + filename_size);
  	if ((s->block_aligned_filename_size % s->block_size) != 0) {
  		s->num_rand_bytes += (s->block_size
  				      - (s->block_aligned_filename_size
  					 % s->block_size));
  		s->block_aligned_filename_size = (s->num_rand_bytes
  						  + filename_size);
  	}
  	/* Octet 0: Tag 70 identifier
  	 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
  	 *              and block-aligned encrypted filename size)
  	 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
  	 * Octet N2-N3: Cipher identifier (1 octet)
  	 * Octets N3-N4: Block-aligned encrypted filename
  	 *  - Consists of a minimum number of random characters, a \0
  	 *    separator, and then the filename */
4a26620df   Tyler Hicks   eCryptfs: Improve...
711
  	s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
712
713
714
715
716
717
  			      + s->block_aligned_filename_size);
  	if (dest == NULL) {
  		(*packet_size) = s->max_packet_size;
  		goto out_unlock;
  	}
  	if (s->max_packet_size > (*remaining_bytes)) {
a8f12864c   Michael Halcrow   eCryptfs: Fix dat...
718
719
720
  		printk(KERN_WARNING "%s: Require [%zd] bytes to write; only "
  		       "[%zd] available
  ", __func__, s->max_packet_size,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
721
722
723
724
  		       (*remaining_bytes));
  		rc = -EINVAL;
  		goto out_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
725
726
727
728
729
730
731
732
733
734
735
736
737
  
  	s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL);
  	if (!s->skcipher_req) {
  		printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
  		       "skcipher_request_alloc for %s
  ", __func__,
  		       crypto_skcipher_driver_name(s->skcipher_tfm));
  		rc = -ENOMEM;
  		goto out_unlock;
  	}
  
  	skcipher_request_set_callback(s->skcipher_req,
  				      CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
738
739
740
741
  	s->block_aligned_filename = kzalloc(s->block_aligned_filename_size,
  					    GFP_KERNEL);
  	if (!s->block_aligned_filename) {
  		printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
df261c52a   Michael Halcrow   eCryptfs: Replace...
742
743
  		       "kzalloc [%zd] bytes
  ", __func__,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
744
745
746
747
  		       s->block_aligned_filename_size);
  		rc = -ENOMEM;
  		goto out_unlock;
  	}
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
  	dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE;
  	rc = ecryptfs_write_packet_length(&dest[s->i],
  					  (ECRYPTFS_SIG_SIZE
  					   + 1 /* Cipher code */
  					   + s->block_aligned_filename_size),
  					  &s->packet_size_len);
  	if (rc) {
  		printk(KERN_ERR "%s: Error generating tag 70 packet "
  		       "header; cannot generate packet length; rc = [%d]
  ",
  		       __func__, rc);
  		goto out_free_unlock;
  	}
  	s->i += s->packet_size_len;
  	ecryptfs_from_hex(&dest[s->i],
  			  mount_crypt_stat->global_default_fnek_sig,
  			  ECRYPTFS_SIG_SIZE);
  	s->i += ECRYPTFS_SIG_SIZE;
  	s->cipher_code = ecryptfs_code_for_cipher_string(
  		mount_crypt_stat->global_default_fn_cipher_name,
  		mount_crypt_stat->global_default_fn_cipher_key_bytes);
  	if (s->cipher_code == 0) {
  		printk(KERN_WARNING "%s: Unable to generate code for "
a8f12864c   Michael Halcrow   eCryptfs: Fix dat...
771
772
  		       "cipher [%s] with key bytes [%zd]
  ", __func__,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
773
774
775
776
777
778
  		       mount_crypt_stat->global_default_fn_cipher_name,
  		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
  		rc = -EINVAL;
  		goto out_free_unlock;
  	}
  	dest[s->i++] = s->cipher_code;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
779
780
  	/* TODO: Support other key modules than passphrase for
  	 * filename encryption */
df6ad33ba   Tyler Hicks   eCryptfs: Filenam...
781
782
783
784
785
786
787
  	if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
  		rc = -EOPNOTSUPP;
  		printk(KERN_INFO "%s: Filename encryption only supports "
  		       "password tokens
  ", __func__);
  		goto out_free_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
788
789
790
  	s->hash_tfm = crypto_alloc_shash(ECRYPTFS_TAG_70_DIGEST, 0, 0);
  	if (IS_ERR(s->hash_tfm)) {
  			rc = PTR_ERR(s->hash_tfm);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
791
792
793
794
795
796
  			printk(KERN_ERR "%s: Error attempting to "
  			       "allocate hash crypto context; rc = [%d]
  ",
  			       __func__, rc);
  			goto out_free_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
797
798
799
800
801
802
803
804
805
806
  
  	s->hash_desc = kmalloc(sizeof(*s->hash_desc) +
  			       crypto_shash_descsize(s->hash_tfm), GFP_KERNEL);
  	if (!s->hash_desc) {
  		printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
  		       "kmalloc [%zd] bytes
  ", __func__,
  		       sizeof(*s->hash_desc) +
  		       crypto_shash_descsize(s->hash_tfm));
  		rc = -ENOMEM;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
807
808
  		goto out_release_free_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
809
810
811
812
813
814
815
816
  
  	s->hash_desc->tfm = s->hash_tfm;
  	s->hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  
  	rc = crypto_shash_digest(s->hash_desc,
  				 (u8 *)s->auth_tok->token.password.session_key_encryption_key,
  				 s->auth_tok->token.password.session_key_encryption_key_bytes,
  				 s->hash);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
817
818
  	if (rc) {
  		printk(KERN_ERR
3095e8e36   Herbert Xu   eCryptfs: Use skc...
819
820
  		       "%s: Error computing crypto hash; rc = [%d]
  ",
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
821
822
823
824
825
826
827
828
  		       __func__, rc);
  		goto out_release_free_unlock;
  	}
  	for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) {
  		s->block_aligned_filename[s->j] =
  			s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)];
  		if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)
  		    == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) {
3095e8e36   Herbert Xu   eCryptfs: Use skc...
829
830
831
  			rc = crypto_shash_digest(s->hash_desc, (u8 *)s->hash,
  						ECRYPTFS_TAG_70_DIGEST_SIZE,
  						s->tmp_hash);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
832
833
  			if (rc) {
  				printk(KERN_ERR
3095e8e36   Herbert Xu   eCryptfs: Use skc...
834
  				       "%s: Error computing crypto hash; "
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
835
836
837
838
839
840
841
842
843
844
845
846
847
  				       "rc = [%d]
  ", __func__, rc);
  				goto out_release_free_unlock;
  			}
  			memcpy(s->hash, s->tmp_hash,
  			       ECRYPTFS_TAG_70_DIGEST_SIZE);
  		}
  		if (s->block_aligned_filename[s->j] == '\0')
  			s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL;
  	}
  	memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename,
  	       filename_size);
  	rc = virt_to_scatterlist(s->block_aligned_filename,
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
848
849
  				 s->block_aligned_filename_size, s->src_sg, 2);
  	if (rc < 1) {
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
850
  		printk(KERN_ERR "%s: Internal error whilst attempting to "
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
851
  		       "convert filename memory to scatterlist; rc = [%d]. "
a8f12864c   Michael Halcrow   eCryptfs: Fix dat...
852
853
  		       "block_aligned_filename_size = [%zd]
  ", __func__, rc,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
854
855
856
857
  		       s->block_aligned_filename_size);
  		goto out_release_free_unlock;
  	}
  	rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size,
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
858
859
  				 s->dst_sg, 2);
  	if (rc < 1) {
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
860
861
  		printk(KERN_ERR "%s: Internal error whilst attempting to "
  		       "convert encrypted filename memory to scatterlist; "
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
862
863
864
  		       "rc = [%d]. block_aligned_filename_size = [%zd]
  ",
  		       __func__, rc, s->block_aligned_filename_size);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
865
866
867
868
869
870
  		goto out_release_free_unlock;
  	}
  	/* The characters in the first block effectively do the job
  	 * of the IV here, so we just use 0's for the IV. Note the
  	 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
  	 * >= ECRYPTFS_MAX_IV_BYTES. */
3095e8e36   Herbert Xu   eCryptfs: Use skc...
871
872
  	rc = crypto_skcipher_setkey(
  		s->skcipher_tfm,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
873
874
875
876
877
878
  		s->auth_tok->token.password.session_key_encryption_key,
  		mount_crypt_stat->global_default_fn_cipher_key_bytes);
  	if (rc < 0) {
  		printk(KERN_ERR "%s: Error setting key for crypto context; "
  		       "rc = [%d]. s->auth_tok->token.password.session_key_"
  		       "encryption_key = [0x%p]; mount_crypt_stat->"
df261c52a   Michael Halcrow   eCryptfs: Replace...
879
880
  		       "global_default_fn_cipher_key_bytes = [%zd]
  ", __func__,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
881
882
883
884
885
  		       rc,
  		       s->auth_tok->token.password.session_key_encryption_key,
  		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
  		goto out_release_free_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
886
887
888
  	skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg,
  				   s->block_aligned_filename_size, s->iv);
  	rc = crypto_skcipher_encrypt(s->skcipher_req);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
889
890
891
892
893
894
895
896
897
898
  	if (rc) {
  		printk(KERN_ERR "%s: Error attempting to encrypt filename; "
  		       "rc = [%d]
  ", __func__, rc);
  		goto out_release_free_unlock;
  	}
  	s->i += s->block_aligned_filename_size;
  	(*packet_size) = s->i;
  	(*remaining_bytes) -= (*packet_size);
  out_release_free_unlock:
3095e8e36   Herbert Xu   eCryptfs: Use skc...
899
  	crypto_free_shash(s->hash_tfm);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
900
  out_free_unlock:
00fcf2cb6   Johannes Weiner   ecryptfs: use kzf...
901
  	kzfree(s->block_aligned_filename);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
902
903
904
  out_unlock:
  	mutex_unlock(s->tfm_mutex);
  out:
b5695d046   Roberto Sassu   eCryptfs: write l...
905
906
  	if (auth_tok_key) {
  		up_write(&(auth_tok_key->sem));
aee683b9e   Roberto Sassu   ecryptfs: release...
907
  		key_put(auth_tok_key);
b5695d046   Roberto Sassu   eCryptfs: write l...
908
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
909
910
  	skcipher_request_free(s->skcipher_req);
  	kzfree(s->hash_desc);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
  	kfree(s);
  	return rc;
  }
  
  struct ecryptfs_parse_tag_70_packet_silly_stack {
  	u8 cipher_code;
  	size_t max_packet_size;
  	size_t packet_size_len;
  	size_t parsed_tag_70_packet_size;
  	size_t block_aligned_filename_size;
  	size_t block_size;
  	size_t i;
  	struct mutex *tfm_mutex;
  	char *decrypted_filename;
  	struct ecryptfs_auth_tok *auth_tok;
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
926
927
  	struct scatterlist src_sg[2];
  	struct scatterlist dst_sg[2];
3095e8e36   Herbert Xu   eCryptfs: Use skc...
928
929
  	struct crypto_skcipher *skcipher_tfm;
  	struct skcipher_request *skcipher_req;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
930
931
  	char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
  	char iv[ECRYPTFS_MAX_IV_BYTES];
2a559a8bd   Colin Ian King   eCryptfs: ensure ...
932
  	char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
933
934
935
936
937
  };
  
  /**
   * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet
   * @filename: This function kmalloc's the memory for the filename
7d8bc2be5   Michael Halcrow   eCryptfs: kerneld...
938
939
940
941
942
943
944
945
946
947
948
   * @filename_size: This function sets this to the amount of memory
   *                 kmalloc'd for the filename
   * @packet_size: This function sets this to the the number of octets
   *               in the packet parsed
   * @mount_crypt_stat: The mount-wide cryptographic context
   * @data: The memory location containing the start of the tag 70
   *        packet
   * @max_packet_size: The maximum legal size of the packet to be parsed
   *                   from @data
   *
   * Returns zero on success; non-zero otherwise
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
949
950
951
952
953
954
955
956
   */
  int
  ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
  			     size_t *packet_size,
  			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  			     char *data, size_t max_packet_size)
  {
  	struct ecryptfs_parse_tag_70_packet_silly_stack *s;
aee683b9e   Roberto Sassu   ecryptfs: release...
957
  	struct key *auth_tok_key = NULL;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
958
959
960
961
962
  	int rc = 0;
  
  	(*packet_size) = 0;
  	(*filename_size) = 0;
  	(*filename) = NULL;
3095e8e36   Herbert Xu   eCryptfs: Use skc...
963
  	s = kzalloc(sizeof(*s), GFP_KERNEL);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
964
965
  	if (!s) {
  		printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
a8f12864c   Michael Halcrow   eCryptfs: Fix dat...
966
967
  		       "[%zd] bytes of kernel memory
  ", __func__, sizeof(*s));
d1558f4e9   Herbert Xu   eCryptfs: Use skc...
968
  		return -ENOMEM;
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
969
  	}
4a26620df   Tyler Hicks   eCryptfs: Improve...
970
  	if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) {
df261c52a   Michael Halcrow   eCryptfs: Replace...
971
  		printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
972
973
  		       "at least [%d]
  ", __func__, max_packet_size,
4a26620df   Tyler Hicks   eCryptfs: Improve...
974
  		       ECRYPTFS_TAG_70_MIN_METADATA_SIZE);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  		rc = -EINVAL;
  		goto out;
  	}
  	/* Octet 0: Tag 70 identifier
  	 * Octets 1-N1: Tag 70 packet size (includes cipher identifier
  	 *              and block-aligned encrypted filename size)
  	 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
  	 * Octet N2-N3: Cipher identifier (1 octet)
  	 * Octets N3-N4: Block-aligned encrypted filename
  	 *  - Consists of a minimum number of random numbers, a \0
  	 *    separator, and then the filename */
  	if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) {
  		printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be "
  		       "tag [0x%.2x]
  ", __func__,
  		       data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE);
  		rc = -EINVAL;
  		goto out;
  	}
  	rc = ecryptfs_parse_packet_length(&data[(*packet_size)],
  					  &s->parsed_tag_70_packet_size,
  					  &s->packet_size_len);
  	if (rc) {
  		printk(KERN_WARNING "%s: Error parsing packet length; "
  		       "rc = [%d]
  ", __func__, rc);
  		goto out;
  	}
  	s->block_aligned_filename_size = (s->parsed_tag_70_packet_size
  					  - ECRYPTFS_SIG_SIZE - 1);
  	if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size)
  	    > max_packet_size) {
a8f12864c   Michael Halcrow   eCryptfs: Fix dat...
1007
1008
1009
  		printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet "
  		       "size is [%zd]
  ", __func__, max_packet_size,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
  		       (1 + s->packet_size_len + 1
  			+ s->block_aligned_filename_size));
  		rc = -EINVAL;
  		goto out;
  	}
  	(*packet_size) += s->packet_size_len;
  	ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)],
  			ECRYPTFS_SIG_SIZE);
  	s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  	(*packet_size) += ECRYPTFS_SIG_SIZE;
  	s->cipher_code = data[(*packet_size)++];
  	rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
  	if (rc) {
  		printk(KERN_WARNING "%s: Cipher code [%d] is invalid
  ",
  		       __func__, s->cipher_code);
  		goto out;
  	}
950983fc0   Roberto Sassu   eCryptfs: move ec...
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
  	rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
  					    &s->auth_tok, mount_crypt_stat,
  					    s->fnek_sig_hex);
  	if (rc) {
  		printk(KERN_ERR "%s: Error attempting to find auth tok for "
  		       "fnek sig [%s]; rc = [%d]
  ", __func__, s->fnek_sig_hex,
  		       rc);
  		goto out;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1038
  	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
  							&s->tfm_mutex,
  							s->cipher_string);
  	if (unlikely(rc)) {
  		printk(KERN_ERR "Internal error whilst attempting to get "
  		       "tfm and mutex for cipher name [%s]; rc = [%d]
  ",
  		       s->cipher_string, rc);
  		goto out;
  	}
  	mutex_lock(s->tfm_mutex);
  	rc = virt_to_scatterlist(&data[(*packet_size)],
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
1050
1051
  				 s->block_aligned_filename_size, s->src_sg, 2);
  	if (rc < 1) {
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1052
1053
  		printk(KERN_ERR "%s: Internal error whilst attempting to "
  		       "convert encrypted filename memory to scatterlist; "
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
1054
1055
1056
  		       "rc = [%d]. block_aligned_filename_size = [%zd]
  ",
  		       __func__, rc, s->block_aligned_filename_size);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1057
1058
1059
1060
1061
1062
1063
  		goto out_unlock;
  	}
  	(*packet_size) += s->block_aligned_filename_size;
  	s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
  					GFP_KERNEL);
  	if (!s->decrypted_filename) {
  		printk(KERN_ERR "%s: Out of memory whilst attempting to "
a8f12864c   Michael Halcrow   eCryptfs: Fix dat...
1064
1065
  		       "kmalloc [%zd] bytes
  ", __func__,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1066
1067
1068
1069
1070
  		       s->block_aligned_filename_size);
  		rc = -ENOMEM;
  		goto out_unlock;
  	}
  	rc = virt_to_scatterlist(s->decrypted_filename,
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
1071
1072
  				 s->block_aligned_filename_size, s->dst_sg, 2);
  	if (rc < 1) {
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1073
1074
  		printk(KERN_ERR "%s: Internal error whilst attempting to "
  		       "convert decrypted filename memory to scatterlist; "
8d08dab78   Tyler Hicks   eCryptfs: Allow 2...
1075
1076
1077
  		       "rc = [%d]. block_aligned_filename_size = [%zd]
  ",
  		       __func__, rc, s->block_aligned_filename_size);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1078
1079
  		goto out_free_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
  
  	s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL);
  	if (!s->skcipher_req) {
  		printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
  		       "skcipher_request_alloc for %s
  ", __func__,
  		       crypto_skcipher_driver_name(s->skcipher_tfm));
  		rc = -ENOMEM;
  		goto out_free_unlock;
  	}
  
  	skcipher_request_set_callback(s->skcipher_req,
  				      CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1093
1094
1095
1096
  	/* The characters in the first block effectively do the job of
  	 * the IV here, so we just use 0's for the IV. Note the
  	 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
  	 * >= ECRYPTFS_MAX_IV_BYTES. */
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1097
1098
  	/* TODO: Support other key modules than passphrase for
  	 * filename encryption */
df6ad33ba   Tyler Hicks   eCryptfs: Filenam...
1099
1100
1101
1102
1103
1104
1105
  	if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
  		rc = -EOPNOTSUPP;
  		printk(KERN_INFO "%s: Filename encryption only supports "
  		       "password tokens
  ", __func__);
  		goto out_free_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1106
1107
  	rc = crypto_skcipher_setkey(
  		s->skcipher_tfm,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1108
1109
1110
1111
1112
1113
  		s->auth_tok->token.password.session_key_encryption_key,
  		mount_crypt_stat->global_default_fn_cipher_key_bytes);
  	if (rc < 0) {
  		printk(KERN_ERR "%s: Error setting key for crypto context; "
  		       "rc = [%d]. s->auth_tok->token.password.session_key_"
  		       "encryption_key = [0x%p]; mount_crypt_stat->"
df261c52a   Michael Halcrow   eCryptfs: Replace...
1114
1115
  		       "global_default_fn_cipher_key_bytes = [%zd]
  ", __func__,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1116
1117
1118
1119
1120
  		       rc,
  		       s->auth_tok->token.password.session_key_encryption_key,
  		       mount_crypt_stat->global_default_fn_cipher_key_bytes);
  		goto out_free_unlock;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1121
1122
1123
  	skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg,
  				   s->block_aligned_filename_size, s->iv);
  	rc = crypto_skcipher_decrypt(s->skcipher_req);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1124
1125
1126
1127
1128
1129
  	if (rc) {
  		printk(KERN_ERR "%s: Error attempting to decrypt filename; "
  		       "rc = [%d]
  ", __func__, rc);
  		goto out_free_unlock;
  	}
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
  	while (s->decrypted_filename[s->i] != '\0'
  	       && s->i < s->block_aligned_filename_size)
  		s->i++;
  	if (s->i == s->block_aligned_filename_size) {
  		printk(KERN_WARNING "%s: Invalid tag 70 packet; could not "
  		       "find valid separator between random characters and "
  		       "the filename
  ", __func__);
  		rc = -EINVAL;
  		goto out_free_unlock;
  	}
  	s->i++;
  	(*filename_size) = (s->block_aligned_filename_size - s->i);
  	if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) {
df261c52a   Michael Halcrow   eCryptfs: Replace...
1144
  		printk(KERN_WARNING "%s: Filename size is [%zd], which is "
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1145
1146
1147
1148
1149
1150
1151
1152
  		       "invalid
  ", __func__, (*filename_size));
  		rc = -EINVAL;
  		goto out_free_unlock;
  	}
  	(*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
  	if (!(*filename)) {
  		printk(KERN_ERR "%s: Out of memory whilst attempting to "
a8f12864c   Michael Halcrow   eCryptfs: Fix dat...
1153
1154
  		       "kmalloc [%zd] bytes
  ", __func__,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
  		       ((*filename_size) + 1));
  		rc = -ENOMEM;
  		goto out_free_unlock;
  	}
  	memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size));
  	(*filename)[(*filename_size)] = '\0';
  out_free_unlock:
  	kfree(s->decrypted_filename);
  out_unlock:
  	mutex_unlock(s->tfm_mutex);
  out:
  	if (rc) {
  		(*packet_size) = 0;
  		(*filename_size) = 0;
  		(*filename) = NULL;
  	}
b5695d046   Roberto Sassu   eCryptfs: write l...
1171
1172
  	if (auth_tok_key) {
  		up_write(&(auth_tok_key->sem));
aee683b9e   Roberto Sassu   ecryptfs: release...
1173
  		key_put(auth_tok_key);
b5695d046   Roberto Sassu   eCryptfs: write l...
1174
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1175
  	skcipher_request_free(s->skcipher_req);
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
1176
1177
1178
1179
1180
  	kfree(s);
  	return rc;
  }
  
  static int
cd9d67dfd   Michael Halcrow   eCryptfs: make ne...
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
  ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
  {
  	int rc = 0;
  
  	(*sig) = NULL;
  	switch (auth_tok->token_type) {
  	case ECRYPTFS_PASSWORD:
  		(*sig) = auth_tok->token.password.signature;
  		break;
  	case ECRYPTFS_PRIVATE_KEY:
  		(*sig) = auth_tok->token.private_key.signature;
  		break;
  	default:
  		printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]
  ",
  		       auth_tok->token_type);
  		rc = -EINVAL;
  	}
  	return rc;
  }
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1201
  /**
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1202
1203
1204
   * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
   * @auth_tok: The key authentication token used to decrypt the session key
   * @crypt_stat: The cryptographic context
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1205
   *
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1206
   * Returns zero on success; non-zero error otherwise.
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1207
   */
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1208
1209
1210
  static int
  decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
  				  struct ecryptfs_crypt_stat *crypt_stat)
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1211
  {
19e66a67e   Trevor Highland   eCryptfs: change ...
1212
  	u8 cipher_code = 0;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1213
1214
  	struct ecryptfs_msg_ctx *msg_ctx;
  	struct ecryptfs_message *msg = NULL;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1215
  	char *auth_tok_sig;
3edc8376c   Geyslan G. Bem   ecryptfs: Fix mem...
1216
  	char *payload = NULL;
fa5199648   Simon Que   eCryptfs: initial...
1217
  	size_t payload_len = 0;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1218
  	int rc;
5dda6992a   Michael Halcrow   eCryptfs: remove ...
1219
1220
  	rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
  	if (rc) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1221
1222
1223
1224
1225
1226
  		printk(KERN_ERR "Unrecognized auth tok type: [%d]
  ",
  		       auth_tok->token_type);
  		goto out;
  	}
  	rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
624ae5284   Tyler Hicks   eCryptfs: remove ...
1227
  				 &payload, &payload_len);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1228
  	if (rc) {
f66e883eb   Michael Halcrow   eCryptfs: integra...
1229
1230
  		ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet
  ");
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1231
1232
  		goto out;
  	}
624ae5284   Tyler Hicks   eCryptfs: remove ...
1233
  	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1234
  	if (rc) {
624ae5284   Tyler Hicks   eCryptfs: remove ...
1235
  		ecryptfs_printk(KERN_ERR, "Error sending message to "
290502bee   Kees Cook   eCryptfs: allow u...
1236
1237
  				"ecryptfsd: %d
  ", rc);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
  		goto out;
  	}
  	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
  				"from the user space daemon
  ");
  		rc = -EIO;
  		goto out;
  	}
  	rc = parse_tag_65_packet(&(auth_tok->session_key),
  				 &cipher_code, msg);
  	if (rc) {
  		printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]
  ",
  		       rc);
  		goto out;
  	}
  	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  	       auth_tok->session_key.decrypted_key_size);
  	crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
  	rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid
  ",
  				cipher_code)
  		goto out;
  	}
  	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  	if (ecryptfs_verbosity > 0) {
  		ecryptfs_printk(KERN_DEBUG, "Decrypted session key:
  ");
  		ecryptfs_dump_hex(crypt_stat->key,
  				  crypt_stat->key_size);
  	}
  out:
3a4674180   Tim Gardner   eCryptfs: decrypt...
1275
  	kfree(msg);
3edc8376c   Geyslan G. Bem   ecryptfs: Fix mem...
1276
  	kfree(payload);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1277
1278
1279
1280
1281
  	return rc;
  }
  
  static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
  {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1282
  	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
e0869cc14   Michael Halcrow   eCryptfs: use lis...
1283
  	struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1284

e0869cc14   Michael Halcrow   eCryptfs: use lis...
1285
1286
1287
  	list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
  				 auth_tok_list_head, list) {
  		list_del(&auth_tok_list_item->list);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1288
1289
1290
  		kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  				auth_tok_list_item);
  	}
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1291
1292
1293
  }
  
  struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1294
1295
  /**
   * parse_tag_1_packet
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1296
   * @crypt_stat: The cryptographic context to modify based on packet contents
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1297
1298
   * @data: The raw bytes of the packet.
   * @auth_tok_list: eCryptfs parses packets into authentication tokens;
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1299
1300
   *                 a new authentication token will be placed at the
   *                 end of this list for this packet.
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1301
1302
1303
1304
1305
1306
   * @new_auth_tok: Pointer to a pointer to memory that this function
   *                allocates; sets the memory address of the pointer to
   *                NULL on error. This object is added to the
   *                auth_tok_list.
   * @packet_size: This function writes the size of the parsed packet
   *               into this memory location; zero on error.
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1307
   * @max_packet_size: The maximum allowable packet size
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
   *
   * Returns zero on success; non-zero on error.
   */
  static int
  parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
  		   unsigned char *data, struct list_head *auth_tok_list,
  		   struct ecryptfs_auth_tok **new_auth_tok,
  		   size_t *packet_size, size_t max_packet_size)
  {
  	size_t body_size;
  	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  	size_t length_size;
  	int rc = 0;
  
  	(*packet_size) = 0;
  	(*new_auth_tok) = NULL;
132181796   Michael Halcrow   eCryptfs: fix Tag...
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
  	/**
  	 * This format is inspired by OpenPGP; see RFC 2440
  	 * packet tag 1
  	 *
  	 * Tag 1 identifier (1 byte)
  	 * Max Tag 1 packet size (max 3 bytes)
  	 * Version (1 byte)
  	 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
  	 * Cipher identifier (1 byte)
  	 * Encrypted key size (arbitrary)
  	 *
  	 * 12 bytes minimum packet size
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1336
  	 */
132181796   Michael Halcrow   eCryptfs: fix Tag...
1337
1338
1339
  	if (unlikely(max_packet_size < 12)) {
  		printk(KERN_ERR "Invalid max packet size; must be >=12
  ");
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1340
1341
1342
  		rc = -EINVAL;
  		goto out;
  	}
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1343
  	if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
132181796   Michael Halcrow   eCryptfs: fix Tag...
1344
1345
1346
  		printk(KERN_ERR "Enter w/ first byte != 0x%.2x
  ",
  		       ECRYPTFS_TAG_1_PACKET_TYPE);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1347
1348
1349
1350
1351
1352
  		rc = -EINVAL;
  		goto out;
  	}
  	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  	 * at end of function upon failure */
  	auth_tok_list_item =
132181796   Michael Halcrow   eCryptfs: fix Tag...
1353
1354
  		kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
  				  GFP_KERNEL);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1355
  	if (!auth_tok_list_item) {
132181796   Michael Halcrow   eCryptfs: fix Tag...
1356
1357
  		printk(KERN_ERR "Unable to allocate memory
  ");
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1358
1359
1360
  		rc = -ENOMEM;
  		goto out;
  	}
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1361
  	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
f66e883eb   Michael Halcrow   eCryptfs: integra...
1362
1363
  	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  					  &length_size);
5dda6992a   Michael Halcrow   eCryptfs: remove ...
1364
  	if (rc) {
132181796   Michael Halcrow   eCryptfs: fix Tag...
1365
1366
1367
  		printk(KERN_WARNING "Error parsing packet length; "
  		       "rc = [%d]
  ", rc);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1368
1369
  		goto out_free;
  	}
132181796   Michael Halcrow   eCryptfs: fix Tag...
1370
  	if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
81acbcd6c   Andrew Morton   ecryptfs: printk ...
1371
1372
  		printk(KERN_WARNING "Invalid body size ([%td])
  ", body_size);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1373
1374
1375
1376
1377
  		rc = -EINVAL;
  		goto out_free;
  	}
  	(*packet_size) += length_size;
  	if (unlikely((*packet_size) + body_size > max_packet_size)) {
132181796   Michael Halcrow   eCryptfs: fix Tag...
1378
1379
  		printk(KERN_WARNING "Packet size exceeds max
  ");
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1380
1381
1382
  		rc = -EINVAL;
  		goto out_free;
  	}
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1383
  	if (unlikely(data[(*packet_size)++] != 0x03)) {
132181796   Michael Halcrow   eCryptfs: fix Tag...
1384
1385
1386
  		printk(KERN_WARNING "Unknown version number [%d]
  ",
  		       data[(*packet_size) - 1]);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1387
1388
1389
  		rc = -EINVAL;
  		goto out_free;
  	}
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1390
1391
1392
1393
1394
1395
1396
  	ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
  			&data[(*packet_size)], ECRYPTFS_SIG_SIZE);
  	*packet_size += ECRYPTFS_SIG_SIZE;
  	/* This byte is skipped because the kernel does not need to
  	 * know which public key encryption algorithm was used */
  	(*packet_size)++;
  	(*new_auth_tok)->session_key.encrypted_key_size =
132181796   Michael Halcrow   eCryptfs: fix Tag...
1397
  		body_size - (ECRYPTFS_SIG_SIZE + 2);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1398
1399
  	if ((*new_auth_tok)->session_key.encrypted_key_size
  	    > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
132181796   Michael Halcrow   eCryptfs: fix Tag...
1400
1401
  		printk(KERN_WARNING "Tag 1 packet contains key larger "
  		       "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1402
1403
1404
  		rc = -EINVAL;
  		goto out;
  	}
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1405
  	memcpy((*new_auth_tok)->session_key.encrypted_key,
132181796   Michael Halcrow   eCryptfs: fix Tag...
1406
  	       &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1407
1408
1409
1410
1411
1412
  	(*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
  	(*new_auth_tok)->session_key.flags &=
  		~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  	(*new_auth_tok)->session_key.flags |=
  		ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  	(*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
132181796   Michael Halcrow   eCryptfs: fix Tag...
1413
  	(*new_auth_tok)->flags = 0;
e2bd99ec5   Michael Halcrow   [PATCH] eCryptfs:...
1414
1415
1416
1417
  	(*new_auth_tok)->session_key.flags &=
  		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  	(*new_auth_tok)->session_key.flags &=
  		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
  	list_add(&auth_tok_list_item->list, auth_tok_list);
  	goto out;
  out_free:
  	(*new_auth_tok) = NULL;
  	memset(auth_tok_list_item, 0,
  	       sizeof(struct ecryptfs_auth_tok_list_item));
  	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  			auth_tok_list_item);
  out:
  	if (rc)
  		(*packet_size) = 0;
  	return rc;
  }
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
  /**
   * parse_tag_3_packet
   * @crypt_stat: The cryptographic context to modify based on packet
   *              contents.
   * @data: The raw bytes of the packet.
   * @auth_tok_list: eCryptfs parses packets into authentication tokens;
   *                 a new authentication token will be placed at the end
   *                 of this list for this packet.
   * @new_auth_tok: Pointer to a pointer to memory that this function
   *                allocates; sets the memory address of the pointer to
   *                NULL on error. This object is added to the
   *                auth_tok_list.
   * @packet_size: This function writes the size of the parsed packet
   *               into this memory location; zero on error.
   * @max_packet_size: maximum number of bytes to parse
   *
   * Returns zero on success; non-zero on error.
   */
  static int
  parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
  		   unsigned char *data, struct list_head *auth_tok_list,
  		   struct ecryptfs_auth_tok **new_auth_tok,
  		   size_t *packet_size, size_t max_packet_size)
  {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1455
1456
1457
  	size_t body_size;
  	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  	size_t length_size;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1458
  	int rc = 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1459
1460
1461
  
  	(*packet_size) = 0;
  	(*new_auth_tok) = NULL;
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
  	/**
  	 *This format is inspired by OpenPGP; see RFC 2440
  	 * packet tag 3
  	 *
  	 * Tag 3 identifier (1 byte)
  	 * Max Tag 3 packet size (max 3 bytes)
  	 * Version (1 byte)
  	 * Cipher code (1 byte)
  	 * S2K specifier (1 byte)
  	 * Hash identifier (1 byte)
  	 * Salt (ECRYPTFS_SALT_SIZE)
  	 * Hash iterations (1 byte)
  	 * Encrypted key (arbitrary)
  	 *
  	 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1477
  	 */
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1478
1479
1480
  	if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
  		printk(KERN_ERR "Max packet size too large
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1481
1482
1483
  		rc = -EINVAL;
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1484
  	if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1485
1486
1487
  		printk(KERN_ERR "First byte != 0x%.2x; invalid packet
  ",
  		       ECRYPTFS_TAG_3_PACKET_TYPE);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1488
1489
1490
1491
1492
1493
  		rc = -EINVAL;
  		goto out;
  	}
  	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  	 * at end of function upon failure */
  	auth_tok_list_item =
c37622296   Robert P. J. Day   [PATCH] Transform...
1494
  	    kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1495
  	if (!auth_tok_list_item) {
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1496
1497
  		printk(KERN_ERR "Unable to allocate memory
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1498
1499
1500
  		rc = -ENOMEM;
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1501
  	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
f66e883eb   Michael Halcrow   eCryptfs: integra...
1502
1503
  	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  					  &length_size);
5dda6992a   Michael Halcrow   eCryptfs: remove ...
1504
  	if (rc) {
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1505
1506
1507
  		printk(KERN_WARNING "Error parsing packet length; rc = [%d]
  ",
  		       rc);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1508
1509
  		goto out_free;
  	}
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1510
  	if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
81acbcd6c   Andrew Morton   ecryptfs: printk ...
1511
1512
  		printk(KERN_WARNING "Invalid body size ([%td])
  ", body_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1513
1514
1515
1516
  		rc = -EINVAL;
  		goto out_free;
  	}
  	(*packet_size) += length_size;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1517
  	if (unlikely((*packet_size) + body_size > max_packet_size)) {
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1518
1519
  		printk(KERN_ERR "Packet size exceeds max
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1520
1521
1522
  		rc = -EINVAL;
  		goto out_free;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1523
  	(*new_auth_tok)->session_key.encrypted_key_size =
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1524
  		(body_size - (ECRYPTFS_SALT_SIZE + 5));
f151cd2c5   Ramon de Carvalho Valle   eCryptfs: parse_t...
1525
1526
1527
1528
1529
1530
1531
1532
  	if ((*new_auth_tok)->session_key.encrypted_key_size
  	    > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
  		printk(KERN_WARNING "Tag 3 packet contains key larger "
  		       "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
  ");
  		rc = -EINVAL;
  		goto out_free;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1533
  	if (unlikely(data[(*packet_size)++] != 0x04)) {
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1534
1535
1536
  		printk(KERN_WARNING "Unknown version number [%d]
  ",
  		       data[(*packet_size) - 1]);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1537
1538
1539
  		rc = -EINVAL;
  		goto out_free;
  	}
b0105eaef   Tyler Hicks   eCryptfs: Handle ...
1540
1541
1542
1543
  	rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
  					    (u16)data[(*packet_size)]);
  	if (rc)
  		goto out_free;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
  	/* A little extra work to differentiate among the AES key
  	 * sizes; see RFC2440 */
  	switch(data[(*packet_size)++]) {
  	case RFC2440_CIPHER_AES_192:
  		crypt_stat->key_size = 24;
  		break;
  	default:
  		crypt_stat->key_size =
  			(*new_auth_tok)->session_key.encrypted_key_size;
  	}
b0105eaef   Tyler Hicks   eCryptfs: Handle ...
1554
1555
1556
  	rc = ecryptfs_init_crypt_ctx(crypt_stat);
  	if (rc)
  		goto out_free;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1557
  	if (unlikely(data[(*packet_size)++] != 0x03)) {
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1558
1559
  		printk(KERN_WARNING "Only S2K ID 3 is currently supported
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1560
1561
1562
  		rc = -ENOSYS;
  		goto out_free;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1563
  	/* TODO: finish the hash mapping */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1564
1565
1566
  	switch (data[(*packet_size)++]) {
  	case 0x01: /* See RFC2440 for these numbers and their mappings */
  		/* Choose MD5 */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1567
1568
1569
  		memcpy((*new_auth_tok)->token.password.salt,
  		       &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
  		(*packet_size) += ECRYPTFS_SALT_SIZE;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1570
  		/* This conversion was taken straight from RFC2440 */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1571
1572
1573
1574
  		(*new_auth_tok)->token.password.hash_iterations =
  			((u32) 16 + (data[(*packet_size)] & 15))
  				<< ((data[(*packet_size)] >> 4) + 6);
  		(*packet_size)++;
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1575
1576
1577
  		/* Friendly reminder:
  		 * (*new_auth_tok)->session_key.encrypted_key_size =
  		 *         (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1578
1579
1580
1581
1582
1583
1584
1585
1586
  		memcpy((*new_auth_tok)->session_key.encrypted_key,
  		       &data[(*packet_size)],
  		       (*new_auth_tok)->session_key.encrypted_key_size);
  		(*packet_size) +=
  			(*new_auth_tok)->session_key.encrypted_key_size;
  		(*new_auth_tok)->session_key.flags &=
  			~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  		(*new_auth_tok)->session_key.flags |=
  			ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
c59becfce   Michael Halcrow   eCryptfs: fix Tag...
1587
  		(*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
  		break;
  	default:
  		ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
  				"[%d]
  ", data[(*packet_size) - 1]);
  		rc = -ENOSYS;
  		goto out_free;
  	}
  	(*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
  	/* TODO: Parametarize; we might actually want userspace to
  	 * decrypt the session key. */
e2bd99ec5   Michael Halcrow   [PATCH] eCryptfs:...
1599
1600
1601
1602
  	(*new_auth_tok)->session_key.flags &=
  			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  	(*new_auth_tok)->session_key.flags &=
  			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
  	list_add(&auth_tok_list_item->list, auth_tok_list);
  	goto out;
  out_free:
  	(*new_auth_tok) = NULL;
  	memset(auth_tok_list_item, 0,
  	       sizeof(struct ecryptfs_auth_tok_list_item));
  	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  			auth_tok_list_item);
  out:
  	if (rc)
  		(*packet_size) = 0;
  	return rc;
  }
  
  /**
   * parse_tag_11_packet
   * @data: The raw bytes of the packet
   * @contents: This function writes the data contents of the literal
   *            packet into this memory location
   * @max_contents_bytes: The maximum number of bytes that this function
   *                      is allowed to write into contents
   * @tag_11_contents_size: This function writes the size of the parsed
   *                        contents into this memory location; zero on
   *                        error
   * @packet_size: This function writes the size of the parsed packet
   *               into this memory location; zero on error
   * @max_packet_size: maximum number of bytes to parse
   *
   * Returns zero on success; non-zero on error.
   */
  static int
  parse_tag_11_packet(unsigned char *data, unsigned char *contents,
  		    size_t max_contents_bytes, size_t *tag_11_contents_size,
  		    size_t *packet_size, size_t max_packet_size)
  {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1638
1639
  	size_t body_size;
  	size_t length_size;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1640
  	int rc = 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1641
1642
1643
  
  	(*packet_size) = 0;
  	(*tag_11_contents_size) = 0;
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
  	/* This format is inspired by OpenPGP; see RFC 2440
  	 * packet tag 11
  	 *
  	 * Tag 11 identifier (1 byte)
  	 * Max Tag 11 packet size (max 3 bytes)
  	 * Binary format specifier (1 byte)
  	 * Filename length (1 byte)
  	 * Filename ("_CONSOLE") (8 bytes)
  	 * Modification date (4 bytes)
  	 * Literal data (arbitrary)
  	 *
  	 * We need at least 16 bytes of data for the packet to even be
  	 * valid.
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1657
  	 */
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1658
1659
1660
  	if (max_packet_size < 16) {
  		printk(KERN_ERR "Maximum packet size too small
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1661
1662
1663
  		rc = -EINVAL;
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1664
  	if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1665
1666
  		printk(KERN_WARNING "Invalid tag 11 packet format
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1667
1668
1669
  		rc = -EINVAL;
  		goto out;
  	}
f66e883eb   Michael Halcrow   eCryptfs: integra...
1670
1671
  	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  					  &length_size);
5dda6992a   Michael Halcrow   eCryptfs: remove ...
1672
  	if (rc) {
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1673
1674
  		printk(KERN_WARNING "Invalid tag 11 packet format
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1675
1676
  		goto out;
  	}
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1677
  	if (body_size < 14) {
81acbcd6c   Andrew Morton   ecryptfs: printk ...
1678
1679
  		printk(KERN_WARNING "Invalid body size ([%td])
  ", body_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1680
1681
1682
  		rc = -EINVAL;
  		goto out;
  	}
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1683
1684
  	(*packet_size) += length_size;
  	(*tag_11_contents_size) = (body_size - 14);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1685
  	if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1686
1687
  		printk(KERN_ERR "Packet size exceeds max
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1688
1689
1690
  		rc = -EINVAL;
  		goto out;
  	}
6352a2930   Tyler Hicks   eCryptfs: Check T...
1691
1692
1693
1694
1695
1696
1697
  	if (unlikely((*tag_11_contents_size) > max_contents_bytes)) {
  		printk(KERN_ERR "Literal data section in tag 11 packet exceeds "
  		       "expected size
  ");
  		rc = -EINVAL;
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1698
  	if (data[(*packet_size)++] != 0x62) {
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1699
1700
  		printk(KERN_WARNING "Unrecognizable packet
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1701
1702
1703
  		rc = -EINVAL;
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1704
  	if (data[(*packet_size)++] != 0x08) {
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1705
1706
  		printk(KERN_WARNING "Unrecognizable packet
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1707
1708
1709
  		rc = -EINVAL;
  		goto out;
  	}
f648104a0   Michael Halcrow   eCryptfs: fix Tag...
1710
  	(*packet_size) += 12; /* Ignore filename and modification date */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1711
1712
  	memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
  	(*packet_size) += (*tag_11_contents_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1713
1714
1715
1716
1717
1718
1719
  out:
  	if (rc) {
  		(*packet_size) = 0;
  		(*tag_11_contents_size) = 0;
  	}
  	return rc;
  }
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1720
1721
1722
1723
1724
1725
1726
1727
  int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
  				      struct ecryptfs_auth_tok **auth_tok,
  				      char *sig)
  {
  	int rc = 0;
  
  	(*auth_tok_key) = request_key(&key_type_user, sig, NULL);
  	if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
1252cc3b2   Roberto Sassu   eCryptfs: added s...
1728
1729
1730
1731
1732
1733
1734
1735
1736
  		(*auth_tok_key) = ecryptfs_get_encrypted_key(sig);
  		if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
  			printk(KERN_ERR "Could not find key with description: [%s]
  ",
  			      sig);
  			rc = process_request_key_err(PTR_ERR(*auth_tok_key));
  			(*auth_tok_key) = NULL;
  			goto out;
  		}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1737
  	}
b5695d046   Roberto Sassu   eCryptfs: write l...
1738
  	down_write(&(*auth_tok_key)->sem);
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
1739
  	rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok);
aee683b9e   Roberto Sassu   ecryptfs: release...
1740
  	if (rc) {
b5695d046   Roberto Sassu   eCryptfs: write l...
1741
  		up_write(&(*auth_tok_key)->sem);
aee683b9e   Roberto Sassu   ecryptfs: release...
1742
1743
  		key_put(*auth_tok_key);
  		(*auth_tok_key) = NULL;
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
1744
  		goto out;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1745
1746
1747
1748
1749
1750
  	}
  out:
  	return rc;
  }
  
  /**
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1751
1752
1753
   * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
   * @auth_tok: The passphrase authentication token to use to encrypt the FEK
   * @crypt_stat: The cryptographic context
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1754
   *
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1755
   * Returns zero on success; non-zero error otherwise
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1756
   */
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1757
1758
1759
  static int
  decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
  					 struct ecryptfs_crypt_stat *crypt_stat)
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1760
  {
ac97b9f9a   Michael Halcrow   eCryptfs: Allocat...
1761
1762
  	struct scatterlist dst_sg[2];
  	struct scatterlist src_sg[2];
dd8e2902d   Michael Halcrow   eCryptfs: remove ...
1763
  	struct mutex *tfm_mutex;
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1764
1765
  	struct crypto_skcipher *tfm;
  	struct skcipher_request *req = NULL;
8bba066f4   Michael Halcrow   [PATCH] eCryptfs:...
1766
  	int rc = 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1767

f4aad16ad   Michael Halcrow   eCryptfs: add key...
1768
1769
1770
1771
1772
1773
1774
1775
1776
  	if (unlikely(ecryptfs_verbosity > 0)) {
  		ecryptfs_printk(
  			KERN_DEBUG, "Session key encryption key (size [%d]):
  ",
  			auth_tok->token.password.session_key_encryption_key_bytes);
  		ecryptfs_dump_hex(
  			auth_tok->token.password.session_key_encryption_key,
  			auth_tok->token.password.session_key_encryption_key_bytes);
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1777
  	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1778
1779
1780
1781
1782
1783
1784
  							crypt_stat->cipher);
  	if (unlikely(rc)) {
  		printk(KERN_ERR "Internal error whilst attempting to get "
  		       "tfm and mutex for cipher name [%s]; rc = [%d]
  ",
  		       crypt_stat->cipher, rc);
  		goto out;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1785
  	}
5dda6992a   Michael Halcrow   eCryptfs: remove ...
1786
1787
  	rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
  				 auth_tok->session_key.encrypted_key_size,
ac97b9f9a   Michael Halcrow   eCryptfs: Allocat...
1788
1789
  				 src_sg, 2);
  	if (rc < 1 || rc > 2) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
  		printk(KERN_ERR "Internal error whilst attempting to convert "
  			"auth_tok->session_key.encrypted_key to scatterlist; "
  			"expected rc = 1; got rc = [%d]. "
  		       "auth_tok->session_key.encrypted_key_size = [%d]
  ", rc,
  			auth_tok->session_key.encrypted_key_size);
  		goto out;
  	}
  	auth_tok->session_key.decrypted_key_size =
  		auth_tok->session_key.encrypted_key_size;
5dda6992a   Michael Halcrow   eCryptfs: remove ...
1800
1801
  	rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
  				 auth_tok->session_key.decrypted_key_size,
ac97b9f9a   Michael Halcrow   eCryptfs: Allocat...
1802
1803
  				 dst_sg, 2);
  	if (rc < 1 || rc > 2) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1804
1805
1806
1807
1808
1809
1810
  		printk(KERN_ERR "Internal error whilst attempting to convert "
  			"auth_tok->session_key.decrypted_key to scatterlist; "
  			"expected rc = 1; got rc = [%d]
  ", rc);
  		goto out;
  	}
  	mutex_lock(tfm_mutex);
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
  	req = skcipher_request_alloc(tfm, GFP_KERNEL);
  	if (!req) {
  		mutex_unlock(tfm_mutex);
  		printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
  		       "skcipher_request_alloc for %s
  ", __func__,
  		       crypto_skcipher_driver_name(tfm));
  		rc = -ENOMEM;
  		goto out;
  	}
  
  	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  				      NULL, NULL);
  	rc = crypto_skcipher_setkey(
  		tfm, auth_tok->token.password.session_key_encryption_key,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1826
1827
1828
  		crypt_stat->key_size);
  	if (unlikely(rc < 0)) {
  		mutex_unlock(tfm_mutex);
e5d9cbde6   Michael Halcrow   [PATCH] eCryptfs:...
1829
1830
1831
  		printk(KERN_ERR "Error setting key for crypto context
  ");
  		rc = -EINVAL;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1832
  		goto out;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1833
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1834
1835
1836
1837
  	skcipher_request_set_crypt(req, src_sg, dst_sg,
  				   auth_tok->session_key.encrypted_key_size,
  				   NULL);
  	rc = crypto_skcipher_decrypt(req);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1838
1839
  	mutex_unlock(tfm_mutex);
  	if (unlikely(rc)) {
8bba066f4   Michael Halcrow   [PATCH] eCryptfs:...
1840
1841
  		printk(KERN_ERR "Error decrypting; rc = [%d]
  ", rc);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1842
  		goto out;
8bba066f4   Michael Halcrow   [PATCH] eCryptfs:...
1843
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1844
1845
1846
  	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  	       auth_tok->session_key.decrypted_key_size);
e2bd99ec5   Michael Halcrow   [PATCH] eCryptfs:...
1847
  	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1848
  	if (unlikely(ecryptfs_verbosity > 0)) {
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
1849
1850
  		ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:
  ",
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1851
  				crypt_stat->key_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1852
1853
  		ecryptfs_dump_hex(crypt_stat->key,
  				  crypt_stat->key_size);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1854
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1855
  out:
3095e8e36   Herbert Xu   eCryptfs: Use skc...
1856
  	skcipher_request_free(req);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1857
1858
1859
1860
1861
  	return rc;
  }
  
  /**
   * ecryptfs_parse_packet_set
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
1862
1863
1864
   * @crypt_stat: The cryptographic context
   * @src: Virtual address of region of memory containing the packets
   * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
   *
   * Get crypt_stat to have the file's session key if the requisite key
   * is available to decrypt the session key.
   *
   * Returns Zero if a valid authentication token was retrieved and
   * processed; negative value for file not encrypted or for error
   * conditions.
   */
  int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  			      unsigned char *src,
  			      struct dentry *ecryptfs_dentry)
  {
  	size_t i = 0;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1878
  	size_t found_auth_tok;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1879
  	size_t next_packet_is_auth_tok_packet;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1880
  	struct list_head auth_tok_list;
dd8e2902d   Michael Halcrow   eCryptfs: remove ...
1881
1882
  	struct ecryptfs_auth_tok *matching_auth_tok;
  	struct ecryptfs_auth_tok *candidate_auth_tok;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1883
  	char *candidate_auth_tok_sig;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1884
1885
1886
  	size_t packet_size;
  	struct ecryptfs_auth_tok *new_auth_tok;
  	unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1887
  	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1888
1889
  	size_t tag_11_contents_size;
  	size_t tag_11_packet_size;
aee683b9e   Roberto Sassu   ecryptfs: release...
1890
  	struct key *auth_tok_key = NULL;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1891
  	int rc = 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1892
1893
  
  	INIT_LIST_HEAD(&auth_tok_list);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1894
  	/* Parse the header to find as many packets as we can; these will be
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1895
1896
1897
  	 * added the our &auth_tok_list */
  	next_packet_is_auth_tok_packet = 1;
  	while (next_packet_is_auth_tok_packet) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
1898
  		size_t max_packet_size = ((PAGE_SIZE - 8) - i);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
  
  		switch (src[i]) {
  		case ECRYPTFS_TAG_3_PACKET_TYPE:
  			rc = parse_tag_3_packet(crypt_stat,
  						(unsigned char *)&src[i],
  						&auth_tok_list, &new_auth_tok,
  						&packet_size, max_packet_size);
  			if (rc) {
  				ecryptfs_printk(KERN_ERR, "Error parsing "
  						"tag 3 packet
  ");
  				rc = -EIO;
  				goto out_wipe_list;
  			}
  			i += packet_size;
  			rc = parse_tag_11_packet((unsigned char *)&src[i],
  						 sig_tmp_space,
  						 ECRYPTFS_SIG_SIZE,
  						 &tag_11_contents_size,
  						 &tag_11_packet_size,
  						 max_packet_size);
  			if (rc) {
  				ecryptfs_printk(KERN_ERR, "No valid "
  						"(ecryptfs-specific) literal "
  						"packet containing "
  						"authentication token "
  						"signature found after "
  						"tag 3 packet
  ");
  				rc = -EIO;
  				goto out_wipe_list;
  			}
  			i += tag_11_packet_size;
  			if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  				ecryptfs_printk(KERN_ERR, "Expected "
  						"signature of size [%d]; "
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
1935
1936
  						"read size [%zd]
  ",
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1937
1938
1939
1940
1941
1942
1943
1944
1945
  						ECRYPTFS_SIG_SIZE,
  						tag_11_contents_size);
  				rc = -EIO;
  				goto out_wipe_list;
  			}
  			ecryptfs_to_hex(new_auth_tok->token.password.signature,
  					sig_tmp_space, tag_11_contents_size);
  			new_auth_tok->token.password.signature[
  				ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
e2bd99ec5   Michael Halcrow   [PATCH] eCryptfs:...
1946
  			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1947
  			break;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
  		case ECRYPTFS_TAG_1_PACKET_TYPE:
  			rc = parse_tag_1_packet(crypt_stat,
  						(unsigned char *)&src[i],
  						&auth_tok_list, &new_auth_tok,
  						&packet_size, max_packet_size);
  			if (rc) {
  				ecryptfs_printk(KERN_ERR, "Error parsing "
  						"tag 1 packet
  ");
  				rc = -EIO;
  				goto out_wipe_list;
  			}
  			i += packet_size;
e2bd99ec5   Michael Halcrow   [PATCH] eCryptfs:...
1961
  			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
1962
  			break;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1963
1964
1965
1966
1967
1968
  		case ECRYPTFS_TAG_11_PACKET_TYPE:
  			ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  					"(Tag 11 not allowed by itself)
  ");
  			rc = -EIO;
  			goto out_wipe_list;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1969
  		default:
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
1970
1971
  			ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] "
  					"of the file header; hex value of "
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1972
1973
1974
1975
1976
1977
  					"character is [0x%.2x]
  ", i, src[i]);
  			next_packet_is_auth_tok_packet = 0;
  		}
  	}
  	if (list_empty(&auth_tok_list)) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1978
1979
1980
1981
1982
  		printk(KERN_ERR "The lower file appears to be a non-encrypted "
  		       "eCryptfs file; this is not supported in this version "
  		       "of the eCryptfs kernel module
  ");
  		rc = -EINVAL;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1983
1984
  		goto out;
  	}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
1985
1986
1987
1988
1989
1990
1991
1992
1993
  	/* auth_tok_list contains the set of authentication tokens
  	 * parsed from the metadata. We need to find a matching
  	 * authentication token that has the secret component(s)
  	 * necessary to decrypt the EFEK in the auth_tok parsed from
  	 * the metadata. There may be several potential matches, but
  	 * just one will be sufficient to decrypt to get the FEK. */
  find_next_matching_auth_tok:
  	found_auth_tok = 0;
  	list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
1994
1995
1996
1997
1998
1999
2000
  		candidate_auth_tok = &auth_tok_list_item->auth_tok;
  		if (unlikely(ecryptfs_verbosity > 0)) {
  			ecryptfs_printk(KERN_DEBUG,
  					"Considering cadidate auth tok:
  ");
  			ecryptfs_dump_auth_tok(candidate_auth_tok);
  		}
5dda6992a   Michael Halcrow   eCryptfs: remove ...
2001
2002
2003
  		rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
  					       candidate_auth_tok);
  		if (rc) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2004
2005
2006
2007
2008
2009
2010
  			printk(KERN_ERR
  			       "Unrecognized candidate auth tok type: [%d]
  ",
  			       candidate_auth_tok->token_type);
  			rc = -EINVAL;
  			goto out_wipe_list;
  		}
39fac853a   Roberto Sassu   ecryptfs: checkin...
2011
  		rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
aee683b9e   Roberto Sassu   ecryptfs: release...
2012
  					       &matching_auth_tok,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
2013
  					       crypt_stat->mount_crypt_stat,
5dda6992a   Michael Halcrow   eCryptfs: remove ...
2014
  					       candidate_auth_tok_sig);
39fac853a   Roberto Sassu   ecryptfs: checkin...
2015
  		if (!rc) {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2016
  			found_auth_tok = 1;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2017
  			goto found_matching_auth_tok;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2018
2019
  		}
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2020
  	if (!found_auth_tok) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2021
2022
2023
  		ecryptfs_printk(KERN_ERR, "Could not find a usable "
  				"authentication token
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2024
2025
  		rc = -EIO;
  		goto out_wipe_list;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2026
  	}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2027
  found_matching_auth_tok:
e2bd99ec5   Michael Halcrow   [PATCH] eCryptfs:...
2028
  	if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2029
  		memcpy(&(candidate_auth_tok->token.private_key),
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2030
  		       &(matching_auth_tok->token.private_key),
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2031
  		       sizeof(struct ecryptfs_private_key));
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2032
2033
  		up_write(&(auth_tok_key->sem));
  		key_put(auth_tok_key);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2034
  		rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2035
2036
  						       crypt_stat);
  	} else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2037
  		memcpy(&(candidate_auth_tok->token.password),
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2038
  		       &(matching_auth_tok->token.password),
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2039
  		       sizeof(struct ecryptfs_password));
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2040
2041
  		up_write(&(auth_tok_key->sem));
  		key_put(auth_tok_key);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2042
2043
  		rc = decrypt_passphrase_encrypted_session_key(
  			candidate_auth_tok, crypt_stat);
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2044
2045
2046
2047
  	} else {
  		up_write(&(auth_tok_key->sem));
  		key_put(auth_tok_key);
  		rc = -EINVAL;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2048
2049
  	}
  	if (rc) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2050
2051
2052
2053
2054
2055
  		struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
  
  		ecryptfs_printk(KERN_WARNING, "Error decrypting the "
  				"session key for authentication token with sig "
  				"[%.*s]; rc = [%d]. Removing auth tok "
  				"candidate from the list and searching for "
888d57bbc   Joe Perches   fs/ecryptfs: Add ...
2056
2057
2058
  				"the next match.
  ", ECRYPTFS_SIG_SIZE_HEX,
  				candidate_auth_tok_sig,	rc);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
  		list_for_each_entry_safe(auth_tok_list_item,
  					 auth_tok_list_item_tmp,
  					 &auth_tok_list, list) {
  			if (candidate_auth_tok
  			    == &auth_tok_list_item->auth_tok) {
  				list_del(&auth_tok_list_item->list);
  				kmem_cache_free(
  					ecryptfs_auth_tok_list_item_cache,
  					auth_tok_list_item);
  				goto find_next_matching_auth_tok;
  			}
  		}
  		BUG();
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2072
2073
2074
2075
2076
2077
2078
  	}
  	rc = ecryptfs_compute_root_iv(crypt_stat);
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error computing "
  				"the root IV
  ");
  		goto out_wipe_list;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
  	}
  	rc = ecryptfs_init_crypt_ctx(crypt_stat);
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  				"context for cipher [%s]; rc = [%d]
  ",
  				crypt_stat->cipher, rc);
  	}
  out_wipe_list:
  	wipe_auth_tok_list(&auth_tok_list);
  out:
  	return rc;
  }
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2092

dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2093
  static int
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2094
2095
  pki_encrypt_session_key(struct key *auth_tok_key,
  			struct ecryptfs_auth_tok *auth_tok,
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2096
2097
2098
2099
  			struct ecryptfs_crypt_stat *crypt_stat,
  			struct ecryptfs_key_record *key_rec)
  {
  	struct ecryptfs_msg_ctx *msg_ctx = NULL;
624ae5284   Tyler Hicks   eCryptfs: remove ...
2100
  	char *payload = NULL;
99b373ff2   Tyler Hicks   eCryptfs: Fix pay...
2101
  	size_t payload_len = 0;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2102
2103
2104
2105
  	struct ecryptfs_message *msg;
  	int rc;
  
  	rc = write_tag_66_packet(auth_tok->token.private_key.signature,
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
2106
2107
2108
  				 ecryptfs_code_for_cipher_string(
  					 crypt_stat->cipher,
  					 crypt_stat->key_size),
624ae5284   Tyler Hicks   eCryptfs: remove ...
2109
  				 crypt_stat, &payload, &payload_len);
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2110
2111
  	up_write(&(auth_tok_key->sem));
  	key_put(auth_tok_key);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2112
2113
2114
2115
2116
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet
  ");
  		goto out;
  	}
624ae5284   Tyler Hicks   eCryptfs: remove ...
2117
  	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2118
  	if (rc) {
624ae5284   Tyler Hicks   eCryptfs: remove ...
2119
  		ecryptfs_printk(KERN_ERR, "Error sending message to "
290502bee   Kees Cook   eCryptfs: allow u...
2120
2121
  				"ecryptfsd: %d
  ", rc);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
  		goto out;
  	}
  	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
  				"from the user space daemon
  ");
  		rc = -EIO;
  		goto out;
  	}
  	rc = parse_tag_67_packet(key_rec, msg);
  	if (rc)
  		ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet
  ");
  	kfree(msg);
  out:
624ae5284   Tyler Hicks   eCryptfs: remove ...
2138
  	kfree(payload);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2139
2140
2141
2142
2143
  	return rc;
  }
  /**
   * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
   * @dest: Buffer into which to write the packet
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
2144
   * @remaining_bytes: Maximum number of bytes that can be writtn
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2145
2146
   * @auth_tok_key: The authentication token key to unlock and put when done with
   *                @auth_tok
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
2147
2148
2149
   * @auth_tok: The authentication token used for generating the tag 1 packet
   * @crypt_stat: The cryptographic context
   * @key_rec: The key record struct for the tag 1 packet
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2150
2151
2152
2153
2154
2155
   * @packet_size: This function will write the number of bytes that end
   *               up constituting the packet; set to zero on error
   *
   * Returns zero on success; non-zero on error.
   */
  static int
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2156
  write_tag_1_packet(char *dest, size_t *remaining_bytes,
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2157
  		   struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok,
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2158
  		   struct ecryptfs_crypt_stat *crypt_stat,
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2159
2160
2161
2162
  		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
  {
  	size_t i;
  	size_t encrypted_session_key_valid = 0;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2163
  	size_t packet_size_length;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2164
  	size_t max_packet_size;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
  	int rc = 0;
  
  	(*packet_size) = 0;
  	ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
  			  ECRYPTFS_SIG_SIZE);
  	encrypted_session_key_valid = 0;
  	for (i = 0; i < crypt_stat->key_size; i++)
  		encrypted_session_key_valid |=
  			auth_tok->session_key.encrypted_key[i];
  	if (encrypted_session_key_valid) {
  		memcpy(key_rec->enc_key,
  		       auth_tok->session_key.encrypted_key,
  		       auth_tok->session_key.encrypted_key_size);
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2178
2179
  		up_write(&(auth_tok_key->sem));
  		key_put(auth_tok_key);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2180
2181
2182
2183
2184
  		goto encrypted_session_key_set;
  	}
  	if (auth_tok->session_key.encrypted_key_size == 0)
  		auth_tok->session_key.encrypted_key_size =
  			auth_tok->token.private_key.key_size;
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2185
2186
  	rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat,
  				     key_rec);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2187
  	if (rc) {
f66e883eb   Michael Halcrow   eCryptfs: integra...
2188
2189
2190
  		printk(KERN_ERR "Failed to encrypt session key via a key "
  		       "module; rc = [%d]
  ", rc);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2191
2192
2193
2194
2195
2196
2197
2198
  		goto out;
  	}
  	if (ecryptfs_verbosity > 0) {
  		ecryptfs_printk(KERN_DEBUG, "Encrypted key:
  ");
  		ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
  	}
  encrypted_session_key_set:
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
  	/* This format is inspired by OpenPGP; see RFC 2440
  	 * packet tag 1 */
  	max_packet_size = (1                         /* Tag 1 identifier */
  			   + 3                       /* Max Tag 1 packet size */
  			   + 1                       /* Version */
  			   + ECRYPTFS_SIG_SIZE       /* Key identifier */
  			   + 1                       /* Cipher identifier */
  			   + key_rec->enc_key_size); /* Encrypted key size */
  	if (max_packet_size > (*remaining_bytes)) {
  		printk(KERN_ERR "Packet length larger than maximum allowable; "
81acbcd6c   Andrew Morton   ecryptfs: printk ...
2209
  		       "need up to [%td] bytes, but there are only [%td] "
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2210
2211
  		       "available
  ", max_packet_size, (*remaining_bytes));
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2212
2213
2214
2215
  		rc = -EINVAL;
  		goto out;
  	}
  	dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
f66e883eb   Michael Halcrow   eCryptfs: integra...
2216
2217
2218
  	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  					  (max_packet_size - 4),
  					  &packet_size_length);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
  	if (rc) {
  		ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
  				"header; cannot generate packet length
  ");
  		goto out;
  	}
  	(*packet_size) += packet_size_length;
  	dest[(*packet_size)++] = 0x03; /* version 3 */
  	memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
  	(*packet_size) += ECRYPTFS_SIG_SIZE;
  	dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
  	memcpy(&dest[(*packet_size)], key_rec->enc_key,
  	       key_rec->enc_key_size);
  	(*packet_size) += key_rec->enc_key_size;
  out:
  	if (rc)
  		(*packet_size) = 0;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2236
2237
  	else
  		(*remaining_bytes) -= (*packet_size);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2238
2239
  	return rc;
  }
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2240
2241
2242
2243
  
  /**
   * write_tag_11_packet
   * @dest: Target into which Tag 11 packet is to be written
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
2244
   * @remaining_bytes: Maximum packet length
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2245
2246
2247
2248
2249
2250
2251
   * @contents: Byte array of contents to copy in
   * @contents_length: Number of bytes in contents
   * @packet_length: Length of the Tag 11 packet written; zero on error
   *
   * Returns zero on success; non-zero on error.
   */
  static int
81acbcd6c   Andrew Morton   ecryptfs: printk ...
2252
  write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
146a46063   Michael Halcrow   eCryptfs: fix Tag...
2253
  		    size_t contents_length, size_t *packet_length)
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2254
  {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2255
  	size_t packet_size_length;
146a46063   Michael Halcrow   eCryptfs: fix Tag...
2256
  	size_t max_packet_size;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2257
  	int rc = 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2258
2259
  
  	(*packet_length) = 0;
146a46063   Michael Halcrow   eCryptfs: fix Tag...
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
  	/* This format is inspired by OpenPGP; see RFC 2440
  	 * packet tag 11 */
  	max_packet_size = (1                   /* Tag 11 identifier */
  			   + 3                 /* Max Tag 11 packet size */
  			   + 1                 /* Binary format specifier */
  			   + 1                 /* Filename length */
  			   + 8                 /* Filename ("_CONSOLE") */
  			   + 4                 /* Modification date */
  			   + contents_length); /* Literal data */
  	if (max_packet_size > (*remaining_bytes)) {
  		printk(KERN_ERR "Packet length larger than maximum allowable; "
81acbcd6c   Andrew Morton   ecryptfs: printk ...
2271
  		       "need up to [%td] bytes, but there are only [%td] "
146a46063   Michael Halcrow   eCryptfs: fix Tag...
2272
2273
  		       "available
  ", max_packet_size, (*remaining_bytes));
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2274
  		rc = -EINVAL;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2275
2276
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2277
  	dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
f66e883eb   Michael Halcrow   eCryptfs: integra...
2278
2279
2280
  	rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
  					  (max_packet_size - 4),
  					  &packet_size_length);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2281
  	if (rc) {
146a46063   Michael Halcrow   eCryptfs: fix Tag...
2282
2283
2284
  		printk(KERN_ERR "Error generating tag 11 packet header; cannot "
  		       "generate packet length. rc = [%d]
  ", rc);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2285
2286
2287
  		goto out;
  	}
  	(*packet_length) += packet_size_length;
146a46063   Michael Halcrow   eCryptfs: fix Tag...
2288
  	dest[(*packet_length)++] = 0x62; /* binary data format specifier */
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2289
2290
2291
  	dest[(*packet_length)++] = 8;
  	memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  	(*packet_length) += 8;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2292
2293
  	memset(&dest[(*packet_length)], 0x00, 4);
  	(*packet_length) += 4;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2294
2295
2296
2297
2298
  	memcpy(&dest[(*packet_length)], contents, contents_length);
  	(*packet_length) += contents_length;
   out:
  	if (rc)
  		(*packet_length) = 0;
146a46063   Michael Halcrow   eCryptfs: fix Tag...
2299
2300
  	else
  		(*remaining_bytes) -= (*packet_length);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2301
2302
2303
2304
2305
2306
  	return rc;
  }
  
  /**
   * write_tag_3_packet
   * @dest: Buffer into which to write the packet
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
2307
   * @remaining_bytes: Maximum number of bytes that can be written
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2308
2309
2310
2311
2312
2313
2314
2315
2316
   * @auth_tok: Authentication token
   * @crypt_stat: The cryptographic context
   * @key_rec: encrypted key
   * @packet_size: This function will write the number of bytes that end
   *               up constituting the packet; set to zero on error
   *
   * Returns zero on success; non-zero on error.
   */
  static int
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2317
2318
  write_tag_3_packet(char *dest, size_t *remaining_bytes,
  		   struct ecryptfs_auth_tok *auth_tok,
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2319
2320
2321
  		   struct ecryptfs_crypt_stat *crypt_stat,
  		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
  {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2322
  	size_t i;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2323
2324
  	size_t encrypted_session_key_valid = 0;
  	char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
ac97b9f9a   Michael Halcrow   eCryptfs: Allocat...
2325
2326
  	struct scatterlist dst_sg[2];
  	struct scatterlist src_sg[2];
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2327
  	struct mutex *tfm_mutex = NULL;
19e66a67e   Trevor Highland   eCryptfs: change ...
2328
  	u8 cipher_code;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2329
2330
2331
2332
  	size_t packet_size_length;
  	size_t max_packet_size;
  	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  		crypt_stat->mount_crypt_stat;
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2333
2334
  	struct crypto_skcipher *tfm;
  	struct skcipher_request *req;
8bba066f4   Michael Halcrow   [PATCH] eCryptfs:...
2335
  	int rc = 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2336
2337
  
  	(*packet_size) = 0;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2338
  	ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2339
  			  ECRYPTFS_SIG_SIZE);
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2340
  	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2341
2342
2343
2344
2345
2346
2347
2348
2349
  							crypt_stat->cipher);
  	if (unlikely(rc)) {
  		printk(KERN_ERR "Internal error whilst attempting to get "
  		       "tfm and mutex for cipher name [%s]; rc = [%d]
  ",
  		       crypt_stat->cipher, rc);
  		goto out;
  	}
  	if (mount_crypt_stat->global_default_cipher_key_size == 0) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2350
  		printk(KERN_WARNING "No key size specified at mount; "
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2351
2352
2353
  		       "defaulting to [%d]
  ",
  		       crypto_skcipher_default_keysize(tfm));
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2354
  		mount_crypt_stat->global_default_cipher_key_size =
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2355
  			crypto_skcipher_default_keysize(tfm);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2356
  	}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2357
2358
2359
  	if (crypt_stat->key_size == 0)
  		crypt_stat->key_size =
  			mount_crypt_stat->global_default_cipher_key_size;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2360
2361
2362
2363
2364
2365
2366
  	if (auth_tok->session_key.encrypted_key_size == 0)
  		auth_tok->session_key.encrypted_key_size =
  			crypt_stat->key_size;
  	if (crypt_stat->key_size == 24
  	    && strcmp("aes", crypt_stat->cipher) == 0) {
  		memset((crypt_stat->key + 24), 0, 8);
  		auth_tok->session_key.encrypted_key_size = 32;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2367
2368
  	} else
  		auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2369
  	key_rec->enc_key_size =
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2370
  		auth_tok->session_key.encrypted_key_size;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2371
2372
2373
2374
2375
2376
2377
  	encrypted_session_key_valid = 0;
  	for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
  		encrypted_session_key_valid |=
  			auth_tok->session_key.encrypted_key[i];
  	if (encrypted_session_key_valid) {
  		ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
  				"using auth_tok->session_key.encrypted_key, "
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
2378
2379
  				"where key_rec->enc_key_size = [%zd]
  ",
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2380
2381
2382
2383
2384
2385
  				key_rec->enc_key_size);
  		memcpy(key_rec->enc_key,
  		       auth_tok->session_key.encrypted_key,
  		       key_rec->enc_key_size);
  		goto encrypted_session_key_set;
  	}
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2386
2387
  	if (auth_tok->token.password.flags &
  	    ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2388
2389
2390
2391
2392
2393
2394
2395
2396
  		ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  				"session key encryption key of size [%d]
  ",
  				auth_tok->token.password.
  				session_key_encryption_key_bytes);
  		memcpy(session_key_encryption_key,
  		       auth_tok->token.password.session_key_encryption_key,
  		       crypt_stat->key_size);
  		ecryptfs_printk(KERN_DEBUG,
df2e301fe   Jean Delvare   fs: Merge split s...
2397
2398
  				"Cached session key encryption key:
  ");
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2399
2400
2401
2402
2403
2404
2405
2406
  		if (ecryptfs_verbosity > 0)
  			ecryptfs_dump_hex(session_key_encryption_key, 16);
  	}
  	if (unlikely(ecryptfs_verbosity > 0)) {
  		ecryptfs_printk(KERN_DEBUG, "Session key encryption key:
  ");
  		ecryptfs_dump_hex(session_key_encryption_key, 16);
  	}
5dda6992a   Michael Halcrow   eCryptfs: remove ...
2407
  	rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
ac97b9f9a   Michael Halcrow   eCryptfs: Allocat...
2408
2409
  				 src_sg, 2);
  	if (rc < 1 || rc > 2) {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2410
  		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2411
  				"for crypt_stat session key; expected rc = 1; "
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
2412
2413
  				"got rc = [%d]. key_rec->enc_key_size = [%zd]
  ",
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2414
  				rc, key_rec->enc_key_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2415
2416
2417
  		rc = -ENOMEM;
  		goto out;
  	}
5dda6992a   Michael Halcrow   eCryptfs: remove ...
2418
  	rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
ac97b9f9a   Michael Halcrow   eCryptfs: Allocat...
2419
2420
  				 dst_sg, 2);
  	if (rc < 1 || rc > 2) {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2421
  		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2422
2423
  				"for crypt_stat encrypted session key; "
  				"expected rc = 1; got rc = [%d]. "
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
2424
2425
  				"key_rec->enc_key_size = [%zd]
  ", rc,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2426
  				key_rec->enc_key_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2427
2428
2429
  		rc = -ENOMEM;
  		goto out;
  	}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2430
  	mutex_lock(tfm_mutex);
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2431
2432
  	rc = crypto_skcipher_setkey(tfm, session_key_encryption_key,
  				    crypt_stat->key_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2433
  	if (rc < 0) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2434
  		mutex_unlock(tfm_mutex);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2435
  		ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
8bba066f4   Michael Halcrow   [PATCH] eCryptfs:...
2436
2437
  				"context; rc = [%d]
  ", rc);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2438
2439
  		goto out;
  	}
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
  
  	req = skcipher_request_alloc(tfm, GFP_KERNEL);
  	if (!req) {
  		mutex_unlock(tfm_mutex);
  		ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst "
  				"attempting to skcipher_request_alloc for "
  				"%s
  ", crypto_skcipher_driver_name(tfm));
  		rc = -ENOMEM;
  		goto out;
  	}
  
  	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  				      NULL, NULL);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2454
  	rc = 0;
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
2455
2456
  	ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key
  ",
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2457
  			crypt_stat->key_size);
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2458
2459
2460
  	skcipher_request_set_crypt(req, src_sg, dst_sg,
  				   (*key_rec).enc_key_size, NULL);
  	rc = crypto_skcipher_encrypt(req);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2461
  	mutex_unlock(tfm_mutex);
3095e8e36   Herbert Xu   eCryptfs: Use skc...
2462
  	skcipher_request_free(req);
8bba066f4   Michael Halcrow   [PATCH] eCryptfs:...
2463
2464
2465
2466
2467
  	if (rc) {
  		printk(KERN_ERR "Error encrypting; rc = [%d]
  ", rc);
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2468
2469
  	ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:
  ");
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2470
  	if (ecryptfs_verbosity > 0) {
f24b38874   Tyler Hicks   ecryptfs: Fix ecr...
2471
2472
  		ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:
  ",
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2473
  				key_rec->enc_key_size);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2474
2475
  		ecryptfs_dump_hex(key_rec->enc_key,
  				  key_rec->enc_key_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2476
  	}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
  encrypted_session_key_set:
  	/* This format is inspired by OpenPGP; see RFC 2440
  	 * packet tag 3 */
  	max_packet_size = (1                         /* Tag 3 identifier */
  			   + 3                       /* Max Tag 3 packet size */
  			   + 1                       /* Version */
  			   + 1                       /* Cipher code */
  			   + 1                       /* S2K specifier */
  			   + 1                       /* Hash identifier */
  			   + ECRYPTFS_SALT_SIZE      /* Salt */
  			   + 1                       /* Hash iterations */
  			   + key_rec->enc_key_size); /* Encrypted key size */
  	if (max_packet_size > (*remaining_bytes)) {
81acbcd6c   Andrew Morton   ecryptfs: printk ...
2490
2491
2492
  		printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
  		       "there are only [%td] available
  ", max_packet_size,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2493
  		       (*remaining_bytes));
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2494
2495
2496
  		rc = -EINVAL;
  		goto out;
  	}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2497
  	dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2498
2499
  	/* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
  	 * to get the number of octets in the actual Tag 3 packet */
f66e883eb   Michael Halcrow   eCryptfs: integra...
2500
2501
2502
  	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  					  (max_packet_size - 4),
  					  &packet_size_length);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2503
  	if (rc) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2504
2505
2506
  		printk(KERN_ERR "Error generating tag 3 packet header; cannot "
  		       "generate packet length. rc = [%d]
  ", rc);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2507
2508
2509
2510
  		goto out;
  	}
  	(*packet_size) += packet_size_length;
  	dest[(*packet_size)++] = 0x04; /* version 4 */
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2511
2512
  	/* TODO: Break from RFC2440 so that arbitrary ciphers can be
  	 * specified with strings */
9c79f34f7   Michael Halcrow   eCryptfs: Filenam...
2513
2514
  	cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher,
  						      crypt_stat->key_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
  	if (cipher_code == 0) {
  		ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  				"cipher [%s]
  ", crypt_stat->cipher);
  		rc = -EINVAL;
  		goto out;
  	}
  	dest[(*packet_size)++] = cipher_code;
  	dest[(*packet_size)++] = 0x03;	/* S2K */
  	dest[(*packet_size)++] = 0x01;	/* MD5 (TODO: parameterize) */
  	memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  	       ECRYPTFS_SALT_SIZE);
  	(*packet_size) += ECRYPTFS_SALT_SIZE;	/* salt */
  	dest[(*packet_size)++] = 0x60;	/* hash iterations (65536) */
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2529
2530
2531
  	memcpy(&dest[(*packet_size)], key_rec->enc_key,
  	       key_rec->enc_key_size);
  	(*packet_size) += key_rec->enc_key_size;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2532
  out:
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2533
2534
  	if (rc)
  		(*packet_size) = 0;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2535
2536
  	else
  		(*remaining_bytes) -= (*packet_size);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2537
2538
  	return rc;
  }
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2539
  struct kmem_cache *ecryptfs_key_record_cache;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2540
2541
  /**
   * ecryptfs_generate_key_packet_set
22e78fafb   Michael Halcrow   eCryptfs: kerneld...
2542
   * @dest_base: Virtual address from which to write the key record set
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
   * @crypt_stat: The cryptographic context from which the
   *              authentication tokens will be retrieved
   * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
   *                   for the global parameters
   * @len: The amount written
   * @max: The maximum amount of data allowed to be written
   *
   * Generates a key packet set and writes it to the virtual address
   * passed in.
   *
   * Returns zero on success; non-zero on error.
   */
  int
  ecryptfs_generate_key_packet_set(char *dest_base,
  				 struct ecryptfs_crypt_stat *crypt_stat,
  				 struct dentry *ecryptfs_dentry, size_t *len,
  				 size_t max)
  {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2561
  	struct ecryptfs_auth_tok *auth_tok;
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
2562
  	struct key *auth_tok_key = NULL;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2563
2564
2565
2566
  	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  		&ecryptfs_superblock_to_private(
  			ecryptfs_dentry->d_sb)->mount_crypt_stat;
  	size_t written;
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2567
  	struct ecryptfs_key_record *key_rec;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2568
  	struct ecryptfs_key_sig *key_sig;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2569
  	int rc = 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2570
2571
  
  	(*len) = 0;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2572
  	mutex_lock(&crypt_stat->keysig_list_mutex);
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2573
2574
2575
2576
2577
  	key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
  	if (!key_rec) {
  		rc = -ENOMEM;
  		goto out;
  	}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2578
2579
2580
  	list_for_each_entry(key_sig, &crypt_stat->keysig_list,
  			    crypt_stat_list) {
  		memset(key_rec, 0, sizeof(*key_rec));
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
2581
2582
  		rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key,
  							   &auth_tok,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2583
2584
2585
  							   mount_crypt_stat,
  							   key_sig->keysig);
  		if (rc) {
0e1fc5ef4   Roberto Sassu   eCryptfs: verify ...
2586
2587
2588
2589
  			printk(KERN_WARNING "Unable to retrieve auth tok with "
  			       "sig = [%s]
  ", key_sig->keysig);
  			rc = process_find_global_auth_tok_for_sig_err(rc);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2590
2591
  			goto out_free;
  		}
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2592
2593
  		if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  			rc = write_tag_3_packet((dest_base + (*len)),
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2594
  						&max, auth_tok,
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2595
  						crypt_stat, key_rec,
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2596
  						&written);
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2597
2598
  			up_write(&(auth_tok_key->sem));
  			key_put(auth_tok_key);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2599
2600
2601
2602
  			if (rc) {
  				ecryptfs_printk(KERN_WARNING, "Error "
  						"writing tag 3 packet
  ");
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2603
  				goto out_free;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2604
2605
2606
  			}
  			(*len) += written;
  			/* Write auth tok signature packet */
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2607
2608
2609
  			rc = write_tag_11_packet((dest_base + (*len)), &max,
  						 key_rec->sig,
  						 ECRYPTFS_SIG_SIZE, &written);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2610
2611
2612
2613
  			if (rc) {
  				ecryptfs_printk(KERN_ERR, "Error writing "
  						"auth tok signature packet
  ");
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2614
  				goto out_free;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2615
2616
  			}
  			(*len) += written;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2617
  		} else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2618
2619
  			rc = write_tag_1_packet(dest_base + (*len), &max,
  						auth_tok_key, auth_tok,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2620
  						crypt_stat, key_rec, &written);
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2621
2622
2623
2624
  			if (rc) {
  				ecryptfs_printk(KERN_WARNING, "Error "
  						"writing tag 1 packet
  ");
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2625
  				goto out_free;
dddfa461f   Michael Halcrow   [PATCH] eCryptfs:...
2626
2627
  			}
  			(*len) += written;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2628
  		} else {
b2987a5e0   Tyler Hicks   eCryptfs: Unlock ...
2629
2630
  			up_write(&(auth_tok_key->sem));
  			key_put(auth_tok_key);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2631
2632
2633
2634
  			ecryptfs_printk(KERN_WARNING, "Unsupported "
  					"authentication token type
  ");
  			rc = -EINVAL;
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2635
  			goto out_free;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2636
  		}
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2637
2638
  	}
  	if (likely(max > 0)) {
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2639
2640
2641
2642
2643
2644
  		dest_base[(*len)] = 0x00;
  	} else {
  		ecryptfs_printk(KERN_ERR, "Error writing boundary byte
  ");
  		rc = -EIO;
  	}
eb95e7ffa   Michael Halcrow   [PATCH] eCryptfs:...
2645
2646
  out_free:
  	kmem_cache_free(ecryptfs_key_record_cache, key_rec);
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2647
2648
2649
  out:
  	if (rc)
  		(*len) = 0;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2650
2651
2652
2653
2654
2655
2656
2657
2658
  	mutex_unlock(&crypt_stat->keysig_list_mutex);
  	return rc;
  }
  
  struct kmem_cache *ecryptfs_key_sig_cache;
  
  int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
  {
  	struct ecryptfs_key_sig *new_key_sig;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2659
2660
2661
  
  	new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
  	if (!new_key_sig) {
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2662
2663
2664
  		printk(KERN_ERR
  		       "Error allocating from ecryptfs_key_sig_cache
  ");
aa06117f1   Roland Dreier   eCryptfs: Fix loc...
2665
  		return -ENOMEM;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2666
2667
  	}
  	memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
7762e230f   Roberto Sassu   eCryptfs: modifie...
2668
  	new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
aa06117f1   Roland Dreier   eCryptfs: Fix loc...
2669
  	/* Caller must hold keysig_list_mutex */
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2670
  	list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
aa06117f1   Roland Dreier   eCryptfs: Fix loc...
2671
2672
  
  	return 0;
237fead61   Michael Halcrow   [PATCH] ecryptfs:...
2673
  }
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2674
2675
2676
2677
2678
  
  struct kmem_cache *ecryptfs_global_auth_tok_cache;
  
  int
  ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
84814d642   Tyler Hicks   eCryptfs: don't e...
2679
  			     char *sig, u32 global_auth_tok_flags)
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2680
2681
2682
  {
  	struct ecryptfs_global_auth_tok *new_auth_tok;
  	int rc = 0;
459e21642   Eric Sandeen   ecryptfs: initial...
2683
  	new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2684
2685
2686
2687
2688
2689
2690
2691
2692
  					GFP_KERNEL);
  	if (!new_auth_tok) {
  		rc = -ENOMEM;
  		printk(KERN_ERR "Error allocating from "
  		       "ecryptfs_global_auth_tok_cache
  ");
  		goto out;
  	}
  	memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
84814d642   Tyler Hicks   eCryptfs: don't e...
2693
  	new_auth_tok->flags = global_auth_tok_flags;
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2694
2695
2696
2697
  	new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  	list_add(&new_auth_tok->mount_crypt_stat_list,
  		 &mount_crypt_stat->global_auth_tok_list);
f4aad16ad   Michael Halcrow   eCryptfs: add key...
2698
2699
2700
2701
  	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  out:
  	return rc;
  }