audit: add record for multiple object contexts

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

    type=MAC_OBJ_CONTEXTS
      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 <casey@schaufler-ca.com>
[PM: subj tweak, audit example readability indents]
Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Casey Schaufler 2025-08-16 10:28:59 -07:00 committed by Paul Moore
parent eb59d494ee
commit 0ffbc876d0
6 changed files with 78 additions and 34 deletions

View File

@ -151,6 +151,7 @@ extern unsigned compat_signal_class[];
/* bit values for audit_cfg_lsm */
#define AUDIT_CFG_LSM_SECCTX_SUBJECT BIT(0)
#define AUDIT_CFG_LSM_SECCTX_OBJECT BIT(1)
struct filename;
@ -191,6 +192,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);
@ -258,6 +260,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;

View File

@ -149,6 +149,7 @@
#define AUDIT_LANDLOCK_ACCESS 1423 /* Landlock denial */
#define AUDIT_LANDLOCK_DOMAIN 1424 /* Landlock domain status */
#define AUDIT_MAC_TASK_CONTEXTS 1425 /* Multiple LSM task contexts */
#define AUDIT_MAC_OBJ_CONTEXTS 1426 /* Multiple LSM objext contexts */
#define AUDIT_FIRST_KERN_ANOM_MSG 1700
#define AUDIT_LAST_KERN_ANOM_MSG 1799

View File

@ -85,7 +85,9 @@ static unsigned int audit_net_id;
/* Number of modules that provide a security context.
List of lsms that provide a security context */
static u32 audit_subj_secctx_cnt;
static u32 audit_obj_secctx_cnt;
static const struct lsm_id *audit_subj_lsms[MAX_LSM_COUNT];
static const struct lsm_id *audit_obj_lsms[MAX_LSM_COUNT];
/**
* struct audit_net - audit private network namespace data
@ -305,6 +307,12 @@ void audit_cfg_lsm(const struct lsm_id *lsmid, int flags)
return;
audit_subj_lsms[audit_subj_secctx_cnt++] = lsmid;
}
if (flags & AUDIT_CFG_LSM_SECCTX_OBJECT) {
for (i = 0 ; i < audit_obj_secctx_cnt; i++)
if (audit_obj_lsms[i] == lsmid)
return;
audit_obj_lsms[audit_obj_secctx_cnt++] = lsmid;
}
}
/**
@ -1142,7 +1150,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;
@ -2337,6 +2344,55 @@ 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 ctx;
if (audit_obj_secctx_cnt < 2) {
error = security_lsmprop_to_secctx(prop, &ctx, LSM_ID_UNDEF);
if (error < 0) {
if (error != -EINVAL)
goto error_path;
return error;
}
audit_log_format(ab, " obj=%s", ctx.context);
security_release_secctx(&ctx);
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_obj_secctx_cnt; i++) {
rc = security_lsmprop_to_secctx(prop, &ctx,
audit_obj_lsms[i]->id);
if (rc < 0) {
audit_log_format(ab, "%sobj_%s=?", space,
audit_obj_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_obj_lsms[i]->name, ctx.context);
security_release_secctx(&ctx);
}
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)
{

View File

@ -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) {

View File

@ -7619,7 +7619,9 @@ static __init int selinux_init(void)
cred_init_security();
/* Inform the audit system that secctx is used */
audit_cfg_lsm(&selinux_lsmid, AUDIT_CFG_LSM_SECCTX_SUBJECT);
audit_cfg_lsm(&selinux_lsmid,
AUDIT_CFG_LSM_SECCTX_SUBJECT |
AUDIT_CFG_LSM_SECCTX_OBJECT);
default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
if (!default_noexec)

View File

@ -5268,7 +5268,9 @@ static __init int smack_init(void)
init_smack_known_list();
/* Inform the audit system that secctx is used */
audit_cfg_lsm(&smack_lsmid, AUDIT_CFG_LSM_SECCTX_SUBJECT);
audit_cfg_lsm(&smack_lsmid,
AUDIT_CFG_LSM_SECCTX_SUBJECT |
AUDIT_CFG_LSM_SECCTX_OBJECT);
return 0;
}