ima: Fix use-after-free on a dentry's dname.name
[linux-2.6-block.git] / security / integrity / ima / ima_api.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
3323eec9
MZ
2/*
3 * Copyright (C) 2008 IBM Corporation
4 *
5 * Author: Mimi Zohar <zohar@us.ibm.com>
6 *
3323eec9 7 * File: ima_api.c
2fe5d6de
MZ
8 * Implements must_appraise_or_measure, collect_measurement,
9 * appraise_measurement, store_measurement and store_template.
3323eec9 10 */
5a0e3ad6 11#include <linux/slab.h>
2fe5d6de
MZ
12#include <linux/file.h>
13#include <linux/fs.h>
14#include <linux/xattr.h>
15#include <linux/evm.h>
54f03916 16#include <linux/fsverity.h>
1525b06d 17
3323eec9 18#include "ima.h"
2fe5d6de 19
a7ed7c60
RS
20/*
21 * ima_free_template_entry - free an existing template entry
22 */
23void ima_free_template_entry(struct ima_template_entry *entry)
24{
25 int i;
26
27 for (i = 0; i < entry->template_desc->num_fields; i++)
28 kfree(entry->template_data[i].data);
29
aa724fe1 30 kfree(entry->digests);
a7ed7c60
RS
31 kfree(entry);
32}
33
7bc5f447
RS
34/*
35 * ima_alloc_init_template - create and initialize a new template entry
36 */
23b57419 37int ima_alloc_init_template(struct ima_event_data *event_data,
19453ce0
MG
38 struct ima_template_entry **entry,
39 struct ima_template_desc *desc)
7bc5f447 40{
19453ce0 41 struct ima_template_desc *template_desc;
aa724fe1 42 struct tpm_digest *digests;
a71dc65d 43 int i, result = 0;
7bc5f447 44
19453ce0
MG
45 if (desc)
46 template_desc = desc;
47 else
48 template_desc = ima_template_desc_current();
49
2a7f0e53
GS
50 *entry = kzalloc(struct_size(*entry, template_data,
51 template_desc->num_fields), GFP_NOFS);
a71dc65d 52 if (!*entry)
7bc5f447
RS
53 return -ENOMEM;
54
aa724fe1
RS
55 digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
56 sizeof(*digests), GFP_NOFS);
57 if (!digests) {
42413b49
RS
58 kfree(*entry);
59 *entry = NULL;
60 return -ENOMEM;
aa724fe1
RS
61 }
62
63 (*entry)->digests = digests;
a7ed7c60 64 (*entry)->template_desc = template_desc;
a71dc65d 65 for (i = 0; i < template_desc->num_fields; i++) {
b2724d58
EB
66 const struct ima_template_field *field =
67 template_desc->fields[i];
a71dc65d 68 u32 len;
7bc5f447 69
23b57419 70 result = field->field_init(event_data,
a71dc65d
RS
71 &((*entry)->template_data[i]));
72 if (result != 0)
73 goto out;
7bc5f447 74
a71dc65d
RS
75 len = (*entry)->template_data[i].len;
76 (*entry)->template_data_len += sizeof(len);
77 (*entry)->template_data_len += len;
78 }
7bc5f447 79 return 0;
a71dc65d 80out:
a7ed7c60 81 ima_free_template_entry(*entry);
a71dc65d 82 *entry = NULL;
7bc5f447
RS
83 return result;
84}
85
3323eec9
MZ
86/*
87 * ima_store_template - store ima template measurements
88 *
89 * Calculate the hash of a template entry, add the template entry
90 * to an ordered list of measurement entries maintained inside the kernel,
91 * and also update the aggregate integrity value (maintained inside the
92 * configured TPM PCR) over the hashes of the current list of measurement
93 * entries.
94 *
95 * Applications retrieve the current kernel-held measurement list through
96 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
97 * TPM PCR (called quote) can be retrieved using a TPM user space library
98 * and is used to validate the measurement list.
99 *
100 * Returns 0 on success, error code otherwise
101 */
102int ima_store_template(struct ima_template_entry *entry,
9803d413 103 int violation, struct inode *inode,
14b1da85 104 const unsigned char *filename, int pcr)
3323eec9 105{
52a13284
MZ
106 static const char op[] = "add_template_measure";
107 static const char audit_cause[] = "hashing_error";
a71dc65d 108 char *template_name = entry->template_desc->name;
3323eec9
MZ
109 int result;
110
3323eec9 111 if (!violation) {
a71dc65d 112 result = ima_calc_field_array_hash(&entry->template_data[0],
7ca79645 113 entry);
3323eec9
MZ
114 if (result < 0) {
115 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
a71dc65d 116 template_name, op,
3323eec9
MZ
117 audit_cause, result, 0);
118 return result;
119 }
120 }
14b1da85 121 entry->pcr = pcr;
9803d413 122 result = ima_add_template_entry(entry, violation, op, inode, filename);
3323eec9
MZ
123 return result;
124}
125
126/*
127 * ima_add_violation - add violation to measurement list.
128 *
129 * Violations are flagged in the measurement list with zero hash values.
130 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
131 * value is invalidated.
132 */
7d802a22 133void ima_add_violation(struct file *file, const unsigned char *filename,
4de2f084
RS
134 struct ima_iint_cache *iint, const char *op,
135 const char *cause)
3323eec9
MZ
136{
137 struct ima_template_entry *entry;
31d4b761 138 struct inode *inode = file_inode(file);
e038f5f6
TJB
139 struct ima_event_data event_data = { .iint = iint,
140 .file = file,
141 .filename = filename,
142 .violation = cause };
3323eec9
MZ
143 int violation = 1;
144 int result;
145
146 /* can overflow, only indicator */
147 atomic_long_inc(&ima_htable.violations);
148
19453ce0 149 result = ima_alloc_init_template(&event_data, &entry, NULL);
7bc5f447 150 if (result < 0) {
3323eec9
MZ
151 result = -ENOMEM;
152 goto err_out;
153 }
14b1da85
ER
154 result = ima_store_template(entry, violation, inode,
155 filename, CONFIG_IMA_MEASURE_PCR_IDX);
3323eec9 156 if (result < 0)
a7ed7c60 157 ima_free_template_entry(entry);
3323eec9
MZ
158err_out:
159 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
160 op, cause, result, 0);
161}
162
163/**
d9d300cd 164 * ima_get_action - appraise & measure decision based on policy.
39f60c1c 165 * @idmap: idmap of the mount the inode was found from
4834177e 166 * @inode: pointer to the inode associated with the object being validated
d906c10d
MG
167 * @cred: pointer to credentials structure to validate
168 * @secid: secid of the task being validated
20f482ab
LZ
169 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC,
170 * MAY_APPEND)
4ad87a3d 171 * @func: caller identifier
725de7fa 172 * @pcr: pointer filled in if matched measure policy sets pcr=
19453ce0 173 * @template_desc: pointer filled in if matched measure policy sets template=
2b4a2474 174 * @func_data: func specific data, may be NULL
1624dc00 175 * @allowed_algos: allowlist of hash algorithms for the IMA xattr
3323eec9
MZ
176 *
177 * The policy is defined in terms of keypairs:
2bb930ab 178 * subj=, obj=, type=, func=, mask=, fsmagic=
3323eec9 179 * subj,obj, and type: are LSM specific.
d906c10d 180 * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK
4958db32
RS
181 * | KEXEC_CMDLINE | KEY_CHECK | CRITICAL_DATA | SETXATTR_CHECK
182 * | MMAP_CHECK_REQPROT
2bb930ab 183 * mask: contains the permission mask
3323eec9
MZ
184 * fsmagic: hex value
185 *
2fe5d6de
MZ
186 * Returns IMA_MEASURE, IMA_APPRAISE mask.
187 *
188 */
39f60c1c 189int ima_get_action(struct mnt_idmap *idmap, struct inode *inode,
a2d2329e
CB
190 const struct cred *cred, u32 secid, int mask,
191 enum ima_hooks func, int *pcr,
e9085e0a 192 struct ima_template_desc **template_desc,
1624dc00 193 const char *func_data, unsigned int *allowed_algos)
3323eec9 194{
da1b0029 195 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
2fe5d6de 196
fd5f4e90 197 flags &= ima_policy_flag;
2fe5d6de 198
39f60c1c 199 return ima_match_policy(idmap, inode, cred, secid, func, mask,
1624dc00
TS
200 flags, pcr, template_desc, func_data,
201 allowed_algos);
2fe5d6de 202}
3323eec9 203
4de2f084
RS
204static bool ima_get_verity_digest(struct ima_iint_cache *iint,
205 struct inode *inode,
74836ecb 206 struct ima_max_digest_data *hash)
54f03916 207{
74836ecb
EB
208 enum hash_algo alg;
209 int digest_len;
54f03916
MZ
210
211 /*
212 * On failure, 'measure' policy rules will result in a file data
213 * hash containing 0's.
214 */
4de2f084 215 digest_len = fsverity_get_digest(inode, hash->digest, NULL, &alg);
74836ecb
EB
216 if (digest_len == 0)
217 return false;
54f03916
MZ
218
219 /*
220 * Unlike in the case of actually calculating the file hash, in
221 * the fsverity case regardless of the hash algorithm, return
222 * the verity digest to be included in the measurement list. A
223 * mismatch between the verity algorithm and the xattr signature
224 * algorithm, if one exists, will be detected later.
225 */
74836ecb
EB
226 hash->hdr.algo = alg;
227 hash->hdr.length = digest_len;
228 return true;
54f03916
MZ
229}
230
3323eec9
MZ
231/*
232 * ima_collect_measurement - collect file measurement
233 *
234 * Calculate the file hash, if it doesn't already exist,
235 * storing the measurement and i_version in the iint.
236 *
237 * Must be called with iint->mutex held.
238 *
239 * Return 0 on success, error code otherwise
240 */
4de2f084
RS
241int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
242 void *buf, loff_t size, enum hash_algo algo,
243 struct modsig *modsig)
3323eec9 244{
f9b2a735 245 const char *audit_cause = "failed";
496ad9aa 246 struct inode *inode = file_inode(file);
b836c4d2 247 struct inode *real_inode = d_real_inode(file_dentry(file));
8c54135e 248 struct ima_max_digest_data hash;
be84f32b 249 struct name_snapshot filename;
db1d1e8b 250 struct kstat stat;
2fe5d6de 251 int result = 0;
f3cc6b25
MZ
252 int length;
253 void *tmpbuf;
db1d1e8b 254 u64 i_version = 0;
3323eec9 255
e5092255
TJB
256 /*
257 * Always collect the modsig, because IMA might have already collected
258 * the file digest without collecting the modsig in a previous
259 * measurement rule.
260 */
261 if (modsig)
262 ima_collect_modsig(modsig, buf, size);
263
f3cc6b25
MZ
264 if (iint->flags & IMA_COLLECTED)
265 goto out;
3323eec9 266
f3cc6b25 267 /*
65603435 268 * Detecting file change is based on i_version. On filesystems
8c54135e
MZ
269 * which do not support i_version, support was originally limited
270 * to an initial measurement/appraisal/audit, but was modified to
271 * assume the file changed.
f3cc6b25 272 */
db1d1e8b
JL
273 result = vfs_getattr_nosec(&file->f_path, &stat, STATX_CHANGE_COOKIE,
274 AT_STATX_SYNC_AS_STAT);
275 if (!result && (stat.result_mask & STATX_CHANGE_COOKIE))
276 i_version = stat.change_cookie;
f3cc6b25 277 hash.hdr.algo = algo;
54f03916 278 hash.hdr.length = hash_digest_size[algo];
f9b2a735 279
f3cc6b25
MZ
280 /* Initialize hash digest to 0's in case of failure */
281 memset(&hash.digest, 0, sizeof(hash.digest));
282
54f03916 283 if (iint->flags & IMA_VERITY_REQUIRED) {
4de2f084 284 if (!ima_get_verity_digest(iint, inode, &hash)) {
54f03916 285 audit_cause = "no-verity-digest";
74836ecb 286 result = -ENODATA;
54f03916
MZ
287 }
288 } else if (buf) {
f3cc6b25 289 result = ima_calc_buffer_hash(buf, size, &hash.hdr);
54f03916 290 } else {
f3cc6b25 291 result = ima_calc_file_hash(file, &hash.hdr);
54f03916 292 }
f3cc6b25 293
6dc387d5 294 if (result && result != -EBADF && result != -EINVAL)
f3cc6b25
MZ
295 goto out;
296
297 length = sizeof(hash.hdr) + hash.hdr.length;
298 tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS);
299 if (!tmpbuf) {
300 result = -ENOMEM;
301 goto out;
3323eec9 302 }
f3cc6b25
MZ
303
304 iint->ima_hash = tmpbuf;
305 memcpy(iint->ima_hash, &hash, length);
306 iint->version = i_version;
b836c4d2
MZ
307 if (real_inode != inode) {
308 iint->real_ino = real_inode->i_ino;
309 iint->real_dev = real_inode->i_sb->s_dev;
310 }
f3cc6b25
MZ
311
312 /* Possibly temporary failure due to type of read (eg. O_DIRECT) */
313 if (!result)
314 iint->flags |= IMA_COLLECTED;
f9b2a735 315out:
f3cc6b25
MZ
316 if (result) {
317 if (file->f_flags & O_DIRECT)
318 audit_cause = "failed(directio)";
319
be84f32b
SB
320 take_dentry_name_snapshot(&filename, file->f_path.dentry);
321
2fe5d6de 322 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
be84f32b
SB
323 filename.name.name, "collect_data",
324 audit_cause, result, 0);
325
326 release_dentry_name_snapshot(&filename);
f3cc6b25 327 }
3323eec9
MZ
328 return result;
329}
330
331/*
332 * ima_store_measurement - store file measurement
333 *
334 * Create an "ima" template and then store the template by calling
335 * ima_store_template.
336 *
337 * We only get here if the inode has not already been measured,
338 * but the measurement could already exist:
2bb930ab 339 * - multiple copies of the same file on either the same or
3323eec9
MZ
340 * different filesystems.
341 * - the inode was previously flushed as well as the iint info,
342 * containing the hashing info.
343 *
344 * Must be called with iint->mutex held.
345 */
4de2f084
RS
346void ima_store_measurement(struct ima_iint_cache *iint, struct file *file,
347 const unsigned char *filename,
bcbc9b0c 348 struct evm_ima_xattr_data *xattr_value,
3878d505 349 int xattr_len, const struct modsig *modsig, int pcr,
19453ce0 350 struct ima_template_desc *template_desc)
3323eec9 351{
52a13284
MZ
352 static const char op[] = "add_template_measure";
353 static const char audit_cause[] = "ENOMEM";
3323eec9 354 int result = -ENOMEM;
496ad9aa 355 struct inode *inode = file_inode(file);
3323eec9 356 struct ima_template_entry *entry;
e038f5f6
TJB
357 struct ima_event_data event_data = { .iint = iint,
358 .file = file,
359 .filename = filename,
360 .xattr_value = xattr_value,
3878d505
TJB
361 .xattr_len = xattr_len,
362 .modsig = modsig };
3323eec9
MZ
363 int violation = 0;
364
e5092255
TJB
365 /*
366 * We still need to store the measurement in the case of MODSIG because
367 * we only have its contents to put in the list at the time of
368 * appraisal, but a file measurement from earlier might already exist in
369 * the measurement list.
370 */
371 if (iint->measured_pcrs & (0x1 << pcr) && !modsig)
2fe5d6de
MZ
372 return;
373
19453ce0 374 result = ima_alloc_init_template(&event_data, &entry, template_desc);
7bc5f447 375 if (result < 0) {
3323eec9
MZ
376 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
377 op, audit_cause, result, 0);
378 return;
379 }
3323eec9 380
14b1da85 381 result = ima_store_template(entry, violation, inode, filename, pcr);
f3cc6b25 382 if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) {
3323eec9 383 iint->flags |= IMA_MEASURED;
a422638d
ER
384 iint->measured_pcrs |= (0x1 << pcr);
385 }
45fae749 386 if (result < 0)
a7ed7c60 387 ima_free_template_entry(entry);
3323eec9 388}
e7c568e0 389
4de2f084 390void ima_audit_measurement(struct ima_iint_cache *iint,
e7c568e0
PM
391 const unsigned char *filename)
392{
393 struct audit_buffer *ab;
e456ef88 394 char *hash;
5278aa52 395 const char *algo_name = hash_algo_name[iint->ima_hash->algo];
e7c568e0
PM
396 int i;
397
398 if (iint->flags & IMA_AUDITED)
399 return;
400
e456ef88
TA
401 hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL);
402 if (!hash)
403 return;
404
a35c3fb6
DK
405 for (i = 0; i < iint->ima_hash->length; i++)
406 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
e7c568e0
PM
407 hash[i * 2] = '\0';
408
cdfb6b34 409 ab = audit_log_start(audit_context(), GFP_KERNEL,
e7c568e0
PM
410 AUDIT_INTEGRITY_RULE);
411 if (!ab)
e456ef88 412 goto out;
e7c568e0
PM
413
414 audit_log_format(ab, "file=");
415 audit_log_untrustedstring(ab, filename);
e456ef88 416 audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash);
e7c568e0 417
2a1fe215 418 audit_log_task_info(ab);
e7c568e0
PM
419 audit_log_end(ab);
420
421 iint->flags |= IMA_AUDITED;
e456ef88
TA
422out:
423 kfree(hash);
424 return;
e7c568e0 425}
ea1046d4 426
bc15ed66
MZ
427/*
428 * ima_d_path - return a pointer to the full pathname
429 *
430 * Attempt to return a pointer to the full pathname for use in the
431 * IMA measurement list, IMA audit records, and auditing logs.
432 *
433 * On failure, return a pointer to a copy of the filename, not dname.
434 * Returning a pointer to dname, could result in using the pointer
435 * after the memory has been freed.
436 */
437const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
ea1046d4 438{
be84f32b 439 struct name_snapshot filename;
ea1046d4
DK
440 char *pathname = NULL;
441
456f5fd3 442 *pathbuf = __getname();
ea1046d4 443 if (*pathbuf) {
17f4bad3 444 pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
ea1046d4 445 if (IS_ERR(pathname)) {
456f5fd3 446 __putname(*pathbuf);
ea1046d4
DK
447 *pathbuf = NULL;
448 pathname = NULL;
449 }
450 }
bc15ed66
MZ
451
452 if (!pathname) {
be84f32b
SB
453 take_dentry_name_snapshot(&filename, path->dentry);
454 strscpy(namebuf, filename.name.name, NAME_MAX);
455 release_dentry_name_snapshot(&filename);
456
bc15ed66
MZ
457 pathname = namebuf;
458 }
459
460 return pathname;
ea1046d4 461}