ima_appraise.c 9.5 KB
Newer Older
M
Mimi Zohar 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2011 IBM Corporation
 *
 * Author:
 * Mimi Zohar <zohar@us.ibm.com>
 *
 * 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.
 */
#include <linux/module.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/xattr.h>
#include <linux/magic.h>
#include <linux/ima.h>
#include <linux/evm.h>
18
#include <crypto/hash_info.h>
M
Mimi Zohar 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#include "ima.h"

static int __init default_appraise_setup(char *str)
{
	if (strncmp(str, "off", 3) == 0)
		ima_appraise = 0;
	else if (strncmp(str, "fix", 3) == 0)
		ima_appraise = IMA_APPRAISE_FIX;
	return 1;
}

__setup("ima_appraise=", default_appraise_setup);

/*
 * ima_must_appraise - set appraise flag
 *
 * Return 1 to appraise
 */
D
Dmitry Kasatkin 已提交
38
int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
M
Mimi Zohar 已提交
39
{
40 41 42 43
	if (!ima_appraise)
		return 0;

	return ima_match_policy(inode, func, mask, IMA_APPRAISE);
M
Mimi Zohar 已提交
44 45
}

46
static int ima_fix_xattr(struct dentry *dentry,
47
			 struct integrity_iint_cache *iint)
M
Mimi Zohar 已提交
48
{
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	int rc, offset;
	u8 algo = iint->ima_hash->algo;

	if (algo <= HASH_ALGO_SHA1) {
		offset = 1;
		iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
	} else {
		offset = 0;
		iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
		iint->ima_hash->xattr.ng.algo = algo;
	}
	rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
				   &iint->ima_hash->xattr.data[offset],
				   (sizeof(iint->ima_hash->xattr) - offset) +
				   iint->ima_hash->length, 0);
	return rc;
M
Mimi Zohar 已提交
65 66
}

67 68 69 70
/* Return specific func appraised cached result */
enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
					   int func)
{
D
Dmitry Kasatkin 已提交
71
	switch (func) {
72 73 74 75 76 77
	case MMAP_CHECK:
		return iint->ima_mmap_status;
	case BPRM_CHECK:
		return iint->ima_bprm_status;
	case MODULE_CHECK:
		return iint->ima_module_status;
78 79
	case FIRMWARE_CHECK:
		return iint->ima_firmware_status;
80 81 82 83 84 85 86 87 88
	case FILE_CHECK:
	default:
		return iint->ima_file_status;
	}
}

static void ima_set_cache_status(struct integrity_iint_cache *iint,
				 int func, enum integrity_status status)
{
D
Dmitry Kasatkin 已提交
89
	switch (func) {
90 91 92 93 94 95 96 97 98
	case MMAP_CHECK:
		iint->ima_mmap_status = status;
		break;
	case BPRM_CHECK:
		iint->ima_bprm_status = status;
		break;
	case MODULE_CHECK:
		iint->ima_module_status = status;
		break;
99 100 101
	case FIRMWARE_CHECK:
		iint->ima_firmware_status = status;
		break;
102 103 104 105 106 107 108 109 110
	case FILE_CHECK:
	default:
		iint->ima_file_status = status;
		break;
	}
}

static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
{
D
Dmitry Kasatkin 已提交
111
	switch (func) {
112 113 114 115 116 117 118 119 120
	case MMAP_CHECK:
		iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
		break;
	case BPRM_CHECK:
		iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
		break;
	case MODULE_CHECK:
		iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
		break;
121 122 123
	case FIRMWARE_CHECK:
		iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
		break;
124 125 126 127 128 129 130
	case FILE_CHECK:
	default:
		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
		break;
	}
}

131 132 133 134 135
void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
		       struct ima_digest_data *hash)
{
	struct signature_v2_hdr *sig;

136
	if (!xattr_value || xattr_len < 2)
137 138
		return;

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	switch (xattr_value->type) {
	case EVM_IMA_XATTR_DIGSIG:
		sig = (typeof(sig))xattr_value;
		if (sig->version != 2 || xattr_len <= sizeof(*sig))
			return;
		hash->algo = sig->hash_algo;
		break;
	case IMA_XATTR_DIGEST_NG:
		hash->algo = xattr_value->digest[0];
		break;
	case IMA_XATTR_DIGEST:
		/* this is for backward compatibility */
		if (xattr_len == 21) {
			unsigned int zero = 0;
			if (!memcmp(&xattr_value->digest[16], &zero, 4))
				hash->algo = HASH_ALGO_MD5;
			else
				hash->algo = HASH_ALGO_SHA1;
		} else if (xattr_len == 17)
			hash->algo = HASH_ALGO_MD5;
		break;
	}
161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

int ima_read_xattr(struct dentry *dentry,
		   struct evm_ima_xattr_data **xattr_value)
{
	struct inode *inode = dentry->d_inode;

	if (!inode->i_op->getxattr)
		return 0;

	return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
				  0, GFP_NOFS);
}

M
Mimi Zohar 已提交
175 176 177 178 179 180 181 182
/*
 * ima_appraise_measurement - appraise file measurement
 *
 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
 * Assuming success, compare the xattr hash with the collected measurement.
 *
 * Return 0 on success, error code otherwise
 */
183
int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
184 185 186
			     struct file *file, const unsigned char *filename,
			     struct evm_ima_xattr_data *xattr_value,
			     int xattr_len)
M
Mimi Zohar 已提交
187
{
188 189
	static const char op[] = "appraise_data";
	char *cause = "unknown";
M
Mimi Zohar 已提交
190 191 192
	struct dentry *dentry = file->f_dentry;
	struct inode *inode = dentry->d_inode;
	enum integrity_status status = INTEGRITY_UNKNOWN;
193
	int rc = xattr_len, hash_start = 0;
M
Mimi Zohar 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

	if (!ima_appraise)
		return 0;
	if (!inode->i_op->getxattr)
		return INTEGRITY_UNKNOWN;

	if (rc <= 0) {
		if (rc && rc != -ENODATA)
			goto out;

		cause = "missing-hash";
		status =
		    (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
		goto out;
	}

210
	status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
M
Mimi Zohar 已提交
211 212 213 214 215 216 217 218
	if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
		if ((status == INTEGRITY_NOLABEL)
		    || (status == INTEGRITY_NOXATTRS))
			cause = "missing-HMAC";
		else if (status == INTEGRITY_FAIL)
			cause = "invalid-HMAC";
		goto out;
	}
219
	switch (xattr_value->type) {
220 221 222
	case IMA_XATTR_DIGEST_NG:
		/* first byte contains algorithm id */
		hash_start = 1;
223
	case IMA_XATTR_DIGEST:
224
		if (iint->flags & IMA_DIGSIG_REQUIRED) {
225
			cause = "IMA-signature-required";
226 227 228
			status = INTEGRITY_FAIL;
			break;
		}
229 230
		if (xattr_len - sizeof(xattr_value->type) - hash_start >=
				iint->ima_hash->length)
231 232 233
			/* xattr length may be longer. md5 hash in previous
			   version occupied 20 bytes in xattr, instead of 16
			 */
234
			rc = memcmp(&xattr_value->digest[hash_start],
235 236
				    iint->ima_hash->digest,
				    iint->ima_hash->length);
237 238
		else
			rc = -EINVAL;
239 240 241 242 243 244 245 246 247 248
		if (rc) {
			cause = "invalid-hash";
			status = INTEGRITY_FAIL;
			break;
		}
		status = INTEGRITY_PASS;
		break;
	case EVM_IMA_XATTR_DIGSIG:
		iint->flags |= IMA_DIGSIG;
		rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
249
					     (const char *)xattr_value, rc,
250 251
					     iint->ima_hash->digest,
					     iint->ima_hash->length);
252 253 254 255 256 257 258 259 260 261 262 263 264
		if (rc == -EOPNOTSUPP) {
			status = INTEGRITY_UNKNOWN;
		} else if (rc) {
			cause = "invalid-signature";
			status = INTEGRITY_FAIL;
		} else {
			status = INTEGRITY_PASS;
		}
		break;
	default:
		status = INTEGRITY_UNKNOWN;
		cause = "unknown-ima-data";
		break;
M
Mimi Zohar 已提交
265
	}
266

M
Mimi Zohar 已提交
267 268
out:
	if (status != INTEGRITY_PASS) {
269 270 271
		if ((ima_appraise & IMA_APPRAISE_FIX) &&
		    (!xattr_value ||
		     xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
272 273
			if (!ima_fix_xattr(dentry, iint))
				status = INTEGRITY_PASS;
M
Mimi Zohar 已提交
274 275 276
		}
		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
				    op, cause, rc, 0);
277
	} else {
278
		ima_cache_flags(iint, func);
M
Mimi Zohar 已提交
279
	}
280
	ima_set_cache_status(iint, func, status);
M
Mimi Zohar 已提交
281 282 283 284 285 286 287 288 289 290 291
	return status;
}

/*
 * ima_update_xattr - update 'security.ima' hash value
 */
void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
{
	struct dentry *dentry = file->f_dentry;
	int rc = 0;

292 293 294 295
	/* do not collect and update hash for digital signatures */
	if (iint->flags & IMA_DIGSIG)
		return;

296
	rc = ima_collect_measurement(iint, file, NULL, NULL);
M
Mimi Zohar 已提交
297 298
	if (rc < 0)
		return;
299

M
Mimi Zohar 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	ima_fix_xattr(dentry, iint);
}

/**
 * ima_inode_post_setattr - reflect file metadata changes
 * @dentry: pointer to the affected dentry
 *
 * Changes to a dentry's metadata might result in needing to appraise.
 *
 * This function is called from notify_change(), which expects the caller
 * to lock the inode's i_mutex.
 */
void ima_inode_post_setattr(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	struct integrity_iint_cache *iint;
	int must_appraise, rc;

	if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
	    || !inode->i_op->removexattr)
		return;

	must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
	iint = integrity_iint_find(inode);
	if (iint) {
325 326 327
		iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
				 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
				 IMA_ACTION_FLAGS);
M
Mimi Zohar 已提交
328 329 330 331 332 333 334
		if (must_appraise)
			iint->flags |= IMA_APPRAISE;
	}
	if (!must_appraise)
		rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
	return;
}
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

/*
 * ima_protect_xattr - protect 'security.ima'
 *
 * Ensure that not just anyone can modify or remove 'security.ima'.
 */
static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
			     const void *xattr_value, size_t xattr_value_len)
{
	if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		return 1;
	}
	return 0;
}

352
static void ima_reset_appraise_flags(struct inode *inode, int digsig)
353 354 355 356 357 358 359 360 361 362
{
	struct integrity_iint_cache *iint;

	if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
		return;

	iint = integrity_iint_find(inode);
	if (!iint)
		return;

363
	iint->flags &= ~IMA_DONE_MASK;
364 365
	if (digsig)
		iint->flags |= IMA_DIGSIG;
366 367 368 369 370 371
	return;
}

int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
		       const void *xattr_value, size_t xattr_value_len)
{
372
	const struct evm_ima_xattr_data *xvalue = xattr_value;
373 374 375 376 377
	int result;

	result = ima_protect_xattr(dentry, xattr_name, xattr_value,
				   xattr_value_len);
	if (result == 1) {
378 379
		ima_reset_appraise_flags(dentry->d_inode,
			 (xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0);
380 381 382 383 384 385 386 387 388 389 390
		result = 0;
	}
	return result;
}

int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
{
	int result;

	result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
	if (result == 1) {
391
		ima_reset_appraise_flags(dentry->d_inode, 0);
392 393 394 395
		result = 0;
	}
	return result;
}