Skip to content

Commit c7d0936

Browse files
Mimi Zohartorvalds
Mimi Zohar
authored andcommitted
ima: support restoring multiple template formats
The configured IMA measurement list template format can be replaced at runtime on the boot command line, including a custom template format. This patch adds support for restoring a measuremement list containing multiple builtin/custom template formats. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Mimi Zohar <[email protected]> Acked-by: Dmitry Kasatkin <[email protected]> Cc: Thiago Jung Bauermann <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Cc: Andreas Steffen <[email protected]> Cc: Josh Sklar <[email protected]> Cc: Dave Young <[email protected]> Cc: Vivek Goyal <[email protected]> Cc: Baoquan He <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Stewart Smith <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3f23d62 commit c7d0936

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

security/integrity/ima/ima_template.c

+49-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,14 @@ static int template_desc_init_fields(const char *template_fmt,
155155
{
156156
const char *template_fmt_ptr;
157157
struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
158-
int template_num_fields = template_fmt_size(template_fmt);
158+
int template_num_fields;
159159
int i, len;
160160

161+
if (num_fields && *num_fields > 0) /* already initialized? */
162+
return 0;
163+
164+
template_num_fields = template_fmt_size(template_fmt);
165+
161166
if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
162167
pr_err("format string '%s' contains too many fields\n",
163168
template_fmt);
@@ -236,6 +241,34 @@ int __init ima_init_template(void)
236241
return result;
237242
}
238243

244+
static struct ima_template_desc *restore_template_fmt(char *template_name)
245+
{
246+
struct ima_template_desc *template_desc = NULL;
247+
int ret;
248+
249+
ret = template_desc_init_fields(template_name, NULL, NULL);
250+
if (ret < 0) {
251+
pr_err("attempting to initialize the template \"%s\" failed\n",
252+
template_name);
253+
goto out;
254+
}
255+
256+
template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
257+
if (!template_desc)
258+
goto out;
259+
260+
template_desc->name = "";
261+
template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
262+
if (!template_desc->fmt)
263+
goto out;
264+
265+
spin_lock(&template_list);
266+
list_add_tail_rcu(&template_desc->list, &defined_templates);
267+
spin_unlock(&template_list);
268+
out:
269+
return template_desc;
270+
}
271+
239272
static int ima_restore_template_data(struct ima_template_desc *template_desc,
240273
void *template_data,
241274
int template_data_size,
@@ -373,10 +406,23 @@ int ima_restore_measurement_list(loff_t size, void *buf)
373406
break;
374407
}
375408

376-
/* get template format */
377409
template_desc = lookup_template_desc(template_name);
378410
if (!template_desc) {
379-
pr_err("template \"%s\" not found\n", template_name);
411+
template_desc = restore_template_fmt(template_name);
412+
if (!template_desc)
413+
break;
414+
}
415+
416+
/*
417+
* Only the running system's template format is initialized
418+
* on boot. As needed, initialize the other template formats.
419+
*/
420+
ret = template_desc_init_fields(template_desc->fmt,
421+
&(template_desc->fields),
422+
&(template_desc->num_fields));
423+
if (ret < 0) {
424+
pr_err("attempting to restore the template fmt \"%s\" \
425+
failed\n", template_desc->fmt);
380426
ret = -EINVAL;
381427
break;
382428
}

0 commit comments

Comments
 (0)