-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusers.c
2287 lines (1676 loc) · 59.2 KB
/
users.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
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 "gsfs.h"
int GSFS_add_new_user(struct super_block* sb,rsa_context*rsa,uid_t uid){
struct GSFS_sb* gsb=(struct GSFS_sb*)sb->s_fs_info;
if(add_new_public_key(gsb,rsa,uid))
return -1;
printk("<1>" "GSFS: Adding new user public key with uid: %u\n",uid);
return 0;
}
int GSFS_user_login(struct super_block* sb,rsa_context*rsa,uid_t uid, char* pt, int ptlen, char* ct, int ctlen ){
struct GSFS_sb* gsb=(struct GSFS_sb*)sb->s_fs_info;
if(GSFS_add_new_private_key(sb, rsa, uid, pt, ptlen, ct, ctlen))
return -1;
printk("<1>" "GSFS: user with uid: %u logged in\n",uid);
#ifdef gsfs_test
clear_test_indecis();
#endif
get_inode_for_incom_inodes_of_uid(gsb, uid);
return 0;
}
int GSFS_user_logout(struct super_block* sb,uid_t uid){
int ret=0;
//ret=GSFS_remove_private_key(sb,uid)
return ret;
}
#ifdef gsfs_test
#define test_add_user_block
#endif
#ifdef test_add_user_block
#define gwt(p) p
#define gwtc if(rep-repp>replen-300){printk("<0>" "%s\n",repp);rep=repp;}
#else
#define gwt(p)
#define gwtc
#endif
int add_user_block_to_inode(struct inode* in){
struct GSFS_inode * inf=(struct GSFS_inode*)in->i_private,
*pinf;
struct GSFS_inode_disk_inf *ind=&inf->disk_info;
struct super_block* sb=in->i_sb;
struct GSFS_sb* gsb=(struct GSFS_sb*)sb->s_fs_info;
unsigned int res[2],
i,
err=0,
res_num;
struct buffer_head* bh;
struct inode_user_page* iup;
struct rsa_key* rkey;
struct inode* pin=0;
//struct crust_state* cs;
unsigned short gdlen,
gd_per_page,
gdoffset,
gdpage,
inf_index_in_parent;
struct GSFS_dirent *gd;
struct users** uev;
crust_struct** csev;
char *gdh;
char *new_owner_key;
#ifdef test_add_user_block
int replen=1000;
char *repp,
*rep;
rep=kzalloc(replen, GFP_KERNEL);
repp=rep;
sprintf(rep, "add_user_block_to_inode for inode with ino:%lu* ",in->i_ino);
rep+=strlen(rep);
#endif
if(inf->igflags & igflag_first_level_sec_inode)
return -1;
//it is part of above condition
//because in this version of GSFS you cant remove parent link if exists
//if(inf->igflags & igflag_active_parent_link)
// return -1;
if(ind->dir_inode_security.child_num!=0)
return -1;
if(inf->igflags & igflag_secure)
if(ind->iuid != current->loginuid)
return -1;
pin=GSFS_get_inode(gsb, inf->disk_info.parent_ino);
if(!pin)
return -1;
pinf=(struct GSFS_inode*)pin->i_private;
if(!pinf)
return -1;
down_write(&inf->inode_rwsem);
//it is impossible for inode to have owner_key because it hasnt user_block
/*
if(inf->igflags & igflag_secure)
if(!inf->inode_crust_struct->owner_key){
int ret;
spin_lock(&inf->inode_crust_struct->lock);
ret=read_owner_key_for_crust_struct(inf->inode_crust_struct, in->i_sb,
ind->dir_inode_security.inode_user_block_hash);
spin_unlock(&inf->inode_crust_struct->lock);
if(ret){
gwt(sprintf(rep,"Unable to read owner key * "));
gwt(rep+=strlen(rep));
up_write(&inf->inode_rwsem);
goto bad_ret;
}
}
*/
down_write(&pinf->inode_rwsem);
gwt(sprintf(rep,"parent inode: %ld * ",pin->i_ino));
gwt(rep+=strlen(rep));
//allocating 1 block for user_block of inode or 2 block if it isnot secure
if(inf->igflags & igflag_secure)
res_num=1;
else
res_num=2;
i=BAT_get_some_blocks(gsb, res_num, res);
if(i!= res_num){
res_num=i;
goto bat_ret;
}
ind->dir_inode_security.user_block=res[0];
if(!(inf->igflags & igflag_secure)){
ind->dir_inode_security.gdirent_hash_block=res[1];
ind->SAT_index=SAT_get_one_index(gsb);
if(ind->SAT_index==-1)
goto bat_ret;
gwt(sprintf(rep,"new SAT_index: %d * ",ind->SAT_index));
gwt(rep+=strlen(rep));
}
gwt(sprintf(rep,"allocated blocks num:%d and blocks: %d %d* ",res_num,res[0],res[1]));
gwt(rep+=strlen(rep));
//preparing owner key
new_owner_key=kzalloc(gsfs_aes_keylen, GFP_KERNEL);
if(inf->igflags & igflag_secure){
//its parent has its owner key
/*
ind->dir_inode_security.last_crust_ver++;
memset(inf->inode_crust_state, 0, sizeof(struct crust_state));
*/
get_random_bytes(new_owner_key, gsfs_aes_keylen);
put_crust_struct(inf->inode_crust_struct);
gwt(sprintf(rep,"inode is secure, new owner_key: * "));
gwt(rep+=strlen(rep));
gwt(printhexstring(new_owner_key, rep, 16));
gwt(rep+=strlen(rep));
gwtc;
}
else{
//parent is not secure and we should allocate new owner key
add_child_to_parent(pin, in);
//inf->parent=pin;
//ind->dir_inode_security.last_crust_ver=0;
//new_owner_key=kzalloc(gsfs_aes_keylen, GFP_KERNEL);
get_random_bytes(new_owner_key, gsfs_aes_keylen);
//inf->owner_key=kzalloc(gsfs_aes_keylen,GFP_KERNEL);
//memcpy(inf->owner_key,new_owner_key,gsfs_aes_keylen);
//inf->igflags|=igflag_present_owner_key;
//add_event_to_inode(pin, ind->index_in_parent, Non_FL_Owner_Key_Set_Event,
// new_owner_key, gsfs_aes_keylen, event_flag_from_disk);
//inf->inode_crust_state=kzalloc(sizeof(struct crust_state),GFP_KERNEL);
gwt(sprintf(rep,"inode isnt secure, allocating owner_key: "));
gwt(rep+=strlen(rep));
gwt(printhexstring(new_owner_key, rep, 16));
gwt(rep+=strlen(rep));
gwtc;
}
//inf->crust_last_ver=ind->dir_inode_security.last_crust_ver;
//now the owner_key is available
//we should create new crust struct and complete iup field
//with encrypted owner key and crust state
inf->inode_crust_struct=kzalloc(sizeof(crust_struct), GFP_KERNEL);
spin_lock_init(&inf->inode_crust_struct->lock);
inf->inode_crust_struct->owner_key=new_owner_key;
inf->inode_crust_struct->count=1;
inf->inode_crust_struct->max_ver=0;
inf->inode_crust_struct->user_block=ind->dir_inode_security.user_block;
crust_get_next_state(&inf->inode_crust_struct->crust_state, inf->inode_crust_struct->max_ver,
inf->inode_crust_struct->owner_key);
//add_event for new crust_struct
csev=kzalloc(sizeof(crust_struct*), GFP_KERNEL);
*csev=get_crust_struct(inf->inode_crust_struct);
add_event_to_inode(pin, ind->index_in_parent, Crust_Struct_Set_VEvent, csev,
sizeof(crust_struct*), event_flag_from_disk);
rkey=get_rsa_key(gsb, current->loginuid, 0);
if(!rkey || !rkey->key)
goto cs_bat_ret;
gwt(sprintf(rep,"new crust_state: "));
gwt(rep+=strlen(rep));
gwt(printhexstring((char*)(&inf->inode_crust_struct->crust_state), rep, 81));
gwt(rep+=strlen(rep));
gwtc;
bh=__bread(in->i_sb->s_bdev, ind->dir_inode_security.user_block, Block_Size);
iup=(struct inode_user_page*)bh->b_data;
lock_buffer(bh);
iup->num=1;
iup->max_ver=0;
spin_lock(&rkey->lock);
iup->owner_key.uid=current->loginuid;
iup->owner_key.writability=1;
i=rsa_encrypt_owner_key_for_user_block(iup->owner_key.rsa_encrypted_key, new_owner_key, rkey->key);
iup->users_key[0].uid=current->loginuid;
iup->users_key[0].writability=1;
i=rsa_encrypt_crust_state_for_user_block(iup->users_key[0].rsa_encrypted_key,
&inf->inode_crust_struct->crust_state, rkey->key);
spin_unlock(&rkey->lock);
get_crust_hash(iup->crust_hash, &inf->inode_crust_struct->crust_state);
gwt(sprintf(rep,"crust hash for user_block: "));
gwt(rep+=strlen(rep));
gwt(printhexstring(iup->crust_hash, rep, 16));
gwt(rep+=strlen(rep));
get_user_block_hash(ind->dir_inode_security.inode_user_block_hash, bh->b_data);
gwt(sprintf(rep,"user block hash: "));
gwt(rep+=strlen(rep));
gwt(printhexstring(ind->dir_inode_security.inode_user_block_hash, rep, 16));
gwt(rep+=strlen(rep));
mark_buffer_dirty(bh);
set_buffer_uptodate(bh);
unlock_buffer(bh);
brelse(bh);
//changing flags of inode
inf->igflags |=(igflag_secure | igflag_first_level_sec_inode );
//inf->igflags &= ~(igflag_active_parent_link); //it is impossible because above filter :)
gwt(sprintf(rep,"new flags of inf: %x * ",inf->igflags));
gwt(rep+=strlen(rep));
//now we should set users field of our new be secured inode
if(inf->users) //only if it was secure previously
put_users(inf->users);
inf->users=kzalloc(sizeof(struct users), GFP_KERNEL);
spin_lock_init(&inf->users->lock);
inf->users->users_num=1;
inf->users->users=kzalloc(sizeof(unsigned int), GFP_KERNEL);
inf->users->writability=kzalloc(sizeof(char), GFP_KERNEL);
inf->users->users[0]=current->loginuid;
inf->users->writability[0]=1;
get_users(inf->users);
uev=kzalloc(sizeof(struct users*),GFP_KERNEL);
*uev=inf->users;
add_event_to_inode(pin, ind->index_in_parent, Users_Set_VEvent, uev,
sizeof(struct users*), event_flag_from_disk);
get_users(inf->users);
gwt(sprintf(rep,"new users struct count:%d* ",inf->users->count));
gwt(rep+=strlen(rep));
gwtc;
//now we should update gdirent fields
gdlen=gsfs_dirent_len;
gd_per_page=Block_Size/gdlen;
gdpage=ind->index_in_parent/gd_per_page;
gdoffset=ind->index_in_parent%gd_per_page;
gwt(sprintf(rep,"reading gdpage:%d gdoffset:%d* ", gdpage, gdoffset));
gwt(rep+=strlen(rep));
bh=__bread(pin->i_sb->s_bdev,get_dp_bn_of_in(pin,gdpage),Block_Size);
gd=(struct GSFS_dirent*)bh->b_data;
gd+=gdoffset;
//encrypting inl if its parent is not sec and it is sec
if(!(pinf->igflags & igflag_secure)){
//if(!(inf->igflags & igflag_encrypted_inl)){
struct gdirent_inl inl;
char key[gsfs_aes_keylen];
spin_lock(&inf->inode_crust_struct->lock);
crust_get_key_of_state(&inf->inode_crust_struct->crust_state,
inf->inode_crust_struct->max_ver, key);
spin_unlock(&inf->inode_crust_struct->lock);
encrypt_inl(&inl, &gd->gd_inl, key);
memcpy(&gd->gd_inl, &inl, gsfs_inl_len);
inf->igflags |=igflag_encrypted_inl;
gd->gd_dirent_inl_ver=inf->inode_crust_struct->max_ver;
memset(key, 0, gsfs_aes_keylen);
memset(&inl, 0, gsfs_inl_len);
gwt(sprintf(rep,"encrypt_inl to: "));
gwt(rep+=strlen(rep));
gwt(printhexstring((char*)&gd->gd_inl, rep, 64));
gwt(rep+=strlen(rep));
gwtc;
//}
}
else{
//we should change inl key to be compatible with new allocated crust
//if(!(inf->igflags & igflag_first_level_sec_inode)){
struct gdirent_inl inl;
char key[gsfs_aes_keylen];
spin_lock(&pinf->inode_crust_struct->lock);
crust_get_key_of_state(&pinf->inode_crust_struct->crust_state,
gd->gd_dirent_inl_ver, key);
spin_unlock(&pinf->inode_crust_struct->lock);
decrypt_inl(&inl, &gd->gd_inl, key);
gwt(sprintf(rep,"decrypt previous inl from: "));
gwt(rep+=strlen(rep));
gwt(printhexstring((char*)&gd->gd_inl, rep, 64));
gwt(rep+=strlen(rep));
gwtc;
gwt(sprintf(rep,"to: "));
gwt(rep+=strlen(rep));
gwt(printhexstring((char*)&inl, rep, 64));
gwt(rep+=strlen(rep));
gwtc;
spin_lock(&inf->inode_crust_struct->lock);
crust_get_key_of_state(&inf->inode_crust_struct->crust_state,
inf->inode_crust_struct->max_ver, key);
gd->gd_dirent_inl_ver=inf->inode_crust_struct->max_ver;
spin_unlock(&inf->inode_crust_struct->lock);
encrypt_inl(&gd->gd_inl, &inl, key);
gwt(sprintf(rep,"and decrypt_inl to: "));
gwt(rep+=strlen(rep));
gwt(printhexstring((char*)&gd->gd_inl, rep, 64));
gwt(rep+=strlen(rep));
gwtc;
memset(key, 0, gsfs_aes_keylen);
//}
}
////setting gdflags
gd->gd_flags=igflag_ondisk(inf->igflags);
//cleaning previous gd_child_security_fields
//gd->gd_child_security_fields.gd_dirent_crust_link_pver=0;
//memset(&gd->gd_child_security_fields.gd_crust_state_link, 0, sizeof(struct crust_state));
////we remain owner_key_link if it exists even we write it in user block for owner
//if(!(pinf->igflags & igflag_secure ))
// memset(gd->gd_child_security_fields.gd_owner_key_link, 0, gsfs_aes_keylen);
////setting user_block and crust_ver and copying user_block_hash
gd->gd_first_dir_security_fields.gd_user_block=ind->dir_inode_security.user_block;
memcpy(gd->gd_first_dir_security_fields.gd_user_block_hash,
ind->dir_inode_security.inode_user_block_hash, gsfs_hashlen);
gdh=kzalloc(gsfs_hashlen, GFP_KERNEL);
get_gdirent_hash(gdh, gd);
mark_buffer_dirty(bh);
set_buffer_uptodate(bh);
brelse(bh);
gwt(sprintf(rep,"new gdirent hash: "));
gwt(rep+=strlen(rep));
gwt(printhexstring(gdh, rep, 16));
gwt(rep+=strlen(rep));
gwtc;
inf->add_event_to_parent(in, GDirent_Hash_Changed_Event, gdh, gsfs_hashlen, 0);
//making inode dirty
mark_inode_dirty(in);
inf->igflags|=igflag_inode_metadata_changed;
inf_index_in_parent=ind->index_in_parent;
up_write(&pinf->inode_rwsem);
up_write(&inf->inode_rwsem);
down_write(&pinf->inode_rwsem);
//set_one_index(pinf->disk_info.dir_inode_security.dir_inode_first_level_child_array,
// inf_index_in_parent, GSFS_DEDICATION_ARRAY_LEN_BITS);
//now we should update parents igflag for integrity
do{
unsigned int pino;
unsigned short inf_igflags;
gwt(sprintf(rep,"updating parent with ino: %lu* ",pin->i_ino));
gwt(rep+=strlen(rep));
gwtc;
mark_inode_dirty(pin);
pinf->igflags |= igflag_inode_metadata_changed;
if(pinf->igflags & igflag_secure)
break;
set_one_index(pinf->disk_info.dir_inode_security.dir_inode_sec_has_sec_child_array,
inf_index_in_parent, GSFS_DEDICATION_ARRAY_LEN_BITS);
pinf->disk_info.dir_inode_security.sec_has_sec_child_num++;
if(pinf->igflags & igflag_has_sec_child)
break;
pinf->igflags|=igflag_has_sec_child;
//allocating blocks for data,mdata,gdirent hash of pin
res_num=BAT_get_some_blocks(gsb, 1, res);
if(res_num!=1)
goto bat_ret2;
pinf->disk_info.dir_inode_security.gdirent_hash_block=res[0];
pinf->disk_info.SAT_index=SAT_get_one_index(gsb);
gwt(sprintf(rep,"allocated blocks for parents: %u and new sat_index: %u * ",res[0],pinf->disk_info.SAT_index));
gwt(rep+=strlen(rep));
gwtc;
if(pin->i_ino==1){
gsb->gsb_disk.root_inode_has_secure_child=0x37;
gsb->sgflags|=sgflag_sb_ondisk;
sb->s_dirt=1;
break;
}
//else we should update gdirent of pin in its pin:
pino=pinf->disk_info.parent_ino;
in=pin;
inf=pinf;
pin=GSFS_get_inode(gsb, pino);
pinf=(struct GSFS_inode*)pin->i_private;
inf_index_in_parent=inf->disk_info.index_in_parent;
inf_igflags=inf->igflags;
down_write(&pinf->inode_rwsem);
add_child_to_parent(pin, in);
//inf->parent=pin;
up_write(&pinf->inode_rwsem);
up_write(&inf->inode_rwsem);
iput(in);
down_write(&pinf->inode_rwsem);
gdpage=inf_index_in_parent/gd_per_page;
gdoffset=inf_index_in_parent%gd_per_page;
bh=__bread(pin->i_sb->s_bdev,get_dp_bn_of_in(pin,gdpage),Block_Size);
gd=(struct GSFS_dirent*)bh->b_data;
gd+=gdoffset;
gd->gd_flags=igflag_ondisk(inf_igflags);
gdh=kzalloc(gsfs_hashlen, GFP_KERNEL);
get_gdirent_hash(gdh, gd);
mark_buffer_dirty(bh);
set_buffer_uptodate(bh);
brelse(bh);
gwt(sprintf(rep,"gdhash sent to parent of inode: %lx is:* ",in->i_ino));
gwt(rep+=strlen(rep));
gwt(printhexstring(gdh, rep, 16));
gwt(rep+=strlen(rep));
gwtc;
add_event_to_inode(pin, inf_index_in_parent, GDirent_Hash_Changed_Event, gdh, gsfs_hashlen, 0);
}while(pin);
if(pin){
up_write(&pinf->inode_rwsem);
//if(pinf->igflags & igflag_secure)
iput(pin);
}
gwt(printk("<0>" "%s * ret= 0 *\n",repp));
gwt(memset(repp,0,replen));
gwt(kfree(repp));
return 0;
cs_bat_ret:
put_crust_struct(inf->inode_crust_struct);
bat_ret:
err=-1;
bat_ret2:
for(i=0;i<res_num;i++)
BAT_clear_one_block(gsb, res[i]);
if(pin){
up_write(&pinf->inode_rwsem);
iput(pin);
}
if(err==-1)
up_write(&inf->inode_rwsem);
gwt(printk("<0>" "%s * ret= -1 *\n",repp));
gwt(memset(repp,0,replen));
gwt(kfree(repp));
return -1;
}
int GSFS_make_sec(struct super_block*sb, char* dest){
struct nameidata nd;
int ret;
struct inode* in;
ret=path_lookup(dest, LOOKUP_DIRECTORY, &nd);
if(ret)
return ret;
in=nd.path.dentry->d_inode;
path_put(&nd.path);
if(in->i_sb->s_magic != GSFS_MAGIC)
return -1;
if(in->i_ino == 1)
return -1;
atomic_inc(&in->i_count);
ret=add_user_block_to_inode(in);
iput(in);
return ret;
}
#ifdef gsfs_test
#define test_add_users_to_inode
#endif
#ifdef test_add_users_to_inode
#define gwti(p) p
#else
#define gwti(p)
#endif
void make_inl_unencrypted(struct inode* in){
struct GSFS_inode *inf=(struct GSFS_inode*)in->i_private,
*pinf;
struct inode *pin;
struct GSFS_inode_disk_inf *ind=&inf->disk_info;
struct super_block *sb=in->i_sb;
struct buffer_head *bh;
#ifdef test_add_users_to_inode
char repp[1000],
*rep=repp;
#endif
if(!(inf->igflags & igflag_secure))
return;
if(!(inf->igflags & igflag_encrypted_inl))
return;
gwti(sprintf(rep,"make_inl_unencrypted for inode %lu with flags %x * ",in->i_ino, inf->igflags));
gwti(rep+=strlen(rep));
down_write(&inf->inode_rwsem);
inf->igflags &= ~igflag_encrypted_inl;
pin=GSFS_get_inode((struct GSFS_sb*)in->i_sb->s_fs_info, inf->disk_info.parent_ino);
//inf->parent;
if(pin){
short gdlen=gsfs_dirent_len;
short gd_per_page=Block_Size/gdlen;
int gdpage,
gdoffset;
struct GSFS_dirent* gd;
char *gdhash;
struct gdirent_inl inl;
char key[gsfs_aes_keylen];
pinf=(struct GSFS_inode*)pin->i_private;
down_write(&pinf->inode_rwsem);
gdpage=ind->index_in_parent/gd_per_page;
gdoffset=ind->index_in_parent%gd_per_page;
bh=__bread(sb->s_bdev, get_dp_bn_of_in(pin,gdpage), Block_Size);
gd=(struct GSFS_dirent*)bh->b_data;
gd+=gdoffset;
//changing flags
gd->gd_flags &= ~igflag_encrypted_inl;
gwti(sprintf(rep,"new gdflags : %x * ",gd->gd_flags));
gwti(rep+=strlen(rep));
//decrypting inl
spin_lock(&inf->inode_crust_struct->lock);
crust_get_key_of_state(&inf->inode_crust_struct->crust_state, gd->gd_dirent_inl_ver, key);
spin_unlock(&inf->inode_crust_struct->lock);
decrypt_inl(&inl, &gd->gd_inl, key);
gwti(sprintf(rep,"decrypting inl to name:%s, len:%d, in:%u * ", inl.name, inl.len, inl.ino));
gwti(rep+=strlen(rep));
memcpy(&gd->gd_inl, &inl, gsfs_inl_len);
//new hash of gdirent
//adding new hash of gdirent to parent
gdhash=kzalloc(gsfs_hashlen, GFP_KERNEL);
get_gdirent_hash(gdhash, gd);
gwti(sprintf(rep,"new gdhash : "));
gwti(rep+=strlen(rep));
gwti(printhexstring(gdhash, rep, 16));
gwti(rep+=strlen(rep));
inf->add_event_to_parent(in, GDirent_Hash_Changed_Event, gdhash, gsfs_hashlen, 0);
mark_buffer_dirty(bh);
set_buffer_uptodate(bh);
brelse(bh);
mark_inode_dirty(pin);
up_write(&pinf->inode_rwsem);
}
inf->igflags |=igflag_inode_metadata_changed;
mark_inode_dirty(in);
up_write(&inf->inode_rwsem);
gwti(printk("<0>" "%s\n",repp));
if(pin){
make_inl_unencrypted(pin);
iput(pin);
}
return;
}
//you should not get inf->inode_rwsem
//you should not get pinf->inode_rwsem before calling
void update_inode_users(struct inode* in, int update_users){
struct GSFS_inode *inf=(struct GSFS_inode*)in->i_private,
*pinf=0;
struct inode *pin=0;
struct GSFS_inode_disk_inf *ind=&inf->disk_info;
int k;
#ifdef test_add_users_to_inode
char repp[1000],
*rep=repp;
#endif
gwti(sprintf(rep, "update_inode_users for inode: %ld * ", in->i_ino));
gwti(rep+=strlen(rep));
down_write(&inf->inode_rwsem);
if(update_users){
if(inf->igflags & igflag_active_user_block)
if(!(inf->igflags & igflag_active_parent_link)){
gwti(sprintf(rep, " This inode hasn't parent link and therefore its users will not change * "));
gwti(rep+=strlen(rep));
goto ret_sem;
}
pin=GSFS_get_inode((struct GSFS_sb*)in->i_sb->s_fs_info, inf->disk_info.parent_ino);
//inf->parent;
if(!pin){
gwti(sprintf(rep, "No parent * "));
gwti(rep+=strlen(rep));
goto ret_sem;
}
pinf=(struct GSFS_inode*)pin->i_private;
if(!pinf){
gwti(sprintf(rep, "No parent inf* "));
gwti(rep+=strlen(rep));
goto ret_sem;
}
//we havent got it from parent
down_write(&pinf->inode_rwsem);
put_users(inf->users);
//put_crust_struct(inf->inode_crust_struct);
inf->users=0;
//inf->inode_crust_struct=0;
k=get_inode_users_and_or_crust_state_from_parent(pin, ind->index_in_parent, 0, &inf->users,
ind->dir_inode_security.user_block,
ind->dir_inode_security.inode_user_block_hash,
inf->igflags, 0, 1, 0);
//we havent got it from parent
up_write(&pinf->inode_rwsem);
gwti(sprintf(rep, "k= %d * ",k));
gwti(rep+=strlen(rep));
}
if(ind->dir_inode_security.child_num){
int res_num;
res_num=avl_tree_get_size(inf->children);
if(res_num){
atn** res;
int i;
res=kzalloc(sizeof(atn *)*res_num,GFP_KERNEL);
avl_tree_get_all_nodes(inf->children, res, res_num);
up_write(&inf->inode_rwsem);
for(i=0;i<res_num;i++){
struct inode* in=res[i]->data->inode;
if(in)
update_inode_users(in, 1);
#ifdef test_add_users_to_inode
if(in)
sprintf(rep, "update_inode_users for inode: %ld * ",res[i]->data->inode->i_ino);
else
sprintf(rep, "update_inode_users for children with no inode ???? * ");
rep+=strlen(rep);
#endif
}
kfree(res);
down_write(&inf->inode_rwsem);
}
}
ret_sem:
up_write(&inf->inode_rwsem);
if(pin)
iput(pin);
gwti(printk("<0>" "%s\n",repp));
return ;
}
int add_users_to_inode(struct inode* in, unsigned int* uids, unsigned int* writes, int num){
struct GSFS_inode *inf=(struct GSFS_inode*)in->i_private,
*pinf=0;
struct inode *pin=0;
struct GSFS_inode_disk_inf *ind=&inf->disk_info;
struct super_block *sb=in->i_sb;
struct GSFS_sb *gsb=(struct GSFS_sb*)sb->s_fs_info;
struct buffer_head *bh;
int ret=-1;
struct inode_user_page* iup;
struct rsa_key *rkey;
int i;
struct users *pusers,
**uev;
crust_struct **csev=0;
#ifdef test_add_users_to_inode
char *repp,
*rep;
repp=kzalloc(3000,GFP_KERNEL);
rep=repp;
#endif
if(!(inf->igflags & igflag_secure))
return -1;
if(inf->igflags & igflag_incomplete_inode)
return -1;
if(ind->iuid != current->loginuid)
return -1;
//in this version we cant add user block to inode with children
//because this inode can access other children of parent if we get it the crust_state of parent
//we should find some solutions for this problem
if(!(inf->igflags & igflag_first_level_sec_inode))
if(ind->dir_inode_security.child_num)
return -1;
gwti(sprintf(rep,"add_users_to_inode for inode: %lu and flags: %x * ",in->i_ino, inf->igflags));
gwti(rep+=strlen(rep));
down_write(&inf->inode_rwsem);
//if in is not first_level_sec we should make it fls
if(!(inf->igflags & igflag_first_level_sec_inode)){
unsigned int res;
//block dedication for user_block
if( BAT_get_some_blocks(gsb, 1, &res)!=1)
goto retsem;
ind->dir_inode_security.user_block=res;
//ind->dir_inode_security.last_crust_ver=inf->crust_last_ver;
gwti(sprintf(rep,"adding user block to inode with number: %u * ",res));
gwti(rep+=strlen(rep));
}
//adding users to user block user block
bh=__bread(sb->s_bdev, ind->dir_inode_security.user_block, Block_Size);
lock_buffer(bh);
iup=(struct inode_user_page*)bh->b_data;
gwti(sprintf(rep,"openning user block with number %u * ",ind->dir_inode_security.user_block));
gwti(rep+=strlen(rep));
//adding owner_rsa_key if user block is new === always
if(inf->igflags & igflag_first_level_sec_inode){
//comparing user_block hash
char ubhash[gsfs_hashlen];
int hr;
hr=get_user_block_hash(ubhash, bh->b_data);
if(hr || strncmp(ubhash, ind->dir_inode_security.inode_user_block_hash, gsfs_hashlen)){
printk("<1>" "Bad user block hash for inode:%ld\n", in->i_ino);
gwti(sprintf(rep,"Bad user block hash for inode * "));
gwti(rep+=strlen(rep));
unlock_buffer(bh);
brelse(bh);
goto retsem;
}
/*
if(!inf->inode_crust_struct->owner_key)
if(read_owner_key_for_crust_struct(inf->inode_crust_struct, in->i_sb,
ind->dir_inode_security.user_block_hash)){
gwti(sprintf(rep,"unable to read owner_key of crsut_struct * "));
gwti(rep+=strlen(rep));
goto retsem;
}
*/
}
else{
/*
if(!(inf->igflags & igflag_present_owner_key))
if(read_owner_key_for_sec_inode(in)){
gwti(sprintf(rep,"Cannot read owner key of inode * "));
gwti(rep+=strlen(rep));
brelse(bh);
goto retsem;
}
*/
crust_struct *pcs;
//we should allocate new crust_struct and write first fields of iup
put_crust_struct(inf->inode_crust_struct);
inf->inode_crust_struct=kzalloc(sizeof(crust_struct), GFP_KERNEL);
spin_lock_init(&inf->inode_crust_struct->lock);
inf->inode_crust_struct->owner_key=kmalloc(gsfs_aes_keylen, GFP_KERNEL);
get_random_bytes(inf->inode_crust_struct->owner_key, gsfs_aes_keylen);
gwti(sprintf(rep,"user_block is new and new owner_key : "));
gwti(rep+=strlen(rep));
gwti(printhexstring(inf->inode_crust_struct->owner_key, rep, 16));
gwti(rep+=strlen(rep));
inf->inode_crust_struct->count=1;
inf->inode_crust_struct->max_ver=0;
inf->inode_crust_struct->user_block=ind->dir_inode_security.user_block;
crust_get_next_state(&inf->inode_crust_struct->crust_state, inf->inode_crust_struct->max_ver,
inf->inode_crust_struct->owner_key);
gwti(sprintf(rep,"* inode_crust_state: "));
gwti(rep+=strlen(rep));
gwti(printhexstring((char*)&inf->inode_crust_struct->crust_state, rep, 81));
gwti(rep+=strlen(rep));
//add_event for new crust_struct
csev=kmalloc(sizeof(crust_struct*), GFP_KERNEL);
*csev=get_crust_struct(inf->inode_crust_struct);
//adding later when we will have pinf;
iup->num=1;
iup->max_ver=0;
rkey=get_rsa_key(gsb, current->loginuid, 0);
spin_lock(&rkey->lock);
iup->owner_key.uid=current->loginuid;
iup->owner_key.writability=1;
i=rsa_encrypt_owner_key_for_user_block(iup->owner_key.rsa_encrypted_key,
inf->inode_crust_struct->owner_key, rkey->key);
gwti(sprintf(rep,"adding rsa encrypted owner key: %d * ",i));
gwti(rep+=strlen(rep));
iup->users_key[0].uid=current->loginuid;
iup->users_key[0].writability=1;