Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005,2006,2007,2008 IBM Corporation |
| 3 | * |
| 4 | * Authors: |
| 5 | * Reiner Sailer <sailer@watson.ibm.com> |
| 6 | * Serge Hallyn <serue@us.ibm.com> |
| 7 | * Kylene Hall <kylene@us.ibm.com> |
| 8 | * Mimi Zohar <zohar@us.ibm.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation, version 2 of the |
| 13 | * License. |
| 14 | * |
| 15 | * File: ima_main.c |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 16 | * implements the IMA hooks: ima_bprm_check, ima_file_mmap, |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 17 | * and ima_file_check. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 18 | */ |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/binfmts.h> |
| 22 | #include <linux/mount.h> |
| 23 | #include <linux/mman.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 25 | #include <linux/xattr.h> |
James Morris | d5813a5 | 2011-08-30 10:19:50 +1000 | [diff] [blame] | 26 | #include <linux/ima.h> |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame^] | 27 | #include <crypto/hash_info.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 28 | |
| 29 | #include "ima.h" |
| 30 | |
| 31 | int ima_initialized; |
| 32 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 33 | #ifdef CONFIG_IMA_APPRAISE |
| 34 | int ima_appraise = IMA_APPRAISE_ENFORCE; |
| 35 | #else |
| 36 | int ima_appraise; |
| 37 | #endif |
| 38 | |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame^] | 39 | int ima_hash_algo = HASH_ALGO_SHA1; |
| 40 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 41 | static int __init hash_setup(char *str) |
| 42 | { |
Mimi Zohar | 07ff7a0 | 2009-05-05 13:13:10 -0400 | [diff] [blame] | 43 | if (strncmp(str, "md5", 3) == 0) |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame^] | 44 | ima_hash_algo = HASH_ALGO_MD5; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 45 | return 1; |
| 46 | } |
| 47 | __setup("ima_hash=", hash_setup); |
| 48 | |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 49 | /* |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 50 | * ima_rdwr_violation_check |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 51 | * |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 52 | * Only invalidate the PCR for measured files: |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 53 | * - Opening a file for write when already open for read, |
| 54 | * results in a time of measure, time of use (ToMToU) error. |
| 55 | * - Opening a file for read when already open for write, |
| 56 | * could result in a file measurement error. |
| 57 | * |
| 58 | */ |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 59 | static void ima_rdwr_violation_check(struct file *file) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 60 | { |
| 61 | struct dentry *dentry = file->f_path.dentry; |
David Howells | c77cece | 2013-06-13 23:37:49 +0100 | [diff] [blame] | 62 | struct inode *inode = file_inode(file); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 63 | fmode_t mode = file->f_mode; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 64 | int must_measure; |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 65 | bool send_tomtou = false, send_writers = false; |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 66 | char *pathbuf = NULL; |
| 67 | const char *pathname; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 68 | |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 69 | if (!S_ISREG(inode->i_mode) || !ima_initialized) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 70 | return; |
Eric Paris | a178d20 | 2010-10-25 14:41:59 -0400 | [diff] [blame] | 71 | |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 72 | mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */ |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 73 | |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 74 | if (mode & FMODE_WRITE) { |
Mimi Zohar | a68a27b | 2010-11-02 10:10:56 -0400 | [diff] [blame] | 75 | if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 76 | send_tomtou = true; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 77 | goto out; |
| 78 | } |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 79 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 80 | must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK); |
| 81 | if (!must_measure) |
Eric Paris | bade72d | 2010-10-25 14:42:25 -0400 | [diff] [blame] | 82 | goto out; |
| 83 | |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 84 | if (atomic_read(&inode->i_writecount) > 0) |
| 85 | send_writers = true; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 86 | out: |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 87 | mutex_unlock(&inode->i_mutex); |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 88 | |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 89 | if (!send_tomtou && !send_writers) |
| 90 | return; |
| 91 | |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 92 | pathname = ima_d_path(&file->f_path, &pathbuf); |
| 93 | if (!pathname || strlen(pathname) > IMA_EVENT_NAME_LEN_MAX) |
| 94 | pathname = dentry->d_name.name; |
| 95 | |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 96 | if (send_tomtou) |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 97 | ima_add_violation(inode, pathname, |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 98 | "invalid_pcr", "ToMToU"); |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 99 | if (send_writers) |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 100 | ima_add_violation(inode, pathname, |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 101 | "invalid_pcr", "open_writers"); |
| 102 | kfree(pathbuf); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 105 | static void ima_check_last_writer(struct integrity_iint_cache *iint, |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 106 | struct inode *inode, struct file *file) |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 107 | { |
Al Viro | 4b2a2c6 | 2011-07-26 04:30:35 -0400 | [diff] [blame] | 108 | fmode_t mode = file->f_mode; |
Eric Paris | 497f323 | 2010-10-25 14:41:32 -0400 | [diff] [blame] | 109 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 110 | if (!(mode & FMODE_WRITE)) |
| 111 | return; |
| 112 | |
| 113 | mutex_lock(&inode->i_mutex); |
| 114 | if (atomic_read(&inode->i_writecount) == 1 && |
| 115 | iint->version != inode->i_version) { |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 116 | iint->flags &= ~IMA_DONE_MASK; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 117 | if (iint->flags & IMA_APPRAISE) |
| 118 | ima_update_xattr(iint, file); |
| 119 | } |
| 120 | mutex_unlock(&inode->i_mutex); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 123 | /** |
| 124 | * ima_file_free - called on __fput() |
| 125 | * @file: pointer to file structure being freed |
| 126 | * |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 127 | * Flag files that changed, based on i_version |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 128 | */ |
| 129 | void ima_file_free(struct file *file) |
| 130 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 131 | struct inode *inode = file_inode(file); |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 132 | struct integrity_iint_cache *iint; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 133 | |
Mimi Zohar | e950598 | 2010-08-31 09:38:51 -0400 | [diff] [blame] | 134 | if (!iint_initialized || !S_ISREG(inode->i_mode)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 135 | return; |
Eric Paris | 196f518 | 2010-10-25 14:42:19 -0400 | [diff] [blame] | 136 | |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 137 | iint = integrity_iint_find(inode); |
Mimi Zohar | 854fdd5 | 2010-11-02 10:14:22 -0400 | [diff] [blame] | 138 | if (!iint) |
| 139 | return; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 140 | |
Mimi Zohar | 854fdd5 | 2010-11-02 10:14:22 -0400 | [diff] [blame] | 141 | ima_check_last_writer(iint, inode, file); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 144 | static int process_measurement(struct file *file, const char *filename, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 145 | int mask, int function) |
| 146 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 147 | struct inode *inode = file_inode(file); |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 148 | struct integrity_iint_cache *iint; |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 149 | char *pathbuf = NULL; |
| 150 | const char *pathname = NULL; |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 151 | int rc = -ENOMEM, action, must_appraise, _func; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 152 | |
| 153 | if (!ima_initialized || !S_ISREG(inode->i_mode)) |
| 154 | return 0; |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 155 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 156 | /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action |
| 157 | * bitmask based on the appraise/audit/measurement policy. |
| 158 | * Included is the appraise submask. |
| 159 | */ |
Dmitry Kasatkin | d9d300c | 2012-06-27 11:26:14 +0300 | [diff] [blame] | 160 | action = ima_get_action(inode, mask, function); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 161 | if (!action) |
| 162 | return 0; |
| 163 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 164 | must_appraise = action & IMA_APPRAISE; |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 165 | |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 166 | /* Is the appraise rule hook specific? */ |
| 167 | _func = (action & IMA_FILE_APPRAISE) ? FILE_CHECK : function; |
| 168 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 169 | mutex_lock(&inode->i_mutex); |
| 170 | |
Dmitry Kasatkin | bf2276d | 2011-10-19 12:04:40 +0300 | [diff] [blame] | 171 | iint = integrity_inode_get(inode); |
| 172 | if (!iint) |
| 173 | goto out; |
| 174 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 175 | /* Determine if already appraised/measured based on bitmask |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 176 | * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED, |
| 177 | * IMA_AUDIT, IMA_AUDITED) |
| 178 | */ |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 179 | iint->flags |= action; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 180 | action &= IMA_DO_MASK; |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 181 | action &= ~((iint->flags & IMA_DONE_MASK) >> 1); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 182 | |
| 183 | /* Nothing to do, just return existing appraised status */ |
| 184 | if (!action) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 185 | if (must_appraise) |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 186 | rc = ima_get_cache_status(iint, _func); |
Dmitry Kasatkin | a175b8b | 2012-09-27 15:06:28 +0300 | [diff] [blame] | 187 | goto out_digsig; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 188 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 189 | |
| 190 | rc = ima_collect_measurement(iint, file); |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 191 | if (rc != 0) |
Dmitry Kasatkin | a175b8b | 2012-09-27 15:06:28 +0300 | [diff] [blame] | 192 | goto out_digsig; |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 193 | |
Mimi Zohar | df2c2af | 2013-04-15 11:27:20 -0400 | [diff] [blame] | 194 | pathname = !filename ? ima_d_path(&file->f_path, &pathbuf) : filename; |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 195 | if (!pathname) |
Mimi Zohar | df2c2af | 2013-04-15 11:27:20 -0400 | [diff] [blame] | 196 | pathname = (const char *)file->f_dentry->d_name.name; |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 197 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 198 | if (action & IMA_MEASURE) |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 199 | ima_store_measurement(iint, file, pathname); |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 200 | if (action & IMA_APPRAISE_SUBMASK) |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 201 | rc = ima_appraise_measurement(_func, iint, file, pathname); |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 202 | if (action & IMA_AUDIT) |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 203 | ima_audit_measurement(iint, pathname); |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 204 | kfree(pathbuf); |
Dmitry Kasatkin | a175b8b | 2012-09-27 15:06:28 +0300 | [diff] [blame] | 205 | out_digsig: |
| 206 | if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG)) |
| 207 | rc = -EACCES; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 208 | out: |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 209 | mutex_unlock(&inode->i_mutex); |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 210 | if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE)) |
| 211 | return -EACCES; |
| 212 | return 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * ima_file_mmap - based on policy, collect/store measurement. |
| 217 | * @file: pointer to the file to be measured (May be NULL) |
| 218 | * @prot: contains the protection that will be applied by the kernel. |
| 219 | * |
| 220 | * Measure files being mmapped executable based on the ima_must_measure() |
| 221 | * policy decision. |
| 222 | * |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 223 | * On success return 0. On integrity appraisal error, assuming the file |
| 224 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 225 | */ |
| 226 | int ima_file_mmap(struct file *file, unsigned long prot) |
| 227 | { |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 228 | if (file && (prot & PROT_EXEC)) |
Mimi Zohar | df2c2af | 2013-04-15 11:27:20 -0400 | [diff] [blame] | 229 | return process_measurement(file, NULL, MAY_EXEC, MMAP_CHECK); |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 230 | return 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * ima_bprm_check - based on policy, collect/store measurement. |
| 235 | * @bprm: contains the linux_binprm structure |
| 236 | * |
| 237 | * The OS protects against an executable file, already open for write, |
| 238 | * from being executed in deny_write_access() and an executable file, |
| 239 | * already open for execute, from being modified in get_write_access(). |
| 240 | * So we can be certain that what we verify and measure here is actually |
| 241 | * what is being executed. |
| 242 | * |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 243 | * On success return 0. On integrity appraisal error, assuming the file |
| 244 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 245 | */ |
| 246 | int ima_bprm_check(struct linux_binprm *bprm) |
| 247 | { |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 248 | return process_measurement(bprm->file, |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 249 | (strcmp(bprm->filename, bprm->interp) == 0) ? |
| 250 | bprm->filename : bprm->interp, |
| 251 | MAY_EXEC, BPRM_CHECK); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 254 | /** |
| 255 | * ima_path_check - based on policy, collect/store measurement. |
| 256 | * @file: pointer to the file to be measured |
| 257 | * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE |
| 258 | * |
| 259 | * Measure files based on the ima_must_measure() policy decision. |
| 260 | * |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 261 | * On success return 0. On integrity appraisal error, assuming the file |
| 262 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 263 | */ |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 264 | int ima_file_check(struct file *file, int mask) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 265 | { |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 266 | ima_rdwr_violation_check(file); |
Mimi Zohar | df2c2af | 2013-04-15 11:27:20 -0400 | [diff] [blame] | 267 | return process_measurement(file, NULL, |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 268 | mask & (MAY_READ | MAY_WRITE | MAY_EXEC), |
| 269 | FILE_CHECK); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 270 | } |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 271 | EXPORT_SYMBOL_GPL(ima_file_check); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 272 | |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 273 | /** |
| 274 | * ima_module_check - based on policy, collect/store/appraise measurement. |
| 275 | * @file: pointer to the file to be measured/appraised |
| 276 | * |
| 277 | * Measure/appraise kernel modules based on policy. |
| 278 | * |
Dmitry Kasatkin | 750943a | 2012-09-27 15:57:10 +0300 | [diff] [blame] | 279 | * On success return 0. On integrity appraisal error, assuming the file |
| 280 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 281 | */ |
| 282 | int ima_module_check(struct file *file) |
| 283 | { |
Mimi Zohar | a7f2a36 | 2012-12-21 08:34:21 -0500 | [diff] [blame] | 284 | if (!file) { |
Mimi Zohar | a7f2a36 | 2012-12-21 08:34:21 -0500 | [diff] [blame] | 285 | #ifndef CONFIG_MODULE_SIG_FORCE |
Mimi Zohar | a2c2c3a7 | 2013-02-24 23:42:36 -0500 | [diff] [blame] | 286 | if ((ima_appraise & IMA_APPRAISE_MODULES) && |
| 287 | (ima_appraise & IMA_APPRAISE_ENFORCE)) |
Linus Torvalds | 33673dc | 2013-02-21 08:18:12 -0800 | [diff] [blame] | 288 | return -EACCES; /* INTEGRITY_UNKNOWN */ |
Mimi Zohar | a7f2a36 | 2012-12-21 08:34:21 -0500 | [diff] [blame] | 289 | #endif |
Linus Torvalds | 33673dc | 2013-02-21 08:18:12 -0800 | [diff] [blame] | 290 | return 0; /* We rely on module signature checking */ |
| 291 | } |
Mimi Zohar | df2c2af | 2013-04-15 11:27:20 -0400 | [diff] [blame] | 292 | return process_measurement(file, NULL, MAY_EXEC, MODULE_CHECK); |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 293 | } |
| 294 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 295 | static int __init init_ima(void) |
| 296 | { |
| 297 | int error; |
| 298 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 299 | error = ima_init(); |
Dmitry Kasatkin | 7ff2267 | 2012-06-25 12:18:11 +0300 | [diff] [blame] | 300 | if (!error) |
| 301 | ima_initialized = 1; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 302 | return error; |
| 303 | } |
| 304 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 305 | late_initcall(init_ima); /* Start IMA after the TPM is available */ |
| 306 | |
| 307 | MODULE_DESCRIPTION("Integrity Measurement Architecture"); |
| 308 | MODULE_LICENSE("GPL"); |