Create a new audit record AUDIT_MAC_OBJ_CONTEXTS.
An example of the MAC_OBJ_CONTEXTS (1424) record is:

    type=MAC_OBJ_CONTEXTS[1424]
    msg=audit(1601152467.009:1050):
    obj_selinux=unconfined_u:object_r:user_home_t:s0

When an audit event includes a AUDIT_MAC_OBJ_CONTEXTS record
the "obj=" field in other records in the event will be "obj=?".
An AUDIT_MAC_OBJ_CONTEXTS record is supplied when the system has
multiple security modules that may make access decisions based
on an object security context.

Signed-off-by: Casey Schaufler <ca...@schaufler-ca.com>
---
 include/linux/audit.h      |  6 +++++
 include/uapi/linux/audit.h |  1 +
 kernel/audit.c             | 51 +++++++++++++++++++++++++++++++++++++-
 kernel/auditsc.c           | 45 ++++++++-------------------------
 4 files changed, 68 insertions(+), 35 deletions(-)

diff --git a/include/linux/audit.h b/include/linux/audit.h
index 3402e3ca43c6..8fdfa2721273 100644
--- a/include/linux/audit.h
+++ b/include/linux/audit.h
@@ -188,6 +188,7 @@ extern void             audit_log_path_denied(int type,
 extern void                audit_log_lost(const char *message);
 
 extern int audit_log_subj_ctx(struct audit_buffer *ab, struct lsm_prop *prop);
+extern int audit_log_obj_ctx(struct audit_buffer *ab, struct lsm_prop *prop);
 extern int audit_log_task_context(struct audit_buffer *ab);
 extern void audit_log_task_info(struct audit_buffer *ab);
 
@@ -255,6 +256,11 @@ static inline int audit_log_subj_ctx(struct audit_buffer 
*ab,
 {
        return 0;
 }
+static inline int audit_log_obj_ctx(struct audit_buffer *ab,
+                                   struct lsm_prop *prop)
+{
+       return 0;
+}
 static inline int audit_log_task_context(struct audit_buffer *ab)
 {
        return 0;
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 5ebb5d80363d..8ca58144bcc6 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -147,6 +147,7 @@
 #define AUDIT_IPE_CONFIG_CHANGE        1421    /* IPE config change */
 #define AUDIT_IPE_POLICY_LOAD  1422    /* IPE policy load */
 #define AUDIT_MAC_TASK_CONTEXTS        1423    /* Multiple LSM task contexts */
+#define AUDIT_MAC_OBJ_CONTEXTS 1424    /* Multiple LSM objext contexts */
 
 #define AUDIT_FIRST_KERN_ANOM_MSG   1700
 #define AUDIT_LAST_KERN_ANOM_MSG    1799
diff --git a/kernel/audit.c b/kernel/audit.c
index 8ce453f6dc7d..69db0ee09a3f 100644
--- a/kernel/audit.c
+++ b/kernel/audit.c
@@ -1133,7 +1133,6 @@ static int is_audit_feature_set(int i)
        return af.features & AUDIT_FEATURE_TO_MASK(i);
 }
 
-
 static int audit_get_feature(struct sk_buff *skb)
 {
        u32 seq;
@@ -2324,6 +2323,56 @@ int audit_log_task_context(struct audit_buffer *ab)
 }
 EXPORT_SYMBOL(audit_log_task_context);
 
+int audit_log_obj_ctx(struct audit_buffer *ab, struct lsm_prop *prop)
+{
+       int i;
+       int rc;
+       int error = 0;
+       char *space = "";
+       struct lsm_context context;
+
+       if (audit_secctx_cnt < 2) {
+               error = security_lsmprop_to_secctx(prop, &context,
+                                                  LSM_ID_UNDEF);
+               if (error < 0) {
+                       if (error != -EINVAL)
+                               goto error_path;
+                       return error;
+               }
+               audit_log_format(ab, " obj=%s", context.context);
+               security_release_secctx(&context);
+               return 0;
+       }
+       audit_log_format(ab, " obj=?");
+       error = audit_buffer_aux_new(ab, AUDIT_MAC_OBJ_CONTEXTS);
+       if (error)
+               goto error_path;
+
+       for (i = 0; i < audit_secctx_cnt; i++) {
+               rc = security_lsmprop_to_secctx(prop, &context,
+                                               audit_lsms[i]->id);
+               if (rc < 0) {
+                       audit_log_format(ab, "%sobj_%s=?", space,
+                                        audit_lsms[i]->name);
+                       if (rc != -EINVAL)
+                               audit_panic("error in audit_log_obj_ctx");
+                       error = rc;
+               } else {
+                       audit_log_format(ab, "%sobj_%s=%s", space,
+                                        audit_lsms[i]->name, context.context);
+                       security_release_secctx(&context);
+               }
+               space = " ";
+       }
+
+       audit_buffer_aux_end(ab);
+       return error;
+
+error_path:
+       audit_panic("error in audit_log_obj_ctx");
+       return error;
+}
+
 void audit_log_d_path_exe(struct audit_buffer *ab,
                          struct mm_struct *mm)
 {
diff --git a/kernel/auditsc.c b/kernel/auditsc.c
index dc3f7e9666f2..e39d7be20c29 100644
--- a/kernel/auditsc.c
+++ b/kernel/auditsc.c
@@ -1098,7 +1098,6 @@ static int audit_log_pid_context(struct audit_context 
*context, pid_t pid,
                                 char *comm)
 {
        struct audit_buffer *ab;
-       struct lsm_context ctx;
        int rc = 0;
 
        ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
@@ -1108,15 +1107,9 @@ static int audit_log_pid_context(struct audit_context 
*context, pid_t pid,
        audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
                         from_kuid(&init_user_ns, auid),
                         from_kuid(&init_user_ns, uid), sessionid);
-       if (lsmprop_is_set(prop)) {
-               if (security_lsmprop_to_secctx(prop, &ctx, LSM_ID_UNDEF) < 0) {
-                       audit_log_format(ab, " obj=(none)");
-                       rc = 1;
-               } else {
-                       audit_log_format(ab, " obj=%s", ctx.context);
-                       security_release_secctx(&ctx);
-               }
-       }
+       if (lsmprop_is_set(prop) && audit_log_obj_ctx(ab, prop))
+               rc = 1;
+
        audit_log_format(ab, " ocomm=");
        audit_log_untrustedstring(ab, comm);
        audit_log_end(ab);
@@ -1392,16 +1385,8 @@ static void show_special(struct audit_context *context, 
int *call_panic)
                                 from_kgid(&init_user_ns, context->ipc.gid),
                                 context->ipc.mode);
                if (lsmprop_is_set(&context->ipc.oprop)) {
-                       struct lsm_context lsmctx;
-
-                       if (security_lsmprop_to_secctx(&context->ipc.oprop,
-                                                      &lsmctx,
-                                                      LSM_ID_UNDEF) < 0) {
+                       if (audit_log_obj_ctx(ab, &context->ipc.oprop))
                                *call_panic = 1;
-                       } else {
-                               audit_log_format(ab, " obj=%s", lsmctx.context);
-                               security_release_secctx(&lsmctx);
-                       }
                }
                if (context->ipc.has_perm) {
                        audit_log_end(ab);
@@ -1558,18 +1543,9 @@ static void audit_log_name(struct audit_context 
*context, struct audit_names *n,
                                 from_kgid(&init_user_ns, n->gid),
                                 MAJOR(n->rdev),
                                 MINOR(n->rdev));
-       if (lsmprop_is_set(&n->oprop)) {
-               struct lsm_context ctx;
-
-               if (security_lsmprop_to_secctx(&n->oprop, &ctx,
-                                              LSM_ID_UNDEF) < 0) {
-                       if (call_panic)
-                               *call_panic = 2;
-               } else {
-                       audit_log_format(ab, " obj=%s", ctx.context);
-                       security_release_secctx(&ctx);
-               }
-       }
+       if (lsmprop_is_set(&n->oprop) &&
+           audit_log_obj_ctx(ab, &n->oprop))
+               *call_panic = 2;
 
        /* log the audit_names record type */
        switch (n->type) {
@@ -1780,15 +1756,16 @@ static void audit_log_exit(void)
                                                  axs->target_sessionid[i],
                                                  &axs->target_ref[i],
                                                  axs->target_comm[i]))
-                               call_panic = 1;
+                       call_panic = 1;
        }
 
        if (context->target_pid &&
            audit_log_pid_context(context, context->target_pid,
                                  context->target_auid, context->target_uid,
                                  context->target_sessionid,
-                                 &context->target_ref, context->target_comm))
-                       call_panic = 1;
+                                 &context->target_ref,
+                                 context->target_comm))
+               call_panic = 1;
 
        if (context->pwd.dentry && context->pwd.mnt) {
                ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
-- 
2.47.0


Reply via email to