Skip to content

Commit 39eeb4f

Browse files
author
Mimi Zohar
committed
security: define kernel_read_file hook
The kernel_read_file security hook is called prior to reading the file into memory. Changelog v4+: - export security_kernel_read_file() Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com> Acked-by: Kees Cook <keescook@chromium.org> Acked-by: Luis R. Rodriguez <mcgrof@kernel.org> Acked-by: Casey Schaufler <casey@schaufler-ca.com>
1 parent e40ba6d commit 39eeb4f

6 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎fs/exec.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,10 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
842842
if (!S_ISREG(file_inode(file)->i_mode) || max_size < 0)
843843
return -EINVAL;
844844

845+
ret = security_kernel_read_file(file, id);
846+
if (ret)
847+
return ret;
848+
845849
i_size = i_size_read(file_inode(file));
846850
if (max_size > 0 && i_size > max_size)
847851
return -EFBIG;

‎include/linux/ima.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern int ima_file_check(struct file *file, int mask, int opened);
1919
extern void ima_file_free(struct file *file);
2020
extern int ima_file_mmap(struct file *file, unsigned long prot);
2121
extern int ima_module_check(struct file *file);
22+
extern int ima_read_file(struct file *file, enum kernel_read_file_id id);
2223
extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
2324
enum kernel_read_file_id id);
2425

@@ -48,6 +49,11 @@ static inline int ima_module_check(struct file *file)
4849
return 0;
4950
}
5051

52+
static inline int ima_read_file(struct file *file, enum kernel_read_file_id id)
53+
{
54+
return 0;
55+
}
56+
5157
static inline int ima_post_read_file(struct file *file, void *buf, loff_t size,
5258
enum kernel_read_file_id id)
5359
{

‎include/linux/lsm_hooks.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,12 @@
552552
* the kernel module to load. If the module is being loaded from a blob,
553553
* this argument will be NULL.
554554
* Return 0 if permission is granted.
555+
* @kernel_read_file:
556+
* Read a file specified by userspace.
557+
* @file contains the file structure pointing to the file being read
558+
* by the kernel.
559+
* @id kernel read file identifier
560+
* Return 0 if permission is granted.
555561
* @kernel_post_read_file:
556562
* Read a file specified by userspace.
557563
* @file contains the file structure pointing to the file being read
@@ -1455,6 +1461,7 @@ union security_list_options {
14551461
int (*kernel_create_files_as)(struct cred *new, struct inode *inode);
14561462
int (*kernel_module_request)(char *kmod_name);
14571463
int (*kernel_module_from_file)(struct file *file);
1464+
int (*kernel_read_file)(struct file *file, enum kernel_read_file_id id);
14581465
int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size,
14591466
enum kernel_read_file_id id);
14601467
int (*task_fix_setuid)(struct cred *new, const struct cred *old,
@@ -1715,6 +1722,7 @@ struct security_hook_heads {
17151722
struct list_head cred_transfer;
17161723
struct list_head kernel_act_as;
17171724
struct list_head kernel_create_files_as;
1725+
struct list_head kernel_read_file;
17181726
struct list_head kernel_post_read_file;
17191727
struct list_head kernel_module_request;
17201728
struct list_head kernel_module_from_file;

‎include/linux/security.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ int security_kernel_act_as(struct cred *new, u32 secid);
302302
int security_kernel_create_files_as(struct cred *new, struct inode *inode);
303303
int security_kernel_module_request(char *kmod_name);
304304
int security_kernel_module_from_file(struct file *file);
305+
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id);
305306
int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
306307
enum kernel_read_file_id id);
307308
int security_task_fix_setuid(struct cred *new, const struct cred *old,
@@ -863,6 +864,12 @@ static inline int security_kernel_module_from_file(struct file *file)
863864
return 0;
864865
}
865866

867+
static inline int security_kernel_read_file(struct file *file,
868+
enum kernel_read_file_id id)
869+
{
870+
return 0;
871+
}
872+
866873
static inline int security_kernel_post_read_file(struct file *file,
867874
char *buf, loff_t size,
868875
enum kernel_read_file_id id)

‎security/integrity/ima/ima_main.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,22 @@ int ima_module_check(struct file *file)
337337
return process_measurement(file, NULL, 0, MAY_EXEC, MODULE_CHECK, 0);
338338
}
339339

340+
/**
341+
* ima_read_file - pre-measure/appraise hook decision based on policy
342+
* @file: pointer to the file to be measured/appraised/audit
343+
* @read_id: caller identifier
344+
*
345+
* Permit reading a file based on policy. The policy rules are written
346+
* in terms of the policy identifier. Appraising the integrity of
347+
* a file requires a file descriptor.
348+
*
349+
* For permission return 0, otherwise return -EACCES.
350+
*/
351+
int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
352+
{
353+
return 0;
354+
}
355+
340356
/**
341357
* ima_post_read_file - in memory collect/appraise/audit measurement
342358
* @file: pointer to the file to be measured/appraised/audit

‎security/security.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,17 @@ int security_kernel_module_from_file(struct file *file)
899899
return ima_module_check(file);
900900
}
901901

902+
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
903+
{
904+
int ret;
905+
906+
ret = call_int_hook(kernel_read_file, 0, file, id);
907+
if (ret)
908+
return ret;
909+
return ima_read_file(file, id);
910+
}
911+
EXPORT_SYMBOL_GPL(security_kernel_read_file);
912+
902913
int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
903914
enum kernel_read_file_id id)
904915
{
@@ -1696,6 +1707,8 @@ struct security_hook_heads security_hook_heads = {
16961707
LIST_HEAD_INIT(security_hook_heads.kernel_module_request),
16971708
.kernel_module_from_file =
16981709
LIST_HEAD_INIT(security_hook_heads.kernel_module_from_file),
1710+
.kernel_read_file =
1711+
LIST_HEAD_INIT(security_hook_heads.kernel_read_file),
16991712
.kernel_post_read_file =
17001713
LIST_HEAD_INIT(security_hook_heads.kernel_post_read_file),
17011714
.task_fix_setuid =

0 commit comments

Comments
 (0)