]> Pileus Git - ~andy/linux/blobdiff - security/selinux/avc.c
SELinux: unify the selinux_audit_data and selinux_late_audit_data
[~andy/linux] / security / selinux / avc.c
index 6989472d0957d89ab80e773c4ce1e2751de56cbe..c03a964ffde249bcf657ca8fc50aeeb6a67f24a9 100644 (file)
@@ -436,9 +436,9 @@ static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
 {
        struct common_audit_data *ad = a;
        audit_log_format(ab, "avc:  %s ",
-                        ad->selinux_audit_data.denied ? "denied" : "granted");
-       avc_dump_av(ab, ad->selinux_audit_data.tclass,
-                       ad->selinux_audit_data.audited);
+                        ad->selinux_audit_data->denied ? "denied" : "granted");
+       avc_dump_av(ab, ad->selinux_audit_data->tclass,
+                       ad->selinux_audit_data->audited);
        audit_log_format(ab, " for ");
 }
 
@@ -452,22 +452,23 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
 {
        struct common_audit_data *ad = a;
        audit_log_format(ab, " ");
-       avc_dump_query(ab, ad->selinux_audit_data.ssid,
-                          ad->selinux_audit_data.tsid,
-                          ad->selinux_audit_data.tclass);
+       avc_dump_query(ab, ad->selinux_audit_data->ssid,
+                          ad->selinux_audit_data->tsid,
+                          ad->selinux_audit_data->tclass);
 }
 
 /* This is the slow part of avc audit with big stack footprint */
-static noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
+noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
                u32 requested, u32 audited, u32 denied,
-               struct av_decision *avd, struct common_audit_data *a,
+               struct common_audit_data *a,
                unsigned flags)
 {
        struct common_audit_data stack_data;
+       struct selinux_audit_data sad;
 
        if (!a) {
                a = &stack_data;
-               COMMON_AUDIT_DATA_INIT(a, NONE);
+               a->type = LSM_AUDIT_DATA_NONE;
        }
 
        /*
@@ -481,77 +482,17 @@ static noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
            (flags & MAY_NOT_BLOCK))
                return -ECHILD;
 
-       a->selinux_audit_data.tclass = tclass;
-       a->selinux_audit_data.requested = requested;
-       a->selinux_audit_data.ssid = ssid;
-       a->selinux_audit_data.tsid = tsid;
-       a->selinux_audit_data.audited = audited;
-       a->selinux_audit_data.denied = denied;
-       a->lsm_pre_audit = avc_audit_pre_callback;
-       a->lsm_post_audit = avc_audit_post_callback;
-       common_lsm_audit(a);
-       return 0;
-}
+       sad.tclass = tclass;
+       sad.requested = requested;
+       sad.ssid = ssid;
+       sad.tsid = tsid;
+       sad.audited = audited;
+       sad.denied = denied;
 
-/**
- * avc_audit - Audit the granting or denial of permissions.
- * @ssid: source security identifier
- * @tsid: target security identifier
- * @tclass: target security class
- * @requested: requested permissions
- * @avd: access vector decisions
- * @result: result from avc_has_perm_noaudit
- * @a:  auxiliary audit data
- * @flags: VFS walk flags
- *
- * Audit the granting or denial of permissions in accordance
- * with the policy.  This function is typically called by
- * avc_has_perm() after a permission check, but can also be
- * called directly by callers who use avc_has_perm_noaudit()
- * in order to separate the permission check from the auditing.
- * For example, this separation is useful when the permission check must
- * be performed under a lock, to allow the lock to be released
- * before calling the auditing code.
- */
-int avc_audit(u32 ssid, u32 tsid,
-              u16 tclass, u32 requested,
-              struct av_decision *avd, int result, struct common_audit_data *a,
-              unsigned flags)
-{
-       u32 denied, audited;
-       denied = requested & ~avd->allowed;
-       if (unlikely(denied)) {
-               audited = denied & avd->auditdeny;
-               /*
-                * a->selinux_audit_data.auditdeny is TRICKY!  Setting a bit in
-                * this field means that ANY denials should NOT be audited if
-                * the policy contains an explicit dontaudit rule for that
-                * permission.  Take notice that this is unrelated to the
-                * actual permissions that were denied.  As an example lets
-                * assume:
-                *
-                * denied == READ
-                * avd.auditdeny & ACCESS == 0 (not set means explicit rule)
-                * selinux_audit_data.auditdeny & ACCESS == 1
-                *
-                * We will NOT audit the denial even though the denied
-                * permission was READ and the auditdeny checks were for
-                * ACCESS
-                */
-               if (a &&
-                   a->selinux_audit_data.auditdeny &&
-                   !(a->selinux_audit_data.auditdeny & avd->auditdeny))
-                       audited = 0;
-       } else if (result)
-               audited = denied = requested;
-       else
-               audited = requested & avd->auditallow;
-       if (likely(!audited))
-               return 0;
-
-       return slow_avc_audit(ssid, tsid, tclass,
-               requested, audited, denied,
-               avd, a, flags);
+       a->selinux_audit_data = &sad;
+
+       common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
+       return 0;
 }
 
 /**
@@ -741,6 +682,41 @@ int avc_ss_reset(u32 seqno)
        return rc;
 }
 
+/*
+ * Slow-path helper function for avc_has_perm_noaudit,
+ * when the avc_node lookup fails. We get called with
+ * the RCU read lock held, and need to return with it
+ * still held, but drop if for the security compute.
+ *
+ * Don't inline this, since it's the slow-path and just
+ * results in a bigger stack frame.
+ */
+static noinline struct avc_node *avc_compute_av(u32 ssid, u32 tsid,
+                        u16 tclass, struct av_decision *avd)
+{
+       rcu_read_unlock();
+       security_compute_av(ssid, tsid, tclass, avd);
+       rcu_read_lock();
+       return avc_insert(ssid, tsid, tclass, avd);
+}
+
+static noinline int avc_denied(u32 ssid, u32 tsid,
+                        u16 tclass, u32 requested,
+                        unsigned flags,
+                        struct av_decision *avd)
+{
+       if (flags & AVC_STRICT)
+               return -EACCES;
+
+       if (selinux_enforcing && !(avd->flags & AVD_FLAGS_PERMISSIVE))
+               return -EACCES;
+
+       avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
+                               tsid, tclass, avd->seqno);
+       return 0;
+}
+
+
 /**
  * avc_has_perm_noaudit - Check permissions but perform no auditing.
  * @ssid: source security identifier
@@ -761,7 +737,7 @@ int avc_ss_reset(u32 seqno)
  * auditing, e.g. in cases where a lock must be held for the check but
  * should be released for the auditing.
  */
-int avc_has_perm_noaudit(u32 ssid, u32 tsid,
+inline int avc_has_perm_noaudit(u32 ssid, u32 tsid,
                         u16 tclass, u32 requested,
                         unsigned flags,
                         struct av_decision *avd)
@@ -776,26 +752,15 @@ int avc_has_perm_noaudit(u32 ssid, u32 tsid,
 
        node = avc_lookup(ssid, tsid, tclass);
        if (unlikely(!node)) {
-               rcu_read_unlock();
-               security_compute_av(ssid, tsid, tclass, avd);
-               rcu_read_lock();
-               node = avc_insert(ssid, tsid, tclass, avd);
+               node = avc_compute_av(ssid, tsid, tclass, avd);
        } else {
                memcpy(avd, &node->ae.avd, sizeof(*avd));
                avd = &node->ae.avd;
        }
 
        denied = requested & ~(avd->allowed);
-
-       if (denied) {
-               if (flags & AVC_STRICT)
-                       rc = -EACCES;
-               else if (!selinux_enforcing || (avd->flags & AVD_FLAGS_PERMISSIVE))
-                       avc_update_node(AVC_CALLBACK_GRANT, requested, ssid,
-                                       tsid, tclass, avd->seqno);
-               else
-                       rc = -EACCES;
-       }
+       if (unlikely(denied))
+               rc = avc_denied(ssid, tsid, tclass, requested, flags, avd);
 
        rcu_read_unlock();
        return rc;