Skip to content

Commit e40ba6d

Browse files
author
Mimi Zohar
committed
firmware: replace call to fw_read_file_contents() with kernel version
Replace the fw_read_file_contents with kernel_file_read_from_path(). Although none of the upstreamed LSMs define a kernel_fw_from_file hook, IMA is called by the security function to prevent unsigned firmware from being loaded and to measure/appraise signed firmware, based on policy. Instead of reading the firmware twice, once for measuring/appraising the firmware and again for reading the firmware contents into memory, the kernel_post_read_file() security hook calculates the file hash based on the in memory file buffer. The firmware is read once. This patch removes the LSM kernel_fw_from_file() hook and security call. Changelog v4+: - revert dropped buf->size assignment - reported by Sergey Senozhatsky v3: - remove kernel_fw_from_file hook - use kernel_file_read_from_path() - requested by Luis v2: - reordered and squashed firmware patches - fix MAX firmware size (Kees Cook) 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>
1 parent 09596b9 commit e40ba6d

7 files changed

Lines changed: 21 additions & 90 deletions

File tree

‎drivers/base/firmware_class.c‎

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/sched.h>
2424
#include <linux/file.h>
2525
#include <linux/list.h>
26+
#include <linux/fs.h>
2627
#include <linux/async.h>
2728
#include <linux/pm.h>
2829
#include <linux/suspend.h>
@@ -291,37 +292,6 @@ static const char * const fw_path[] = {
291292
module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
292293
MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
293294

294-
static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
295-
{
296-
int size;
297-
char *buf;
298-
int rc;
299-
300-
if (!S_ISREG(file_inode(file)->i_mode))
301-
return -EINVAL;
302-
size = i_size_read(file_inode(file));
303-
if (size <= 0)
304-
return -EINVAL;
305-
buf = vmalloc(size);
306-
if (!buf)
307-
return -ENOMEM;
308-
rc = kernel_read(file, 0, buf, size);
309-
if (rc != size) {
310-
if (rc > 0)
311-
rc = -EIO;
312-
goto fail;
313-
}
314-
rc = security_kernel_fw_from_file(file, buf, size);
315-
if (rc)
316-
goto fail;
317-
fw_buf->data = buf;
318-
fw_buf->size = size;
319-
return 0;
320-
fail:
321-
vfree(buf);
322-
return rc;
323-
}
324-
325295
static void fw_finish_direct_load(struct device *device,
326296
struct firmware_buf *buf)
327297
{
@@ -334,6 +304,7 @@ static void fw_finish_direct_load(struct device *device,
334304
static int fw_get_filesystem_firmware(struct device *device,
335305
struct firmware_buf *buf)
336306
{
307+
loff_t size;
337308
int i, len;
338309
int rc = -ENOENT;
339310
char *path;
@@ -343,8 +314,6 @@ static int fw_get_filesystem_firmware(struct device *device,
343314
return -ENOMEM;
344315

345316
for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
346-
struct file *file;
347-
348317
/* skip the unset customized path */
349318
if (!fw_path[i][0])
350319
continue;
@@ -356,18 +325,16 @@ static int fw_get_filesystem_firmware(struct device *device,
356325
break;
357326
}
358327

359-
file = filp_open(path, O_RDONLY, 0);
360-
if (IS_ERR(file))
361-
continue;
362-
rc = fw_read_file_contents(file, buf);
363-
fput(file);
328+
buf->size = 0;
329+
rc = kernel_read_file_from_path(path, &buf->data, &size,
330+
INT_MAX, READING_FIRMWARE);
364331
if (rc) {
365332
dev_warn(device, "loading %s failed with error %d\n",
366333
path, rc);
367334
continue;
368335
}
369-
dev_dbg(device, "direct-loading %s\n",
370-
buf->fw_id);
336+
dev_dbg(device, "direct-loading %s\n", buf->fw_id);
337+
buf->size = size;
371338
fw_finish_direct_load(device, buf);
372339
break;
373340
}
@@ -689,8 +656,9 @@ static ssize_t firmware_loading_store(struct device *dev,
689656
dev_err(dev, "%s: map pages failed\n",
690657
__func__);
691658
else
692-
rc = security_kernel_fw_from_file(NULL,
693-
fw_buf->data, fw_buf->size);
659+
rc = security_kernel_post_read_file(NULL,
660+
fw_buf->data, fw_buf->size,
661+
READING_FIRMWARE);
694662

695663
/*
696664
* Same logic as fw_load_abort, only the DONE bit

‎include/linux/fs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,7 @@ static inline void i_readcount_inc(struct inode *inode)
25772577
extern int do_pipe_flags(int *, int);
25782578

25792579
enum kernel_read_file_id {
2580+
READING_FIRMWARE = 1,
25802581
READING_MAX_ID
25812582
};
25822583

‎include/linux/ima.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ 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_fw_from_file(struct file *file, char *buf, size_t size);
2322
extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
2423
enum kernel_read_file_id id);
2524

@@ -49,11 +48,6 @@ static inline int ima_module_check(struct file *file)
4948
return 0;
5049
}
5150

52-
static inline int ima_fw_from_file(struct file *file, char *buf, size_t size)
53-
{
54-
return 0;
55-
}
56-
5751
static inline int ima_post_read_file(struct file *file, void *buf, loff_t size,
5852
enum kernel_read_file_id id)
5953
{

‎include/linux/lsm_hooks.h‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,6 @@
541541
* @inode points to the inode to use as a reference.
542542
* The current task must be the one that nominated @inode.
543543
* Return 0 if successful.
544-
* @kernel_fw_from_file:
545-
* Load firmware from userspace (not called for built-in firmware).
546-
* @file contains the file structure pointing to the file containing
547-
* the firmware to load. This argument will be NULL if the firmware
548-
* was loaded via the uevent-triggered blob-based interface exposed
549-
* by CONFIG_FW_LOADER_USER_HELPER.
550-
* @buf pointer to buffer containing firmware contents.
551-
* @size length of the firmware contents.
552-
* Return 0 if permission is granted.
553544
* @kernel_module_request:
554545
* Ability to trigger the kernel to automatically upcall to userspace for
555546
* userspace to load a kernel module with the given name.
@@ -1462,7 +1453,6 @@ union security_list_options {
14621453
void (*cred_transfer)(struct cred *new, const struct cred *old);
14631454
int (*kernel_act_as)(struct cred *new, u32 secid);
14641455
int (*kernel_create_files_as)(struct cred *new, struct inode *inode);
1465-
int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
14661456
int (*kernel_module_request)(char *kmod_name);
14671457
int (*kernel_module_from_file)(struct file *file);
14681458
int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size,
@@ -1725,7 +1715,6 @@ struct security_hook_heads {
17251715
struct list_head cred_transfer;
17261716
struct list_head kernel_act_as;
17271717
struct list_head kernel_create_files_as;
1728-
struct list_head kernel_fw_from_file;
17291718
struct list_head kernel_post_read_file;
17301719
struct list_head kernel_module_request;
17311720
struct list_head kernel_module_from_file;

‎include/linux/security.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp);
300300
void security_transfer_creds(struct cred *new, const struct cred *old);
301301
int security_kernel_act_as(struct cred *new, u32 secid);
302302
int security_kernel_create_files_as(struct cred *new, struct inode *inode);
303-
int security_kernel_fw_from_file(struct file *file, char *buf, size_t size);
304303
int security_kernel_module_request(char *kmod_name);
305304
int security_kernel_module_from_file(struct file *file);
306305
int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
@@ -854,12 +853,6 @@ static inline int security_kernel_create_files_as(struct cred *cred,
854853
return 0;
855854
}
856855

857-
static inline int security_kernel_fw_from_file(struct file *file,
858-
char *buf, size_t size)
859-
{
860-
return 0;
861-
}
862-
863856
static inline int security_kernel_module_request(char *kmod_name)
864857
{
865858
return 0;

‎security/integrity/ima/ima_main.c‎

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

340-
int ima_fw_from_file(struct file *file, char *buf, size_t size)
341-
{
342-
if (!file) {
343-
if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
344-
(ima_appraise & IMA_APPRAISE_ENFORCE))
345-
return -EACCES; /* INTEGRITY_UNKNOWN */
346-
return 0;
347-
}
348-
return process_measurement(file, NULL, 0, MAY_EXEC, FIRMWARE_CHECK, 0);
349-
}
350-
351340
/**
352341
* ima_post_read_file - in memory collect/appraise/audit measurement
353342
* @file: pointer to the file to be measured/appraised/audit
@@ -366,12 +355,22 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,
366355
{
367356
enum ima_hooks func = FILE_CHECK;
368357

358+
if (!file && read_id == READING_FIRMWARE) {
359+
if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
360+
(ima_appraise & IMA_APPRAISE_ENFORCE))
361+
return -EACCES; /* INTEGRITY_UNKNOWN */
362+
return 0;
363+
}
364+
369365
if (!file || !buf || size == 0) { /* should never happen */
370366
if (ima_appraise & IMA_APPRAISE_ENFORCE)
371367
return -EACCES;
372368
return 0;
373369
}
374370

371+
if (read_id == READING_FIRMWARE)
372+
func = FIRMWARE_CHECK;
373+
375374
return process_measurement(file, buf, size, MAY_READ, func, 0);
376375
}
377376

‎security/security.c‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -884,17 +884,6 @@ int security_kernel_create_files_as(struct cred *new, struct inode *inode)
884884
return call_int_hook(kernel_create_files_as, 0, new, inode);
885885
}
886886

887-
int security_kernel_fw_from_file(struct file *file, char *buf, size_t size)
888-
{
889-
int ret;
890-
891-
ret = call_int_hook(kernel_fw_from_file, 0, file, buf, size);
892-
if (ret)
893-
return ret;
894-
return ima_fw_from_file(file, buf, size);
895-
}
896-
EXPORT_SYMBOL_GPL(security_kernel_fw_from_file);
897-
898887
int security_kernel_module_request(char *kmod_name)
899888
{
900889
return call_int_hook(kernel_module_request, 0, kmod_name);
@@ -1703,8 +1692,6 @@ struct security_hook_heads security_hook_heads = {
17031692
LIST_HEAD_INIT(security_hook_heads.kernel_act_as),
17041693
.kernel_create_files_as =
17051694
LIST_HEAD_INIT(security_hook_heads.kernel_create_files_as),
1706-
.kernel_fw_from_file =
1707-
LIST_HEAD_INIT(security_hook_heads.kernel_fw_from_file),
17081695
.kernel_module_request =
17091696
LIST_HEAD_INIT(security_hook_heads.kernel_module_request),
17101697
.kernel_module_from_file =

0 commit comments

Comments
 (0)