Skip to content

Commit f3cc6b2

Browse files
author
Mimi Zohar
committed
ima: always measure and audit files in policy
All files matching a "measure" rule must be included in the IMA measurement list, even when the file hash cannot be calculated. Similarly, all files matching an "audit" rule must be audited, even when the file hash can not be calculated. The file data hash field contained in the IMA measurement list template data will contain 0's instead of the actual file hash digest. Note: In general, adding, deleting or in anyway changing which files are included in the IMA measurement list is not a good idea, as it might result in not being able to unseal trusted keys sealed to a specific TPM PCR value. This patch not only adds file measurements that were not previously measured, but specifies that the file hash value for these files will be 0's. As the IMA measurement list ordering is not consistent from one boot to the next, it is unlikely that anyone is sealing keys based on the IMA measurement list. Remote attestation servers should be able to process these new measurement records, but might complain about these unknown records. Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com> Reviewed-by: Dmitry Kasatkin <dmitry.kasatkin@huawei.com>
1 parent 2068626 commit f3cc6b2

3 files changed

Lines changed: 56 additions & 30 deletions

File tree

‎security/integrity/ima/ima_api.c‎

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -199,42 +199,59 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
199199
struct inode *inode = file_inode(file);
200200
const char *filename = file->f_path.dentry->d_name.name;
201201
int result = 0;
202+
int length;
203+
void *tmpbuf;
204+
u64 i_version;
202205
struct {
203206
struct ima_digest_data hdr;
204207
char digest[IMA_MAX_DIGEST_SIZE];
205208
} hash;
206209

207-
if (!(iint->flags & IMA_COLLECTED)) {
208-
u64 i_version = file_inode(file)->i_version;
210+
if (iint->flags & IMA_COLLECTED)
211+
goto out;
209212

210-
if (file->f_flags & O_DIRECT) {
211-
audit_cause = "failed(directio)";
212-
result = -EACCES;
213-
goto out;
214-
}
213+
/*
214+
* Dectecting file change is based on i_version. On filesystems
215+
* which do not support i_version, support is limited to an initial
216+
* measurement/appraisal/audit.
217+
*/
218+
i_version = file_inode(file)->i_version;
219+
hash.hdr.algo = algo;
215220

216-
hash.hdr.algo = algo;
217-
218-
result = (!buf) ? ima_calc_file_hash(file, &hash.hdr) :
219-
ima_calc_buffer_hash(buf, size, &hash.hdr);
220-
if (!result) {
221-
int length = sizeof(hash.hdr) + hash.hdr.length;
222-
void *tmpbuf = krealloc(iint->ima_hash, length,
223-
GFP_NOFS);
224-
if (tmpbuf) {
225-
iint->ima_hash = tmpbuf;
226-
memcpy(iint->ima_hash, &hash, length);
227-
iint->version = i_version;
228-
iint->flags |= IMA_COLLECTED;
229-
} else
230-
result = -ENOMEM;
231-
}
221+
/* Initialize hash digest to 0's in case of failure */
222+
memset(&hash.digest, 0, sizeof(hash.digest));
223+
224+
if (buf)
225+
result = ima_calc_buffer_hash(buf, size, &hash.hdr);
226+
else
227+
result = ima_calc_file_hash(file, &hash.hdr);
228+
229+
if (result && result != -EBADF && result != -EINVAL)
230+
goto out;
231+
232+
length = sizeof(hash.hdr) + hash.hdr.length;
233+
tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS);
234+
if (!tmpbuf) {
235+
result = -ENOMEM;
236+
goto out;
232237
}
238+
239+
iint->ima_hash = tmpbuf;
240+
memcpy(iint->ima_hash, &hash, length);
241+
iint->version = i_version;
242+
243+
/* Possibly temporary failure due to type of read (eg. O_DIRECT) */
244+
if (!result)
245+
iint->flags |= IMA_COLLECTED;
233246
out:
234-
if (result)
247+
if (result) {
248+
if (file->f_flags & O_DIRECT)
249+
audit_cause = "failed(directio)";
250+
235251
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
236252
filename, "collect_data", audit_cause,
237253
result, 0);
254+
}
238255
return result;
239256
}
240257

@@ -278,7 +295,7 @@ void ima_store_measurement(struct integrity_iint_cache *iint,
278295
}
279296

280297
result = ima_store_template(entry, violation, inode, filename, pcr);
281-
if (!result || result == -EEXIST) {
298+
if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) {
282299
iint->flags |= IMA_MEASURED;
283300
iint->measured_pcrs |= (0x1 << pcr);
284301
}

‎security/integrity/ima/ima_crypto.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
441441
loff_t i_size;
442442
int rc;
443443

444+
/*
445+
* For consistency, fail file's opened with the O_DIRECT flag on
446+
* filesystems mounted with/without DAX option.
447+
*/
448+
if (file->f_flags & O_DIRECT) {
449+
hash->length = hash_digest_size[ima_hash_algo];
450+
hash->algo = ima_hash_algo;
451+
return -EINVAL;
452+
}
453+
444454
i_size = i_size_read(file_inode(file));
445455

446456
if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {

‎security/integrity/ima/ima_main.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,23 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
235235
hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
236236

237237
rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
238-
if (rc != 0) {
239-
if (file->f_flags & O_DIRECT)
240-
rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
238+
if (rc != 0 && rc != -EBADF && rc != -EINVAL)
241239
goto out_digsig;
242-
}
243240

244241
if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
245242
pathname = ima_d_path(&file->f_path, &pathbuf, filename);
246243

247244
if (action & IMA_MEASURE)
248245
ima_store_measurement(iint, file, pathname,
249246
xattr_value, xattr_len, pcr);
250-
if (action & IMA_APPRAISE_SUBMASK)
247+
if (rc == 0 && (action & IMA_APPRAISE_SUBMASK))
251248
rc = ima_appraise_measurement(func, iint, file, pathname,
252249
xattr_value, xattr_len, opened);
253250
if (action & IMA_AUDIT)
254251
ima_audit_measurement(iint, pathname);
255252

253+
if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
254+
rc = 0;
256255
out_digsig:
257256
if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG) &&
258257
!(iint->flags & IMA_NEW_FILE))

0 commit comments

Comments
 (0)