mirror of
https://github.com/torvalds/linux.git
synced 2026-09-25 04:43:52 -04:00
LSM: Add "contents" flag to kernel_read_file hook
As with the kernel_load_data LSM hook, add a "contents" flag to the kernel_read_file LSM hook that indicates whether the LSM can expect a matching call to the kernel_post_read_file LSM hook with the full contents of the file. With the coming addition of partial file read support for kernel_read_file*() API, the LSM will no longer be able to always see the entire contents of a file during the read calls. For cases where the LSM must read examine the complete file contents, it will need to do so on its own every time the kernel_read_file hook is called with contents=false (or reject such cases). Adjust all existing LSMs to retain existing behavior. Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com> Link: https://lore.kernel.org/r/20201002173828.2099543-12-keescook@chromium.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
38f901735a
commit
2039bda1fa
@@ -602,6 +602,7 @@ void ima_post_path_mknod(struct dentry *dentry)
|
||||
* ima_read_file - pre-measure/appraise hook decision based on policy
|
||||
* @file: pointer to the file to be measured/appraised/audit
|
||||
* @read_id: caller identifier
|
||||
* @contents: whether a subsequent call will be made to ima_post_read_file()
|
||||
*
|
||||
* Permit reading a file based on policy. The policy rules are written
|
||||
* in terms of the policy identifier. Appraising the integrity of
|
||||
@@ -609,8 +610,15 @@ void ima_post_path_mknod(struct dentry *dentry)
|
||||
*
|
||||
* For permission return 0, otherwise return -EACCES.
|
||||
*/
|
||||
int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
|
||||
int ima_read_file(struct file *file, enum kernel_read_file_id read_id,
|
||||
bool contents)
|
||||
{
|
||||
/* Reject all partial reads during appraisal. */
|
||||
if (!contents) {
|
||||
if (ima_appraise & IMA_APPRAISE_ENFORCE)
|
||||
return -EACCES;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do devices using pre-allocated memory run the risk of the
|
||||
* firmware being accessible to the device prior to the completion
|
||||
|
||||
@@ -118,11 +118,21 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
|
||||
}
|
||||
}
|
||||
|
||||
static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
|
||||
static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
|
||||
bool contents)
|
||||
{
|
||||
struct super_block *load_root;
|
||||
const char *origin = kernel_read_file_id_str(id);
|
||||
|
||||
/*
|
||||
* If we will not know that we'll be seeing the full contents
|
||||
* then we cannot trust a load will be complete and unchanged
|
||||
* off disk. Treat all contents=false hooks as if there were
|
||||
* no associated file struct.
|
||||
*/
|
||||
if (!contents)
|
||||
file = NULL;
|
||||
|
||||
/* If the file id is excluded, ignore the pinning. */
|
||||
if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
|
||||
ignore_read_file_id[id]) {
|
||||
@@ -179,7 +189,7 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
|
||||
|
||||
static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
|
||||
{
|
||||
return loadpin_read_file(NULL, (enum kernel_read_file_id) id);
|
||||
return loadpin_read_file(NULL, (enum kernel_read_file_id) id, contents);
|
||||
}
|
||||
|
||||
static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
|
||||
|
||||
@@ -1672,14 +1672,15 @@ int security_kernel_module_request(char *kmod_name)
|
||||
return integrity_kernel_module_request(kmod_name);
|
||||
}
|
||||
|
||||
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
|
||||
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
|
||||
bool contents)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = call_int_hook(kernel_read_file, 0, file, id);
|
||||
ret = call_int_hook(kernel_read_file, 0, file, id, contents);
|
||||
if (ret)
|
||||
return ret;
|
||||
return ima_read_file(file, id);
|
||||
return ima_read_file(file, id, contents);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(security_kernel_read_file);
|
||||
|
||||
|
||||
@@ -4003,13 +4003,14 @@ static int selinux_kernel_module_from_file(struct file *file)
|
||||
}
|
||||
|
||||
static int selinux_kernel_read_file(struct file *file,
|
||||
enum kernel_read_file_id id)
|
||||
enum kernel_read_file_id id,
|
||||
bool contents)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
switch (id) {
|
||||
case READING_MODULE:
|
||||
rc = selinux_kernel_module_from_file(file);
|
||||
rc = selinux_kernel_module_from_file(contents ? file : NULL);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user