-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_cpenc.c
442 lines (374 loc) · 12.1 KB
/
sys_cpenc.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
#include <crypto/skcipher.h>
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/compiler.h>
#include <linux/key.h>
#include <linux/namei.h>
#include <linux/file.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/linkage.h>
#include <linux/random.h>
#include <linux/moduleloader.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/uaccess.h>
#include <linux/scatterlist.h>
#include <crypto/md5.h>
#include <crypto/hash.h>
#define BUFFER_SIZE PAGE_SIZE
struct skcipher_def
{
struct skcipher_request *request;
struct crypto_skcipher *tfm;
struct scatterlist sg;
};
struct arguments
{
char *inputfile;
char *outputfile;
char *keybuf;
int flags;
int keylen;
};
int crypto_help_function( char *key,char *data,int bytes , int flag)
{
char *iv_data = NULL;
int error = 0;
struct skcipher_request *request = NULL;
struct crypto_skcipher *skcipher = crypto_alloc_skcipher( "ctr(aes)",0,CRYPTO_ALG_ASYNC );
struct skcipher_def *sk = NULL;
//int ret_value;
sk = kmalloc( sizeof( struct skcipher_def ),GFP_KERNEL );
//check if memory allocation is succesful in kernel
if( !sk )
{
kfree( sk );
return -ENOMEM;
}
// check if cipher handle allocation is successfull
if ( IS_ERR( skcipher ) )
{
error = PTR_ERR(skcipher);
printk("cipher handle allocation failed\n");
return error;
}
else
{
request = skcipher_request_alloc( skcipher,GFP_KERNEL );
// check if cipher request allocation is successful
if ( !request )
{
crypto_free_skcipher( skcipher );
printk("cipher request allocation failed\n");
return -ENOMEM;
}
}
// check if key value was properly set
if ( crypto_skcipher_setkey( skcipher,key,16 ) )
{
crypto_free_skcipher( skcipher );
skcipher_request_free( request );
printk("setting key failed in helped function\n");
return -EAGAIN;
}
sk->request = request;
sk->tfm = skcipher;
iv_data = kmalloc( 16,GFP_KERNEL );
// check if iv data allocation is successful
if ( !iv_data )
{
printk("iv data allocation failed\n");
error = -ENOMEM;
goto failed;
}
sg_init_one( &sk->sg,data,bytes );
strncpy( iv_data,"aabbccddeeffgghh",16 );
skcipher_request_set_crypt( request,&sk->sg,&sk->sg,bytes,iv_data );
if( flag == 2)
error = crypto_skcipher_decrypt( sk->request );
else
error = crypto_skcipher_encrypt( sk->request );
failed:
if ( iv_data )
kfree( iv_data );
if ( request )
skcipher_request_free( request );
if ( skcipher )
crypto_free_skcipher( skcipher );
return error;
}
asmlinkage extern long (*sysptr)(void *arg);
asmlinkage long cpenc(void *arg)
{
if (arg != NULL)
{
int result;
struct arguments *args;
umode_t input_file_mode, output_file_mode;
char *source_buff, *destination_buff;
long int key_length = -1, output_file_length = -1, input_file_length = -1;
int error = 0;
struct file *in_filp = NULL;
struct file *out_filp = NULL;
mm_segment_t old_fs;
int out_bytes = 0;
char *data = NULL;
int bytes = 1;
args = ( struct arguments * )kmalloc( sizeof( struct arguments ),GFP_KERNEL );
if (args == NULL)
{
error = -ENOMEM;
printk("Allocation of kernel memory failed\n");
return error;
}
result = copy_from_user( (void *)args,arg,sizeof(struct arguments) );
if(result != 0)
{
error = -EFAULT;
printk("Failed to copy from user\n");
goto copy_from_user_failure;
}
if ( ( ( struct arguments* ) arg )->inputfile == NULL )
{
error = -EINVAL;
printk("input file passed at user is invalid \n");
goto copy_from_user_failure;
}
input_file_length = strnlen_user(((struct arguments*) arg)->inputfile, 32767 );
if ( input_file_length == -1 )
{
error = -EFAULT;
printk("input file passed at user has zero length \n");
goto copy_from_user_failure;
}
args->inputfile = ( char * )kmalloc( input_file_length * sizeof(char),GFP_KERNEL );
if ( ( args->inputfile ) == NULL )
{
error = -ENOMEM;
printk("input file memory allocation at kernel failed \n");
goto copy_from_user_failure;
}
result = strncpy_from_user(args->inputfile, ( ( struct arguments* ) arg )->inputfile, input_file_length);
if( result != ( input_file_length - 1 ) )
{
error = -EFAULT;
printk("failed to copy input file name from user \n");
goto input_file_error;
}
if ( ( ( struct arguments* ) arg )->outputfile == NULL)
{
error = -EINVAL;
printk("output file passed at user is invalid\n");
goto input_file_error;
}
output_file_length = strnlen_user(((struct arguments*)arg)->outputfile,32767 );
if (output_file_length == -1)
{
error = -EFAULT;
printk("output file passed at user has zero length\n");
goto input_file_error;
}
args->outputfile = (char *)kmalloc( output_file_length * sizeof(char),GFP_KERNEL );
if ( ( args->outputfile ) == NULL )
{
error = -ENOMEM;
printk("output file memory allocation at kernel failed \n");
goto input_file_error;
}
result = strncpy_from_user( args->outputfile,((struct arguments*)arg)->outputfile, output_file_length);
if( result != (output_file_length - 1 ) )
{
error = -EFAULT;
printk("failed to copy output file name from user\n");
goto output_file_error;
}
if ( ( ( struct arguments* ) arg )->keybuf == NULL )
{
error = -EINVAL;
printk("key buffer passed at user is invalid\n");
goto output_file_error;
}
key_length = strnlen_user( ( ( struct arguments* ) arg)->keybuf, 32767);
if( key_length == -1 )
{
error = -EFAULT;
printk("key buffer passed at user has zero length\n");
goto output_file_error;
}
args->keybuf = (char *)kmalloc( key_length * sizeof(char), GFP_KERNEL );
if( ( args->keybuf ) == NULL )
{
error = -ENOMEM;
printk("key buff memory allocation failed at kernel\n");
goto output_file_error;
}
result = strncpy_from_user( args->keybuf, ((struct arguments*)arg)->keybuf, key_length);
if( result != ( key_length - 1 ) )
{
error = -EFAULT;
printk("failed to copy key buffer from user \n");
goto key_buffer_error;
}
if ( strlen( args->keybuf ) != args->keylen )
{
error = -EINVAL;
printk("unequal kernel key buff and key buff lengths \n");
goto key_buffer_error;
}
if( ( args->flags ) !=2 && ( args->flags ) !=1 && ( args->flags ) != 4)
{
error = -EINVAL;
printk("Invalid flag argument values\n");
goto key_buffer_error;
}
source_buff = (char *)kmalloc( ( PAGE_SIZE + 1 )*sizeof(char), GFP_KERNEL );
if ( source_buff == NULL )
{
error = -ENOMEM;
printk("allocation of kernel memory to source buffer failed\n");
goto key_buffer_error;
}
destination_buff = ( char * )kmalloc( ( PAGE_SIZE + 1 ) * sizeof(char), GFP_KERNEL );
if ( destination_buff == NULL)
{
error = -ENOMEM;
printk("allocation of kernel memory to destination buffer failed\n");
goto source_buffer_error;
}
in_filp = filp_open(args->inputfile, O_RDONLY, 0);
if ( !in_filp || IS_ERR(in_filp) )
{
error = (int) PTR_ERR(in_filp);
printk("input file opening failed with error - %d\n", error);
goto destination_error;
}
input_file_mode = in_filp->f_inode->i_mode;
in_filp->f_pos = 0;
if( !S_ISREG(input_file_mode) )
{
error = -EISDIR;
printk("irregular input file\n");
goto input_file_creation_error;
}
out_filp = filp_open(args->outputfile, O_WRONLY | O_CREAT, input_file_mode);
if( !out_filp || IS_ERR( out_filp ) )
{
error = PTR_ERR( out_filp );
printk("output file creation failed\n");
goto input_file_creation_error;
}
output_file_mode = out_filp->f_inode->i_mode;
if(!S_ISREG(output_file_mode))
{
error = -EISDIR;
printk(" irregular output file\n");
goto output_file_creation_error;
}
out_filp->f_pos = 0;
if( out_filp->f_inode->i_ino == in_filp->f_inode->i_ino )
{
error = -EPERM;
printk(" output & input files are same \n");
goto output_file_creation_error;
}
data = kmalloc(BUFFER_SIZE, GFP_USER);
old_fs = get_fs();
set_fs(KERNEL_DS);
while( bytes > 0 )
{
bytes = vfs_read( in_filp,data,BUFFER_SIZE,&in_filp->f_pos );
if( bytes < 0)
{
error = bytes;
printk(" reading input file failed\n");
goto err;
}
if ( args->flags == 4 )
{
goto copy;
}
else if(args->flags == 2)
{
error = crypto_help_function((char *) args->keybuf, data, bytes, args->flags);
if(error == 0)
printk("decryption of file successful");
else
{
printk("decryption of file failed\n");
goto err;
}
}
else if ( args->flags == 1 )
{
error = crypto_help_function((char *) args->keybuf, data, bytes, args->flags);
if(error == 0)
printk("encryption of file successful");
else
{
printk("encryption of file failed\n");
goto err;
}
}
copy:
out_bytes = vfs_write( out_filp,data,bytes,&out_filp->f_pos );
if( out_bytes < 0 )
{
error = out_bytes;
printk("write to output file failed\n");
goto err;
}
}
err:
set_fs(old_fs);
output_file_creation_error:
if(out_filp)
filp_close(out_filp, NULL);
input_file_creation_error:
if(in_filp)
filp_close(in_filp, NULL);
destination_error:
if(destination_buff)
kfree( destination_buff );
source_buffer_error:
if(source_buff)
kfree( source_buff );
key_buffer_error:
if(args->keybuf)
kfree( args->keybuf );
output_file_error:
if(args->outputfile)
kfree( args->outputfile );
input_file_error:
if( args->inputfile )
kfree( args->inputfile );
copy_from_user_failure:
kfree( args );
return error;
}
else
{
printk("Null arguments in user level\n");
return -EINVAL;
}
}
static int __init init_sys_cpenc(void)
{
printk("installed new sys_cpenc module\n");
if (sysptr == NULL)
sysptr = cpenc;
return 0;
}
static void __exit exit_sys_cpenc(void)
{
if (sysptr != NULL)
sysptr = NULL;
printk("removed sys_cpenc module\n");
}
module_init(init_sys_cpenc);
module_exit(exit_sys_cpenc);
MODULE_LICENSE("GPL");