2019-06-01 08:08:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-06-07 10:16:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Politecnico di Torino, Italy
|
2020-07-05 21:45:12 +00:00
|
|
|
* TORSEC group -- https://security.polito.it
|
2013-06-07 10:16:30 +00:00
|
|
|
*
|
|
|
|
* Author: Roberto Sassu <roberto.sassu@polito.it>
|
|
|
|
*
|
|
|
|
* File: ima_template_lib.c
|
|
|
|
* Library of supported template fields.
|
|
|
|
*/
|
2013-06-07 10:16:32 +00:00
|
|
|
|
2013-06-07 10:16:30 +00:00
|
|
|
#include "ima_template_lib.h"
|
2021-05-14 15:27:52 +00:00
|
|
|
#include <linux/xattr.h>
|
2021-06-01 08:23:38 +00:00
|
|
|
#include <linux/evm.h>
|
2013-06-07 10:16:30 +00:00
|
|
|
|
2013-06-07 10:16:32 +00:00
|
|
|
static bool ima_template_hash_algo_allowed(u8 algo)
|
|
|
|
{
|
|
|
|
if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum data_formats {
|
|
|
|
DATA_FMT_DIGEST = 0,
|
|
|
|
DATA_FMT_DIGEST_WITH_ALGO,
|
2021-12-23 17:29:56 +00:00
|
|
|
DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
|
2013-07-23 15:15:00 +00:00
|
|
|
DATA_FMT_STRING,
|
2021-05-28 07:38:06 +00:00
|
|
|
DATA_FMT_HEX,
|
|
|
|
DATA_FMT_UINT
|
2013-06-07 10:16:32 +00:00
|
|
|
};
|
|
|
|
|
2021-12-23 17:29:56 +00:00
|
|
|
enum digest_type {
|
|
|
|
DIGEST_TYPE_IMA,
|
2021-12-23 17:29:56 +00:00
|
|
|
DIGEST_TYPE_VERITY,
|
2021-12-23 17:29:56 +00:00
|
|
|
DIGEST_TYPE__LAST
|
|
|
|
};
|
|
|
|
|
2021-12-23 17:29:56 +00:00
|
|
|
#define DIGEST_TYPE_NAME_LEN_MAX 7 /* including NUL */
|
2021-12-23 17:29:56 +00:00
|
|
|
static const char * const digest_type_name[DIGEST_TYPE__LAST] = {
|
2021-12-23 17:29:56 +00:00
|
|
|
[DIGEST_TYPE_IMA] = "ima",
|
|
|
|
[DIGEST_TYPE_VERITY] = "verity"
|
2021-12-23 17:29:56 +00:00
|
|
|
};
|
|
|
|
|
2013-06-07 10:16:30 +00:00
|
|
|
static int ima_write_template_field_data(const void *data, const u32 datalen,
|
|
|
|
enum data_formats datafmt,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
u8 *buf, *buf_ptr;
|
2014-02-03 12:56:05 +00:00
|
|
|
u32 buflen = datalen;
|
2013-06-07 10:16:30 +00:00
|
|
|
|
2014-02-03 12:56:05 +00:00
|
|
|
if (datafmt == DATA_FMT_STRING)
|
2013-06-07 10:16:30 +00:00
|
|
|
buflen = datalen + 1;
|
|
|
|
|
|
|
|
buf = kzalloc(buflen, GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(buf, data, datalen);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace all space characters with underscore for event names and
|
|
|
|
* strings. This avoid that, during the parsing of a measurements list,
|
|
|
|
* filenames with spaces or that end with the suffix ' (deleted)' are
|
|
|
|
* split into multiple template fields (the space is the delimitator
|
|
|
|
* character for measurements lists in ASCII format).
|
|
|
|
*/
|
2014-02-03 12:56:05 +00:00
|
|
|
if (datafmt == DATA_FMT_STRING) {
|
2013-06-07 10:16:30 +00:00
|
|
|
for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
|
|
|
|
if (*buf_ptr == ' ')
|
|
|
|
*buf_ptr = '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
field_data->data = buf;
|
|
|
|
field_data->len = buflen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ima_show_template_data_ascii(struct seq_file *m,
|
|
|
|
enum ima_show_type show,
|
|
|
|
enum data_formats datafmt,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2015-06-11 15:54:42 +00:00
|
|
|
u8 *buf_ptr = field_data->data;
|
|
|
|
u32 buflen = field_data->len;
|
2013-06-07 10:16:32 +00:00
|
|
|
|
2013-06-07 10:16:30 +00:00
|
|
|
switch (datafmt) {
|
2021-12-23 17:29:56 +00:00
|
|
|
case DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
|
2013-06-07 10:16:32 +00:00
|
|
|
case DATA_FMT_DIGEST_WITH_ALGO:
|
2021-12-23 17:29:56 +00:00
|
|
|
buf_ptr = strrchr(field_data->data, ':');
|
2013-06-07 10:16:32 +00:00
|
|
|
if (buf_ptr != field_data->data)
|
|
|
|
seq_printf(m, "%s", field_data->data);
|
|
|
|
|
|
|
|
/* skip ':' and '\0' */
|
|
|
|
buf_ptr += 2;
|
|
|
|
buflen -= buf_ptr - field_data->data;
|
2020-08-23 22:36:59 +00:00
|
|
|
fallthrough;
|
2013-06-07 10:16:30 +00:00
|
|
|
case DATA_FMT_DIGEST:
|
2013-07-23 15:15:00 +00:00
|
|
|
case DATA_FMT_HEX:
|
|
|
|
if (!buflen)
|
|
|
|
break;
|
2013-06-07 10:16:32 +00:00
|
|
|
ima_print_digest(m, buf_ptr, buflen);
|
2013-06-07 10:16:30 +00:00
|
|
|
break;
|
|
|
|
case DATA_FMT_STRING:
|
2013-06-07 10:16:32 +00:00
|
|
|
seq_printf(m, "%s", buf_ptr);
|
2013-06-07 10:16:30 +00:00
|
|
|
break;
|
2021-05-28 07:38:06 +00:00
|
|
|
case DATA_FMT_UINT:
|
|
|
|
switch (field_data->len) {
|
|
|
|
case sizeof(u8):
|
|
|
|
seq_printf(m, "%u", *(u8 *)buf_ptr);
|
|
|
|
break;
|
|
|
|
case sizeof(u16):
|
|
|
|
if (ima_canonical_fmt)
|
|
|
|
seq_printf(m, "%u",
|
2021-06-08 12:31:21 +00:00
|
|
|
le16_to_cpu(*(__le16 *)buf_ptr));
|
2021-05-28 07:38:06 +00:00
|
|
|
else
|
|
|
|
seq_printf(m, "%u", *(u16 *)buf_ptr);
|
|
|
|
break;
|
|
|
|
case sizeof(u32):
|
|
|
|
if (ima_canonical_fmt)
|
|
|
|
seq_printf(m, "%u",
|
2021-06-08 12:31:21 +00:00
|
|
|
le32_to_cpu(*(__le32 *)buf_ptr));
|
2021-05-28 07:38:06 +00:00
|
|
|
else
|
|
|
|
seq_printf(m, "%u", *(u32 *)buf_ptr);
|
|
|
|
break;
|
|
|
|
case sizeof(u64):
|
|
|
|
if (ima_canonical_fmt)
|
|
|
|
seq_printf(m, "%llu",
|
2021-06-08 12:31:21 +00:00
|
|
|
le64_to_cpu(*(__le64 *)buf_ptr));
|
2021-05-28 07:38:06 +00:00
|
|
|
else
|
|
|
|
seq_printf(m, "%llu", *(u64 *)buf_ptr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-06-07 20:49:34 +00:00
|
|
|
break;
|
2013-06-07 10:16:30 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ima_show_template_data_binary(struct seq_file *m,
|
|
|
|
enum ima_show_type show,
|
|
|
|
enum data_formats datafmt,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2014-02-03 12:56:04 +00:00
|
|
|
u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
|
|
|
|
strlen(field_data->data) : field_data->len;
|
|
|
|
|
2016-12-20 00:22:57 +00:00
|
|
|
if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
|
2021-06-08 12:31:22 +00:00
|
|
|
u32 field_len = !ima_canonical_fmt ?
|
|
|
|
len : (__force u32)cpu_to_le32(len);
|
2016-12-20 00:22:57 +00:00
|
|
|
|
|
|
|
ima_putc(m, &field_len, sizeof(field_len));
|
|
|
|
}
|
2013-11-08 18:21:40 +00:00
|
|
|
|
2014-02-03 12:56:04 +00:00
|
|
|
if (!len)
|
2013-06-07 10:16:30 +00:00
|
|
|
return;
|
2013-11-08 18:21:40 +00:00
|
|
|
|
2014-02-03 12:56:04 +00:00
|
|
|
ima_putc(m, field_data->data, len);
|
2013-06-07 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ima_show_template_field_data(struct seq_file *m,
|
|
|
|
enum ima_show_type show,
|
|
|
|
enum data_formats datafmt,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
switch (show) {
|
|
|
|
case IMA_SHOW_ASCII:
|
|
|
|
ima_show_template_data_ascii(m, show, datafmt, field_data);
|
|
|
|
break;
|
|
|
|
case IMA_SHOW_BINARY:
|
2013-11-08 18:21:40 +00:00
|
|
|
case IMA_SHOW_BINARY_NO_FIELD_LEN:
|
2014-02-03 12:56:04 +00:00
|
|
|
case IMA_SHOW_BINARY_OLD_STRING_FMT:
|
2013-06-07 10:16:30 +00:00
|
|
|
ima_show_template_data_binary(m, show, datafmt, field_data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:16:32 +00:00
|
|
|
void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
|
|
|
|
field_data);
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:29:56 +00:00
|
|
|
void ima_show_template_digest_ngv2(struct seq_file *m, enum ima_show_type show,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
ima_show_template_field_data(m, show,
|
|
|
|
DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
|
|
|
|
field_data);
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:16:30 +00:00
|
|
|
void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:15:00 +00:00
|
|
|
void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
|
|
|
|
}
|
|
|
|
|
2019-06-24 06:23:30 +00:00
|
|
|
void ima_show_template_buf(struct seq_file *m, enum ima_show_type show,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
|
|
|
|
}
|
|
|
|
|
2021-05-28 07:38:06 +00:00
|
|
|
void ima_show_template_uint(struct seq_file *m, enum ima_show_type show,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
ima_show_template_field_data(m, show, DATA_FMT_UINT, field_data);
|
|
|
|
}
|
|
|
|
|
ima: introduce ima_parse_buf()
ima_parse_buf() takes as input the buffer start and end pointers, and
stores the result in a static array of ima_field_data structures,
where the len field contains the length parsed from the buffer, and
the data field contains the address of the buffer just after the length.
Optionally, the function returns the current value of the buffer pointer
and the number of array elements written.
A bitmap has been added as parameter of ima_parse_buf() to handle
the cases where the length is not prepended to data. Each bit corresponds
to an element of the ima_field_data array. If a bit is set, the length
is not parsed from the buffer, but is read from the corresponding element
of the array (the length must be set before calling the function).
ima_parse_buf() can perform three checks upon request by callers,
depending on the enforce mask passed to it:
- ENFORCE_FIELDS: matching of number of fields (length-data combination)
- there must be enough data in the buffer to parse the number of fields
requested (output: current value of buffer pointer)
- ENFORCE_BUFEND: matching of buffer end
- the ima_field_data array must be large enough to contain lengths and
data pointers for the amount of data requested (output: number
of fields written)
- ENFORCE_FIELDS | ENFORCE_BUFEND: matching of both
Use cases
- measurement entry header: ENFORCE_FIELDS | ENFORCE_BUFEND
- four fields must be parsed: pcr, digest, template name, template data
- ENFORCE_BUFEND is enforced only for the last measurement entry
- template digest (Crypto Agile): ENFORCE_BUFEND
- since only the total template digest length is known, the function
parses length-data combinations until the buffer end is reached
- template data: ENFORCE_FIELDS | ENFORCE_BUFEND
- since the number of fields and the total template data length
are known, the function can perform both checks
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2017-05-16 12:53:41 +00:00
|
|
|
/**
|
|
|
|
* ima_parse_buf() - Parses lengths and data from an input buffer
|
|
|
|
* @bufstartp: Buffer start address.
|
|
|
|
* @bufendp: Buffer end address.
|
|
|
|
* @bufcurp: Pointer to remaining (non-parsed) data.
|
|
|
|
* @maxfields: Length of fields array.
|
|
|
|
* @fields: Array containing lengths and pointers of parsed data.
|
|
|
|
* @curfields: Number of array items containing parsed data.
|
|
|
|
* @len_mask: Bitmap (if bit is set, data length should not be parsed).
|
|
|
|
* @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.
|
|
|
|
* @bufname: String identifier of the input buffer.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -EINVAL on error.
|
|
|
|
*/
|
|
|
|
int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
|
|
|
|
int maxfields, struct ima_field_data *fields, int *curfields,
|
|
|
|
unsigned long *len_mask, int enforce_mask, char *bufname)
|
|
|
|
{
|
|
|
|
void *bufp = bufstartp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < maxfields; i++) {
|
|
|
|
if (len_mask == NULL || !test_bit(i, len_mask)) {
|
|
|
|
if (bufp > (bufendp - sizeof(u32)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ima_canonical_fmt)
|
2021-06-08 12:31:21 +00:00
|
|
|
fields[i].len = le32_to_cpu(*(__le32 *)bufp);
|
|
|
|
else
|
|
|
|
fields[i].len = *(u32 *)bufp;
|
ima: introduce ima_parse_buf()
ima_parse_buf() takes as input the buffer start and end pointers, and
stores the result in a static array of ima_field_data structures,
where the len field contains the length parsed from the buffer, and
the data field contains the address of the buffer just after the length.
Optionally, the function returns the current value of the buffer pointer
and the number of array elements written.
A bitmap has been added as parameter of ima_parse_buf() to handle
the cases where the length is not prepended to data. Each bit corresponds
to an element of the ima_field_data array. If a bit is set, the length
is not parsed from the buffer, but is read from the corresponding element
of the array (the length must be set before calling the function).
ima_parse_buf() can perform three checks upon request by callers,
depending on the enforce mask passed to it:
- ENFORCE_FIELDS: matching of number of fields (length-data combination)
- there must be enough data in the buffer to parse the number of fields
requested (output: current value of buffer pointer)
- ENFORCE_BUFEND: matching of buffer end
- the ima_field_data array must be large enough to contain lengths and
data pointers for the amount of data requested (output: number
of fields written)
- ENFORCE_FIELDS | ENFORCE_BUFEND: matching of both
Use cases
- measurement entry header: ENFORCE_FIELDS | ENFORCE_BUFEND
- four fields must be parsed: pcr, digest, template name, template data
- ENFORCE_BUFEND is enforced only for the last measurement entry
- template digest (Crypto Agile): ENFORCE_BUFEND
- since only the total template digest length is known, the function
parses length-data combinations until the buffer end is reached
- template data: ENFORCE_FIELDS | ENFORCE_BUFEND
- since the number of fields and the total template data length
are known, the function can perform both checks
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2017-05-16 12:53:41 +00:00
|
|
|
|
|
|
|
bufp += sizeof(u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufp > (bufendp - fields[i].len))
|
|
|
|
break;
|
|
|
|
|
|
|
|
fields[i].data = bufp;
|
|
|
|
bufp += fields[i].len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
|
|
|
|
pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
|
|
|
|
bufname, maxfields, i);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
|
|
|
|
pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
|
|
|
|
bufname, bufendp, bufp);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curfields)
|
|
|
|
*curfields = i;
|
|
|
|
|
|
|
|
if (bufcurp)
|
|
|
|
*bufcurp = bufp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-28 02:19:32 +00:00
|
|
|
static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,
|
2021-12-23 17:29:56 +00:00
|
|
|
u8 digest_type, u8 hash_algo,
|
2013-11-08 18:21:37 +00:00
|
|
|
struct ima_field_data *field_data)
|
2013-06-07 10:16:32 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* digest formats:
|
|
|
|
* - DATA_FMT_DIGEST: digest
|
2022-04-14 20:46:39 +00:00
|
|
|
* - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest,
|
2021-12-23 17:29:56 +00:00
|
|
|
* - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
|
|
|
|
* <digest type> + ':' + <hash algo> + ':' + '\0' + digest,
|
2022-04-14 20:46:39 +00:00
|
|
|
*
|
|
|
|
* where 'DATA_FMT_DIGEST' is the original digest format ('d')
|
|
|
|
* with a hash size limitation of 20 bytes,
|
2021-12-23 17:29:56 +00:00
|
|
|
* where <digest type> is either "ima" or "verity",
|
2022-04-14 20:46:39 +00:00
|
|
|
* where <hash algo> is the hash_algo_name[] string.
|
2013-06-07 10:16:32 +00:00
|
|
|
*/
|
2021-12-23 17:29:56 +00:00
|
|
|
u8 buffer[DIGEST_TYPE_NAME_LEN_MAX + CRYPTO_MAX_ALG_NAME + 2 +
|
|
|
|
IMA_MAX_DIGEST_SIZE] = { 0 };
|
2013-06-07 10:16:32 +00:00
|
|
|
enum data_formats fmt = DATA_FMT_DIGEST;
|
|
|
|
u32 offset = 0;
|
|
|
|
|
2021-12-23 17:29:56 +00:00
|
|
|
if (digest_type < DIGEST_TYPE__LAST && hash_algo < HASH_ALGO__LAST) {
|
|
|
|
fmt = DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO;
|
|
|
|
offset += 1 + sprintf(buffer, "%s:%s:",
|
|
|
|
digest_type_name[digest_type],
|
|
|
|
hash_algo_name[hash_algo]);
|
|
|
|
} else if (hash_algo < HASH_ALGO__LAST) {
|
2013-06-07 10:16:32 +00:00
|
|
|
fmt = DATA_FMT_DIGEST_WITH_ALGO;
|
2021-12-23 17:29:56 +00:00
|
|
|
offset += 1 + sprintf(buffer, "%s:",
|
|
|
|
hash_algo_name[hash_algo]);
|
2013-06-07 10:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (digest)
|
|
|
|
memcpy(buffer + offset, digest, digestsize);
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* If digest is NULL, the event being recorded is a violation.
|
2022-06-30 15:23:38 +00:00
|
|
|
* Make room for the digest by increasing the offset by the
|
|
|
|
* hash algorithm digest size.
|
2013-06-07 10:16:32 +00:00
|
|
|
*/
|
2022-06-30 15:23:38 +00:00
|
|
|
offset += hash_digest_size[hash_algo];
|
2013-06-07 10:16:32 +00:00
|
|
|
|
|
|
|
return ima_write_template_field_data(buffer, offset + digestsize,
|
|
|
|
fmt, field_data);
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:16:30 +00:00
|
|
|
/*
|
2013-06-07 10:16:32 +00:00
|
|
|
* This function writes the digest of an event (with size limit).
|
2013-06-07 10:16:30 +00:00
|
|
|
*/
|
2015-04-11 15:09:50 +00:00
|
|
|
int ima_eventdigest_init(struct ima_event_data *event_data,
|
2013-06-07 10:16:30 +00:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2022-01-24 19:26:23 +00:00
|
|
|
struct ima_max_digest_data hash;
|
integrity: Avoid -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally.
There is currently an object (`hdr)` in `struct ima_max_digest_data`
that contains a flexible structure (`struct ima_digest_data`):
struct ima_max_digest_data {
struct ima_digest_data hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
So, in order to avoid ending up with a flexible-array member in the
middle of a struct, we use the `__struct_group()` helper to separate
the flexible array from the rest of the members in the flexible
structure:
struct ima_digest_data {
__struct_group(ima_digest_data_hdr, hdr, __packed,
... the rest of the members
);
u8 digest[];
} __packed;
And similarly for `struct evm_ima_xattr_data`.
With the change described above, we can now declare an object of the
type of the tagged `struct ima_digest_data_hdr`, without embedding the
flexible array in the middle of another struct:
struct ima_max_digest_data {
struct ima_digest_data_hdr hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
And similarly for `struct evm_digest` and `struct evm_xattr`.
We also use `container_of()` whenever we need to retrieve a pointer to
the flexible structure.
So, with these changes, fix the following warnings:
security/integrity/evm/evm.h:64:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Link: https://github.com/KSPP/linux/issues/202
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2024-04-04 15:00:48 +00:00
|
|
|
struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
|
|
|
|
struct ima_digest_data, hdr);
|
2013-06-07 10:16:32 +00:00
|
|
|
u8 *cur_digest = NULL;
|
|
|
|
u32 cur_digestsize = 0;
|
2013-06-07 10:16:30 +00:00
|
|
|
struct inode *inode;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
memset(&hash, 0, sizeof(hash));
|
|
|
|
|
2015-04-11 15:12:39 +00:00
|
|
|
if (event_data->violation) /* recording a violation. */
|
2013-06-07 10:16:30 +00:00
|
|
|
goto out;
|
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
|
|
|
|
cur_digest = event_data->iint->ima_hash->digest;
|
|
|
|
cur_digestsize = event_data->iint->ima_hash->length;
|
2013-06-07 10:16:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:08:21 +00:00
|
|
|
if ((const char *)event_data->filename == boot_aggregate_name) {
|
|
|
|
if (ima_tpm_chip) {
|
|
|
|
hash.hdr.algo = HASH_ALGO_SHA1;
|
integrity: Avoid -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally.
There is currently an object (`hdr)` in `struct ima_max_digest_data`
that contains a flexible structure (`struct ima_digest_data`):
struct ima_max_digest_data {
struct ima_digest_data hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
So, in order to avoid ending up with a flexible-array member in the
middle of a struct, we use the `__struct_group()` helper to separate
the flexible array from the rest of the members in the flexible
structure:
struct ima_digest_data {
__struct_group(ima_digest_data_hdr, hdr, __packed,
... the rest of the members
);
u8 digest[];
} __packed;
And similarly for `struct evm_ima_xattr_data`.
With the change described above, we can now declare an object of the
type of the tagged `struct ima_digest_data_hdr`, without embedding the
flexible array in the middle of another struct:
struct ima_max_digest_data {
struct ima_digest_data_hdr hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
And similarly for `struct evm_digest` and `struct evm_xattr`.
We also use `container_of()` whenever we need to retrieve a pointer to
the flexible structure.
So, with these changes, fix the following warnings:
security/integrity/evm/evm.h:64:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Link: https://github.com/KSPP/linux/issues/202
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2024-04-04 15:00:48 +00:00
|
|
|
result = ima_calc_boot_aggregate(hash_hdr);
|
2020-06-03 15:08:21 +00:00
|
|
|
|
|
|
|
/* algo can change depending on available PCR banks */
|
|
|
|
if (!result && hash.hdr.algo != HASH_ALGO_SHA1)
|
|
|
|
result = -EINVAL;
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
memset(&hash, 0, sizeof(hash));
|
|
|
|
}
|
|
|
|
|
integrity: Avoid -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally.
There is currently an object (`hdr)` in `struct ima_max_digest_data`
that contains a flexible structure (`struct ima_digest_data`):
struct ima_max_digest_data {
struct ima_digest_data hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
So, in order to avoid ending up with a flexible-array member in the
middle of a struct, we use the `__struct_group()` helper to separate
the flexible array from the rest of the members in the flexible
structure:
struct ima_digest_data {
__struct_group(ima_digest_data_hdr, hdr, __packed,
... the rest of the members
);
u8 digest[];
} __packed;
And similarly for `struct evm_ima_xattr_data`.
With the change described above, we can now declare an object of the
type of the tagged `struct ima_digest_data_hdr`, without embedding the
flexible array in the middle of another struct:
struct ima_max_digest_data {
struct ima_digest_data_hdr hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
And similarly for `struct evm_digest` and `struct evm_xattr`.
We also use `container_of()` whenever we need to retrieve a pointer to
the flexible structure.
So, with these changes, fix the following warnings:
security/integrity/evm/evm.h:64:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Link: https://github.com/KSPP/linux/issues/202
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2024-04-04 15:00:48 +00:00
|
|
|
cur_digest = hash_hdr->digest;
|
2020-06-03 15:08:21 +00:00
|
|
|
cur_digestsize = hash_digest_size[HASH_ALGO_SHA1];
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
if (!event_data->file) /* missing info to re-calculate the digest */
|
2013-06-07 10:16:30 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
inode = file_inode(event_data->file);
|
2013-06-07 10:16:32 +00:00
|
|
|
hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
|
|
|
|
ima_hash_algo : HASH_ALGO_SHA1;
|
integrity: Avoid -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally.
There is currently an object (`hdr)` in `struct ima_max_digest_data`
that contains a flexible structure (`struct ima_digest_data`):
struct ima_max_digest_data {
struct ima_digest_data hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
So, in order to avoid ending up with a flexible-array member in the
middle of a struct, we use the `__struct_group()` helper to separate
the flexible array from the rest of the members in the flexible
structure:
struct ima_digest_data {
__struct_group(ima_digest_data_hdr, hdr, __packed,
... the rest of the members
);
u8 digest[];
} __packed;
And similarly for `struct evm_ima_xattr_data`.
With the change described above, we can now declare an object of the
type of the tagged `struct ima_digest_data_hdr`, without embedding the
flexible array in the middle of another struct:
struct ima_max_digest_data {
struct ima_digest_data_hdr hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
And similarly for `struct evm_digest` and `struct evm_xattr`.
We also use `container_of()` whenever we need to retrieve a pointer to
the flexible structure.
So, with these changes, fix the following warnings:
security/integrity/evm/evm.h:64:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Link: https://github.com/KSPP/linux/issues/202
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2024-04-04 15:00:48 +00:00
|
|
|
result = ima_calc_file_hash(event_data->file, hash_hdr);
|
2013-06-07 10:16:30 +00:00
|
|
|
if (result) {
|
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
|
2015-04-11 15:09:50 +00:00
|
|
|
event_data->filename, "collect_data",
|
2013-06-07 10:16:30 +00:00
|
|
|
"failed", result, 0);
|
|
|
|
return result;
|
|
|
|
}
|
integrity: Avoid -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally.
There is currently an object (`hdr)` in `struct ima_max_digest_data`
that contains a flexible structure (`struct ima_digest_data`):
struct ima_max_digest_data {
struct ima_digest_data hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
So, in order to avoid ending up with a flexible-array member in the
middle of a struct, we use the `__struct_group()` helper to separate
the flexible array from the rest of the members in the flexible
structure:
struct ima_digest_data {
__struct_group(ima_digest_data_hdr, hdr, __packed,
... the rest of the members
);
u8 digest[];
} __packed;
And similarly for `struct evm_ima_xattr_data`.
With the change described above, we can now declare an object of the
type of the tagged `struct ima_digest_data_hdr`, without embedding the
flexible array in the middle of another struct:
struct ima_max_digest_data {
struct ima_digest_data_hdr hdr;
u8 digest[HASH_MAX_DIGESTSIZE];
} __packed;
And similarly for `struct evm_digest` and `struct evm_xattr`.
We also use `container_of()` whenever we need to retrieve a pointer to
the flexible structure.
So, with these changes, fix the following warnings:
security/integrity/evm/evm.h:64:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/evm/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/ima/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
security/integrity/platform_certs/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Link: https://github.com/KSPP/linux/issues/202
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2024-04-04 15:00:48 +00:00
|
|
|
cur_digest = hash_hdr->digest;
|
2013-06-07 10:16:32 +00:00
|
|
|
cur_digestsize = hash.hdr.length;
|
2013-06-07 10:16:30 +00:00
|
|
|
out:
|
2013-11-08 18:21:36 +00:00
|
|
|
return ima_eventdigest_init_common(cur_digest, cur_digestsize,
|
2021-12-23 17:29:56 +00:00
|
|
|
DIGEST_TYPE__LAST, HASH_ALGO__LAST,
|
|
|
|
field_data);
|
2013-06-07 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-06-07 10:16:32 +00:00
|
|
|
* This function writes the digest of an event (without size limit).
|
2013-06-07 10:16:30 +00:00
|
|
|
*/
|
2015-04-11 15:09:50 +00:00
|
|
|
int ima_eventdigest_ng_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
2013-06-07 10:16:32 +00:00
|
|
|
{
|
2022-04-27 12:12:38 +00:00
|
|
|
u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
|
2013-06-07 10:16:32 +00:00
|
|
|
u32 cur_digestsize = 0;
|
|
|
|
|
2015-04-11 15:12:39 +00:00
|
|
|
if (event_data->violation) /* recording a violation. */
|
2013-06-07 10:16:32 +00:00
|
|
|
goto out;
|
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
cur_digest = event_data->iint->ima_hash->digest;
|
|
|
|
cur_digestsize = event_data->iint->ima_hash->length;
|
2013-06-07 10:16:32 +00:00
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
hash_algo = event_data->iint->ima_hash->algo;
|
2013-06-07 10:16:32 +00:00
|
|
|
out:
|
|
|
|
return ima_eventdigest_init_common(cur_digest, cur_digestsize,
|
2021-12-23 17:29:56 +00:00
|
|
|
DIGEST_TYPE__LAST, hash_algo,
|
|
|
|
field_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function writes the digest of an event (without size limit),
|
|
|
|
* prefixed with both the digest type and hash algorithm.
|
|
|
|
*/
|
|
|
|
int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
|
|
|
|
u32 cur_digestsize = 0;
|
|
|
|
u8 digest_type = DIGEST_TYPE_IMA;
|
|
|
|
|
|
|
|
if (event_data->violation) /* recording a violation. */
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
cur_digest = event_data->iint->ima_hash->digest;
|
|
|
|
cur_digestsize = event_data->iint->ima_hash->length;
|
|
|
|
|
|
|
|
hash_algo = event_data->iint->ima_hash->algo;
|
2021-12-23 17:29:56 +00:00
|
|
|
if (event_data->iint->flags & IMA_VERITY_REQUIRED)
|
|
|
|
digest_type = DIGEST_TYPE_VERITY;
|
2021-12-23 17:29:56 +00:00
|
|
|
out:
|
|
|
|
return ima_eventdigest_init_common(cur_digest, cur_digestsize,
|
|
|
|
digest_type, hash_algo,
|
|
|
|
field_data);
|
2013-06-07 10:16:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 02:19:32 +00:00
|
|
|
/*
|
|
|
|
* This function writes the digest of the file which is expected to match the
|
|
|
|
* digest contained in the file's appended signature.
|
|
|
|
*/
|
|
|
|
int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
enum hash_algo hash_algo;
|
|
|
|
const u8 *cur_digest;
|
|
|
|
u32 cur_digestsize;
|
|
|
|
|
|
|
|
if (!event_data->modsig)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (event_data->violation) {
|
|
|
|
/* Recording a violation. */
|
|
|
|
hash_algo = HASH_ALGO_SHA1;
|
|
|
|
cur_digest = NULL;
|
|
|
|
cur_digestsize = 0;
|
|
|
|
} else {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,
|
|
|
|
&cur_digest, &cur_digestsize);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
|
|
|
|
/* There was some error collecting the digest. */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ima_eventdigest_init_common(cur_digest, cur_digestsize,
|
2021-12-23 17:29:56 +00:00
|
|
|
DIGEST_TYPE__LAST, hash_algo,
|
|
|
|
field_data);
|
2019-06-28 02:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
static int ima_eventname_init_common(struct ima_event_data *event_data,
|
2013-06-07 10:16:32 +00:00
|
|
|
struct ima_field_data *field_data,
|
|
|
|
bool size_limit)
|
2013-06-07 10:16:30 +00:00
|
|
|
{
|
|
|
|
const char *cur_filename = NULL;
|
2024-03-22 14:03:12 +00:00
|
|
|
struct name_snapshot filename;
|
2013-06-07 10:16:30 +00:00
|
|
|
u32 cur_filename_len = 0;
|
2024-03-22 14:03:12 +00:00
|
|
|
bool snapshot = false;
|
|
|
|
int ret;
|
2013-06-07 10:16:30 +00:00
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
BUG_ON(event_data->filename == NULL && event_data->file == NULL);
|
2013-06-07 10:16:30 +00:00
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
if (event_data->filename) {
|
|
|
|
cur_filename = event_data->filename;
|
|
|
|
cur_filename_len = strlen(event_data->filename);
|
2013-06-07 10:16:30 +00:00
|
|
|
|
2013-06-07 10:16:32 +00:00
|
|
|
if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
|
2013-06-07 10:16:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-04-11 15:09:50 +00:00
|
|
|
if (event_data->file) {
|
2024-03-22 14:03:12 +00:00
|
|
|
take_dentry_name_snapshot(&filename,
|
|
|
|
event_data->file->f_path.dentry);
|
|
|
|
snapshot = true;
|
|
|
|
cur_filename = filename.name.name;
|
2013-06-07 10:16:30 +00:00
|
|
|
cur_filename_len = strlen(cur_filename);
|
|
|
|
} else
|
|
|
|
/*
|
|
|
|
* Truncate filename if the latter is too long and
|
|
|
|
* the file descriptor is not available.
|
|
|
|
*/
|
|
|
|
cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
|
|
|
|
out:
|
2024-03-22 14:03:12 +00:00
|
|
|
ret = ima_write_template_field_data(cur_filename, cur_filename_len,
|
|
|
|
DATA_FMT_STRING, field_data);
|
|
|
|
|
|
|
|
if (snapshot)
|
|
|
|
release_dentry_name_snapshot(&filename);
|
|
|
|
|
|
|
|
return ret;
|
2013-06-07 10:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function writes the name of an event (with size limit).
|
|
|
|
*/
|
2015-04-11 15:09:50 +00:00
|
|
|
int ima_eventname_init(struct ima_event_data *event_data,
|
2013-06-07 10:16:32 +00:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2015-04-11 15:09:50 +00:00
|
|
|
return ima_eventname_init_common(event_data, field_data, true);
|
2013-06-07 10:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function writes the name of an event (without size limit).
|
|
|
|
*/
|
2015-04-11 15:09:50 +00:00
|
|
|
int ima_eventname_ng_init(struct ima_event_data *event_data,
|
2013-06-07 10:16:32 +00:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2015-04-11 15:09:50 +00:00
|
|
|
return ima_eventname_init_common(event_data, field_data, false);
|
2013-06-07 10:16:30 +00:00
|
|
|
}
|
2013-07-23 15:15:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventsig_init - include the file signature as part of the template data
|
|
|
|
*/
|
2015-04-11 15:09:50 +00:00
|
|
|
int ima_eventsig_init(struct ima_event_data *event_data,
|
2013-07-23 15:15:00 +00:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2015-04-11 15:09:50 +00:00
|
|
|
struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
|
2013-07-23 15:15:00 +00:00
|
|
|
|
ima: support fs-verity file digest based version 3 signatures
IMA may verify a file's integrity against a "good" value stored in the
'security.ima' xattr or as an appended signature, based on policy. When
the "good value" is stored in the xattr, the xattr may contain a file
hash or signature. In either case, the "good" value is preceded by a
header. The first byte of the xattr header indicates the type of data
- hash, signature - stored in the xattr. To support storing fs-verity
signatures in the 'security.ima' xattr requires further differentiating
the fs-verity signature from the existing IMA signature.
In addition the signatures stored in 'security.ima' xattr, need to be
disambiguated. Instead of directly signing the fs-verity digest, a new
signature format version 3 is defined as the hash of the ima_file_id
structure, which identifies the type of signature and the digest.
The IMA policy defines "which" files are to be measured, verified, and/or
audited. For those files being verified, the policy rules indicate "how"
the file should be verified. For example to require a file be signed,
the appraise policy rule must include the 'appraise_type' option.
appraise_type:= [imasig] | [imasig|modsig] | [sigv3]
where 'imasig' is the original or signature format v2 (default),
where 'modsig' is an appended signature,
where 'sigv3' is the signature format v3.
The policy rule must also indicate the type of digest, if not the IMA
default, by first specifying the digest type:
digest_type:= [verity]
The following policy rule requires fsverity signatures. The rule may be
constrained, for example based on a fsuuid or LSM label.
appraise func=BPRM_CHECK digest_type=verity appraise_type=sigv3
Acked-by: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2021-11-24 15:56:33 +00:00
|
|
|
if (!xattr_value ||
|
|
|
|
(xattr_value->type != EVM_IMA_XATTR_DIGSIG &&
|
|
|
|
xattr_value->type != IMA_VERITY_DIGSIG))
|
2021-05-14 15:27:52 +00:00
|
|
|
return ima_eventevmsig_init(event_data, field_data);
|
2013-07-23 15:15:00 +00:00
|
|
|
|
2018-03-14 20:20:18 +00:00
|
|
|
return ima_write_template_field_data(xattr_value, event_data->xattr_len,
|
|
|
|
DATA_FMT_HEX, field_data);
|
2013-07-23 15:15:00 +00:00
|
|
|
}
|
2019-06-24 06:23:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the
|
|
|
|
* template data.
|
|
|
|
*/
|
|
|
|
int ima_eventbuf_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
if ((!event_data->buf) || (event_data->buf_len == 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ima_write_template_field_data(event_data->buf,
|
|
|
|
event_data->buf_len, DATA_FMT_HEX,
|
|
|
|
field_data);
|
|
|
|
}
|
2019-06-28 02:19:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventmodsig_init - include the appended file signature as part of the
|
|
|
|
* template data
|
|
|
|
*/
|
|
|
|
int ima_eventmodsig_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
const void *data;
|
|
|
|
u32 data_len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!event_data->modsig)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* modsig is a runtime structure containing pointers. Get its raw data
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return ima_write_template_field_data(data, data_len, DATA_FMT_HEX,
|
|
|
|
field_data);
|
|
|
|
}
|
2021-05-14 15:27:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventevmsig_init - include the EVM portable signature as part of the
|
|
|
|
* template data
|
|
|
|
*/
|
|
|
|
int ima_eventevmsig_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
struct evm_ima_xattr_data *xattr_data = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (!event_data->file)
|
|
|
|
return 0;
|
|
|
|
|
2023-01-13 11:49:22 +00:00
|
|
|
rc = vfs_getxattr_alloc(&nop_mnt_idmap, file_dentry(event_data->file),
|
2021-05-14 15:27:52 +00:00
|
|
|
XATTR_NAME_EVM, (char **)&xattr_data, 0,
|
|
|
|
GFP_NOFS);
|
2022-11-09 19:14:35 +00:00
|
|
|
if (rc <= 0 || xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG) {
|
|
|
|
rc = 0;
|
|
|
|
goto out;
|
2021-05-14 15:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = ima_write_template_field_data((char *)xattr_data, rc, DATA_FMT_HEX,
|
|
|
|
field_data);
|
2022-11-09 19:14:35 +00:00
|
|
|
|
|
|
|
out:
|
2021-05-14 15:27:52 +00:00
|
|
|
kfree(xattr_data);
|
|
|
|
return rc;
|
|
|
|
}
|
2021-05-28 07:38:07 +00:00
|
|
|
|
|
|
|
static int ima_eventinodedac_init_common(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data,
|
|
|
|
bool get_uid)
|
|
|
|
{
|
|
|
|
unsigned int id;
|
|
|
|
|
|
|
|
if (!event_data->file)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (get_uid)
|
|
|
|
id = i_uid_read(file_inode(event_data->file));
|
|
|
|
else
|
|
|
|
id = i_gid_read(file_inode(event_data->file));
|
|
|
|
|
|
|
|
if (ima_canonical_fmt) {
|
|
|
|
if (sizeof(id) == sizeof(u16))
|
2021-06-08 12:31:22 +00:00
|
|
|
id = (__force u16)cpu_to_le16(id);
|
2021-05-28 07:38:07 +00:00
|
|
|
else
|
2021-06-08 12:31:22 +00:00
|
|
|
id = (__force u32)cpu_to_le32(id);
|
2021-05-28 07:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ima_write_template_field_data((void *)&id, sizeof(id),
|
|
|
|
DATA_FMT_UINT, field_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventinodeuid_init - include the inode UID as part of the template
|
|
|
|
* data
|
|
|
|
*/
|
|
|
|
int ima_eventinodeuid_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
return ima_eventinodedac_init_common(event_data, field_data, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventinodegid_init - include the inode GID as part of the template
|
|
|
|
* data
|
|
|
|
*/
|
|
|
|
int ima_eventinodegid_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
return ima_eventinodedac_init_common(event_data, field_data, false);
|
|
|
|
}
|
2021-05-28 07:38:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventinodemode_init - include the inode mode as part of the template
|
|
|
|
* data
|
|
|
|
*/
|
|
|
|
int ima_eventinodemode_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
2021-06-08 12:31:22 +00:00
|
|
|
u16 mode;
|
2021-05-28 07:38:08 +00:00
|
|
|
|
|
|
|
if (!event_data->file)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
inode = file_inode(event_data->file);
|
|
|
|
mode = inode->i_mode;
|
|
|
|
if (ima_canonical_fmt)
|
2021-06-08 12:31:22 +00:00
|
|
|
mode = (__force u16)cpu_to_le16(mode);
|
2021-05-28 07:38:08 +00:00
|
|
|
|
|
|
|
return ima_write_template_field_data((char *)&mode, sizeof(mode),
|
|
|
|
DATA_FMT_UINT, field_data);
|
|
|
|
}
|
2021-06-01 08:23:38 +00:00
|
|
|
|
|
|
|
static int ima_eventinodexattrs_init_common(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data,
|
|
|
|
char type)
|
|
|
|
{
|
|
|
|
u8 *buffer = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!event_data->file)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rc = evm_read_protected_xattrs(file_dentry(event_data->file), NULL, 0,
|
|
|
|
type, ima_canonical_fmt);
|
|
|
|
if (rc < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
buffer = kmalloc(rc, GFP_KERNEL);
|
|
|
|
if (!buffer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rc = evm_read_protected_xattrs(file_dentry(event_data->file), buffer,
|
|
|
|
rc, type, ima_canonical_fmt);
|
|
|
|
if (rc < 0) {
|
|
|
|
rc = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ima_write_template_field_data((char *)buffer, rc, DATA_FMT_HEX,
|
|
|
|
field_data);
|
|
|
|
out:
|
|
|
|
kfree(buffer);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventinodexattrnames_init - include a list of xattr names as part of the
|
|
|
|
* template data
|
|
|
|
*/
|
|
|
|
int ima_eventinodexattrnames_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
return ima_eventinodexattrs_init_common(event_data, field_data, 'n');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventinodexattrlengths_init - include a list of xattr lengths as part of
|
|
|
|
* the template data
|
|
|
|
*/
|
|
|
|
int ima_eventinodexattrlengths_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
return ima_eventinodexattrs_init_common(event_data, field_data, 'l');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventinodexattrvalues_init - include a list of xattr values as part of
|
|
|
|
* the template data
|
|
|
|
*/
|
|
|
|
int ima_eventinodexattrvalues_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
return ima_eventinodexattrs_init_common(event_data, field_data, 'v');
|
|
|
|
}
|