linux/security/integrity/ima/ima_template_lib.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2013 Politecnico di Torino, Italy
   3 *                    TORSEC group -- http://security.polito.it
   4 *
   5 * Author: Roberto Sassu <roberto.sassu@polito.it>
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License as
   9 * published by the Free Software Foundation, version 2 of the
  10 * License.
  11 *
  12 * File: ima_template_lib.c
  13 *      Library of supported template fields.
  14 */
  15
  16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17
  18#include "ima_template_lib.h"
  19
  20static bool ima_template_hash_algo_allowed(u8 algo)
  21{
  22        if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
  23                return true;
  24
  25        return false;
  26}
  27
  28enum data_formats {
  29        DATA_FMT_DIGEST = 0,
  30        DATA_FMT_DIGEST_WITH_ALGO,
  31        DATA_FMT_STRING,
  32        DATA_FMT_HEX
  33};
  34
  35static int ima_write_template_field_data(const void *data, const u32 datalen,
  36                                         enum data_formats datafmt,
  37                                         struct ima_field_data *field_data)
  38{
  39        u8 *buf, *buf_ptr;
  40        u32 buflen = datalen;
  41
  42        if (datafmt == DATA_FMT_STRING)
  43                buflen = datalen + 1;
  44
  45        buf = kzalloc(buflen, GFP_KERNEL);
  46        if (!buf)
  47                return -ENOMEM;
  48
  49        memcpy(buf, data, datalen);
  50
  51        /*
  52         * Replace all space characters with underscore for event names and
  53         * strings. This avoid that, during the parsing of a measurements list,
  54         * filenames with spaces or that end with the suffix ' (deleted)' are
  55         * split into multiple template fields (the space is the delimitator
  56         * character for measurements lists in ASCII format).
  57         */
  58        if (datafmt == DATA_FMT_STRING) {
  59                for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
  60                        if (*buf_ptr == ' ')
  61                                *buf_ptr = '_';
  62        }
  63
  64        field_data->data = buf;
  65        field_data->len = buflen;
  66        return 0;
  67}
  68
  69static void ima_show_template_data_ascii(struct seq_file *m,
  70                                         enum ima_show_type show,
  71                                         enum data_formats datafmt,
  72                                         struct ima_field_data *field_data)
  73{
  74        u8 *buf_ptr = field_data->data;
  75        u32 buflen = field_data->len;
  76
  77        switch (datafmt) {
  78        case DATA_FMT_DIGEST_WITH_ALGO:
  79                buf_ptr = strnchr(field_data->data, buflen, ':');
  80                if (buf_ptr != field_data->data)
  81                        seq_printf(m, "%s", field_data->data);
  82
  83                /* skip ':' and '\0' */
  84                buf_ptr += 2;
  85                buflen -= buf_ptr - field_data->data;
  86        case DATA_FMT_DIGEST:
  87        case DATA_FMT_HEX:
  88                if (!buflen)
  89                        break;
  90                ima_print_digest(m, buf_ptr, buflen);
  91                break;
  92        case DATA_FMT_STRING:
  93                seq_printf(m, "%s", buf_ptr);
  94                break;
  95        default:
  96                break;
  97        }
  98}
  99
 100static void ima_show_template_data_binary(struct seq_file *m,
 101                                          enum ima_show_type show,
 102                                          enum data_formats datafmt,
 103                                          struct ima_field_data *field_data)
 104{
 105        u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
 106            strlen(field_data->data) : field_data->len;
 107
 108        if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
 109                u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
 110
 111                ima_putc(m, &field_len, sizeof(field_len));
 112        }
 113
 114        if (!len)
 115                return;
 116
 117        ima_putc(m, field_data->data, len);
 118}
 119
 120static void ima_show_template_field_data(struct seq_file *m,
 121                                         enum ima_show_type show,
 122                                         enum data_formats datafmt,
 123                                         struct ima_field_data *field_data)
 124{
 125        switch (show) {
 126        case IMA_SHOW_ASCII:
 127                ima_show_template_data_ascii(m, show, datafmt, field_data);
 128                break;
 129        case IMA_SHOW_BINARY:
 130        case IMA_SHOW_BINARY_NO_FIELD_LEN:
 131        case IMA_SHOW_BINARY_OLD_STRING_FMT:
 132                ima_show_template_data_binary(m, show, datafmt, field_data);
 133                break;
 134        default:
 135                break;
 136        }
 137}
 138
 139void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
 140                              struct ima_field_data *field_data)
 141{
 142        ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
 143}
 144
 145void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
 146                                 struct ima_field_data *field_data)
 147{
 148        ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
 149                                     field_data);
 150}
 151
 152void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
 153                              struct ima_field_data *field_data)
 154{
 155        ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
 156}
 157
 158void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
 159                           struct ima_field_data *field_data)
 160{
 161        ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
 162}
 163
 164void ima_show_template_buf(struct seq_file *m, enum ima_show_type show,
 165                           struct ima_field_data *field_data)
 166{
 167        ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
 168}
 169
 170/**
 171 * ima_parse_buf() - Parses lengths and data from an input buffer
 172 * @bufstartp:       Buffer start address.
 173 * @bufendp:         Buffer end address.
 174 * @bufcurp:         Pointer to remaining (non-parsed) data.
 175 * @maxfields:       Length of fields array.
 176 * @fields:          Array containing lengths and pointers of parsed data.
 177 * @curfields:       Number of array items containing parsed data.
 178 * @len_mask:        Bitmap (if bit is set, data length should not be parsed).
 179 * @enforce_mask:    Check if curfields == maxfields and/or bufcurp == bufendp.
 180 * @bufname:         String identifier of the input buffer.
 181 *
 182 * Return: 0 on success, -EINVAL on error.
 183 */
 184int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
 185                  int maxfields, struct ima_field_data *fields, int *curfields,
 186                  unsigned long *len_mask, int enforce_mask, char *bufname)
 187{
 188        void *bufp = bufstartp;
 189        int i;
 190
 191        for (i = 0; i < maxfields; i++) {
 192                if (len_mask == NULL || !test_bit(i, len_mask)) {
 193                        if (bufp > (bufendp - sizeof(u32)))
 194                                break;
 195
 196                        fields[i].len = *(u32 *)bufp;
 197                        if (ima_canonical_fmt)
 198                                fields[i].len = le32_to_cpu(fields[i].len);
 199
 200                        bufp += sizeof(u32);
 201                }
 202
 203                if (bufp > (bufendp - fields[i].len))
 204                        break;
 205
 206                fields[i].data = bufp;
 207                bufp += fields[i].len;
 208        }
 209
 210        if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
 211                pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
 212                       bufname, maxfields, i);
 213                return -EINVAL;
 214        }
 215
 216        if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
 217                pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
 218                       bufname, bufendp, bufp);
 219                return -EINVAL;
 220        }
 221
 222        if (curfields)
 223                *curfields = i;
 224
 225        if (bufcurp)
 226                *bufcurp = bufp;
 227
 228        return 0;
 229}
 230
 231static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,
 232                                       u8 hash_algo,
 233                                       struct ima_field_data *field_data)
 234{
 235        /*
 236         * digest formats:
 237         *  - DATA_FMT_DIGEST: digest
 238         *  - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
 239         *    where <hash algo> is provided if the hash algoritm is not
 240         *    SHA1 or MD5
 241         */
 242        u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
 243        enum data_formats fmt = DATA_FMT_DIGEST;
 244        u32 offset = 0;
 245
 246        if (hash_algo < HASH_ALGO__LAST) {
 247                fmt = DATA_FMT_DIGEST_WITH_ALGO;
 248                offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
 249                                   hash_algo_name[hash_algo]);
 250                buffer[offset] = ':';
 251                offset += 2;
 252        }
 253
 254        if (digest)
 255                memcpy(buffer + offset, digest, digestsize);
 256        else
 257                /*
 258                 * If digest is NULL, the event being recorded is a violation.
 259                 * Make room for the digest by increasing the offset of
 260                 * IMA_DIGEST_SIZE.
 261                 */
 262                offset += IMA_DIGEST_SIZE;
 263
 264        return ima_write_template_field_data(buffer, offset + digestsize,
 265                                             fmt, field_data);
 266}
 267
 268/*
 269 * This function writes the digest of an event (with size limit).
 270 */
 271int ima_eventdigest_init(struct ima_event_data *event_data,
 272                         struct ima_field_data *field_data)
 273{
 274        struct {
 275                struct ima_digest_data hdr;
 276                char digest[IMA_MAX_DIGEST_SIZE];
 277        } hash;
 278        u8 *cur_digest = NULL;
 279        u32 cur_digestsize = 0;
 280        struct inode *inode;
 281        int result;
 282
 283        memset(&hash, 0, sizeof(hash));
 284
 285        if (event_data->violation)      /* recording a violation. */
 286                goto out;
 287
 288        if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
 289                cur_digest = event_data->iint->ima_hash->digest;
 290                cur_digestsize = event_data->iint->ima_hash->length;
 291                goto out;
 292        }
 293
 294        if ((const char *)event_data->filename == boot_aggregate_name) {
 295                if (ima_tpm_chip) {
 296                        hash.hdr.algo = HASH_ALGO_SHA1;
 297                        result = ima_calc_boot_aggregate(&hash.hdr);
 298
 299                        /* algo can change depending on available PCR banks */
 300                        if (!result && hash.hdr.algo != HASH_ALGO_SHA1)
 301                                result = -EINVAL;
 302
 303                        if (result < 0)
 304                                memset(&hash, 0, sizeof(hash));
 305                }
 306
 307                cur_digest = hash.hdr.digest;
 308                cur_digestsize = hash_digest_size[HASH_ALGO_SHA1];
 309                goto out;
 310        }
 311
 312        if (!event_data->file)  /* missing info to re-calculate the digest */
 313                return -EINVAL;
 314
 315        inode = file_inode(event_data->file);
 316        hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
 317            ima_hash_algo : HASH_ALGO_SHA1;
 318        result = ima_calc_file_hash(event_data->file, &hash.hdr);
 319        if (result) {
 320                integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
 321                                    event_data->filename, "collect_data",
 322                                    "failed", result, 0);
 323                return result;
 324        }
 325        cur_digest = hash.hdr.digest;
 326        cur_digestsize = hash.hdr.length;
 327out:
 328        return ima_eventdigest_init_common(cur_digest, cur_digestsize,
 329                                           HASH_ALGO__LAST, field_data);
 330}
 331
 332/*
 333 * This function writes the digest of an event (without size limit).
 334 */
 335int ima_eventdigest_ng_init(struct ima_event_data *event_data,
 336                            struct ima_field_data *field_data)
 337{
 338        u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
 339        u32 cur_digestsize = 0;
 340
 341        if (event_data->violation)      /* recording a violation. */
 342                goto out;
 343
 344        cur_digest = event_data->iint->ima_hash->digest;
 345        cur_digestsize = event_data->iint->ima_hash->length;
 346
 347        hash_algo = event_data->iint->ima_hash->algo;
 348out:
 349        return ima_eventdigest_init_common(cur_digest, cur_digestsize,
 350                                           hash_algo, field_data);
 351}
 352
 353/*
 354 * This function writes the digest of the file which is expected to match the
 355 * digest contained in the file's appended signature.
 356 */
 357int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
 358                                struct ima_field_data *field_data)
 359{
 360        enum hash_algo hash_algo;
 361        const u8 *cur_digest;
 362        u32 cur_digestsize;
 363
 364        if (!event_data->modsig)
 365                return 0;
 366
 367        if (event_data->violation) {
 368                /* Recording a violation. */
 369                hash_algo = HASH_ALGO_SHA1;
 370                cur_digest = NULL;
 371                cur_digestsize = 0;
 372        } else {
 373                int rc;
 374
 375                rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,
 376                                           &cur_digest, &cur_digestsize);
 377                if (rc)
 378                        return rc;
 379                else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
 380                        /* There was some error collecting the digest. */
 381                        return -EINVAL;
 382        }
 383
 384        return ima_eventdigest_init_common(cur_digest, cur_digestsize,
 385                                           hash_algo, field_data);
 386}
 387
 388static int ima_eventname_init_common(struct ima_event_data *event_data,
 389                                     struct ima_field_data *field_data,
 390                                     bool size_limit)
 391{
 392        const char *cur_filename = NULL;
 393        u32 cur_filename_len = 0;
 394
 395        BUG_ON(event_data->filename == NULL && event_data->file == NULL);
 396
 397        if (event_data->filename) {
 398                cur_filename = event_data->filename;
 399                cur_filename_len = strlen(event_data->filename);
 400
 401                if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
 402                        goto out;
 403        }
 404
 405        if (event_data->file) {
 406                cur_filename = event_data->file->f_path.dentry->d_name.name;
 407                cur_filename_len = strlen(cur_filename);
 408        } else
 409                /*
 410                 * Truncate filename if the latter is too long and
 411                 * the file descriptor is not available.
 412                 */
 413                cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
 414out:
 415        return ima_write_template_field_data(cur_filename, cur_filename_len,
 416                                             DATA_FMT_STRING, field_data);
 417}
 418
 419/*
 420 * This function writes the name of an event (with size limit).
 421 */
 422int ima_eventname_init(struct ima_event_data *event_data,
 423                       struct ima_field_data *field_data)
 424{
 425        return ima_eventname_init_common(event_data, field_data, true);
 426}
 427
 428/*
 429 * This function writes the name of an event (without size limit).
 430 */
 431int ima_eventname_ng_init(struct ima_event_data *event_data,
 432                          struct ima_field_data *field_data)
 433{
 434        return ima_eventname_init_common(event_data, field_data, false);
 435}
 436
 437/*
 438 *  ima_eventsig_init - include the file signature as part of the template data
 439 */
 440int ima_eventsig_init(struct ima_event_data *event_data,
 441                      struct ima_field_data *field_data)
 442{
 443        struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
 444
 445        if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
 446                return 0;
 447
 448        return ima_write_template_field_data(xattr_value, event_data->xattr_len,
 449                                             DATA_FMT_HEX, field_data);
 450}
 451
 452/*
 453 *  ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the
 454 *  template data.
 455 */
 456int ima_eventbuf_init(struct ima_event_data *event_data,
 457                      struct ima_field_data *field_data)
 458{
 459        if ((!event_data->buf) || (event_data->buf_len == 0))
 460                return 0;
 461
 462        return ima_write_template_field_data(event_data->buf,
 463                                             event_data->buf_len, DATA_FMT_HEX,
 464                                             field_data);
 465}
 466
 467/*
 468 *  ima_eventmodsig_init - include the appended file signature as part of the
 469 *  template data
 470 */
 471int ima_eventmodsig_init(struct ima_event_data *event_data,
 472                         struct ima_field_data *field_data)
 473{
 474        const void *data;
 475        u32 data_len;
 476        int rc;
 477
 478        if (!event_data->modsig)
 479                return 0;
 480
 481        /*
 482         * modsig is a runtime structure containing pointers. Get its raw data
 483         * instead.
 484         */
 485        rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len);
 486        if (rc)
 487                return rc;
 488
 489        return ima_write_template_field_data(data, data_len, DATA_FMT_HEX,
 490                                             field_data);
 491}
 492