Release 4.11 security/integrity/ima/ima_appraise.c
/*
* 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>
#include "ima.h"
static int __init default_appraise_setup(char *str)
{
if (strncmp(str, "off", 3) == 0)
ima_appraise = 0;
else if (strncmp(str, "log", 3) == 0)
ima_appraise = IMA_APPRAISE_LOG;
else if (strncmp(str, "fix", 3) == 0)
ima_appraise = IMA_APPRAISE_FIX;
return 1;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 49 | 73.13% | 1 | 50.00% |
Dmitry Kasatkin | 18 | 26.87% | 1 | 50.00% |
Total | 67 | 100.00% | 2 | 100.00% |
__setup("ima_appraise=", default_appraise_setup);
/*
* ima_must_appraise - set appraise flag
*
* Return 1 to appraise
*/
int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
{
if (!ima_appraise)
return 0;
return ima_match_policy(inode, func, mask, IMA_APPRAISE, NULL);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 34 | 87.18% | 2 | 50.00% |
Dmitry Kasatkin | 3 | 7.69% | 1 | 25.00% |
Eric Richter | 2 | 5.13% | 1 | 25.00% |
Total | 39 | 100.00% | 4 | 100.00% |
static int ima_fix_xattr(struct dentry *dentry,
struct integrity_iint_cache *iint)
{
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;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Kasatkin | 94 | 72.31% | 4 | 66.67% |
Mimi Zohar | 36 | 27.69% | 2 | 33.33% |
Total | 130 | 100.00% | 6 | 100.00% |
/* Return specific func appraised cached result */
enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
enum ima_hooks func)
{
switch (func) {
case MMAP_CHECK:
return iint->ima_mmap_status;
case BPRM_CHECK:
return iint->ima_bprm_status;
case FILE_CHECK:
case POST_SETATTR:
return iint->ima_file_status;
case MODULE_CHECK ... MAX_CHECK - 1:
default:
return iint->ima_read_status;
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 61 | 100.00% | 4 | 100.00% |
Total | 61 | 100.00% | 4 | 100.00% |
static void ima_set_cache_status(struct integrity_iint_cache *iint,
enum ima_hooks func,
enum integrity_status status)
{
switch (func) {
case MMAP_CHECK:
iint->ima_mmap_status = status;
break;
case BPRM_CHECK:
iint->ima_bprm_status = status;
break;
case FILE_CHECK:
case POST_SETATTR:
iint->ima_file_status = status;
break;
case MODULE_CHECK ... MAX_CHECK - 1:
default:
iint->ima_read_status = status;
break;
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 73 | 100.00% | 4 | 100.00% |
Total | 73 | 100.00% | 4 | 100.00% |
static void ima_cache_flags(struct integrity_iint_cache *iint,
enum ima_hooks func)
{
switch (func) {
case MMAP_CHECK:
iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
break;
case BPRM_CHECK:
iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
break;
case FILE_CHECK:
case POST_SETATTR:
iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
break;
case MODULE_CHECK ... MAX_CHECK - 1:
default:
iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
break;
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 85 | 100.00% | 4 | 100.00% |
Total | 85 | 100.00% | 4 | 100.00% |
enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
int xattr_len)
{
struct signature_v2_hdr *sig;
enum hash_algo ret;
if (!xattr_value || xattr_len < 2)
/* return default hash algo */
return ima_hash_algo;
switch (xattr_value->type) {
case EVM_IMA_XATTR_DIGSIG:
sig = (typeof(sig))xattr_value;
if (sig->version != 2 || xattr_len <= sizeof(*sig))
return ima_hash_algo;
return sig->hash_algo;
break;
case IMA_XATTR_DIGEST_NG:
ret = xattr_value->digest[0];
if (ret < HASH_ALGO__LAST)
return ret;
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))
return HASH_ALGO_MD5;
else
return HASH_ALGO_SHA1;
} else if (xattr_len == 17)
return HASH_ALGO_MD5;
break;
}
/* return default hash algo */
return ima_hash_algo;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Kasatkin | 148 | 90.80% | 3 | 75.00% |
Seth Forshee | 15 | 9.20% | 1 | 25.00% |
Total | 163 | 100.00% | 4 | 100.00% |
int ima_read_xattr(struct dentry *dentry,
struct evm_ima_xattr_data **xattr_value)
{
ssize_t ret;
ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
0, GFP_NOFS);
if (ret == -EOPNOTSUPP)
ret = 0;
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dmitry Kasatkin | 35 | 66.04% | 1 | 50.00% |
Andreas Gruenbacher | 18 | 33.96% | 1 | 50.00% |
Total | 53 | 100.00% | 2 | 100.00% |
/*
* 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
*/
int ima_appraise_measurement(enum ima_hooks func,
struct integrity_iint_cache *iint,
struct file *file, const unsigned char *filename,
struct evm_ima_xattr_data *xattr_value,
int xattr_len, int opened)
{
static const char op[] = "appraise_data";
char *cause = "unknown";
struct dentry *dentry = file_dentry(file);
struct inode *inode = d_backing_inode(dentry);
enum integrity_status status = INTEGRITY_UNKNOWN;
int rc = xattr_len, hash_start = 0;
if (!(inode->i_opflags & IOP_XATTR))
return INTEGRITY_UNKNOWN;
if (rc <= 0) {
if (rc && rc != -ENODATA)
goto out;
cause = "missing-hash";
status = INTEGRITY_NOLABEL;
if (opened & FILE_CREATED) {
iint->flags |= IMA_NEW_FILE;
status = INTEGRITY_PASS;
}
goto out;
}
status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
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;
}
switch (xattr_value->type) {
case IMA_XATTR_DIGEST_NG:
/* first byte contains algorithm id */
hash_start = 1;
case IMA_XATTR_DIGEST:
if (iint->flags & IMA_DIGSIG_REQUIRED) {
cause = "IMA-signature-required";
status = INTEGRITY_FAIL;
break;
}
if (xattr_len - sizeof(xattr_value->type) - hash_start >=
iint->ima_hash->length)
/* xattr length may be longer. md5 hash in previous
version occupied 20 bytes in xattr, instead of 16
*/
rc = memcmp(&xattr_value->digest[hash_start],
iint->ima_hash->digest,
iint->ima_hash->length);
else
rc = -EINVAL;
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,
(const char *)xattr_value, rc,
iint->ima_hash->digest,
iint->ima_hash->length);
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;
}
out:
if (status != INTEGRITY_PASS) {
if ((ima_appraise & IMA_APPRAISE_FIX) &&
(!xattr_value ||
xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
if (!ima_fix_xattr(dentry, iint))
status = INTEGRITY_PASS;
} else if ((inode->i_size == 0) &&
(iint->flags & IMA_NEW_FILE) &&
(xattr_value &&
xattr_value->type == EVM_IMA_XATTR_DIGSIG)) {
status = INTEGRITY_PASS;
}
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
op, cause, rc, 0);
} else {
ima_cache_flags(iint, func);
}
ima_set_cache_status(iint, func, status);
return status;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 294 | 56.32% | 6 | 30.00% |
Dmitry Kasatkin | 216 | 41.38% | 10 | 50.00% |
Andreas Gruenbacher | 5 | 0.96% | 1 | 5.00% |
Miklos Szeredi | 3 | 0.57% | 1 | 5.00% |
David Howells | 3 | 0.57% | 1 | 5.00% |
Richard Guy Briggs | 1 | 0.19% | 1 | 5.00% |
Total | 522 | 100.00% | 20 | 100.00% |
/*
* ima_update_xattr - update 'security.ima' hash value
*/
void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
{
struct dentry *dentry = file_dentry(file);
int rc = 0;
/* do not collect and update hash for digital signatures */
if (iint->flags & IMA_DIGSIG)
return;
rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
if (rc < 0)
return;
ima_fix_xattr(dentry, iint);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 54 | 78.26% | 2 | 33.33% |
Dmitry Kasatkin | 12 | 17.39% | 3 | 50.00% |
Miklos Szeredi | 3 | 4.35% | 1 | 16.67% |
Total | 69 | 100.00% | 6 | 100.00% |
/**
* 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 = d_backing_inode(dentry);
struct integrity_iint_cache *iint;
int must_appraise;
if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
|| !(inode->i_opflags & IOP_XATTR))
return;
must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
iint = integrity_iint_find(inode);
if (iint) {
iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
IMA_ACTION_RULE_FLAGS);
if (must_appraise)
iint->flags |= IMA_APPRAISE;
}
if (!must_appraise)
__vfs_removexattr(dentry, XATTR_NAME_IMA);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 104 | 88.14% | 3 | 50.00% |
Andreas Gruenbacher | 6 | 5.08% | 1 | 16.67% |
Roberto Sassu | 5 | 4.24% | 1 | 16.67% |
David Howells | 3 | 2.54% | 1 | 16.67% |
Total | 118 | 100.00% | 6 | 100.00% |
/*
* 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;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 55 | 100.00% | 1 | 100.00% |
Total | 55 | 100.00% | 1 | 100.00% |
static void ima_reset_appraise_flags(struct inode *inode, int digsig)
{
struct integrity_iint_cache *iint;
if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
return;
iint = integrity_iint_find(inode);
if (!iint)
return;
iint->flags &= ~IMA_DONE_MASK;
iint->measured_pcrs = 0;
if (digsig)
iint->flags |= IMA_DIGSIG;
return;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 62 | 83.78% | 2 | 40.00% |
Eric Richter | 6 | 8.11% | 1 | 20.00% |
Roberto Sassu | 5 | 6.76% | 1 | 20.00% |
Dmitry Kasatkin | 1 | 1.35% | 1 | 20.00% |
Total | 74 | 100.00% | 5 | 100.00% |
int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
const void *xattr_value, size_t xattr_value_len)
{
const struct evm_ima_xattr_data *xvalue = xattr_value;
int result;
result = ima_protect_xattr(dentry, xattr_name, xattr_value,
xattr_value_len);
if (result == 1) {
if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
return -EINVAL;
ima_reset_appraise_flags(d_backing_inode(dentry),
(xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0);
result = 0;
}
return result;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 75 | 75.76% | 3 | 60.00% |
Dmitry Kasatkin | 24 | 24.24% | 2 | 40.00% |
Total | 99 | 100.00% | 5 | 100.00% |
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) {
ima_reset_appraise_flags(d_backing_inode(dentry), 0);
result = 0;
}
return result;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 53 | 94.64% | 2 | 66.67% |
David Howells | 3 | 5.36% | 1 | 33.33% |
Total | 56 | 100.00% | 3 | 100.00% |
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Mimi Zohar | 1073 | 63.04% | 14 | 36.84% |
Dmitry Kasatkin | 551 | 32.37% | 16 | 42.11% |
Andreas Gruenbacher | 29 | 1.70% | 1 | 2.63% |
Seth Forshee | 15 | 0.88% | 1 | 2.63% |
Roberto Sassu | 10 | 0.59% | 1 | 2.63% |
David Howells | 9 | 0.53% | 1 | 2.63% |
Eric Richter | 8 | 0.47% | 2 | 5.26% |
Miklos Szeredi | 6 | 0.35% | 1 | 2.63% |
Richard Guy Briggs | 1 | 0.06% | 1 | 2.63% |
Total | 1702 | 100.00% | 38 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.