2013-06-07 18:16:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Politecnico di Torino, Italy
|
|
|
|
* TORSEC group -- http://security.polito.it
|
|
|
|
*
|
|
|
|
* Author: Roberto Sassu <roberto.sassu@polito.it>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation, version 2 of the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* File: ima_template_lib.c
|
|
|
|
* Library of supported template fields.
|
|
|
|
*/
|
2013-06-07 18:16:32 +08:00
|
|
|
|
2013-06-07 18:16:30 +08:00
|
|
|
#include "ima_template_lib.h"
|
|
|
|
|
2013-06-07 18:16:32 +08: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,
|
2013-07-23 23:15:00 +08:00
|
|
|
DATA_FMT_STRING,
|
|
|
|
DATA_FMT_HEX
|
2013-06-07 18:16:32 +08:00
|
|
|
};
|
|
|
|
|
2013-06-07 18:16:30 +08: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 20:56:05 +08:00
|
|
|
u32 buflen = datalen;
|
2013-06-07 18:16:30 +08:00
|
|
|
|
2014-02-03 20:56:05 +08:00
|
|
|
if (datafmt == DATA_FMT_STRING)
|
2013-06-07 18:16:30 +08: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 20:56:05 +08:00
|
|
|
if (datafmt == DATA_FMT_STRING) {
|
2013-06-07 18:16:30 +08: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 23:54:42 +08:00
|
|
|
u8 *buf_ptr = field_data->data;
|
|
|
|
u32 buflen = field_data->len;
|
2013-06-07 18:16:32 +08:00
|
|
|
|
2013-06-07 18:16:30 +08:00
|
|
|
switch (datafmt) {
|
2013-06-07 18:16:32 +08:00
|
|
|
case DATA_FMT_DIGEST_WITH_ALGO:
|
|
|
|
buf_ptr = strnchr(field_data->data, buflen, ':');
|
|
|
|
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;
|
2013-06-07 18:16:30 +08:00
|
|
|
case DATA_FMT_DIGEST:
|
2013-07-23 23:15:00 +08:00
|
|
|
case DATA_FMT_HEX:
|
|
|
|
if (!buflen)
|
|
|
|
break;
|
2013-06-07 18:16:32 +08:00
|
|
|
ima_print_digest(m, buf_ptr, buflen);
|
2013-06-07 18:16:30 +08:00
|
|
|
break;
|
|
|
|
case DATA_FMT_STRING:
|
2013-06-07 18:16:32 +08:00
|
|
|
seq_printf(m, "%s", buf_ptr);
|
2013-06-07 18:16:30 +08:00
|
|
|
break;
|
|
|
|
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 20:56:04 +08:00
|
|
|
u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
|
|
|
|
strlen(field_data->data) : field_data->len;
|
|
|
|
|
2016-12-20 08:22:57 +08:00
|
|
|
if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
|
|
|
|
u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
|
|
|
|
|
|
|
|
ima_putc(m, &field_len, sizeof(field_len));
|
|
|
|
}
|
2013-11-09 02:21:40 +08:00
|
|
|
|
2014-02-03 20:56:04 +08:00
|
|
|
if (!len)
|
2013-06-07 18:16:30 +08:00
|
|
|
return;
|
2013-11-09 02:21:40 +08:00
|
|
|
|
2014-02-03 20:56:04 +08:00
|
|
|
ima_putc(m, field_data->data, len);
|
2013-06-07 18:16:30 +08: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-09 02:21:40 +08:00
|
|
|
case IMA_SHOW_BINARY_NO_FIELD_LEN:
|
2014-02-03 20:56:04 +08:00
|
|
|
case IMA_SHOW_BINARY_OLD_STRING_FMT:
|
2013-06-07 18:16:30 +08: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 18:16:32 +08: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);
|
|
|
|
}
|
|
|
|
|
2013-06-07 18:16:30 +08: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 23:15:00 +08: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);
|
|
|
|
}
|
|
|
|
|
2013-06-07 18:16:32 +08:00
|
|
|
static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
|
2013-11-09 02:21:37 +08:00
|
|
|
struct ima_field_data *field_data)
|
2013-06-07 18:16:32 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* digest formats:
|
|
|
|
* - DATA_FMT_DIGEST: digest
|
|
|
|
* - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
|
|
|
|
* where <hash algo> is provided if the hash algoritm is not
|
|
|
|
* SHA1 or MD5
|
|
|
|
*/
|
|
|
|
u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
|
|
|
|
enum data_formats fmt = DATA_FMT_DIGEST;
|
|
|
|
u32 offset = 0;
|
|
|
|
|
2013-11-09 02:21:37 +08:00
|
|
|
if (hash_algo < HASH_ALGO__LAST) {
|
2013-06-07 18:16:32 +08:00
|
|
|
fmt = DATA_FMT_DIGEST_WITH_ALGO;
|
2013-11-09 02:21:37 +08:00
|
|
|
offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
|
|
|
|
hash_algo_name[hash_algo]);
|
2013-06-07 18:16:32 +08:00
|
|
|
buffer[offset] = ':';
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (digest)
|
|
|
|
memcpy(buffer + offset, digest, digestsize);
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* If digest is NULL, the event being recorded is a violation.
|
|
|
|
* Make room for the digest by increasing the offset of
|
|
|
|
* IMA_DIGEST_SIZE.
|
|
|
|
*/
|
|
|
|
offset += IMA_DIGEST_SIZE;
|
|
|
|
|
|
|
|
return ima_write_template_field_data(buffer, offset + digestsize,
|
|
|
|
fmt, field_data);
|
|
|
|
}
|
|
|
|
|
2013-06-07 18:16:30 +08:00
|
|
|
/*
|
2013-06-07 18:16:32 +08:00
|
|
|
* This function writes the digest of an event (with size limit).
|
2013-06-07 18:16:30 +08:00
|
|
|
*/
|
2015-04-11 23:09:50 +08:00
|
|
|
int ima_eventdigest_init(struct ima_event_data *event_data,
|
2013-06-07 18:16:30 +08:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
struct ima_digest_data hdr;
|
|
|
|
char digest[IMA_MAX_DIGEST_SIZE];
|
|
|
|
} hash;
|
2013-06-07 18:16:32 +08:00
|
|
|
u8 *cur_digest = NULL;
|
|
|
|
u32 cur_digestsize = 0;
|
2013-06-07 18:16:30 +08:00
|
|
|
struct inode *inode;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
memset(&hash, 0, sizeof(hash));
|
|
|
|
|
2015-04-11 23:12:39 +08:00
|
|
|
if (event_data->violation) /* recording a violation. */
|
2013-06-07 18:16:30 +08:00
|
|
|
goto out;
|
|
|
|
|
2015-04-11 23:09:50 +08: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 18:16:30 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
if (!event_data->file) /* missing info to re-calculate the digest */
|
2013-06-07 18:16:30 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
inode = file_inode(event_data->file);
|
2013-06-07 18:16:32 +08:00
|
|
|
hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
|
|
|
|
ima_hash_algo : HASH_ALGO_SHA1;
|
2015-04-11 23:09:50 +08:00
|
|
|
result = ima_calc_file_hash(event_data->file, &hash.hdr);
|
2013-06-07 18:16:30 +08:00
|
|
|
if (result) {
|
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
|
2015-04-11 23:09:50 +08:00
|
|
|
event_data->filename, "collect_data",
|
2013-06-07 18:16:30 +08:00
|
|
|
"failed", result, 0);
|
|
|
|
return result;
|
|
|
|
}
|
2013-06-07 18:16:32 +08:00
|
|
|
cur_digest = hash.hdr.digest;
|
|
|
|
cur_digestsize = hash.hdr.length;
|
2013-06-07 18:16:30 +08:00
|
|
|
out:
|
2013-11-09 02:21:36 +08:00
|
|
|
return ima_eventdigest_init_common(cur_digest, cur_digestsize,
|
2013-11-09 02:21:37 +08:00
|
|
|
HASH_ALGO__LAST, field_data);
|
2013-06-07 18:16:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-06-07 18:16:32 +08:00
|
|
|
* This function writes the digest of an event (without size limit).
|
2013-06-07 18:16:30 +08:00
|
|
|
*/
|
2015-04-11 23:09:50 +08:00
|
|
|
int ima_eventdigest_ng_init(struct ima_event_data *event_data,
|
|
|
|
struct ima_field_data *field_data)
|
2013-06-07 18:16:32 +08:00
|
|
|
{
|
2013-11-09 02:21:35 +08:00
|
|
|
u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
|
2013-06-07 18:16:32 +08:00
|
|
|
u32 cur_digestsize = 0;
|
|
|
|
|
2015-04-11 23:12:39 +08:00
|
|
|
if (event_data->violation) /* recording a violation. */
|
2013-06-07 18:16:32 +08:00
|
|
|
goto out;
|
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
cur_digest = event_data->iint->ima_hash->digest;
|
|
|
|
cur_digestsize = event_data->iint->ima_hash->length;
|
2013-06-07 18:16:32 +08:00
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
hash_algo = event_data->iint->ima_hash->algo;
|
2013-06-07 18:16:32 +08:00
|
|
|
out:
|
|
|
|
return ima_eventdigest_init_common(cur_digest, cur_digestsize,
|
2013-11-09 02:21:37 +08:00
|
|
|
hash_algo, field_data);
|
2013-06-07 18:16:32 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
static int ima_eventname_init_common(struct ima_event_data *event_data,
|
2013-06-07 18:16:32 +08:00
|
|
|
struct ima_field_data *field_data,
|
|
|
|
bool size_limit)
|
2013-06-07 18:16:30 +08:00
|
|
|
{
|
|
|
|
const char *cur_filename = NULL;
|
|
|
|
u32 cur_filename_len = 0;
|
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
BUG_ON(event_data->filename == NULL && event_data->file == NULL);
|
2013-06-07 18:16:30 +08:00
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
if (event_data->filename) {
|
|
|
|
cur_filename = event_data->filename;
|
|
|
|
cur_filename_len = strlen(event_data->filename);
|
2013-06-07 18:16:30 +08:00
|
|
|
|
2013-06-07 18:16:32 +08:00
|
|
|
if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
|
2013-06-07 18:16:30 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
if (event_data->file) {
|
|
|
|
cur_filename = event_data->file->f_path.dentry->d_name.name;
|
2013-06-07 18:16:30 +08: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:
|
|
|
|
return ima_write_template_field_data(cur_filename, cur_filename_len,
|
2014-02-03 20:56:05 +08:00
|
|
|
DATA_FMT_STRING, field_data);
|
2013-06-07 18:16:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function writes the name of an event (with size limit).
|
|
|
|
*/
|
2015-04-11 23:09:50 +08:00
|
|
|
int ima_eventname_init(struct ima_event_data *event_data,
|
2013-06-07 18:16:32 +08:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2015-04-11 23:09:50 +08:00
|
|
|
return ima_eventname_init_common(event_data, field_data, true);
|
2013-06-07 18:16:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function writes the name of an event (without size limit).
|
|
|
|
*/
|
2015-04-11 23:09:50 +08:00
|
|
|
int ima_eventname_ng_init(struct ima_event_data *event_data,
|
2013-06-07 18:16:32 +08:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
2015-04-11 23:09:50 +08:00
|
|
|
return ima_eventname_init_common(event_data, field_data, false);
|
2013-06-07 18:16:30 +08:00
|
|
|
}
|
2013-07-23 23:15:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_eventsig_init - include the file signature as part of the template data
|
|
|
|
*/
|
2015-04-11 23:09:50 +08:00
|
|
|
int ima_eventsig_init(struct ima_event_data *event_data,
|
2013-07-23 23:15:00 +08:00
|
|
|
struct ima_field_data *field_data)
|
|
|
|
{
|
|
|
|
enum data_formats fmt = DATA_FMT_HEX;
|
2015-04-11 23:09:50 +08:00
|
|
|
struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
|
|
|
|
int xattr_len = event_data->xattr_len;
|
2013-07-23 23:15:00 +08:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,
|
|
|
|
field_data);
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|