Skip to content

Commit 19453ce

Browse files
Matthew Garrettmimizohar
authored andcommitted
IMA: support for per policy rule template formats
Admins may wish to log different measurements using different IMA templates. Add support for overriding the default template on a per-rule basis. Inspired-by: Roberto Sassu <roberto.sassu@huawei.com> Signed-off-by: Matthew Garrett <mjg59@google.com> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 8c65578 commit 19453ce

8 files changed

Lines changed: 79 additions & 29 deletions

File tree

‎Documentation/ABI/testing/ima_policy‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Description:
2424
[euid=] [fowner=] [fsname=]]
2525
lsm: [[subj_user=] [subj_role=] [subj_type=]
2626
[obj_user=] [obj_role=] [obj_type=]]
27-
option: [[appraise_type=]] [permit_directio]
28-
27+
option: [[appraise_type=]] [template=] [permit_directio]
2928
base: func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK]
3029
[FIRMWARE_CHECK]
3130
[KEXEC_KERNEL_CHECK] [KEXEC_INITRAMFS_CHECK]
@@ -38,6 +37,8 @@ Description:
3837
fowner:= decimal value
3938
lsm: are LSM specific
4039
option: appraise_type:= [imasig]
40+
template:= name of a defined IMA template type
41+
(eg, ima-ng). Only valid when action is "measure".
4142
pcr:= decimal value
4243

4344
default policy:

‎security/integrity/ima/ima.h‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
146146
int ima_init_crypto(void);
147147
void ima_putc(struct seq_file *m, void *data, int datalen);
148148
void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
149+
int template_desc_init_fields(const char *template_fmt,
150+
const struct ima_template_field ***fields,
151+
int *num_fields);
149152
struct ima_template_desc *ima_template_desc_current(void);
153+
struct ima_template_desc *lookup_template_desc(const char *name);
150154
int ima_restore_measurement_entry(struct ima_template_entry *entry);
151155
int ima_restore_measurement_list(loff_t bufsize, void *buf);
152156
int ima_measurements_show(struct seq_file *m, void *v);
@@ -195,19 +199,22 @@ enum ima_hooks {
195199

196200
/* LIM API function definitions */
197201
int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
198-
int mask, enum ima_hooks func, int *pcr);
202+
int mask, enum ima_hooks func, int *pcr,
203+
struct ima_template_desc **template_desc);
199204
int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
200205
int ima_collect_measurement(struct integrity_iint_cache *iint,
201206
struct file *file, void *buf, loff_t size,
202207
enum hash_algo algo);
203208
void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
204209
const unsigned char *filename,
205210
struct evm_ima_xattr_data *xattr_value,
206-
int xattr_len, int pcr);
211+
int xattr_len, int pcr,
212+
struct ima_template_desc *template_desc);
207213
void ima_audit_measurement(struct integrity_iint_cache *iint,
208214
const unsigned char *filename);
209215
int ima_alloc_init_template(struct ima_event_data *event_data,
210-
struct ima_template_entry **entry);
216+
struct ima_template_entry **entry,
217+
struct ima_template_desc *template_desc);
211218
int ima_store_template(struct ima_template_entry *entry, int violation,
212219
struct inode *inode,
213220
const unsigned char *filename, int pcr);
@@ -216,7 +223,8 @@ const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
216223

217224
/* IMA policy related functions */
218225
int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
219-
enum ima_hooks func, int mask, int flags, int *pcr);
226+
enum ima_hooks func, int mask, int flags, int *pcr,
227+
struct ima_template_desc **template_desc);
220228
void ima_init_policy(void);
221229
void ima_update_policy(void);
222230
void ima_update_policy_flag(void);

‎security/integrity/ima/ima_api.c‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ void ima_free_template_entry(struct ima_template_entry *entry)
3838
* ima_alloc_init_template - create and initialize a new template entry
3939
*/
4040
int ima_alloc_init_template(struct ima_event_data *event_data,
41-
struct ima_template_entry **entry)
41+
struct ima_template_entry **entry,
42+
struct ima_template_desc *desc)
4243
{
43-
struct ima_template_desc *template_desc = ima_template_desc_current();
44+
struct ima_template_desc *template_desc;
4445
int i, result = 0;
4546

47+
if (desc)
48+
template_desc = desc;
49+
else
50+
template_desc = ima_template_desc_current();
51+
4652
*entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
4753
sizeof(struct ima_field_data), GFP_NOFS);
4854
if (!*entry)
@@ -143,7 +149,7 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
143149
/* can overflow, only indicator */
144150
atomic_long_inc(&ima_htable.violations);
145151

146-
result = ima_alloc_init_template(&event_data, &entry);
152+
result = ima_alloc_init_template(&event_data, &entry, NULL);
147153
if (result < 0) {
148154
result = -ENOMEM;
149155
goto err_out;
@@ -166,6 +172,7 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
166172
* MAY_APPEND)
167173
* @func: caller identifier
168174
* @pcr: pointer filled in if matched measure policy sets pcr=
175+
* @template_desc: pointer filled in if matched measure policy sets template=
169176
*
170177
* The policy is defined in terms of keypairs:
171178
* subj=, obj=, type=, func=, mask=, fsmagic=
@@ -178,13 +185,15 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
178185
*
179186
*/
180187
int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
181-
int mask, enum ima_hooks func, int *pcr)
188+
int mask, enum ima_hooks func, int *pcr,
189+
struct ima_template_desc **template_desc)
182190
{
183191
int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
184192

185193
flags &= ima_policy_flag;
186194

187-
return ima_match_policy(inode, cred, secid, func, mask, flags, pcr);
195+
return ima_match_policy(inode, cred, secid, func, mask, flags, pcr,
196+
template_desc);
188197
}
189198

190199
/*
@@ -279,7 +288,8 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
279288
void ima_store_measurement(struct integrity_iint_cache *iint,
280289
struct file *file, const unsigned char *filename,
281290
struct evm_ima_xattr_data *xattr_value,
282-
int xattr_len, int pcr)
291+
int xattr_len, int pcr,
292+
struct ima_template_desc *template_desc)
283293
{
284294
static const char op[] = "add_template_measure";
285295
static const char audit_cause[] = "ENOMEM";
@@ -296,7 +306,7 @@ void ima_store_measurement(struct integrity_iint_cache *iint,
296306
if (iint->measured_pcrs & (0x1 << pcr))
297307
return;
298308

299-
result = ima_alloc_init_template(&event_data, &entry);
309+
result = ima_alloc_init_template(&event_data, &entry, template_desc);
300310
if (result < 0) {
301311
integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
302312
op, audit_cause, result, 0);

‎security/integrity/ima/ima_appraise.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
5757

5858
security_task_getsecid(current, &secid);
5959
return ima_match_policy(inode, current_cred(), secid, func, mask,
60-
IMA_APPRAISE | IMA_HASH, NULL);
60+
IMA_APPRAISE | IMA_HASH, NULL, NULL);
6161
}
6262

6363
static int ima_fix_xattr(struct dentry *dentry,

‎security/integrity/ima/ima_init.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static int __init ima_add_boot_aggregate(void)
7272
}
7373
}
7474

75-
result = ima_alloc_init_template(&event_data, &entry);
75+
result = ima_alloc_init_template(&event_data, &entry, NULL);
7676
if (result < 0) {
7777
audit_cause = "alloc_entry";
7878
goto err_out;

‎security/integrity/ima/ima_main.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static int process_measurement(struct file *file, const struct cred *cred,
199199
{
200200
struct inode *inode = file_inode(file);
201201
struct integrity_iint_cache *iint = NULL;
202-
struct ima_template_desc *template_desc;
202+
struct ima_template_desc *template_desc = NULL;
203203
char *pathbuf = NULL;
204204
char filename[NAME_MAX];
205205
const char *pathname = NULL;
@@ -217,7 +217,8 @@ static int process_measurement(struct file *file, const struct cred *cred,
217217
* bitmask based on the appraise/audit/measurement policy.
218218
* Included is the appraise submask.
219219
*/
220-
action = ima_get_action(inode, cred, secid, mask, func, &pcr);
220+
action = ima_get_action(inode, cred, secid, mask, func, &pcr,
221+
&template_desc);
221222
violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
222223
(ima_policy_flag & IMA_MEASURE));
223224
if (!action && !violation_check)
@@ -304,7 +305,6 @@ static int process_measurement(struct file *file, const struct cred *cred,
304305
goto out_locked;
305306
}
306307

307-
template_desc = ima_template_desc_current();
308308
if ((action & IMA_APPRAISE_SUBMASK) ||
309309
strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
310310
/* read 'security.ima' */
@@ -321,7 +321,8 @@ static int process_measurement(struct file *file, const struct cred *cred,
321321

322322
if (action & IMA_MEASURE)
323323
ima_store_measurement(iint, file, pathname,
324-
xattr_value, xattr_len, pcr);
324+
xattr_value, xattr_len, pcr,
325+
template_desc);
325326
if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
326327
inode_lock(inode);
327328
rc = ima_appraise_measurement(func, iint, file, pathname,

‎security/integrity/ima/ima_policy.c‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
484486
int 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

758766
static 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)

‎security/integrity/ima/ima_template.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ static const struct ima_template_field supported_fields[] = {
4747
#define MAX_TEMPLATE_NAME_LEN 15
4848

4949
static struct ima_template_desc *ima_template;
50-
static struct ima_template_desc *lookup_template_desc(const char *name);
51-
static int template_desc_init_fields(const char *template_fmt,
52-
const struct ima_template_field ***fields,
53-
int *num_fields);
5450

5551
static int __init ima_template_setup(char *str)
5652
{
@@ -108,7 +104,7 @@ static int __init ima_template_fmt_setup(char *str)
108104
}
109105
__setup("ima_template_fmt=", ima_template_fmt_setup);
110106

111-
static struct ima_template_desc *lookup_template_desc(const char *name)
107+
struct ima_template_desc *lookup_template_desc(const char *name)
112108
{
113109
struct ima_template_desc *template_desc;
114110
int found = 0;
@@ -153,9 +149,9 @@ static int template_fmt_size(const char *template_fmt)
153149
return j + 1;
154150
}
155151

156-
static int template_desc_init_fields(const char *template_fmt,
157-
const struct ima_template_field ***fields,
158-
int *num_fields)
152+
int template_desc_init_fields(const char *template_fmt,
153+
const struct ima_template_field ***fields,
154+
int *num_fields)
159155
{
160156
const char *template_fmt_ptr;
161157
const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];

0 commit comments

Comments
 (0)