-
Notifications
You must be signed in to change notification settings - Fork 3
/
mvfs_auditops.c
1680 lines (1493 loc) · 50.9 KB
/
mvfs_auditops.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
/* * (C) Copyright IBM Corporation 1991, 2013. */
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Author: IBM Corporation
This module is part of the IBM (R) Rational (R) ClearCase (R)
Multi-version file system (MVFS).
For support, please visit http://www.ibm.com/software/support
*/
/*
*/
#include "mvfs_audit.h"
#include "mvfs_base.h"
#include "mvfs_systm.h"
#include "mvfs.h"
#include <tbs_errno.h>
#include "mvfs_copy.h"
#include "mvfs_transtype.h"
mvfs_audit_data_t mvfs_audit_data_var;
STATIC MVFS_NOINLINE mfs_auditfile_t *
mfs_afpnew(
char *pname,
char *upname,
CLR_VNODE_T *cvp
);
STATIC void MVFS_NOINLINE
mfs_afpdestroy(
mfs_auditfile_t *afp
);
STATIC MVFS_NOINLINE struct mfs_auditfile *
mfs_afpget(
char *pname,
char *upname,
CLR_VNODE_T *cvp
);
STATIC void mfs_afp_obsolete(P1(mvfs_thread_t *mth));
STATIC int mfs_cmpdirrec(P1(mfs_auditrec_t *rp)
PN(u_int kind)
PN(VNODE_T *dvp)
PN(char *nm)
PN(size_t nmlen)
PN(VNODE_T *vp)
PN(VATTR_T *vap));
STATIC int mfs_cmprwrec(P1(mfs_auditrec_t *rp)
PN(u_int kind)
PN(VNODE_T *vp)
PN(VATTR_T *vap));
STATIC int mfs_cmpviewrec(P1(mfs_auditrec_t *rp)
PN(VNODE_T *vp));
STATIC int mfs_isdupl(P1(mfs_auditfile_t *afp)
PN(u_int kind)
PN(VNODE_T *dvp)
PN(char *nm)
PN(size_t nmlen)
PN(VNODE_T *vp)
PN(VATTR_T *vap));
STATIC void mvfs_auditwrite_int(P1(mfs_auditfile_t *afp)
PN(mvfs_thread_t *mth));
/*
* Initialize the structures for managing the audit subsystem
*/
int
mvfs_auditinit(mvfs_cache_sizes_t *mma_sizes)
{
mvfs_audit_data_t *madp;
MDKI_AUDIT_ALLOC_DATA();
madp = MDKI_AUDIT_GET_DATAP();
INITLOCK(&(madp->mfs_aflock), "mfsaflck");
INITSPLOCK(madp->mvfs_audgenlock,"mvfs_audgenlock");
INIT_LIST(madp->mfs_aflist, mfs_auditfile_t);
#ifdef MVFS_DEBUG
madp->mvfs_debug_no_audit_detail = 0;
#endif
madp->mvfs_audit_gen = 0;
return 0;
}
void
mvfs_auditfree()
{
mvfs_audit_data_t *madp = MDKI_AUDIT_GET_DATAP();
FREELOCK(&(madp->mfs_aflock));
FREESPLOCK(madp->mvfs_audgenlock);
/* Free the memory for the mvfs_audit_data_t if it was allocated. */
MDKI_AUDIT_FREE_DATA();
}
/*
* Utility routines to manipulate the audit file struct
*/
/*
* MFS_AFPNEW - create an afp struct
*/
STATIC MVFS_NOINLINE mfs_auditfile_t *
mfs_afpnew(
char *pname,
char *upname,
CLR_VNODE_T *cvp
)
{
mvfs_audit_data_t *madp = MDKI_AUDIT_GET_DATAP();
mfs_auditfile_t *afp;
ASSERT(ISLOCKED(&(madp->mfs_aflock))); /* Need lock to avoid races */
afp = (mfs_auditfile_t *)KMEM_ALLOC(sizeof(mfs_auditfile_t), KM_NOSLEEP);
if (afp != NULL) {
BZERO(afp, sizeof(*afp));
afp->buf = (mfs_auditrec_t *)KMEM_ALLOC(mvfs_auditbufsiz, KM_NOSLEEP);
if (afp->buf == NULL) goto errout;
afp->buflen = mvfs_auditbufsiz;
afp->path = STRDUP(pname);
if (afp->path == NULL) goto errout;
afp->upath = STRDUP(upname);
if (afp->upath == NULL) goto errout;
/* Save credentials of "creating" process. These are used
for all writes to the file. Prevents trojan horses from
using a filename and then executing a setuid program
which has greater rights. */
afp->cred = MVFS_DUP_DEFAULT_CREDS();
afp->cvp = cvp;
CVN_HOLD(cvp); /* Hold for this reference */
INITLOCK(&afp->lock, "afplock ");
afp->curpos = afp->buf;
/*
* It is OK to set these values without the afp->lock held
* since the afp is not yet on the mfs_aflist.
*/
afp->refcnt = 1; /* init */
afp->destroy = 0; /* init destroy flag */
afp->obsolete = 0; /* init obsolete flag */
ADD_TO_EOL(&(madp->mfs_aflist), afp);
}
return(afp);
errout:
if (afp) {
if (afp->buf) KMEM_FREE(afp->buf, afp->buflen);
if (afp->path) STRFREE(afp->path);
if (afp->upath) STRFREE(afp->upath);
if (afp->cvp) CVN_RELE(afp->cvp);
KMEM_FREE(afp, sizeof(*afp));
}
return(NULL);
}
/*
* MFS_AFPGET - find and existing or create a new afp struct
* Note that the "key" is only the pathname. If the user
* uses different pathnames to the same file, the audits
* will not be synchronized, and the audit output file may
* contain trash.
*/
STATIC MVFS_NOINLINE struct mfs_auditfile *
mfs_afpget(
char *pname,
char *upname,
CLR_VNODE_T *cvp
)
{
mvfs_audit_data_t *madp = MDKI_AUDIT_GET_DATAP();
register struct mfs_auditfile *afp;
MVFS_LOCK(&(madp->mfs_aflock)); /* lock AF list */
for (afp = madp->mfs_aflist.next;
afp != (mfs_auditfile_t *)&(madp->mfs_aflist);
afp = afp->next)
{
/*
* Only reuse audit entry if (1) it is not marked to be destroyed,
* and (2) there is a match on both the pathnames and the vnode
* (from the lookup of that pathname).
* It is possible to have entries that have not been
* freed yet because some daemon was started under the build.
* e.g. clearmake creates /tmp/auditfile
* long-running daemons starts and keeps running.
* clearmake terminates audit, and does audit for /tmp/auditfile
* clearmake deleted (unlinks) /tmp/auditfile
* (note that long-running daemon still has vnode open).
* clearmake creates new scratch auditfile at /tmp/auditfile
* but this is a different object (and vnode), even
* though the name is the same.
*/
if (afp->destroy != 1 &&
(STRCMP(pname, afp->path) == 0) &&
CVN_CMP(cvp, afp->cvp) &&
(STRCMP(upname, afp->upath) == 0))
{
MVFS_LOCK(&afp->lock);
afp->refcnt++;
afp->obsolete = 0; /* Can't be obsolete. */
MVFS_UNLOCK(&afp->lock);
MVFS_UNLOCK(&(madp->mfs_aflock));
MDB_XLOG((MDB_AUDITF,
"afpget: afp=%"KS_FMT_PTR_T" refcnt=%d pid=%d\n",
afp, afp->refcnt, MDKI_CURPID()));
return(afp);
}
}
/* No previous found, allocate a new struct */
afp = mfs_afpnew(pname, upname, cvp);
MVFS_UNLOCK(&(madp->mfs_aflock));
MDB_XLOG((MDB_AUDITF, "afpget: (new) afp=%"KS_FMT_PTR_T" refcnt=%d pid=%d\n",
afp, afp->refcnt, MDKI_CURPID()));
return(afp);
}
STATIC void MVFS_NOINLINE
mfs_afpdestroy(
mfs_auditfile_t *afp
)
{
mvfs_audit_data_t *madp = MDKI_AUDIT_GET_DATAP();
ASSERT(afp->refcnt = 1); /* No destroy if more refs */
MDB_XLOG((MDB_AUDITF, "afpdestroy: afp=%"KS_FMT_PTR_T" refcnt=%d pid=%d\n",
afp, afp->refcnt, MDKI_CURPID()));
MVFS_LOCK(&(madp->mfs_aflock));
RM_LIST(afp);
MVFS_UNLOCK(&(madp->mfs_aflock));
FREELOCK(&afp->lock);
if (afp->cvp) CVN_RELE(afp->cvp);
if (afp->path) STRFREE(afp->path);
if (afp->upath) STRFREE(afp->upath);
if (afp->buf) KMEM_FREE(afp->buf, afp->buflen);
if (afp->cred) MDKI_CRFREE(afp->cred);
MVFS_FREE_VATTR_FIELDS(&afp->va);
#ifdef MVFS_DEBUG
BZERO(afp, sizeof(*afp));
#endif
KMEM_FREE(afp, sizeof(*afp));
}
void
mfs_afphold(afp)
mfs_auditfile_t *afp;
{
mvfs_audit_data_t *madp = MDKI_AUDIT_GET_DATAP();
MVFS_LOCK(&afp->lock);
afp->refcnt++;
MVFS_UNLOCK(&afp->lock);
/* we can't debug print inside afphold() because of spin locks
* held. by callers. This debug code is now in the callers.
*/
/* MDB_XLOG((MDB_AUDITF, "afphold: afp=%"KS_FMT_PTR_T" refcnt=%d pid=%d\n",
afp, afp->refcnt, MDKI_CURPID())); */
}
/*
* thr must be the caller's thread struct. It's passed in for
* efficiency (so we don't need to do a hash lookup to find it
* ourselves.
*/
void
mvfs_afprele(afp, thr)
mfs_auditfile_t *afp;
mvfs_thread_t *thr;
{
mvfs_common_data_t *mcdp = MDKI_COMMON_GET_DATAP();
mvfs_audit_data_t *madp = MDKI_AUDIT_GET_DATAP();
if (afp == NULL) return;
/*
* can't hold it over potential I/O or blocking memory allocation *
* operations
*/
ASSERT(NOTLOCKEDBYME(&(mcdp->proc_thr.mvfs_proclock)));
MDB_XLOG((MDB_AUDITF, "afprele: afp=%"KS_FMT_PTR_T" refcnt=%d pid=%d\n",
afp, afp->refcnt, MDKI_CURPID()));
MVFS_LOCK(&afp->lock);
if (afp->refcnt == 1) { /* Sync contents */
mvfs_auditwrite_int(afp, thr);
/*
* Need to unlock apf->lock to prevent a deadlock, mfs_afpdestroy
* needs to take the global mfs_aflock. So we mark this afp for
* destroy so it will be skipped when searching the auditfile list.
*/
afp->destroy = 1;
MVFS_UNLOCK(&afp->lock);
mfs_afpdestroy(afp);
} else {
afp->refcnt--;
MVFS_UNLOCK(&afp->lock);
}
}
/* There are two versions because we need to release from both threads
and processes */
/* This must be called with the thread's proc structure already locked */
void
mvfs_afprele_proc(
mvfs_proc_t *proc,
mvfs_thread_t *thr
)
{
mvfs_common_data_t *mcdp = MDKI_COMMON_GET_DATAP();
mfs_auditfile_t *my_afp;
ASSERT(ISLOCKEDBYME(&(mcdp->proc_thr.mvfs_proclock)));
/* We're going to unlock so we can do the rele (because it might do I/O).
** Our caller is in the process of freeing the proc structure and this is
** the rele for the reference to the afp stored there. So, set it to NULL
** to make sure somebody else doesn't stumble on it while we're unlocked.
**
** Note, the other way a proc could get an afp would be to call
** mfs_afpget(), which searches through a list of afp's (mfs_aflist) and, if
** it finds a "matching" one, holds it and returns it. That case is handled
** in mfs_afprele() above (in the afp_race: "loop").
*/
my_afp = proc->mp_afp;
proc->mp_afp = NULL;
if (my_afp != NULL) {
MVFS_UNLOCK(&(mcdp->proc_thr.mvfs_proclock));
mvfs_afprele(my_afp, thr);
} else {
/* Our "contract" says to return with the proc lock unlocked. */
MVFS_UNLOCK(&(mcdp->proc_thr.mvfs_proclock));
}
}
void
mvfs_afprele_thr(thr)
mvfs_thread_t *thr;
{
mvfs_afprele(thr->thr_afp, thr);
thr->thr_afp = NULL; /* Kill ptr */
}
STATIC void
mfs_afp_obsolete(mth)
mvfs_thread_t *mth;
{
register mfs_auditfile_t *afp;
afp = mth->thr_afp;
MVFS_LOCK(&afp->lock);
afp->obsolete = 1; /* Set obsolete flag */
MVFS_UNLOCK(&afp->lock);
return;
}
/*
* MFS_GETBH - get current build handle
*/
view_bhandle_t
mfs_getbh(CALL_DATA_T *cd)
{
mvfs_thread_t *mth;
mth = MVFS_MYTHREAD(cd);
return(mth->thr_bh);
}
/*
* MFS_AUDITIOCTL - ioctl's related to audits. Note that the
* arg data has already been copied in (except for strbuf's) and
* that any results will be copied out by mfs_mioctl() in vnodeops.
*/
int
mfs_auditioctl(
mvfscmd_block_t *kdata,
CALL_DATA_T *cd,
MVFS_CALLER_INFO *callinfo
)
{
mvfs_thread_t *mth;
tbs_status_t tbsstatus = TBS_ST_OK;
int error = 0;
int s;
timestruc_t stime; /* For statistics */
timestruc_t dtime;
MDKI_HRTIME(&stime);
mvfs_procpurge(MVFS_PROCPURGE_SLEEP); /* Clean up any dead proc state */
/* mfs_mioctl() validated the call and copied in cmdblock; we
* copy in data here, and convert for LP64 if necessary
*/
mth = MVFS_MYTHREAD(cd); /* Get my process state */
switch (MCB_CMD(kdata)) {
case MVFS_CMD_GET_BH: {
auto mvfs_bhinfo_t bhinfo;
if ((error = CopyInMvfs_bhinfo(kdata->infop, &bhinfo, callinfo)) != 0)
break;
bhinfo.bh = mth->thr_bh;
bhinfo.bh_ref_time = mth->thr_bh_ref_time;
bhinfo.flags = 0;
if (mth->thr_usereftime) bhinfo.flags |= MVFS_BHF_DNC_REFTIME;
if (mth->thr_attrgen != 0) bhinfo.flags |= MVFS_BHF_REVALIDATE;
error = CopyOutMvfs_bhinfo(&bhinfo, kdata->infop, callinfo);
break;
}
case MVFS_CMD_SET_BH: {
auto mvfs_bhinfo_t bhinfo;
if ((error = CopyInMvfs_bhinfo(kdata->infop, &bhinfo, callinfo)) != 0)
break;
kdata->status = TBS_ST_OK;
tbsstatus = MVFS_ESTABLISH_FORK_HANDLER();
if (tbsstatus != TBS_ST_OK) break;
mth->thr_bh = bhinfo.bh;
mth->thr_bh_ref_time = bhinfo.bh_ref_time;
mth->thr_usereftime = ((bhinfo.flags & MVFS_BHF_DNC_REFTIME) != 0);
/*
* Update attributes generation if requested
* Note: mfs_mn_newattrgen gets the mnode monitor lock (mfs_mnlock)
*/
if ((bhinfo.flags & MVFS_BHF_REVALIDATE) != 0) {
mth->thr_attrgen = mfs_mn_newattrgen();
}
mvfs_sync_procstate(mth);
MDB_XLOG((MDB_AUDITF, "newsetbh: bh=%x.%x ref=%"KS_FMT_TV_SEC_T_X".%"KS_FMT_TV_USEC_T_X" gen=%d pid=%d\n",
bhinfo.bh.build_session, bhinfo.bh.target_id,
bhinfo.bh_ref_time.tv_sec, bhinfo.bh_ref_time.tv_usec,
mth->thr_attrgen,
MDKI_CURPID()));
break;
}
/*
* New invalidation based on generation tag.
* Forces refetch of all attributes from view for objects
* referenced by this process (and children from forks after
* this point!)
*/
case MVFS_CMD_REVALIDATE:
mth->thr_attrgen = mfs_mn_newattrgen();
mvfs_sync_procstate(mth);
break;
case MVFS_CMD_GET_AFILE: {
auto mfs_strbufpn_t afile;
if ((error = CopyInMfs_strbufpn(kdata->infop, &afile, callinfo)) != 0)
break;
kdata->status = TBS_ST_OK;
if (mth->thr_afp && mth->thr_afp->upath)
error = mfs_copyout_strbufpn(afile, mth->thr_afp->upath);
else
error = mfs_copyout_strbufpn(afile, NULL);
if (error != 0)
break;
error = CopyOutMfs_strbufpn(&afile, kdata->infop, callinfo);
break;
}
case MVFS_CMD_SET_AFILE: {
auto mfs_strbufpn_pair_t afile_info;
auto char *path;
auto char *upath;
auto CLR_VNODE_T *afvp;
auto SPL_T s;
mvfs_audit_data_t *madp = MDKI_AUDIT_GET_DATAP();
if ((error = CopyInMfs_strbufpn_pair(kdata->infop, &afile_info, callinfo)) != 0)
break;
/* We're going to use the pathname strings that we just copied in
** pointers to, so we need to copy them in as well. We could use
** the MFS_STRBUFPN_PAIR_COPYIN_STRS macro defined in
** sys/mfs_ioctl.h. However, we really just want the pathnames
** (path and upath) and are no longer interested in the
** mfs_strbufpn_pair structure, so it makes sense to just copy in
** the strings. A few notes...on Unix/Linux the
** MFS_STRBUFPN_PAIR_GET_KPN macro just returns the upn, so we are
** actually copying in the same string twice. Also, at least one
** of our callee's (mfs_afpget) may copy the strings we pass in
** with STRDUP, so we may "permanently" use the the "duplicated"
** space. However, we're willing to waste the space for coding
** simplicity.
*/
error = mfs_copyin_strbufpn(MFS_STRBUFPN_PAIR_GET_KPN(&afile_info), &path);
if (error) break;
error = mfs_copyin_strbufpn(MFS_STRBUFPN_PAIR_GET_UPN(&afile_info), &upath);
if (error) {
STRFREE(path);
break;
}
/*
* Verify the file exists and is suitable for auditing
* This is very important or deadlocks/crashes may
* result from unexpected recursion in the MFS!
*/
MFS_INHAUDIT(mth);
error = LOOKUP_AUDIT_FILE(path, &afvp, MVFS_CD2CRED(cd));
if (!error) {
CLR_VNODE_T *rvp = NULL;
if (MVOP_REALCVP(afvp, &rvp) == 0 && afvp != rvp) {
/*
* got a realvp through the operation, use that instead.
*/
CVN_HOLD(rvp);
CVN_RELE(afvp);
afvp = rvp;
}
if (MFS_VPISMFS(MVFS_CVP_TO_VP(afvp))) {
error = EINVAL;
} else if (!MVFS_ISVTYPE(MVFS_CVP_TO_VP(afvp), VREG)) {
error = EISDIR;
} else {
error = MVOP_ACCESS(MVFS_CVP_TO_VP(afvp), VWRITE, 0,
cd, NULL);
}
if (error) CVN_RELE(afvp);
}
MFS_ENBAUDIT(mth);
if (error) {
MDB_XLOG((MDB_AUDITF, "setafile: path=%s error=%d pid=%d\n",
path, error, MDKI_CURPID()));
STRFREE(path);
STRFREE(upath);
break;
}
tbsstatus = MVFS_ESTABLISH_FORK_HANDLER();
if (tbsstatus != TBS_ST_OK) break;
/* File is suitable, release old file binding and
try to attach new file. */
if (mth->thr_afp != NULL) {
mvfs_afprele_thr(mth);
}
mth->thr_afp = mfs_afpget(path, upath, afvp);
if (mth->thr_afp == NULL) error = ENOMEM;
/*
* Assign a new audit generation number
* to force a unique choid for every different
* audit file (on DO's).
*/
SPLOCK(madp->mvfs_audgenlock, s);
madp->mvfs_audit_gen++; /* Protected by mvfs_audgenlock */
mth->thr_aud_seq = madp->mvfs_audit_gen; /* State for this process */
SPUNLOCK(madp->mvfs_audgenlock, s);
mvfs_sync_procstate(mth);
CVN_RELE(afvp); /* afpget held audit file vnode if needed */
MDB_XLOG((MDB_AUDITF, "setafile: path=%s afp=%"KS_FMT_PTR_T" err=%d pid=%d\n",
path, mth->thr_afp, error, MDKI_CURPID()));
STRFREE(path);
STRFREE(upath);
break;
}
case MVFS_CMD_GET_PROCF: {
auto u_long procflags;
procflags = 0;
if (mth->thr_auditon) procflags |= MVFS_PF_AUDITON;
if (mth->thr_auditv) procflags |= MVFS_PF_AUDITVOB;
if (mth->thr_auditnv) procflags |= MVFS_PF_AUDITNONVOB;
error = CopyOutMvfs_u_long(&procflags, kdata->infop, callinfo);
break;
}
case MVFS_CMD_SET_PROCF: {
auto u_long procflags;
if ((error = CopyInMvfs_u_long(kdata->infop, &procflags, callinfo)) != 0)
break;
tbsstatus = MVFS_ESTABLISH_FORK_HANDLER();
if (tbsstatus != TBS_ST_OK) break;
mth->thr_auditv = ((procflags & MVFS_PF_AUDITVOB) != 0);
mth->thr_auditnv= ((procflags & MVFS_PF_AUDITNONVOB) != 0);
mvfs_sync_procstate(mth);
break;
}
case MVFS_CMD_START_AUDIT: {
auto u_long aflags;
if ((error = CopyInMvfs_u_long(kdata->infop, &aflags, callinfo)) != 0)
break;
MDB_XLOG((MDB_AUDITF, "startaudit: pid=%d\n", MDKI_CURPID()));
tbsstatus = MVFS_ESTABLISH_FORK_HANDLER();
if (tbsstatus != TBS_ST_OK) break;
/*
* We need to insure no other thread has set the audit
* state since we last synchronized, so we take the proc lock
* and check it out.
*/
MVFS_LOCK(&(mth->thr_proc->mp_lock));
if (mth->thr_proc->mp_auditon) {
MVFS_UNLOCK(&(mth->thr_proc->mp_lock));
error = EEXIST;
break;
}
if (mth->thr_afp != mth->thr_proc->mp_afp) {
/* build interference of some sort. Some other thread
* changed the audit file pointer between when we
* entered the FS and now. We can't handle this if we
* want to be atomic on audit state enabling per
* process.
*/
MVFS_UNLOCK(&(mth->thr_proc->mp_lock));
error = EINVAL; /* XXX ? */
break;
}
mth->thr_auditon = 1;
mth->thr_auditv = ((aflags & MVFS_PF_AUDITVOB) != 0);
mth->thr_auditnv= ((aflags & MVFS_PF_AUDITNONVOB) != 0);
#if defined(ATRIA_LP64) || defined(ATRIA_LLP64)
if (MDKI_CALLER_IS_32BIT(callinfo))
mth->thr_afp->af_transtype = 1;
#endif
mvfs_sync_procstate_locked(mth); /* XXX */
MVFS_UNLOCK(&(mth->thr_proc->mp_lock));
MVFS_MDEP_PROC_START_AUDIT(); /* update NT's mdep proc shadow */
break;
}
case MVFS_CMD_STOP_AUDIT: { /* Stops audit and release resources */
MVFS_MDEP_PROC_STOP_AUDIT(); /* update NT's mdep proc shadow */
MDB_XLOG((MDB_AUDITF, "stopaudit: pid=%d\n", MDKI_CURPID()));
if (mth->thr_afp) { /* Sync/release audit file if one */
mvfs_auditwrite(mth);
error = mth->thr_afp->auditwerr; /* Delayed write error */
mfs_afp_obsolete(mth); /* Obsolete (shut off) the auditfile */
mvfs_afprele_thr(mth); /* Release struct now */
}
mth->thr_auditon = 0;
mvfs_sync_procstate(mth); /* it will drop */
break;
}
case MVFS_CMD_SYNC_AUDIT: {
MDB_XLOG((MDB_AUDITF, "syncaudit: pid=%d\n", MDKI_CURPID()));
if (mth->thr_afp) { /* Sync the audit file */
mvfs_auditwrite(mth);
error = mth->thr_afp->auditwerr; /* Return any write error */
} else error = ENOENT; /* No audit file */
break;
}
default: {
error = ENOTTY;
break;
}
case MVFS_CMD_AUDIT_MARKER: {
auto u_long marker_flags;
if ((error = CopyInMvfs_u_long(kdata->infop, &marker_flags, callinfo)) != 0)
break;
MDB_XLOG((MDB_AUDITF, "auditmarker: pid=%d flags=%lx\n",
MDKI_CURPID(), marker_flags));
if (mth->thr_afp) {
/* write a marker */
MFS_AUDIT_EXT(MFS_AR_MARKER, NULL, NULL, NULL, NULL, NULL,
marker_flags, cd);
if (mth->thr_afp == NULL) {
/* If mth->thr_afp->obsolete was set, the mfs_audit
* will have set mth->thr_afp to NULL so there is
* nothing left for us to do. This means that someone
* else has called MVFS_CMD_STOP_AUDIT.
* I do not return EINVAL as we would if thr_afp was
* already NULL because we actually did the work that
* was needed to finish cleaning up the audit file.
*/
MDB_XLOG((MDB_AUDITF,
"auditmarker: Audit stopped. pid=%d flags=%lx\n",
MDKI_CURPID(), marker_flags));
break;
}
/*
* As a side-effect, auditwrite also cleans out the
* in-core buffer and thus ensures that items accessed
* on both sides of the marker get recorded on both
* sides.
*/
mvfs_auditwrite(mth);
error = mth->thr_afp->auditwerr;
} else
error = EINVAL;
}
} /* end of switch */
if (error == 0 && tbsstatus != TBS_ST_OK) {
error = tbs_status2errno(tbsstatus);
}
MVFS_BUMPTIME(stime, dtime, mfs_austat.au_ioctltime);
return(error);
}
int
mfs_v2objtype(VTYPE_T vtype)
{
switch (vtype) {
case VDIR: return(MFS_OT_DIR);
case VREG: return(MFS_OT_REG);
case VLNK: return(MFS_OT_LNK);
case VBLK: return(MFS_OT_BLK);
case VCHR: return(MFS_OT_CHR);
default: return(MFS_OT_NONE);
}
}
STATIC int
mfs_cmpdirrec(rp, kind, dvp, nm, nmlen, vp, vap)
register mfs_auditrec_t *rp;
u_int kind;
VNODE_T *dvp;
char *nm;
size_t nmlen;
VNODE_T *vp;
VATTR_T *vap;
{
struct timeval tv;
if (rp->mfs_dirrec.namlen != nmlen) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch namlen\n"));
return(0);
}
if (rp->mfs_dirrec.objtype != mfs_v2objtype(MVFS_GETVTYPE(vp))) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch objtype\n"));
return(0);
}
VATTR_GET_MTIME_TV(vap, &tv);
if (!MFS_TVEQ(tv, rp->mfs_dirrec.objdtm)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch dtm\n"));
return(0);
}
if (dvp && MFS_ISVOB(VTOM(dvp))) {
if (!MFS_OIDEQ(rp->mfs_dirrec.diroid,VTOM(dvp)->mn_vob.attr.obj_oid)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch diroid\n"));
return(0);
}
}
if (MFS_ISVOB(VTOM(vp))) {
if (!MFS_OIDEQ(rp->mfs_dirrec.objoid,VTOM(vp)->mn_vob.attr.obj_oid)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch objoid\n"));
return(0);
}
if (!MFS_OIDEQ(rp->mfs_dirrec.voboid,V_TO_MMI(vp)->mmi_voboid)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch voboid\n"));
return(0);
}
if (rp->mfs_dirrec.objsn.sn_high != VTOM(vp)->mn_vob.vfh.ver_dbid ||
rp->mfs_dirrec.objsn.sn_low != VTOM(vp)->mn_vob.vfh.gen) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch obj serial number\n"));
return(0);
}
}
/* Use bcmp - it is faster */
if (BCMP(rp->mfs_dirrec.name, nm, nmlen)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch name\n"));
return(0);
}
/* Everything matches, return a duplicate */
return(1);
}
STATIC int
mfs_cmpviewrec(rp, vp)
register mfs_auditrec_t *rp;
VNODE_T *vp;
{
char *nm = mfs_vp2vw(vp);
size_t nmlen;
if (nm) nmlen = STRLEN(nm);
else nmlen = 0;
if (MFS_VIEW(vp) == NULL) {
if (!MFS_UUIDNULL(rp->mfs_viewrec.viewuuid)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch uuid\n"));
return(0);
}
} else {
if (!MFS_UUIDEQ(rp->mfs_viewrec.viewuuid,
VTOM(MFS_VIEW(vp))->mn_view.svr.uuid)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch uuid\n"));
return(0);
}
}
if (BCMP(rp->mfs_viewrec.name, nm, nmlen)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch name\n"));
return(0);
}
/* All match - return dupl */
return(1);
}
STATIC int
mfs_cmprwrec(rp, kind, vp, vap)
register mfs_auditrec_t *rp;
u_int kind;
VNODE_T *vp;
VATTR_T *vap;
{
struct timeval tv;
if (rp->mfs_rwrec.objtype != mfs_v2objtype(MVFS_GETVTYPE(vp))) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch objtype\n"));
return(0);
}
/* Ignore DTM changes on writes. They always change
with every write. */
if (kind != MFS_AR_WRITE) {
VATTR_GET_MTIME_TV(vap, &tv);
if (!MFS_TVEQ(tv, rp->mfs_rwrec.objdtm)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch dtm\n"));
return(0);
}
}
if (MFS_ISVOB(VTOM(vp))) {
if (!MFS_OIDEQ(rp->mfs_rwrec.objoid,VTOM(vp)->mn_vob.attr.obj_oid)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch objoid\n"));
return(0);
}
if (!MFS_OIDEQ(rp->mfs_rwrec.voboid,V_TO_MMI(vp)->mmi_voboid)) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch voboid\n"));
return(0);
}
if (rp->mfs_dirrec.objsn.sn_high != VTOM(vp)->mn_vob.vfh.ver_dbid ||
rp->mfs_dirrec.objsn.sn_low != VTOM(vp)->mn_vob.vfh.gen) {
MDB_XLOG((MDB_AUDITOPS2, "mismatch obj serial number\n"));
return(0);
}
}
/* Everything matched - return a dupl */
return(1);
}
STATIC int
mfs_isdupl(afp, kind, dvp, nm, nmlen, vp, vap)
mfs_auditfile_t *afp;
u_int kind;
VNODE_T *dvp;
char *nm;
size_t nmlen;
VNODE_T *vp;
VATTR_T *vap;
{
int i;
mfs_auditrec_t *rp;
ASSERT(ISLOCKED(&afp->lock));
/* Look back through the current buffer for a duplicate.
At a max, look back a parameterized number of entries. This should
be tuned to handle a reasonable depth of LOOKUP/READ
pairs during hdr file nesting. */
for (i=0, rp = afp->lastpos; i < mvfs_duplsearchmax && rp >= afp->buf;
i++, rp = MFS_PREVREC(rp)) {
if (rp->kind != kind) {
continue;
}
switch (kind) {
case MFS_AR_ROOT:
case MFS_AR_LOOKUP:
case MFS_AR_RDLINK:
if (mfs_cmpdirrec(rp, kind, dvp,nm,nmlen, vp, vap)) return(1);
break;
case MFS_AR_READ:
case MFS_AR_WRITE:
if (mfs_cmprwrec(rp, kind, vp, vap)) return(1);
break;
case MFS_AR_LINK: /* Hard to dupl once name exists */
case MFS_AR_UNLINK: /* Hard to dupl once name gone */
case MFS_AR_CREATE: /* Hard to dupl once name created */
case MFS_AR_RENAME: /* Never dupls because name/oid changes */
case MFS_AR_CHOID: /* Hard to dupl cause can never get same oid */
case MFS_AR_SYMLINK: /* Hard to dupl symlink create */
return(0);
case MFS_AR_VIEW:
if (mfs_cmpviewrec(rp, vp)) return (1);
break;
case MFS_AR_MARKER:
break; /* never considered a duplicate */
}
}
/* No dupl found, return such */
MDB_XLOG((MDB_AUDITOPS2, "mfs_isdupl: no dupl: lastpos=%x\n", afp->lastpos));
return(0);
}
/*
* mfs_init_rmstat - routine to fill in a rmstat structure for a
* vnode. Used to save remove information from before the
* remove operation, because during the audit, we can't do a
* stat to get that info on a file-handle that may now
* be stale (which happened when the remove succeeded).
*/
void
mfs_init_rmstat(vp, rmstatp)
VNODE_T *vp;
mfs_auditrmstat_t *rmstatp;
{
mfs_mnode_t *mnp;
ASSERT(MFS_VPISMFS(vp));
mnp = VTOM(vp);
if (MFS_ISVOB(mnp)) {
rmstatp->objtype = (u_short)mfs_v2objtype(MVFS_GETVTYPE(vp));
rmstatp->objdtm.tv_sec = 0;
rmstatp->objdtm.tv_usec = 0;
rmstatp->voboid = V_TO_MMI(vp)->mmi_voboid;
rmstatp->objoid = mnp->mn_vob.attr.obj_oid;
rmstatp->objsn.sn_high = mnp->mn_vob.vfh.ver_dbid;
rmstatp->objsn.sn_low = mnp->mn_vob.vfh.gen;
rmstatp->elemoid = mnp->mn_vob.attr.elem_oid;
rmstatp->mtype = (ks_uint32_t) mnp->mn_vob.attr.mtype;
} else {
BZERO(rmstatp, sizeof(*rmstatp));
rmstatp->objtype = (u_short)mfs_v2objtype(MVFS_GETVTYPE(vp));
}
}
int
mfs_audit(
int kind,
VNODE_T *dvp,
char *nm1,
VNODE_T *dvp2,
char *nm2,
VNODE_T *vp,
u_long flags,
CALL_DATA_T *cd
)
{
register mfs_auditfile_t *afp;
register mfs_auditrec_t *rp = 0; /* shut up GCC */
mfs_auditrmstat_t *rmstatp;
mvfs_thread_t *mth;
struct timeval mtime;
mfs_mnode_t *mnp;