blob: 681cb6e7225760a92b7987ebc9e30909718eb77b [file] [log] [blame]
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05001/*
2 * Copyright (C) 2011 IBM Corporation
3 *
4 * Author:
5 * Mimi Zohar <zohar@us.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 */
11#include <linux/module.h>
12#include <linux/file.h>
13#include <linux/fs.h>
14#include <linux/xattr.h>
15#include <linux/magic.h>
16#include <linux/ima.h>
17#include <linux/evm.h>
18
19#include "ima.h"
20
21static int __init default_appraise_setup(char *str)
22{
23 if (strncmp(str, "off", 3) == 0)
24 ima_appraise = 0;
25 else if (strncmp(str, "fix", 3) == 0)
26 ima_appraise = IMA_APPRAISE_FIX;
27 return 1;
28}
29
30__setup("ima_appraise=", default_appraise_setup);
31
32/*
33 * ima_must_appraise - set appraise flag
34 *
35 * Return 1 to appraise
36 */
37int ima_must_appraise(struct inode *inode, enum ima_hooks func, int mask)
38{
Mimi Zohar07f6a792011-03-09 22:25:48 -050039 if (!ima_appraise)
40 return 0;
41
42 return ima_match_policy(inode, func, mask, IMA_APPRAISE);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050043}
44
45static void ima_fix_xattr(struct dentry *dentry,
46 struct integrity_iint_cache *iint)
47{
48 iint->digest[0] = IMA_XATTR_DIGEST;
49 __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
50 iint->digest, IMA_DIGEST_SIZE + 1, 0);
51}
52
53/*
54 * ima_appraise_measurement - appraise file measurement
55 *
56 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
57 * Assuming success, compare the xattr hash with the collected measurement.
58 *
59 * Return 0 on success, error code otherwise
60 */
61int ima_appraise_measurement(struct integrity_iint_cache *iint,
62 struct file *file, const unsigned char *filename)
63{
64 struct dentry *dentry = file->f_dentry;
65 struct inode *inode = dentry->d_inode;
66 u8 xattr_value[IMA_DIGEST_SIZE];
67 enum integrity_status status = INTEGRITY_UNKNOWN;
68 const char *op = "appraise_data";
69 char *cause = "unknown";
70 int rc;
71
72 if (!ima_appraise)
73 return 0;
74 if (!inode->i_op->getxattr)
75 return INTEGRITY_UNKNOWN;
76
77 if (iint->flags & IMA_APPRAISED)
78 return iint->ima_status;
79
80 rc = inode->i_op->getxattr(dentry, XATTR_NAME_IMA, xattr_value,
81 IMA_DIGEST_SIZE);
82 if (rc <= 0) {
83 if (rc && rc != -ENODATA)
84 goto out;
85
86 cause = "missing-hash";
87 status =
88 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
89 goto out;
90 }
91
92 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
93 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
94 if ((status == INTEGRITY_NOLABEL)
95 || (status == INTEGRITY_NOXATTRS))
96 cause = "missing-HMAC";
97 else if (status == INTEGRITY_FAIL)
98 cause = "invalid-HMAC";
99 goto out;
100 }
101
102 rc = memcmp(xattr_value, iint->digest, IMA_DIGEST_SIZE);
103 if (rc) {
104 status = INTEGRITY_FAIL;
105 cause = "invalid-hash";
106 print_hex_dump_bytes("security.ima: ", DUMP_PREFIX_NONE,
107 xattr_value, IMA_DIGEST_SIZE);
108 print_hex_dump_bytes("collected: ", DUMP_PREFIX_NONE,
109 iint->digest, IMA_DIGEST_SIZE);
110 goto out;
111 }
112 status = INTEGRITY_PASS;
113 iint->flags |= IMA_APPRAISED;
114out:
115 if (status != INTEGRITY_PASS) {
116 if (ima_appraise & IMA_APPRAISE_FIX) {
117 ima_fix_xattr(dentry, iint);
118 status = INTEGRITY_PASS;
119 }
120 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
121 op, cause, rc, 0);
122 }
123 iint->ima_status = status;
124 return status;
125}
126
127/*
128 * ima_update_xattr - update 'security.ima' hash value
129 */
130void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
131{
132 struct dentry *dentry = file->f_dentry;
133 int rc = 0;
134
135 rc = ima_collect_measurement(iint, file);
136 if (rc < 0)
137 return;
138 ima_fix_xattr(dentry, iint);
139}
140
141/**
142 * ima_inode_post_setattr - reflect file metadata changes
143 * @dentry: pointer to the affected dentry
144 *
145 * Changes to a dentry's metadata might result in needing to appraise.
146 *
147 * This function is called from notify_change(), which expects the caller
148 * to lock the inode's i_mutex.
149 */
150void ima_inode_post_setattr(struct dentry *dentry)
151{
152 struct inode *inode = dentry->d_inode;
153 struct integrity_iint_cache *iint;
154 int must_appraise, rc;
155
156 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
157 || !inode->i_op->removexattr)
158 return;
159
160 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
161 iint = integrity_iint_find(inode);
162 if (iint) {
163 if (must_appraise)
164 iint->flags |= IMA_APPRAISE;
165 else
166 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
167 }
168 if (!must_appraise)
169 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
170 return;
171}