Skip to content

Commit c2426d2

Browse files
Roberto SassuMimi Zohar
authored andcommitted
ima: added support for new kernel cmdline parameter ima_template_fmt
This patch allows users to provide a custom template format through the new kernel command line parameter 'ima_template_fmt'. If the supplied format is not valid, IMA uses the default template descriptor. Changelog: - v3: - added check for 'fields' and 'num_fields' in template_desc_init_fields() (suggested by Mimi Zohar) - v2: - using template_desc_init_fields() to validate a format string (Roberto Sassu) - updated documentation by stating that only the chosen template descriptor is initialized (Roberto Sassu) - v1: - simplified code of ima_template_fmt_setup() (Roberto Sassu, suggested by Mimi Zohar) Signed-off-by: Roberto Sassu <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 1bd7fac commit c2426d2

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

Documentation/kernel-parameters.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
13181318
Formats: { "ima" | "ima-ng" }
13191319
Default: "ima-ng"
13201320

1321+
ima_template_fmt=
1322+
[IMA] Define a custom template format.
1323+
Format: { "field1|...|fieldN" }
1324+
13211325
ima.ahash_minsize= [IMA] Minimum file size for asynchronous hash usage
13221326
Format: <min_file_size>
13231327
Set the minimal file size for using asynchronous hash.

Documentation/security/IMA-templates.txt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,22 @@ Managing templates with these structures is very simple. To support
2727
a new data type, developers define the field identifier and implement
2828
two functions, init() and show(), respectively to generate and display
2929
measurement entries. Defining a new template descriptor requires
30-
specifying the template format, a string of field identifiers separated
31-
by the '|' character. While in the current implementation it is possible
32-
to define new template descriptors only by adding their definition in the
33-
template specific code (ima_template.c), in a future version it will be
34-
possible to register a new template on a running kernel by supplying to IMA
35-
the desired format string. In this version, IMA initializes at boot time
36-
all defined template descriptors by translating the format into an array
37-
of template fields structures taken from the set of the supported ones.
30+
specifying the template format (a string of field identifiers separated
31+
by the '|' character) through the 'ima_template_fmt' kernel command line
32+
parameter. At boot time, IMA initializes the chosen template descriptor
33+
by translating the format into an array of template fields structures taken
34+
from the set of the supported ones.
3835

3936
After the initialization step, IMA will call ima_alloc_init_template()
4037
(new function defined within the patches for the new template management
4138
mechanism) to generate a new measurement entry by using the template
4239
descriptor chosen through the kernel configuration or through the newly
43-
introduced 'ima_template=' kernel command line parameter. It is during this
44-
phase that the advantages of the new architecture are clearly shown:
45-
the latter function will not contain specific code to handle a given template
46-
but, instead, it simply calls the init() method of the template fields
47-
associated to the chosen template descriptor and store the result (pointer
48-
to allocated data and data length) in the measurement entry structure.
40+
introduced 'ima_template' and 'ima_template_fmt' kernel command line parameters.
41+
It is during this phase that the advantages of the new architecture are
42+
clearly shown: the latter function will not contain specific code to handle
43+
a given template but, instead, it simply calls the init() method of the template
44+
fields associated to the chosen template descriptor and store the result
45+
(pointer to allocated data and data length) in the measurement entry structure.
4946

5047
The same mechanism is employed to display measurements entries.
5148
The functions ima[_ascii]_measurements_show() retrieve, for each entry,
@@ -86,4 +83,6 @@ currently the following methods are supported:
8683
- select a template descriptor among those supported in the kernel
8784
configuration ('ima-ng' is the default choice);
8885
- specify a template descriptor name from the kernel command line through
89-
the 'ima_template=' parameter.
86+
the 'ima_template=' parameter;
87+
- register a new template descriptor with custom format through the kernel
88+
command line parameter 'ima_template_fmt='.

security/integrity/ima/ima_template.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static struct ima_template_desc defined_templates[] = {
2424
{.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
2525
{.name = "ima-ng", .fmt = "d-ng|n-ng"},
2626
{.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
27+
{.name = "", .fmt = ""}, /* placeholder for a custom format */
2728
};
2829

2930
static struct ima_template_field supported_fields[] = {
@@ -41,12 +42,18 @@ static struct ima_template_field supported_fields[] = {
4142

4243
static struct ima_template_desc *ima_template;
4344
static struct ima_template_desc *lookup_template_desc(const char *name);
45+
static int template_desc_init_fields(const char *template_fmt,
46+
struct ima_template_field ***fields,
47+
int *num_fields);
4448

4549
static int __init ima_template_setup(char *str)
4650
{
4751
struct ima_template_desc *template_desc;
4852
int template_len = strlen(str);
4953

54+
if (ima_template)
55+
return 1;
56+
5057
/*
5158
* Verify that a template with the supplied name exists.
5259
* If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
@@ -73,6 +80,25 @@ static int __init ima_template_setup(char *str)
7380
}
7481
__setup("ima_template=", ima_template_setup);
7582

83+
static int __init ima_template_fmt_setup(char *str)
84+
{
85+
int num_templates = ARRAY_SIZE(defined_templates);
86+
87+
if (ima_template)
88+
return 1;
89+
90+
if (template_desc_init_fields(str, NULL, NULL) < 0) {
91+
pr_err("format string '%s' not valid, using template %s\n",
92+
str, CONFIG_IMA_DEFAULT_TEMPLATE);
93+
return 1;
94+
}
95+
96+
defined_templates[num_templates - 1].fmt = str;
97+
ima_template = defined_templates + num_templates - 1;
98+
return 1;
99+
}
100+
__setup("ima_template_fmt=", ima_template_fmt_setup);
101+
76102
static struct ima_template_desc *lookup_template_desc(const char *name)
77103
{
78104
int i;
@@ -146,12 +172,15 @@ static int template_desc_init_fields(const char *template_fmt,
146172
}
147173
}
148174

149-
*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
150-
if (*fields == NULL)
151-
return -ENOMEM;
175+
if (fields && num_fields) {
176+
*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
177+
if (*fields == NULL)
178+
return -ENOMEM;
179+
180+
memcpy(*fields, found_fields, i * sizeof(*fields));
181+
*num_fields = i;
182+
}
152183

153-
memcpy(*fields, found_fields, i * sizeof(*fields));
154-
*num_fields = i;
155184
return 0;
156185
}
157186

0 commit comments

Comments
 (0)