Whamcloud - gitweb
r=tappro
[fs/lustre-release.git] / lustre / smfs / audit.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/audit_mds.c
5  *  Lustre filesystem audit part for MDS
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27
28 #define DEBUG_SUBSYSTEM S_SM
29
30 #include <linux/kmod.h>
31 #include <linux/init.h>
32 #include <linux/fs.h>
33 #include <linux/slab.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_support.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_idl.h>
38 #include <linux/lustre_fsfilt.h>
39 #include <linux/lustre_smfs.h>
40 #include <linux/lustre_audit.h>
41 #include <linux/lustre_log.h>
42 #include "smfs_internal.h"
43
44 static audit_op hook2audit(hook_op hook)
45 {
46         audit_op opcode = AUDIT_UNKNOWN;
47
48         switch (hook) 
49         {
50                 case HOOK_CREATE:
51                 case HOOK_SYMLINK:
52                 case HOOK_MKDIR:
53                 case HOOK_MKNOD:
54                         return AUDIT_CREATE;
55                         
56                 case HOOK_LINK:
57                         return AUDIT_LINK;
58                                                 
59                 case HOOK_RMDIR:
60                 case HOOK_UNLINK:
61                         return AUDIT_UNLINK;
62                         
63                 case HOOK_READLINK:
64                         return AUDIT_READLINK;
65                         
66                 case HOOK_RENAME:
67                         return AUDIT_RENAME;
68                         
69                 case HOOK_SETATTR:
70                 case HOOK_F_SETATTR:
71                         return AUDIT_SETATTR;
72                         
73                 case HOOK_SI_WRITE:
74                 case HOOK_WRITE:
75                         return AUDIT_WRITE;
76                         
77                 case HOOK_SI_READ:
78                 case HOOK_READ:
79                         return AUDIT_READ;
80
81                 case HOOK_READDIR:
82                         return AUDIT_READDIR;
83
84                 default:
85                         break;
86         }
87         
88         return opcode;
89 }
90
91 struct inode * get_inode_from_hook(hook_op hook, void * msg) 
92 {
93         struct inode * inode;
94         
95         switch (hook)
96         {
97                 case HOOK_LINK:
98                 {
99                         struct hook_link_msg * m = msg;
100                         inode = m->dentry->d_inode;
101                         break;
102                 }
103                 case HOOK_UNLINK:
104                 case HOOK_RMDIR:
105                 {
106                         struct hook_unlink_msg * m = msg;
107                         inode = m->dentry->d_inode;
108                         break;
109                 }
110                 case HOOK_READLINK:
111                 {
112                         struct hook_symlink_msg * m = msg;
113                         inode = m->dentry->d_inode;
114                         break;
115                 }        
116                 case HOOK_RENAME:
117                 {
118                         struct hook_rename_msg * m = msg;
119                         inode = m->dentry->d_inode;
120                         break;
121                 }        
122                 default:
123                         inode = NULL;
124         }
125
126         return inode;
127 }
128
129 static inline int smfs_get_inode_audit(struct inode *inode, __u64 *mask)
130 {
131         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_cache_fsfilt;
132         struct smfs_inode_info * smi = I2SMI(inode);
133         int rc = 0;
134         
135         if (smi->au_info.au_valid)
136                 *mask = smi->au_info.au_mask;
137         else {
138                 rc = fsfilt->fs_get_xattr(I2CI(inode), AUDIT_ATTR_EA, mask, sizeof(*mask));
139                 if (rc <= 0)
140                         *mask = AUDIT_OFF;
141                 smi->au_info.au_valid = 1;
142                 smi->au_info.au_mask = *mask;
143         }
144         return 0;
145 }
146
147 /* is called also from fsfilt_smfs_get_info */
148 int smfs_get_audit(struct super_block * sb, struct inode * parent,
149                    struct inode * inode,  __u64 * mask)
150 {
151         struct smfs_super_info * smb = S2SMI(sb);
152         struct obd_device * obd = smb->smsi_exp->exp_obd;
153         struct audit_priv * priv = NULL;
154         
155         ENTRY;
156         
157         if (!SMFS_IS(smb->plg_flags, SMFS_PLG_AUDIT))
158                 RETURN(-EINVAL);
159         
160         priv = smfs_get_plg_priv(S2SMI(sb), SMFS_PLG_AUDIT);
161               
162         /* omit __iopen__ dir */
163         if (parent->i_ino == SMFS_IOPEN_INO)
164                 RETURN(-ENOENT);
165         
166         if (!priv)
167                 RETURN(-ENOENT);
168         
169         if (IS_AUDIT(priv->a_mask)) {
170                 /* no audit for directories on OSS */
171                 if (inode && S_ISDIR(inode->i_mode) &&
172                     !strcmp(obd->obd_type->typ_name, OBD_FILTER_DEVICENAME))
173                         RETURN(-EINVAL);
174                 (*mask) = priv->a_mask;
175                 RETURN(0);
176         }
177         /* get inode audit EA */
178         smfs_get_inode_audit(parent, mask);
179         /* check if parent has audit */
180         if (IS_AUDIT(*mask))
181                 RETURN(0);
182         
183         if (!inode)
184                 RETURN(-ENOENT);
185         
186         smfs_get_inode_audit(inode, mask);
187         if (IS_AUDIT(*mask))
188                 RETURN(0);
189
190         RETURN(-ENODATA);
191 }
192
193 int smfs_audit_check(struct inode * parent, hook_op hook, int ret,
194                      struct audit_priv * priv, void * msg)
195 {
196         audit_op code;
197         __u64 mask = 0;
198         int rc = 0;
199         
200         ENTRY;
201
202         if (hook == HOOK_SPECIAL) { 
203                 struct audit_info * info = msg;
204                 code = info->m.code;
205         }
206         else
207                 code = hook2audit(hook);
208
209         rc = smfs_get_audit(parent->i_sb, parent,
210                             get_inode_from_hook(hook, msg),
211                             &mask);
212         if (rc < 0)
213                 RETURN(0);
214         //should only failures be audited?
215         if (ret >= 0 && IS_AUDIT_OP(mask, AUDIT_FAIL))
216                 RETURN(0); 
217
218         //check audit mask
219         RETURN(IS_AUDIT_OP(mask, code));
220 }
221
222 static int smfs_set_fs_audit (struct super_block * sb, __u64 *mask)
223 {
224         struct smfs_super_info * smb = S2SMI(sb);
225         struct fsfilt_operations * fsfilt = smb->sm_fsfilt;
226         int rc = 0;
227         loff_t off = 0;
228         struct file * f = NULL;
229         struct audit_priv *priv;
230         struct lvfs_run_ctxt * ctxt, saved;
231         ENTRY;
232         
233         ctxt = &smb->smsi_exp->exp_obd->obd_lvfs_ctxt;
234         
235         priv = smfs_get_plg_priv(smb, SMFS_PLG_AUDIT);
236         if(!priv) {
237                 CERROR("Audit is not initialized, use mountoptions 'audit'\n");
238                 RETURN(-EINVAL);
239         }
240         
241         push_ctxt(&saved, ctxt, NULL);
242
243         f = filp_open(AUDIT_ATTR_FILE, O_RDWR|O_CREAT, 0600);
244         if (IS_ERR(f)) {
245                 CERROR("cannot get audit_setting file\n");
246                 rc = -EINVAL;
247                 goto exit;
248         }
249                 
250         rc = fsfilt->fs_write_record(f, mask, sizeof(*mask), &off, 1);
251         if (rc) {
252                 CERROR("error writting audit setting: rc = %d\n", rc);
253                 goto exit;
254         }
255         
256         priv->a_mask = (*mask);
257         
258 exit:
259         if (f)
260                 filp_close(f, 0);
261
262         pop_ctxt(&saved, ctxt, NULL);
263
264         RETURN (rc);
265 }
266
267 //set audit attributes for directory/file
268 int smfs_set_audit(struct super_block * sb, struct inode * inode,
269                    __u64 * mask)
270 {
271         void * handle = NULL;
272         struct fsfilt_operations * fsfilt = S2SMI(sb)->sm_fsfilt;
273         struct smfs_inode_info *smi = NULL;
274         int rc = 0;
275         
276         ENTRY;
277         
278         if (IS_AUDIT_OP((*mask), AUDIT_FS))
279                 return smfs_set_fs_audit(sb, mask);
280
281         LASSERT(inode);
282         smi = I2SMI(inode);
283         handle = fsfilt->fs_start(inode, FSFILT_OP_SETATTR, NULL, 0);
284         if (IS_ERR(handle))
285                 RETURN(PTR_ERR(handle));
286         
287         if (fsfilt->fs_set_xattr)
288                 rc = fsfilt->fs_set_xattr(inode, handle, AUDIT_ATTR_EA,
289                                           mask, sizeof(*mask));
290         /* save audit EA in inode_info */
291         if (rc >= 0) {
292                 smi->au_info.au_mask = *mask;
293                 smi->au_info.au_valid = 1;
294         }
295         
296         fsfilt->fs_commit(inode->i_sb, inode, handle, 1);
297         RETURN(rc);
298                                 
299 }
300
301 static int smfs_audit_post_op(hook_op code, struct inode * inode, void * msg,
302                               int ret, void * arg)
303 {
304         int rc = 0, len;
305         char * buffer = NULL;
306         struct audit_record * rec = NULL;
307         struct llog_rec_hdr * llh;
308         struct timeval cur_time;
309         struct audit_priv * priv = arg;
310         audit_get_op * handler = priv->audit_get_record;
311
312         //check that we are in lustre ctxt
313         if (!SMFS_IS(I2SMI(inode)->smi_flags, SMFS_PLG_AUDIT))
314                 return 0;
315         
316         if (!handler || !handler[code])
317                 return 0;
318         
319         if (smfs_audit_check(inode, code, ret, priv, msg) == 0)
320                 return 0;
321
322         ENTRY;
323         
324         do_gettimeofday(&cur_time);
325
326         OBD_ALLOC(buffer, PAGE_SIZE);
327         if (!buffer)
328                 RETURN(-ENOMEM);
329         
330         llh = (void*)buffer;
331         //fill common fields
332         rec = (void*)(buffer + sizeof(*llh));
333                
334         rec->result = ret;
335         rec->uid = current->uid;
336         rec->gid = current->gid;
337         rec->nid = current->user->nid;
338         rec->time = cur_time.tv_sec * USEC_PER_SEC + cur_time.tv_usec;
339         
340         len = handler[code](inode, msg, priv, (char*)rec,
341                                            &llh->lrh_type);
342         
343         LASSERT(llh->lrh_type == SMFS_AUDIT_GEN_REC ||
344                 llh->lrh_type == SMFS_AUDIT_NAME_REC);
345
346         llh->lrh_len = size_round(len);
347
348         rc = llog_cat_add_rec(priv->audit_ctxt->loc_handle, llh, NULL,
349                               (void*)rec, NULL, NULL); 
350         if (rc != 0) {
351                 CERROR("Error adding audit record: %d\n", rc);
352                 rc= -EINVAL;
353         } else {
354                 audit_notify(priv->audit_ctxt->loc_handle, priv->au_id2name);
355         }
356         
357         OBD_FREE(buffer, PAGE_SIZE);
358         
359         RETURN(rc);
360 }
361
362 /* Helpers */
363 static int smfs_trans_audit (struct super_block *sb, void *arg,
364                            struct audit_priv * priv)
365 {
366         int size = 1; //one record in log per operation.
367
368         return size;
369 }
370
371 extern int mds_alloc_inode_ids(struct obd_device *, struct inode *,
372                         void *, struct lustre_id *, struct lustre_id *);
373
374 static int smfs_start_audit(struct super_block *sb, void *arg,
375                           struct audit_priv * audit_p)
376 {
377         struct smfs_super_info * smb = S2SMI(sb);
378         struct fsfilt_operations * fsfilt = smb->sm_fsfilt;
379         struct obd_device *obd = arg;
380         struct file * f;
381         int rc = 0;
382
383         ENTRY;
384
385         //is plugin already activated
386         if (SMFS_IS(smb->plg_flags, SMFS_PLG_AUDIT))
387                 RETURN(0);
388         
389         rc = audit_start_transferd();
390         if (rc) {
391                 CERROR("can't start audit transfer daemon. rc:%d\n", rc);
392                 RETURN(rc);
393         }
394         
395         if (obd && obd->obd_type && obd->obd_type->typ_name) {
396                 if (!strcmp(obd->obd_type->typ_name, "mds")) {
397                         CDEBUG(D_INODE, "Setup MDS audit handler\n");
398                         audit_mds_setup(obd, sb, audit_p);
399                 }
400                 else if (!strcmp(obd->obd_type->typ_name, "obdfilter")) {
401                         CDEBUG(D_INODE, "Setup OST audit handler\n");
402                         audit_ost_setup(obd, sb, audit_p);
403                 }
404                 else {
405                         CDEBUG(D_INODE, "Unknown obd type %s\n",
406                                obd->obd_type->typ_name);       
407                         RETURN(0);
408                 }
409         }
410         //read fs audit settings if any
411         audit_p->a_mask = AUDIT_OFF;
412
413         f = filp_open(AUDIT_ATTR_FILE, O_RDONLY, 0644);
414         if (!IS_ERR(f)) {
415                 loff_t off = 0;
416                 rc = fsfilt->fs_read_record(f, &audit_p->a_mask, 
417                                         sizeof(audit_p->a_mask), &off);
418                 if (rc) {
419                         CERROR("error reading audit setting: rc = %d\n", rc);
420                 }
421                 filp_close(f, 0);
422         }
423         
424         SMFS_SET(smb->plg_flags, SMFS_PLG_AUDIT);
425
426         RETURN(0);
427 }
428
429 int smfs_stop_audit(struct super_block *sb, void *arg,
430                   struct audit_priv * audit_p)
431 {
432         struct smfs_super_info * smb = S2SMI(sb);
433         struct llog_ctxt *ctxt = audit_p->audit_ctxt;
434         ENTRY;
435
436         if (!SMFS_IS(smb->plg_flags, SMFS_PLG_AUDIT))
437                 RETURN(0);
438
439         audit_stop_transferd();
440
441         SMFS_CLEAR(smb->plg_flags, SMFS_PLG_AUDIT);
442
443         if (ctxt->loc_llogs)
444                 ctxt->loc_llogs->llog_ctxt[LLOG_AUDIT_ORIG_CTXT] = NULL;
445
446         llog_catalog_cleanup(ctxt);
447         OBD_FREE(ctxt, sizeof(*ctxt));
448         audit_p->audit_ctxt = NULL;
449         
450         RETURN(0);
451 }
452
453 int smfs_audit_set_info(struct super_block *sb, void *arg,
454                         struct audit_priv *priv) {
455         struct plg_info_msg * msg = arg;
456         if (KEY_IS(msg->key, "id2name")) {
457                 priv->au_id2name = msg->val;
458         }
459                          
460         return 0;
461 }
462
463 typedef int (*audit_helper)(struct super_block * sb, void *msg, struct audit_priv *);
464 static audit_helper smfs_audit_helpers[PLG_HELPER_MAX] = {
465         [PLG_START]      smfs_start_audit,
466         [PLG_STOP]       smfs_stop_audit,
467         [PLG_TRANS_SIZE] smfs_trans_audit,
468         [PLG_TEST_INODE] NULL,
469         [PLG_SET_INODE]  NULL,
470         [PLG_SET_INFO]   smfs_audit_set_info,
471 };
472
473 static int smfs_audit_help_op(int code, struct super_block * sb,
474                             void * arg, void * priv)
475 {
476         int rc = 0;
477         
478         if (smfs_audit_helpers[code])
479                 rc = smfs_audit_helpers[code](sb, arg, (struct audit_priv *) priv);
480         return rc;
481 }
482
483 static int smfs_exit_audit(struct super_block *sb, 
484                            void * arg)
485 {
486         struct audit_priv * priv = arg;
487         struct smfs_plugin * plg;
488         ENTRY;
489
490         plg = smfs_deregister_plugin(sb, SMFS_PLG_AUDIT);
491         if (plg)
492                 OBD_FREE(plg, sizeof(*plg));
493         else
494                 CERROR("Cannot find AUDIT plugin while unregistering\n");
495         
496         if (priv)
497                 OBD_FREE(priv, sizeof(*priv));
498         
499         RETURN(0);
500 }
501
502 int smfs_init_audit(struct super_block *sb)
503 {
504         int rc = 0;
505         struct audit_priv * priv = NULL;
506         struct smfs_plugin * plg = NULL;
507
508         ENTRY;
509         
510         OBD_ALLOC(plg, sizeof(*plg));
511         if (!plg) {
512                 rc = -ENOMEM;
513                 goto exit;
514         }
515         
516         plg->plg_type = SMFS_PLG_AUDIT;
517         plg->plg_post_op = &smfs_audit_post_op;
518         plg->plg_helper = &smfs_audit_help_op;
519         plg->plg_exit = &smfs_exit_audit;
520
521         OBD_ALLOC(priv, sizeof(*priv));
522         if (!priv) {
523                 rc = -ENOMEM;
524                 goto exit;
525         }
526
527         plg->plg_private = priv;
528         rc = smfs_register_plugin(sb, plg);
529         if (!rc)
530                 RETURN(0);
531 exit:
532         if (priv)
533                 OBD_FREE(priv, sizeof(*priv));
534         
535         if (plg)
536                 OBD_FREE(plg, sizeof(*plg));
537
538         RETURN(rc);
539
540 }
541
542 int audit_client_log(struct super_block * sb, struct audit_msg * msg)
543 {
544         struct smfs_super_info * smb = S2SMI(sb);
545         char *buffer = NULL, *pbuf = NULL;
546         struct audit_record * rec = NULL;
547         struct llog_rec_hdr * llh;
548         struct llog_handle * ll_handle = NULL;
549         int len = 0, rc = 0;
550         struct timeval cur_time;
551         //char name[32];
552         struct audit_priv * priv;
553         
554         ENTRY;
555         
556         do_gettimeofday(&cur_time);
557         
558         priv = smfs_get_plg_priv(smb, SMFS_PLG_AUDIT);
559         if (!priv)
560                 RETURN(-EINVAL);
561         
562         ll_handle = priv->audit_ctxt->loc_handle;
563         
564         OBD_ALLOC(buffer, PAGE_SIZE);
565         if (!buffer)
566                 RETURN(-ENOMEM);
567         
568         llh = (void*)buffer;
569         llh->lrh_type = SMFS_AUDIT_GEN_REC;
570
571         //fill common fields
572         rec = (void*)buffer + sizeof(*llh);
573         rec->opcode = msg->code;
574         rec->result = msg->result;
575         rec->uid = msg->uid;
576         rec->gid = msg->gid;
577         rec->nid = msg->nid;
578         rec->time = cur_time.tv_sec * USEC_PER_SEC + cur_time.tv_usec;
579         len = sizeof(*rec);
580         pbuf = (char*)rec + len;
581
582         CDEBUG(D_VFSTRACE, "AUDITLOG:"DLID4"\n", OLID4(&msg->id));
583         /* check id is valid */
584         LASSERT(id_ino(&msg->id));
585         LASSERT(id_fid(&msg->id));
586         //LASSERT(id_type(&msg->id) & S_IFMT);
587
588         switch (msg->code) {
589                 case AUDIT_READ:    
590                 case AUDIT_WRITE:
591                 case AUDIT_MMAP:
592                 case AUDIT_OPEN:
593                 case AUDIT_STAT:
594                         len += audit_rec_from_id(&pbuf, &msg->id);
595                         break;
596                 default:
597                         CERROR("Unknown code %i in audit_msg\n", msg->code);
598         }
599         
600         llh->lrh_len = size_round(len);
601
602         rc = llog_cat_add_rec(ll_handle, llh, NULL, (void*)rec, NULL, NULL);
603         if (rc != 0) {
604                 CERROR("Error adding audit client record: %d\n", rc);
605                 rc= -EINVAL;
606         } else {
607                 audit_notify(ll_handle, priv->au_id2name);
608         }
609         
610         OBD_FREE(buffer, PAGE_SIZE);
611         return rc;
612 }
613