Skip to content

Commit a1db742

Browse files
author
Mimi Zohar
committed
module: replace copy_module_from_fd with kernel version
Replace copy_module_from_fd() with kernel_read_file_from_fd(). Although none of the upstreamed LSMs define a kernel_module_from_file hook, IMA is called, based on policy, to prevent unsigned kernel modules from being loaded by the original kernel module syscall and to measure/appraise signed kernel modules. The security function security_kernel_module_from_file() was called prior to reading a kernel module. Preventing unsigned kernel modules from being loaded by the original kernel module syscall remains on the pre-read kernel_read_file() security hook. Instead of reading the kernel module twice, once for measuring/appraising and again for loading the kernel module, the signature validation is moved to the kernel_post_read_file() security hook. This patch removes the security_kernel_module_from_file() hook and security call. 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> Cc: Rusty Russell <rusty@rustcorp.com.au>
1 parent b844f0e commit a1db742

7 files changed

Lines changed: 22 additions & 112 deletions

File tree

‎include/linux/fs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,7 @@ extern int do_pipe_flags(int *, int);
25782578

25792579
enum kernel_read_file_id {
25802580
READING_FIRMWARE = 1,
2581+
READING_MODULE,
25812582
READING_MAX_ID
25822583
};
25832584

‎include/linux/ima.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extern int ima_bprm_check(struct linux_binprm *bprm);
1818
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);
21-
extern int ima_module_check(struct file *file);
2221
extern int ima_read_file(struct file *file, enum kernel_read_file_id id);
2322
extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
2423
enum kernel_read_file_id id);
@@ -44,11 +43,6 @@ static inline int ima_file_mmap(struct file *file, unsigned long prot)
4443
return 0;
4544
}
4645

47-
static inline int ima_module_check(struct file *file)
48-
{
49-
return 0;
50-
}
51-
5246
static inline int ima_read_file(struct file *file, enum kernel_read_file_id id)
5347
{
5448
return 0;

‎include/linux/lsm_hooks.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,6 @@
546546
* userspace to load a kernel module with the given name.
547547
* @kmod_name name of the module requested by the kernel
548548
* Return 0 if successful.
549-
* @kernel_module_from_file:
550-
* Load a kernel module from userspace.
551-
* @file contains the file structure pointing to the file containing
552-
* the kernel module to load. If the module is being loaded from a blob,
553-
* this argument will be NULL.
554-
* Return 0 if permission is granted.
555549
* @kernel_read_file:
556550
* Read a file specified by userspace.
557551
* @file contains the file structure pointing to the file being read
@@ -1725,7 +1719,6 @@ struct security_hook_heads {
17251719
struct list_head kernel_read_file;
17261720
struct list_head kernel_post_read_file;
17271721
struct list_head kernel_module_request;
1728-
struct list_head kernel_module_from_file;
17291722
struct list_head task_fix_setuid;
17301723
struct list_head task_setpgid;
17311724
struct list_head task_getpgid;

‎include/linux/security.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,6 @@ static inline int security_kernel_module_request(char *kmod_name)
859859
return 0;
860860
}
861861

862-
static inline int security_kernel_module_from_file(struct file *file)
863-
{
864-
return 0;
865-
}
866-
867862
static inline int security_kernel_read_file(struct file *file,
868863
enum kernel_read_file_id id)
869864
{

‎kernel/module.c‎

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,7 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,
26542654
if (info->len < sizeof(*(info->hdr)))
26552655
return -ENOEXEC;
26562656

2657-
err = security_kernel_module_from_file(NULL);
2657+
err = security_kernel_read_file(NULL, READING_MODULE);
26582658
if (err)
26592659
return err;
26602660

@@ -2672,63 +2672,6 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,
26722672
return 0;
26732673
}
26742674

2675-
/* Sets info->hdr and info->len. */
2676-
static int copy_module_from_fd(int fd, struct load_info *info)
2677-
{
2678-
struct fd f = fdget(fd);
2679-
int err;
2680-
struct kstat stat;
2681-
loff_t pos;
2682-
ssize_t bytes = 0;
2683-
2684-
if (!f.file)
2685-
return -ENOEXEC;
2686-
2687-
err = security_kernel_module_from_file(f.file);
2688-
if (err)
2689-
goto out;
2690-
2691-
err = vfs_getattr(&f.file->f_path, &stat);
2692-
if (err)
2693-
goto out;
2694-
2695-
if (stat.size > INT_MAX) {
2696-
err = -EFBIG;
2697-
goto out;
2698-
}
2699-
2700-
/* Don't hand 0 to vmalloc, it whines. */
2701-
if (stat.size == 0) {
2702-
err = -EINVAL;
2703-
goto out;
2704-
}
2705-
2706-
info->hdr = vmalloc(stat.size);
2707-
if (!info->hdr) {
2708-
err = -ENOMEM;
2709-
goto out;
2710-
}
2711-
2712-
pos = 0;
2713-
while (pos < stat.size) {
2714-
bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
2715-
stat.size - pos);
2716-
if (bytes < 0) {
2717-
vfree(info->hdr);
2718-
err = bytes;
2719-
goto out;
2720-
}
2721-
if (bytes == 0)
2722-
break;
2723-
pos += bytes;
2724-
}
2725-
info->len = pos;
2726-
2727-
out:
2728-
fdput(f);
2729-
return err;
2730-
}
2731-
27322675
static void free_copy(struct load_info *info)
27332676
{
27342677
vfree(info->hdr);
@@ -3589,8 +3532,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
35893532

35903533
SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
35913534
{
3592-
int err;
35933535
struct load_info info = { };
3536+
loff_t size;
3537+
void *hdr;
3538+
int err;
35943539

35953540
err = may_init_module();
35963541
if (err)
@@ -3602,9 +3547,12 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
36023547
|MODULE_INIT_IGNORE_VERMAGIC))
36033548
return -EINVAL;
36043549

3605-
err = copy_module_from_fd(fd, &info);
3550+
err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3551+
READING_MODULE);
36063552
if (err)
36073553
return err;
3554+
info.hdr = hdr;
3555+
info.len = size;
36083556

36093557
return load_module(&info, uargs, flags);
36103558
}

‎security/integrity/ima/ima_main.c‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -315,28 +315,6 @@ int ima_file_check(struct file *file, int mask, int opened)
315315
}
316316
EXPORT_SYMBOL_GPL(ima_file_check);
317317

318-
/**
319-
* ima_module_check - based on policy, collect/store/appraise measurement.
320-
* @file: pointer to the file to be measured/appraised
321-
*
322-
* Measure/appraise kernel modules based on policy.
323-
*
324-
* On success return 0. On integrity appraisal error, assuming the file
325-
* is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
326-
*/
327-
int ima_module_check(struct file *file)
328-
{
329-
if (!file) {
330-
#ifndef CONFIG_MODULE_SIG_FORCE
331-
if ((ima_appraise & IMA_APPRAISE_MODULES) &&
332-
(ima_appraise & IMA_APPRAISE_ENFORCE))
333-
return -EACCES; /* INTEGRITY_UNKNOWN */
334-
#endif
335-
return 0; /* We rely on module signature checking */
336-
}
337-
return process_measurement(file, NULL, 0, MAY_EXEC, MODULE_CHECK, 0);
338-
}
339-
340318
/**
341319
* ima_read_file - pre-measure/appraise hook decision based on policy
342320
* @file: pointer to the file to be measured/appraised/audit
@@ -350,6 +328,14 @@ int ima_module_check(struct file *file)
350328
*/
351329
int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
352330
{
331+
if (!file && read_id == READING_MODULE) {
332+
#ifndef CONFIG_MODULE_SIG_FORCE
333+
if ((ima_appraise & IMA_APPRAISE_MODULES) &&
334+
(ima_appraise & IMA_APPRAISE_ENFORCE))
335+
return -EACCES; /* INTEGRITY_UNKNOWN */
336+
#endif
337+
return 0; /* We rely on module signature checking */
338+
}
353339
return 0;
354340
}
355341

@@ -378,6 +364,9 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,
378364
return 0;
379365
}
380366

367+
if (!file && read_id == READING_MODULE) /* MODULE_SIG_FORCE enabled */
368+
return 0;
369+
381370
if (!file || !buf || size == 0) { /* should never happen */
382371
if (ima_appraise & IMA_APPRAISE_ENFORCE)
383372
return -EACCES;
@@ -386,6 +375,8 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,
386375

387376
if (read_id == READING_FIRMWARE)
388377
func = FIRMWARE_CHECK;
378+
else if (read_id == READING_MODULE)
379+
func = MODULE_CHECK;
389380

390381
return process_measurement(file, buf, size, MAY_READ, func, 0);
391382
}

‎security/security.c‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -889,16 +889,6 @@ int security_kernel_module_request(char *kmod_name)
889889
return call_int_hook(kernel_module_request, 0, kmod_name);
890890
}
891891

892-
int security_kernel_module_from_file(struct file *file)
893-
{
894-
int ret;
895-
896-
ret = call_int_hook(kernel_module_from_file, 0, file);
897-
if (ret)
898-
return ret;
899-
return ima_module_check(file);
900-
}
901-
902892
int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
903893
{
904894
int ret;
@@ -1705,8 +1695,6 @@ struct security_hook_heads security_hook_heads = {
17051695
LIST_HEAD_INIT(security_hook_heads.kernel_create_files_as),
17061696
.kernel_module_request =
17071697
LIST_HEAD_INIT(security_hook_heads.kernel_module_request),
1708-
.kernel_module_from_file =
1709-
LIST_HEAD_INIT(security_hook_heads.kernel_module_from_file),
17101698
.kernel_read_file =
17111699
LIST_HEAD_INIT(security_hook_heads.kernel_read_file),
17121700
.kernel_post_read_file =

0 commit comments

Comments
 (0)