-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_xcrypt.c
More file actions
1145 lines (951 loc) · 31.1 KB
/
sys_xcrypt.c
File metadata and controls
1145 lines (951 loc) · 31.1 KB
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <linux/linkage.h>
#include <linux/moduleloader.h>
#include <linux/mm.h> // for verify_area: defined in uacces.h
#include <linux/highmem.h>
#include <linux/slab.h>// for kmalloc and kfree
#include <linux/fs.h> // for file api
#include <linux/types.h>// for u8
#include <linux/stat.h> // for checking dir or file
# include <linux/namei.h>
// for crypto stuff
#include <linux/crypto.h>
#include <linux/scatterlist.h>
#include <crypto/md5.h>
#include <crypto/hash.h>
#include "sys_xcrypt.h"
// courtesy: fs/ecryptfs/crypto.c
#define DECRYPT 0
#define ENCRYPT 1
#define MD5_DIGEST_LENGTH 16
struct xcryptargs* kCryptArgs;
/*
* This method is a courtesy of net/ceph/crypto.c
* Thanks to: http://stackoverflow.com/questions/6059528
* /want-an-example-for-using-aes-encryption-method-in-kernel-version-above-or-equal
* It initializes the encryption/decryption destination buffer
*/
int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
const void *buf, unsigned int buf_len)
{
struct scatterlist *sg;
const bool is_vmalloc = is_vmalloc_addr(buf);
unsigned int off = offset_in_page(buf);
unsigned int chunk_cnt = 1;
unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
int i;
int ret;
if (buf_len == 0)
{
memset(sgt, 0, sizeof(*sgt));
return -EINVAL;
}
if (is_vmalloc)
{
chunk_cnt = chunk_len >> PAGE_SHIFT;
chunk_len = PAGE_SIZE;
}
if (chunk_cnt > 1)
{
ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
if (ret)
return ret;
} else
{
WARN_ON(chunk_cnt != 1);
sg_init_table(prealloc_sg, 1);
sgt->sgl = prealloc_sg;
sgt->nents = sgt->orig_nents = 1;
}
for_each_sg(sgt->sgl, sg, sgt->orig_nents, i)
{
struct page *page;
unsigned int len = min(chunk_len - off, buf_len);
if (is_vmalloc)
page = vmalloc_to_page(buf);
else
page = virt_to_page(buf);
sg_set_page(sg, page, len, off);
off = 0;
buf += len;
buf_len -= len;
}
WARN_ON(buf_len != 0);
return 0;
}
/*
* Frees the sg_table initialized in setup sg_table
* This method is a courtesy of net/ceph/crypto.c
*/
void teardown_sgtable(struct sg_table *sgt)
{
if (sgt->orig_nents > 1)
sg_free_table(sgt);
}
/*
* This method is a courtesy of net/ceph/crypto.c
* it places the encrypted buffer in dst by encrypting the src using key
* This method employs a beautiful way of padding, which avoids to store pad byte in the header
*/
int ceph_aes_encrypt(const void *key, int key_len, void *dst, size_t *dst_len,
const void *src, size_t src_len, char* aes_iv){
struct scatterlist sg_in[2], prealloc_sg;
struct sg_table sg_out;
// allocate the cipher handle for the AES block cipher
struct crypto_blkcipher *tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
int ret;
void *iv;
int ivsize;
// zero_padding holds the number by which it falls short by 16
// the last 4 bits of the src length is taken and subtracted from 16
// this gives the length to be padded.
// if the src_len is a multiple of 16, we dont hold 0, but instead we hold 16
size_t zero_padding = (0x10 - (src_len & 0x0f));
char pad[16];
if (IS_ERR(tfm))
return PTR_ERR(tfm);
// Below is a brilliant step ( by ceph/crypto.c) of padding the src with the amount to be padded
// This way after decryption, when we check the last byte, we know how much padding was added
// and subtract it from dest length
memset(pad, zero_padding, zero_padding);
// update the destination length to reflect the padding done to source
*dst_len = src_len + zero_padding;
sg_init_table(sg_in, 2);
sg_set_buf(&sg_in[0], src, src_len);
sg_set_buf(&sg_in[1], pad, zero_padding);
ret = setup_sgtable(&sg_out, &prealloc_sg, dst, *dst_len);
if (ret)
goto out_tfm;
// set the symmetric key
crypto_blkcipher_setkey((void *)tfm, key, key_len);
// get the address to store the instantiation vector
iv = crypto_blkcipher_crt(tfm)->iv;
// query the cipher handle for the length of IV
// we know that atleast it is 16. no need to have special check for aes_iv
// as it is always taken care for the length to be atleast 16.
ivsize = crypto_blkcipher_ivsize(tfm);
memcpy(iv, aes_iv, ivsize);
print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
key, key_len, 1);
print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 1,
src, src_len, 1);
print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
pad, zero_padding, 1);
ret = crypto_blkcipher_encrypt(&desc, sg_out.sgl, sg_in,
src_len + zero_padding);// start encrypting
if (ret < 0)
{
pr_err("ceph_aes_crypt failed %d\n", ret);
goto out_sg;
}
print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
dst, *dst_len, 1);
out_sg:
teardown_sgtable(&sg_out);
out_tfm:
crypto_free_blkcipher(tfm);
return ret;
}
/*
* This method is a courtesy of net/ceph/crypto.c
* This method decrypts the given src buf using the key provided and places result into dst
*/
int ceph_aes_decrypt(const void *key, int key_len, void *dst, size_t *dst_len,
const void *src, size_t src_len, char* aes_iv){
struct sg_table sg_in;
struct scatterlist sg_out[2], prealloc_sg;
// crypto handle for aes in cipher block chaining mode
struct crypto_blkcipher *tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
struct blkcipher_desc desc = { .tfm = tfm };
char pad[16];
void *iv;
int ivsize;
int ret;
int last_byte;
if (IS_ERR(tfm))
return PTR_ERR(tfm);
sg_init_table(sg_out, 2);
sg_set_buf(&sg_out[0], dst, *dst_len);
sg_set_buf(&sg_out[1], pad, sizeof(pad));
ret = setup_sgtable(&sg_in, &prealloc_sg, src, src_len);
if (ret)
goto out_tfm;
print_hex_dump(KERN_ERR, "dec key0: ", DUMP_PREFIX_NONE, 16, 1,
key, key_len, 1);
crypto_blkcipher_setkey((void *)tfm, key, key_len);// set decrypt key
iv = crypto_blkcipher_crt(tfm)->iv;
ivsize = crypto_blkcipher_ivsize(tfm);
memcpy(iv, aes_iv, ivsize);
print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
key, key_len, 1);
print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
src, src_len, 1);
ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in.sgl, src_len);
if (ret < 0)
{
pr_err("ceph_aes_decrypt failed %d\n", ret);
goto out_sg;
}
// get the last byte of the decrypted source
if (src_len <= *dst_len)
last_byte = ((char *)dst)[src_len - 1];
else
last_byte = pad[src_len - *dst_len - 1];
// this holds by what length you need to go back by rejecting the pad bits
if (last_byte <= 16 && src_len >= last_byte)
{
*dst_len = src_len - last_byte;// source length is now total length - padded bytes
} else
{
pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
last_byte, (int)src_len);
return -EPERM; /* bad padding */
}
print_hex_dump(KERN_ERR, "dec out: ", DUMP_PREFIX_NONE, 16, 1,
dst, *dst_len, 1);
out_sg:
teardown_sgtable(&sg_in);
out_tfm:
crypto_free_blkcipher(tfm);
return ret;
}
/*
*Checks whether the input file can be opened in read mode
* if the file is a directory
* if the file operations on the file allow read
* courtesy wrapfs_read_file method from hw1.txt
* fs/open.c
*/
struct file* checkInputFile(char* infile, long* ret)
{
struct file *filp = NULL;
if(infile == NULL)
{
*ret = -EINVAL;
return filp;
}
filp = filp_open(infile, O_RDONLY, 0);
if (!filp || IS_ERR(filp))
{
*ret = -EPERM;
printk("wrapfs_read_file err %d\n", (int) PTR_ERR(filp));
return NULL;
}
// check if opened file is a directory
if(S_ISDIR(filp->f_path.dentry->d_inode->i_mode))
{
*ret = -EISDIR;
printk("file is a directory\n");
goto CLOSEFILE;
}
if (!filp->f_op->read)// filesystem doesnot allow reads
{
// cannot read, now close file pointer
*ret = -EPERM;
goto CLOSEFILE;
}else
{
goto OUT;
}
CLOSEFILE:
filp_close(filp, NULL);
OUT:
return filp;
}
/*
* Check if the file can be opened or created using the permissions of infile
* Check if the file operations permit write
* courtesy: fs/open.c
*/
struct file* checkOutputFile(char* outfile,struct file* infile)
{
struct file *filp = NULL;
if(infile == NULL)
{
return filp;
}
if(outfile == NULL)
{
return filp;
}
// now get permissions of infile
umode_t inMode = infile->f_path.dentry->d_inode->i_mode;
filp = filp_open(outfile, O_WRONLY|O_CREAT, inMode);// automatically follows symlinks
if (!filp || IS_ERR(filp))
{
printk("wrapfs__create_file_ err %d\n", (int) PTR_ERR(filp));
return NULL;
}
// check if i can write into this file
if (!filp->f_op->write)// filesystem doesnot allow writes
{
// cannot write, now close file pointer
filp_close(filp, NULL);
return NULL;
}
return filp;
}
/*
* Delete a file , given the file struct
* Before unlinking close the file
* Courtesy: fs/namei.c
*/
int deleteFile(struct file* filp)
{
int ret = 0;
if( filp == NULL || IS_ERR(filp))
{
ret = -EINVAL;
printk("Passed a empty file pointer. Check Input!\n");
return ret;
}
struct inode* file_inode = filp->f_path.dentry->d_parent->d_inode;
struct dentry* file_dentry = filp->f_path.dentry;
if(!filp_close(filp, NULL))
{
ret = -EPERM;
printk("File close failed\n");
return ret;
}
ret = vfs_unlink(file_inode, file_dentry, NULL);
if(ret != 0)
printk("Failed to delete the output file");
else
printk(" File deletion successfull");
return ret;
}
/*courtesy: fs/ecryptfs/crypto.c: ecryptfs_calculate_md5
* calculate mds digest of src whose length is len and place in dst
*/
int calculate_md5(char* dst, char* src, int len)
{
int rc = 0;
struct hash_desc desc;
struct crypto_hash *tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
if(!tfm || IS_ERR(tfm))
{
rc = PTR_ERR(tfm);
printk("error allocating crypto context");
goto OUT;
}
desc.tfm = tfm;
rc = crypto_hash_init(&desc);
if(rc)
{
printk(" error initializing crypto hash");
goto OUT;
}
struct scatterlist sg;
sg_init_one(&sg, (u8 *)src, len);
rc = crypto_hash_update(&desc, &sg, len);
if(rc)
{
printk(" error updating crypto hash");
goto OUT;
}
rc = crypto_hash_final(&desc, dst);
if(rc)
{
printk(" error finalizing crypto hash");
goto OUT;
}
OUT:
return rc;
}
/* print bytes in dex value */
void print_md5(unsigned char *mess_dig) {
int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
{
printk("%02x", mess_dig[i]);
}
printk("\n");
}
/*
* Generate an instantiation vector for every page
* A predefined string is concatenated with the page number
* A 16 byte MD5 sum of this string is used as a IV
*/
void getAESinstantiationVector(char* aes, long pageCount)
{
#ifdef EXTRA_CREDIT
char* tempAES = "1234567812345678";// predefined string
char pageCountBuffer[11]; // max long value is 10 charcters long
memset(pageCountBuffer, 0, 11);
sprintf(pageCountBuffer, "%ld", pageCount);
int totlength = strlen(tempAES) + strlen(pageCountBuffer);
char* aesbuf = (char*)kmalloc(totlength+1, GFP_KERNEL);
memset(aesbuf, 0, totlength+1);
strcat(aesbuf, tempAES);
strcat(aesbuf, pageCountBuffer);// concatenate page number
printk("inst vector: %s, %d\n", aesbuf, strlen(aesbuf));
calculate_md5(aes, aesbuf, strlen(aesbuf));
// free aesbuf as we are done with copy
if(aesbuf)
kfree(aesbuf);
#else
char* tempAES = "1234567812345678";
memcpy(aes, tempAES, 16);
#endif
}
/*
* encrypt the input file using keybuf and place in outputFile
* msgDigest is the preamble to be written into the encrypted file
*/
long doEncrypt(struct file* inputFile, struct file* outputFile, char* msgDigest, int keylen, void* keybuf)
{
long ret = 0;
long pageCount = 0;
printk("Starting to encrypt\n");
printk("Encrypting with: "); print_md5(keybuf);printk(" whose digest is: "); print_md5(msgDigest);
// set the file positions to the starting
inputFile->f_pos = 0;
outputFile->f_pos = 0;
mm_segment_t oldfs;
oldfs = get_fs();// store the prev transalation so that we dont mess later
set_fs(KERNEL_DS);// the read_buffer points to kernel , no need of translation
// write the message digest to output file
if(outputFile->f_op->write(outputFile, msgDigest, 16, &(outputFile->f_pos)) == 0)
{
ret = -EIO;
printk(" Could not write message digest to outfile\n");// delete out file
goto DELOUTFILE;
}
// I have a source length, now this can be padded upto a max of 128 bits. srcbuf + pad = page size
char* read_buffer = (char *) kmalloc(PAGE_SIZE, GFP_KERNEL);
if(!read_buffer || IS_ERR(read_buffer))
{
ret = -ENOMEM;
goto DELOUTFILE; // unlink the output file
}
memset(read_buffer, 0, PAGE_SIZE);
// I have a encryption biffer, which can be equal to the src buffer size
char* encrypt_buffer = (char *) kmalloc(PAGE_SIZE, GFP_KERNEL);
if(!encrypt_buffer || IS_ERR(encrypt_buffer))
{
ret = -ENOMEM;
goto FREEREADBUF; // unlink the output file and free read_buffer
}
memset(encrypt_buffer, 0, PAGE_SIZE);
size_t bytesRead = 0;
size_t writeSize = (size_t) PAGE_SIZE;
while(true)
{
memset(read_buffer, 0, PAGE_SIZE);
memset(encrypt_buffer, 0, PAGE_SIZE);
// read page size -16 as 16 can be added as padding bytes
bytesRead = inputFile->f_op->read(inputFile, read_buffer, PAGE_SIZE - 16, &inputFile->f_pos);
if(bytesRead < 0)
{
// if less than 0 free read buffer, write buffer and felete output file
ret = -EIO;
goto FREEWRITEBUF;
}
if(bytesRead == 0)
{
// end of file
goto FREEWRITEBUF;
}
// our encryption destination len would be the bytes read from the file
printk("Encrypting data which is %d len\n", bytesRead);
writeSize = bytesRead;
pageCount++;
char aesIV[17];
memset(aesIV, 0, 17);
getAESinstantiationVector(aesIV, pageCount);
printk("my inst vector: ");print_md5(aesIV);
ret = ceph_aes_encrypt(keybuf, keylen, encrypt_buffer,&writeSize, read_buffer, bytesRead, aesIV);
printk("Encrypted: %d bytes\n", writeSize);
if( ret != 0)
{
printk("encryption falied");
//free read buffer, write buffer and felete output file
goto FREEWRITEBUF;
}
// encryption successful, write to output file
if(outputFile->f_op->write(outputFile, encrypt_buffer, writeSize, &outputFile->f_pos) <= 0 )
{
ret = -EIO;// if writing failed, free read buffer, write buffer and felete output file
goto FREEWRITEBUF;
}
}
FREEWRITEBUF:
if(encrypt_buffer)
kfree(encrypt_buffer);
FREEREADBUF:
if(read_buffer)
kfree(read_buffer);
DELOUTFILE:
if( ret != 0)
deleteFile(outputFile);
set_fs(oldfs);
return ret;
}
/*
* Place the decrypted content into output file
* msgDigest should be checked with the preamble
*/
long doDecrypt(struct file* inputFile, struct file* outputFile, char* msgDigest, int keylen, void* keybuf)
{
long ret = 0;
long pageCount = 0;
printk("Starting to decrypt\n");
printk("Decrypting with the key: "); print_md5(keybuf);
// set the file positions to the starting
inputFile->f_pos = 0;
outputFile->f_pos = 0;
mm_segment_t oldfs;
oldfs = get_fs();// store the prev transalation so that we dont mess later
set_fs(KERNEL_DS);// the read_buffer points to kernel , no need of translation
char* read_buffer = (char *) kmalloc(PAGE_SIZE, GFP_KERNEL);
if(!read_buffer || IS_ERR(read_buffer))
{
ret = -ENOMEM;
goto DELOUTFILE; // unlink the output file
}
memset(read_buffer, 0, PAGE_SIZE);
// I have a decryption buffer, which can be equal to the src buffer size
char* decrypt_buffer = (char *) kmalloc(PAGE_SIZE, GFP_KERNEL);
if(!decrypt_buffer || IS_ERR(decrypt_buffer))
{
ret = -ENOMEM;
goto FREEREADBUF; // unlink the output file and free read_buffer
}
memset(decrypt_buffer, 0, PAGE_SIZE);
size_t bytesRead = 0;
size_t readSize = (size_t) PAGE_SIZE;
bytesRead = inputFile->f_op->read(inputFile, read_buffer, 16, &inputFile->f_pos);
printk(" The hashed password value read is: "); print_md5(read_buffer);
ret = memcmp(read_buffer, msgDigest, 16);
if( ret != 0)
{
ret = -EINVAL;
printk("Incorrect key provided to decrypt");
goto FREEWRITEBUF; // free read write and del output file
}
while(true)
{
bytesRead = 0;
memset(read_buffer, 0, PAGE_SIZE);
memset(decrypt_buffer, 0, PAGE_SIZE);
bytesRead = inputFile->f_op->read(inputFile, read_buffer, PAGE_SIZE, &inputFile->f_pos);
if(bytesRead < 0)
{
// if less than 0 free read buffer, write buffer and felete output file
ret = -EIO;
goto FREEWRITEBUF;
}
if(bytesRead == 0)
{
goto FREEWRITEBUF;
}
pageCount++;
printk("Decrypting %d bytes", bytesRead);
char aesIV[17];
memset(aesIV, 0, 17);
getAESinstantiationVector(aesIV, pageCount);
printk("my inst vector: ");print_md5(aesIV);
readSize = bytesRead;
ret = ceph_aes_decrypt(keybuf, keylen, decrypt_buffer,&readSize, read_buffer, bytesRead, aesIV);
if( ret < 0)
{
printk(" decryption failed in kernel ");
goto FREEWRITEBUF;
}
if(outputFile->f_op->write(outputFile, decrypt_buffer, readSize, &outputFile->f_pos) <=0)
{
ret = -EIO;// if writing failed, free read buffer, write buffer and felete output file
goto FREEWRITEBUF;
}
}
FREEWRITEBUF:
if(decrypt_buffer)
kfree(decrypt_buffer);
FREEREADBUF:
if(read_buffer)
kfree(read_buffer);
DELOUTFILE:
if( ret != 0)
deleteFile(outputFile);
set_fs(oldfs);
return ret;
}
/*
* Do encryption or decryption based on the flag
* generate message digest of the key provided
*/
long doxcrypt(struct file* inputFile,struct file* outputFile, void* keybufCopy, int keylen, int flags)
{
printk(" In the xcrypt function \n ");
long ret = 0;
if(inputFile == NULL)
return -EINVAL;
if(outputFile == NULL)
return -EINVAL;
if(keybufCopy == NULL)
return -EINVAL;
mm_segment_t oldfs;
oldfs = get_fs();// store the prev transalation so that we dont mess later
set_fs(KERNEL_DS);// the read_buffer points to kernel , no need of translation
// calculate the message digest
char messageDigest[17];
memset(messageDigest, 0, 17);
ret = calculate_md5(messageDigest, keybufCopy, keylen);
if(ret != 0)
{
goto OUT;
}
printk(" The key :"); print_md5(keybufCopy);printk(" msg digest: ");print_md5(messageDigest);
if(flags == ENCRYPT)
{
// call encrypt
ret = doEncrypt(inputFile, outputFile, messageDigest, keylen, keybufCopy);
}else if(flags == DECRYPT)
{
// call decrypt
ret = doDecrypt(inputFile, outputFile, messageDigest, keylen, keybufCopy);
}
else
{
// invalid flag value
ret = -EINVAL;
}
OUT:
set_fs(oldfs);// restore the prev translation
return ret;
}
/*
* Check if the user provided arguments belong to the user space and are valid physical memory locations
* may be redundant, as copyfromuser also does a access check
*/
long isArgsValid(void* args)
{
if (args == NULL)
{
return -EINVAL;
}
struct xcryptargs* cargs = (struct xcryptargs*)args;
if(cargs->infile == NULL || cargs->outfile == NULL || cargs->keybuf == NULL || cargs->keylen == 0)
return -EINVAL;
if(!access_ok(VERIFY_READ, cargs->infile, sizeof(cargs->infile)))
{
return -EFAULT;
}
if(!access_ok(VERIFY_READ, cargs->outfile, sizeof(cargs->outfile)))
{
return -EFAULT;
}
if(!access_ok(VERIFY_READ, cargs->keybuf, sizeof(cargs->keybuf)))
{
return -EFAULT;
}
return 0;
}
/*
* Copy the data from userland to kernel land
* No matter how big the user key is , it is always truncated to 16 bytes
* If the key provided is less than 16, an error is thrown to the user
* also we check for filenames exceeding max path length
*/
long copyFromUserland(struct xcryptargs* userArgs, struct xcryptargs* kernelArgs)
{
long ret = 0;
printk("in copy from userland\n");
// copy the input file
// strlen_user copies the null byte as well
if(strlen_user(userArgs->infile)-1 >= PATH_MAX)
{
ret = ENAMETOOLONG;
goto OUT;
}
kernelArgs->infile = kmalloc(strlen_user(userArgs->infile), GFP_KERNEL);
if(kernelArgs->infile == NULL)
{
ret = -ENOMEM;
goto OUT;
}
memset(kernelArgs->infile, 0,strlen_user(userArgs->infile));
if(copy_from_user(kernelArgs->infile, userArgs->infile, strlen_user(userArgs->infile)) != 0)
{
ret = -EFAULT;
goto OUT;// free infile
}
// copy the output file
if(strlen_user(userArgs->outfile)-1 >= PATH_MAX)
{
ret = ENAMETOOLONG;
goto OUT;
}
kernelArgs->outfile = kmalloc(strlen_user(userArgs->outfile), GFP_KERNEL);
if(kernelArgs->outfile == NULL)
{
ret = -ENOMEM;
goto OUT;// free infile
}
memset(kernelArgs->outfile, 0,strlen_user(userArgs->outfile));
if(copy_from_user(kernelArgs->outfile, userArgs->outfile, strlen_user(userArgs->outfile)) != 0)
{
ret = -EFAULT;
goto OUT;// FREE infile and outfile
}
// copy the key buffer
kernelArgs->keybuf = kmalloc( MD5_DIGEST_LENGTH, GFP_KERNEL);
if(kernelArgs->keybuf == NULL)
{
ret = -ENOMEM;
goto OUT;// free infile and outfile
}
memset(kernelArgs->keybuf, 0, MD5_DIGEST_LENGTH);
if(copy_from_user(kernelArgs->keybuf, userArgs->keybuf, MD5_DIGEST_LENGTH) != 0)
{
ret = -EFAULT;
goto OUT; // free infile, outfile and keybuff
}
// copy the key length
if(copy_from_user(&kernelArgs->keylen, &userArgs->keylen, sizeof(int)) != 0)
{
ret = -EFAULT;
goto OUT; // free infile, outfile and keybuff
}
if(strlen_user(userArgs->keybuf)-1 != kernelArgs->keylen)
{
printk(" user passed buffer length and passed keylength do not match");
ret = -EFAULT;
goto OUT; // free infile, outfile and keybuff
}
// check if keylen is less than 16
if( kernelArgs->keylen < MD5_DIGEST_LENGTH)
{
printk(" key passed less than 128 bits");
ret = -EINVAL;
goto OUT;
}
// copy the flags
if(copy_from_user(&kernelArgs->flags, &userArgs->flags, sizeof(int)) != 0)
{
ret = -EFAULT;
goto OUT;
}
// look only at the LSB
kernelArgs->flags = kernelArgs->flags & 1;
if(kernelArgs->flags < 0 || kernelArgs->flags > 1) // though the o/p produce either 0 or 1, this check may be redundant
{
printk(" invalid flag value");
ret = -EINVAL;
goto OUT;
}
// Exit from the function; free all of them if allocated in the end
OUT:
return ret;
}
/*
* checks if the two files have identical inodes in same super block
*/
bool isIpFileEqualsOpFile( struct file* infile, char* outfile)
{
struct file *filp = NULL;
if(infile == NULL || outfile == NULL)
{
return false;
}
umode_t inMode = infile->f_path.dentry->d_inode->i_mode;
filp = filp_open(outfile, O_RDONLY|O_WRONLY, inMode);
if (!filp || IS_ERR(filp))
{
return false;
}
// this means the output file exists, check if equal to input file
// check if the inode numbers are same in the same super block
if((infile->f_path.dentry->d_inode->i_ino == filp->f_path.dentry->d_inode->i_ino) &&
(infile->f_path.dentry->d_inode->i_sb == filp->f_path.dentry->d_inode->i_sb))
{
printk(" input and out put files are same \n");
return true;
}
return false;
}
/*
*Renames the tempOutputFilep to outfile
*/
int doRename(struct file* tempOutputFilep, char* outfile)
{
int ret = 0;
struct file* outfilep = checkOutputFile(outfile, tempOutputFilep);
if(outfilep == NULL)
{
ret = -EPERM;
goto OUT;
}
if(!(tempOutputFilep))
{
ret = -EINVAL;
if(outfilep)
filp_close(outfilep, NULL);
goto OUT;
}
printk(" Starting to rename \n");
struct inode* old_file = tempOutputFilep->f_path.dentry->d_parent->d_inode;
struct dentry* old_dentry = tempOutputFilep->f_path.dentry;
struct inode* new_file = outfilep->f_path.dentry->d_parent->d_inode;
struct dentry* new_dentry = outfilep->f_path.dentry;
if(tempOutputFilep)
filp_close(tempOutputFilep, NULL);// this may fail?
if(outfilep)
filp_close(outfilep, NULL);// this may fail?
ret = vfs_rename(old_file, old_dentry, new_file, new_dentry, NULL, 0);
OUT:
return ret;
}
bool isDir(char* file)
{
struct file *filp = NULL;
filp = filp_open(file, O_RDONLY, 0);
if (!filp || IS_ERR(filp))
{
return false;
}
// now that i have file pointer, check if its directory
if(S_ISDIR(filp->f_path.dentry->d_inode->i_mode))
{
// before returning close the file pointer
printk(" file specified is a directory\n");
filp_close(filp, NULL);
return true;
}
filp_close(filp, NULL);
return false;
}
/*
* Heart of the logic. Initiates the encryption/decryption module
*/
long sys_xcrypt(char* infile, char* outfile, char* keybuf,int keylen, int flags)
{
long ret = 0;
// truncating key to only 16 bits
char paswordDigest[17];