Path: blob/master/security/integrity/ima/ima_template_lib.c
26424 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2013 Politecnico di Torino, Italy3* TORSEC group -- https://security.polito.it4*5* Author: Roberto Sassu <[email protected]>6*7* File: ima_template_lib.c8* Library of supported template fields.9*/1011#include "ima_template_lib.h"12#include <linux/xattr.h>13#include <linux/evm.h>1415static bool ima_template_hash_algo_allowed(u8 algo)16{17if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)18return true;1920return false;21}2223enum data_formats {24DATA_FMT_DIGEST = 0,25DATA_FMT_DIGEST_WITH_ALGO,26DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,27DATA_FMT_STRING,28DATA_FMT_HEX,29DATA_FMT_UINT30};3132enum digest_type {33DIGEST_TYPE_IMA,34DIGEST_TYPE_VERITY,35DIGEST_TYPE__LAST36};3738#define DIGEST_TYPE_NAME_LEN_MAX 7 /* including NUL */39static const char * const digest_type_name[DIGEST_TYPE__LAST] = {40[DIGEST_TYPE_IMA] = "ima",41[DIGEST_TYPE_VERITY] = "verity"42};4344static int ima_write_template_field_data(const void *data, const u32 datalen,45enum data_formats datafmt,46struct ima_field_data *field_data)47{48u8 *buf, *buf_ptr;49u32 buflen = datalen;5051if (datafmt == DATA_FMT_STRING)52buflen = datalen + 1;5354buf = kzalloc(buflen, GFP_KERNEL);55if (!buf)56return -ENOMEM;5758memcpy(buf, data, datalen);5960/*61* Replace all space characters with underscore for event names and62* strings. This avoid that, during the parsing of a measurements list,63* filenames with spaces or that end with the suffix ' (deleted)' are64* split into multiple template fields (the space is the delimitator65* character for measurements lists in ASCII format).66*/67if (datafmt == DATA_FMT_STRING) {68for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)69if (*buf_ptr == ' ')70*buf_ptr = '_';71}7273field_data->data = buf;74field_data->len = buflen;75return 0;76}7778static void ima_show_template_data_ascii(struct seq_file *m,79enum ima_show_type show,80enum data_formats datafmt,81struct ima_field_data *field_data)82{83u8 *buf_ptr = field_data->data;84u32 buflen = field_data->len;8586switch (datafmt) {87case DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:88case DATA_FMT_DIGEST_WITH_ALGO:89buf_ptr = strrchr(field_data->data, ':');90if (buf_ptr != field_data->data)91seq_printf(m, "%s", field_data->data);9293/* skip ':' and '\0' */94buf_ptr += 2;95buflen -= buf_ptr - field_data->data;96fallthrough;97case DATA_FMT_DIGEST:98case DATA_FMT_HEX:99if (!buflen)100break;101ima_print_digest(m, buf_ptr, buflen);102break;103case DATA_FMT_STRING:104seq_printf(m, "%s", buf_ptr);105break;106case DATA_FMT_UINT:107switch (field_data->len) {108case sizeof(u8):109seq_printf(m, "%u", *(u8 *)buf_ptr);110break;111case sizeof(u16):112if (ima_canonical_fmt)113seq_printf(m, "%u",114le16_to_cpu(*(__le16 *)buf_ptr));115else116seq_printf(m, "%u", *(u16 *)buf_ptr);117break;118case sizeof(u32):119if (ima_canonical_fmt)120seq_printf(m, "%u",121le32_to_cpu(*(__le32 *)buf_ptr));122else123seq_printf(m, "%u", *(u32 *)buf_ptr);124break;125case sizeof(u64):126if (ima_canonical_fmt)127seq_printf(m, "%llu",128le64_to_cpu(*(__le64 *)buf_ptr));129else130seq_printf(m, "%llu", *(u64 *)buf_ptr);131break;132default:133break;134}135break;136default:137break;138}139}140141static void ima_show_template_data_binary(struct seq_file *m,142enum ima_show_type show,143enum data_formats datafmt,144struct ima_field_data *field_data)145{146u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?147strlen(field_data->data) : field_data->len;148149if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {150u32 field_len = !ima_canonical_fmt ?151len : (__force u32)cpu_to_le32(len);152153ima_putc(m, &field_len, sizeof(field_len));154}155156if (!len)157return;158159ima_putc(m, field_data->data, len);160}161162static void ima_show_template_field_data(struct seq_file *m,163enum ima_show_type show,164enum data_formats datafmt,165struct ima_field_data *field_data)166{167switch (show) {168case IMA_SHOW_ASCII:169ima_show_template_data_ascii(m, show, datafmt, field_data);170break;171case IMA_SHOW_BINARY:172case IMA_SHOW_BINARY_NO_FIELD_LEN:173case IMA_SHOW_BINARY_OLD_STRING_FMT:174ima_show_template_data_binary(m, show, datafmt, field_data);175break;176default:177break;178}179}180181void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,182struct ima_field_data *field_data)183{184ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);185}186187void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,188struct ima_field_data *field_data)189{190ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,191field_data);192}193194void ima_show_template_digest_ngv2(struct seq_file *m, enum ima_show_type show,195struct ima_field_data *field_data)196{197ima_show_template_field_data(m, show,198DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,199field_data);200}201202void ima_show_template_string(struct seq_file *m, enum ima_show_type show,203struct ima_field_data *field_data)204{205ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);206}207208void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,209struct ima_field_data *field_data)210{211ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);212}213214void ima_show_template_buf(struct seq_file *m, enum ima_show_type show,215struct ima_field_data *field_data)216{217ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);218}219220void ima_show_template_uint(struct seq_file *m, enum ima_show_type show,221struct ima_field_data *field_data)222{223ima_show_template_field_data(m, show, DATA_FMT_UINT, field_data);224}225226/**227* ima_parse_buf() - Parses lengths and data from an input buffer228* @bufstartp: Buffer start address.229* @bufendp: Buffer end address.230* @bufcurp: Pointer to remaining (non-parsed) data.231* @maxfields: Length of fields array.232* @fields: Array containing lengths and pointers of parsed data.233* @curfields: Number of array items containing parsed data.234* @len_mask: Bitmap (if bit is set, data length should not be parsed).235* @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.236* @bufname: String identifier of the input buffer.237*238* Return: 0 on success, -EINVAL on error.239*/240int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,241int maxfields, struct ima_field_data *fields, int *curfields,242unsigned long *len_mask, int enforce_mask, char *bufname)243{244void *bufp = bufstartp;245int i;246247for (i = 0; i < maxfields; i++) {248if (len_mask == NULL || !test_bit(i, len_mask)) {249if (bufp > (bufendp - sizeof(u32)))250break;251252if (ima_canonical_fmt)253fields[i].len = le32_to_cpu(*(__le32 *)bufp);254else255fields[i].len = *(u32 *)bufp;256257bufp += sizeof(u32);258}259260if (bufp > (bufendp - fields[i].len))261break;262263fields[i].data = bufp;264bufp += fields[i].len;265}266267if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {268pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",269bufname, maxfields, i);270return -EINVAL;271}272273if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {274pr_err("%s: buf end mismatch: expected: %p, current: %p\n",275bufname, bufendp, bufp);276return -EINVAL;277}278279if (curfields)280*curfields = i;281282if (bufcurp)283*bufcurp = bufp;284285return 0;286}287288static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,289u8 digest_type, u8 hash_algo,290struct ima_field_data *field_data)291{292/*293* digest formats:294* - DATA_FMT_DIGEST: digest295* - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest,296* - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:297* <digest type> + ':' + <hash algo> + ':' + '\0' + digest,298*299* where 'DATA_FMT_DIGEST' is the original digest format ('d')300* with a hash size limitation of 20 bytes,301* where <digest type> is either "ima" or "verity",302* where <hash algo> is the hash_algo_name[] string.303*/304u8 buffer[DIGEST_TYPE_NAME_LEN_MAX + CRYPTO_MAX_ALG_NAME + 2 +305IMA_MAX_DIGEST_SIZE] = { 0 };306enum data_formats fmt = DATA_FMT_DIGEST;307u32 offset = 0;308309if (digest_type < DIGEST_TYPE__LAST && hash_algo < HASH_ALGO__LAST) {310fmt = DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO;311offset += 1 + sprintf(buffer, "%s:%s:",312digest_type_name[digest_type],313hash_algo_name[hash_algo]);314} else if (hash_algo < HASH_ALGO__LAST) {315fmt = DATA_FMT_DIGEST_WITH_ALGO;316offset += 1 + sprintf(buffer, "%s:",317hash_algo_name[hash_algo]);318}319320if (digest) {321memcpy(buffer + offset, digest, digestsize);322} else {323/*324* If digest is NULL, the event being recorded is a violation.325* Make room for the digest by increasing the offset by the326* hash algorithm digest size. If the hash algorithm is not327* specified increase the offset by IMA_DIGEST_SIZE which328* fits SHA1 or MD5329*/330if (hash_algo < HASH_ALGO__LAST)331offset += hash_digest_size[hash_algo];332else333offset += IMA_DIGEST_SIZE;334}335336return ima_write_template_field_data(buffer, offset + digestsize,337fmt, field_data);338}339340/*341* This function writes the digest of an event (with size limit).342*/343int ima_eventdigest_init(struct ima_event_data *event_data,344struct ima_field_data *field_data)345{346struct ima_max_digest_data hash;347struct ima_digest_data *hash_hdr = container_of(&hash.hdr,348struct ima_digest_data, hdr);349u8 *cur_digest = NULL;350u32 cur_digestsize = 0;351struct inode *inode;352int result;353354memset(&hash, 0, sizeof(hash));355356if (event_data->violation) /* recording a violation. */357goto out;358359if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {360cur_digest = event_data->iint->ima_hash->digest;361cur_digestsize = event_data->iint->ima_hash->length;362goto out;363}364365if ((const char *)event_data->filename == boot_aggregate_name) {366if (ima_tpm_chip) {367hash.hdr.algo = HASH_ALGO_SHA1;368result = ima_calc_boot_aggregate(hash_hdr);369370/* algo can change depending on available PCR banks */371if (!result && hash.hdr.algo != HASH_ALGO_SHA1)372result = -EINVAL;373374if (result < 0)375memset(&hash, 0, sizeof(hash));376}377378cur_digest = hash_hdr->digest;379cur_digestsize = hash_digest_size[HASH_ALGO_SHA1];380goto out;381}382383if (!event_data->file) /* missing info to re-calculate the digest */384return -EINVAL;385386inode = file_inode(event_data->file);387hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?388ima_hash_algo : HASH_ALGO_SHA1;389result = ima_calc_file_hash(event_data->file, hash_hdr);390if (result) {391integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,392event_data->filename, "collect_data",393"failed", result, 0);394return result;395}396cur_digest = hash_hdr->digest;397cur_digestsize = hash.hdr.length;398out:399return ima_eventdigest_init_common(cur_digest, cur_digestsize,400DIGEST_TYPE__LAST, HASH_ALGO__LAST,401field_data);402}403404/*405* This function writes the digest of an event (without size limit).406*/407int ima_eventdigest_ng_init(struct ima_event_data *event_data,408struct ima_field_data *field_data)409{410u8 *cur_digest = NULL, hash_algo = ima_hash_algo;411u32 cur_digestsize = 0;412413if (event_data->violation) /* recording a violation. */414goto out;415416cur_digest = event_data->iint->ima_hash->digest;417cur_digestsize = event_data->iint->ima_hash->length;418419hash_algo = event_data->iint->ima_hash->algo;420out:421return ima_eventdigest_init_common(cur_digest, cur_digestsize,422DIGEST_TYPE__LAST, hash_algo,423field_data);424}425426/*427* This function writes the digest of an event (without size limit),428* prefixed with both the digest type and hash algorithm.429*/430int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,431struct ima_field_data *field_data)432{433u8 *cur_digest = NULL, hash_algo = ima_hash_algo;434u32 cur_digestsize = 0;435u8 digest_type = DIGEST_TYPE_IMA;436437if (event_data->violation) /* recording a violation. */438goto out;439440cur_digest = event_data->iint->ima_hash->digest;441cur_digestsize = event_data->iint->ima_hash->length;442443hash_algo = event_data->iint->ima_hash->algo;444if (event_data->iint->flags & IMA_VERITY_REQUIRED)445digest_type = DIGEST_TYPE_VERITY;446out:447return ima_eventdigest_init_common(cur_digest, cur_digestsize,448digest_type, hash_algo,449field_data);450}451452/*453* This function writes the digest of the file which is expected to match the454* digest contained in the file's appended signature.455*/456int ima_eventdigest_modsig_init(struct ima_event_data *event_data,457struct ima_field_data *field_data)458{459enum hash_algo hash_algo;460const u8 *cur_digest;461u32 cur_digestsize;462463if (!event_data->modsig)464return 0;465466if (event_data->violation) {467/* Recording a violation. */468hash_algo = HASH_ALGO_SHA1;469cur_digest = NULL;470cur_digestsize = 0;471} else {472int rc;473474rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,475&cur_digest, &cur_digestsize);476if (rc)477return rc;478else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)479/* There was some error collecting the digest. */480return -EINVAL;481}482483return ima_eventdigest_init_common(cur_digest, cur_digestsize,484DIGEST_TYPE__LAST, hash_algo,485field_data);486}487488static int ima_eventname_init_common(struct ima_event_data *event_data,489struct ima_field_data *field_data,490bool size_limit)491{492const char *cur_filename = NULL;493struct name_snapshot filename;494u32 cur_filename_len = 0;495bool snapshot = false;496int ret;497498BUG_ON(event_data->filename == NULL && event_data->file == NULL);499500if (event_data->filename) {501cur_filename = event_data->filename;502cur_filename_len = strlen(event_data->filename);503504if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)505goto out;506}507508if (event_data->file) {509take_dentry_name_snapshot(&filename,510event_data->file->f_path.dentry);511snapshot = true;512cur_filename = filename.name.name;513cur_filename_len = strlen(cur_filename);514} else515/*516* Truncate filename if the latter is too long and517* the file descriptor is not available.518*/519cur_filename_len = IMA_EVENT_NAME_LEN_MAX;520out:521ret = ima_write_template_field_data(cur_filename, cur_filename_len,522DATA_FMT_STRING, field_data);523524if (snapshot)525release_dentry_name_snapshot(&filename);526527return ret;528}529530/*531* This function writes the name of an event (with size limit).532*/533int ima_eventname_init(struct ima_event_data *event_data,534struct ima_field_data *field_data)535{536return ima_eventname_init_common(event_data, field_data, true);537}538539/*540* This function writes the name of an event (without size limit).541*/542int ima_eventname_ng_init(struct ima_event_data *event_data,543struct ima_field_data *field_data)544{545return ima_eventname_init_common(event_data, field_data, false);546}547548/*549* ima_eventsig_init - include the file signature as part of the template data550*/551int ima_eventsig_init(struct ima_event_data *event_data,552struct ima_field_data *field_data)553{554struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;555556if (!xattr_value ||557(xattr_value->type != EVM_IMA_XATTR_DIGSIG &&558xattr_value->type != IMA_VERITY_DIGSIG))559return ima_eventevmsig_init(event_data, field_data);560561return ima_write_template_field_data(xattr_value, event_data->xattr_len,562DATA_FMT_HEX, field_data);563}564565/*566* ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the567* template data.568*/569int ima_eventbuf_init(struct ima_event_data *event_data,570struct ima_field_data *field_data)571{572if ((!event_data->buf) || (event_data->buf_len == 0))573return 0;574575return ima_write_template_field_data(event_data->buf,576event_data->buf_len, DATA_FMT_HEX,577field_data);578}579580/*581* ima_eventmodsig_init - include the appended file signature as part of the582* template data583*/584int ima_eventmodsig_init(struct ima_event_data *event_data,585struct ima_field_data *field_data)586{587const void *data;588u32 data_len;589int rc;590591if (!event_data->modsig)592return 0;593594/*595* modsig is a runtime structure containing pointers. Get its raw data596* instead.597*/598rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len);599if (rc)600return rc;601602return ima_write_template_field_data(data, data_len, DATA_FMT_HEX,603field_data);604}605606/*607* ima_eventevmsig_init - include the EVM portable signature as part of the608* template data609*/610int ima_eventevmsig_init(struct ima_event_data *event_data,611struct ima_field_data *field_data)612{613struct evm_ima_xattr_data *xattr_data = NULL;614int rc = 0;615616if (!event_data->file)617return 0;618619rc = vfs_getxattr_alloc(&nop_mnt_idmap, file_dentry(event_data->file),620XATTR_NAME_EVM, (char **)&xattr_data, 0,621GFP_NOFS);622if (rc <= 0 || xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG) {623rc = 0;624goto out;625}626627rc = ima_write_template_field_data((char *)xattr_data, rc, DATA_FMT_HEX,628field_data);629630out:631kfree(xattr_data);632return rc;633}634635static int ima_eventinodedac_init_common(struct ima_event_data *event_data,636struct ima_field_data *field_data,637bool get_uid)638{639unsigned int id;640641if (!event_data->file)642return 0;643644if (get_uid)645id = i_uid_read(file_inode(event_data->file));646else647id = i_gid_read(file_inode(event_data->file));648649if (ima_canonical_fmt) {650if (sizeof(id) == sizeof(u16))651id = (__force u16)cpu_to_le16(id);652else653id = (__force u32)cpu_to_le32(id);654}655656return ima_write_template_field_data((void *)&id, sizeof(id),657DATA_FMT_UINT, field_data);658}659660/*661* ima_eventinodeuid_init - include the inode UID as part of the template662* data663*/664int ima_eventinodeuid_init(struct ima_event_data *event_data,665struct ima_field_data *field_data)666{667return ima_eventinodedac_init_common(event_data, field_data, true);668}669670/*671* ima_eventinodegid_init - include the inode GID as part of the template672* data673*/674int ima_eventinodegid_init(struct ima_event_data *event_data,675struct ima_field_data *field_data)676{677return ima_eventinodedac_init_common(event_data, field_data, false);678}679680/*681* ima_eventinodemode_init - include the inode mode as part of the template682* data683*/684int ima_eventinodemode_init(struct ima_event_data *event_data,685struct ima_field_data *field_data)686{687struct inode *inode;688u16 mode;689690if (!event_data->file)691return 0;692693inode = file_inode(event_data->file);694mode = inode->i_mode;695if (ima_canonical_fmt)696mode = (__force u16)cpu_to_le16(mode);697698return ima_write_template_field_data((char *)&mode, sizeof(mode),699DATA_FMT_UINT, field_data);700}701702static int ima_eventinodexattrs_init_common(struct ima_event_data *event_data,703struct ima_field_data *field_data,704char type)705{706u8 *buffer = NULL;707int rc;708709if (!event_data->file)710return 0;711712rc = evm_read_protected_xattrs(file_dentry(event_data->file), NULL, 0,713type, ima_canonical_fmt);714if (rc < 0)715return 0;716717buffer = kmalloc(rc, GFP_KERNEL);718if (!buffer)719return 0;720721rc = evm_read_protected_xattrs(file_dentry(event_data->file), buffer,722rc, type, ima_canonical_fmt);723if (rc < 0) {724rc = 0;725goto out;726}727728rc = ima_write_template_field_data((char *)buffer, rc, DATA_FMT_HEX,729field_data);730out:731kfree(buffer);732return rc;733}734735/*736* ima_eventinodexattrnames_init - include a list of xattr names as part of the737* template data738*/739int ima_eventinodexattrnames_init(struct ima_event_data *event_data,740struct ima_field_data *field_data)741{742return ima_eventinodexattrs_init_common(event_data, field_data, 'n');743}744745/*746* ima_eventinodexattrlengths_init - include a list of xattr lengths as part of747* the template data748*/749int ima_eventinodexattrlengths_init(struct ima_event_data *event_data,750struct ima_field_data *field_data)751{752return ima_eventinodexattrs_init_common(event_data, field_data, 'l');753}754755/*756* ima_eventinodexattrvalues_init - include a list of xattr values as part of757* the template data758*/759int ima_eventinodexattrvalues_init(struct ima_event_data *event_data,760struct ima_field_data *field_data)761{762return ima_eventinodexattrs_init_common(event_data, field_data, 'v');763}764765766