/
main.c
510 lines (456 loc) · 13.8 KB
/
main.c
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
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
314
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Copyright (c) 1998-2011 Erez Zadok
* Copyright (c) 2009 Shrikar Archak
* Copyright (c) 2003-2011 Stony Brook University
* Copyright (c) 2003-2011 The Research Foundation of SUNY
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include "transcryptfs.h"
#include "crypto/hash.h"
#include "crypto/pbkdf2.h"
#include <linux/pagemap.h>
#include <linux/parser.h>
#include <linux/fs_stack.h>
#include <linux/module.h>
/* transcryptfs inode cache constructor */
static void inode_info_init_once(void *obj)
{
struct transcryptfs_inode_info *i = (struct transcryptfs_inode_info *)obj;
inode_init_once(&i->vfs_inode);
}
struct kmem_cache *transcryptfs_sb_info_cache;
// struct kmem_cache *transcryptfs_header_cache;
static struct file_system_type transcryptfs_fs_type;
static struct transcryptfs_cache_info {
struct kmem_cache **cache;
const char *name;
size_t size;
void (*ctor)(void *obj);
} transcryptfs_cache_infos[] = {
{
.cache = &transcryptfs_inode_info_cache,
.name = "transcryptfs_inode_cache",
.size = sizeof(struct transcryptfs_inode_info),
.ctor = inode_info_init_once,
},
{
.cache = &transcryptfs_dentry_info_cache,
.name = "transcryptfs_dentry_cache",
.size = sizeof(struct transcryptfs_dentry_info),
},
{
.cache = &transcryptfs_file_info_cache,
.name = "transcryptfs_file_cache",
.size = sizeof(struct transcryptfs_file_info),
},
{
.cache = &transcryptfs_open_req_cache,
.name = "transcryptfs_open_req_cache",
.size = sizeof(struct transcryptfs_open_req),
},
{
.cache = &transcryptfs_sb_info_cache,
.name = "transcryptfs_sb_cache",
.size = sizeof(struct transcryptfs_sb_info),
},
{
.cache = &transcryptfs_header_cache,
.name = "transcryptfs_headers",
.size = PAGE_CACHE_SIZE,
},
};
static int transcryptfs_init_lower_file(struct dentry *dentry,
struct file **lower_file)
{
const struct cred *cred = current_cred();
struct dentry *lower_dentry = transcryptfs_dentry_to_lower(dentry);
struct vfsmount *lower_mnt = transcryptfs_dentry_to_lower_mnt(dentry);
int err;
err = transcryptfs_privileged_open(lower_file, lower_dentry, lower_mnt,
cred);
if (err) {
printk(KERN_ERR "Error opening lower file "
"for lower_dentry [0x%p] and lower_mnt [0x%p]; "
"err = [%d]\n", lower_dentry, lower_mnt, err);
(*lower_file) = NULL;
}
return err;
}
int transcryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
{
struct transcryptfs_inode_info *inode_info;
int count, err = 0;
inode_info = TRANSCRYPTFS_I(inode);
mutex_lock(&inode_info->lower_file_mutex);
count = atomic_inc_return(&inode_info->lower_file_count);
if (WARN_ON_ONCE(count < 1))
err = -EINVAL;
else if (count == 1) {
err = transcryptfs_init_lower_file(dentry,
&inode_info->lower_file);
if (err)
atomic_set(&inode_info->lower_file_count, 0);
}
mutex_unlock(&inode_info->lower_file_mutex);
return err;
}
void transcryptfs_put_lower_file(struct inode *inode)
{
struct transcryptfs_inode_info *inode_info;
inode_info = TRANSCRYPTFS_I(inode);
if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
&inode_info->lower_file_mutex)) {
fput(inode_info->lower_file);
inode_info->lower_file = NULL;
mutex_unlock(&inode_info->lower_file_mutex);
}
}
/*
* There is no need to lock the transcryptfs_super_info's rwsem as there is no
* way anyone can have a reference to the superblock at this point in time.
*/
// static int transcryptfs_read_super(struct super_block *sb, void *raw_data, int silent)
// {
// int err = 0;
// struct super_block *lower_sb;
// struct path lower_path;
// char *dev_name = (char *) raw_data;
// struct inode *inode;
// printk(KERN_INFO
// "transcryptfs: Entered transcryptfs_read_super");
//
// if (!dev_name) {
// printk(KERN_ERR
// "transcryptfs: read_super: missing dev_name argument\n");
// err = -EINVAL;
// goto out;
// }
//
// /* parse lower path */
// err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
// &lower_path);
// if (err) {
// printk(KERN_ERR "transcryptfs: error accessing "
// "lower directory '%s'\n", dev_name);
// goto out;
// }
//
// /* allocate superblock private data */
// sb->s_fs_info = kzalloc(sizeof(struct transcryptfs_sb_info), GFP_KERNEL);
// if (!TRANSCRYPTFS_SB(sb)) {
// printk(KERN_CRIT "transcryptfs: read_super: out of memory\n");
// err = -ENOMEM;
// goto out_free;
// }
//
// /* set the lower superblock field of upper superblock */
// lower_sb = lower_path.dentry->d_sb;
// atomic_inc(&lower_sb->s_active);
// transcryptfs_set_lower_super(sb, lower_sb);
//
// /* inherit maxbytes from lower file system */
// sb->s_maxbytes = lower_sb->s_maxbytes;
//
// /*
// * Our c/m/atime granularity is 1 ns because we may stack on file
// * systems whose granularity is as good.
// */
// sb->s_time_gran = 1;
//
// sb->s_op = &transcryptfs_sops;
//
// /* get a new inode and allocate our root dentry */
// inode = transcryptfs_get_inode(lower_path.dentry->d_inode, sb);
// if (IS_ERR(inode)) {
// err = PTR_ERR(inode);
// goto out_sput;
// }
// sb->s_root = d_alloc_root(inode);
// if (!sb->s_root) {
// err = -ENOMEM;
// goto out_iput;
// }
// d_set_d_op(sb->s_root, &transcryptfs_dops);
//
// /* link the upper and lower dentries */
// sb->s_root->d_fsdata = NULL;
// err = new_dentry_private_data(sb->s_root);
// if (err)
// goto out_freeroot;
//
// /* if get here: cannot have error */
//
// /* set the lower dentries for s_root */
// transcryptfs_set_lower_path(sb->s_root, &lower_path);
//
// /*
// * No need to call interpose because we already have a positive
// * dentry, which was instantiated by d_alloc_root. Just need to
// * d_rehash it.
// */
// d_rehash(sb->s_root);
// if (!silent)
// printk(KERN_INFO
// "transcryptfs: mounted on top of %s type %s\n",
// dev_name, lower_sb->s_type->name);
// goto out; /* all is well */
//
// /* no longer needed: free_dentry_private_data(sb->s_root); */
// out_freeroot:
// dput(sb->s_root);
// out_iput:
// iput(inode);
// out_sput:
// /* drop refs we took earlier */
// atomic_dec(&lower_sb->s_active);
// kfree(TRANSCRYPTFS_SB(sb));
// sb->s_fs_info = NULL;
// out_free:
// path_put(&lower_path);
//
// out:
// return err;
// }
enum { transcryptfs_opt_mount_pass, transcryptfs_opt_err };
static const match_table_t tokens = {
{transcryptfs_opt_mount_pass, "pass=%s"},
{transcryptfs_opt_err, NULL}
};
static void transcryptfs_init_mount_crypt_stat(
struct transcryptfs_mount_crypt_stat *mount_crypt_stat)
{
memset((void *)mount_crypt_stat, 0,
sizeof(struct transcryptfs_mount_crypt_stat));
mount_crypt_stat->fsk_size = TRANSCRYPTFS_FSK_BYTES;
memcpy(mount_crypt_stat->global_blinding_cipher,
TRANSCRYPTFS_DEFAULT_BLINDING_CIPHER, 8);
mount_crypt_stat->global_blinding_cipher[8]='\0';
mount_crypt_stat->flags |= TRANSCRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
}
static int transcryptfs_parse_options(struct transcryptfs_sb_info *sbi, char *options,
uid_t *check_ruid)
{
int err = 0;
substring_t args[MAX_OPT_ARGS];
int token;
char *p, *passphrase, salt[SHA1_DIGEST_LENGTH];
int passlen;
struct transcryptfs_mount_crypt_stat *mount_crypt_stat =
&sbi->mount_crypt_stat;
*check_ruid = 0;
memset(salt, 0, SHA1_DIGEST_LENGTH);
if (!options) {
err = -EINVAL;
goto out;
}
transcryptfs_init_mount_crypt_stat(mount_crypt_stat);
// printk(KERN_ERR "raw_data= [%s]", options);
while ((p = strsep(&options, ",")) != NULL) {
if (!*p)
continue;
token = match_token(p, tokens, args);
switch (token) {
case transcryptfs_opt_mount_pass:
passphrase = args[0].from;
passlen = strlen(passphrase);
err = pkcs5_pbkdf2(passphrase, passlen, salt,
SHA1_DIGEST_LENGTH, mount_crypt_stat->fsk, 16, 10000);
break;
case transcryptfs_opt_err:
default:
printk(KERN_ERR "%s: Transcryptfs: unrecognized option!"
,__func__);
}
}
// memcpy(mount_crypt_stat->fsk, "TRANSCRYPTTRANSC", 16);
out:
return err;
}
struct dentry *transcryptfs_mount(struct file_system_type *fs_type, int flags,
const char *dev_name, void *raw_data)
{
struct super_block *s;
// void *lower_path_name = (void *) dev_name;
struct transcryptfs_sb_info *sbi;
struct transcryptfs_dentry_info *root_info;
struct inode *inode;
struct path path;
uid_t check_ruid;
int err;
sbi = kmem_cache_zalloc(transcryptfs_sb_info_cache, GFP_KERNEL);
if (!sbi) {
err = -ENOMEM;
goto out;
}
err = transcryptfs_parse_options(sbi, raw_data, &check_ruid);
if (err) {
printk( KERN_ERR "Error parsing options." );
goto out;
}
s = sget(fs_type, NULL, set_anon_super, NULL);
if (IS_ERR(s)) {
err = PTR_ERR(s);
goto out;
}
s->s_flags = flags;
err = bdi_setup_and_register(&sbi->bdi, "transcryptfs", BDI_CAP_MAP_COPY);
if (err)
goto out_deact_super;
s->s_fs_info = sbi;
s->s_bdi = &sbi->bdi;
sbi = NULL;
s->s_op = &transcryptfs_sops;
s->s_d_op = &transcryptfs_dops;
err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
if (err) {
printk(KERN_WARNING "kern_path() failed\n");
goto out;
}
if (path.dentry->d_sb->s_type == &transcryptfs_fs_type) {
err = -EINVAL;
printk(KERN_ERR "Can't mount on transcryptfs filesystem");
goto out_free;
}
transcryptfs_set_lower_super(s, path.dentry->d_sb);
s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
s->s_blocksize = path.dentry->d_sb->s_blocksize;
s->s_magic = TRANSCRYPTFS_SUPER_MAGIC;
inode = transcryptfs_get_inode(path.dentry->d_inode, s);
err = PTR_ERR(inode);
if (IS_ERR(inode))
goto out_free;
s->s_root = d_make_root(inode);
if (!s->s_root) {
iput(inode);
err = -ENOMEM;
goto out_free;
}
err = -ENOMEM;
root_info = kmem_cache_zalloc(transcryptfs_dentry_info_cache, GFP_KERNEL);
if(!root_info)
goto out_free;
transcryptfs_set_dentry_private(s->s_root, root_info);
transcryptfs_set_dentry_lower(s->s_root, path.dentry);
transcryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
s->s_flags |= MS_ACTIVE;
return dget(s->s_root);
out_free:
path_put(&path);
out_deact_super:
deactivate_locked_super(s);
out:
if (sbi)
kmem_cache_free(transcryptfs_sb_info_cache, sbi);
printk(KERN_ERR "Error while mounting!; err = [%d]\n", err);
return ERR_PTR(err);
// return mount_nodev(fs_type, flags, lower_path_name,
// transcryptfs_read_super);
}
static void transcryptfs_kill_block_super(struct super_block *sb)
{
struct transcryptfs_sb_info *sb_info = TRANSCRYPTFS_SB(sb);
kill_anon_super(sb);
if (!sb_info)
return;
transcryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
bdi_destroy(&sb_info->bdi);
kmem_cache_free(transcryptfs_sb_info_cache, sb_info);
}
static struct file_system_type transcryptfs_fs_type = {
.owner = THIS_MODULE,
.name = TRANSCRYPTFS_NAME,
.mount = transcryptfs_mount,
.kill_sb = transcryptfs_kill_block_super,
.fs_flags = 0,
};
static void transcryptfs_free_kmem_caches(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(transcryptfs_cache_infos); i++) {
struct transcryptfs_cache_info *info;
info = &transcryptfs_cache_infos[i];
if(*(info->cache))
kmem_cache_destroy(*(info->cache));
}
}
static int transcryptfs_init_kmem_caches(void)
{
int i;
// asm("int $3");
for (i = 0; i < ARRAY_SIZE(transcryptfs_cache_infos); i++) {
struct transcryptfs_cache_info *info;
info = &transcryptfs_cache_infos[i];
*(info->cache) = kmem_cache_create(info->name, info->size,
0, SLAB_HWCACHE_ALIGN, info->ctor);
// pr_info("The new address for %s is %p",info->name, info->cache);
if (!*(info->cache)) {
transcryptfs_free_kmem_caches();
printk(KERN_WARNING "%s: "
"kmem_cache_create failed\n",
info->name);
return -ENOMEM;
}
}
return 0;
}
static int __init transcryptfs_init(void)
{
int err = 0;
pr_info("Registering transcryptfs " TRANSCRYPTFS_VERSION "\n");
pr_info("inside init_transcryptfs_fs " TRANSCRYPTFS_VERSION "\n");
err = transcryptfs_init_kmem_caches();
if (err) {
printk(KERN_ERR
"Failed to allocate one or more kmem_cache objects\n");
goto out;
}
// err = transcryptfs_init_inode_cache();
// if (err) {
// printk(KERN_ERR
// "Failed to allocate inode cache objects\n");
// goto out;
// }
// err = transcryptfs_init_dentry_cache();
// if (err) {
// printk(KERN_ERR
// "Failed to allocate dentry cache objects\n");
// goto out_free_inode_cache;
// }
err = register_filesystem(&transcryptfs_fs_type);
if (err) {
printk(KERN_ERR
"Failed to register filesystem\n");
goto out_free_kmem_caches;
}
err = transcryptfs_init_kthread();
if (err) {
printk(KERN_ERR
"Failed to initialize kthread\n");
goto out_unregister_filesystem;
}
goto out;
out_unregister_filesystem:
unregister_filesystem(&transcryptfs_fs_type);
out_free_kmem_caches:
transcryptfs_free_kmem_caches();
out:
return err;
}
static void __exit transcryptfs_exit(void)
{
// transcryptfs_destroy_inode_cache();
// transcryptfs_destroy_dentry_cache();
// int err;
transcryptfs_destroy_kthread();
unregister_filesystem(&transcryptfs_fs_type);
transcryptfs_free_kmem_caches();
pr_info("Completed transcryptfs module unload\n");
}
MODULE_AUTHOR("Sourav, Adarsh");
MODULE_DESCRIPTION("Transcryptfs " TRANSCRYPTFS_VERSION);
MODULE_LICENSE("GPL");
module_init(transcryptfs_init);
module_exit(transcryptfs_exit);