Audit: unify the printk of an skb when auditd not around
Eric Paris
maintainerPATCH 1/7 Audit: unify the printk of an skb when auditd not around
Remove code duplication of skb printk when auditd is not around in userspace to deal with this message.
---
kernel/audit.c | 39 ++++++++++++++++++++++----------------- 1 files changed, 22 insertions(+), 17 deletions(-)
kernel/audit.c +22 -17
@@ -375,6 +375,25 @@ static void audit_hold_skb(struct sk_buff *skb)kfree_skb (skb); } +/* +* For one reason or another this nlh isn't getting delivered to the userspace +* audit daemon, just send it to printk. +*/ +static void audit_printk_skb (struct sk_buff *skb) +{ +struct nlmsghdr *nlh =nlmsg_hdr (skb); +char *data =NLMSG_DATA (nlh); + +if (nlh->nlmsg_type !=AUDIT_EOE ) { +if (printk_ratelimit ()) +printk (KERN_NOTICE "type=%d %s\n" , nlh->nlmsg_type, data); +else +audit_log_lost ("printk limit exceeded\n" ); + } + +audit_hold_skb (skb); +} +static void kauditd_send_skb (struct sk_buff *skb) {int err;
@@ -427,14 +446,8 @@ static int kauditd_thread(void *dummy)if (skb) {if (audit_pid)kauditd_send_skb (skb); -else { -if (printk_ratelimit ()) -printk (KERN_NOTICE "%s\n" , skb->data +NLMSG_SPACE (0 )); -else -audit_log_lost ("printk limit exceeded\n" ); - -audit_hold_skb (skb); - } +else +audit_printk_skb (skb); }else {DECLARE_WAITQUEUE (wait, current);set_current_state (TASK_INTERRUPTIBLE );
@@ -1478,15 +1491,7 @@ void audit_log_end(struct audit_buffer *ab)skb_queue_tail (&audit_skb_queue, skb);wake_up_interruptible (&kauditd_wait); }else { -if (nlh->nlmsg_type !=AUDIT_EOE ) { -if (printk_ratelimit ()) { -printk (KERN_NOTICE "type=%d %s\n" , - nlh->nlmsg_type, - skb->data +NLMSG_SPACE (0 )); - }else -audit_log_lost ("printk limit exceeded\n" ); - } -audit_hold_skb (skb); +audit_printk_skb (skb); } ab->skb =NULL ; }
Eric Paris
maintainerPATCH 2/7 Audit: cleanup netlink mesg handling
The audit handling of netlink messages is all over the place. Clean things up, use predetermined macros, generally make it more readable.
---
kernel/audit.c | 30 +++++++++++++++--------------- 1 files changed, 15 insertions(+), 15 deletions(-)
kernel/audit.c +15 -15
@@ -528,22 +528,20 @@ struct sk_buff *audit_make_reply(int pid, int seq, int type, int done, {struct sk_buff *skb;struct nlmsghdr *nlh; -int len =NLMSG_SPACE (size);void *data;int flags = multi ?NLM_F_MULTI :0 ;int t = done ?NLMSG_DONE : type; - skb =alloc_skb (len,GFP_KERNEL ); + skb =nlmsg_new (size,GFP_KERNEL );if (!skb)return NULL ; - nlh =NLMSG_PUT (skb, pid, seq, t, size); - nlh->nlmsg_flags = flags; - data =NLMSG_DATA (nlh); + nlh =NLMSG_NEW (skb, pid, seq, t, size, flags); + data =NLMSG_DATA (nlh);memcpy (data, payload, size);return skb; -nlmsg_failure:/* Used by NLMSG_PUT */ +nlmsg_failure:/* Used by NLMSG_NEW */ if (skb)kfree_skb (skb);return NULL ;
@@ -1083,18 +1081,20 @@ static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, goto err; } - ab->skb =alloc_skb (AUDIT_BUFSIZ , gfp_mask); -if (!ab->skb) - goto err; - ab->ctx = ctx; ab->gfp_mask = gfp_mask; - nlh = (struct nlmsghdr *)skb_put (ab->skb,NLMSG_SPACE (0 )); - nlh->nlmsg_type = type; - nlh->nlmsg_flags =0 ; - nlh->nlmsg_pid =0 ; - nlh->nlmsg_seq =0 ; + + ab->skb =nlmsg_new (AUDIT_BUFSIZ , gfp_mask); +if (!ab->skb) + goto nlmsg_failure; + + nlh =NLMSG_NEW (ab->skb,0 ,0 , type,0 ,0 ); +return ab; + +nlmsg_failure:/* Used by NLMSG_NEW */ +kfree_skb (ab->skb); + ab->skb =NULL ; err:audit_buffer_free (ab);return NULL ;
Eric Paris
maintainerPATCH 3/7 Audit: clean up audit_receive_skb
In reply to Eric Parisaudit_receive_skb is hard to clearly parse what it is doing to the netlink message. Clean the function up so it is easy and clear to see what is going on.
---
kernel/audit.c | 35 ++++++++++++++++++----------------- 1 files changed, 18 insertions(+), 17 deletions(-)
kernel/audit.c +18 -17
@@ -937,28 +937,29 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) }/* -* Get message from skb (based on rtnetlink_rcv_skb). Each message is -* processed by audit_receive_msg. Malformed skbs with wrong length are -* discarded silently. +* Get message from skb. Each message is processed by audit_receive_msg. +* Malformed skbs with wrong length are discarded silently. */ static void audit_receive_skb (struct sk_buff *skb) { -int err; -struct nlmsghdr *nlh; -u32 rlen; +struct nlmsghdr *nlh; +/* +* len MUST be signed for NLMSG_NEXT to be able to dec it below 0 +* if the nlmsg_len was not aligned +*/ +int len; +int err; -while (skb->len >=NLMSG_SPACE (0 )) { - nlh =nlmsg_hdr (skb); -if (nlh->nlmsg_len <sizeof (*nlh) || skb->len < nlh->nlmsg_len) -return ; - rlen =NLMSG_ALIGN (nlh->nlmsg_len); -if (rlen > skb->len) - rlen = skb->len; -if ((err =audit_receive_msg (skb, nlh))) { + nlh =nlmsg_hdr (skb); + len = skb->len; + +while (NLMSG_OK (nlh, len)) { + err =audit_receive_msg (skb, nlh); +/* if err or if this message says it wants a response */ +if (err || (nlh->nlmsg_flags &NLM_F_ACK ))netlink_ack (skb, nlh, err); - }else if (nlh->nlmsg_flags &NLM_F_ACK ) -netlink_ack (skb, nlh,0 ); -skb_pull (skb, rlen); + + nlh =NLMSG_NEXT (nlh, len); } }
Eric Paris
maintainerPATCH 5/7 audit: seperate audit inode watches into a subfile
In reply to Eric ParisIn preparation for converting audit to use fsnotify instead of inotify we seperate the inode watching code into it's own file. This is similar to how the audit tree watching code is already seperated into audit_tree.c
---
kernel/audit/Makefile | 2 kernel/audit/audit.c | 16 - kernel/audit/audit.h | 39 ++- kernel/audit/audit_watch.c | 544 ++++++++++++++++++++++++++++++++++++++++++++ kernel/audit/auditfilter.c | 491 +--------------------------------------- kernel/audit/auditsc.c | 6 6 files changed, 582 insertions(+), 516 deletions(-) create mode 100644 kernel/audit/audit_watch.c
kernel/audit/Makefile +1 -1
@@ -3,5 +3,5 @@ # obj-$(CONFIG_AUDIT) += audit.o auditfilter.o -obj-$(CONFIG_AUDITSYSCALL) += auditsc.o +obj-$(CONFIG_AUDITSYSCALL) += auditsc.o audit_watch.o obj-$(CONFIG_AUDIT_TREE) += audit_tree.o
kernel/audit/audit.c +0 -16
@@ -115,9 +115,6 @@ static atomic_t audit_lost = ATOMIC_INIT(0);/* The netlink socket. */ static struct sock *audit_sock; -/* Inotify handle. */ -struct inotify_handle *audit_ih; -/* Hash for inode-based rules */ struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS ];
@@ -971,13 +968,6 @@ static void audit_receive(struct sk_buff *skb)mutex_unlock (&audit_cmd_mutex); } -#ifdef CONFIG_AUDITSYSCALL -static const struct inotify_operations audit_inotify_ops = { - .handle_event = audit_handle_ievent, - .destroy_watch = audit_free_parent, -}; -#endif -/* Initialize audit support at boot time. */ static int __init audit_init (void ) {
@@ -1003,12 +993,6 @@ static int __init audit_init(void)audit_log (NULL ,GFP_KERNEL ,AUDIT_KERNEL ,"initialized" ); -#ifdef CONFIG_AUDITSYSCALL - audit_ih =inotify_init (&audit_inotify_ops); -if (IS_ERR (audit_ih)) -audit_panic ("cannot initialize inotify handle" ); -#endif -for (i =0 ; i <AUDIT_INODE_BUCKETS ; i++)INIT_LIST_HEAD (&audit_inode_hash[i]);
kernel/audit/audit.h +20 -19
@@ -53,18 +53,7 @@ enum audit_state { };/* Rule lists */ -struct audit_parent ; - -struct audit_watch { -atomic_t count;/* reference count */ -char *path;/* insertion path */ -dev_t dev;/* associated superblock device */ -unsigned long ino;/* associated inode number */ -struct audit_parent *parent;/* associated parent */ -struct list_head wlist;/* entry in parent->watches list */ -struct list_head rules;/* associated rules */ -}; - +struct audit_watch ;struct audit_tree ;struct audit_chunk ;
@@ -108,19 +97,31 @@ struct audit_netlink_list {int audit_send_list (void *); -struct inotify_watch ; -/* Inotify handle */ -extern struct inotify_handle *audit_ih; - -extern void audit_free_parent (struct inotify_watch *); -extern void audit_handle_ievent (struct inotify_watch *,u32 ,u32 ,u32 , -const char *,struct inode *);extern int selinux_audit_rule_update (void );extern struct mutex audit_filter_mutex;extern void audit_free_rule_rcu (struct rcu_head *);extern struct list_head audit_filter_list[]; +/* audit watch functions */ +extern unsigned long audit_watch_inode (struct audit_watch *watch); +extern dev_t audit_watch_dev (struct audit_watch *watch); +extern void audit_put_watch (struct audit_watch *watch); +extern void audit_get_watch (struct audit_watch *watch); +extern int audit_to_watch (struct audit_krule *krule,char *path,int len,u32 op); +extern int audit_get_nd (char *path,struct nameidata **ndp,struct nameidata **ndw); +extern void audit_put_nd (struct nameidata *ndp,struct nameidata *ndw); +extern int audit_add_watch (struct audit_krule *krule,struct nameidata *ndp, +struct nameidata *ndw); +extern void audit_remove_watch (struct audit_watch *watch); +extern void audit_remove_watch_rule (struct audit_krule *krule,struct list_head *list); +extern void audit_inotify_unregister (struct list_head *in_list); +extern char *audit_watch_path (struct audit_watch *watch); +extern struct list_head *audit_watch_rules (struct audit_watch *watch); + +extern struct audit_entry *audit_dupe_rule (struct audit_krule *old, +struct audit_watch *watch); +#ifdef CONFIG_AUDIT_TREE extern struct audit_chunk *audit_tree_lookup (const struct inode *);extern void audit_put_chunk (struct audit_chunk *);
kernel/audit/audit_watch.c new file +544 -0
544 lines not on this page · raw message
kernel/audit/auditfilter.c +14 -477
596 lines not on this page · raw message
kernel/audit/auditsc.c +3 -3
@@ -548,9 +548,9 @@ static int audit_filter_rules(struct task_struct *tsk, }break ;case AUDIT_WATCH : -if (name && rule->watch->ino != (unsigned long )-1 ) - result = (name->dev == rule->watch->dev && - name->ino == rule->watch->ino); +if (name &&audit_watch_inode (rule->watch) != (unsigned long )-1 ) + result = (name->dev ==audit_watch_dev (rule->watch) && + name->ino ==audit_watch_inode (rule->watch));break ;case AUDIT_DIR :if (ctx)
Eric Paris
maintainerPATCH 6/7 Audit: move audit_get_nd completely into audit_watch
In reply to Eric Parisaudit_get_nd() is only used by audit_watch and could be more cleanly implemented by having the audit watch functions call it when needed rather than making the generic audit rule parsing code deal with those objects.
---
kernel/audit/audit.h | 5 +---- kernel/audit/audit_watch.c | 27 ++++++++++++++++++++------- kernel/audit/auditfilter.c | 15 ++------------- 3 files changed, 23 insertions(+), 24 deletions(-)
kernel/audit/audit.h +1 -4
@@ -109,10 +109,7 @@ extern dev_t audit_watch_dev(struct audit_watch *watch);extern void audit_put_watch (struct audit_watch *watch);extern void audit_get_watch (struct audit_watch *watch);extern int audit_to_watch (struct audit_krule *krule,char *path,int len,u32 op); -extern int audit_get_nd (char *path,struct nameidata **ndp,struct nameidata **ndw); -extern void audit_put_nd (struct nameidata *ndp,struct nameidata *ndw); -extern int audit_add_watch (struct audit_krule *krule,struct nameidata *ndp, -struct nameidata *ndw); +extern int audit_add_watch (struct audit_krule *krule);extern void audit_remove_watch (struct audit_watch *watch);extern void audit_remove_watch_rule (struct audit_krule *krule,struct list_head *list);extern void audit_inotify_unregister (struct list_head *in_list);
kernel/audit/audit_watch.c +20 -7
@@ -355,7 +355,7 @@ void audit_inotify_unregister(struct list_head *in_list) }/* Get path information necessary for adding watches. */ -int audit_get_nd (char *path,struct nameidata **ndp,struct nameidata **ndw) +static int audit_get_nd (char *path,struct nameidata **ndp,struct nameidata **ndw) {struct nameidata *ndparent, *ndwatch;int err;
@@ -390,7 +390,7 @@ int audit_get_nd(char *path, struct nameidata **ndp, struct nameidata **ndw) }/* Release resources used for watch path information. */ -void audit_put_nd (struct nameidata *ndp,struct nameidata *ndw) +static void audit_put_nd (struct nameidata *ndp,struct nameidata *ndw) {if (ndp) {path_put (&ndp->path);
@@ -436,14 +436,24 @@ static void audit_add_to_parent(struct audit_krule *krule,/* Find a matching watch entry, or add this one. * Caller must hold audit_filter_mutex. */ -int audit_add_watch (struct audit_krule *krule,struct nameidata *ndp, -struct nameidata *ndw) +int audit_add_watch (struct audit_krule *krule) {struct audit_watch *watch = krule->watch;struct inotify_watch *i_watch;struct audit_parent *parent; +struct nameidata *ndp =NULL , *ndw =NULL ;int ret =0 ; +mutex_unlock (&audit_filter_mutex); + +/* Avoid calling path_lookup under audit_filter_mutex. */ + ret =audit_get_nd (watch->path, &ndp, &ndw); +if (ret) { +/* caller expects mutex locked */ +mutex_lock (&audit_filter_mutex); + goto error; + } +/* update watch filter fields */ if (ndw) { watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
@@ -455,15 +465,14 @@ int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,* inotify watch is found, inotify_find_watch() grabs a reference before * returning. */ -mutex_unlock (&audit_filter_mutex); -if (inotify_find_watch (audit_ih, ndp->path.dentry->d_inode, &i_watch) <0 ) { parent =audit_init_parent (ndp);if (IS_ERR (parent)) {/* caller expects mutex locked */ mutex_lock (&audit_filter_mutex); -return PTR_ERR (parent); + ret =PTR_ERR (parent); + goto error; } }else parent = container_of(i_watch, struct audit_parent, wdata);
@@ -478,7 +487,11 @@ int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,/* match get in audit_init_parent or inotify_find_watch */ put_inotify_watch (&parent->wdata); + +error: +audit_put_nd (ndp, ndw);/* NULL args OK */ return ret; + }void audit_remove_watch_rule (struct audit_krule *krule,struct list_head *list)
kernel/audit/auditfilter.c +2 -13
@@ -864,7 +864,6 @@ static inline int audit_add_rule(struct audit_entry *entry)struct audit_entry *e;struct audit_watch *watch = entry->rule.watch;struct audit_tree *tree = entry->rule.tree; -struct nameidata *ndp =NULL , *ndw =NULL ;struct list_head *list;int h, err;#ifdef CONFIG_AUDITSYSCALL
@@ -878,8 +877,8 @@ static inline int audit_add_rule(struct audit_entry *entry)mutex_lock (&audit_filter_mutex); e =audit_find_rule (entry, &list); -mutex_unlock (&audit_filter_mutex);if (e) { +mutex_unlock (&audit_filter_mutex); err = -EEXIST ;/* normally audit_add_tree_rule() will free it on failure */ if (tree)
@@ -887,17 +886,9 @@ static inline int audit_add_rule(struct audit_entry *entry) goto error; } -/* Avoid calling path_lookup under audit_filter_mutex. */ -if (watch) { - err =audit_get_nd (audit_watch_path (watch), &ndp, &ndw); -if (err) - goto error; - } - -mutex_lock (&audit_filter_mutex);if (watch) {/* audit_filter_mutex is dropped and re-taken during this call */ - err =audit_add_watch (&entry->rule, ndp, ndw); + err =audit_add_watch (&entry->rule);if (err) {mutex_unlock (&audit_filter_mutex); goto error;
@@ -942,11 +933,9 @@ static inline int audit_add_rule(struct audit_entry *entry)#endif mutex_unlock (&audit_filter_mutex); -audit_put_nd (ndp, ndw);/* NULL args OK */ return 0 ; error: -audit_put_nd (ndp, ndw);/* NULL args OK */ if (watch)audit_put_watch (watch);/* tmp watch, matches initial get */ return err;
Eric Paris
maintainerPATCH 7/7 audit: use kern_path rather than namei for watch creation
In reply to Eric Pariskernel watches jump through stupid allocation hoops to get the namei data for the path in question and it's parent. All useless crap since we just need to hold a struct path for a bit.
---
kernel/audit/audit_watch.c | 68 ++++++++++++++++---------------------------- 1 files changed, 25 insertions(+), 43 deletions(-)
kernel/audit/audit_watch.c +25 -43
@@ -130,7 +130,7 @@ dev_t audit_watch_dev(struct audit_watch *watch) }/* Initialize a parent watch entry. */ -static struct audit_parent *audit_init_parent (struct nameidata *ndp) +static struct audit_parent *audit_init_parent (struct path *parent_path) {struct audit_parent *parent;s32 wd;
@@ -146,7 +146,7 @@ static struct audit_parent *audit_init_parent(struct nameidata *ndp)/* grab a ref so inotify watch hangs around until we take audit_filter_mutex */ get_inotify_watch (&parent->wdata); wd =inotify_add_watch (audit_ih, &parent->wdata, - ndp->path.dentry->d_inode,AUDIT_IN_WATCH ); + parent_path->dentry->d_inode,AUDIT_IN_WATCH );if (wd <0 ) {audit_free_parent (&parent->wdata);return ERR_PTR (wd);
@@ -355,51 +355,33 @@ void audit_inotify_unregister(struct list_head *in_list) }/* Get path information necessary for adding watches. */ -static int audit_get_nd (char *path,struct nameidata **ndp,struct nameidata **ndw) +static int audit_get_path (const char *path,struct path *parent_path, +struct path *watch_path) { -struct nameidata *ndparent, *ndwatch;int err; - ndparent =kmalloc (sizeof (*ndparent),GFP_KERNEL ); -if (unlikely (!ndparent)) -return -ENOMEM ; + parent_path->dentry =NULL ; + watch_path->dentry =NULL ; - ndwatch =kmalloc (sizeof (*ndwatch),GFP_KERNEL ); -if (unlikely (!ndwatch)) { -kfree (ndparent); -return -ENOMEM ; - } - - err =path_lookup (path,LOOKUP_PARENT , ndparent); -if (err) { -kfree (ndparent); -kfree (ndwatch); + err =kern_path (path,LOOKUP_PARENT , parent_path); +if (err)return err; - } - err =path_lookup (path,0 , ndwatch); -if (err) { -kfree (ndwatch); - ndwatch =NULL ; - } - - *ndp = ndparent; - *ndw = ndwatch; +/* watch_path might not exist, we don't care about errors here. */ +kern_path (path,0 , watch_path);return 0 ; }/* Release resources used for watch path information. */ -static void audit_put_nd (struct nameidata *ndp,struct nameidata *ndw) +static void audit_put_path (struct path *parent_path,struct path *watch_path) { -if (ndp) { -path_put (&ndp->path); -kfree (ndp); - } -if (ndw) { -path_put (&ndw->path); -kfree (ndw); - } +/* there should always be a valid parent path */ +if (parent_path->dentry) +path_put (parent_path); +/* creating a watch on a non-existant file may not have a watch_path->dentry */ +if (watch_path->dentry) +path_put (watch_path); }/* Associate the given rule with an existing parent inotify_watch.
@@ -441,13 +423,13 @@ int audit_add_watch(struct audit_krule *krule)struct audit_watch *watch = krule->watch;struct inotify_watch *i_watch;struct audit_parent *parent; -struct nameidata *ndp =NULL , *ndw =NULL ; +struct path parent_path, watch_path;int ret =0 ;mutex_unlock (&audit_filter_mutex);/* Avoid calling path_lookup under audit_filter_mutex. */ - ret =audit_get_nd (watch->path, &ndp, &ndw); + ret =audit_get_path (watch->path, &parent_path, &watch_path);if (ret) {/* caller expects mutex locked */ mutex_lock (&audit_filter_mutex);
@@ -455,9 +437,9 @@ int audit_add_watch(struct audit_krule *krule) }/* update watch filter fields */ -if (ndw) { - watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev; - watch->ino = ndw->path.dentry->d_inode->i_ino; +if (watch_path.dentry) { + watch->dev = watch_path.dentry->d_inode->i_sb->s_dev; + watch->ino = watch_path.dentry->d_inode->i_ino; }/* The audit_filter_mutex must not be held during inotify calls because
@@ -465,9 +447,9 @@ int audit_add_watch(struct audit_krule *krule)* inotify watch is found, inotify_find_watch() grabs a reference before * returning. */ -if (inotify_find_watch (audit_ih, ndp->path.dentry->d_inode, +if (inotify_find_watch (audit_ih, parent_path.dentry->d_inode, &i_watch) <0 ) { - parent =audit_init_parent (ndp); + parent =audit_init_parent (&parent_path);if (IS_ERR (parent)) {/* caller expects mutex locked */ mutex_lock (&audit_filter_mutex);
@@ -489,7 +471,7 @@ int audit_add_watch(struct audit_krule *krule)put_inotify_watch (&parent->wdata); error: -audit_put_nd (ndp, ndw);/* NULL args OK */ +audit_put_path (&parent_path, &watch_path);return ret; }