Skip to content

Commit f66e883

Browse files
Michael Halcrowtorvalds
authored andcommitted
eCryptfs: integrate eCryptfs device handle into the module.
Update the versioning information. Make the message types generic. Add an outgoing message queue to the daemon struct. Make the functions to parse and write the packet lengths available to the rest of the module. Add functions to create and destroy the daemon structs. Clean up some of the comments and make the code a little more consistent with itself. [akpm@linux-foundation.org: printk fixes] Signed-off-by: Michael Halcrow <mhalcrow@us.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8bf2deb commit f66e883

File tree

6 files changed

+435
-226
lines changed

6 files changed

+435
-226
lines changed

fs/ecryptfs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
obj-$(CONFIG_ECRYPT_FS) += ecryptfs.o
66

7-
ecryptfs-objs := dentry.o file.o inode.o main.o super.o mmap.o read_write.o crypto.o keystore.o messaging.o netlink.o debug.o
7+
ecryptfs-objs := dentry.o file.o inode.o main.o super.o mmap.o read_write.o crypto.o keystore.o messaging.o netlink.o miscdev.o debug.o

fs/ecryptfs/ecryptfs_kernel.h

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Copyright (C) 1997-2003 Erez Zadok
66
* Copyright (C) 2001-2003 Stony Brook University
7-
* Copyright (C) 2004-2007 International Business Machines Corp.
7+
* Copyright (C) 2004-2008 International Business Machines Corp.
88
* Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
99
* Trevor S. Highland <trevor.highland@gmail.com>
1010
* Tyler Hicks <tyhicks@ou.edu>
@@ -49,11 +49,13 @@
4949
#define ECRYPTFS_VERSIONING_POLICY 0x00000008
5050
#define ECRYPTFS_VERSIONING_XATTR 0x00000010
5151
#define ECRYPTFS_VERSIONING_MULTKEY 0x00000020
52+
#define ECRYPTFS_VERSIONING_DEVMISC 0x00000040
5253
#define ECRYPTFS_VERSIONING_MASK (ECRYPTFS_VERSIONING_PASSPHRASE \
5354
| ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH \
5455
| ECRYPTFS_VERSIONING_PUBKEY \
5556
| ECRYPTFS_VERSIONING_XATTR \
56-
| ECRYPTFS_VERSIONING_MULTKEY)
57+
| ECRYPTFS_VERSIONING_MULTKEY \
58+
| ECRYPTFS_VERSIONING_DEVMISC)
5759
#define ECRYPTFS_MAX_PASSWORD_LENGTH 64
5860
#define ECRYPTFS_MAX_PASSPHRASE_BYTES ECRYPTFS_MAX_PASSWORD_LENGTH
5961
#define ECRYPTFS_SALT_SIZE 8
@@ -73,17 +75,14 @@
7375
#define ECRYPTFS_DEFAULT_MSG_CTX_ELEMS 32
7476
#define ECRYPTFS_DEFAULT_SEND_TIMEOUT HZ
7577
#define ECRYPTFS_MAX_MSG_CTX_TTL (HZ*3)
76-
#define ECRYPTFS_NLMSG_HELO 100
77-
#define ECRYPTFS_NLMSG_QUIT 101
78-
#define ECRYPTFS_NLMSG_REQUEST 102
79-
#define ECRYPTFS_NLMSG_RESPONSE 103
8078
#define ECRYPTFS_MAX_PKI_NAME_BYTES 16
8179
#define ECRYPTFS_DEFAULT_NUM_USERS 4
8280
#define ECRYPTFS_MAX_NUM_USERS 32768
8381
#define ECRYPTFS_TRANSPORT_NETLINK 0
8482
#define ECRYPTFS_TRANSPORT_CONNECTOR 1
8583
#define ECRYPTFS_TRANSPORT_RELAYFS 2
86-
#define ECRYPTFS_DEFAULT_TRANSPORT ECRYPTFS_TRANSPORT_NETLINK
84+
#define ECRYPTFS_TRANSPORT_MISCDEV 3
85+
#define ECRYPTFS_DEFAULT_TRANSPORT ECRYPTFS_TRANSPORT_MISCDEV
8786
#define ECRYPTFS_XATTR_NAME "user.ecryptfs"
8887

8988
#define RFC2440_CIPHER_DES3_EDE 0x02
@@ -366,32 +365,62 @@ struct ecryptfs_auth_tok_list_item {
366365
};
367366

368367
struct ecryptfs_message {
368+
/* Can never be greater than ecryptfs_message_buf_len */
369+
/* Used to find the parent msg_ctx */
370+
/* Inherits from msg_ctx->index */
369371
u32 index;
370372
u32 data_len;
371373
u8 data[];
372374
};
373375

374376
struct ecryptfs_msg_ctx {
375-
#define ECRYPTFS_MSG_CTX_STATE_FREE 0x0001
376-
#define ECRYPTFS_MSG_CTX_STATE_PENDING 0x0002
377-
#define ECRYPTFS_MSG_CTX_STATE_DONE 0x0003
378-
u32 state;
379-
unsigned int index;
380-
unsigned int counter;
377+
#define ECRYPTFS_MSG_CTX_STATE_FREE 0x01
378+
#define ECRYPTFS_MSG_CTX_STATE_PENDING 0x02
379+
#define ECRYPTFS_MSG_CTX_STATE_DONE 0x03
380+
#define ECRYPTFS_MSG_CTX_STATE_NO_REPLY 0x04
381+
u8 state;
382+
#define ECRYPTFS_MSG_HELO 100
383+
#define ECRYPTFS_MSG_QUIT 101
384+
#define ECRYPTFS_MSG_REQUEST 102
385+
#define ECRYPTFS_MSG_RESPONSE 103
386+
u8 type;
387+
u32 index;
388+
/* Counter converts to a sequence number. Each message sent
389+
* out for which we expect a response has an associated
390+
* sequence number. The response must have the same sequence
391+
* number as the counter for the msg_stc for the message to be
392+
* valid. */
393+
u32 counter;
394+
size_t msg_size;
381395
struct ecryptfs_message *msg;
382396
struct task_struct *task;
383397
struct list_head node;
398+
struct list_head daemon_out_list;
384399
struct mutex mux;
385400
};
386401

387402
extern unsigned int ecryptfs_transport;
388403

389-
struct ecryptfs_daemon_id {
404+
struct ecryptfs_daemon;
405+
406+
struct ecryptfs_daemon {
407+
#define ECRYPTFS_DAEMON_IN_READ 0x00000001
408+
#define ECRYPTFS_DAEMON_IN_POLL 0x00000002
409+
#define ECRYPTFS_DAEMON_ZOMBIE 0x00000004
410+
#define ECRYPTFS_DAEMON_MISCDEV_OPEN 0x00000008
411+
u32 flags;
412+
u32 num_queued_msg_ctx;
390413
pid_t pid;
391-
uid_t uid;
392-
struct hlist_node id_chain;
414+
uid_t euid;
415+
struct task_struct *task;
416+
struct mutex mux;
417+
struct list_head msg_ctx_out_queue;
418+
wait_queue_head_t wait;
419+
struct hlist_node euid_chain;
393420
};
394421

422+
extern struct mutex ecryptfs_daemon_hash_mux;
423+
395424
static inline struct ecryptfs_file_info *
396425
ecryptfs_file_to_private(struct file *file)
397426
{
@@ -593,13 +622,13 @@ int ecryptfs_init_messaging(unsigned int transport);
593622
void ecryptfs_release_messaging(unsigned int transport);
594623

595624
int ecryptfs_send_netlink(char *data, int data_len,
596-
struct ecryptfs_msg_ctx *msg_ctx, u16 msg_type,
625+
struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
597626
u16 msg_flags, pid_t daemon_pid);
598627
int ecryptfs_init_netlink(void);
599628
void ecryptfs_release_netlink(void);
600629

601630
int ecryptfs_send_connector(char *data, int data_len,
602-
struct ecryptfs_msg_ctx *msg_ctx, u16 msg_type,
631+
struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
603632
u16 msg_flags, pid_t daemon_pid);
604633
int ecryptfs_init_connector(void);
605634
void ecryptfs_release_connector(void);
@@ -642,5 +671,19 @@ int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
642671
size_t offset_in_page, size_t size,
643672
struct inode *ecryptfs_inode);
644673
struct page *ecryptfs_get_locked_page(struct file *file, loff_t index);
674+
int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon);
675+
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid);
676+
int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
677+
size_t *length_size);
678+
int ecryptfs_write_packet_length(char *dest, size_t size,
679+
size_t *packet_size_length);
680+
int ecryptfs_init_ecryptfs_miscdev(void);
681+
void ecryptfs_destroy_ecryptfs_miscdev(void);
682+
int ecryptfs_send_miscdev(char *data, size_t data_size,
683+
struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
684+
u16 msg_flags, struct ecryptfs_daemon *daemon);
685+
void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);
686+
int
687+
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid);
645688

646689
#endif /* #ifndef ECRYPTFS_KERNEL_H */

fs/ecryptfs/keystore.c

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ static int process_request_key_err(long err_code)
6565
}
6666

6767
/**
68-
* parse_packet_length
68+
* ecryptfs_parse_packet_length
6969
* @data: Pointer to memory containing length at offset
7070
* @size: This function writes the decoded size to this memory
7171
* address; zero on error
7272
* @length_size: The number of bytes occupied by the encoded length
7373
*
7474
* Returns zero on success; non-zero on error
7575
*/
76-
static int parse_packet_length(unsigned char *data, size_t *size,
77-
size_t *length_size)
76+
int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
77+
size_t *length_size)
7878
{
7979
int rc = 0;
8080

@@ -105,7 +105,7 @@ static int parse_packet_length(unsigned char *data, size_t *size,
105105
}
106106

107107
/**
108-
* write_packet_length
108+
* ecryptfs_write_packet_length
109109
* @dest: The byte array target into which to write the length. Must
110110
* have at least 5 bytes allocated.
111111
* @size: The length to write.
@@ -114,8 +114,8 @@ static int parse_packet_length(unsigned char *data, size_t *size,
114114
*
115115
* Returns zero on success; non-zero on error.
116116
*/
117-
static int write_packet_length(char *dest, size_t size,
118-
size_t *packet_size_length)
117+
int ecryptfs_write_packet_length(char *dest, size_t size,
118+
size_t *packet_size_length)
119119
{
120120
int rc = 0;
121121

@@ -162,8 +162,8 @@ write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
162162
goto out;
163163
}
164164
message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
165-
rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
166-
&packet_size_len);
165+
rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
166+
&packet_size_len);
167167
if (rc) {
168168
ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
169169
"header; cannot generate packet length\n");
@@ -172,8 +172,9 @@ write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
172172
i += packet_size_len;
173173
memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
174174
i += ECRYPTFS_SIG_SIZE_HEX;
175-
rc = write_packet_length(&message[i], session_key->encrypted_key_size,
176-
&packet_size_len);
175+
rc = ecryptfs_write_packet_length(&message[i],
176+
session_key->encrypted_key_size,
177+
&packet_size_len);
177178
if (rc) {
178179
ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
179180
"header; cannot generate packet length\n");
@@ -225,7 +226,7 @@ parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
225226
rc = -EIO;
226227
goto out;
227228
}
228-
rc = parse_packet_length(&data[i], &m_size, &data_len);
229+
rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
229230
if (rc) {
230231
ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
231232
"rc = [%d]\n", rc);
@@ -304,8 +305,8 @@ write_tag_66_packet(char *signature, u8 cipher_code,
304305
goto out;
305306
}
306307
message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
307-
rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
308-
&packet_size_len);
308+
rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
309+
&packet_size_len);
309310
if (rc) {
310311
ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
311312
"header; cannot generate packet length\n");
@@ -315,8 +316,8 @@ write_tag_66_packet(char *signature, u8 cipher_code,
315316
memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
316317
i += ECRYPTFS_SIG_SIZE_HEX;
317318
/* The encrypted key includes 1 byte cipher code and 2 byte checksum */
318-
rc = write_packet_length(&message[i], crypt_stat->key_size + 3,
319-
&packet_size_len);
319+
rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
320+
&packet_size_len);
320321
if (rc) {
321322
ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
322323
"header; cannot generate packet length\n");
@@ -357,38 +358,43 @@ parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
357358
/* verify that everything through the encrypted FEK size is present */
358359
if (message_len < 4) {
359360
rc = -EIO;
361+
printk(KERN_ERR "%s: message_len is [%Zd]; minimum acceptable "
362+
"message length is [%d]\n", __func__, message_len, 4);
360363
goto out;
361364
}
362365
if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
363-
ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_67\n");
364366
rc = -EIO;
367+
printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n",
368+
__func__);
365369
goto out;
366370
}
367371
if (data[i++]) {
368-
ecryptfs_printk(KERN_ERR, "Status indicator has non zero value"
369-
" [%d]\n", data[i-1]);
370372
rc = -EIO;
373+
printk(KERN_ERR "%s: Status indicator has non zero "
374+
"value [%d]\n", __func__, data[i-1]);
375+
371376
goto out;
372377
}
373-
rc = parse_packet_length(&data[i], &key_rec->enc_key_size, &data_len);
378+
rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
379+
&data_len);
374380
if (rc) {
375381
ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
376382
"rc = [%d]\n", rc);
377383
goto out;
378384
}
379385
i += data_len;
380386
if (message_len < (i + key_rec->enc_key_size)) {
381-
ecryptfs_printk(KERN_ERR, "message_len [%d]; max len is [%d]\n",
382-
message_len, (i + key_rec->enc_key_size));
383387
rc = -EIO;
388+
printk(KERN_ERR "%s: message_len [%Zd]; max len is [%Zd]\n",
389+
__func__, message_len, (i + key_rec->enc_key_size));
384390
goto out;
385391
}
386392
if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
387-
ecryptfs_printk(KERN_ERR, "Encrypted key_size [%d] larger than "
388-
"the maximum key size [%d]\n",
389-
key_rec->enc_key_size,
390-
ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
391393
rc = -EIO;
394+
printk(KERN_ERR "%s: Encrypted key_size [%Zd] larger than "
395+
"the maximum key size [%d]\n", __func__,
396+
key_rec->enc_key_size,
397+
ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
392398
goto out;
393399
}
394400
memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
@@ -445,7 +451,7 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
445451
rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
446452
&netlink_message, &netlink_message_length);
447453
if (rc) {
448-
ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet");
454+
ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
449455
goto out;
450456
}
451457
rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
@@ -570,8 +576,8 @@ parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
570576
goto out;
571577
}
572578
(*new_auth_tok) = &auth_tok_list_item->auth_tok;
573-
rc = parse_packet_length(&data[(*packet_size)], &body_size,
574-
&length_size);
579+
rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
580+
&length_size);
575581
if (rc) {
576582
printk(KERN_WARNING "Error parsing packet length; "
577583
"rc = [%d]\n", rc);
@@ -704,8 +710,8 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
704710
goto out;
705711
}
706712
(*new_auth_tok) = &auth_tok_list_item->auth_tok;
707-
rc = parse_packet_length(&data[(*packet_size)], &body_size,
708-
&length_size);
713+
rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
714+
&length_size);
709715
if (rc) {
710716
printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
711717
rc);
@@ -852,8 +858,8 @@ parse_tag_11_packet(unsigned char *data, unsigned char *contents,
852858
rc = -EINVAL;
853859
goto out;
854860
}
855-
rc = parse_packet_length(&data[(*packet_size)], &body_size,
856-
&length_size);
861+
rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
862+
&length_size);
857863
if (rc) {
858864
printk(KERN_WARNING "Invalid tag 11 packet format\n");
859865
goto out;
@@ -1405,8 +1411,8 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes,
14051411
auth_tok->token.private_key.key_size;
14061412
rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
14071413
if (rc) {
1408-
ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1409-
"via a pki");
1414+
printk(KERN_ERR "Failed to encrypt session key via a key "
1415+
"module; rc = [%d]\n", rc);
14101416
goto out;
14111417
}
14121418
if (ecryptfs_verbosity > 0) {
@@ -1430,8 +1436,9 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes,
14301436
goto out;
14311437
}
14321438
dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
1433-
rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
1434-
&packet_size_length);
1439+
rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1440+
(max_packet_size - 4),
1441+
&packet_size_length);
14351442
if (rc) {
14361443
ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
14371444
"header; cannot generate packet length\n");
@@ -1489,8 +1496,9 @@ write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
14891496
goto out;
14901497
}
14911498
dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
1492-
rc = write_packet_length(&dest[(*packet_length)],
1493-
(max_packet_size - 4), &packet_size_length);
1499+
rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
1500+
(max_packet_size - 4),
1501+
&packet_size_length);
14941502
if (rc) {
14951503
printk(KERN_ERR "Error generating tag 11 packet header; cannot "
14961504
"generate packet length. rc = [%d]\n", rc);
@@ -1682,8 +1690,9 @@ write_tag_3_packet(char *dest, size_t *remaining_bytes,
16821690
dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
16831691
/* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
16841692
* to get the number of octets in the actual Tag 3 packet */
1685-
rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
1686-
&packet_size_length);
1693+
rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1694+
(max_packet_size - 4),
1695+
&packet_size_length);
16871696
if (rc) {
16881697
printk(KERN_ERR "Error generating tag 3 packet header; cannot "
16891698
"generate packet length. rc = [%d]\n", rc);

0 commit comments

Comments
 (0)