s/retreiv/retriev/g
[safe/jmp/linux-2.6] / kernel / auditsc.c
index 20c7d85..d8a6850 100644 (file)
@@ -40,6 +40,8 @@
 #include <linux/personality.h>
 #include <linux/time.h>
 #include <linux/kthread.h>
+#include <linux/netlink.h>
+#include <linux/compiler.h>
 #include <asm/unistd.h>
 
 /* 0 = no checking
@@ -188,9 +190,36 @@ struct audit_entry {
 
 extern int audit_pid;
 
+/* Copy rule from user-space to kernel-space.  Called from 
+ * audit_add_rule during AUDIT_ADD. */
+static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
+{
+       int i;
+
+       if (s->action != AUDIT_NEVER
+           && s->action != AUDIT_POSSIBLE
+           && s->action != AUDIT_ALWAYS)
+               return -1;
+       if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
+               return -1;
+       if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
+               return -1;
+
+       d->flags        = s->flags;
+       d->action       = s->action;
+       d->field_count  = s->field_count;
+       for (i = 0; i < d->field_count; i++) {
+               d->fields[i] = s->fields[i];
+               d->values[i] = s->values[i];
+       }
+       for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
+       return 0;
+}
+
 /* Check to see if two rules are identical.  It is called from
+ * audit_add_rule during AUDIT_ADD and 
  * audit_del_rule during AUDIT_DEL. */
-static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
+static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
 {
        int i;
 
@@ -219,18 +248,37 @@ static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
 /* Note that audit_add_rule and audit_del_rule are called via
  * audit_receive() in audit.c, and are protected by
  * audit_netlink_sem. */
-static inline void audit_add_rule(struct audit_entry *entry,
+static inline int audit_add_rule(struct audit_rule *rule,
                                  struct list_head *list)
 {
+       struct audit_entry  *entry;
+
+       /* Do not use the _rcu iterator here, since this is the only
+        * addition routine. */
+       list_for_each_entry(entry, list, list) {
+               if (!audit_compare_rule(rule, &entry->rule)) {
+                       return -EEXIST;
+               }
+       }
+
+       if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
+               return -ENOMEM;
+       if (audit_copy_rule(&entry->rule, rule)) {
+               kfree(entry);
+               return -EINVAL;
+       }
+
        if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
                entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
                list_add_rcu(&entry->list, list);
        } else {
                list_add_tail_rcu(&entry->list, list);
        }
+
+       return 0;
 }
 
-static void audit_free_rule(struct rcu_head *head)
+static inline void audit_free_rule(struct rcu_head *head)
 {
        struct audit_entry *e = container_of(head, struct audit_entry, rcu);
        kfree(e);
@@ -256,32 +304,6 @@ static inline int audit_del_rule(struct audit_rule *rule,
        return -ENOENT;         /* No matching rule */
 }
 
-/* Copy rule from user-space to kernel-space.  Called during
- * AUDIT_ADD. */
-static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
-{
-       int i;
-
-       if (s->action != AUDIT_NEVER
-           && s->action != AUDIT_POSSIBLE
-           && s->action != AUDIT_ALWAYS)
-               return -1;
-       if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
-               return -1;
-       if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
-               return -1;
-
-       d->flags        = s->flags;
-       d->action       = s->action;
-       d->field_count  = s->field_count;
-       for (i = 0; i < d->field_count; i++) {
-               d->fields[i] = s->fields[i];
-               d->values[i] = s->values[i];
-       }
-       for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
-       return 0;
-}
-
 static int audit_list_rules(void *_dest)
 {
        int pid, seq;
@@ -311,7 +333,6 @@ static int audit_list_rules(void *_dest)
 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
                                                        uid_t loginuid)
 {
-       struct audit_entry *entry;
        struct task_struct *tsk;
        int *dest;
        int                err = 0;
@@ -338,16 +359,14 @@ int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
                }
                break;
        case AUDIT_ADD:
-               if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
-                       return -ENOMEM;
-               if (audit_copy_rule(&entry->rule, data)) {
-                       kfree(entry);
+               listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
+               if (listnr >= AUDIT_NR_FILTERS)
                        return -EINVAL;
-               }
-               listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
-               audit_add_rule(entry, &audit_filter_list[listnr]);
-               audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, 
-                               "auid=%u added an audit rule\n", loginuid);
+
+               err = audit_add_rule(data, &audit_filter_list[listnr]);
+               if (!err)
+                       audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
+                                 "auid=%u added an audit rule\n", loginuid);
                break;
        case AUDIT_DEL:
                listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
@@ -421,8 +440,12 @@ static int audit_filter_rules(struct task_struct *tsk,
                                result = (ctx->return_code == value);
                        break;
                case AUDIT_SUCCESS:
-                       if (ctx && ctx->return_valid)
-                               result = (ctx->return_valid == AUDITSC_SUCCESS);
+                       if (ctx && ctx->return_valid) {
+                               if (value)
+                                       result = (ctx->return_valid == AUDITSC_SUCCESS);
+                               else
+                                       result = (ctx->return_valid == AUDITSC_FAILURE);
+                       }
                        break;
                case AUDIT_DEVMAJOR:
                        if (ctx) {
@@ -511,54 +534,84 @@ static enum audit_state audit_filter_syscall(struct task_struct *tsk,
                                             struct list_head *list)
 {
        struct audit_entry *e;
-       enum audit_state   state;
-       int                word = AUDIT_WORD(ctx->major);
-       int                bit  = AUDIT_BIT(ctx->major);
+       enum audit_state state;
 
-       if (audit_pid && ctx->pid == audit_pid)
+       if (audit_pid && tsk->tgid == audit_pid)
                return AUDIT_DISABLED;
 
        rcu_read_lock();
-       list_for_each_entry_rcu(e, list, list) {
-               if ((e->rule.mask[word] & bit) == bit
-                   && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
-                       rcu_read_unlock();
-                       return state;
-               }
+       if (!list_empty(list)) {
+                   int word = AUDIT_WORD(ctx->major);
+                   int bit  = AUDIT_BIT(ctx->major);
+
+                   list_for_each_entry_rcu(e, list, list) {
+                           if ((e->rule.mask[word] & bit) == bit
+                               && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
+                                   rcu_read_unlock();
+                                   return state;
+                           }
+                   }
        }
        rcu_read_unlock();
        return AUDIT_BUILD_CONTEXT;
 }
 
-int audit_filter_user(int pid, int type)
+static int audit_filter_user_rules(struct netlink_skb_parms *cb,
+                             struct audit_rule *rule,
+                             enum audit_state *state)
+{
+       int i;
+
+       for (i = 0; i < rule->field_count; i++) {
+               u32 field  = rule->fields[i] & ~AUDIT_NEGATE;
+               u32 value  = rule->values[i];
+               int result = 0;
+
+               switch (field) {
+               case AUDIT_PID:
+                       result = (cb->creds.pid == value);
+                       break;
+               case AUDIT_UID:
+                       result = (cb->creds.uid == value);
+                       break;
+               case AUDIT_GID:
+                       result = (cb->creds.gid == value);
+                       break;
+               case AUDIT_LOGINUID:
+                       result = (cb->loginuid == value);
+                       break;
+               }
+
+               if (rule->fields[i] & AUDIT_NEGATE)
+                       result = !result;
+               if (!result)
+                       return 0;
+       }
+       switch (rule->action) {
+       case AUDIT_NEVER:    *state = AUDIT_DISABLED;       break;
+       case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT;  break;
+       case AUDIT_ALWAYS:   *state = AUDIT_RECORD_CONTEXT; break;
+       }
+       return 1;
+}
+
+int audit_filter_user(struct netlink_skb_parms *cb, int type)
 {
-       struct task_struct *tsk;
        struct audit_entry *e;
        enum audit_state   state;
        int ret = 1;
 
-       read_lock(&tasklist_lock);
-       tsk = find_task_by_pid(pid);
-       if (tsk)
-               get_task_struct(tsk);
-       read_unlock(&tasklist_lock);
-
-       if (!tsk)
-               return -ESRCH;
-
        rcu_read_lock();
        list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
-               if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
+               if (audit_filter_user_rules(cb, &e->rule, &state)) {
                        if (state == AUDIT_DISABLED)
                                ret = 0;
                        break;
                }
        }
        rcu_read_unlock();
-       put_task_struct(tsk);
-
-       return 1; /* Audit by default */
 
+       return ret; /* Audit by default */
 }
 
 /* This should be called with task_lock() held. */
@@ -573,7 +626,7 @@ static inline struct audit_context *audit_get_context(struct task_struct *tsk,
        context->return_valid = return_valid;
        context->return_code  = return_code;
 
-       if (context->in_syscall && !context->auditable && tsk->pid != audit_pid) {
+       if (context->in_syscall && !context->auditable) {
                enum audit_state state;
                state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
                if (state == AUDIT_RECORD_CONTEXT)
@@ -750,13 +803,13 @@ static void audit_log_task_info(struct audit_buffer *ab)
        up_read(&mm->mmap_sem);
 }
 
-static void audit_log_exit(struct audit_context *context)
+static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
 {
        int i;
        struct audit_buffer *ab;
        struct audit_aux_data *aux;
 
-       ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
+       ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
        if (!ab)
                return;         /* audit_panic has been called */
        audit_log_format(ab, "arch=%x syscall=%d",
@@ -872,9 +925,11 @@ void audit_free(struct task_struct *tsk)
                return;
 
        /* Check for system calls that do not go through the exit
-        * function (e.g., exit_group), then free context block. */
+        * function (e.g., exit_group), then free context block. 
+        * We use GFP_ATOMIC here because we might be doing this 
+        * in the context of the idle thread */
        if (context->in_syscall && context->auditable)
-               audit_log_exit(context);
+               audit_log_exit(context, GFP_ATOMIC);
 
        audit_free_context(context);
 }
@@ -953,7 +1008,7 @@ void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
        if (likely(state == AUDIT_DISABLED))
                return;
 
-       context->serial     = audit_serial();
+       context->serial     = 0;
        context->ctime      = CURRENT_TIME;
        context->in_syscall = 1;
        context->auditable  = !!(state == AUDIT_RECORD_CONTEXT);
@@ -976,10 +1031,10 @@ void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
        /* Not having a context here is ok, since the parent may have
         * called __put_task_struct. */
        if (likely(!context))
-               return;
+               goto out;
 
        if (context->in_syscall && context->auditable)
-               audit_log_exit(context);
+               audit_log_exit(context, GFP_KERNEL);
 
        context->in_syscall = 0;
        context->auditable  = 0;
@@ -992,9 +1047,9 @@ void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
        } else {
                audit_free_names(context);
                audit_free_aux(context);
-               audit_zero_context(context, context->state);
                tsk->audit_context = context;
        }
+ out:
        put_task_struct(tsk);
 }
 
@@ -1107,6 +1162,8 @@ void audit_inode(const char *name, const struct inode *inode, unsigned flags)
 void auditsc_get_stamp(struct audit_context *ctx,
                       struct timespec *t, unsigned int *serial)
 {
+       if (!ctx->serial)
+               ctx->serial = audit_serial();
        t->tv_sec  = ctx->ctime.tv_sec;
        t->tv_nsec = ctx->ctime.tv_nsec;
        *serial    = ctx->serial;
@@ -1227,7 +1284,7 @@ void audit_signal_info(int sig, struct task_struct *t)
        extern pid_t audit_sig_pid;
        extern uid_t audit_sig_uid;
 
-       if (unlikely(audit_pid && t->pid == audit_pid)) {
+       if (unlikely(audit_pid && t->tgid == audit_pid)) {
                if (sig == SIGTERM || sig == SIGHUP) {
                        struct audit_context *ctx = current->audit_context;
                        audit_sig_pid = current->pid;