Skip to content

Commit c77b8cd

Browse files
Mimi ZoharJames Morris
authored andcommitted
module: replace the existing LSM hook in init_module
Both the init_module and finit_module syscalls call either directly or indirectly the security_kernel_read_file LSM hook. This patch replaces the direct call in init_module with a call to the new security_kernel_load_data hook and makes the corresponding changes in SELinux, LoadPin, and IMA. Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com> Cc: Jeff Vander Stoep <jeffv@google.com> Cc: Casey Schaufler <casey@schaufler-ca.com> Cc: Kees Cook <keescook@chromium.org> Acked-by: Jessica Yu <jeyu@kernel.org> Acked-by: Paul Moore <paul@paul-moore.com> Acked-by: Kees Cook <keescook@chromium.org> Signed-off-by: James Morris <james.morris@microsoft.com>
1 parent ef96837 commit c77b8cd

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

kernel/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2876,7 +2876,7 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,
28762876
if (info->len < sizeof(*(info->hdr)))
28772877
return -ENOEXEC;
28782878

2879-
err = security_kernel_read_file(NULL, READING_MODULE);
2879+
err = security_kernel_load_data(LOADING_MODULE);
28802880
if (err)
28812881
return err;
28822882

security/integrity/ima/ima_main.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,6 @@ void ima_post_path_mknod(struct dentry *dentry)
429429
*/
430430
int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
431431
{
432-
bool sig_enforce = is_module_sig_enforced();
433-
434-
if (!file && read_id == READING_MODULE) {
435-
if (!sig_enforce && (ima_appraise & IMA_APPRAISE_MODULES) &&
436-
(ima_appraise & IMA_APPRAISE_ENFORCE)) {
437-
pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
438-
return -EACCES; /* INTEGRITY_UNKNOWN */
439-
}
440-
return 0; /* We rely on module signature checking */
441-
}
442432
return 0;
443433
}
444434

@@ -479,9 +469,6 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,
479469
return 0;
480470
}
481471

482-
if (!file && read_id == READING_MODULE) /* MODULE_SIG_FORCE enabled */
483-
return 0;
484-
485472
/* permit signed certs */
486473
if (!file && read_id == READING_X509_CERTIFICATE)
487474
return 0;
@@ -510,6 +497,8 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,
510497
*/
511498
int ima_load_data(enum kernel_load_data_id id)
512499
{
500+
bool sig_enforce;
501+
513502
if ((ima_appraise & IMA_APPRAISE_ENFORCE) != IMA_APPRAISE_ENFORCE)
514503
return 0;
515504

@@ -525,6 +514,14 @@ int ima_load_data(enum kernel_load_data_id id)
525514
pr_err("Prevent firmware sysfs fallback loading.\n");
526515
return -EACCES; /* INTEGRITY_UNKNOWN */
527516
}
517+
break;
518+
case LOADING_MODULE:
519+
sig_enforce = is_module_sig_enforced();
520+
521+
if (!sig_enforce && (ima_appraise & IMA_APPRAISE_MODULES)) {
522+
pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
523+
return -EACCES; /* INTEGRITY_UNKNOWN */
524+
}
528525
default:
529526
break;
530527
}

security/loadpin/loadpin.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,15 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
173173
return 0;
174174
}
175175

176+
static int loadpin_load_data(enum kernel_load_data_id id)
177+
{
178+
return loadpin_read_file(NULL, (enum kernel_read_file_id) id);
179+
}
180+
176181
static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
177182
LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
178183
LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
184+
LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
179185
};
180186

181187
void __init loadpin_add_hooks(void)

security/selinux/hooks.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,6 +4073,20 @@ static int selinux_kernel_read_file(struct file *file,
40734073
return rc;
40744074
}
40754075

4076+
static int selinux_kernel_load_data(enum kernel_load_data_id id)
4077+
{
4078+
int rc = 0;
4079+
4080+
switch (id) {
4081+
case LOADING_MODULE:
4082+
rc = selinux_kernel_module_from_file(NULL);
4083+
default:
4084+
break;
4085+
}
4086+
4087+
return rc;
4088+
}
4089+
40764090
static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
40774091
{
40784092
return avc_has_perm(&selinux_state,
@@ -6972,6 +6986,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
69726986
LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
69736987
LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
69746988
LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6989+
LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
69756990
LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
69766991
LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
69776992
LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),

0 commit comments

Comments
 (0)