1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/**
* Userspace side of procfs communications with eCryptfs kernel
* module.
*
* Copyright (C) 2008 International Business Machines Corp.
* Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
*
* 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.
*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#ifndef S_SPLINT_S
#include <stdio.h>
#include <syslog.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../include/ecryptfs.h"
int ecryptfs_send_miscdev(struct ecryptfs_miscdev_ctx *miscdev_ctx,
struct ecryptfs_message *msg, uint8_t msg_type,
uint16_t msg_flags, uint32_t msg_seq)
{
uint32_t miscdev_msg_data_size;
size_t packet_len_size;
size_t packet_len;
uint32_t msg_seq_be32;
uint32_t i;
ssize_t written;
char packet_len_str[3];
char *miscdev_msg_data;
int rc = 0;
/* miscdevfs packet format:
* Octet 0: Type
* Octets 1-4: network byte order msg_ctx->counter
* Octets 5-N0: Size of struct ecryptfs_message to follow
* Octets N0-N1: struct ecryptfs_message (including data)
*
* Octets 5-N1 not written if the packet type does not
* include a message */
if (msg) {
packet_len = (sizeof(*msg) + msg->data_len);
rc = ecryptfs_write_packet_length(packet_len_str, packet_len,
&packet_len_size);
if (rc)
goto out;
} else {
packet_len_size = 0;
packet_len = 0;
}
miscdev_msg_data_size = (1 + 4 + packet_len_size + packet_len);
miscdev_msg_data = malloc(miscdev_msg_data_size);
if (!miscdev_msg_data) {
rc = -ENOMEM;
goto out;
}
msg_seq_be32 = htonl(msg_seq);
i = 0;
miscdev_msg_data[i++] = msg_type;
memcpy(&miscdev_msg_data[i], (void *)&msg_seq_be32, 4);
i += 4;
if (msg) {
memcpy(&miscdev_msg_data[i], packet_len_str, packet_len_size);
i += packet_len_size;
memcpy(&miscdev_msg_data[i], (void *)msg, packet_len);
}
written = write(miscdev_ctx->miscdev_fd, miscdev_msg_data,
miscdev_msg_data_size);
if (written == -1) {
rc = -EIO;
syslog(LOG_ERR, "Failed to send eCryptfs miscdev message; "
"errno msg = [%m]\n");
}
free(miscdev_msg_data);
out:
return rc;
}
/**
* ecryptfs_recv_miscdev
* @msg: Allocated in this function; callee must deallocate
*/
int ecryptfs_recv_miscdev(struct ecryptfs_miscdev_ctx *miscdev_ctx,
struct ecryptfs_message **msg, uint32_t *msg_seq,
uint8_t *msg_type)
{
ssize_t read_bytes;
uint32_t miscdev_msg_data_size;
size_t packet_len_size;
size_t packet_len;
uint32_t msg_seq_be32;
uint32_t i;
char *miscdev_msg_data;
int rc = 0;
miscdev_msg_data = malloc(ECRYPTFS_MSG_MAX_SIZE);
if (!miscdev_msg_data) {
rc = -ENOMEM;
goto out;
}
read_bytes = read(miscdev_ctx->miscdev_fd, miscdev_msg_data,
ECRYPTFS_MSG_MAX_SIZE);
if (read_bytes == -1) {
rc = -EIO;
syslog(LOG_ERR, "%s: Error attempting to read message from "
"miscdev handle; errno msg = [%m]\n", __FUNCTION__);
goto out;
}
if (read_bytes < (1 + 4)) {
rc = -EINVAL;
syslog(LOG_ERR, "%s: Received invalid packet from kernel; "
"read_bytes = [%zu]; minimum possible packet site is "
"[%d]\n", __FUNCTION__, read_bytes,
(1 + 4));
goto out;
}
i = 0;
(*msg_type) = miscdev_msg_data[i++];
memcpy((void *)&msg_seq_be32, &miscdev_msg_data[i], 4);
i += 4;
(*msg_seq) = ntohl(msg_seq_be32);
if ((*msg_type) == ECRYPTFS_MSG_REQUEST) {
rc = ecryptfs_parse_packet_length((unsigned char *)
&miscdev_msg_data[i],
&packet_len,
&packet_len_size);
if (rc)
goto out;
i += packet_len_size;
} else {
packet_len_size = 0;
packet_len = 0;
}
miscdev_msg_data_size = (1 + 4 + packet_len_size + packet_len);
if (miscdev_msg_data_size != (size_t)read_bytes) {
rc = -EINVAL;
syslog(LOG_ERR, "%s: Invalid packet. (1 + 4 + "
"packet_len_size=[%zu] + packet_len=[%zu])=[%zu] != "
"read_bytes=[%zu]\n", __FUNCTION__, packet_len_size,
packet_len, (1 + 4 + packet_len_size + packet_len),
read_bytes);
goto out;
}
(*msg) = malloc(packet_len);
if (!(*msg)) {
rc = -ENOMEM;
goto out;
}
memcpy((void *)(*msg), (void *)&miscdev_msg_data[i], packet_len);
out:
free(miscdev_msg_data);
return rc;
}
int ecryptfs_init_miscdev(struct ecryptfs_miscdev_ctx *miscdev_ctx)
{
int rc = 0;
miscdev_ctx->miscdev_fd = open(ECRYPTFS_DEFAULT_MISCDEV_FULLPATH_0,
O_RDWR);
if (miscdev_ctx->miscdev_fd == -1) {
syslog(LOG_ERR, "%s: Error whilst attempting to open "
"[%s]; errno msg = [%m]\n", __FUNCTION__,
ECRYPTFS_DEFAULT_MISCDEV_FULLPATH_0);
} else
goto out;
miscdev_ctx->miscdev_fd = open(ECRYPTFS_DEFAULT_MISCDEV_FULLPATH_1,
O_RDWR);
if (miscdev_ctx->miscdev_fd == -1) {
syslog(LOG_ERR, "%s: Error whilst attempting to open "
"[%s]; errno msg = [%m]\n", __FUNCTION__,
ECRYPTFS_DEFAULT_MISCDEV_FULLPATH_1);
rc = -EIO;
}
out:
return rc;
}
void ecryptfs_release_miscdev(struct ecryptfs_miscdev_ctx *miscdev_ctx)
{
close(miscdev_ctx->miscdev_fd);
}
int init_miscdev_daemon(void)
{
return 0;
}
int ecryptfs_run_miscdev_daemon(struct ecryptfs_miscdev_ctx *miscdev_ctx)
{
struct ecryptfs_message *emsg;
struct ecryptfs_ctx ctx;
uint32_t msg_seq;
uint8_t msg_type;
int error_count = 0;
int rc;
memset(&ctx, 0, sizeof(struct ecryptfs_ctx));
rc = ecryptfs_register_key_modules(&ctx);
if (rc) {
syslog(LOG_ERR, "Failed to register key modules; rc = [%d]\n",
rc);
goto out;
}
receive:
emsg = NULL;
rc = ecryptfs_recv_miscdev(miscdev_ctx, &emsg, &msg_seq, &msg_type);
if (rc < 0) {
syslog(LOG_ERR, "Error while receiving eCryptfs message "
"errno = [%d]; errno msg = [%m]\n", errno);
error_count++;
if (error_count > ECRYPTFS_MSG_ERROR_COUNT_THRESHOLD) {
syslog(LOG_ERR, "Messaging error threshold exceeded "
"maximum of [%d]; terminating daemon\n",
ECRYPTFS_MSG_ERROR_COUNT_THRESHOLD);
rc = -EIO;
goto out;
}
} else if (msg_type == ECRYPTFS_MSG_HELO) {
syslog(LOG_DEBUG, "Received eCryptfs HELO message from the "
"kernel\n");
error_count = 0;
} else if (msg_type == ECRYPTFS_MSG_QUIT) {
syslog(LOG_DEBUG, "Received eCryptfs QUIT message from the "
"kernel\n");
free(emsg);
rc = 0;
goto out;
} else if (msg_type == ECRYPTFS_MSG_REQUEST) {
struct ecryptfs_message *reply = NULL;
rc = parse_packet(&ctx, emsg, &reply);
if (rc) {
syslog(LOG_ERR, "Failed to miscdevess packet\n");
free(reply);
goto free_emsg;
}
reply->index = emsg->index;
rc = ecryptfs_send_miscdev(miscdev_ctx, reply,
ECRYPTFS_MSG_RESPONSE, 0, msg_seq);
if (rc < 0) {
syslog(LOG_ERR, "Failed to send message in response to "
"kernel request\n");
}
free(reply);
error_count = 0;
} else
syslog(LOG_DEBUG, "Received unrecognized message type [%d]\n",
msg_type);
free_emsg:
free(emsg);
goto receive;
out:
ecryptfs_free_key_mod_list(&ctx);
return rc;
}
|