@@ -80,6 +80,7 @@ struct ima_rule_entry {
8080 int type ; /* audit type */
8181 } lsm [MAX_LSM_RULES ];
8282 char * fsname ;
83+ struct ima_template_desc * template ;
8384};
8485
8586/*
@@ -473,6 +474,7 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
473474 * @func: IMA hook identifier
474475 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
475476 * @pcr: set the pcr to extend
477+ * @template_desc: the template that should be used for this rule
476478 *
477479 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
478480 * conditions.
@@ -482,7 +484,8 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
482484 * than writes so ima_match_policy() is classical RCU candidate.
483485 */
484486int ima_match_policy (struct inode * inode , const struct cred * cred , u32 secid ,
485- enum ima_hooks func , int mask , int flags , int * pcr )
487+ enum ima_hooks func , int mask , int flags , int * pcr ,
488+ struct ima_template_desc * * template_desc )
486489{
487490 struct ima_rule_entry * entry ;
488491 int action = 0 , actmask = flags | (flags << 1 );
@@ -514,6 +517,11 @@ int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
514517 if ((pcr ) && (entry -> flags & IMA_PCR ))
515518 * pcr = entry -> pcr ;
516519
520+ if (template_desc && entry -> template )
521+ * template_desc = entry -> template ;
522+ else if (template_desc )
523+ * template_desc = ima_template_desc_current ();
524+
517525 if (!actmask )
518526 break ;
519527 }
@@ -752,7 +760,7 @@ enum {
752760 Opt_uid_gt , Opt_euid_gt , Opt_fowner_gt ,
753761 Opt_uid_lt , Opt_euid_lt , Opt_fowner_lt ,
754762 Opt_appraise_type , Opt_permit_directio ,
755- Opt_pcr , Opt_err
763+ Opt_pcr , Opt_template , Opt_err
756764};
757765
758766static const match_table_t policy_tokens = {
@@ -786,6 +794,7 @@ static const match_table_t policy_tokens = {
786794 {Opt_appraise_type , "appraise_type=%s" },
787795 {Opt_permit_directio , "permit_directio" },
788796 {Opt_pcr , "pcr=%s" },
797+ {Opt_template , "template=%s" },
789798 {Opt_err , NULL }
790799};
791800
@@ -839,6 +848,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
839848 char * from ;
840849 char * p ;
841850 bool uid_token ;
851+ struct ima_template_desc * template_desc ;
842852 int result = 0 ;
843853
844854 ab = integrity_audit_log_start (audit_context (), GFP_KERNEL ,
@@ -1134,6 +1144,28 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
11341144 else
11351145 entry -> flags |= IMA_PCR ;
11361146
1147+ break ;
1148+ case Opt_template :
1149+ ima_log_string (ab , "template" , args [0 ].from );
1150+ if (entry -> action != MEASURE ) {
1151+ result = - EINVAL ;
1152+ break ;
1153+ }
1154+ template_desc = lookup_template_desc (args [0 ].from );
1155+ if (!template_desc || entry -> template ) {
1156+ result = - EINVAL ;
1157+ break ;
1158+ }
1159+
1160+ /*
1161+ * template_desc_init_fields() does nothing if
1162+ * the template is already initialised, so
1163+ * it's safe to do this unconditionally
1164+ */
1165+ template_desc_init_fields (template_desc -> fmt ,
1166+ & (template_desc -> fields ),
1167+ & (template_desc -> num_fields ));
1168+ entry -> template = template_desc ;
11371169 break ;
11381170 case Opt_err :
11391171 ima_log_string (ab , "UNKNOWN" , p );
@@ -1410,6 +1442,8 @@ int ima_policy_show(struct seq_file *m, void *v)
14101442 }
14111443 }
14121444 }
1445+ if (entry -> template )
1446+ seq_printf (m , "template=%s " , entry -> template -> name );
14131447 if (entry -> flags & IMA_DIGSIG_REQUIRED )
14141448 seq_puts (m , "appraise_type=imasig " );
14151449 if (entry -> flags & IMA_PERMIT_DIRECTIO )
0 commit comments