2019-06-01 16:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-02-04 22:06:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 IBM Corporation
|
|
|
|
*
|
|
|
|
* Author: Mimi Zohar <zohar@us.ibm.com>
|
|
|
|
*
|
|
|
|
* File: ima_api.c
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
* Implements must_appraise_or_measure, collect_measurement,
|
|
|
|
* appraise_measurement, store_measurement and store_template.
|
2009-02-04 22:06:58 +08:00
|
|
|
*/
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/xattr.h>
|
|
|
|
#include <linux/evm.h>
|
2017-12-11 19:35:21 +08:00
|
|
|
#include <linux/iversion.h>
|
2014-10-30 18:39:39 +08:00
|
|
|
|
2009-02-04 22:06:58 +08:00
|
|
|
#include "ima.h"
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
|
2013-12-03 02:40:34 +08:00
|
|
|
/*
|
|
|
|
* ima_free_template_entry - free an existing template entry
|
|
|
|
*/
|
|
|
|
void ima_free_template_entry(struct ima_template_entry *entry)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < entry->template_desc->num_fields; i++)
|
|
|
|
kfree(entry->template_data[i].data);
|
|
|
|
|
2020-03-25 18:47:09 +08:00
|
|
|
kfree(entry->digests);
|
2013-12-03 02:40:34 +08:00
|
|
|
kfree(entry);
|
|
|
|
}
|
|
|
|
|
2013-06-07 18:16:28 +08:00
|
|
|
/*
|
|
|
|
* ima_alloc_init_template - create and initialize a new template entry
|
|
|
|
*/
|
2015-04-11 23:09:50 +08:00
|
|
|
int ima_alloc_init_template(struct ima_event_data *event_data,
|
2019-06-20 06:46:11 +08:00
|
|
|
struct ima_template_entry **entry,
|
|
|
|
struct ima_template_desc *desc)
|
2013-06-07 18:16:28 +08:00
|
|
|
{
|
2019-06-20 06:46:11 +08:00
|
|
|
struct ima_template_desc *template_desc;
|
2020-03-25 18:47:09 +08:00
|
|
|
struct tpm_digest *digests;
|
2013-06-07 18:16:33 +08:00
|
|
|
int i, result = 0;
|
2013-06-07 18:16:28 +08:00
|
|
|
|
2019-06-20 06:46:11 +08:00
|
|
|
if (desc)
|
|
|
|
template_desc = desc;
|
|
|
|
else
|
|
|
|
template_desc = ima_template_desc_current();
|
|
|
|
|
2019-08-30 01:29:16 +08:00
|
|
|
*entry = kzalloc(struct_size(*entry, template_data,
|
|
|
|
template_desc->num_fields), GFP_NOFS);
|
2013-06-07 18:16:33 +08:00
|
|
|
if (!*entry)
|
2013-06-07 18:16:28 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-03-25 18:47:09 +08:00
|
|
|
digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
|
|
|
|
sizeof(*digests), GFP_NOFS);
|
|
|
|
if (!digests) {
|
2020-06-05 14:50:28 +08:00
|
|
|
kfree(*entry);
|
|
|
|
*entry = NULL;
|
|
|
|
return -ENOMEM;
|
2020-03-25 18:47:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
(*entry)->digests = digests;
|
2013-12-03 02:40:34 +08:00
|
|
|
(*entry)->template_desc = template_desc;
|
2013-06-07 18:16:33 +08:00
|
|
|
for (i = 0; i < template_desc->num_fields; i++) {
|
2018-09-08 04:22:23 +08:00
|
|
|
const struct ima_template_field *field =
|
|
|
|
template_desc->fields[i];
|
2013-06-07 18:16:33 +08:00
|
|
|
u32 len;
|
2013-06-07 18:16:28 +08:00
|
|
|
|
2015-04-11 23:09:50 +08:00
|
|
|
result = field->field_init(event_data,
|
2013-06-07 18:16:33 +08:00
|
|
|
&((*entry)->template_data[i]));
|
|
|
|
if (result != 0)
|
|
|
|
goto out;
|
2013-06-07 18:16:28 +08:00
|
|
|
|
2013-06-07 18:16:33 +08:00
|
|
|
len = (*entry)->template_data[i].len;
|
|
|
|
(*entry)->template_data_len += sizeof(len);
|
|
|
|
(*entry)->template_data_len += len;
|
|
|
|
}
|
2013-06-07 18:16:28 +08:00
|
|
|
return 0;
|
2013-06-07 18:16:33 +08:00
|
|
|
out:
|
2013-12-03 02:40:34 +08:00
|
|
|
ima_free_template_entry(*entry);
|
2013-06-07 18:16:33 +08:00
|
|
|
*entry = NULL;
|
2013-06-07 18:16:28 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-04 22:06:58 +08:00
|
|
|
/*
|
|
|
|
* ima_store_template - store ima template measurements
|
|
|
|
*
|
|
|
|
* Calculate the hash of a template entry, add the template entry
|
|
|
|
* to an ordered list of measurement entries maintained inside the kernel,
|
|
|
|
* and also update the aggregate integrity value (maintained inside the
|
|
|
|
* configured TPM PCR) over the hashes of the current list of measurement
|
|
|
|
* entries.
|
|
|
|
*
|
|
|
|
* Applications retrieve the current kernel-held measurement list through
|
|
|
|
* the securityfs entries in /sys/kernel/security/ima. The signed aggregate
|
|
|
|
* TPM PCR (called quote) can be retrieved using a TPM user space library
|
|
|
|
* and is used to validate the measurement list.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, error code otherwise
|
|
|
|
*/
|
|
|
|
int ima_store_template(struct ima_template_entry *entry,
|
2013-06-07 18:16:27 +08:00
|
|
|
int violation, struct inode *inode,
|
2016-06-02 02:14:03 +08:00
|
|
|
const unsigned char *filename, int pcr)
|
2009-02-04 22:06:58 +08:00
|
|
|
{
|
2013-12-12 03:44:04 +08:00
|
|
|
static const char op[] = "add_template_measure";
|
|
|
|
static const char audit_cause[] = "hashing_error";
|
2013-06-07 18:16:33 +08:00
|
|
|
char *template_name = entry->template_desc->name;
|
2009-02-04 22:06:58 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!violation) {
|
2013-06-07 18:16:33 +08:00
|
|
|
result = ima_calc_field_array_hash(&entry->template_data[0],
|
2020-03-25 18:47:08 +08:00
|
|
|
entry);
|
2009-02-04 22:06:58 +08:00
|
|
|
if (result < 0) {
|
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
|
2013-06-07 18:16:33 +08:00
|
|
|
template_name, op,
|
2009-02-04 22:06:58 +08:00
|
|
|
audit_cause, result, 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 02:14:03 +08:00
|
|
|
entry->pcr = pcr;
|
2013-06-07 18:16:27 +08:00
|
|
|
result = ima_add_template_entry(entry, violation, op, inode, filename);
|
2009-02-04 22:06:58 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_add_violation - add violation to measurement list.
|
|
|
|
*
|
|
|
|
* Violations are flagged in the measurement list with zero hash values.
|
|
|
|
* By extending the PCR with 0xFF's instead of with zeroes, the PCR
|
|
|
|
* value is invalidated.
|
|
|
|
*/
|
2013-06-07 18:16:26 +08:00
|
|
|
void ima_add_violation(struct file *file, const unsigned char *filename,
|
2015-04-11 23:12:39 +08:00
|
|
|
struct integrity_iint_cache *iint,
|
2009-02-04 22:06:58 +08:00
|
|
|
const char *op, const char *cause)
|
|
|
|
{
|
|
|
|
struct ima_template_entry *entry;
|
2013-12-11 14:11:28 +08:00
|
|
|
struct inode *inode = file_inode(file);
|
2019-06-11 14:28:10 +08:00
|
|
|
struct ima_event_data event_data = { .iint = iint,
|
|
|
|
.file = file,
|
|
|
|
.filename = filename,
|
|
|
|
.violation = cause };
|
2009-02-04 22:06:58 +08:00
|
|
|
int violation = 1;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* can overflow, only indicator */
|
|
|
|
atomic_long_inc(&ima_htable.violations);
|
|
|
|
|
2019-06-20 06:46:11 +08:00
|
|
|
result = ima_alloc_init_template(&event_data, &entry, NULL);
|
2013-06-07 18:16:28 +08:00
|
|
|
if (result < 0) {
|
2009-02-04 22:06:58 +08:00
|
|
|
result = -ENOMEM;
|
|
|
|
goto err_out;
|
|
|
|
}
|
2016-06-02 02:14:03 +08:00
|
|
|
result = ima_store_template(entry, violation, inode,
|
|
|
|
filename, CONFIG_IMA_MEASURE_PCR_IDX);
|
2009-02-04 22:06:58 +08:00
|
|
|
if (result < 0)
|
2013-12-03 02:40:34 +08:00
|
|
|
ima_free_template_entry(entry);
|
2009-02-04 22:06:58 +08:00
|
|
|
err_out:
|
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
|
|
|
|
op, cause, result, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-27 16:26:14 +08:00
|
|
|
* ima_get_action - appraise & measure decision based on policy.
|
2021-01-21 21:19:45 +08:00
|
|
|
* @mnt_userns: user namespace of the mount the inode was found from
|
2020-07-09 14:19:11 +08:00
|
|
|
* @inode: pointer to the inode associated with the object being validated
|
2018-01-09 05:36:20 +08:00
|
|
|
* @cred: pointer to credentials structure to validate
|
|
|
|
* @secid: secid of the task being validated
|
2017-01-06 12:38:11 +08:00
|
|
|
* @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC,
|
|
|
|
* MAY_APPEND)
|
2016-01-15 09:59:14 +08:00
|
|
|
* @func: caller identifier
|
2016-06-02 02:14:02 +08:00
|
|
|
* @pcr: pointer filled in if matched measure policy sets pcr=
|
2019-06-20 06:46:11 +08:00
|
|
|
* @template_desc: pointer filled in if matched measure policy sets template=
|
2021-01-08 12:07:01 +08:00
|
|
|
* @func_data: func specific data, may be NULL
|
2009-02-04 22:06:58 +08:00
|
|
|
*
|
|
|
|
* The policy is defined in terms of keypairs:
|
2014-03-05 00:04:20 +08:00
|
|
|
* subj=, obj=, type=, func=, mask=, fsmagic=
|
2009-02-04 22:06:58 +08:00
|
|
|
* subj,obj, and type: are LSM specific.
|
2018-01-09 05:36:20 +08:00
|
|
|
* func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK
|
2021-01-08 12:07:03 +08:00
|
|
|
* | KEXEC_CMDLINE | KEY_CHECK | CRITICAL_DATA
|
2014-03-05 00:04:20 +08:00
|
|
|
* mask: contains the permission mask
|
2009-02-04 22:06:58 +08:00
|
|
|
* fsmagic: hex value
|
|
|
|
*
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
* Returns IMA_MEASURE, IMA_APPRAISE mask.
|
|
|
|
*
|
|
|
|
*/
|
2021-01-21 21:19:45 +08:00
|
|
|
int ima_get_action(struct user_namespace *mnt_userns, struct inode *inode,
|
|
|
|
const struct cred *cred, u32 secid, int mask,
|
|
|
|
enum ima_hooks func, int *pcr,
|
2019-12-12 00:47:06 +08:00
|
|
|
struct ima_template_desc **template_desc,
|
2021-01-08 12:07:01 +08:00
|
|
|
const char *func_data)
|
2009-02-04 22:06:58 +08:00
|
|
|
{
|
ima: support new "hash" and "dont_hash" policy actions
The builtin ima_appraise_tcb policy, which is specified on the boot
command line, can be replaced with a custom policy, normally early in
the boot process. Custom policies can be more restrictive in some ways,
like requiring file signatures, but can be less restrictive in other
ways, like not appraising mutable files. With a less restrictive policy
in place, files in the builtin policy might not be hashed and labeled
with a security.ima hash. On reboot, files which should be labeled in
the ima_appraise_tcb are not labeled, possibly preventing the system
from booting properly.
To resolve this problem, this patch extends the existing IMA policy
actions "measure", "dont_measure", "appraise", "dont_appraise", and
"audit" with "hash" and "dont_hash". The new "hash" action will write
the file hash as security.ima, but without requiring the file to be
appraised as well.
For example, the builtin ima_appraise_tcb policy includes the rule,
"appraise fowner=0". Adding the "hash fowner=0" rule to a custom
policy, will cause the needed file hashes to be calculated and written
as security.ima xattrs.
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2016-09-29 22:04:52 +08:00
|
|
|
int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
|
2014-11-05 23:01:14 +08:00
|
|
|
flags &= ima_policy_flag;
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
|
2021-01-21 21:19:45 +08:00
|
|
|
return ima_match_policy(mnt_userns, inode, cred, secid, func, mask,
|
idmapped-mounts-v5.12
-----BEGIN PGP SIGNATURE-----
iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCYCegywAKCRCRxhvAZXjc
ouJ6AQDlf+7jCQlQdeKKoN9QDFfMzG1ooemat36EpRRTONaGuAD8D9A4sUsG4+5f
4IU5Lj9oY4DEmF8HenbWK2ZHsesL2Qg=
=yPaw
-----END PGP SIGNATURE-----
Merge tag 'idmapped-mounts-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux
Pull idmapped mounts from Christian Brauner:
"This introduces idmapped mounts which has been in the making for some
time. Simply put, different mounts can expose the same file or
directory with different ownership. This initial implementation comes
with ports for fat, ext4 and with Christoph's port for xfs with more
filesystems being actively worked on by independent people and
maintainers.
Idmapping mounts handle a wide range of long standing use-cases. Here
are just a few:
- Idmapped mounts make it possible to easily share files between
multiple users or multiple machines especially in complex
scenarios. For example, idmapped mounts will be used in the
implementation of portable home directories in
systemd-homed.service(8) where they allow users to move their home
directory to an external storage device and use it on multiple
computers where they are assigned different uids and gids. This
effectively makes it possible to assign random uids and gids at
login time.
- It is possible to share files from the host with unprivileged
containers without having to change ownership permanently through
chown(2).
- It is possible to idmap a container's rootfs and without having to
mangle every file. For example, Chromebooks use it to share the
user's Download folder with their unprivileged containers in their
Linux subsystem.
- It is possible to share files between containers with
non-overlapping idmappings.
- Filesystem that lack a proper concept of ownership such as fat can
use idmapped mounts to implement discretionary access (DAC)
permission checking.
- They allow users to efficiently changing ownership on a per-mount
basis without having to (recursively) chown(2) all files. In
contrast to chown (2) changing ownership of large sets of files is
instantenous with idmapped mounts. This is especially useful when
ownership of a whole root filesystem of a virtual machine or
container is changed. With idmapped mounts a single syscall
mount_setattr syscall will be sufficient to change the ownership of
all files.
- Idmapped mounts always take the current ownership into account as
idmappings specify what a given uid or gid is supposed to be mapped
to. This contrasts with the chown(2) syscall which cannot by itself
take the current ownership of the files it changes into account. It
simply changes the ownership to the specified uid and gid. This is
especially problematic when recursively chown(2)ing a large set of
files which is commong with the aforementioned portable home
directory and container and vm scenario.
- Idmapped mounts allow to change ownership locally, restricting it
to specific mounts, and temporarily as the ownership changes only
apply as long as the mount exists.
Several userspace projects have either already put up patches and
pull-requests for this feature or will do so should you decide to pull
this:
- systemd: In a wide variety of scenarios but especially right away
in their implementation of portable home directories.
https://systemd.io/HOME_DIRECTORY/
- container runtimes: containerd, runC, LXD:To share data between
host and unprivileged containers, unprivileged and privileged
containers, etc. The pull request for idmapped mounts support in
containerd, the default Kubernetes runtime is already up for quite
a while now: https://github.com/containerd/containerd/pull/4734
- The virtio-fs developers and several users have expressed interest
in using this feature with virtual machines once virtio-fs is
ported.
- ChromeOS: Sharing host-directories with unprivileged containers.
I've tightly synced with all those projects and all of those listed
here have also expressed their need/desire for this feature on the
mailing list. For more info on how people use this there's a bunch of
talks about this too. Here's just two recent ones:
https://www.cncf.io/wp-content/uploads/2020/12/Rootless-Containers-in-Gitpod.pdf
https://fosdem.org/2021/schedule/event/containers_idmap/
This comes with an extensive xfstests suite covering both ext4 and
xfs:
https://git.kernel.org/brauner/xfstests-dev/h/idmapped_mounts
It covers truncation, creation, opening, xattrs, vfscaps, setid
execution, setgid inheritance and more both with idmapped and
non-idmapped mounts. It already helped to discover an unrelated xfs
setgid inheritance bug which has since been fixed in mainline. It will
be sent for inclusion with the xfstests project should you decide to
merge this.
In order to support per-mount idmappings vfsmounts are marked with
user namespaces. The idmapping of the user namespace will be used to
map the ids of vfs objects when they are accessed through that mount.
By default all vfsmounts are marked with the initial user namespace.
The initial user namespace is used to indicate that a mount is not
idmapped. All operations behave as before and this is verified in the
testsuite.
Based on prior discussions we want to attach the whole user namespace
and not just a dedicated idmapping struct. This allows us to reuse all
the helpers that already exist for dealing with idmappings instead of
introducing a whole new range of helpers. In addition, if we decide in
the future that we are confident enough to enable unprivileged users
to setup idmapped mounts the permission checking can take into account
whether the caller is privileged in the user namespace the mount is
currently marked with.
The user namespace the mount will be marked with can be specified by
passing a file descriptor refering to the user namespace as an
argument to the new mount_setattr() syscall together with the new
MOUNT_ATTR_IDMAP flag. The system call follows the openat2() pattern
of extensibility.
The following conditions must be met in order to create an idmapped
mount:
- The caller must currently have the CAP_SYS_ADMIN capability in the
user namespace the underlying filesystem has been mounted in.
- The underlying filesystem must support idmapped mounts.
- The mount must not already be idmapped. This also implies that the
idmapping of a mount cannot be altered once it has been idmapped.
- The mount must be a detached/anonymous mount, i.e. it must have
been created by calling open_tree() with the OPEN_TREE_CLONE flag
and it must not already have been visible in the filesystem.
The last two points guarantee easier semantics for userspace and the
kernel and make the implementation significantly simpler.
By default vfsmounts are marked with the initial user namespace and no
behavioral or performance changes are observed.
The manpage with a detailed description can be found here:
https://git.kernel.org/brauner/man-pages/c/1d7b902e2875a1ff342e036a9f866a995640aea8
In order to support idmapped mounts, filesystems need to be changed
and mark themselves with the FS_ALLOW_IDMAP flag in fs_flags. The
patches to convert individual filesystem are not very large or
complicated overall as can be seen from the included fat, ext4, and
xfs ports. Patches for other filesystems are actively worked on and
will be sent out separately. The xfstestsuite can be used to verify
that port has been done correctly.
The mount_setattr() syscall is motivated independent of the idmapped
mounts patches and it's been around since July 2019. One of the most
valuable features of the new mount api is the ability to perform
mounts based on file descriptors only.
Together with the lookup restrictions available in the openat2()
RESOLVE_* flag namespace which we added in v5.6 this is the first time
we are close to hardened and race-free (e.g. symlinks) mounting and
path resolution.
While userspace has started porting to the new mount api to mount
proper filesystems and create new bind-mounts it is currently not
possible to change mount options of an already existing bind mount in
the new mount api since the mount_setattr() syscall is missing.
With the addition of the mount_setattr() syscall we remove this last
restriction and userspace can now fully port to the new mount api,
covering every use-case the old mount api could. We also add the
crucial ability to recursively change mount options for a whole mount
tree, both removing and adding mount options at the same time. This
syscall has been requested multiple times by various people and
projects.
There is a simple tool available at
https://github.com/brauner/mount-idmapped
that allows to create idmapped mounts so people can play with this
patch series. I'll add support for the regular mount binary should you
decide to pull this in the following weeks:
Here's an example to a simple idmapped mount of another user's home
directory:
u1001@f2-vm:/$ sudo ./mount --idmap both:1000:1001:1 /home/ubuntu/ /mnt
u1001@f2-vm:/$ ls -al /home/ubuntu/
total 28
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 28 22:07 .
drwxr-xr-x 4 root root 4096 Oct 28 04:00 ..
-rw------- 1 ubuntu ubuntu 3154 Oct 28 22:12 .bash_history
-rw-r--r-- 1 ubuntu ubuntu 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Feb 25 2020 .bashrc
-rw-r--r-- 1 ubuntu ubuntu 807 Feb 25 2020 .profile
-rw-r--r-- 1 ubuntu ubuntu 0 Oct 16 16:11 .sudo_as_admin_successful
-rw------- 1 ubuntu ubuntu 1144 Oct 28 00:43 .viminfo
u1001@f2-vm:/$ ls -al /mnt/
total 28
drwxr-xr-x 2 u1001 u1001 4096 Oct 28 22:07 .
drwxr-xr-x 29 root root 4096 Oct 28 22:01 ..
-rw------- 1 u1001 u1001 3154 Oct 28 22:12 .bash_history
-rw-r--r-- 1 u1001 u1001 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 u1001 u1001 3771 Feb 25 2020 .bashrc
-rw-r--r-- 1 u1001 u1001 807 Feb 25 2020 .profile
-rw-r--r-- 1 u1001 u1001 0 Oct 16 16:11 .sudo_as_admin_successful
-rw------- 1 u1001 u1001 1144 Oct 28 00:43 .viminfo
u1001@f2-vm:/$ touch /mnt/my-file
u1001@f2-vm:/$ setfacl -m u:1001:rwx /mnt/my-file
u1001@f2-vm:/$ sudo setcap -n 1001 cap_net_raw+ep /mnt/my-file
u1001@f2-vm:/$ ls -al /mnt/my-file
-rw-rwxr--+ 1 u1001 u1001 0 Oct 28 22:14 /mnt/my-file
u1001@f2-vm:/$ ls -al /home/ubuntu/my-file
-rw-rwxr--+ 1 ubuntu ubuntu 0 Oct 28 22:14 /home/ubuntu/my-file
u1001@f2-vm:/$ getfacl /mnt/my-file
getfacl: Removing leading '/' from absolute path names
# file: mnt/my-file
# owner: u1001
# group: u1001
user::rw-
user:u1001:rwx
group::rw-
mask::rwx
other::r--
u1001@f2-vm:/$ getfacl /home/ubuntu/my-file
getfacl: Removing leading '/' from absolute path names
# file: home/ubuntu/my-file
# owner: ubuntu
# group: ubuntu
user::rw-
user:ubuntu:rwx
group::rw-
mask::rwx
other::r--"
* tag 'idmapped-mounts-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux: (41 commits)
xfs: remove the possibly unused mp variable in xfs_file_compat_ioctl
xfs: support idmapped mounts
ext4: support idmapped mounts
fat: handle idmapped mounts
tests: add mount_setattr() selftests
fs: introduce MOUNT_ATTR_IDMAP
fs: add mount_setattr()
fs: add attr_flags_to_mnt_flags helper
fs: split out functions to hold writers
namespace: only take read lock in do_reconfigure_mnt()
mount: make {lock,unlock}_mount_hash() static
namespace: take lock_mount_hash() directly when changing flags
nfs: do not export idmapped mounts
overlayfs: do not mount on top of idmapped mounts
ecryptfs: do not mount on top of idmapped mounts
ima: handle idmapped mounts
apparmor: handle idmapped mounts
fs: make helpers idmap mount aware
exec: handle idmapped mounts
would_dump: handle idmapped mounts
...
2021-02-24 05:39:45 +08:00
|
|
|
flags, pcr, template_desc, func_data);
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
}
|
2009-02-04 22:06:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_collect_measurement - collect file measurement
|
|
|
|
*
|
|
|
|
* Calculate the file hash, if it doesn't already exist,
|
|
|
|
* storing the measurement and i_version in the iint.
|
|
|
|
*
|
|
|
|
* Must be called with iint->mutex held.
|
|
|
|
*
|
|
|
|
* Return 0 on success, error code otherwise
|
|
|
|
*/
|
2011-03-10 03:13:22 +08:00
|
|
|
int ima_collect_measurement(struct integrity_iint_cache *iint,
|
2016-01-15 06:57:47 +08:00
|
|
|
struct file *file, void *buf, loff_t size,
|
2019-06-28 10:19:31 +08:00
|
|
|
enum hash_algo algo, struct modsig *modsig)
|
2009-02-04 22:06:58 +08:00
|
|
|
{
|
ima: audit log files opened with O_DIRECT flag
Files are measured or appraised based on the IMA policy. When a
file, in policy, is opened with the O_DIRECT flag, a deadlock
occurs.
The first attempt at resolving this lockdep temporarily removed the
O_DIRECT flag and restored it, after calculating the hash. The
second attempt introduced the O_DIRECT_HAVELOCK flag. Based on this
flag, do_blockdev_direct_IO() would skip taking the i_mutex a second
time. The third attempt, by Dmitry Kasatkin, resolves the i_mutex
locking issue, by re-introducing the IMA mutex, but uncovered
another problem. Reading a file with O_DIRECT flag set, writes
directly to userspace pages. A second patch allocates a user-space
like memory. This works for all IMA hooks, except ima_file_free(),
which is called on __fput() to recalculate the file hash.
Until this last issue is addressed, do not 'collect' the
measurement for measuring, appraising, or auditing files opened
with the O_DIRECT flag set. Based on policy, permit or deny file
access. This patch defines a new IMA policy rule option named
'permit_directio'. Policy rules could be defined, based on LSM
or other criteria, to permit specific applications to open files
with the O_DIRECT flag set.
Changelog v1:
- permit or deny file access based IMA policy rules
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Acked-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Cc: <stable@vger.kernel.org>
2014-05-12 21:28:11 +08:00
|
|
|
const char *audit_cause = "failed";
|
2013-01-24 06:07:38 +08:00
|
|
|
struct inode *inode = file_inode(file);
|
2014-10-31 13:22:04 +08:00
|
|
|
const char *filename = file->f_path.dentry->d_name.name;
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
int result = 0;
|
2017-06-18 11:56:23 +08:00
|
|
|
int length;
|
|
|
|
void *tmpbuf;
|
|
|
|
u64 i_version;
|
2013-04-25 15:44:04 +08:00
|
|
|
struct {
|
|
|
|
struct ima_digest_data hdr;
|
|
|
|
char digest[IMA_MAX_DIGEST_SIZE];
|
|
|
|
} hash;
|
2009-02-04 22:06:58 +08:00
|
|
|
|
2019-06-28 10:19:33 +08:00
|
|
|
/*
|
|
|
|
* Always collect the modsig, because IMA might have already collected
|
|
|
|
* the file digest without collecting the modsig in a previous
|
|
|
|
* measurement rule.
|
|
|
|
*/
|
|
|
|
if (modsig)
|
|
|
|
ima_collect_modsig(modsig, buf, size);
|
|
|
|
|
2017-06-18 11:56:23 +08:00
|
|
|
if (iint->flags & IMA_COLLECTED)
|
|
|
|
goto out;
|
2009-02-04 22:06:58 +08:00
|
|
|
|
2017-06-18 11:56:23 +08:00
|
|
|
/*
|
|
|
|
* Dectecting file change is based on i_version. On filesystems
|
|
|
|
* which do not support i_version, support is limited to an initial
|
|
|
|
* measurement/appraisal/audit.
|
|
|
|
*/
|
2017-12-11 19:35:21 +08:00
|
|
|
i_version = inode_query_iversion(inode);
|
2017-06-18 11:56:23 +08:00
|
|
|
hash.hdr.algo = algo;
|
ima: audit log files opened with O_DIRECT flag
Files are measured or appraised based on the IMA policy. When a
file, in policy, is opened with the O_DIRECT flag, a deadlock
occurs.
The first attempt at resolving this lockdep temporarily removed the
O_DIRECT flag and restored it, after calculating the hash. The
second attempt introduced the O_DIRECT_HAVELOCK flag. Based on this
flag, do_blockdev_direct_IO() would skip taking the i_mutex a second
time. The third attempt, by Dmitry Kasatkin, resolves the i_mutex
locking issue, by re-introducing the IMA mutex, but uncovered
another problem. Reading a file with O_DIRECT flag set, writes
directly to userspace pages. A second patch allocates a user-space
like memory. This works for all IMA hooks, except ima_file_free(),
which is called on __fput() to recalculate the file hash.
Until this last issue is addressed, do not 'collect' the
measurement for measuring, appraising, or auditing files opened
with the O_DIRECT flag set. Based on policy, permit or deny file
access. This patch defines a new IMA policy rule option named
'permit_directio'. Policy rules could be defined, based on LSM
or other criteria, to permit specific applications to open files
with the O_DIRECT flag set.
Changelog v1:
- permit or deny file access based IMA policy rules
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Acked-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Cc: <stable@vger.kernel.org>
2014-05-12 21:28:11 +08:00
|
|
|
|
2017-06-18 11:56:23 +08:00
|
|
|
/* Initialize hash digest to 0's in case of failure */
|
|
|
|
memset(&hash.digest, 0, sizeof(hash.digest));
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
result = ima_calc_buffer_hash(buf, size, &hash.hdr);
|
|
|
|
else
|
|
|
|
result = ima_calc_file_hash(file, &hash.hdr);
|
|
|
|
|
|
|
|
if (result && result != -EBADF && result != -EINVAL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
length = sizeof(hash.hdr) + hash.hdr.length;
|
|
|
|
tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS);
|
|
|
|
if (!tmpbuf) {
|
|
|
|
result = -ENOMEM;
|
|
|
|
goto out;
|
2009-02-04 22:06:58 +08:00
|
|
|
}
|
2017-06-18 11:56:23 +08:00
|
|
|
|
|
|
|
iint->ima_hash = tmpbuf;
|
|
|
|
memcpy(iint->ima_hash, &hash, length);
|
|
|
|
iint->version = i_version;
|
|
|
|
|
|
|
|
/* Possibly temporary failure due to type of read (eg. O_DIRECT) */
|
|
|
|
if (!result)
|
|
|
|
iint->flags |= IMA_COLLECTED;
|
ima: audit log files opened with O_DIRECT flag
Files are measured or appraised based on the IMA policy. When a
file, in policy, is opened with the O_DIRECT flag, a deadlock
occurs.
The first attempt at resolving this lockdep temporarily removed the
O_DIRECT flag and restored it, after calculating the hash. The
second attempt introduced the O_DIRECT_HAVELOCK flag. Based on this
flag, do_blockdev_direct_IO() would skip taking the i_mutex a second
time. The third attempt, by Dmitry Kasatkin, resolves the i_mutex
locking issue, by re-introducing the IMA mutex, but uncovered
another problem. Reading a file with O_DIRECT flag set, writes
directly to userspace pages. A second patch allocates a user-space
like memory. This works for all IMA hooks, except ima_file_free(),
which is called on __fput() to recalculate the file hash.
Until this last issue is addressed, do not 'collect' the
measurement for measuring, appraising, or auditing files opened
with the O_DIRECT flag set. Based on policy, permit or deny file
access. This patch defines a new IMA policy rule option named
'permit_directio'. Policy rules could be defined, based on LSM
or other criteria, to permit specific applications to open files
with the O_DIRECT flag set.
Changelog v1:
- permit or deny file access based IMA policy rules
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Acked-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Cc: <stable@vger.kernel.org>
2014-05-12 21:28:11 +08:00
|
|
|
out:
|
2017-06-18 11:56:23 +08:00
|
|
|
if (result) {
|
|
|
|
if (file->f_flags & O_DIRECT)
|
|
|
|
audit_cause = "failed(directio)";
|
|
|
|
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
|
ima: audit log files opened with O_DIRECT flag
Files are measured or appraised based on the IMA policy. When a
file, in policy, is opened with the O_DIRECT flag, a deadlock
occurs.
The first attempt at resolving this lockdep temporarily removed the
O_DIRECT flag and restored it, after calculating the hash. The
second attempt introduced the O_DIRECT_HAVELOCK flag. Based on this
flag, do_blockdev_direct_IO() would skip taking the i_mutex a second
time. The third attempt, by Dmitry Kasatkin, resolves the i_mutex
locking issue, by re-introducing the IMA mutex, but uncovered
another problem. Reading a file with O_DIRECT flag set, writes
directly to userspace pages. A second patch allocates a user-space
like memory. This works for all IMA hooks, except ima_file_free(),
which is called on __fput() to recalculate the file hash.
Until this last issue is addressed, do not 'collect' the
measurement for measuring, appraising, or auditing files opened
with the O_DIRECT flag set. Based on policy, permit or deny file
access. This patch defines a new IMA policy rule option named
'permit_directio'. Policy rules could be defined, based on LSM
or other criteria, to permit specific applications to open files
with the O_DIRECT flag set.
Changelog v1:
- permit or deny file access based IMA policy rules
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Acked-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Cc: <stable@vger.kernel.org>
2014-05-12 21:28:11 +08:00
|
|
|
filename, "collect_data", audit_cause,
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
result, 0);
|
2017-06-18 11:56:23 +08:00
|
|
|
}
|
2009-02-04 22:06:58 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ima_store_measurement - store file measurement
|
|
|
|
*
|
|
|
|
* Create an "ima" template and then store the template by calling
|
|
|
|
* ima_store_template.
|
|
|
|
*
|
|
|
|
* We only get here if the inode has not already been measured,
|
|
|
|
* but the measurement could already exist:
|
2014-03-05 00:04:20 +08:00
|
|
|
* - multiple copies of the same file on either the same or
|
2009-02-04 22:06:58 +08:00
|
|
|
* different filesystems.
|
|
|
|
* - the inode was previously flushed as well as the iint info,
|
|
|
|
* containing the hashing info.
|
|
|
|
*
|
|
|
|
* Must be called with iint->mutex held.
|
|
|
|
*/
|
2011-03-10 03:13:22 +08:00
|
|
|
void ima_store_measurement(struct integrity_iint_cache *iint,
|
2013-07-23 23:15:00 +08:00
|
|
|
struct file *file, const unsigned char *filename,
|
|
|
|
struct evm_ima_xattr_data *xattr_value,
|
2019-06-28 10:19:32 +08:00
|
|
|
int xattr_len, const struct modsig *modsig, int pcr,
|
2019-06-20 06:46:11 +08:00
|
|
|
struct ima_template_desc *template_desc)
|
2009-02-04 22:06:58 +08:00
|
|
|
{
|
2013-12-12 03:44:04 +08:00
|
|
|
static const char op[] = "add_template_measure";
|
|
|
|
static const char audit_cause[] = "ENOMEM";
|
2009-02-04 22:06:58 +08:00
|
|
|
int result = -ENOMEM;
|
2013-01-24 06:07:38 +08:00
|
|
|
struct inode *inode = file_inode(file);
|
2009-02-04 22:06:58 +08:00
|
|
|
struct ima_template_entry *entry;
|
2019-06-11 14:28:10 +08:00
|
|
|
struct ima_event_data event_data = { .iint = iint,
|
|
|
|
.file = file,
|
|
|
|
.filename = filename,
|
|
|
|
.xattr_value = xattr_value,
|
2019-06-28 10:19:32 +08:00
|
|
|
.xattr_len = xattr_len,
|
|
|
|
.modsig = modsig };
|
2009-02-04 22:06:58 +08:00
|
|
|
int violation = 0;
|
|
|
|
|
2019-06-28 10:19:33 +08:00
|
|
|
/*
|
|
|
|
* We still need to store the measurement in the case of MODSIG because
|
|
|
|
* we only have its contents to put in the list at the time of
|
|
|
|
* appraisal, but a file measurement from earlier might already exist in
|
|
|
|
* the measurement list.
|
|
|
|
*/
|
|
|
|
if (iint->measured_pcrs & (0x1 << pcr) && !modsig)
|
ima: integrity appraisal extension
IMA currently maintains an integrity measurement list used to assert the
integrity of the running system to a third party. The IMA-appraisal
extension adds local integrity validation and enforcement of the
measurement against a "good" value stored as an extended attribute
'security.ima'. The initial methods for validating 'security.ima' are
hashed based, which provides file data integrity, and digital signature
based, which in addition to providing file data integrity, provides
authenticity.
This patch creates and maintains the 'security.ima' xattr, containing
the file data hash measurement. Protection of the xattr is provided by
EVM, if enabled and configured.
Based on policy, IMA calls evm_verifyxattr() to verify a file's metadata
integrity and, assuming success, compares the file's current hash value
with the one stored as an extended attribute in 'security.ima'.
Changelov v4:
- changed iint cache flags to hex values
Changelog v3:
- change appraisal default for filesystems without xattr support to fail
Changelog v2:
- fix audit msg 'res' value
- removed unused 'ima_appraise=' values
Changelog v1:
- removed unused iint mutex (Dmitry Kasatkin)
- setattr hook must not reset appraised (Dmitry Kasatkin)
- evm_verifyxattr() now differentiates between no 'security.evm' xattr
(INTEGRITY_NOLABEL) and no EVM 'protected' xattrs included in the
'security.evm' (INTEGRITY_NOXATTRS).
- replace hash_status with ima_status (Dmitry Kasatkin)
- re-initialize slab element ima_status on free (Dmitry Kasatkin)
- include 'security.ima' in EVM if CONFIG_IMA_APPRAISE, not CONFIG_IMA
- merged half "ima: ima_must_appraise_or_measure API change" (Dmitry Kasatkin)
- removed unnecessary error variable in process_measurement() (Dmitry Kasatkin)
- use ima_inode_post_setattr() stub function, if IMA_APPRAISE not configured
(moved ima_inode_post_setattr() to ima_appraise.c)
- make sure ima_collect_measurement() can read file
Changelog:
- add 'iint' to evm_verifyxattr() call (Dimitry Kasatkin)
- fix the race condition between chmod, which takes the i_mutex and then
iint->mutex, and ima_file_free() and process_measurement(), which take
the locks in the reverse order, by eliminating iint->mutex. (Dmitry Kasatkin)
- cleanup of ima_appraise_measurement() (Dmitry Kasatkin)
- changes as a result of the iint not allocated for all regular files, but
only for those measured/appraised.
- don't try to appraise new/empty files
- expanded ima_appraisal description in ima/Kconfig
- IMA appraise definitions required even if IMA_APPRAISE not enabled
- add return value to ima_must_appraise() stub
- unconditionally set status = INTEGRITY_PASS *after* testing status,
not before. (Found by Joe Perches)
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
2012-02-13 23:15:05 +08:00
|
|
|
return;
|
|
|
|
|
2019-06-20 06:46:11 +08:00
|
|
|
result = ima_alloc_init_template(&event_data, &entry, template_desc);
|
2013-06-07 18:16:28 +08:00
|
|
|
if (result < 0) {
|
2009-02-04 22:06:58 +08:00
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
|
|
|
|
op, audit_cause, result, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-02 02:14:03 +08:00
|
|
|
result = ima_store_template(entry, violation, inode, filename, pcr);
|
2017-06-18 11:56:23 +08:00
|
|
|
if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) {
|
2009-02-04 22:06:58 +08:00
|
|
|
iint->flags |= IMA_MEASURED;
|
2016-06-02 02:14:06 +08:00
|
|
|
iint->measured_pcrs |= (0x1 << pcr);
|
|
|
|
}
|
2011-12-19 22:57:27 +08:00
|
|
|
if (result < 0)
|
2013-12-03 02:40:34 +08:00
|
|
|
ima_free_template_entry(entry);
|
2009-02-04 22:06:58 +08:00
|
|
|
}
|
2012-06-15 01:04:36 +08:00
|
|
|
|
|
|
|
void ima_audit_measurement(struct integrity_iint_cache *iint,
|
|
|
|
const unsigned char *filename)
|
|
|
|
{
|
|
|
|
struct audit_buffer *ab;
|
2018-03-09 07:08:36 +08:00
|
|
|
char *hash;
|
2013-06-07 18:16:38 +08:00
|
|
|
const char *algo_name = hash_algo_name[iint->ima_hash->algo];
|
2012-06-15 01:04:36 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (iint->flags & IMA_AUDITED)
|
|
|
|
return;
|
|
|
|
|
2018-03-09 07:08:36 +08:00
|
|
|
hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL);
|
|
|
|
if (!hash)
|
|
|
|
return;
|
|
|
|
|
2013-04-25 15:44:04 +08:00
|
|
|
for (i = 0; i < iint->ima_hash->length; i++)
|
|
|
|
hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
|
2012-06-15 01:04:36 +08:00
|
|
|
hash[i * 2] = '\0';
|
|
|
|
|
2018-05-13 09:58:20 +08:00
|
|
|
ab = audit_log_start(audit_context(), GFP_KERNEL,
|
2012-06-15 01:04:36 +08:00
|
|
|
AUDIT_INTEGRITY_RULE);
|
|
|
|
if (!ab)
|
2018-03-09 07:08:36 +08:00
|
|
|
goto out;
|
2012-06-15 01:04:36 +08:00
|
|
|
|
|
|
|
audit_log_format(ab, "file=");
|
|
|
|
audit_log_untrustedstring(ab, filename);
|
2018-03-09 07:08:36 +08:00
|
|
|
audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash);
|
2012-06-15 01:04:36 +08:00
|
|
|
|
2018-11-27 07:40:07 +08:00
|
|
|
audit_log_task_info(ab);
|
2012-06-15 01:04:36 +08:00
|
|
|
audit_log_end(ab);
|
|
|
|
|
|
|
|
iint->flags |= IMA_AUDITED;
|
2018-03-09 07:08:36 +08:00
|
|
|
out:
|
|
|
|
kfree(hash);
|
|
|
|
return;
|
2012-06-15 01:04:36 +08:00
|
|
|
}
|
2012-09-04 05:40:17 +08:00
|
|
|
|
2017-01-17 19:45:41 +08:00
|
|
|
/*
|
|
|
|
* ima_d_path - return a pointer to the full pathname
|
|
|
|
*
|
|
|
|
* Attempt to return a pointer to the full pathname for use in the
|
|
|
|
* IMA measurement list, IMA audit records, and auditing logs.
|
|
|
|
*
|
|
|
|
* On failure, return a pointer to a copy of the filename, not dname.
|
|
|
|
* Returning a pointer to dname, could result in using the pointer
|
|
|
|
* after the memory has been freed.
|
|
|
|
*/
|
|
|
|
const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
|
2012-09-04 05:40:17 +08:00
|
|
|
{
|
|
|
|
char *pathname = NULL;
|
|
|
|
|
2014-10-02 02:43:10 +08:00
|
|
|
*pathbuf = __getname();
|
2012-09-04 05:40:17 +08:00
|
|
|
if (*pathbuf) {
|
2014-08-19 21:48:39 +08:00
|
|
|
pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
|
2012-09-04 05:40:17 +08:00
|
|
|
if (IS_ERR(pathname)) {
|
2014-10-02 02:43:10 +08:00
|
|
|
__putname(*pathbuf);
|
2012-09-04 05:40:17 +08:00
|
|
|
*pathbuf = NULL;
|
|
|
|
pathname = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 19:45:41 +08:00
|
|
|
|
|
|
|
if (!pathname) {
|
|
|
|
strlcpy(namebuf, path->dentry->d_name.name, NAME_MAX);
|
|
|
|
pathname = namebuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathname;
|
2012-09-04 05:40:17 +08:00
|
|
|
}
|