]>
Commit | Line | Data |
---|---|---|
237fead6 MH |
1 | /** |
2 | * eCryptfs: Linux filesystem encryption layer | |
3 | * | |
4 | * Copyright (C) 1997-2003 Erez Zadok | |
5 | * Copyright (C) 2001-2003 Stony Brook University | |
dd2a3b7a | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
237fead6 MH |
7 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
8 | * Michael C. Thompson <mcthomps@us.ibm.com> | |
dddfa461 | 9 | * Tyler Hicks <tyhicks@ou.edu> |
237fead6 MH |
10 | * |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License as | |
13 | * published by the Free Software Foundation; either version 2 of the | |
14 | * License, or (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, but | |
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
24 | * 02111-1307, USA. | |
25 | */ | |
26 | ||
27 | #include <linux/dcache.h> | |
28 | #include <linux/file.h> | |
29 | #include <linux/module.h> | |
30 | #include <linux/namei.h> | |
31 | #include <linux/skbuff.h> | |
32 | #include <linux/crypto.h> | |
33 | #include <linux/netlink.h> | |
34 | #include <linux/mount.h> | |
237fead6 MH |
35 | #include <linux/pagemap.h> |
36 | #include <linux/key.h> | |
37 | #include <linux/parser.h> | |
0cc72dc7 | 38 | #include <linux/fs_stack.h> |
237fead6 MH |
39 | #include "ecryptfs_kernel.h" |
40 | ||
41 | /** | |
42 | * Module parameter that defines the ecryptfs_verbosity level. | |
43 | */ | |
44 | int ecryptfs_verbosity = 0; | |
45 | ||
46 | module_param(ecryptfs_verbosity, int, 0); | |
47 | MODULE_PARM_DESC(ecryptfs_verbosity, | |
48 | "Initial verbosity level (0 or 1; defaults to " | |
49 | "0, which is Quiet)"); | |
50 | ||
dddfa461 MH |
51 | /** |
52 | * Module parameter that defines the number of netlink message buffer | |
53 | * elements | |
54 | */ | |
55 | unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS; | |
56 | ||
57 | module_param(ecryptfs_message_buf_len, uint, 0); | |
58 | MODULE_PARM_DESC(ecryptfs_message_buf_len, | |
59 | "Number of message buffer elements"); | |
60 | ||
61 | /** | |
62 | * Module parameter that defines the maximum guaranteed amount of time to wait | |
63 | * for a response through netlink. The actual sleep time will be, more than | |
64 | * likely, a small amount greater than this specified value, but only less if | |
65 | * the netlink message successfully arrives. | |
66 | */ | |
67 | signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ; | |
68 | ||
69 | module_param(ecryptfs_message_wait_timeout, long, 0); | |
70 | MODULE_PARM_DESC(ecryptfs_message_wait_timeout, | |
71 | "Maximum number of seconds that an operation will " | |
72 | "sleep while waiting for a message response from " | |
73 | "userspace"); | |
74 | ||
75 | /** | |
76 | * Module parameter that is an estimate of the maximum number of users | |
77 | * that will be concurrently using eCryptfs. Set this to the right | |
78 | * value to balance performance and memory use. | |
79 | */ | |
80 | unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS; | |
81 | ||
82 | module_param(ecryptfs_number_of_users, uint, 0); | |
83 | MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of " | |
84 | "concurrent users of eCryptfs"); | |
85 | ||
86 | unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT; | |
87 | ||
237fead6 MH |
88 | void __ecryptfs_printk(const char *fmt, ...) |
89 | { | |
90 | va_list args; | |
91 | va_start(args, fmt); | |
92 | if (fmt[1] == '7') { /* KERN_DEBUG */ | |
93 | if (ecryptfs_verbosity >= 1) | |
94 | vprintk(fmt, args); | |
95 | } else | |
96 | vprintk(fmt, args); | |
97 | va_end(args); | |
98 | } | |
99 | ||
4981e081 MH |
100 | /** |
101 | * ecryptfs_init_persistent_file | |
102 | * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with | |
103 | * the lower dentry and the lower mount set | |
104 | * | |
105 | * eCryptfs only ever keeps a single open file for every lower | |
106 | * inode. All I/O operations to the lower inode occur through that | |
107 | * file. When the first eCryptfs dentry that interposes with the first | |
108 | * lower dentry for that inode is created, this function creates the | |
109 | * persistent file struct and associates it with the eCryptfs | |
110 | * inode. When the eCryptfs inode is destroyed, the file is closed. | |
111 | * | |
112 | * The persistent file will be opened with read/write permissions, if | |
113 | * possible. Otherwise, it is opened read-only. | |
114 | * | |
115 | * This function does nothing if a lower persistent file is already | |
116 | * associated with the eCryptfs inode. | |
117 | * | |
118 | * Returns zero on success; non-zero otherwise | |
119 | */ | |
7896b631 | 120 | static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) |
4981e081 MH |
121 | { |
122 | struct ecryptfs_inode_info *inode_info = | |
123 | ecryptfs_inode_to_private(ecryptfs_dentry->d_inode); | |
124 | int rc = 0; | |
125 | ||
126 | mutex_lock(&inode_info->lower_file_mutex); | |
127 | if (!inode_info->lower_file) { | |
128 | struct dentry *lower_dentry; | |
129 | struct vfsmount *lower_mnt = | |
130 | ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry); | |
131 | ||
132 | lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); | |
746f1e55 MH |
133 | rc = ecryptfs_privileged_open(&inode_info->lower_file, |
134 | lower_dentry, lower_mnt); | |
135 | if (rc || IS_ERR(inode_info->lower_file)) { | |
4981e081 | 136 | printk(KERN_ERR "Error opening lower persistent file " |
746f1e55 MH |
137 | "for lower_dentry [0x%p] and lower_mnt [0x%p]; " |
138 | "rc = [%d]\n", lower_dentry, lower_mnt, rc); | |
4981e081 MH |
139 | rc = PTR_ERR(inode_info->lower_file); |
140 | inode_info->lower_file = NULL; | |
141 | } | |
142 | } | |
143 | mutex_unlock(&inode_info->lower_file_mutex); | |
144 | return rc; | |
145 | } | |
146 | ||
237fead6 MH |
147 | /** |
148 | * ecryptfs_interpose | |
149 | * @lower_dentry: Existing dentry in the lower filesystem | |
150 | * @dentry: ecryptfs' dentry | |
151 | * @sb: ecryptfs's super_block | |
152 | * @flag: If set to true, then d_add is called, else d_instantiate is called | |
153 | * | |
154 | * Interposes upper and lower dentries. | |
155 | * | |
156 | * Returns zero on success; non-zero otherwise | |
157 | */ | |
158 | int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry, | |
159 | struct super_block *sb, int flag) | |
160 | { | |
161 | struct inode *lower_inode; | |
162 | struct inode *inode; | |
163 | int rc = 0; | |
164 | ||
165 | lower_inode = lower_dentry->d_inode; | |
166 | if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) { | |
167 | rc = -EXDEV; | |
168 | goto out; | |
169 | } | |
170 | if (!igrab(lower_inode)) { | |
171 | rc = -ESTALE; | |
172 | goto out; | |
173 | } | |
174 | inode = iget5_locked(sb, (unsigned long)lower_inode, | |
175 | ecryptfs_inode_test, ecryptfs_inode_set, | |
176 | lower_inode); | |
177 | if (!inode) { | |
178 | rc = -EACCES; | |
179 | iput(lower_inode); | |
180 | goto out; | |
181 | } | |
182 | if (inode->i_state & I_NEW) | |
183 | unlock_new_inode(inode); | |
184 | else | |
185 | iput(lower_inode); | |
186 | if (S_ISLNK(lower_inode->i_mode)) | |
187 | inode->i_op = &ecryptfs_symlink_iops; | |
188 | else if (S_ISDIR(lower_inode->i_mode)) | |
189 | inode->i_op = &ecryptfs_dir_iops; | |
190 | if (S_ISDIR(lower_inode->i_mode)) | |
191 | inode->i_fop = &ecryptfs_dir_fops; | |
26da8205 | 192 | if (special_file(lower_inode->i_mode)) |
237fead6 MH |
193 | init_special_inode(inode, lower_inode->i_mode, |
194 | lower_inode->i_rdev); | |
195 | dentry->d_op = &ecryptfs_dops; | |
196 | if (flag) | |
197 | d_add(dentry, inode); | |
198 | else | |
199 | d_instantiate(dentry, inode); | |
0cc72dc7 | 200 | fsstack_copy_attr_all(inode, lower_inode, NULL); |
237fead6 MH |
201 | /* This size will be overwritten for real files w/ headers and |
202 | * other metadata */ | |
0cc72dc7 | 203 | fsstack_copy_inode_size(inode, lower_inode); |
4981e081 MH |
204 | rc = ecryptfs_init_persistent_file(dentry); |
205 | if (rc) { | |
206 | printk(KERN_ERR "%s: Error attempting to initialize the " | |
207 | "persistent file for the dentry with name [%s]; " | |
18d1dbf1 | 208 | "rc = [%d]\n", __func__, dentry->d_name.name, rc); |
4981e081 MH |
209 | goto out; |
210 | } | |
237fead6 MH |
211 | out: |
212 | return rc; | |
213 | } | |
214 | ||
2830bfd6 ES |
215 | enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, |
216 | ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher, | |
217 | ecryptfs_opt_ecryptfs_key_bytes, | |
17398957 MH |
218 | ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata, |
219 | ecryptfs_opt_encrypted_view, ecryptfs_opt_err }; | |
237fead6 MH |
220 | |
221 | static match_table_t tokens = { | |
222 | {ecryptfs_opt_sig, "sig=%s"}, | |
223 | {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"}, | |
237fead6 MH |
224 | {ecryptfs_opt_cipher, "cipher=%s"}, |
225 | {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"}, | |
226 | {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"}, | |
227 | {ecryptfs_opt_passthrough, "ecryptfs_passthrough"}, | |
17398957 MH |
228 | {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"}, |
229 | {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"}, | |
237fead6 MH |
230 | {ecryptfs_opt_err, NULL} |
231 | }; | |
232 | ||
f4aad16a MH |
233 | static int ecryptfs_init_global_auth_toks( |
234 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
237fead6 | 235 | { |
f4aad16a | 236 | struct ecryptfs_global_auth_tok *global_auth_tok; |
237fead6 | 237 | int rc = 0; |
237fead6 | 238 | |
f4aad16a MH |
239 | list_for_each_entry(global_auth_tok, |
240 | &mount_crypt_stat->global_auth_tok_list, | |
241 | mount_crypt_stat_list) { | |
5dda6992 MH |
242 | rc = ecryptfs_keyring_auth_tok_for_sig( |
243 | &global_auth_tok->global_auth_tok_key, | |
244 | &global_auth_tok->global_auth_tok, | |
245 | global_auth_tok->sig); | |
246 | if (rc) { | |
f4aad16a MH |
247 | printk(KERN_ERR "Could not find valid key in user " |
248 | "session keyring for sig specified in mount " | |
249 | "option: [%s]\n", global_auth_tok->sig); | |
250 | global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; | |
251 | rc = 0; | |
252 | } else | |
253 | global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; | |
237fead6 | 254 | } |
237fead6 MH |
255 | return rc; |
256 | } | |
257 | ||
f4aad16a MH |
258 | static void ecryptfs_init_mount_crypt_stat( |
259 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
260 | { | |
261 | memset((void *)mount_crypt_stat, 0, | |
262 | sizeof(struct ecryptfs_mount_crypt_stat)); | |
263 | INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list); | |
264 | mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex); | |
265 | mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED; | |
266 | } | |
267 | ||
237fead6 MH |
268 | /** |
269 | * ecryptfs_parse_options | |
270 | * @sb: The ecryptfs super block | |
271 | * @options: The options pased to the kernel | |
272 | * | |
273 | * Parse mount options: | |
274 | * debug=N - ecryptfs_verbosity level for debug output | |
275 | * sig=XXX - description(signature) of the key to use | |
276 | * | |
277 | * Returns the dentry object of the lower-level (lower/interposed) | |
278 | * directory; We want to mount our stackable file system on top of | |
279 | * that lower directory. | |
280 | * | |
281 | * The signature of the key to use must be the description of a key | |
282 | * already in the keyring. Mounting will fail if the key can not be | |
283 | * found. | |
284 | * | |
285 | * Returns zero on success; non-zero on error | |
286 | */ | |
287 | static int ecryptfs_parse_options(struct super_block *sb, char *options) | |
288 | { | |
289 | char *p; | |
290 | int rc = 0; | |
291 | int sig_set = 0; | |
292 | int cipher_name_set = 0; | |
293 | int cipher_key_bytes; | |
294 | int cipher_key_bytes_set = 0; | |
237fead6 MH |
295 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
296 | &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; | |
297 | substring_t args[MAX_OPT_ARGS]; | |
298 | int token; | |
299 | char *sig_src; | |
237fead6 MH |
300 | char *cipher_name_dst; |
301 | char *cipher_name_src; | |
302 | char *cipher_key_bytes_src; | |
237fead6 MH |
303 | int cipher_name_len; |
304 | ||
305 | if (!options) { | |
306 | rc = -EINVAL; | |
307 | goto out; | |
308 | } | |
956159c3 | 309 | ecryptfs_init_mount_crypt_stat(mount_crypt_stat); |
237fead6 MH |
310 | while ((p = strsep(&options, ",")) != NULL) { |
311 | if (!*p) | |
312 | continue; | |
313 | token = match_token(p, tokens, args); | |
314 | switch (token) { | |
315 | case ecryptfs_opt_sig: | |
316 | case ecryptfs_opt_ecryptfs_sig: | |
317 | sig_src = args[0].from; | |
f4aad16a MH |
318 | rc = ecryptfs_add_global_auth_tok(mount_crypt_stat, |
319 | sig_src); | |
320 | if (rc) { | |
321 | printk(KERN_ERR "Error attempting to register " | |
322 | "global sig; rc = [%d]\n", rc); | |
323 | goto out; | |
324 | } | |
237fead6 MH |
325 | sig_set = 1; |
326 | break; | |
237fead6 MH |
327 | case ecryptfs_opt_cipher: |
328 | case ecryptfs_opt_ecryptfs_cipher: | |
329 | cipher_name_src = args[0].from; | |
330 | cipher_name_dst = | |
331 | mount_crypt_stat-> | |
332 | global_default_cipher_name; | |
333 | strncpy(cipher_name_dst, cipher_name_src, | |
334 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); | |
335 | ecryptfs_printk(KERN_DEBUG, | |
336 | "The mount_crypt_stat " | |
337 | "global_default_cipher_name set to: " | |
338 | "[%s]\n", cipher_name_dst); | |
339 | cipher_name_set = 1; | |
340 | break; | |
341 | case ecryptfs_opt_ecryptfs_key_bytes: | |
342 | cipher_key_bytes_src = args[0].from; | |
343 | cipher_key_bytes = | |
344 | (int)simple_strtol(cipher_key_bytes_src, | |
345 | &cipher_key_bytes_src, 0); | |
346 | mount_crypt_stat->global_default_cipher_key_size = | |
347 | cipher_key_bytes; | |
348 | ecryptfs_printk(KERN_DEBUG, | |
349 | "The mount_crypt_stat " | |
350 | "global_default_cipher_key_size " | |
351 | "set to: [%d]\n", mount_crypt_stat-> | |
352 | global_default_cipher_key_size); | |
353 | cipher_key_bytes_set = 1; | |
354 | break; | |
355 | case ecryptfs_opt_passthrough: | |
356 | mount_crypt_stat->flags |= | |
357 | ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED; | |
358 | break; | |
17398957 MH |
359 | case ecryptfs_opt_xattr_metadata: |
360 | mount_crypt_stat->flags |= | |
361 | ECRYPTFS_XATTR_METADATA_ENABLED; | |
362 | break; | |
363 | case ecryptfs_opt_encrypted_view: | |
364 | mount_crypt_stat->flags |= | |
365 | ECRYPTFS_XATTR_METADATA_ENABLED; | |
366 | mount_crypt_stat->flags |= | |
367 | ECRYPTFS_ENCRYPTED_VIEW_ENABLED; | |
368 | break; | |
237fead6 MH |
369 | case ecryptfs_opt_err: |
370 | default: | |
371 | ecryptfs_printk(KERN_WARNING, | |
372 | "eCryptfs: unrecognized option '%s'\n", | |
373 | p); | |
374 | } | |
375 | } | |
237fead6 MH |
376 | if (!sig_set) { |
377 | rc = -EINVAL; | |
956159c3 MH |
378 | ecryptfs_printk(KERN_ERR, "You must supply at least one valid " |
379 | "auth tok signature as a mount " | |
237fead6 MH |
380 | "parameter; see the eCryptfs README\n"); |
381 | goto out; | |
382 | } | |
383 | if (!cipher_name_set) { | |
384 | cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER); | |
385 | if (unlikely(cipher_name_len | |
386 | >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) { | |
387 | rc = -EINVAL; | |
388 | BUG(); | |
389 | goto out; | |
390 | } | |
391 | memcpy(mount_crypt_stat->global_default_cipher_name, | |
392 | ECRYPTFS_DEFAULT_CIPHER, cipher_name_len); | |
393 | mount_crypt_stat->global_default_cipher_name[cipher_name_len] | |
394 | = '\0'; | |
395 | } | |
396 | if (!cipher_key_bytes_set) { | |
e5d9cbde | 397 | mount_crypt_stat->global_default_cipher_key_size = 0; |
237fead6 | 398 | } |
af440f52 ES |
399 | mutex_lock(&key_tfm_list_mutex); |
400 | if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name, | |
401 | NULL)) | |
402 | rc = ecryptfs_add_new_key_tfm( | |
403 | NULL, mount_crypt_stat->global_default_cipher_name, | |
404 | mount_crypt_stat->global_default_cipher_key_size); | |
405 | mutex_unlock(&key_tfm_list_mutex); | |
5dda6992 | 406 | if (rc) { |
f4aad16a | 407 | printk(KERN_ERR "Error attempting to initialize cipher with " |
81acbcd6 | 408 | "name = [%s] and key size = [%td]; rc = [%d]\n", |
237fead6 MH |
409 | mount_crypt_stat->global_default_cipher_name, |
410 | mount_crypt_stat->global_default_cipher_key_size, rc); | |
237fead6 MH |
411 | rc = -EINVAL; |
412 | goto out; | |
413 | } | |
5dda6992 MH |
414 | rc = ecryptfs_init_global_auth_toks(mount_crypt_stat); |
415 | if (rc) { | |
f4aad16a MH |
416 | printk(KERN_WARNING "One or more global auth toks could not " |
417 | "properly register; rc = [%d]\n", rc); | |
237fead6 | 418 | } |
f4aad16a | 419 | rc = 0; |
237fead6 MH |
420 | out: |
421 | return rc; | |
422 | } | |
423 | ||
424 | struct kmem_cache *ecryptfs_sb_info_cache; | |
425 | ||
426 | /** | |
427 | * ecryptfs_fill_super | |
428 | * @sb: The ecryptfs super block | |
429 | * @raw_data: The options passed to mount | |
430 | * @silent: Not used but required by function prototype | |
431 | * | |
432 | * Sets up what we can of the sb, rest is done in ecryptfs_read_super | |
433 | * | |
434 | * Returns zero on success; non-zero otherwise | |
435 | */ | |
436 | static int | |
437 | ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent) | |
438 | { | |
439 | int rc = 0; | |
440 | ||
441 | /* Released in ecryptfs_put_super() */ | |
442 | ecryptfs_set_superblock_private(sb, | |
c3762229 | 443 | kmem_cache_zalloc(ecryptfs_sb_info_cache, |
e94b1766 | 444 | GFP_KERNEL)); |
237fead6 MH |
445 | if (!ecryptfs_superblock_to_private(sb)) { |
446 | ecryptfs_printk(KERN_WARNING, "Out of memory\n"); | |
447 | rc = -ENOMEM; | |
448 | goto out; | |
449 | } | |
237fead6 MH |
450 | sb->s_op = &ecryptfs_sops; |
451 | /* Released through deactivate_super(sb) from get_sb_nodev */ | |
452 | sb->s_root = d_alloc(NULL, &(const struct qstr) { | |
453 | .hash = 0,.name = "/",.len = 1}); | |
454 | if (!sb->s_root) { | |
455 | ecryptfs_printk(KERN_ERR, "d_alloc failed\n"); | |
456 | rc = -ENOMEM; | |
457 | goto out; | |
458 | } | |
459 | sb->s_root->d_op = &ecryptfs_dops; | |
460 | sb->s_root->d_sb = sb; | |
461 | sb->s_root->d_parent = sb->s_root; | |
462 | /* Released in d_release when dput(sb->s_root) is called */ | |
463 | /* through deactivate_super(sb) from get_sb_nodev() */ | |
464 | ecryptfs_set_dentry_private(sb->s_root, | |
c3762229 | 465 | kmem_cache_zalloc(ecryptfs_dentry_info_cache, |
e94b1766 | 466 | GFP_KERNEL)); |
237fead6 MH |
467 | if (!ecryptfs_dentry_to_private(sb->s_root)) { |
468 | ecryptfs_printk(KERN_ERR, | |
469 | "dentry_info_cache alloc failed\n"); | |
470 | rc = -ENOMEM; | |
471 | goto out; | |
472 | } | |
237fead6 MH |
473 | rc = 0; |
474 | out: | |
475 | /* Should be able to rely on deactivate_super called from | |
476 | * get_sb_nodev */ | |
477 | return rc; | |
478 | } | |
479 | ||
480 | /** | |
481 | * ecryptfs_read_super | |
482 | * @sb: The ecryptfs super block | |
483 | * @dev_name: The path to mount over | |
484 | * | |
485 | * Read the super block of the lower filesystem, and use | |
486 | * ecryptfs_interpose to create our initial inode and super block | |
487 | * struct. | |
488 | */ | |
489 | static int ecryptfs_read_super(struct super_block *sb, const char *dev_name) | |
490 | { | |
491 | int rc; | |
492 | struct nameidata nd; | |
493 | struct dentry *lower_root; | |
494 | struct vfsmount *lower_mnt; | |
495 | ||
496 | memset(&nd, 0, sizeof(struct nameidata)); | |
82b16528 | 497 | rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd); |
237fead6 MH |
498 | if (rc) { |
499 | ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n"); | |
65dc8145 | 500 | goto out; |
237fead6 | 501 | } |
4ac91378 JB |
502 | lower_root = nd.path.dentry; |
503 | lower_mnt = nd.path.mnt; | |
237fead6 MH |
504 | ecryptfs_set_superblock_lower(sb, lower_root->d_sb); |
505 | sb->s_maxbytes = lower_root->d_sb->s_maxbytes; | |
7c9e70ef | 506 | sb->s_blocksize = lower_root->d_sb->s_blocksize; |
237fead6 MH |
507 | ecryptfs_set_dentry_lower(sb->s_root, lower_root); |
508 | ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt); | |
5dda6992 MH |
509 | rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0); |
510 | if (rc) | |
237fead6 MH |
511 | goto out_free; |
512 | rc = 0; | |
513 | goto out; | |
514 | out_free: | |
1d957f9b | 515 | path_put(&nd.path); |
237fead6 MH |
516 | out: |
517 | return rc; | |
518 | } | |
519 | ||
520 | /** | |
521 | * ecryptfs_get_sb | |
522 | * @fs_type | |
523 | * @flags | |
524 | * @dev_name: The path to mount over | |
525 | * @raw_data: The options passed into the kernel | |
526 | * | |
527 | * The whole ecryptfs_get_sb process is broken into 4 functions: | |
528 | * ecryptfs_parse_options(): handle options passed to ecryptfs, if any | |
529 | * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block | |
530 | * with as much information as it can before needing | |
531 | * the lower filesystem. | |
532 | * ecryptfs_read_super(): this accesses the lower filesystem and uses | |
533 | * ecryptfs_interpolate to perform most of the linking | |
534 | * ecryptfs_interpolate(): links the lower filesystem into ecryptfs | |
535 | */ | |
536 | static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags, | |
537 | const char *dev_name, void *raw_data, | |
538 | struct vfsmount *mnt) | |
539 | { | |
540 | int rc; | |
541 | struct super_block *sb; | |
542 | ||
543 | rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt); | |
544 | if (rc < 0) { | |
545 | printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc); | |
546 | goto out; | |
547 | } | |
548 | sb = mnt->mnt_sb; | |
549 | rc = ecryptfs_parse_options(sb, raw_data); | |
550 | if (rc) { | |
551 | printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc); | |
552 | goto out_abort; | |
553 | } | |
554 | rc = ecryptfs_read_super(sb, dev_name); | |
555 | if (rc) { | |
556 | printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc); | |
557 | goto out_abort; | |
558 | } | |
559 | goto out; | |
560 | out_abort: | |
561 | dput(sb->s_root); | |
562 | up_write(&sb->s_umount); | |
563 | deactivate_super(sb); | |
564 | out: | |
565 | return rc; | |
566 | } | |
567 | ||
568 | /** | |
569 | * ecryptfs_kill_block_super | |
570 | * @sb: The ecryptfs super block | |
571 | * | |
572 | * Used to bring the superblock down and free the private data. | |
573 | * Private data is free'd in ecryptfs_put_super() | |
574 | */ | |
575 | static void ecryptfs_kill_block_super(struct super_block *sb) | |
576 | { | |
577 | generic_shutdown_super(sb); | |
578 | } | |
579 | ||
580 | static struct file_system_type ecryptfs_fs_type = { | |
581 | .owner = THIS_MODULE, | |
582 | .name = "ecryptfs", | |
583 | .get_sb = ecryptfs_get_sb, | |
584 | .kill_sb = ecryptfs_kill_block_super, | |
585 | .fs_flags = 0 | |
586 | }; | |
587 | ||
588 | /** | |
589 | * inode_info_init_once | |
590 | * | |
591 | * Initializes the ecryptfs_inode_info_cache when it is created | |
592 | */ | |
593 | static void | |
4ba9b9d0 | 594 | inode_info_init_once(struct kmem_cache *cachep, void *vptr) |
237fead6 MH |
595 | { |
596 | struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; | |
597 | ||
a35afb83 | 598 | inode_init_once(&ei->vfs_inode); |
237fead6 MH |
599 | } |
600 | ||
601 | static struct ecryptfs_cache_info { | |
e18b890b | 602 | struct kmem_cache **cache; |
237fead6 MH |
603 | const char *name; |
604 | size_t size; | |
4ba9b9d0 | 605 | void (*ctor)(struct kmem_cache *cache, void *obj); |
237fead6 MH |
606 | } ecryptfs_cache_infos[] = { |
607 | { | |
608 | .cache = &ecryptfs_auth_tok_list_item_cache, | |
609 | .name = "ecryptfs_auth_tok_list_item", | |
610 | .size = sizeof(struct ecryptfs_auth_tok_list_item), | |
611 | }, | |
612 | { | |
613 | .cache = &ecryptfs_file_info_cache, | |
614 | .name = "ecryptfs_file_cache", | |
615 | .size = sizeof(struct ecryptfs_file_info), | |
616 | }, | |
617 | { | |
618 | .cache = &ecryptfs_dentry_info_cache, | |
619 | .name = "ecryptfs_dentry_info_cache", | |
620 | .size = sizeof(struct ecryptfs_dentry_info), | |
621 | }, | |
622 | { | |
623 | .cache = &ecryptfs_inode_info_cache, | |
624 | .name = "ecryptfs_inode_cache", | |
625 | .size = sizeof(struct ecryptfs_inode_info), | |
626 | .ctor = inode_info_init_once, | |
627 | }, | |
628 | { | |
629 | .cache = &ecryptfs_sb_info_cache, | |
630 | .name = "ecryptfs_sb_cache", | |
631 | .size = sizeof(struct ecryptfs_sb_info), | |
632 | }, | |
237fead6 MH |
633 | { |
634 | .cache = &ecryptfs_header_cache_1, | |
635 | .name = "ecryptfs_headers_1", | |
636 | .size = PAGE_CACHE_SIZE, | |
637 | }, | |
638 | { | |
639 | .cache = &ecryptfs_header_cache_2, | |
640 | .name = "ecryptfs_headers_2", | |
641 | .size = PAGE_CACHE_SIZE, | |
642 | }, | |
dd2a3b7a MH |
643 | { |
644 | .cache = &ecryptfs_xattr_cache, | |
645 | .name = "ecryptfs_xattr_cache", | |
646 | .size = PAGE_CACHE_SIZE, | |
647 | }, | |
eb95e7ff MH |
648 | { |
649 | .cache = &ecryptfs_key_record_cache, | |
650 | .name = "ecryptfs_key_record_cache", | |
651 | .size = sizeof(struct ecryptfs_key_record), | |
652 | }, | |
956159c3 MH |
653 | { |
654 | .cache = &ecryptfs_key_sig_cache, | |
655 | .name = "ecryptfs_key_sig_cache", | |
656 | .size = sizeof(struct ecryptfs_key_sig), | |
657 | }, | |
658 | { | |
659 | .cache = &ecryptfs_global_auth_tok_cache, | |
660 | .name = "ecryptfs_global_auth_tok_cache", | |
661 | .size = sizeof(struct ecryptfs_global_auth_tok), | |
662 | }, | |
663 | { | |
664 | .cache = &ecryptfs_key_tfm_cache, | |
665 | .name = "ecryptfs_key_tfm_cache", | |
666 | .size = sizeof(struct ecryptfs_key_tfm), | |
667 | }, | |
746f1e55 MH |
668 | { |
669 | .cache = &ecryptfs_open_req_cache, | |
670 | .name = "ecryptfs_open_req_cache", | |
671 | .size = sizeof(struct ecryptfs_open_req), | |
672 | }, | |
237fead6 MH |
673 | }; |
674 | ||
675 | static void ecryptfs_free_kmem_caches(void) | |
676 | { | |
677 | int i; | |
678 | ||
679 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { | |
680 | struct ecryptfs_cache_info *info; | |
681 | ||
682 | info = &ecryptfs_cache_infos[i]; | |
683 | if (*(info->cache)) | |
684 | kmem_cache_destroy(*(info->cache)); | |
685 | } | |
686 | } | |
687 | ||
688 | /** | |
689 | * ecryptfs_init_kmem_caches | |
690 | * | |
691 | * Returns zero on success; non-zero otherwise | |
692 | */ | |
693 | static int ecryptfs_init_kmem_caches(void) | |
694 | { | |
695 | int i; | |
696 | ||
697 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { | |
698 | struct ecryptfs_cache_info *info; | |
699 | ||
700 | info = &ecryptfs_cache_infos[i]; | |
701 | *(info->cache) = kmem_cache_create(info->name, info->size, | |
20c2df83 | 702 | 0, SLAB_HWCACHE_ALIGN, info->ctor); |
237fead6 MH |
703 | if (!*(info->cache)) { |
704 | ecryptfs_free_kmem_caches(); | |
705 | ecryptfs_printk(KERN_WARNING, "%s: " | |
706 | "kmem_cache_create failed\n", | |
707 | info->name); | |
708 | return -ENOMEM; | |
709 | } | |
710 | } | |
711 | return 0; | |
712 | } | |
713 | ||
6e90aa97 | 714 | static struct kobject *ecryptfs_kobj; |
237fead6 | 715 | |
386f275f KS |
716 | static ssize_t version_show(struct kobject *kobj, |
717 | struct kobj_attribute *attr, char *buff) | |
237fead6 MH |
718 | { |
719 | return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK); | |
720 | } | |
721 | ||
386f275f | 722 | static struct kobj_attribute version_attr = __ATTR_RO(version); |
237fead6 | 723 | |
30a468b1 GKH |
724 | static struct attribute *attributes[] = { |
725 | &version_attr.attr, | |
30a468b1 GKH |
726 | NULL, |
727 | }; | |
728 | ||
729 | static struct attribute_group attr_group = { | |
730 | .attrs = attributes, | |
731 | }; | |
237fead6 MH |
732 | |
733 | static int do_sysfs_registration(void) | |
734 | { | |
735 | int rc; | |
736 | ||
6e90aa97 GKH |
737 | ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj); |
738 | if (!ecryptfs_kobj) { | |
917e865d GKH |
739 | printk(KERN_ERR "Unable to create ecryptfs kset\n"); |
740 | rc = -ENOMEM; | |
237fead6 MH |
741 | goto out; |
742 | } | |
6e90aa97 | 743 | rc = sysfs_create_group(ecryptfs_kobj, &attr_group); |
237fead6 MH |
744 | if (rc) { |
745 | printk(KERN_ERR | |
30a468b1 | 746 | "Unable to create ecryptfs version attributes\n"); |
197b12d6 | 747 | kobject_put(ecryptfs_kobj); |
237fead6 MH |
748 | } |
749 | out: | |
750 | return rc; | |
751 | } | |
752 | ||
a75de1b3 RK |
753 | static void do_sysfs_unregistration(void) |
754 | { | |
6e90aa97 | 755 | sysfs_remove_group(ecryptfs_kobj, &attr_group); |
197b12d6 | 756 | kobject_put(ecryptfs_kobj); |
a75de1b3 RK |
757 | } |
758 | ||
237fead6 MH |
759 | static int __init ecryptfs_init(void) |
760 | { | |
761 | int rc; | |
762 | ||
763 | if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { | |
764 | rc = -EINVAL; | |
765 | ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " | |
766 | "larger than the host's page size, and so " | |
767 | "eCryptfs cannot run on this system. The " | |
768 | "default eCryptfs extent size is [%d] bytes; " | |
769 | "the page size is [%d] bytes.\n", | |
770 | ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE); | |
771 | goto out; | |
772 | } | |
773 | rc = ecryptfs_init_kmem_caches(); | |
774 | if (rc) { | |
775 | printk(KERN_ERR | |
776 | "Failed to allocate one or more kmem_cache objects\n"); | |
777 | goto out; | |
778 | } | |
779 | rc = register_filesystem(&ecryptfs_fs_type); | |
780 | if (rc) { | |
781 | printk(KERN_ERR "Failed to register filesystem\n"); | |
cf81f89d | 782 | goto out_free_kmem_caches; |
237fead6 | 783 | } |
237fead6 MH |
784 | rc = do_sysfs_registration(); |
785 | if (rc) { | |
786 | printk(KERN_ERR "sysfs registration failed\n"); | |
cf81f89d | 787 | goto out_unregister_filesystem; |
237fead6 | 788 | } |
746f1e55 MH |
789 | rc = ecryptfs_init_kthread(); |
790 | if (rc) { | |
791 | printk(KERN_ERR "%s: kthread initialization failed; " | |
792 | "rc = [%d]\n", __func__, rc); | |
793 | goto out_do_sysfs_unregistration; | |
794 | } | |
dddfa461 MH |
795 | rc = ecryptfs_init_messaging(ecryptfs_transport); |
796 | if (rc) { | |
746f1e55 | 797 | printk(KERN_ERR "Failure occured while attempting to " |
dddfa461 | 798 | "initialize the eCryptfs netlink socket\n"); |
746f1e55 | 799 | goto out_destroy_kthread; |
956159c3 MH |
800 | } |
801 | rc = ecryptfs_init_crypto(); | |
802 | if (rc) { | |
803 | printk(KERN_ERR "Failure whilst attempting to init crypto; " | |
804 | "rc = [%d]\n", rc); | |
cf81f89d | 805 | goto out_release_messaging; |
dddfa461 | 806 | } |
2830bfd6 ES |
807 | if (ecryptfs_verbosity > 0) |
808 | printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values " | |
809 | "will be written to the syslog!\n", ecryptfs_verbosity); | |
810 | ||
cf81f89d MH |
811 | goto out; |
812 | out_release_messaging: | |
813 | ecryptfs_release_messaging(ecryptfs_transport); | |
746f1e55 MH |
814 | out_destroy_kthread: |
815 | ecryptfs_destroy_kthread(); | |
cf81f89d MH |
816 | out_do_sysfs_unregistration: |
817 | do_sysfs_unregistration(); | |
818 | out_unregister_filesystem: | |
819 | unregister_filesystem(&ecryptfs_fs_type); | |
820 | out_free_kmem_caches: | |
821 | ecryptfs_free_kmem_caches(); | |
237fead6 MH |
822 | out: |
823 | return rc; | |
824 | } | |
825 | ||
826 | static void __exit ecryptfs_exit(void) | |
827 | { | |
cf81f89d MH |
828 | int rc; |
829 | ||
830 | rc = ecryptfs_destroy_crypto(); | |
831 | if (rc) | |
832 | printk(KERN_ERR "Failure whilst attempting to destroy crypto; " | |
833 | "rc = [%d]\n", rc); | |
dddfa461 | 834 | ecryptfs_release_messaging(ecryptfs_transport); |
746f1e55 | 835 | ecryptfs_destroy_kthread(); |
cf81f89d | 836 | do_sysfs_unregistration(); |
237fead6 MH |
837 | unregister_filesystem(&ecryptfs_fs_type); |
838 | ecryptfs_free_kmem_caches(); | |
839 | } | |
840 | ||
841 | MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>"); | |
842 | MODULE_DESCRIPTION("eCryptfs"); | |
843 | ||
844 | MODULE_LICENSE("GPL"); | |
845 | ||
846 | module_init(ecryptfs_init) | |
847 | module_exit(ecryptfs_exit) |