Skip to content

Commit

Permalink
lsm: use lsm_prop in security_current_getsecid
Browse files Browse the repository at this point in the history
Change the security_current_getsecid_subj() and
security_task_getsecid_obj() interfaces to fill in a lsm_prop structure
instead of a u32 secid.  Audit interfaces will need to collect all
possible security data for possible reporting.

Cc: linux-integrity@vger.kernel.org
Cc: audit@vger.kernel.org
Cc: selinux@vger.kernel.org
Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
[PM: subject line tweak]
Signed-off-by: Paul Moore <paul@paul-moore.com>
  • Loading branch information
Casey Schaufler authored and Paul Moore committed Oct 11, 2024
1 parent e4f6822 commit 37f670a
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 108 deletions.
6 changes: 3 additions & 3 deletions include/linux/lsm_hook_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ LSM_HOOK(int, 0, task_fix_setgroups, struct cred *new, const struct cred * old)
LSM_HOOK(int, 0, task_setpgid, struct task_struct *p, pid_t pgid)
LSM_HOOK(int, 0, task_getpgid, struct task_struct *p)
LSM_HOOK(int, 0, task_getsid, struct task_struct *p)
LSM_HOOK(void, LSM_RET_VOID, current_getsecid_subj, u32 *secid)
LSM_HOOK(void, LSM_RET_VOID, task_getsecid_obj,
struct task_struct *p, u32 *secid)
LSM_HOOK(void, LSM_RET_VOID, current_getlsmprop_subj, struct lsm_prop *prop)
LSM_HOOK(void, LSM_RET_VOID, task_getlsmprop_obj,
struct task_struct *p, struct lsm_prop *prop)
LSM_HOOK(int, 0, task_setnice, struct task_struct *p, int nice)
LSM_HOOK(int, 0, task_setioprio, struct task_struct *p, int ioprio)
LSM_HOOK(int, 0, task_getioprio, struct task_struct *p)
Expand Down
13 changes: 7 additions & 6 deletions include/linux/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ int security_task_fix_setgroups(struct cred *new, const struct cred *old);
int security_task_setpgid(struct task_struct *p, pid_t pgid);
int security_task_getpgid(struct task_struct *p);
int security_task_getsid(struct task_struct *p);
void security_current_getsecid_subj(u32 *secid);
void security_task_getsecid_obj(struct task_struct *p, u32 *secid);
void security_current_getlsmprop_subj(struct lsm_prop *prop);
void security_task_getlsmprop_obj(struct task_struct *p, struct lsm_prop *prop);
int security_task_setnice(struct task_struct *p, int nice);
int security_task_setioprio(struct task_struct *p, int ioprio);
int security_task_getioprio(struct task_struct *p);
Expand Down Expand Up @@ -1305,14 +1305,15 @@ static inline int security_task_getsid(struct task_struct *p)
return 0;
}

static inline void security_current_getsecid_subj(u32 *secid)
static inline void security_current_getlsmprop_subj(struct lsm_prop *prop)
{
*secid = 0;
lsmprop_init(prop);
}

static inline void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
static inline void security_task_getlsmprop_obj(struct task_struct *p,
struct lsm_prop *prop)
{
*secid = 0;
lsmprop_init(prop);
}

static inline int security_task_setnice(struct task_struct *p, int nice)
Expand Down
11 changes: 5 additions & 6 deletions kernel/audit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2179,16 +2179,16 @@ void audit_log_key(struct audit_buffer *ab, char *key)

int audit_log_task_context(struct audit_buffer *ab)
{
struct lsm_prop prop;
char *ctx = NULL;
unsigned len;
int error;
u32 sid;

security_current_getsecid_subj(&sid);
if (!sid)
security_current_getlsmprop_subj(&prop);
if (!lsmprop_is_set(&prop))
return 0;

error = security_secid_to_secctx(sid, &ctx, &len);
error = security_lsmprop_to_secctx(&prop, &ctx, &len);
if (error) {
if (error != -EINVAL)
goto error_path;
Expand Down Expand Up @@ -2405,8 +2405,7 @@ int audit_signal_info(int sig, struct task_struct *t)
audit_sig_uid = auid;
else
audit_sig_uid = uid;
/* scaffolding */
security_current_getsecid_subj(&audit_sig_lsm.scaffold.secid);
security_current_getlsmprop_subj(&audit_sig_lsm);
}

return audit_signal_info_syscall(t);
Expand Down
3 changes: 1 addition & 2 deletions kernel/auditfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,7 @@ int audit_filter(int msgtype, unsigned int listtype)
case AUDIT_SUBJ_CLR:
if (f->lsm_rule) {
/* scaffolding */
security_current_getsecid_subj(
&prop.scaffold.secid);
security_current_getlsmprop_subj(&prop);
result = security_audit_rule_match(
&prop, f->type, f->op,
f->lsm_rule);
Expand Down
22 changes: 14 additions & 8 deletions kernel/auditsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ static int audit_filter_rules(struct task_struct *tsk,
{
const struct cred *cred;
int i, need_sid = 1;
u32 sid;
struct lsm_prop prop = { };
unsigned int sessionid;

Expand Down Expand Up @@ -675,15 +674,14 @@ static int audit_filter_rules(struct task_struct *tsk,
* fork()/copy_process() in which case
* the new @tsk creds are still a dup
* of @current's creds so we can still
* use security_current_getsecid_subj()
* use
* security_current_getlsmprop_subj()
* here even though it always refs
* @current's creds
*/
security_current_getsecid_subj(&sid);
security_current_getlsmprop_subj(&prop);
need_sid = 0;
}
/* scaffolding */
prop.scaffold.secid = sid;
result = security_audit_rule_match(&prop,
f->type,
f->op,
Expand Down Expand Up @@ -2730,12 +2728,15 @@ int __audit_sockaddr(int len, void *a)
void __audit_ptrace(struct task_struct *t)
{
struct audit_context *context = audit_context();
struct lsm_prop prop;

context->target_pid = task_tgid_nr(t);
context->target_auid = audit_get_loginuid(t);
context->target_uid = task_uid(t);
context->target_sessionid = audit_get_sessionid(t);
security_task_getsecid_obj(t, &context->target_sid);
security_task_getlsmprop_obj(t, &prop);
/* scaffolding */
context->target_sid = prop.scaffold.secid;
memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
}

Expand All @@ -2751,6 +2752,7 @@ int audit_signal_info_syscall(struct task_struct *t)
struct audit_aux_data_pids *axp;
struct audit_context *ctx = audit_context();
kuid_t t_uid = task_uid(t);
struct lsm_prop prop;

if (!audit_signals || audit_dummy_context())
return 0;
Expand All @@ -2762,7 +2764,9 @@ int audit_signal_info_syscall(struct task_struct *t)
ctx->target_auid = audit_get_loginuid(t);
ctx->target_uid = t_uid;
ctx->target_sessionid = audit_get_sessionid(t);
security_task_getsecid_obj(t, &ctx->target_sid);
security_task_getlsmprop_obj(t, &prop);
/* scaffolding */
ctx->target_sid = prop.scaffold.secid;
memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
return 0;
}
Expand All @@ -2783,7 +2787,9 @@ int audit_signal_info_syscall(struct task_struct *t)
axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
axp->target_uid[axp->pid_count] = t_uid;
axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]);
security_task_getlsmprop_obj(t, &prop);
/* scaffolding */
axp->target_sid[axp->pid_count] = prop.scaffold.secid;
memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
axp->pid_count++;

Expand Down
5 changes: 4 additions & 1 deletion net/netlabel/netlabel_unlabeled.c
Original file line number Diff line number Diff line change
Expand Up @@ -1534,11 +1534,14 @@ int __init netlbl_unlabel_defconf(void)
int ret_val;
struct netlbl_dom_map *entry;
struct netlbl_audit audit_info;
struct lsm_prop prop;

/* Only the kernel is allowed to call this function and the only time
* it is called is at bootup before the audit subsystem is reporting
* messages so don't worry to much about these values. */
security_current_getsecid_subj(&audit_info.secid);
security_current_getlsmprop_subj(&prop);
/* scaffolding */
audit_info.secid = prop.scaffold.secid;
audit_info.loginuid = GLOBAL_ROOT_UID;
audit_info.sessionid = 0;

Expand Down
6 changes: 5 additions & 1 deletion net/netlabel/netlabel_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
*/
static inline void netlbl_netlink_auditinfo(struct netlbl_audit *audit_info)
{
security_current_getsecid_subj(&audit_info->secid);
struct lsm_prop prop;

security_current_getlsmprop_subj(&prop);
/* scaffolding */
audit_info->secid = prop.scaffold.secid;
audit_info->loginuid = audit_get_loginuid(current);
audit_info->sessionid = audit_get_sessionid(current);
}
Expand Down
20 changes: 14 additions & 6 deletions security/apparmor/lsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -982,17 +982,24 @@ static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm)
return;
}

static void apparmor_current_getsecid_subj(u32 *secid)
static void apparmor_current_getlsmprop_subj(struct lsm_prop *prop)
{
struct aa_label *label = __begin_current_label_crit_section();
*secid = label->secid;

prop->apparmor.label = label;
/* scaffolding */
prop->scaffold.secid = label->secid;
__end_current_label_crit_section(label);
}

static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
static void apparmor_task_getlsmprop_obj(struct task_struct *p,
struct lsm_prop *prop)
{
struct aa_label *label = aa_get_task_label(p);
*secid = label->secid;

prop->apparmor.label = label;
/* scaffolding */
prop->scaffold.secid = label->secid;
aa_put_label(label);
}

Expand Down Expand Up @@ -1503,8 +1510,9 @@ static struct security_hook_list apparmor_hooks[] __ro_after_init = {

LSM_HOOK_INIT(task_free, apparmor_task_free),
LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
LSM_HOOK_INIT(current_getlsmprop_subj,
apparmor_current_getlsmprop_subj),
LSM_HOOK_INIT(task_getlsmprop_obj, apparmor_task_getlsmprop_obj),
LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
LSM_HOOK_INIT(task_kill, apparmor_task_kill),
LSM_HOOK_INIT(userns_create, apparmor_userns_create),
Expand Down
6 changes: 3 additions & 3 deletions security/integrity/ima/ima.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ static inline void ima_process_queued_keys(void) {}

/* LIM API function definitions */
int ima_get_action(struct mnt_idmap *idmap, struct inode *inode,
const struct cred *cred, u32 secid, int mask,
const struct cred *cred, struct lsm_prop *prop, int mask,
enum ima_hooks func, int *pcr,
struct ima_template_desc **template_desc,
const char *func_data, unsigned int *allowed_algos);
Expand Down Expand Up @@ -400,8 +400,8 @@ const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);

/* IMA policy related functions */
int ima_match_policy(struct mnt_idmap *idmap, struct inode *inode,
const struct cred *cred, u32 secid, enum ima_hooks func,
int mask, int flags, int *pcr,
const struct cred *cred, struct lsm_prop *prop,
enum ima_hooks func, int mask, int flags, int *pcr,
struct ima_template_desc **template_desc,
const char *func_data, unsigned int *allowed_algos);
void ima_init_policy(void);
Expand Down
6 changes: 3 additions & 3 deletions security/integrity/ima/ima_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
* @idmap: idmap of the mount the inode was found from
* @inode: pointer to the inode associated with the object being validated
* @cred: pointer to credentials structure to validate
* @secid: secid of the task being validated
* @prop: properties of the task being validated
* @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC,
* MAY_APPEND)
* @func: caller identifier
Expand All @@ -187,7 +187,7 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
*
*/
int ima_get_action(struct mnt_idmap *idmap, struct inode *inode,
const struct cred *cred, u32 secid, int mask,
const struct cred *cred, struct lsm_prop *prop, int mask,
enum ima_hooks func, int *pcr,
struct ima_template_desc **template_desc,
const char *func_data, unsigned int *allowed_algos)
Expand All @@ -196,7 +196,7 @@ int ima_get_action(struct mnt_idmap *idmap, struct inode *inode,

flags &= ima_policy_flag;

return ima_match_policy(idmap, inode, cred, secid, func, mask,
return ima_match_policy(idmap, inode, cred, prop, func, mask,
flags, pcr, template_desc, func_data,
allowed_algos);
}
Expand Down
6 changes: 3 additions & 3 deletions security/integrity/ima/ima_appraise.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ bool is_ima_appraise_enabled(void)
int ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode,
int mask, enum ima_hooks func)
{
u32 secid;
struct lsm_prop prop;

if (!ima_appraise)
return 0;

security_current_getsecid_subj(&secid);
return ima_match_policy(idmap, inode, current_cred(), secid,
security_current_getlsmprop_subj(&prop);
return ima_match_policy(idmap, inode, current_cred(), &prop,
func, mask, IMA_APPRAISE | IMA_HASH, NULL,
NULL, NULL, NULL);
}
Expand Down
Loading

0 comments on commit 37f670a

Please sign in to comment.