Skip to content

Commit 2039bda

Browse files
keesgregkh
authored andcommittedOct 5, 2020
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>
1 parent 38f9017 commit 2039bda

File tree

9 files changed

+41
-14
lines changed

9 files changed

+41
-14
lines changed
 

‎fs/kernel_read_file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int kernel_read_file(struct file *file, void **buf,
3939
if (ret)
4040
return ret;
4141

42-
ret = security_kernel_read_file(file, id);
42+
ret = security_kernel_read_file(file, id, true);
4343
if (ret)
4444
goto out;
4545

‎include/linux/ima.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ extern int ima_file_mprotect(struct vm_area_struct *vma, unsigned long prot);
2323
extern int ima_load_data(enum kernel_load_data_id id, bool contents);
2424
extern int ima_post_load_data(char *buf, loff_t size,
2525
enum kernel_load_data_id id, char *description);
26-
extern int ima_read_file(struct file *file, enum kernel_read_file_id id);
26+
extern int ima_read_file(struct file *file, enum kernel_read_file_id id,
27+
bool contents);
2728
extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
2829
enum kernel_read_file_id id);
2930
extern void ima_post_path_mknod(struct dentry *dentry);
@@ -92,7 +93,8 @@ static inline int ima_post_load_data(char *buf, loff_t size,
9293
return 0;
9394
}
9495

95-
static inline int ima_read_file(struct file *file, enum kernel_read_file_id id)
96+
static inline int ima_read_file(struct file *file, enum kernel_read_file_id id,
97+
bool contents)
9698
{
9799
return 0;
98100
}

‎include/linux/lsm_hook_defs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ LSM_HOOK(int, 0, kernel_load_data, enum kernel_load_data_id id, bool contents)
188188
LSM_HOOK(int, 0, kernel_post_load_data, char *buf, loff_t size,
189189
enum kernel_read_file_id id, char *description)
190190
LSM_HOOK(int, 0, kernel_read_file, struct file *file,
191-
enum kernel_read_file_id id)
191+
enum kernel_read_file_id id, bool contents)
192192
LSM_HOOK(int, 0, kernel_post_read_file, struct file *file, char *buf,
193193
loff_t size, enum kernel_read_file_id id)
194194
LSM_HOOK(int, 0, task_fix_setuid, struct cred *new, const struct cred *old,

‎include/linux/lsm_hooks.h

+3
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@
651651
* @file contains the file structure pointing to the file being read
652652
* by the kernel.
653653
* @id kernel read file identifier
654+
* @contents if a subsequent @kernel_post_read_file will be called.
654655
* Return 0 if permission is granted.
655656
* @kernel_post_read_file:
656657
* Read a file specified by userspace.
@@ -659,6 +660,8 @@
659660
* @buf pointer to buffer containing the file contents.
660661
* @size length of the file contents.
661662
* @id kernel read file identifier
663+
* This must be paired with a prior @kernel_read_file call that had
664+
* @contents set to true.
662665
* Return 0 if permission is granted.
663666
* @task_fix_setuid:
664667
* Update the module's state after setting one or more of the user

‎include/linux/security.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ int security_kernel_load_data(enum kernel_load_data_id id, bool contents);
391391
int security_kernel_post_load_data(char *buf, loff_t size,
392392
enum kernel_load_data_id id,
393393
char *description);
394-
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id);
394+
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
395+
bool contents);
395396
int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
396397
enum kernel_read_file_id id);
397398
int security_task_fix_setuid(struct cred *new, const struct cred *old,
@@ -1030,7 +1031,8 @@ static inline int security_kernel_post_load_data(char *buf, loff_t size,
10301031
}
10311032

10321033
static inline int security_kernel_read_file(struct file *file,
1033-
enum kernel_read_file_id id)
1034+
enum kernel_read_file_id id,
1035+
bool contents)
10341036
{
10351037
return 0;
10361038
}

‎security/integrity/ima/ima_main.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,23 @@ void ima_post_path_mknod(struct dentry *dentry)
602602
* ima_read_file - pre-measure/appraise hook decision based on policy
603603
* @file: pointer to the file to be measured/appraised/audit
604604
* @read_id: caller identifier
605+
* @contents: whether a subsequent call will be made to ima_post_read_file()
605606
*
606607
* Permit reading a file based on policy. The policy rules are written
607608
* in terms of the policy identifier. Appraising the integrity of
608609
* a file requires a file descriptor.
609610
*
610611
* For permission return 0, otherwise return -EACCES.
611612
*/
612-
int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
613+
int ima_read_file(struct file *file, enum kernel_read_file_id read_id,
614+
bool contents)
613615
{
616+
/* Reject all partial reads during appraisal. */
617+
if (!contents) {
618+
if (ima_appraise & IMA_APPRAISE_ENFORCE)
619+
return -EACCES;
620+
}
621+
614622
/*
615623
* Do devices using pre-allocated memory run the risk of the
616624
* firmware being accessible to the device prior to the completion

‎security/loadpin/loadpin.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,21 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
118118
}
119119
}
120120

121-
static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
121+
static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
122+
bool contents)
122123
{
123124
struct super_block *load_root;
124125
const char *origin = kernel_read_file_id_str(id);
125126

127+
/*
128+
* If we will not know that we'll be seeing the full contents
129+
* then we cannot trust a load will be complete and unchanged
130+
* off disk. Treat all contents=false hooks as if there were
131+
* no associated file struct.
132+
*/
133+
if (!contents)
134+
file = NULL;
135+
126136
/* If the file id is excluded, ignore the pinning. */
127137
if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
128138
ignore_read_file_id[id]) {
@@ -179,7 +189,7 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
179189

180190
static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
181191
{
182-
return loadpin_read_file(NULL, (enum kernel_read_file_id) id);
192+
return loadpin_read_file(NULL, (enum kernel_read_file_id) id, contents);
183193
}
184194

185195
static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {

‎security/security.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1672,14 +1672,15 @@ int security_kernel_module_request(char *kmod_name)
16721672
return integrity_kernel_module_request(kmod_name);
16731673
}
16741674

1675-
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
1675+
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
1676+
bool contents)
16761677
{
16771678
int ret;
16781679

1679-
ret = call_int_hook(kernel_read_file, 0, file, id);
1680+
ret = call_int_hook(kernel_read_file, 0, file, id, contents);
16801681
if (ret)
16811682
return ret;
1682-
return ima_read_file(file, id);
1683+
return ima_read_file(file, id, contents);
16831684
}
16841685
EXPORT_SYMBOL_GPL(security_kernel_read_file);
16851686

‎security/selinux/hooks.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -4003,13 +4003,14 @@ static int selinux_kernel_module_from_file(struct file *file)
40034003
}
40044004

40054005
static int selinux_kernel_read_file(struct file *file,
4006-
enum kernel_read_file_id id)
4006+
enum kernel_read_file_id id,
4007+
bool contents)
40074008
{
40084009
int rc = 0;
40094010

40104011
switch (id) {
40114012
case READING_MODULE:
4012-
rc = selinux_kernel_module_from_file(file);
4013+
rc = selinux_kernel_module_from_file(contents ? file : NULL);
40134014
break;
40144015
default:
40154016
break;

0 commit comments

Comments
 (0)
Please sign in to comment.