security: Add a cred_getsecid hook
Subsystems (6)
PATCH v5 1/2 security: Add a cred_getsecid hook
For IMA purposes, we want to be able to obtain the prepared secid in the bprm structure before the credentials are committed. Add a cred_getsecid hook that makes this possible.
---
Ok, I'm taking a slightly different approach and just passing in the secid - for everything other than the CREDS_CHECK case this will be the task secid, for CREDS_CHECK it'll be the one associated with the BPRM structure. I believe that this results in zero functional change other than when dealing with the new functionality, and we can come back and rework this once we have a better idea about how IMA should be using secids in the first place.
include/linux/lsm_hooks.h | 6 ++++++ include/linux/security.h | 1 + security/security.c | 7 +++++++ security/selinux/hooks.c | 6 ++++++ security/smack/smack_lsm.c | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+)
include/linux/lsm_hooks.h +6 -0
@@ -554,6 +554,10 @@* @new points to the new credentials. * @old points to the original credentials. * Transfer data from original creds to new creds +* @cred_getsecid: +* Retrieve the security identifier of the cred structure @c +* @c contains the credentials, secid will be placed into @secid. +* In case of failure, @secid will be set to zero. * @kernel_act_as: * Set the credentials for a kernel service to act as (subjective context). * @new points to the credentials to be modified.
@@ -1541,6 +1545,7 @@ union security_list_options {int (*cred_prepare)(struct cred *new,const struct cred *old,gfp_t gfp);void (*cred_transfer)(struct cred *new,const struct cred *old); +void (*cred_getsecid)(const struct cred *c,u32 *secid);int (*kernel_act_as)(struct cred *new,u32 secid);int (*kernel_create_files_as)(struct cred *new,struct inode *inode);int (*kernel_module_request)(char *kmod_name);
@@ -1824,6 +1829,7 @@ struct security_hook_heads {struct list_head cred_free;struct list_head cred_prepare;struct list_head cred_transfer; +struct list_head cred_getsecid;struct list_head kernel_act_as;struct list_head kernel_create_files_as;struct list_head kernel_read_file;
include/linux/security.h +1 -0
@@ -324,6 +324,7 @@ int security_cred_alloc_blank(struct cred *cred, gfp_t gfp);void security_cred_free (struct cred *cred);int security_prepare_creds (struct cred *new,const struct cred *old,gfp_t gfp);void security_transfer_creds (struct cred *new,const struct cred *old); +void security_cred_getsecid (const struct cred *c,u32 *secid);int security_kernel_act_as (struct cred *new,u32 secid);int security_kernel_create_files_as (struct cred *new,struct inode *inode);int security_kernel_module_request (char *kmod_name);
security/security.c +7 -0
@@ -1005,6 +1005,13 @@ void security_transfer_creds(struct cred *new, const struct cred *old)call_void_hook (cred_transfer, new, old); } +void security_cred_getsecid (const struct cred *c,u32 *secid) +{ + *secid =0 ; +call_void_hook (cred_getsecid, c, secid); +} +EXPORT_SYMBOL (security_cred_getsecid); +int security_kernel_act_as (struct cred *new,u32 secid) {return call_int_hook (kernel_act_as,0 , new, secid);
security/selinux/hooks.c +6 -0
@@ -3844,6 +3844,11 @@ static void selinux_cred_transfer(struct cred *new, const struct cred *old) *tsec = *old_tsec; } +static void selinux_cred_getsecid (const struct cred *c,u32 *secid) +{ + *secid =cred_sid (c); +} +/* * set the security data for a kernel service * - all the creation contexts are set to unlabelled
@@ -6479,6 +6484,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (cred_free, selinux_cred_free),LSM_HOOK_INIT (cred_prepare, selinux_cred_prepare),LSM_HOOK_INIT (cred_transfer, selinux_cred_transfer), +LSM_HOOK_INIT (cred_getsecid, selinux_cred_getsecid),LSM_HOOK_INIT (kernel_act_as, selinux_kernel_act_as),LSM_HOOK_INIT (kernel_create_files_as, selinux_kernel_create_files_as), LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
security/smack/smack_lsm.c +18 -0
@@ -2049,6 +2049,23 @@ static void smack_cred_transfer(struct cred *new, const struct cred *old)/* cbs copy rule list */ } +/** +* smack_cred_getsecid - get the secid corresponding to a creds structure +* @c: the object creds +* @secid: where to put the result +* +* Sets the secid to contain a u32 version of the smack label. +*/ +static void smack_cred_getsecid (const struct cred *c,u32 *secid) +{ +struct smack_known *skp; + +rcu_read_lock (); + skp =smk_of_task (c->security); + *secid = skp->smk_secid; +rcu_read_unlock (); +} +/** * smack_kernel_act_as - Set the subjective context in a set of credentials * @new: points to the set of credentials to be modified.
@@ -4727,6 +4744,7 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (cred_free, smack_cred_free),LSM_HOOK_INIT (cred_prepare, smack_cred_prepare),LSM_HOOK_INIT (cred_transfer, smack_cred_transfer), +LSM_HOOK_INIT (cred_getsecid, smack_cred_getsecid),LSM_HOOK_INIT (kernel_act_as, smack_kernel_act_as),LSM_HOOK_INIT (kernel_create_files_as, smack_kernel_create_files_as), LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-goog -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v5 2/2 IMA: Support using new creds in appraisal policy
The existing BPRM_CHECK functionality in IMA validates against the credentials of the existing process, not any new credentials that the child process may transition to. Add an additional CREDS_CHECK target and refactor IMA to pass the appropriate creds structure. In ima_bprm_check(), check with both the existing process credentials and the credentials that will be committed when the new process is started. This will not change behaviour unless the system policy is extended to include CREDS_CHECK targets - BPRM_CHECK will continue to check the same credentials that it did previously.
--- Documentation/ABI/testing/ima_policy | 2 +- security/integrity/iint.c | 1 + security/integrity/ima/ima.h | 9 ++++---- security/integrity/ima/ima_api.c | 9 +++++--- security/integrity/ima/ima_appraise.c | 13 ++++++++++- security/integrity/ima/ima_main.c | 42 ++++++++++++++++++++++++++--------- security/integrity/ima/ima_policy.c | 23 ++++++++++++------- security/integrity/integrity.h | 9 ++++++-- 8 files changed, 79 insertions(+), 29 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
@@ -25,7 +25,7 @@ Description: [obj_user=] [obj_role=] [obj_type=]] option: [[appraise_type=]] [permit_directio] - base: func:= [BPRM_CHECK][MMAP_CHECK][FILE_CHECK][MODULE_CHECK] + base: func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK] [FIRMWARE_CHECK] [KEXEC_KERNEL_CHECK] [KEXEC_INITRAMFS_CHECK] mask:= [[^]MAY_READ] [[^]MAY_WRITE] [[^]MAY_APPEND]
security/integrity/iint.c +1 -0
@@ -78,6 +78,7 @@ static void iint_free(struct integrity_iint_cache *iint) iint->ima_mmap_status =INTEGRITY_UNKNOWN ; iint->ima_bprm_status =INTEGRITY_UNKNOWN ; iint->ima_read_status =INTEGRITY_UNKNOWN ; + iint->ima_creds_status =INTEGRITY_UNKNOWN ; iint->evm_status =INTEGRITY_UNKNOWN ; iint->measured_pcrs =0 ;kmem_cache_free (iint_cache, iint);
security/integrity/ima/ima.h +5 -4
@@ -177,6 +177,7 @@ static inline unsigned long ima_hash_key(u8 *digest) hook(FILE_CHECK) \ hook(MMAP_CHECK) \ hook(BPRM_CHECK) \ + hook(CREDS_CHECK) \ hook(POST_SETATTR) \ hook(MODULE_CHECK) \ hook(FIRMWARE_CHECK) \
@@ -191,8 +192,8 @@ enum ima_hooks { };/* LIM API function definitions */ -int ima_get_action (struct inode *inode,int mask, -enum ima_hooks func,int *pcr); +int ima_get_action (struct inode *inode,const struct cred *cred,u32 secid, +int mask,enum ima_hooks func,int *pcr);int ima_must_measure (struct inode *inode,int mask,enum ima_hooks func); int ima_collect_measurement(struct integrity_iint_cache *iint, struct file *file, void *buf, loff_t size,
@@ -212,8 +213,8 @@ void ima_free_template_entry(struct ima_template_entry *entry);const char *ima_d_path (const struct path *path,char **pathbuf,char *filename);/* IMA policy related functions */ -int ima_match_policy (struct inode *inode,enum ima_hooks func,int mask, -int flags,int *pcr); +int ima_match_policy (struct inode *inode,const struct cred *cred,u32 secid, +enum ima_hooks func,int mask,int flags,int *pcr);void ima_init_policy (void );void ima_update_policy (void );void ima_update_policy_flag (void );
security/integrity/ima/ima_api.c +6 -3
@@ -157,6 +157,8 @@ void ima_add_violation(struct file *file, const unsigned char *filename,/** * ima_get_action - appraise & measure decision based on policy. * @inode: pointer to inode to measure +* @cred: pointer to credentials structure to validate +* @secid: secid of the task being validated * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC, * MAY_APPEND) * @func: caller identifier
@@ -165,20 +167,21 @@ void ima_add_violation(struct file *file, const unsigned char *filename,* The policy is defined in terms of keypairs: * subj=, obj=, type=, func=, mask=, fsmagic= * subj,obj, and type: are LSM specific. -* func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK +* func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK * mask: contains the permission mask * fsmagic: hex value * * Returns IMA_MEASURE, IMA_APPRAISE mask. * */ -int ima_get_action (struct inode *inode,int mask,enum ima_hooks func,int *pcr) +int ima_get_action (struct inode *inode,const struct cred *cred,u32 secid, +int mask,enum ima_hooks func,int *pcr) {int flags =IMA_MEASURE |IMA_AUDIT |IMA_APPRAISE ; flags &= ima_policy_flag; -return ima_match_policy (inode, func, mask, flags, pcr); +return ima_match_policy (inode, cred, secid, func, mask, flags, pcr); }/*
security/integrity/ima/ima_appraise.c +12 -1
@@ -50,10 +50,14 @@ bool is_ima_appraise_enabled(void)*/ int ima_must_appraise (struct inode *inode,int mask,enum ima_hooks func) { +u32 secid; +if (!ima_appraise)return 0 ; -return ima_match_policy (inode, func, mask,IMA_APPRAISE ,NULL ); +security_task_getsecid (current, &secid); +return ima_match_policy (inode,current_cred (), secid, func, mask, +IMA_APPRAISE ,NULL ); } static int ima_fix_xattr(struct dentry *dentry,
@@ -86,6 +90,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,return iint->ima_mmap_status;case BPRM_CHECK :return iint->ima_bprm_status; +case CREDS_CHECK : +return iint->ima_creds_status;case FILE_CHECK :case POST_SETATTR :return iint->ima_file_status;
@@ -106,6 +112,8 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,case BPRM_CHECK : iint->ima_bprm_status = status;break ; +case CREDS_CHECK : + iint->ima_creds_status = status;case FILE_CHECK :case POST_SETATTR : iint->ima_file_status = status;
@@ -127,6 +135,9 @@ static void ima_cache_flags(struct integrity_iint_cache *iint,case BPRM_CHECK : iint->flags |= (IMA_BPRM_APPRAISED |IMA_APPRAISED );break ; +case CREDS_CHECK : + iint->flags |= (IMA_CREDS_APPRAISED |IMA_APPRAISED ); +break ;case FILE_CHECK :case POST_SETATTR : iint->flags |= (IMA_FILE_APPRAISED |IMA_APPRAISED );
security/integrity/ima/ima_main.c +32 -10
84 lines not on this page · raw message
security/integrity/ima/ima_policy.c +15 -8
@@ -241,16 +241,17 @@ static void ima_lsm_update_rules(void)* ima_match_rules - determine whether an inode matches the measure rule. * @rule: a pointer to a rule * @inode: a pointer to an inode +* @cred: a pointer to a credentials structure for user validation +* @secid: the secid of the task to be validated * @func: LIM hook identifier * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) * * Returns true on rule match, false on failure. */ static bool ima_match_rules (struct ima_rule_entry *rule,struct inode *inode, +const struct cred *cred,u32 secid,enum ima_hooks func,int mask) { -struct task_struct *tsk = current; -const struct cred *cred =current_cred ();int i; if ((rule->flags & IMA_FUNC) &&
@@ -285,7 +286,7 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,return false;for (i =0 ; i <MAX_LSM_RULES ; i++) {int rc =0 ; -u32 osid, sid; +u32 osid;int retried =0 ;if (!rule->lsm[i].rule)
@@ -305,8 +306,7 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,case LSM_SUBJ_USER :case LSM_SUBJ_ROLE :case LSM_SUBJ_TYPE : -security_task_getsecid (tsk, &sid); - rc = security_filter_rule_match(sid, + rc = security_filter_rule_match(secid, rule->lsm[i].type, Audit_equal, rule->lsm[i].rule,
@@ -339,6 +339,8 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)return IMA_MMAP_APPRAISE ;case BPRM_CHECK :return IMA_BPRM_APPRAISE ; +case CREDS_CHECK : +return IMA_CREDS_APPRAISE ;case FILE_CHECK :case POST_SETATTR :return IMA_FILE_APPRAISE ;
@@ -351,6 +353,9 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)/** * ima_match_policy - decision based on LSM and other conditions * @inode: pointer to an inode for which the policy decision is being made +* @cred: pointer to a credentials structure for which the policy decision is +* being made +* @secid: LSM secid of the task to be validated * @func: IMA hook identifier * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) * @pcr: set the pcr to extend
@@ -362,8 +367,8 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)* list when walking it. Reads are many orders of magnitude more numerous * than writes so ima_match_policy() is classical RCU candidate. */ -int ima_match_policy (struct inode *inode,enum ima_hooks func,int mask, -int flags,int *pcr) +int ima_match_policy (struct inode *inode,const struct cred *cred,u32 secid, +enum ima_hooks func,int mask,int flags,int *pcr) {struct ima_rule_entry *entry;int action =0 , actmask = flags | (flags <<1 );
@@ -374,7 +379,7 @@ int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,if (!(entry->action & actmask))continue ; -if (!ima_match_rules (entry, inode, func, mask)) +if (!ima_match_rules (entry, inode, cred, secid, func, mask))continue ; action |= entry->flags &IMA_ACTION_FLAGS ;
@@ -691,6 +696,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =MMAP_CHECK ; else if (strcmp(args[0].from, "BPRM_CHECK") == 0) entry->func = BPRM_CHECK; + else if (strcmp(args[0].from, "CREDS_CHECK") == 0) + entry->func = CREDS_CHECK; else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") == 0) entry->func = KEXEC_KERNEL_CHECK;
security/integrity/integrity.h +7 -2
@@ -48,10 +48,14 @@#define IMA_BPRM_APPRAISED 0x00002000#define IMA_READ_APPRAISE 0x00004000#define IMA_READ_APPRAISED 0x00008000 +#define IMA_CREDS_APPRAISE 0x00010000 +#define IMA_CREDS_APPRAISED 0x00020000#define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \ - IMA_BPRM_APPRAISE | IMA_READ_APPRAISE) + IMA_BPRM_APPRAISE | IMA_READ_APPRAISE | \ + IMA_CREDS_APPRAISE)#define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \ - IMA_BPRM_APPRAISED | IMA_READ_APPRAISED) + IMA_BPRM_APPRAISED | IMA_READ_APPRAISED | \ + IMA_CREDS_APPRAISED)enum evm_ima_xattr_type {IMA_XATTR_DIGEST =0x01 ,
@@ -108,6 +112,7 @@ struct integrity_iint_cache { enum integrity_status ima_mmap_status:4; enum integrity_status ima_bprm_status:4; enum integrity_status ima_read_status:4; + enum integrity_status ima_creds_status:4; enum integrity_status evm_status:4;struct ima_digest_data *ima_hash; };
Signature · 6 lines
2.16.0.rc0.223.g4a4ac83678-goog -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH V5 2/2] IMA: Support using new creds in appraisal policy
Quoted 9 lines · Matthew Garrett
The existing
BPRM_CHECKfunctionality in IMA validates against the credentials of the existing process, not any new credentials that the child process may transition to. Add an additionalCREDS_CHECKtarget and refactor IMA to pass the appropriate creds structure. Inima_bprm_check(), check with both the existing process credentials and the credentials that will be committed when the new process is started. This will not change behaviour unless the system policy is extended to includeCREDS_CHECKtargets -BPRM_CHECKwill continue to check the same credentials that it did previously.
Refactoring IMA to pass the creds structure all the way down is a generic solution, but if the CREDS_CHECK rule is only being called from ima_bprm_check(), "container_of" the bprm->file returns a pointer to the bprm structure. ?Perhaps you could limit the amount of refactoring needed based on the func.
I would prefer new arguments be added to process_measurement() only as a last resort.
Could you include in the patch description a simple method for testing this change?
thanks,
Mimi
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH V5 2/2] IMA: Support using new creds in appraisal policy
Quoted 11 lines · Mimi Zohar
Quoted 9 lines · Matthew Garrett
The existing
BPRM_CHECKfunctionality in IMA validates against the credentials of the existing process, not any new credentials that the child process may transition to. Add an additionalCREDS_CHECKtarget and refactor IMA to pass the appropriate creds structure. Inima_bprm_check(), check with both the existing process credentials and the credentials that will be committed when the new process is started. This will not change behaviour unless the system policy is extended to includeCREDS_CHECKtargets -BPRM_CHECKwill continue to check the same credentials that it did previously.
Quoted 5 lines
Refactoring IMA to pass the creds structure all the way down is a generic solution, but if the
CREDS_CHECKrule is only being called fromima_bprm_check(), "container_of" thebprm->filereturns a pointer to the bprm structure. Perhaps you could limit the amount of refactoring needed based on the func.
Hm. This would avoid adding an argument to process_measurement(), but we'd still need to pass additional information down through ima_get_action() in order to get the creds and secid right. It feels a little ugly to have process_measurement() recreate information rather than having the caller pass it in, but I'm not going to object.
Quoted 2 lines
Could you include in the patch description a simple method for testing this change?
Certainly.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH V5 2/2] IMA: Support using new creds in appraisal policy
Quoted 18 lines · Matthew Garrett
Quoted 11 lines · Mimi Zohar
Quoted 9 lines · Matthew Garrett
The existing
BPRM_CHECKfunctionality in IMA validates against the credentials of the existing process, not any new credentials that the child process may transition to. Add an additionalCREDS_CHECKtarget and refactor IMA to pass the appropriate creds structure. Inima_bprm_check(), check with both the existing process credentials and the credentials that will be committed when the new process is started. This will not change behaviour unless the system policy is extended to includeCREDS_CHECKtargets -BPRM_CHECKwill continue to check the same credentials that it did previously.Quoted 5 lines
Refactoring IMA to pass the creds structure all the way down is a generic solution, but if the
CREDS_CHECKrule is only being called fromima_bprm_check(), "container_of" thebprm->filereturns a pointer to the bprm structure. Perhaps you could limit the amount of refactoring needed based on the func.
Quoted 5 lines
Hm. This would avoid adding an argument to
process_measurement(), but we'd still need to pass additional information down throughima_get_action()in order to get the creds and secid right. It feels a little ugly to haveprocess_measurement()recreate information rather than having the caller pass it in, but I'm not going to object.
Agreed. ?Initially, I was thinking that only ima_match_rules() would use container_of, but the very first thing process_measurement() does is get the inode from the file. ?Everything afterwards passes the inode.
Quoted 4 lines
Quoted 2 lines
Could you include in the patch description a simple method for testing this change?
Certainly.
Thanks!
Mimi
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html