Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/ecryptfs/main.c
26282 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* eCryptfs: Linux filesystem encryption layer
4
*
5
* Copyright (C) 1997-2003 Erez Zadok
6
* Copyright (C) 2001-2003 Stony Brook University
7
* Copyright (C) 2004-2007 International Business Machines Corp.
8
* Author(s): Michael A. Halcrow <[email protected]>
9
* Michael C. Thompson <[email protected]>
10
* Tyler Hicks <[email protected]>
11
*/
12
13
#include <linux/dcache.h>
14
#include <linux/file.h>
15
#include <linux/module.h>
16
#include <linux/namei.h>
17
#include <linux/skbuff.h>
18
#include <linux/pagemap.h>
19
#include <linux/key.h>
20
#include <linux/fs_context.h>
21
#include <linux/fs_parser.h>
22
#include <linux/fs_stack.h>
23
#include <linux/sysfs.h>
24
#include <linux/slab.h>
25
#include <linux/magic.h>
26
#include "ecryptfs_kernel.h"
27
28
/*
29
* Module parameter that defines the ecryptfs_verbosity level.
30
*/
31
int ecryptfs_verbosity = 0;
32
33
module_param(ecryptfs_verbosity, int, 0);
34
MODULE_PARM_DESC(ecryptfs_verbosity,
35
"Initial verbosity level (0 or 1; defaults to "
36
"0, which is Quiet)");
37
38
/*
39
* Module parameter that defines the number of message buffer elements
40
*/
41
unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
42
43
module_param(ecryptfs_message_buf_len, uint, 0);
44
MODULE_PARM_DESC(ecryptfs_message_buf_len,
45
"Number of message buffer elements");
46
47
/*
48
* Module parameter that defines the maximum guaranteed amount of time to wait
49
* for a response from ecryptfsd. The actual sleep time will be, more than
50
* likely, a small amount greater than this specified value, but only less if
51
* the message successfully arrives.
52
*/
53
signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
54
55
module_param(ecryptfs_message_wait_timeout, long, 0);
56
MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
57
"Maximum number of seconds that an operation will "
58
"sleep while waiting for a message response from "
59
"userspace");
60
61
/*
62
* Module parameter that is an estimate of the maximum number of users
63
* that will be concurrently using eCryptfs. Set this to the right
64
* value to balance performance and memory use.
65
*/
66
unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
67
68
module_param(ecryptfs_number_of_users, uint, 0);
69
MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
70
"concurrent users of eCryptfs");
71
72
void __ecryptfs_printk(const char *fmt, ...)
73
{
74
va_list args;
75
va_start(args, fmt);
76
if (fmt[1] == '7') { /* KERN_DEBUG */
77
if (ecryptfs_verbosity >= 1)
78
vprintk(fmt, args);
79
} else
80
vprintk(fmt, args);
81
va_end(args);
82
}
83
84
/*
85
* ecryptfs_init_lower_file
86
* @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
87
* the lower dentry and the lower mount set
88
*
89
* eCryptfs only ever keeps a single open file for every lower
90
* inode. All I/O operations to the lower inode occur through that
91
* file. When the first eCryptfs dentry that interposes with the first
92
* lower dentry for that inode is created, this function creates the
93
* lower file struct and associates it with the eCryptfs
94
* inode. When all eCryptfs files associated with the inode are released, the
95
* file is closed.
96
*
97
* The lower file will be opened with read/write permissions, if
98
* possible. Otherwise, it is opened read-only.
99
*
100
* This function does nothing if a lower file is already
101
* associated with the eCryptfs inode.
102
*
103
* Returns zero on success; non-zero otherwise
104
*/
105
static int ecryptfs_init_lower_file(struct dentry *dentry,
106
struct file **lower_file)
107
{
108
const struct cred *cred = current_cred();
109
const struct path *path = ecryptfs_dentry_to_lower_path(dentry);
110
int rc;
111
112
rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
113
cred);
114
if (rc) {
115
printk(KERN_ERR "Error opening lower file "
116
"for lower_dentry [0x%p] and lower_mnt [0x%p]; "
117
"rc = [%d]\n", path->dentry, path->mnt, rc);
118
(*lower_file) = NULL;
119
}
120
return rc;
121
}
122
123
int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
124
{
125
struct ecryptfs_inode_info *inode_info;
126
int count, rc = 0;
127
128
inode_info = ecryptfs_inode_to_private(inode);
129
mutex_lock(&inode_info->lower_file_mutex);
130
count = atomic_inc_return(&inode_info->lower_file_count);
131
if (WARN_ON_ONCE(count < 1))
132
rc = -EINVAL;
133
else if (count == 1) {
134
rc = ecryptfs_init_lower_file(dentry,
135
&inode_info->lower_file);
136
if (rc)
137
atomic_set(&inode_info->lower_file_count, 0);
138
}
139
mutex_unlock(&inode_info->lower_file_mutex);
140
return rc;
141
}
142
143
void ecryptfs_put_lower_file(struct inode *inode)
144
{
145
struct ecryptfs_inode_info *inode_info;
146
147
inode_info = ecryptfs_inode_to_private(inode);
148
if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
149
&inode_info->lower_file_mutex)) {
150
filemap_write_and_wait(inode->i_mapping);
151
fput(inode_info->lower_file);
152
inode_info->lower_file = NULL;
153
mutex_unlock(&inode_info->lower_file_mutex);
154
}
155
}
156
157
enum {
158
Opt_sig, Opt_ecryptfs_sig, Opt_cipher, Opt_ecryptfs_cipher,
159
Opt_ecryptfs_key_bytes, Opt_passthrough, Opt_xattr_metadata,
160
Opt_encrypted_view, Opt_fnek_sig, Opt_fn_cipher,
161
Opt_fn_cipher_key_bytes, Opt_unlink_sigs, Opt_mount_auth_tok_only,
162
Opt_check_dev_ruid
163
};
164
165
static const struct fs_parameter_spec ecryptfs_fs_param_spec[] = {
166
fsparam_string ("sig", Opt_sig),
167
fsparam_string ("ecryptfs_sig", Opt_ecryptfs_sig),
168
fsparam_string ("cipher", Opt_cipher),
169
fsparam_string ("ecryptfs_cipher", Opt_ecryptfs_cipher),
170
fsparam_u32 ("ecryptfs_key_bytes", Opt_ecryptfs_key_bytes),
171
fsparam_flag ("ecryptfs_passthrough", Opt_passthrough),
172
fsparam_flag ("ecryptfs_xattr_metadata", Opt_xattr_metadata),
173
fsparam_flag ("ecryptfs_encrypted_view", Opt_encrypted_view),
174
fsparam_string ("ecryptfs_fnek_sig", Opt_fnek_sig),
175
fsparam_string ("ecryptfs_fn_cipher", Opt_fn_cipher),
176
fsparam_u32 ("ecryptfs_fn_key_bytes", Opt_fn_cipher_key_bytes),
177
fsparam_flag ("ecryptfs_unlink_sigs", Opt_unlink_sigs),
178
fsparam_flag ("ecryptfs_mount_auth_tok_only", Opt_mount_auth_tok_only),
179
fsparam_flag ("ecryptfs_check_dev_ruid", Opt_check_dev_ruid),
180
{}
181
};
182
183
static int ecryptfs_init_global_auth_toks(
184
struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
185
{
186
struct ecryptfs_global_auth_tok *global_auth_tok;
187
struct ecryptfs_auth_tok *auth_tok;
188
int rc = 0;
189
190
list_for_each_entry(global_auth_tok,
191
&mount_crypt_stat->global_auth_tok_list,
192
mount_crypt_stat_list) {
193
rc = ecryptfs_keyring_auth_tok_for_sig(
194
&global_auth_tok->global_auth_tok_key, &auth_tok,
195
global_auth_tok->sig);
196
if (rc) {
197
printk(KERN_ERR "Could not find valid key in user "
198
"session keyring for sig specified in mount "
199
"option: [%s]\n", global_auth_tok->sig);
200
global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
201
goto out;
202
} else {
203
global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
204
up_write(&(global_auth_tok->global_auth_tok_key)->sem);
205
}
206
}
207
out:
208
return rc;
209
}
210
211
static void ecryptfs_init_mount_crypt_stat(
212
struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
213
{
214
memset((void *)mount_crypt_stat, 0,
215
sizeof(struct ecryptfs_mount_crypt_stat));
216
INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
217
mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
218
mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
219
}
220
221
struct ecryptfs_fs_context {
222
/* Mount option status trackers */
223
bool check_ruid;
224
bool sig_set;
225
bool cipher_name_set;
226
bool cipher_key_bytes_set;
227
bool fn_cipher_name_set;
228
bool fn_cipher_key_bytes_set;
229
};
230
231
/**
232
* ecryptfs_parse_param
233
* @fc: The ecryptfs filesystem context
234
* @param: The mount parameter to parse
235
*
236
* The signature of the key to use must be the description of a key
237
* already in the keyring. Mounting will fail if the key can not be
238
* found.
239
*
240
* Returns zero on success; non-zero on error
241
*/
242
static int ecryptfs_parse_param(
243
struct fs_context *fc,
244
struct fs_parameter *param)
245
{
246
int rc;
247
int opt;
248
struct fs_parse_result result;
249
struct ecryptfs_fs_context *ctx = fc->fs_private;
250
struct ecryptfs_sb_info *sbi = fc->s_fs_info;
251
struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
252
&sbi->mount_crypt_stat;
253
254
opt = fs_parse(fc, ecryptfs_fs_param_spec, param, &result);
255
if (opt < 0)
256
return opt;
257
258
switch (opt) {
259
case Opt_sig:
260
case Opt_ecryptfs_sig:
261
rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
262
param->string, 0);
263
if (rc) {
264
printk(KERN_ERR "Error attempting to register "
265
"global sig; rc = [%d]\n", rc);
266
return rc;
267
}
268
ctx->sig_set = 1;
269
break;
270
case Opt_cipher:
271
case Opt_ecryptfs_cipher:
272
strscpy(mount_crypt_stat->global_default_cipher_name,
273
param->string);
274
ctx->cipher_name_set = 1;
275
break;
276
case Opt_ecryptfs_key_bytes:
277
mount_crypt_stat->global_default_cipher_key_size =
278
result.uint_32;
279
ctx->cipher_key_bytes_set = 1;
280
break;
281
case Opt_passthrough:
282
mount_crypt_stat->flags |=
283
ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
284
break;
285
case Opt_xattr_metadata:
286
mount_crypt_stat->flags |= ECRYPTFS_XATTR_METADATA_ENABLED;
287
break;
288
case Opt_encrypted_view:
289
mount_crypt_stat->flags |= ECRYPTFS_XATTR_METADATA_ENABLED;
290
mount_crypt_stat->flags |= ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
291
break;
292
case Opt_fnek_sig:
293
strscpy(mount_crypt_stat->global_default_fnek_sig,
294
param->string);
295
rc = ecryptfs_add_global_auth_tok(
296
mount_crypt_stat,
297
mount_crypt_stat->global_default_fnek_sig,
298
ECRYPTFS_AUTH_TOK_FNEK);
299
if (rc) {
300
printk(KERN_ERR "Error attempting to register "
301
"global fnek sig [%s]; rc = [%d]\n",
302
mount_crypt_stat->global_default_fnek_sig, rc);
303
return rc;
304
}
305
mount_crypt_stat->flags |=
306
(ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
307
| ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
308
break;
309
case Opt_fn_cipher:
310
strscpy(mount_crypt_stat->global_default_fn_cipher_name,
311
param->string);
312
ctx->fn_cipher_name_set = 1;
313
break;
314
case Opt_fn_cipher_key_bytes:
315
mount_crypt_stat->global_default_fn_cipher_key_bytes =
316
result.uint_32;
317
ctx->fn_cipher_key_bytes_set = 1;
318
break;
319
case Opt_unlink_sigs:
320
mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
321
break;
322
case Opt_mount_auth_tok_only:
323
mount_crypt_stat->flags |= ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
324
break;
325
case Opt_check_dev_ruid:
326
ctx->check_ruid = 1;
327
break;
328
default:
329
return -EINVAL;
330
}
331
332
return 0;
333
}
334
335
static int ecryptfs_validate_options(struct fs_context *fc)
336
{
337
int rc = 0;
338
u8 cipher_code;
339
struct ecryptfs_fs_context *ctx = fc->fs_private;
340
struct ecryptfs_sb_info *sbi = fc->s_fs_info;
341
struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
342
343
344
mount_crypt_stat = &sbi->mount_crypt_stat;
345
346
if (!ctx->sig_set) {
347
rc = -EINVAL;
348
ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
349
"auth tok signature as a mount "
350
"parameter; see the eCryptfs README\n");
351
goto out;
352
}
353
if (!ctx->cipher_name_set) {
354
int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
355
356
BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
357
strcpy(mount_crypt_stat->global_default_cipher_name,
358
ECRYPTFS_DEFAULT_CIPHER);
359
}
360
if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
361
&& !ctx->fn_cipher_name_set)
362
strcpy(mount_crypt_stat->global_default_fn_cipher_name,
363
mount_crypt_stat->global_default_cipher_name);
364
if (!ctx->cipher_key_bytes_set)
365
mount_crypt_stat->global_default_cipher_key_size = 0;
366
if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
367
&& !ctx->fn_cipher_key_bytes_set)
368
mount_crypt_stat->global_default_fn_cipher_key_bytes =
369
mount_crypt_stat->global_default_cipher_key_size;
370
371
cipher_code = ecryptfs_code_for_cipher_string(
372
mount_crypt_stat->global_default_cipher_name,
373
mount_crypt_stat->global_default_cipher_key_size);
374
if (!cipher_code) {
375
ecryptfs_printk(KERN_ERR,
376
"eCryptfs doesn't support cipher: %s\n",
377
mount_crypt_stat->global_default_cipher_name);
378
rc = -EINVAL;
379
goto out;
380
}
381
382
mutex_lock(&key_tfm_list_mutex);
383
if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
384
NULL)) {
385
rc = ecryptfs_add_new_key_tfm(
386
NULL, mount_crypt_stat->global_default_cipher_name,
387
mount_crypt_stat->global_default_cipher_key_size);
388
if (rc) {
389
printk(KERN_ERR "Error attempting to initialize "
390
"cipher with name = [%s] and key size = [%td]; "
391
"rc = [%d]\n",
392
mount_crypt_stat->global_default_cipher_name,
393
mount_crypt_stat->global_default_cipher_key_size,
394
rc);
395
rc = -EINVAL;
396
mutex_unlock(&key_tfm_list_mutex);
397
goto out;
398
}
399
}
400
if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
401
&& !ecryptfs_tfm_exists(
402
mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
403
rc = ecryptfs_add_new_key_tfm(
404
NULL, mount_crypt_stat->global_default_fn_cipher_name,
405
mount_crypt_stat->global_default_fn_cipher_key_bytes);
406
if (rc) {
407
printk(KERN_ERR "Error attempting to initialize "
408
"cipher with name = [%s] and key size = [%td]; "
409
"rc = [%d]\n",
410
mount_crypt_stat->global_default_fn_cipher_name,
411
mount_crypt_stat->global_default_fn_cipher_key_bytes,
412
rc);
413
rc = -EINVAL;
414
mutex_unlock(&key_tfm_list_mutex);
415
goto out;
416
}
417
}
418
mutex_unlock(&key_tfm_list_mutex);
419
rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
420
if (rc)
421
printk(KERN_WARNING "One or more global auth toks could not "
422
"properly register; rc = [%d]\n", rc);
423
out:
424
return rc;
425
}
426
427
struct kmem_cache *ecryptfs_sb_info_cache;
428
static struct file_system_type ecryptfs_fs_type;
429
430
/*
431
* ecryptfs_get_tree
432
* @fc: The filesystem context
433
*/
434
static int ecryptfs_get_tree(struct fs_context *fc)
435
{
436
struct super_block *s;
437
struct ecryptfs_fs_context *ctx = fc->fs_private;
438
struct ecryptfs_sb_info *sbi = fc->s_fs_info;
439
struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
440
struct ecryptfs_dentry_info *root_info;
441
const char *err = "Getting sb failed";
442
struct inode *inode;
443
struct path path;
444
int rc;
445
446
if (!fc->source) {
447
rc = -EINVAL;
448
err = "Device name cannot be null";
449
goto out;
450
}
451
452
mount_crypt_stat = &sbi->mount_crypt_stat;
453
rc = ecryptfs_validate_options(fc);
454
if (rc) {
455
err = "Error validating options";
456
goto out;
457
}
458
459
s = sget_fc(fc, NULL, set_anon_super_fc);
460
if (IS_ERR(s)) {
461
rc = PTR_ERR(s);
462
goto out;
463
}
464
465
rc = super_setup_bdi(s);
466
if (rc)
467
goto out1;
468
469
ecryptfs_set_superblock_private(s, sbi);
470
471
/* ->kill_sb() will take care of sbi after that point */
472
sbi = NULL;
473
s->s_op = &ecryptfs_sops;
474
s->s_xattr = ecryptfs_xattr_handlers;
475
set_default_d_op(s, &ecryptfs_dops);
476
477
err = "Reading sb failed";
478
rc = kern_path(fc->source, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
479
if (rc) {
480
ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
481
goto out1;
482
}
483
if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
484
rc = -EINVAL;
485
printk(KERN_ERR "Mount on filesystem of type "
486
"eCryptfs explicitly disallowed due to "
487
"known incompatibilities\n");
488
goto out_free;
489
}
490
491
if (is_idmapped_mnt(path.mnt)) {
492
rc = -EINVAL;
493
printk(KERN_ERR "Mounting on idmapped mounts currently disallowed\n");
494
goto out_free;
495
}
496
497
if (ctx->check_ruid &&
498
!uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
499
rc = -EPERM;
500
printk(KERN_ERR "Mount of device (uid: %d) not owned by "
501
"requested user (uid: %d)\n",
502
i_uid_read(d_inode(path.dentry)),
503
from_kuid(&init_user_ns, current_uid()));
504
goto out_free;
505
}
506
507
ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
508
509
/**
510
* Set the POSIX ACL flag based on whether they're enabled in the lower
511
* mount.
512
*/
513
s->s_flags = fc->sb_flags & ~SB_POSIXACL;
514
s->s_flags |= path.dentry->d_sb->s_flags & SB_POSIXACL;
515
516
/**
517
* Force a read-only eCryptfs mount when:
518
* 1) The lower mount is ro
519
* 2) The ecryptfs_encrypted_view mount option is specified
520
*/
521
if (sb_rdonly(path.dentry->d_sb) || mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
522
s->s_flags |= SB_RDONLY;
523
524
s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
525
s->s_blocksize = path.dentry->d_sb->s_blocksize;
526
s->s_magic = ECRYPTFS_SUPER_MAGIC;
527
s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
528
529
rc = -EINVAL;
530
if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
531
pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
532
goto out_free;
533
}
534
535
inode = ecryptfs_get_inode(d_inode(path.dentry), s);
536
rc = PTR_ERR(inode);
537
if (IS_ERR(inode))
538
goto out_free;
539
540
s->s_root = d_make_root(inode);
541
if (!s->s_root) {
542
rc = -ENOMEM;
543
goto out_free;
544
}
545
546
rc = -ENOMEM;
547
root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
548
if (!root_info)
549
goto out_free;
550
551
/* ->kill_sb() will take care of root_info */
552
ecryptfs_set_dentry_private(s->s_root, root_info);
553
root_info->lower_path = path;
554
555
s->s_flags |= SB_ACTIVE;
556
fc->root = dget(s->s_root);
557
return 0;
558
559
out_free:
560
path_put(&path);
561
out1:
562
deactivate_locked_super(s);
563
out:
564
if (sbi)
565
ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
566
567
printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
568
return rc;
569
}
570
571
/**
572
* ecryptfs_kill_block_super
573
* @sb: The ecryptfs super block
574
*
575
* Used to bring the superblock down and free the private data.
576
*/
577
static void ecryptfs_kill_block_super(struct super_block *sb)
578
{
579
struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
580
kill_anon_super(sb);
581
if (!sb_info)
582
return;
583
ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
584
kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
585
}
586
587
static void ecryptfs_free_fc(struct fs_context *fc)
588
{
589
struct ecryptfs_fs_context *ctx = fc->fs_private;
590
struct ecryptfs_sb_info *sbi = fc->s_fs_info;
591
592
kfree(ctx);
593
594
if (sbi) {
595
ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
596
kmem_cache_free(ecryptfs_sb_info_cache, sbi);
597
}
598
}
599
600
static const struct fs_context_operations ecryptfs_context_ops = {
601
.free = ecryptfs_free_fc,
602
.parse_param = ecryptfs_parse_param,
603
.get_tree = ecryptfs_get_tree,
604
.reconfigure = NULL,
605
};
606
607
static int ecryptfs_init_fs_context(struct fs_context *fc)
608
{
609
struct ecryptfs_fs_context *ctx;
610
struct ecryptfs_sb_info *sbi = NULL;
611
612
ctx = kzalloc(sizeof(struct ecryptfs_fs_context), GFP_KERNEL);
613
if (!ctx)
614
return -ENOMEM;
615
sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
616
if (!sbi) {
617
kfree(ctx);
618
ctx = NULL;
619
return -ENOMEM;
620
}
621
622
ecryptfs_init_mount_crypt_stat(&sbi->mount_crypt_stat);
623
624
fc->fs_private = ctx;
625
fc->s_fs_info = sbi;
626
fc->ops = &ecryptfs_context_ops;
627
return 0;
628
}
629
630
static struct file_system_type ecryptfs_fs_type = {
631
.owner = THIS_MODULE,
632
.name = "ecryptfs",
633
.init_fs_context = ecryptfs_init_fs_context,
634
.parameters = ecryptfs_fs_param_spec,
635
.kill_sb = ecryptfs_kill_block_super,
636
.fs_flags = 0
637
};
638
MODULE_ALIAS_FS("ecryptfs");
639
640
/*
641
* inode_info_init_once
642
*
643
* Initializes the ecryptfs_inode_info_cache when it is created
644
*/
645
static void
646
inode_info_init_once(void *vptr)
647
{
648
struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
649
650
inode_init_once(&ei->vfs_inode);
651
}
652
653
static struct ecryptfs_cache_info {
654
struct kmem_cache **cache;
655
const char *name;
656
size_t size;
657
slab_flags_t flags;
658
void (*ctor)(void *obj);
659
} ecryptfs_cache_infos[] = {
660
{
661
.cache = &ecryptfs_auth_tok_list_item_cache,
662
.name = "ecryptfs_auth_tok_list_item",
663
.size = sizeof(struct ecryptfs_auth_tok_list_item),
664
},
665
{
666
.cache = &ecryptfs_file_info_cache,
667
.name = "ecryptfs_file_cache",
668
.size = sizeof(struct ecryptfs_file_info),
669
},
670
{
671
.cache = &ecryptfs_dentry_info_cache,
672
.name = "ecryptfs_dentry_info_cache",
673
.size = sizeof(struct ecryptfs_dentry_info),
674
},
675
{
676
.cache = &ecryptfs_inode_info_cache,
677
.name = "ecryptfs_inode_cache",
678
.size = sizeof(struct ecryptfs_inode_info),
679
.flags = SLAB_ACCOUNT,
680
.ctor = inode_info_init_once,
681
},
682
{
683
.cache = &ecryptfs_sb_info_cache,
684
.name = "ecryptfs_sb_cache",
685
.size = sizeof(struct ecryptfs_sb_info),
686
},
687
{
688
.cache = &ecryptfs_header_cache,
689
.name = "ecryptfs_headers",
690
.size = PAGE_SIZE,
691
},
692
{
693
.cache = &ecryptfs_xattr_cache,
694
.name = "ecryptfs_xattr_cache",
695
.size = PAGE_SIZE,
696
},
697
{
698
.cache = &ecryptfs_key_record_cache,
699
.name = "ecryptfs_key_record_cache",
700
.size = sizeof(struct ecryptfs_key_record),
701
},
702
{
703
.cache = &ecryptfs_key_sig_cache,
704
.name = "ecryptfs_key_sig_cache",
705
.size = sizeof(struct ecryptfs_key_sig),
706
},
707
{
708
.cache = &ecryptfs_global_auth_tok_cache,
709
.name = "ecryptfs_global_auth_tok_cache",
710
.size = sizeof(struct ecryptfs_global_auth_tok),
711
},
712
{
713
.cache = &ecryptfs_key_tfm_cache,
714
.name = "ecryptfs_key_tfm_cache",
715
.size = sizeof(struct ecryptfs_key_tfm),
716
},
717
};
718
719
static void ecryptfs_free_kmem_caches(void)
720
{
721
int i;
722
723
/*
724
* Make sure all delayed rcu free inodes are flushed before we
725
* destroy cache.
726
*/
727
rcu_barrier();
728
729
for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
730
struct ecryptfs_cache_info *info;
731
732
info = &ecryptfs_cache_infos[i];
733
kmem_cache_destroy(*(info->cache));
734
}
735
}
736
737
/**
738
* ecryptfs_init_kmem_caches
739
*
740
* Returns zero on success; non-zero otherwise
741
*/
742
static int ecryptfs_init_kmem_caches(void)
743
{
744
int i;
745
746
for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
747
struct ecryptfs_cache_info *info;
748
749
info = &ecryptfs_cache_infos[i];
750
*(info->cache) = kmem_cache_create(info->name, info->size, 0,
751
SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
752
if (!*(info->cache)) {
753
ecryptfs_free_kmem_caches();
754
ecryptfs_printk(KERN_WARNING, "%s: "
755
"kmem_cache_create failed\n",
756
info->name);
757
return -ENOMEM;
758
}
759
}
760
return 0;
761
}
762
763
static struct kobject *ecryptfs_kobj;
764
765
static ssize_t version_show(struct kobject *kobj,
766
struct kobj_attribute *attr, char *buff)
767
{
768
return sysfs_emit(buff, "%d\n", ECRYPTFS_VERSIONING_MASK);
769
}
770
771
static struct kobj_attribute version_attr = __ATTR_RO(version);
772
773
static struct attribute *attributes[] = {
774
&version_attr.attr,
775
NULL,
776
};
777
778
static const struct attribute_group attr_group = {
779
.attrs = attributes,
780
};
781
782
static int do_sysfs_registration(void)
783
{
784
int rc;
785
786
ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
787
if (!ecryptfs_kobj) {
788
printk(KERN_ERR "Unable to create ecryptfs kset\n");
789
rc = -ENOMEM;
790
goto out;
791
}
792
rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
793
if (rc) {
794
printk(KERN_ERR
795
"Unable to create ecryptfs version attributes\n");
796
kobject_put(ecryptfs_kobj);
797
}
798
out:
799
return rc;
800
}
801
802
static void do_sysfs_unregistration(void)
803
{
804
sysfs_remove_group(ecryptfs_kobj, &attr_group);
805
kobject_put(ecryptfs_kobj);
806
}
807
808
static int __init ecryptfs_init(void)
809
{
810
int rc;
811
812
if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
813
rc = -EINVAL;
814
ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
815
"larger than the host's page size, and so "
816
"eCryptfs cannot run on this system. The "
817
"default eCryptfs extent size is [%u] bytes; "
818
"the page size is [%lu] bytes.\n",
819
ECRYPTFS_DEFAULT_EXTENT_SIZE,
820
(unsigned long)PAGE_SIZE);
821
goto out;
822
}
823
rc = ecryptfs_init_kmem_caches();
824
if (rc) {
825
printk(KERN_ERR
826
"Failed to allocate one or more kmem_cache objects\n");
827
goto out;
828
}
829
rc = do_sysfs_registration();
830
if (rc) {
831
printk(KERN_ERR "sysfs registration failed\n");
832
goto out_free_kmem_caches;
833
}
834
rc = ecryptfs_init_kthread();
835
if (rc) {
836
printk(KERN_ERR "%s: kthread initialization failed; "
837
"rc = [%d]\n", __func__, rc);
838
goto out_do_sysfs_unregistration;
839
}
840
rc = ecryptfs_init_messaging();
841
if (rc) {
842
printk(KERN_ERR "Failure occurred while attempting to "
843
"initialize the communications channel to "
844
"ecryptfsd\n");
845
goto out_destroy_kthread;
846
}
847
rc = ecryptfs_init_crypto();
848
if (rc) {
849
printk(KERN_ERR "Failure whilst attempting to init crypto; "
850
"rc = [%d]\n", rc);
851
goto out_release_messaging;
852
}
853
rc = register_filesystem(&ecryptfs_fs_type);
854
if (rc) {
855
printk(KERN_ERR "Failed to register filesystem\n");
856
goto out_destroy_crypto;
857
}
858
if (ecryptfs_verbosity > 0)
859
printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
860
"will be written to the syslog!\n", ecryptfs_verbosity);
861
862
goto out;
863
out_destroy_crypto:
864
ecryptfs_destroy_crypto();
865
out_release_messaging:
866
ecryptfs_release_messaging();
867
out_destroy_kthread:
868
ecryptfs_destroy_kthread();
869
out_do_sysfs_unregistration:
870
do_sysfs_unregistration();
871
out_free_kmem_caches:
872
ecryptfs_free_kmem_caches();
873
out:
874
return rc;
875
}
876
877
static void __exit ecryptfs_exit(void)
878
{
879
int rc;
880
881
rc = ecryptfs_destroy_crypto();
882
if (rc)
883
printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
884
"rc = [%d]\n", rc);
885
ecryptfs_release_messaging();
886
ecryptfs_destroy_kthread();
887
do_sysfs_unregistration();
888
unregister_filesystem(&ecryptfs_fs_type);
889
ecryptfs_free_kmem_caches();
890
}
891
892
MODULE_AUTHOR("Michael A. Halcrow <[email protected]>");
893
MODULE_DESCRIPTION("eCryptfs");
894
895
MODULE_LICENSE("GPL");
896
897
module_init(ecryptfs_init)
898
module_exit(ecryptfs_exit)
899
900