Introduce partial kernel_read_file() support
Subsystems (15)
v4 cover Introduce partial kernel_read_file() support
v4:
- add more reviews (mimi, luis)
- adjusted comment (mimi)
- fixed build error when not building firmware tests (0day, sfr)
- fixed needless .xz read (tiwai)
- rebased to driver-core-next
v3: https://lore.kernel.org/lkml/[email protected]/
v2: lost to the ether
v1: https://lore.kernel.org/lkml/[email protected]/
Hi,
Here's my tree for adding partial read support in kernel_read_file(), which fixes a number of issues along the way. It's got Scott's firmware and IMA patches ported and everything tests cleanly for me (even with CONFIG_IMA_APPRAISE=y), and now appears to pass 0day. :)
The intention is for this to go via Greg's tree since Scott's driver code will depend on it.
Thanks,
-Kees
Kees Cook (13):
test_firmware: Test platform fw loading on non-EFI systems fs/kernel_read_file: Remove FIRMWARE_PREALLOC_BUFFER enum fs/kernel_read_file: Remove FIRMWARE_EFI_EMBEDDED enum fs/kernel_read_file: Split into separate source file fs/kernel_read_file: Remove redundant size argument fs/kernel_read_file: Switch buffer size arg to size_t fs/kernel_read_file: Add file_size output argument
LSM: Introduce kernel_post_load_data() hook
firmware_loader: Use security_post_load_data()
module: Call security_kernel_post_load_data()
LSM: Add "contents" flag to kernel_read_file hook fs/kernel_file_read: Add "offset" arg for partial reads
firmware: Store opt_flags in fw_priv
Scott Branden (4):
fs/kernel_read_file: Split into separate include file
IMA: Add support for file reads without contents
firmware: Add request_partial_firmware_into_buf()
test_firmware: Test partial read support
drivers/base/firmware_loader/fallback.c | 19 +- drivers/base/firmware_loader/fallback.h | 5 +- .../base/firmware_loader/fallback_platform.c | 11 +- drivers/base/firmware_loader/firmware.h | 7 +- drivers/base/firmware_loader/main.c | 135 ++++++++++--- drivers/firmware/efi/embedded-firmware.c | 21 +- drivers/firmware/efi/embedded-firmware.h | 21 ++ fs/Makefile | 3 +- fs/exec.c | 132 +----------- fs/kernel_read_file.c | 189 ++++++++++++++++++ include/linux/efi_embedded_fw.h | 13 -- include/linux/firmware.h | 12 ++ include/linux/fs.h | 39 ---- include/linux/ima.h | 19 +- include/linux/kernel_read_file.h | 55 +++++ include/linux/lsm_hook_defs.h | 6 +- include/linux/lsm_hooks.h | 12 ++ include/linux/security.h | 19 +- kernel/kexec.c | 2 +- kernel/kexec_file.c | 19 +- kernel/module.c | 24 ++- lib/test_firmware.c | 159 +++++++++++++-- security/integrity/digsig.c | 8 +- security/integrity/ima/ima_fs.c | 10 +- security/integrity/ima/ima_main.c | 70 +++++-- security/integrity/ima/ima_policy.c | 1 + security/loadpin/loadpin.c | 17 +- security/security.c | 26 ++- security/selinux/hooks.c | 8 +- .../selftests/firmware/fw_filesystem.sh | 91 +++++++++ 30 files changed, 839 insertions(+), 314 deletions(-) create mode 100644 drivers/firmware/efi/embedded-firmware.h create mode 100644 fs/kernel_read_file.c create mode 100644 include/linux/kernel_read_file.h
Signature · 1 line
2.25.1
PATCH v4 1/17 test_firmware: Test platform fw loading on non-EFI systems
On non-EFI systems, it wasn't possible to test the platform firmware loader because it will have never set "checked_fw" during __init. Instead, allow the test code to override this check. Additionally split the declarations into a private header file so it there is greater enforcement of the symbol visibility.
--- drivers/firmware/efi/embedded-firmware.c | 21 ++++++++++++++++----- drivers/firmware/efi/embedded-firmware.h | 21 +++++++++++++++++++++ include/linux/efi_embedded_fw.h | 13 ------------- lib/test_firmware.c | 5 +++++ 4 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 drivers/firmware/efi/embedded-firmware.h
drivers/firmware/efi/embedded-firmware.c +16 -5
@@ -14,11 +14,22 @@#include <linux/vmalloc.h> #include <crypto/sha.h> +#include "embedded-firmware.h" + +#ifdef CONFIG_TEST_FIRMWARE +# define EFI_EMBEDDED_FW_VISIBILITY +#else +# define EFI_EMBEDDED_FW_VISIBILITY static +#endif + +EFI_EMBEDDED_FW_VISIBILITY LIST_HEAD (efi_embedded_fw_list ); +EFI_EMBEDDED_FW_VISIBILITY bool efi_embedded_fw_checked; +/* Exported for use by lib/test_firmware.c only */ -LIST_HEAD (efi_embedded_fw_list); +#ifdef CONFIG_TEST_FIRMWARE EXPORT_SYMBOL_GPL (efi_embedded_fw_list); - -static bool checked_for_fw; +EXPORT_SYMBOL_GPL (efi_embedded_fw_checked); +#endif static const struct dmi_system_id *const embedded_fw_table[] = { #ifdef CONFIG_TOUCHSCREEN_DMI
@@ -119,14 +130,14 @@ void __init efi_check_for_embedded_firmwares(void) } } - checked_for_fw = true; + efi_embedded_fw_checked = true; }int efi_get_embedded_fw (const char *name,const u8 **data,size_t *size) {struct efi_embedded_fw *iter, *fw =NULL ; -if (!checked_for_fw) { +if (!efi_embedded_fw_checked) {pr_warn ("Warning %s called while we did not check for embedded fw\n" , __func__);return -ENOENT ;
drivers/firmware/efi/embedded-firmware.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _EFI_EMBEDDED_FW_INTERNAL_H_ +#define _EFI_EMBEDDED_FW_INTERNAL_H_ + +/* +* This struct and efi_embedded_fw_list are private to the efi-embedded fw +* implementation they only in separate header for use by lib/test_firmware.c. +*/ +struct efi_embedded_fw { +struct list_head list; +const char *name; +const u8 *data; +size_t length; +}; + +#ifdef CONFIG_TEST_FIRMWARE +extern struct list_head efi_embedded_fw_list; +extern bool efi_embedded_fw_checked; +#endif + +#endif /* _EFI_EMBEDDED_FW_INTERNAL_H_ */
include/linux/efi_embedded_fw.h +0 -13
@@ -7,19 +7,6 @@#define EFI_EMBEDDED_FW_PREFIX_LEN 8 -/* -* This struct and efi_embedded_fw_list are private to the efi-embedded fw -* implementation they are in this header for use by lib/test_firmware.c only! -*/ -struct efi_embedded_fw { -struct list_head list; -const char *name; -const u8 *data; -size_t length; -}; - -extern struct list_head efi_embedded_fw_list; -/** * struct efi_embedded_fw_desc - This struct is used by the EFI embedded-fw * code to search for embedded firmwares.
lib/test_firmware.c +5 -0
@@ -489,6 +489,7 @@ static ssize_t trigger_request_store(struct device *dev,static DEVICE_ATTR_WO (trigger_request);#ifdef CONFIG_EFI_EMBEDDED_FIRMWARE +#include "../drivers/firmware/efi/embedded-firmware.h" static ssize_t trigger_request_platform_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count)
@@ -501,6 +502,7 @@ static ssize_t trigger_request_platform_store(struct device *dev, };struct efi_embedded_fw efi_embedded_fw;const struct firmware *firmware =NULL ; +bool saved_efi_embedded_fw_checked;char *name;int rc;
@@ -513,6 +515,8 @@ static ssize_t trigger_request_platform_store(struct device *dev, efi_embedded_fw.data = (void *)test_data; efi_embedded_fw.length =sizeof (test_data);list_add (&efi_embedded_fw.list, &efi_embedded_fw_list); + saved_efi_embedded_fw_checked = efi_embedded_fw_checked; + efi_embedded_fw_checked = true;pr_info ("loading '%s'\n" , name); rc =firmware_request_platform (&firmware, name, dev);
@@ -530,6 +534,7 @@ static ssize_t trigger_request_platform_store(struct device *dev, rc = count; out: + efi_embedded_fw_checked = saved_efi_embedded_fw_checked;release_firmware (firmware);list_del (&efi_embedded_fw.list);kfree (name);
Signature · 1 line
2.25.1
PATCH v4 2/17 fs/kernel_read_file: Remove FIRMWARE_PREALLOC_BUFFER enum
In reply to Kees CookFIRMWARE_PREALLOC_BUFFER is a "how", not a "what", and confuses the LSMs that are interested in filtering between types of things. The "how" should be an internal detail made uninteresting to the LSMs.
---
To aid in backporting, this change is made before moving kernel_read_file() to separate header/source files.
--- drivers/base/firmware_loader/main.c | 5 ++--- fs/exec.c | 7 ++++--- include/linux/fs.h | 2 +- kernel/module.c | 2 +- security/integrity/digsig.c | 2 +- security/integrity/ima/ima_fs.c | 2 +- security/integrity/ima/ima_main.c | 6 ++---- 7 files changed, 12 insertions(+), 14 deletions(-)
drivers/base/firmware_loader/main.c +2 -3
@@ -465,14 +465,12 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,int i, len;int rc = -ENOENT ;char *path; -enum kernel_read_file_id id =READING_FIRMWARE ;size_t msize =INT_MAX ;void *buffer =NULL ;/* Already populated data member means we're loading into a buffer */ if (!decompress && fw_priv->data) { buffer = fw_priv->data; - id =READING_FIRMWARE_PREALLOC_BUFFER ; msize = fw_priv->allocated_size; }
@@ -496,7 +494,8 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,/* load firmware files from the mount namespace of init */ rc =kernel_read_file_from_path_initns (path, &buffer, - &size, msize, id); + &size, msize, +READING_FIRMWARE );if (rc) {if (rc != -ENOENT ) dev_warn(device, "loading %s failed with error %d\n",
fs/exec.c +4 -3
@@ -927,6 +927,7 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, {loff_t i_size, pos;ssize_t bytes =0 ; +void *allocated =NULL ;int ret;if (!S_ISREG (file_inode (file)->i_mode) || max_size <0 )
@@ -950,8 +951,8 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, goto out; } -if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) - *buf =vmalloc (i_size); +if (!*buf) + *buf = allocated =vmalloc (i_size);if (!*buf) { ret = -ENOMEM ; goto out;
@@ -980,7 +981,7 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, out_free:if (ret <0 ) { -if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) { +if (allocated) {vfree (*buf); *buf =NULL ; }
include/linux/fs.h +1 -1
@@ -2993,10 +2993,10 @@ static inline void i_readcount_inc(struct inode *inode)#endif extern int do_pipe_flags (int *,int ); +/* This is a list of *what* is being read, not *how*. */ #define __kernel_read_file_id (id) \ id(UNKNOWN, unknown) \ id(FIRMWARE, firmware) \ - id(FIRMWARE_PREALLOC_BUFFER, firmware) \ id(FIRMWARE_EFI_EMBEDDED, firmware) \ id(MODULE, kernel-module) \ id(KEXEC_IMAGE, kexec-image) \
kernel/module.c +1 -1
@@ -3991,7 +3991,7 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) {struct load_info info = { };loff_t size; -void *hdr; +void *hdr =NULL ;int err; err =may_init_module ();
security/integrity/digsig.c +1 -1
@@ -169,7 +169,7 @@ int __init integrity_add_key(const unsigned int id, const void *data,int __init integrity_load_x509 (const unsigned int id,const char *path) { -void *data; +void *data =NULL ;loff_t size;int rc;key_perm_t perm;
security/integrity/ima/ima_fs.c +1 -1
@@ -272,7 +272,7 @@ static const struct file_operations ima_ascii_measurements_ops = {static ssize_t ima_read_policy (char *path) { -void *data; +void *data =NULL ;char *datap;loff_t size;int rc, pathlen =strlen (path);
security/integrity/ima/ima_main.c +2 -4
@@ -611,19 +611,17 @@ void ima_post_path_mknod(struct dentry *dentry)int ima_read_file (struct file *file,enum kernel_read_file_id read_id) {/* -* READING_FIRMWARE_PREALLOC_BUFFER -* * Do devices using pre-allocated memory run the risk of the * firmware being accessible to the device prior to the completion * of IMA's signature verification any more than when using two -* buffers? +* buffers? It may be desirable to include the buffer address +* in this API and walk all the dma_map_single() mappings to check. */ return 0 ; }const int read_idmap[READING_MAX_ID ] = { [READING_FIRMWARE ] =FIRMWARE_CHECK , - [READING_FIRMWARE_PREALLOC_BUFFER ] =FIRMWARE_CHECK , [READING_MODULE ] =MODULE_CHECK , [READING_KEXEC_IMAGE ] =KEXEC_KERNEL_CHECK , [READING_KEXEC_INITRAMFS ] =KEXEC_INITRAMFS_CHECK ,
Signature · 1 line
2.25.1
PATCH v4 3/17 fs/kernel_read_file: Remove FIRMWARE_EFI_EMBEDDED enum
In reply to Kees CookThe "FIRMWARE_EFI_EMBEDDED" enum is a "where", not a "what". It should not be distinguished separately from just "FIRMWARE", as this confuses the LSMs about what is being loaded. Additionally, there was no actual validation of the firmware contents happening.
---
To aid in backporting, this change is made before moving kernel_read_file() to separate header/source files.
--- drivers/base/firmware_loader/fallback_platform.c | 2 +- include/linux/fs.h | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/base/firmware_loader/fallback_platform.c +1 -1
@@ -17,7 +17,7 @@ int firmware_fallback_platform(struct fw_priv *fw_priv, u32 opt_flags)if (!(opt_flags &FW_OPT_FALLBACK_PLATFORM ))return -ENOENT ; - rc =security_kernel_load_data (LOADING_FIRMWARE_EFI_EMBEDDED ); + rc =security_kernel_load_data (LOADING_FIRMWARE );if (rc)return rc;
include/linux/fs.h +1 -2
@@ -2993,11 +2993,10 @@ static inline void i_readcount_inc(struct inode *inode)#endif extern int do_pipe_flags (int *,int ); -/* This is a list of *what* is being read, not *how*. */ +/* This is a list of *what* is being read, not *how* nor *where*. */ #define __kernel_read_file_id (id) \ id(UNKNOWN, unknown) \ id(FIRMWARE, firmware) \ - id(FIRMWARE_EFI_EMBEDDED, firmware) \ id(MODULE, kernel-module) \ id(KEXEC_IMAGE, kexec-image) \ id(KEXEC_INITRAMFS, kexec-initramfs) \
Signature · 1 line
2.25.1
PATCH v4 4/17 fs/kernel_read_file: Split into separate include file
In reply to Kees CookFrom: Scott Branden <[email protected]>
Move kernel_read_file* out of linux/fs.h to its own linux/kernel_read_file.h include file. That header gets pulled in just about everywhere and doesn't really need functions not related to the general fs interface.
--- drivers/base/firmware_loader/main.c | 1 + fs/exec.c | 1 + include/linux/fs.h | 38 --------------------- include/linux/ima.h | 1 + include/linux/kernel_read_file.h | 51 +++++++++++++++++++++++++++++ include/linux/security.h | 1 + kernel/kexec_file.c | 1 + kernel/module.c | 1 + security/integrity/digsig.c | 1 + security/integrity/ima/ima_fs.c | 1 + security/integrity/ima/ima_main.c | 1 + security/integrity/ima/ima_policy.c | 1 + security/loadpin/loadpin.c | 1 + security/security.c | 1 + security/selinux/hooks.c | 1 + 15 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 include/linux/kernel_read_file.h
drivers/base/firmware_loader/main.c +1 -0
@@ -12,6 +12,7 @@#include <linux/capability.h> #include <linux/device.h> +#include <linux/kernel_read_file.h> #include <linux/module.h> #include <linux/init.h> #include <linux/timer.h>
fs/exec.c +1 -0
@@ -23,6 +23,7 @@* formats. */ +#include <linux/kernel_read_file.h> #include <linux/slab.h> #include <linux/file.h> #include <linux/fdtable.h>
include/linux/fs.h +0 -38
@@ -2993,44 +2993,6 @@ static inline void i_readcount_inc(struct inode *inode)#endif extern int do_pipe_flags (int *,int ); -/* This is a list of *what* is being read, not *how* nor *where*. */ -#define __kernel_read_file_id (id) \ - id(UNKNOWN, unknown) \ - id(FIRMWARE, firmware) \ - id(MODULE, kernel-module) \ - id(KEXEC_IMAGE, kexec-image) \ - id(KEXEC_INITRAMFS, kexec-initramfs) \ - id(POLICY, security-policy) \ - id(X509_CERTIFICATE, x509-certificate) \ - id(MAX_ID, ) - -#define __fid_enumify (ENUM , dummy) READING_ ## ENUM, -#define __fid_stringify (dummy, str) #str, - -enum kernel_read_file_id { - __kernel_read_file_id(__fid_enumify) -}; - -static const char *const kernel_read_file_str[] = { -__kernel_read_file_id (__fid_stringify) -}; - -static inline const char *kernel_read_file_id_str (enum kernel_read_file_id id) -{ -if ((unsigned )id >=READING_MAX_ID ) -return kernel_read_file_str[READING_UNKNOWN ]; - -return kernel_read_file_str[id]; -} - -extern int kernel_read_file (struct file *,void **,loff_t *,loff_t , -enum kernel_read_file_id ); -extern int kernel_read_file_from_path (const char *,void **,loff_t *,loff_t , -enum kernel_read_file_id ); -extern int kernel_read_file_from_path_initns (const char *,void **,loff_t *,loff_t , -enum kernel_read_file_id ); -extern int kernel_read_file_from_fd (int ,void **,loff_t *,loff_t , -enum kernel_read_file_id );extern ssize_t kernel_read (struct file *,void *,size_t ,loff_t *);ssize_t __kernel_read (struct file *file,void *buf,size_t count,loff_t *pos);extern ssize_t kernel_write (struct file *,const void *,size_t ,loff_t *);
include/linux/ima.h +1 -0
@@ -7,6 +7,7 @@#ifndef _LINUX_IMA_H#define _LINUX_IMA_H +#include <linux/kernel_read_file.h> #include <linux/fs.h> #include <linux/security.h> #include <linux/kexec.h>
include/linux/kernel_read_file.h new file +51 -0
51 lines not on this page · raw message
include/linux/security.h +1 -0
@@ -23,6 +23,7 @@#ifndef __LINUX_SECURITY_H#define __LINUX_SECURITY_H +#include <linux/kernel_read_file.h> #include <linux/key.h> #include <linux/capability.h> #include <linux/fs.h>
kernel/kexec_file.c +1 -0
@@ -24,6 +24,7 @@#include <linux/elf.h> #include <linux/elfcore.h> #include <linux/kernel.h> +#include <linux/kernel_read_file.h> #include <linux/syscalls.h> #include <linux/vmalloc.h> #include "kexec_internal.h"
kernel/module.c +1 -0
@@ -18,6 +18,7 @@#include <linux/fs.h> #include <linux/sysfs.h> #include <linux/kernel.h> +#include <linux/kernel_read_file.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/elf.h>
security/integrity/digsig.c +1 -0
@@ -10,6 +10,7 @@#include <linux/sched.h> #include <linux/slab.h> #include <linux/cred.h> +#include <linux/kernel_read_file.h> #include <linux/key-type.h> #include <linux/digsig.h> #include <linux/vmalloc.h>
security/integrity/ima/ima_fs.c +1 -0
@@ -13,6 +13,7 @@*/ #include <linux/fcntl.h> +#include <linux/kernel_read_file.h> #include <linux/slab.h> #include <linux/init.h> #include <linux/seq_file.h>
security/integrity/ima/ima_main.c +1 -0
@@ -18,6 +18,7 @@#include <linux/module.h> #include <linux/file.h> #include <linux/binfmts.h> +#include <linux/kernel_read_file.h> #include <linux/mount.h> #include <linux/mman.h> #include <linux/slab.h>
security/integrity/ima/ima_policy.c +1 -0
@@ -9,6 +9,7 @@#include <linux/init.h> #include <linux/list.h> +#include <linux/kernel_read_file.h> #include <linux/fs.h> #include <linux/security.h> #include <linux/magic.h>
security/loadpin/loadpin.c +1 -0
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/fs.h> +#include <linux/kernel_read_file.h> #include <linux/lsm_hooks.h> #include <linux/mount.h> #include <linux/path.h>
security/security.c +1 -0
@@ -16,6 +16,7 @@#include <linux/export.h> #include <linux/init.h> #include <linux/kernel.h> +#include <linux/kernel_read_file.h> #include <linux/lsm_hooks.h> #include <linux/integrity.h> #include <linux/ima.h>
security/selinux/hooks.c +1 -0
@@ -24,6 +24,7 @@#include <linux/init.h> #include <linux/kd.h> #include <linux/kernel.h> +#include <linux/kernel_read_file.h> #include <linux/tracehook.h> #include <linux/errno.h> #include <linux/sched/signal.h>
Signature · 1 line
2.25.1
Re: [PATCH v4 04/17] fs/kernel_read_file: Split into separate include file
Quoted 6 lines · Kees Cook
From: Scott Branden <[email protected]>
Move kernel_read_file* out of
linux/fs.hto its ownlinux/kernel_read_file.hinclude file. That header gets pulled in just about everywhere and doesn't really need functions not related to the general fs interface.
Quoted 8 lines
Suggested-by Christoph Hellwig <[email protected]>Signed-off-by Scott Branden <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>Reviewed-by Mimi Zohar <[email protected]>Reviewed-by Luis Chamberlain <[email protected]>Signed-off-by Kees Cook <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v4 5/17 fs/kernel_read_file: Split into separate source file
In reply to Kees CookThese routines are used in places outside of exec(2), so in preparation for refactoring them, move them into a separate source file, fs/kernel_read_file.c.
--- fs/Makefile | 3 +- fs/exec.c | 132 ---------------------------------------- fs/kernel_read_file.c | 138 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 133 deletions(-) create mode 100644 fs/kernel_read_file.c
fs/Makefile +2 -1
@@ -13,7 +13,8 @@ obj-y := open.o read_write.o file_table.o super.o \ seq_file.o xattr.o libfs.o fs-writeback.o \ pnode.o splice.o sync.o utimes.o d_path.o \ stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \ - fs_types.o fs_context.o fs_parser.o fsopen.o + fs_types.o fs_context.o fs_parser.o fsopen.o \ + kernel_read_file.o ifeq ($(CONFIG_BLOCK),y) obj-y += buffer.o block_dev.o direct-io.o mpage.o
fs/exec.c +0 -132
138 lines not on this page · raw message
fs/kernel_read_file.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 6/17 fs/kernel_read_file: Remove redundant size argument
In reply to Kees CookIn preparation for refactoring kernel_read_file*(), remove the redundant "size" argument which is not needed: it can be included in the return code, with callers adjusted. (VFS reads already cannot be larger than INT_MAX.)
--- drivers/base/firmware_loader/main.c | 10 ++++++---- fs/kernel_read_file.c | 20 +++++++++----------- include/linux/kernel_read_file.h | 8 ++++---- kernel/kexec_file.c | 14 +++++++------- kernel/module.c | 7 +++---- security/integrity/digsig.c | 5 +++-- security/integrity/ima/ima_fs.c | 6 ++++-- 7 files changed, 36 insertions(+), 34 deletions(-)
drivers/base/firmware_loader/main.c +6 -4
@@ -462,7 +462,7 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,size_t in_size, const void *in_buffer)) { - loff_t size; + size_t size;int i, len;int rc = -ENOENT ;char *path;
@@ -494,10 +494,9 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv, fw_priv->size =0 ;/* load firmware files from the mount namespace of init */ - rc =kernel_read_file_from_path_initns (path, &buffer, - &size, msize, + rc =kernel_read_file_from_path_initns (path, &buffer, msize,READING_FIRMWARE ); -if (rc) { +if (rc <0 ) {if (rc != -ENOENT )dev_warn (device,"loading %s failed with error %d\n" , path, rc);
@@ -506,6 +505,9 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv, path);continue ; } + size = rc; + rc =0 ; +dev_dbg (device,"Loading firmware from %s\n" , path);if (decompress) { dev_dbg(device, "f/w decompressing %s\n",
fs/kernel_read_file.c +9 -11
@@ -5,7 +5,7 @@#include <linux/security.h> #include <linux/vmalloc.h> -int kernel_read_file (struct file *file,void **buf,loff_t *size, +int kernel_read_file (struct file *file,void **buf,loff_t max_size,enum kernel_read_file_id id) {loff_t i_size, pos;
@@ -29,7 +29,7 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, ret = -EINVAL ; goto out; } -if (i_size >SIZE_MAX || (max_size >0 && i_size > max_size)) { +if (i_size >INT_MAX || (max_size >0 && i_size > max_size)) { ret = -EFBIG ; goto out; }
@@ -59,8 +59,6 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, } ret =security_kernel_post_read_file (file, *buf, i_size, id); -if (!ret) - *size = pos; out_free:if (ret <0 ) {
@@ -72,11 +70,11 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, out:allow_write_access (file); -return ret; +return ret ==0 ? pos : ret; }EXPORT_SYMBOL_GPL (kernel_read_file); -int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, +int kernel_read_file_from_path (const char *path,void **buf,loff_t max_size,enum kernel_read_file_id id) {struct file *file;
@@ -89,14 +87,14 @@ int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,if (IS_ERR (file))return PTR_ERR (file); - ret =kernel_read_file (file, buf, size, max_size, id); + ret =kernel_read_file (file, buf, max_size, id);fput (file);return ret; }EXPORT_SYMBOL_GPL (kernel_read_file_from_path);int kernel_read_file_from_path_initns (const char *path,void **buf, -loff_t *size,loff_t max_size, +loff_t max_size,enum kernel_read_file_id id) {struct file *file;
@@ -115,13 +113,13 @@ int kernel_read_file_from_path_initns(const char *path, void **buf,if (IS_ERR (file))return PTR_ERR (file); - ret =kernel_read_file (file, buf, size, max_size, id); + ret =kernel_read_file (file, buf, max_size, id);fput (file);return ret; }EXPORT_SYMBOL_GPL (kernel_read_file_from_path_initns); -int kernel_read_file_from_fd (int fd,void **buf,loff_t *size,loff_t max_size, +int kernel_read_file_from_fd (int fd,void **buf,loff_t max_size,enum kernel_read_file_id id) {struct fd f =fdget (fd);
@@ -130,7 +128,7 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,if (!f.file) goto out; - ret =kernel_read_file (f.file, buf, size, max_size, id); + ret =kernel_read_file (f.file, buf, max_size, id); out:fdput (f);return ret;
include/linux/kernel_read_file.h +4 -4
@@ -36,16 +36,16 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id) }int kernel_read_file (struct file *file, -void **buf,loff_t *size,loff_t max_size, +void **buf,loff_t max_size,enum kernel_read_file_id id);int kernel_read_file_from_path (const char *path, -void **buf,loff_t *size,loff_t max_size, +void **buf,loff_t max_size,enum kernel_read_file_id id);int kernel_read_file_from_path_initns (const char *path, -void **buf,loff_t *size,loff_t max_size, +void **buf,loff_t max_size,enum kernel_read_file_id id);int kernel_read_file_from_fd (int fd, -void **buf,loff_t *size,loff_t max_size, +void **buf,loff_t max_size,enum kernel_read_file_id id);#endif /* _LINUX_KERNEL_READ_FILE_H */
kernel/kexec_file.c +7 -7
@@ -220,13 +220,12 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, {int ret;void *ldata; -loff_t size; ret =kernel_read_file_from_fd (kernel_fd, &image->kernel_buf, - &size,INT_MAX ,READING_KEXEC_IMAGE ); -if (ret) +INT_MAX ,READING_KEXEC_IMAGE ); +if (ret <0 )return ret; - image->kernel_buf_len = size; + image->kernel_buf_len = ret;/* Call arch image probe handlers */ ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
@@ -243,11 +242,12 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,/* It is possible that there no initramfs is being loaded */ if (!(flags &KEXEC_FILE_NO_INITRAMFS )) { ret =kernel_read_file_from_fd (initrd_fd, &image->initrd_buf, - &size,INT_MAX , +INT_MAX ,READING_KEXEC_INITRAMFS ); -if (ret) +if (ret <0 ) goto out; - image->initrd_buf_len = size; + image->initrd_buf_len = ret; + ret =0 ; }if (cmdline_len) {
kernel/module.c +3 -4
@@ -3991,7 +3991,6 @@ SYSCALL_DEFINE3(init_module, void __user *, umod, SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) {struct load_info info = { }; -loff_t size;void *hdr =NULL ;int err;
@@ -4005,12 +4004,12 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) |MODULE_INIT_IGNORE_VERMAGIC))return -EINVAL ; - err =kernel_read_file_from_fd (fd, &hdr, &size,INT_MAX , + err =kernel_read_file_from_fd (fd, &hdr,INT_MAX ,READING_MODULE ); -if (err) +if (err <0 )return err; info.hdr = hdr; - info.len = size; + info.len = err;return load_module (&info, uargs, flags); }
security/integrity/digsig.c +3 -2
@@ -171,16 +171,17 @@ int __init integrity_add_key(const unsigned int id, const void *data,int __init integrity_load_x509 (const unsigned int id,const char *path) {void *data =NULL ; -loff_t size; +size_t size;int rc;key_perm_t perm; - rc =kernel_read_file_from_path (path, &data, &size,0 , + rc =kernel_read_file_from_path (path, &data,0 ,READING_X509_CERTIFICATE );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);return rc; } + size = rc; perm = (KEY_POS_ALL & ~KEY_POS_SETATTR ) |KEY_USR_VIEW |KEY_USR_READ ;
security/integrity/ima/ima_fs.c +4 -2
@@ -275,7 +275,7 @@ static ssize_t ima_read_policy(char *path) {void *data =NULL ;char *datap; -loff_t size; +size_t size;int rc, pathlen =strlen (path);char *p;
@@ -284,11 +284,13 @@ static ssize_t ima_read_policy(char *path) datap = path;strsep (&datap,"\n" ); - rc =kernel_read_file_from_path (path, &data, &size,0 ,READING_POLICY ); + rc =kernel_read_file_from_path (path, &data,0 ,READING_POLICY );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);return rc; } + size = rc; + rc =0 ; datap = data;while (size >0 && (p =strsep (&datap,"\n" ))) {
Signature · 1 line
2.25.1
Re: [PATCH v4 06/17] fs/kernel_read_file: Remove redundant size argument
Quoted 9 lines · Kees Cook
In preparation for refactoring kernel_read_file*(), remove the redundant "size" argument which is not needed: it can be included in the return code, with callers adjusted. (VFS reads already cannot be larger than
INT_MAX.)Acked-by Scott Branden <[email protected]>Reviewed-by Mimi Zohar <[email protected]>Reviewed-by Luis Chamberlain <[email protected]>Signed-off-by Kees Cook <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v4 7/17 fs/kernel_read_file: Switch buffer size arg to size_t
In reply to Kees CookIn preparation for further refactoring of kernel_read_file*(), rename the "max_size" argument to the more accurate "buf_size", and correct its type to size_t. Add kerndoc to explain the specifics of how the arguments will be used. Note that with buf_size now size_t, it can no longer be negative (and was never called with a negative value). Adjust callers to use it as a "maximum size" when *buf is NULL.
--- fs/kernel_read_file.c | 34 +++++++++++++++++++++++--------- include/linux/kernel_read_file.h | 8 ++++---- security/integrity/digsig.c | 2 +- security/integrity/ima/ima_fs.c | 2 +- 4 files changed, 31 insertions(+), 15 deletions(-)
fs/kernel_read_file.c +25 -9
@@ -5,15 +5,31 @@#include <linux/security.h> #include <linux/vmalloc.h> +/** +* kernel_read_file() - read file contents into a kernel buffer +* +* @file file to read from +* @buf pointer to a "void *" buffer for reading into (if +* *@buf is NULL, a buffer will be allocated, and +* @buf_size will be ignored) +* @buf_size size of buf, if already allocated. If @buf not +* allocated, this is the largest size to allocate. +* @id the kernel_read_file_id identifying the type of +* file contents being read (for LSMs to examine) +* +* Returns number of bytes read (no single read will be bigger +* than INT_MAX), or negative on error. +* +*/ int kernel_read_file (struct file *file,void **buf, -loff_t max_size,enum kernel_read_file_id id) +size_t buf_size,enum kernel_read_file_id id) {loff_t i_size, pos;ssize_t bytes =0 ;void *allocated =NULL ;int ret; -if (!S_ISREG (file_inode (file)->i_mode) || max_size <0 ) +if (!S_ISREG (file_inode (file)->i_mode))return -EINVAL ; ret =deny_write_access (file);
@@ -29,7 +45,7 @@ int kernel_read_file(struct file *file, void **buf, ret = -EINVAL ; goto out; } -if (i_size >INT_MAX || (max_size >0 && i_size > max_size)) { +if (i_size >INT_MAX || i_size > buf_size) { ret = -EFBIG ; goto out; }
@@ -75,7 +91,7 @@ int kernel_read_file(struct file *file, void **buf,EXPORT_SYMBOL_GPL (kernel_read_file);int kernel_read_file_from_path (const char *path,void **buf, -loff_t max_size,enum kernel_read_file_id id) +size_t buf_size,enum kernel_read_file_id id) {struct file *file;int ret;
@@ -87,14 +103,14 @@ int kernel_read_file_from_path(const char *path, void **buf,if (IS_ERR (file))return PTR_ERR (file); - ret =kernel_read_file (file, buf, max_size, id); + ret =kernel_read_file (file, buf, buf_size, id);fput (file);return ret; }EXPORT_SYMBOL_GPL (kernel_read_file_from_path);int kernel_read_file_from_path_initns (const char *path,void **buf, -loff_t max_size, +size_t buf_size,enum kernel_read_file_id id) {struct file *file;
@@ -113,13 +129,13 @@ int kernel_read_file_from_path_initns(const char *path, void **buf,if (IS_ERR (file))return PTR_ERR (file); - ret =kernel_read_file (file, buf, max_size, id); + ret =kernel_read_file (file, buf, buf_size, id);fput (file);return ret; }EXPORT_SYMBOL_GPL (kernel_read_file_from_path_initns); -int kernel_read_file_from_fd (int fd,void **buf,loff_t max_size, +int kernel_read_file_from_fd (int fd,void **buf,size_t buf_size,enum kernel_read_file_id id) {struct fd f =fdget (fd);
@@ -128,7 +144,7 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t max_size,if (!f.file) goto out; - ret =kernel_read_file (f.file, buf, max_size, id); + ret =kernel_read_file (f.file, buf, buf_size, id); out:fdput (f);return ret;
include/linux/kernel_read_file.h +4 -4
@@ -36,16 +36,16 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id) }int kernel_read_file (struct file *file, -void **buf,loff_t max_size, +void **buf,size_t buf_size,enum kernel_read_file_id id);int kernel_read_file_from_path (const char *path, -void **buf,loff_t max_size, +void **buf,size_t buf_size,enum kernel_read_file_id id);int kernel_read_file_from_path_initns (const char *path, -void **buf,loff_t max_size, +void **buf,size_t buf_size,enum kernel_read_file_id id);int kernel_read_file_from_fd (int fd, -void **buf,loff_t max_size, +void **buf,size_t buf_size,enum kernel_read_file_id id);#endif /* _LINUX_KERNEL_READ_FILE_H */
security/integrity/digsig.c +1 -1
@@ -175,7 +175,7 @@ int __init integrity_load_x509(const unsigned int id, const char *path)int rc;key_perm_t perm; - rc =kernel_read_file_from_path (path, &data,0 , + rc =kernel_read_file_from_path (path, &data,INT_MAX ,READING_X509_CERTIFICATE );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);
security/integrity/ima/ima_fs.c +1 -1
@@ -284,7 +284,7 @@ static ssize_t ima_read_policy(char *path) datap = path;strsep (&datap,"\n" ); - rc =kernel_read_file_from_path (path, &data,0 ,READING_POLICY ); + rc =kernel_read_file_from_path (path, &data,INT_MAX ,READING_POLICY );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);return rc;
Signature · 1 line
2.25.1
Re: [PATCH v4 07/17] fs/kernel_read_file: Switch buffer size arg to size_t
Quoted 11 lines · Kees Cook
In preparation for further refactoring of kernel_read_file*(), rename the "
max_size" argument to the more accurate "buf_size", and correct its type tosize_t. Add kerndoc to explain the specifics of how the arguments will be used. Note that withbuf_sizenowsize_t, it can no longer be negative (and was never called with a negative value). Adjust callers to use it as a "maximum size" when *buf is NULL.Acked-by Scott Branden <[email protected]>Reviewed-by Mimi Zohar <[email protected]>Reviewed-by Luis Chamberlain <[email protected]>Signed-off-by Kees Cook <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v4 8/17 fs/kernel_read_file: Add file_size output argument
In reply to Kees CookIn preparation for adding partial read support, add an optional output argument to kernel_read_file*() that reports the file size so callers can reason more easily about their reading progress.
--- drivers/base/firmware_loader/main.c | 1 + fs/kernel_read_file.c | 19 +++++++++++++------ include/linux/kernel_read_file.h | 4 ++++ kernel/kexec_file.c | 4 ++-- kernel/module.c | 2 +- security/integrity/digsig.c | 2 +- security/integrity/ima/ima_fs.c | 2 +- 7 files changed, 23 insertions(+), 11 deletions(-)
drivers/base/firmware_loader/main.c +1 -0
@@ -495,6 +495,7 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,/* load firmware files from the mount namespace of init */ rc =kernel_read_file_from_path_initns (path, &buffer, msize, +NULL ,READING_FIRMWARE );if (rc <0 ) {if (rc != -ENOENT )
fs/kernel_read_file.c +13 -6
@@ -14,6 +14,8 @@* @buf_size will be ignored) * @buf_size size of buf, if already allocated. If @buf not * allocated, this is the largest size to allocate. +* @file_size if non-NULL, the full size of @file will be +* written here. * @id the kernel_read_file_id identifying the type of * file contents being read (for LSMs to examine) *
@@ -22,7 +24,8 @@* */ int kernel_read_file (struct file *file,void **buf, -size_t buf_size,enum kernel_read_file_id id) +size_t buf_size,size_t *file_size, +enum kernel_read_file_id id) {loff_t i_size, pos;ssize_t bytes =0 ;
@@ -49,6 +52,8 @@ int kernel_read_file(struct file *file, void **buf, ret = -EFBIG ; goto out; } +if (file_size) + *file_size = i_size;if (!*buf) *buf = allocated =vmalloc (i_size);
@@ -91,7 +96,8 @@ int kernel_read_file(struct file *file, void **buf,EXPORT_SYMBOL_GPL (kernel_read_file);int kernel_read_file_from_path (const char *path,void **buf, -size_t buf_size,enum kernel_read_file_id id) +size_t buf_size,size_t *file_size, +enum kernel_read_file_id id) {struct file *file;int ret;
@@ -103,14 +109,14 @@ int kernel_read_file_from_path(const char *path, void **buf,if (IS_ERR (file))return PTR_ERR (file); - ret =kernel_read_file (file, buf, buf_size, id); + ret =kernel_read_file (file, buf, buf_size, file_size, id);fput (file);return ret; }EXPORT_SYMBOL_GPL (kernel_read_file_from_path);int kernel_read_file_from_path_initns (const char *path,void **buf, -size_t buf_size, +size_t buf_size,size_t *file_size,enum kernel_read_file_id id) {struct file *file;
@@ -129,13 +135,14 @@ int kernel_read_file_from_path_initns(const char *path, void **buf,if (IS_ERR (file))return PTR_ERR (file); - ret =kernel_read_file (file, buf, buf_size, id); + ret =kernel_read_file (file, buf, buf_size, file_size, id);fput (file);return ret; }EXPORT_SYMBOL_GPL (kernel_read_file_from_path_initns);int kernel_read_file_from_fd (int fd,void **buf,size_t buf_size, +size_t *file_size,enum kernel_read_file_id id) {struct fd f =fdget (fd);
@@ -144,7 +151,7 @@ int kernel_read_file_from_fd(int fd, void **buf, size_t buf_size,if (!f.file) goto out; - ret =kernel_read_file (f.file, buf, buf_size, id); + ret =kernel_read_file (f.file, buf, buf_size, file_size, id); out:fdput (f);return ret;
include/linux/kernel_read_file.h +4 -0
@@ -37,15 +37,19 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)int kernel_read_file (struct file *file,void **buf,size_t buf_size, +size_t *file_size,enum kernel_read_file_id id);int kernel_read_file_from_path (const char *path,void **buf,size_t buf_size, +size_t *file_size,enum kernel_read_file_id id);int kernel_read_file_from_path_initns (const char *path,void **buf,size_t buf_size, +size_t *file_size,enum kernel_read_file_id id);int kernel_read_file_from_fd (int fd,void **buf,size_t buf_size, +size_t *file_size,enum kernel_read_file_id id);#endif /* _LINUX_KERNEL_READ_FILE_H */
kernel/kexec_file.c +2 -2
@@ -222,7 +222,7 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,void *ldata; ret =kernel_read_file_from_fd (kernel_fd, &image->kernel_buf, -INT_MAX ,READING_KEXEC_IMAGE ); +INT_MAX ,NULL ,READING_KEXEC_IMAGE );if (ret <0 )return ret; image->kernel_buf_len = ret;
@@ -242,7 +242,7 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,/* It is possible that there no initramfs is being loaded */ if (!(flags &KEXEC_FILE_NO_INITRAMFS )) { ret =kernel_read_file_from_fd (initrd_fd, &image->initrd_buf, -INT_MAX , +INT_MAX ,NULL ,READING_KEXEC_INITRAMFS );if (ret <0 ) goto out;
kernel/module.c +1 -1
@@ -4004,7 +4004,7 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) |MODULE_INIT_IGNORE_VERMAGIC))return -EINVAL ; - err =kernel_read_file_from_fd (fd, &hdr,INT_MAX , + err =kernel_read_file_from_fd (fd, &hdr,INT_MAX ,NULL ,READING_MODULE );if (err <0 )return err;
security/integrity/digsig.c +1 -1
@@ -175,7 +175,7 @@ int __init integrity_load_x509(const unsigned int id, const char *path)int rc;key_perm_t perm; - rc =kernel_read_file_from_path (path, &data,INT_MAX , + rc =kernel_read_file_from_path (path, &data,INT_MAX ,NULL ,READING_X509_CERTIFICATE );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);
security/integrity/ima/ima_fs.c +1 -1
@@ -284,7 +284,7 @@ static ssize_t ima_read_policy(char *path) datap = path;strsep (&datap,"\n" ); - rc =kernel_read_file_from_path (path, &data,INT_MAX ,READING_POLICY ); + rc =kernel_read_file_from_path (path, &data,INT_MAX ,NULL ,READING_POLICY );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);return rc;
Signature · 1 line
2.25.1
Re: [PATCH v4 08/17] fs/kernel_read_file: Add file_size output argument
Quoted 8 lines · Kees Cook
In preparation for adding partial read support, add an optional output argument to kernel_read_file*() that reports the file size so callers can reason more easily about their reading progress.
Acked-by Scott Branden <[email protected]>Reviewed-by Mimi Zohar <[email protected]>Reviewed-by Luis Chamberlain <[email protected]>Signed-off-by Kees Cook <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v4 9/17 LSM: Introduce kernel_post_load_data() hook
In reply to Kees CookThere are a few places in the kernel where LSMs would like to have visibility into the contents of a kernel buffer that has been loaded or read. While security_kernel_post_read_file() (which includes the buffer) exists as a pairing for security_kernel_read_file(), no such hook exists to pair with security_kernel_load_data().
Earlier proposals for just using security_kernel_post_read_file() with a NULL file argument were rejected (i.e. "file" should always be valid for the security_..._file hooks, but it appears at least one case was left in the kernel during earlier refactoring. (This will be fixed in a subsequent patch.)
Since not all cases of security_kernel_load_data() can have a single contiguous buffer made available to the LSM hook (e.g. kexec image segments are separately loaded), there needs to be a way for the LSM to reason about its expectations of the hook coverage. In order to handle this, add a "contents" argument to the "kernel_load_data" hook that indicates if the newly added "kernel_post_load_data" hook will be called with the full contents once loaded. That way, LSMs requiring full contents can choose to unilaterally reject "kernel_load_data" with contents=false (which is effectively the existing hook coverage), but when contents=true they can allow it and later evaluate the "kernel_post_load_data" hook once the buffer is loaded.
With this change, LSMs can gain coverage over non-file-backed data loads (e.g. init_module(2) and firmware userspace helper), which will happen in subsequent patches.
Additionally prepare IMA to start processing these cases.
--- drivers/base/firmware_loader/fallback.c | 2 +- .../base/firmware_loader/fallback_platform.c | 2 +- include/linux/ima.h | 12 +++++++++-- include/linux/lsm_hook_defs.h | 4 +++- include/linux/lsm_hooks.h | 9 ++++++++ include/linux/security.h | 12 +++++++++-- kernel/kexec.c | 2 +- kernel/module.c | 2 +- security/integrity/ima/ima_main.c | 21 ++++++++++++++++++- security/loadpin/loadpin.c | 2 +- security/security.c | 18 +++++++++++++--- security/selinux/hooks.c | 2 +- 12 files changed, 73 insertions(+), 15 deletions(-)
drivers/base/firmware_loader/fallback.c +1 -1
@@ -613,7 +613,7 @@ static bool fw_run_sysfs_fallback(u32 opt_flags)return false;/* Also permit LSMs and IMA to fail firmware sysfs fallback */ - ret =security_kernel_load_data (LOADING_FIRMWARE ); + ret =security_kernel_load_data (LOADING_FIRMWARE , false);if (ret <0 )return false;
drivers/base/firmware_loader/fallback_platform.c +1 -1
@@ -17,7 +17,7 @@ int firmware_fallback_platform(struct fw_priv *fw_priv, u32 opt_flags)if (!(opt_flags &FW_OPT_FALLBACK_PLATFORM ))return -ENOENT ; - rc =security_kernel_load_data (LOADING_FIRMWARE ); + rc =security_kernel_load_data (LOADING_FIRMWARE , false);if (rc)return rc;
include/linux/ima.h +10 -2
@@ -20,7 +20,9 @@ extern void ima_post_create_tmpfile(struct inode *inode);extern void ima_file_free (struct file *file);extern int ima_file_mmap (struct file *file,unsigned long prot);extern int ima_file_mprotect (struct vm_area_struct *vma,unsigned long prot); -extern int ima_load_data (enum kernel_load_data_id id); +extern int ima_load_data (enum kernel_load_data_id id,bool contents); +extern int ima_post_load_data (char *buf,loff_t size, +enum kernel_load_data_id id);extern int ima_read_file (struct file *file,enum kernel_read_file_id id);extern int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);
@@ -78,7 +80,13 @@ static inline int ima_file_mprotect(struct vm_area_struct *vma,return 0 ; } -static inline int ima_load_data (enum kernel_load_data_id id) +static inline int ima_load_data (enum kernel_load_data_id id,bool contents) +{ +return 0 ; +} + +static inline int ima_post_load_data (char *buf,loff_t size, +enum kernel_load_data_id id) {return 0 ; }
include/linux/lsm_hook_defs.h +3 -1
@@ -184,7 +184,9 @@ LSM_HOOK(void, LSM_RET_VOID, cred_getsecid, const struct cred *c, u32 *secid) LSM_HOOK(int, 0, kernel_act_as, struct cred *new, u32 secid) LSM_HOOK(int, 0, kernel_create_files_as, struct cred *new, struct inode *inode) LSM_HOOK(int, 0, kernel_module_request, char *kmod_name) -LSM_HOOK(int, 0, kernel_load_data, enum kernel_load_data_id id) +LSM_HOOK(int, 0, kernel_load_data, enum kernel_load_data_id id, bool contents) +LSM_HOOK(int, 0, kernel_post_load_data, char *buf, loff_t size, + enum kernel_read_file_id id) LSM_HOOK(int, 0, kernel_read_file, struct file *file, enum kernel_read_file_id id) LSM_HOOK(int, 0, kernel_post_read_file, struct file *file, char *buf,
include/linux/lsm_hooks.h +9 -0
@@ -635,7 +635,16 @@* @kernel_load_data: * Load data provided by userspace. * @id kernel load data identifier +* @contents if a subsequent @kernel_post_load_data will be called. * Return 0 if permission is granted. +* @kernel_post_load_data: +* Load data provided by a non-file source (usually userspace buffer). +* @buf pointer to buffer containing the data contents. +* @size length of the data contents. +* @id kernel load data identifier +* Return 0 if permission is granted. +* This must be paired with a prior @kernel_load_data call that had +* @contents set to true. * @kernel_read_file: * Read a file specified by userspace. * @file contains the file structure pointing to the file being read
include/linux/security.h +10 -2
@@ -387,7 +387,9 @@ void security_cred_getsecid(const struct cred *c, u32 *secid);int security_kernel_act_as (struct cred *new,u32 secid);int security_kernel_create_files_as (struct cred *new,struct inode *inode);int security_kernel_module_request (char *kmod_name); -int security_kernel_load_data (enum kernel_load_data_id id); +int security_kernel_load_data (enum kernel_load_data_id id,bool contents); +int security_kernel_post_load_data (char *buf,loff_t size, +enum kernel_load_data_id id);int security_kernel_read_file (struct file *file,enum kernel_read_file_id id);int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id);
@@ -1014,7 +1016,13 @@ static inline int security_kernel_module_request(char *kmod_name)return 0 ; } -static inline int security_kernel_load_data (enum kernel_load_data_id id) +static inline int security_kernel_load_data (enum kernel_load_data_id id,bool contents) +{ +return 0 ; +} + +static inline int security_kernel_post_load_data (char *buf,loff_t size, +enum kernel_load_data_id id) {return 0 ; }
kernel/kexec.c +1 -1
@@ -205,7 +205,7 @@ static inline int kexec_load_check(unsigned long nr_segments,return -EPERM ;/* Permit LSMs and IMA to fail the kexec */ - result =security_kernel_load_data (LOADING_KEXEC_IMAGE ); + result =security_kernel_load_data (LOADING_KEXEC_IMAGE , false);if (result <0 )return result;
kernel/module.c +1 -1
@@ -2970,7 +2970,7 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,if (info->len <sizeof (*(info->hdr)))return -ENOEXEC ; - err =security_kernel_load_data (LOADING_MODULE ); + err =security_kernel_load_data (LOADING_MODULE , false);if (err)return err;
security/integrity/ima/ima_main.c +20 -1
@@ -676,6 +676,8 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,/** * ima_load_data - appraise decision based on policy * @id: kernel load data caller identifier +* @contents: whether the full contents will be available in a later +* call to ima_post_load_data(). * * Callers of this LSM hook can not measure, appraise, or audit the * data provided by userspace. Enforce policy rules requring a file
@@ -683,7 +685,7 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,* * For permission return 0, otherwise return -EACCES. */ -int ima_load_data (enum kernel_load_data_id id) +int ima_load_data (enum kernel_load_data_id id,bool contents) {bool ima_enforce, sig_enforce;
@@ -723,6 +725,23 @@ int ima_load_data(enum kernel_load_data_id id)return 0 ; } +/** +* ima_post_load_data - appraise decision based on policy +* @buf: pointer to in memory file contents +* @size: size of in memory file contents +* @id: kernel load data caller identifier +* +* Measure/appraise/audit in memory buffer based on policy. Policy rules +* are written in terms of a policy identifier. +* +* On success return 0. On integrity appraisal error, assuming the file +* is in policy and IMA-appraisal is in enforcing mode, return -EACCES. +*/ +int ima_post_load_data (char *buf,loff_t size,enum kernel_load_data_id load_id) +{ +return 0 ; +} +/* * process_buffer_measurement - Measure the buffer to ima log. * @buf: pointer to the buffer that needs to be added to the log.
security/loadpin/loadpin.c +1 -1
@@ -176,7 +176,7 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)return 0 ; } -static int loadpin_load_data (enum kernel_load_data_id id) +static int loadpin_load_data (enum kernel_load_data_id id,bool contents) {return loadpin_read_file (NULL , (enum kernel_read_file_id ) id); }
security/security.c +15 -3
@@ -1695,17 +1695,29 @@ int security_kernel_post_read_file(struct file *file, char *buf, loff_t size, }EXPORT_SYMBOL_GPL (security_kernel_post_read_file); -int security_kernel_load_data (enum kernel_load_data_id id) +int security_kernel_load_data (enum kernel_load_data_id id,bool contents) {int ret; - ret =call_int_hook (kernel_load_data,0 , id); + ret =call_int_hook (kernel_load_data,0 , id, contents);if (ret)return ret; -return ima_load_data (id); +return ima_load_data (id, contents); }EXPORT_SYMBOL_GPL (security_kernel_load_data); +int security_kernel_post_load_data (char *buf,loff_t size, +enum kernel_load_data_id id) +{ +int ret; + + ret =call_int_hook (kernel_post_load_data,0 , buf, size, id); +if (ret) +return ret; +return ima_post_load_data (buf, size, id); +} +EXPORT_SYMBOL_GPL (security_kernel_post_load_data); +int security_task_fix_setuid (struct cred *new,const struct cred *old,int flags) {
security/selinux/hooks.c +1 -1
@@ -4019,7 +4019,7 @@ static int selinux_kernel_read_file(struct file *file,return rc; } -static int selinux_kernel_load_data (enum kernel_load_data_id id) +static int selinux_kernel_load_data (enum kernel_load_data_id id,bool contents) {int rc =0 ;
Signature · 1 line
2.25.1
Mimi Zohar
maintainerRe: [PATCH v4 09/17] LSM: Introduce kernel_post_load_data() hook
Quoted 24 lines · Kees Cook
There are a few places in the kernel where LSMs would like to have visibility into the contents of a kernel buffer that has been loaded or read. While
security_kernel_post_read_file()(which includes the buffer) exists as a pairing forsecurity_kernel_read_file(), no such hook exists to pair withsecurity_kernel_load_data().Earlier proposals for just using
security_kernel_post_read_file()with a NULL file argument were rejected (i.e. "file" should always be valid for the security_..._file hooks, but it appears at least one case was left in the kernel during earlier refactoring. (This will be fixed in a subsequent patch.)Since not all cases of
security_kernel_load_data()can have a single contiguous buffer made available to the LSM hook (e.g. kexec image segments are separately loaded), there needs to be a way for the LSM to reason about its expectations of the hook coverage. In order to handle this, add a "contents" argument to the "kernel_load_data" hook that indicates if the newly added "kernel_post_load_data" hook will be called with the full contents once loaded. That way, LSMs requiring full contents can choose to unilaterally reject "kernel_load_data" with contents=false (which is effectively the existing hook coverage), but when contents=true they can allow it and later evaluate the "kernel_post_load_data" hook once the buffer is loaded.
Quoted 7 lines
With this change, LSMs can gain coverage over non-file-backed data loads (e.g.
init_module(2)and firmware userspace helper), which will happen in subsequent patches.Additionally prepare IMA to start processing these cases.
Signed-off-by Kees Cook <[email protected]>
Thanks, Kees. Other than a missing "name" field, it looks good.
The security_kernel_post_load_data hook may be used to verify appended signatures and to measure the buffer data. Passing the kernel module (load_info.name) and firmware (fw_name) names is critical at least for IMA-measurement.
thanks,
Mimi
Re: [PATCH v4 09/17] LSM: Introduce kernel_post_load_data() hook
In reply to Kees CookQuoted 25 lines · Kees Cook
There are a few places in the kernel where LSMs would like to have visibility into the contents of a kernel buffer that has been loaded or read. While
security_kernel_post_read_file()(which includes the buffer) exists as a pairing forsecurity_kernel_read_file(), no such hook exists to pair withsecurity_kernel_load_data().Earlier proposals for just using
security_kernel_post_read_file()with a NULL file argument were rejected (i.e. "file" should always be valid for the security_..._file hooks, but it appears at least one case was left in the kernel during earlier refactoring. (This will be fixed in a subsequent patch.)Since not all cases of
security_kernel_load_data()can have a single contiguous buffer made available to the LSM hook (e.g. kexec image segments are separately loaded), there needs to be a way for the LSM to reason about its expectations of the hook coverage. In order to handle this, add a "contents" argument to the "kernel_load_data" hook that indicates if the newly added "kernel_post_load_data" hook will be called with the full contents once loaded. That way, LSMs requiring full contents can choose to unilaterally reject "kernel_load_data" with contents=false (which is effectively the existing hook coverage), but when contents=true they can allow it and later evaluate the "kernel_post_load_data" hook once the buffer is loaded.
Quoted 7 lines
With this change, LSMs can gain coverage over non-file-backed data loads (e.g.
init_module(2)and firmware userspace helper), which will happen in subsequent patches.Additionally prepare IMA to start processing these cases.
Signed-off-by Kees Cook <[email protected]>
Thanks for adding this! Would be really useful for us.
Quoted 2 lines
--- drivers/base/firmware_loader/fallback.c | 2 +-
[...]
Quoted 15 lines
index
5de45010fb1a..1a5c68196faf100644security/selinux/hooks.c +1
-2@@ -4019,7 +4019,7 @@ static int selinux_kernel_read_file(struct file *file,return rc; } -static int selinux_kernel_load_data (enum kernel_load_data_id id) +static int selinux_kernel_load_data (enum kernel_load_data_id id,bool contents) {int rc =0 ; --2.25.1
PATCH v4 10/17 firmware_loader: Use security_post_load_data()
In reply to Kees CookNow that security_post_load_data() is wired up, use it instead of the NULL file argument style of security_post_read_file(), and update the security_kernel_load_data() call to indicate that a security_kernel_post_load_data() call is expected.
Wire up the IMA check to match earlier logic. Perhaps a generalized change to ima_post_load_data() might look something like this:
return process_buffer_measurement(buf, size,
kernel_load_data_id_str(load_id), read_idmap[load_id] ?: FILE_CHECK, 0, NULL);
--- drivers/base/firmware_loader/fallback.c | 8 ++++---- .../base/firmware_loader/fallback_platform.c | 7 ++++++- security/integrity/ima/ima_main.c | 20 +++++++++---------- 3 files changed, 20 insertions(+), 15 deletions(-)
drivers/base/firmware_loader/fallback.c +4 -4
@@ -272,9 +272,9 @@ static ssize_t firmware_loading_store(struct device *dev,dev_err (dev,"%s: map pages failed\n" , __func__); else - rc =security_kernel_post_read_file (NULL , - fw_priv->data, fw_priv->size, -READING_FIRMWARE ); + rc =security_kernel_post_load_data (fw_priv->data, + fw_priv->size, +LOADING_FIRMWARE );/* * Same logic as fw_load_abort, only the DONE bit
@@ -613,7 +613,7 @@ static bool fw_run_sysfs_fallback(u32 opt_flags)return false;/* Also permit LSMs and IMA to fail firmware sysfs fallback */ - ret =security_kernel_load_data (LOADING_FIRMWARE , false); + ret =security_kernel_load_data (LOADING_FIRMWARE , true);if (ret <0 )return false;
drivers/base/firmware_loader/fallback_platform.c +6 -1
@@ -17,7 +17,7 @@ int firmware_fallback_platform(struct fw_priv *fw_priv, u32 opt_flags)if (!(opt_flags &FW_OPT_FALLBACK_PLATFORM ))return -ENOENT ; - rc =security_kernel_load_data (LOADING_FIRMWARE , false); + rc =security_kernel_load_data (LOADING_FIRMWARE , true);if (rc)return rc;
@@ -27,6 +27,11 @@ int firmware_fallback_platform(struct fw_priv *fw_priv, u32 opt_flags)if (fw_priv->data && size > fw_priv->allocated_size)return -ENOMEM ; + + rc =security_kernel_post_load_data ((u8 *)data, size,LOADING_FIRMWARE ); +if (rc) +return rc; +if (!fw_priv->data) fw_priv->data =vmalloc (size);if (!fw_priv->data)
security/integrity/ima/ima_main.c +10 -10
@@ -648,15 +648,6 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,enum ima_hooks func;u32 secid; -if (!file && read_id ==READING_FIRMWARE ) { -if ((ima_appraise &IMA_APPRAISE_FIRMWARE ) && - (ima_appraise &IMA_APPRAISE_ENFORCE )) { -pr_err ("Prevent firmware loading_store.\n" ); -return -EACCES ;/* INTEGRITY_UNKNOWN */ - } -return 0 ; - } -/* permit signed certs */ if (!file && read_id ==READING_X509_CERTIFICATE )return 0 ;
@@ -706,7 +697,7 @@ int ima_load_data(enum kernel_load_data_id id, bool contents) }break ;case LOADING_FIRMWARE : -if (ima_enforce && (ima_appraise &IMA_APPRAISE_FIRMWARE )) { +if (ima_enforce && (ima_appraise &IMA_APPRAISE_FIRMWARE ) && !contents) {pr_err ("Prevent firmware sysfs fallback loading.\n" );return -EACCES ;/* INTEGRITY_UNKNOWN */ }
@@ -739,6 +730,15 @@ int ima_load_data(enum kernel_load_data_id id, bool contents)*/ int ima_post_load_data (char *buf,loff_t size,enum kernel_load_data_id load_id) { +if (load_id ==LOADING_FIRMWARE ) { +if ((ima_appraise &IMA_APPRAISE_FIRMWARE ) && + (ima_appraise &IMA_APPRAISE_ENFORCE )) { +pr_err ("Prevent firmware loading_store.\n" ); +return -EACCES ;/* INTEGRITY_UNKNOWN */ + } +return 0 ; + } +return 0 ; }
Signature · 1 line
2.25.1
Re: [PATCH v4 10/17] firmware_loader: Use security_post_load_data()
Quoted 8 lines · Kees Cook
Now that
security_post_load_data()is wired up, use it instead of the NULL file argument style ofsecurity_post_read_file(), and update thesecurity_kernel_load_data()call to indicate that asecurity_kernel_post_load_data()call is expected.Wire up the IMA check to match earlier logic. Perhaps a generalized change to
ima_post_load_data()might look something like this:
Quoted 6 lines
return process_buffer_measurement(buf, size,
kernel_load_data_id_str(load_id), read_idmap[load_id] ?:FILE_CHECK, 0, NULL);Signed-off-by Kees Cook <[email protected]>
Other than one change and one question below, it looks good.
<snip>
security/integrity/ima/ima_main.c int ima_load_data(enum kernel_load_data_id id, bool contents) · line 701
Quoted 12 lines
security/integrity/ima/ima_main.c +1
-1@@ -706,7 +697,7 @@ int ima_load_data(enum kernel_load_data_id id, bool contents) }break ;case LOADING_FIRMWARE : -if (ima_enforce && (ima_appraise &IMA_APPRAISE_FIRMWARE )) { +if (ima_enforce && (ima_appraise &IMA_APPRAISE_FIRMWARE ) && !contents) {pr_err ("Prevent firmware sysfs fallback loading.\n" );
Appended signatures are limited to kernel modules and, more recently, to the kexec kernel image, not firmware. Without a file descriptor, file signatures stored as an xattrs are not applicable either. We might as well fail earlier, rather than later. Adding "!contents" is unnecessary.
Quoted 4 lines
security/integrity/ima/ima_main.c +0
-0return -EACCES ;/* INTEGRITY_UNKNOWN */ }@@ -739,6 +730,15 @@ int ima_load_data(enum kernel_load_data_id id, bool contents)*/
security/integrity/ima/ima_main.c int ima_load_data(enum kernel_load_data_id id, bool contents) · line 740
Quoted 10 lines
security/integrity/ima/ima_main.c +8
-0int ima_post_load_data (char *buf,loff_t size,enum kernel_load_data_id load_id) { +if (load_id ==LOADING_FIRMWARE ) { +if ((ima_appraise &IMA_APPRAISE_FIRMWARE ) && + (ima_appraise &IMA_APPRAISE_ENFORCE )) { +pr_err ("Prevent firmware loading_store.\n" ); +return -EACCES ;/* INTEGRITY_UNKNOWN */ + } +return 0 ; + }
Even with failing LOADING_FIRMWARE early in ima_load_data(), is this still needed for fw_sysfs_loading()?
thanks,
Mimi
Quoted 4 lines
security/integrity/ima/ima_main.c +1
-0+return 0 ; }
PATCH v4 11/17 module: Call security_kernel_post_load_data()
In reply to Kees CookNow that there is an API for checking loaded contents for modules loaded without a file, call into the LSM hooks.
--- kernel/module.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
kernel/module.c +10 -4
@@ -2970,7 +2970,7 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,if (info->len <sizeof (*(info->hdr)))return -ENOEXEC ; - err =security_kernel_load_data (LOADING_MODULE , false); + err =security_kernel_load_data (LOADING_MODULE , true);if (err)return err;
@@ -2980,11 +2980,17 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,return -ENOMEM ;if (copy_chunked_from_user (info->hdr, umod, info->len) !=0 ) { -vfree (info->hdr); -return -EFAULT ; + err = -EFAULT ; + goto out; } -return 0 ; + err =security_kernel_post_load_data ((char *)info->hdr, info->len, +LOADING_MODULE ); +out: +if (err) +vfree (info->hdr); + +return err; }static void free_copy (struct load_info *info)
Signature · 1 line
2.25.1
Re: [PATCH v4 11/17] module: Call security_kernel_post_load_data()
+++ Kees Cook [29/07/20 10:58 -0700]:
Quoted 5 lines
Now that there is an API for checking loaded contents for modules loaded without a file, call into the LSM hooks.
Cc Jessica Yu <[email protected]>Signed-off-by Kees Cook <[email protected]>
KP Singh
Reviewed-byRe: [PATCH v4 11/17] module: Call security_kernel_post_load_data()
Quoted 9 lines · Jessica Yu
+++ Kees Cook [29/07/20 10:58 -0700]:
Quoted 5 lines
Now that there is an API for checking loaded contents for modules loaded without a file, call into the LSM hooks.
Cc Jessica Yu <[email protected]>Signed-off-by Kees Cook <[email protected]>Acked-by Jessica Yu <[email protected]>
Thanks!
PATCH v4 12/17 LSM: Add "contents" flag to kernel_read_file hook
In reply to Kees CookAs 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.
--- fs/kernel_read_file.c | 2 +- include/linux/ima.h | 6 ++++-- include/linux/lsm_hook_defs.h | 2 +- include/linux/lsm_hooks.h | 3 +++ include/linux/security.h | 6 ++++-- security/integrity/ima/ima_main.c | 10 +++++++++- security/loadpin/loadpin.c | 14 ++++++++++++-- security/security.c | 7 ++++--- security/selinux/hooks.c | 5 +++-- 9 files changed, 41 insertions(+), 14 deletions(-)
fs/kernel_read_file.c +1 -1
@@ -39,7 +39,7 @@ int kernel_read_file(struct file *file, void **buf,if (ret)return ret; - ret =security_kernel_read_file (file, id); + ret =security_kernel_read_file (file, id, true);if (ret) goto out;
include/linux/ima.h +4 -2
@@ -23,7 +23,8 @@ extern int ima_file_mprotect(struct vm_area_struct *vma, unsigned long prot);extern int ima_load_data (enum kernel_load_data_id id,bool contents);extern int ima_post_load_data (char *buf,loff_t size,enum kernel_load_data_id id); -extern int ima_read_file (struct file *file,enum kernel_read_file_id id); +extern int ima_read_file (struct file *file,enum kernel_read_file_id id, +bool contents);extern int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);extern void ima_post_path_mknod (struct dentry *dentry);
@@ -91,7 +92,8 @@ static inline int ima_post_load_data(char *buf, loff_t size,return 0 ; } -static inline int ima_read_file (struct file *file,enum kernel_read_file_id id) +static inline int ima_read_file (struct file *file,enum kernel_read_file_id id, +bool contents) {return 0 ; }
include/linux/lsm_hook_defs.h +1 -1
@@ -188,7 +188,7 @@ LSM_HOOK(int, 0, kernel_load_data, enum kernel_load_data_id id, bool contents) LSM_HOOK(int, 0, kernel_post_load_data, char *buf, loff_t size, enum kernel_read_file_id id) LSM_HOOK(int, 0, kernel_read_file, struct file *file, - enum kernel_read_file_id id) + enum kernel_read_file_id id, bool contents) LSM_HOOK(int, 0, kernel_post_read_file, struct file *file, char *buf, loff_t size, enum kernel_read_file_id id) LSM_HOOK(int, 0, task_fix_setuid, struct cred *new, const struct cred *old,
include/linux/lsm_hooks.h +3 -0
@@ -650,6 +650,7 @@* @file contains the file structure pointing to the file being read * by the kernel. * @id kernel read file identifier +* @contents if a subsequent @kernel_post_read_file will be called. * Return 0 if permission is granted. * @kernel_post_read_file: * Read a file specified by userspace.
@@ -658,6 +659,8 @@* @buf pointer to buffer containing the file contents. * @size length of the file contents. * @id kernel read file identifier +* This must be paired with a prior @kernel_read_file call that had +* @contents set to true. * Return 0 if permission is granted. * @task_fix_setuid: * Update the module's state after setting one or more of the user
include/linux/security.h +4 -2
@@ -390,7 +390,8 @@ int security_kernel_module_request(char *kmod_name);int security_kernel_load_data (enum kernel_load_data_id id,bool contents);int security_kernel_post_load_data (char *buf,loff_t size,enum kernel_load_data_id id); -int security_kernel_read_file (struct file *file,enum kernel_read_file_id id); +int security_kernel_read_file (struct file *file,enum kernel_read_file_id id, +bool contents);int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id); int security_task_fix_setuid(struct cred *new, const struct cred *old,
@@ -1028,7 +1029,8 @@ static inline int security_kernel_post_load_data(char *buf, loff_t size, }static inline int security_kernel_read_file (struct file *file, -enum kernel_read_file_id id) +enum kernel_read_file_id id, +bool contents) {return 0 ; }
security/integrity/ima/ima_main.c +9 -1
@@ -602,6 +602,7 @@ void ima_post_path_mknod(struct dentry *dentry)* ima_read_file - pre-measure/appraise hook decision based on policy * @file: pointer to the file to be measured/appraised/audit * @read_id: caller identifier +* @contents: whether a subsequent call will be made to ima_post_read_file() * * Permit reading a file based on policy. The policy rules are written * in terms of the policy identifier. Appraising the integrity of
@@ -609,8 +610,15 @@ void ima_post_path_mknod(struct dentry *dentry)* * For permission return 0, otherwise return -EACCES. */ -int ima_read_file (struct file *file,enum kernel_read_file_id read_id) +int ima_read_file (struct file *file,enum kernel_read_file_id read_id, +bool contents) { +/* Reject all partial reads during appraisal. */ +if (!contents) { +if (ima_appraise &IMA_APPRAISE_ENFORCE ) +return -EACCES ; + } +/* * Do devices using pre-allocated memory run the risk of the * firmware being accessible to the device prior to the completion
security/loadpin/loadpin.c +12 -2
@@ -117,11 +117,21 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb) } } -static int loadpin_read_file (struct file *file,enum kernel_read_file_id id) +static int loadpin_read_file (struct file *file,enum kernel_read_file_id id, +bool contents) {struct super_block *load_root;const char *origin =kernel_read_file_id_str (id); +/* +* If we will not know that we'll be seeing the full contents +* then we cannot trust a load will be complete and unchanged +* off disk. Treat all contents=false hooks as if there were +* no associated file struct. +*/ +if (!contents) + file =NULL ; +/* If the file id is excluded, ignore the pinning. */ if ((unsigned int )id <ARRAY_SIZE (ignore_read_file_id) && ignore_read_file_id[id]) {
@@ -178,7 +188,7 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)static int loadpin_load_data (enum kernel_load_data_id id,bool contents) { -return loadpin_read_file (NULL , (enum kernel_read_file_id ) id); +return loadpin_read_file (NULL , (enum kernel_read_file_id ) id, contents); } static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
security/security.c +4 -3
@@ -1672,14 +1672,15 @@ int security_kernel_module_request(char *kmod_name)return integrity_kernel_module_request (kmod_name); } -int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) +int security_kernel_read_file (struct file *file,enum kernel_read_file_id id, +bool contents) {int ret; - ret =call_int_hook (kernel_read_file,0 , file, id); + ret =call_int_hook (kernel_read_file,0 , file, id, contents);if (ret)return ret; -return ima_read_file (file, id); +return ima_read_file (file, id, contents); }EXPORT_SYMBOL_GPL (security_kernel_read_file);
security/selinux/hooks.c +3 -2
@@ -4004,13 +4004,14 @@ static int selinux_kernel_module_from_file(struct file *file) }static int selinux_kernel_read_file (struct file *file, -enum kernel_read_file_id id) +enum kernel_read_file_id id, +bool contents) {int rc =0 ;switch (id) {case READING_MODULE : - rc =selinux_kernel_module_from_file (file); + rc =selinux_kernel_module_from_file (contents ? file :NULL );break ;default :break ;
Signature · 1 line
2.25.1
Re: [PATCH v4 12/17] LSM: Add "contents" flag to kernel_read_file hook
Quoted 7 lines · Kees Cook
As with the
kernel_load_dataLSM hook, add a "contents" flag to thekernel_read_fileLSM hook that indicates whether the LSM can expect a matching call to thekernel_post_read_fileLSM 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.
Quoted 6 lines
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_filehook is called with contents=false (or reject such cases). Adjust all existing LSMs to retain existing behavior.Signed-off-by Kees Cook <[email protected]>
PATCH v4 13/17 IMA: Add support for file reads without contents
In reply to Kees CookFrom: Scott Branden <[email protected]>
When the kernel_read_file LSM hook is called with contents=false, IMA can appraise the file directly, without requiring a filled buffer. When such a buffer is available, though, IMA can continue to use it instead of forcing a double read here.
--- security/integrity/ima/ima_main.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
security/integrity/ima/ima_main.c +16 -6
@@ -613,11 +613,8 @@ void ima_post_path_mknod(struct dentry *dentry)int ima_read_file (struct file *file,enum kernel_read_file_id read_id,bool contents) { -/* Reject all partial reads during appraisal. */ -if (!contents) { -if (ima_appraise &IMA_APPRAISE_ENFORCE ) -return -EACCES ; - } +enum ima_hooks func; +u32 secid;/* * Do devices using pre-allocated memory run the risk of the
@@ -626,7 +623,20 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id,* buffers? It may be desirable to include the buffer address * in this API and walk all the dma_map_single() mappings to check. */ -return 0 ; + +/* +* There will be a call made to ima_post_read_file() with +* a filled buffer, so we don't need to perform an extra +* read early here. +*/ +if (contents) +return 0 ; + +/* Read entire file for all partial reads. */ + func = read_idmap[read_id] ?:FILE_CHECK ; +security_task_getsecid (current, &secid); +return process_measurement (file,current_cred (), secid,NULL , +0 ,MAY_READ , func); }const int read_idmap[READING_MAX_ID ] = {
Signature · 1 line
2.25.1
PATCH v4 14/17 fs/kernel_file_read: Add "offset" arg for partial reads
In reply to Kees CookTo perform partial reads, callers of kernel_read_file*() must have a non-NULL file_size argument and a preallocated buffer. The new "offset" argument can then be used to seek to specific locations in the file to fill the buffer to, at most, "buf_size" per call.
Where possible, the LSM hooks can report whether a full file has been read or not so that the contents can be reasoned about.
--- drivers/base/firmware_loader/main.c | 2 +- fs/kernel_read_file.c | 78 ++++++++++++++++++++--------- include/linux/kernel_read_file.h | 8 +-- kernel/kexec_file.c | 4 +- kernel/module.c | 2 +- security/integrity/digsig.c | 2 +- security/integrity/ima/ima_fs.c | 3 +- 7 files changed, 65 insertions(+), 34 deletions(-)
drivers/base/firmware_loader/main.c +1 -1
@@ -494,7 +494,7 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv, fw_priv->size =0 ;/* load firmware files from the mount namespace of init */ - rc =kernel_read_file_from_path_initns (path, &buffer, msize, + rc =kernel_read_file_from_path_initns (path,0 , &buffer, msize,NULL ,READING_FIRMWARE );if (rc <0 ) {
fs/kernel_read_file.c +54 -24
167 lines not on this page · raw message
include/linux/kernel_read_file.h +4 -4
@@ -35,19 +35,19 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)return kernel_read_file_str[id]; } -int kernel_read_file (struct file *file, +int kernel_read_file (struct file *file,loff_t offset,void **buf,size_t buf_size,size_t *file_size,enum kernel_read_file_id id); -int kernel_read_file_from_path (const char *path, +int kernel_read_file_from_path (const char *path,loff_t offset,void **buf,size_t buf_size,size_t *file_size,enum kernel_read_file_id id); -int kernel_read_file_from_path_initns (const char *path, +int kernel_read_file_from_path_initns (const char *path,loff_t offset,void **buf,size_t buf_size,size_t *file_size,enum kernel_read_file_id id); -int kernel_read_file_from_fd (int fd, +int kernel_read_file_from_fd (int fd,loff_t offset,void **buf,size_t buf_size,size_t *file_size,enum kernel_read_file_id id);
kernel/kexec_file.c +2 -2
@@ -221,7 +221,7 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,int ret;void *ldata; - ret =kernel_read_file_from_fd (kernel_fd, &image->kernel_buf, + ret =kernel_read_file_from_fd (kernel_fd,0 , &image->kernel_buf,INT_MAX ,NULL ,READING_KEXEC_IMAGE );if (ret <0 )return ret;
@@ -241,7 +241,7 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,#endif /* It is possible that there no initramfs is being loaded */ if (!(flags &KEXEC_FILE_NO_INITRAMFS )) { - ret =kernel_read_file_from_fd (initrd_fd, &image->initrd_buf, + ret =kernel_read_file_from_fd (initrd_fd,0 , &image->initrd_buf,INT_MAX ,NULL ,READING_KEXEC_INITRAMFS );if (ret <0 )
kernel/module.c +1 -1
@@ -4010,7 +4010,7 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) |MODULE_INIT_IGNORE_VERMAGIC))return -EINVAL ; - err =kernel_read_file_from_fd (fd, &hdr,INT_MAX ,NULL , + err =kernel_read_file_from_fd (fd,0 , &hdr,INT_MAX ,NULL ,READING_MODULE );if (err <0 )return err;
security/integrity/digsig.c +1 -1
@@ -175,7 +175,7 @@ int __init integrity_load_x509(const unsigned int id, const char *path)int rc;key_perm_t perm; - rc =kernel_read_file_from_path (path, &data,INT_MAX ,NULL , + rc =kernel_read_file_from_path (path,0 , &data,INT_MAX ,NULL ,READING_X509_CERTIFICATE );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);
security/integrity/ima/ima_fs.c +2 -1
@@ -284,7 +284,8 @@ static ssize_t ima_read_policy(char *path) datap = path;strsep (&datap,"\n" ); - rc =kernel_read_file_from_path (path, &data,INT_MAX ,NULL ,READING_POLICY ); + rc =kernel_read_file_from_path (path,0 , &data,INT_MAX ,NULL , +READING_POLICY );if (rc <0 ) {pr_err ("Unable to open file: %s (%d)" , path, rc);return rc;
Signature · 1 line
2.25.1
PATCH v4 15/17 firmware: Store opt_flags in fw_priv
In reply to Kees CookInstead of passing opt_flags around so much, store it in the private structure so it can be examined by internals without needing to add more arguments to functions.
--- drivers/base/firmware_loader/fallback.c | 11 +++----- drivers/base/firmware_loader/fallback.h | 5 ++-- .../base/firmware_loader/fallback_platform.c | 4 +-- drivers/base/firmware_loader/firmware.h | 3 ++- drivers/base/firmware_loader/main.c | 25 +++++++++++-------- 5 files changed, 25 insertions(+), 23 deletions(-)
drivers/base/firmware_loader/fallback.c +4 -7
@@ -490,13 +490,11 @@ fw_create_instance(struct firmware *firmware, const char *fw_name,/** * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism * @fw_sysfs: firmware sysfs information for the firmware to load -* @opt_flags: flags of options, FW_OPT_* * @timeout: timeout to wait for the load * * In charge of constructing a sysfs fallback interface for firmware loading. **/ -static int fw_load_sysfs_fallback (struct fw_sysfs *fw_sysfs, -u32 opt_flags,long timeout) +static int fw_load_sysfs_fallback (struct fw_sysfs *fw_sysfs,long timeout) {int retval =0 ;struct device *f_dev = &fw_sysfs->dev;
@@ -518,7 +516,7 @@ static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs,list_add (&fw_priv->pending_list, &pending_fw_head);mutex_unlock (&fw_lock); -if (opt_flags &FW_OPT_UEVENT ) { +if (fw_priv->opt_flags &FW_OPT_UEVENT ) { fw_priv->need_uevent = true;dev_set_uevent_suppress (f_dev, false);dev_dbg (f_dev,"firmware: requesting %s\n" , fw_priv->fw_name);
@@ -580,10 +578,10 @@ static int fw_load_from_user_helper(struct firmware *firmware, } fw_sysfs->fw_priv = firmware->priv; - ret =fw_load_sysfs_fallback (fw_sysfs, opt_flags, timeout); + ret =fw_load_sysfs_fallback (fw_sysfs, timeout);if (!ret) - ret =assign_fw (firmware, device, opt_flags); + ret =assign_fw (firmware, device); out_unlock:usermodehelper_read_unlock ();
@@ -625,7 +623,6 @@ static bool fw_run_sysfs_fallback(u32 opt_flags)* @fw: pointer to firmware image * @name: name of firmware file to look for * @device: device for which firmware is being loaded -* @opt_flags: options to control firmware loading behaviour * @ret: return value from direct lookup which triggered the fallback mechanism * * This function is called if direct lookup for the firmware failed, it enables
drivers/base/firmware_loader/fallback.h +2 -3
@@ -67,10 +67,9 @@ static inline void unregister_sysfs_loader(void)#endif /* CONFIG_FW_LOADER_USER_HELPER */ #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE -int firmware_fallback_platform (struct fw_priv *fw_priv,u32 opt_flags); +int firmware_fallback_platform (struct fw_priv *fw_priv);#else -static inline int firmware_fallback_platform (struct fw_priv *fw_priv, -u32 opt_flags) +static inline int firmware_fallback_platform (struct fw_priv *fw_priv) {return -ENOENT ; }
drivers/base/firmware_loader/fallback_platform.c +2 -2
@@ -8,13 +8,13 @@#include "fallback.h" #include "firmware.h" -int firmware_fallback_platform (struct fw_priv *fw_priv,u32 opt_flags) +int firmware_fallback_platform (struct fw_priv *fw_priv) {const u8 *data;size_t size;int rc; -if (!(opt_flags &FW_OPT_FALLBACK_PLATFORM )) +if (!(fw_priv->opt_flags &FW_OPT_FALLBACK_PLATFORM ))return -ENOENT ; rc =security_kernel_load_data (LOADING_FIRMWARE , true);
drivers/base/firmware_loader/firmware.h +2 -1
@@ -68,6 +68,7 @@ struct fw_priv {void *data;size_t size;size_t allocated_size; +u32 opt_flags;#ifdef CONFIG_FW_LOADER_PAGED_BUF bool is_paged_buf;struct page **pages;
@@ -136,7 +137,7 @@ static inline void fw_state_done(struct fw_priv *fw_priv)__fw_state_set (fw_priv,FW_STATUS_DONE ); } -int assign_fw (struct firmware *fw,struct device *device,u32 opt_flags); +int assign_fw (struct firmware *fw,struct device *device);#ifdef CONFIG_FW_LOADER_PAGED_BUF void fw_free_paged_buf (struct fw_priv *fw_priv);
drivers/base/firmware_loader/main.c +15 -10
@@ -168,7 +168,9 @@ static int fw_cache_piggyback_on_request(const char *name);static struct fw_priv *__allocate_fw_priv (const char *fw_name,struct firmware_cache *fwc, -void *dbuf,size_t size) +void *dbuf, +size_t size, +u32 opt_flags) {struct fw_priv *fw_priv;
@@ -186,6 +188,7 @@ static struct fw_priv *__allocate_fw_priv(const char *fw_name, fw_priv->fwc = fwc; fw_priv->data = dbuf; fw_priv->allocated_size = size; + fw_priv->opt_flags = opt_flags;fw_state_init (fw_priv);#ifdef CONFIG_FW_LOADER_USER_HELPER INIT_LIST_HEAD (&fw_priv->pending_list);
@@ -210,8 +213,10 @@ static struct fw_priv *__lookup_fw_priv(const char *fw_name)/* Returns 1 for batching firmware requests with the same name */ static int alloc_lookup_fw_priv (const char *fw_name,struct firmware_cache *fwc, -struct fw_priv **fw_priv,void *dbuf, -size_t size,u32 opt_flags) +struct fw_priv **fw_priv, +void *dbuf, +size_t size, +u32 opt_flags) {struct fw_priv *tmp;
@@ -227,7 +232,7 @@ static int alloc_lookup_fw_priv(const char *fw_name, } } - tmp =__allocate_fw_priv (fw_name, fwc, dbuf, size); + tmp =__allocate_fw_priv (fw_name, fwc, dbuf, size, opt_flags);if (tmp) {INIT_LIST_HEAD (&tmp->list);if (!(opt_flags &FW_OPT_NOCACHE ))
@@ -635,7 +640,7 @@ static int fw_add_devm_name(struct device *dev, const char *name) }#endif -int assign_fw (struct firmware *fw,struct device *device,u32 opt_flags) +int assign_fw (struct firmware *fw,struct device *device) {struct fw_priv *fw_priv = fw->priv;int ret;
@@ -654,8 +659,8 @@ int assign_fw(struct firmware *fw, struct device *device, u32 opt_flags)* should be fixed in devres or driver core. */ /* don't cache firmware handled without uevent */ -if (device && (opt_flags &FW_OPT_UEVENT ) && - !(opt_flags &FW_OPT_NOCACHE )) { +if (device && (fw_priv->opt_flags &FW_OPT_UEVENT ) && + !(fw_priv->opt_flags &FW_OPT_NOCACHE )) { ret =fw_add_devm_name (device, fw_priv->fw_name);if (ret) {mutex_unlock (&fw_lock);
@@ -667,7 +672,7 @@ int assign_fw(struct firmware *fw, struct device *device, u32 opt_flags)* After caching firmware image is started, let it piggyback * on request firmware. */ -if (!(opt_flags &FW_OPT_NOCACHE ) && +if (!(fw_priv->opt_flags &FW_OPT_NOCACHE ) && fw_priv->fwc->state ==FW_LOADER_START_CACHE ) {if (fw_cache_piggyback_on_request (fw_priv->fw_name))kref_get (&fw_priv->ref);
@@ -778,7 +783,7 @@ _request_firmware(const struct firmware **firmware_p, const char *name,#endif if (ret == -ENOENT ) - ret =firmware_fallback_platform (fw->priv, opt_flags); + ret =firmware_fallback_platform (fw->priv);if (ret) {if (!(opt_flags &FW_OPT_NO_WARN ))
@@ -787,7 +792,7 @@ _request_firmware(const struct firmware **firmware_p, const char *name, name, ret); ret =firmware_fallback_sysfs (fw, name, device, opt_flags, ret); }else - ret =assign_fw (fw, device, opt_flags); + ret =assign_fw (fw, device); out:if (ret <0 ) {
Signature · 1 line
2.25.1
PATCH v4 16/17 firmware: Add request_partial_firmware_into_buf()
In reply to Kees CookFrom: Scott Branden <[email protected]>
Add request_partial_firmware_into_buf() to allow for portions of a firmware file to be read into a buffer. This is needed when large firmware must be loaded in portions from a file on memory constrained systems.
--- drivers/base/firmware_loader/firmware.h | 4 + drivers/base/firmware_loader/main.c | 101 +++++++++++++++++++----- include/linux/firmware.h | 12 +++ 3 files changed, 99 insertions(+), 18 deletions(-)
drivers/base/firmware_loader/firmware.h +4 -0
@@ -32,6 +32,8 @@* @FW_OPT_FALLBACK_PLATFORM: Enable fallback to device fw copy embedded in * the platform's main firmware. If both this fallback and the sysfs * fallback are enabled, then this fallback will be tried first. +* @FW_OPT_PARTIAL: Allow partial read of firmware instead of needing to read +* entire file. */ enum fw_opt {FW_OPT_UEVENT =BIT (0 ),
@@ -41,6 +43,7 @@ enum fw_opt {FW_OPT_NOCACHE =BIT (4 ),FW_OPT_NOFALLBACK_SYSFS =BIT (5 ),FW_OPT_FALLBACK_PLATFORM =BIT (6 ), + FW_OPT_PARTIAL = BIT(7), }; enum fw_status {
@@ -68,6 +71,7 @@ struct fw_priv {void *data;size_t size;size_t allocated_size; +size_t offset;u32 opt_flags;#ifdef CONFIG_FW_LOADER_PAGED_BUF bool is_paged_buf;
drivers/base/firmware_loader/main.c +83 -18
229 lines not on this page · raw message
include/linux/firmware.h +12 -0
@@ -53,6 +53,9 @@ int request_firmware_direct(const struct firmware **fw, const char *name, struct device *device);int request_firmware_into_buf (const struct firmware **firmware_p,const char *name,struct device *device,void *buf,size_t size); +int request_partial_firmware_into_buf (const struct firmware **firmware_p, +const char *name,struct device *device, +void *buf,size_t size,size_t offset);void release_firmware (const struct firmware *fw);#else
@@ -102,6 +105,15 @@ static inline int request_firmware_into_buf(const struct firmware **firmware_p, return -EINVAL; } +static inline int request_partial_firmware_into_buf + (const struct firmware **firmware_p, +const char *name, +struct device *device, +void *buf,size_t size,size_t offset) +{ +return -EINVAL ; +} +#endif int firmware_request_cache (struct device *device,const char *name);
Signature · 1 line
2.25.1
PATCH v4 17/17 test_firmware: Test partial read support
In reply to Kees CookFrom: Scott Branden <[email protected]>
Add additional hooks to test_firmware to pass in support for partial file read using request_firmware_into_buf():
buf_size: size of buffer to request firmware into
partial: indicates that a partial file request is being made
file_offset: to indicate offset into file to request
Also update firmware selftests to use the new partial read test API.
--- lib/test_firmware.c | 154 ++++++++++++++++-- .../selftests/firmware/fw_filesystem.sh | 91 +++++++++++ 2 files changed, 233 insertions(+), 12 deletions(-)
lib/test_firmware.c +142 -12
@@ -50,6 +50,9 @@ struct test_batched_req {* @name: the name of the firmware file to look for * @into_buf: when the into_buf is used if this is true * request_firmware_into_buf() will be used instead. +* @buf_size: size of buf to allocate when into_buf is true +* @file_offset: file offset to request when calling request_firmware_into_buf +* @partial: partial read opt when calling request_firmware_into_buf * @sync_direct: when the sync trigger is used if this is true * request_firmware_direct() will be used instead. * @send_uevent: whether or not to send a uevent for async requests
@@ -89,6 +92,9 @@ struct test_batched_req {struct test_config {char *name;bool into_buf; +size_t buf_size; +size_t file_offset; +bool partial;bool sync_direct;bool send_uevent;u8 num_requests;
@@ -183,6 +189,9 @@ static int __test_firmware_config_init(void) test_fw_config->num_requests =TEST_FIRMWARE_NUM_REQS ; test_fw_config->send_uevent = true; test_fw_config->into_buf = false; + test_fw_config->buf_size =TEST_FIRMWARE_BUF_SIZE ; + test_fw_config->file_offset =0 ; + test_fw_config->partial = false; test_fw_config->sync_direct = false; test_fw_config->req_firmware = request_firmware; test_fw_config->test_result =0 ;
@@ -236,28 +245,35 @@ static ssize_t config_show(struct device *dev, dev_name(dev));if (test_fw_config->name) - len +=scnprintf (buf+len,PAGE_SIZE - len, + len +=scnprintf (buf + len,PAGE_SIZE - len,"name:\t%s\n" , test_fw_config->name);else - len +=scnprintf (buf+len,PAGE_SIZE - len, + len +=scnprintf (buf + len,PAGE_SIZE - len,"name:\tEMTPY\n" ); - len +=scnprintf (buf+len,PAGE_SIZE - len, + len +=scnprintf (buf + len,PAGE_SIZE - len,"num_requests:\t%u\n" , test_fw_config->num_requests); - len +=scnprintf (buf+len,PAGE_SIZE - len, + len +=scnprintf (buf + len,PAGE_SIZE - len,"send_uevent:\t\t%s\n" , test_fw_config->send_uevent ?"FW_ACTION_HOTPLUG" :"FW_ACTION_NOHOTPLUG" ); - len +=scnprintf (buf+len,PAGE_SIZE - len, + len +=scnprintf (buf + len,PAGE_SIZE - len,"into_buf:\t\t%s\n" , test_fw_config->into_buf ?"true" :"false" ); - len +=scnprintf (buf+len,PAGE_SIZE - len, + len +=scnprintf (buf + len,PAGE_SIZE - len, +"buf_size:\t%zu\n" , test_fw_config->buf_size); + len +=scnprintf (buf + len,PAGE_SIZE - len, +"file_offset:\t%zu\n" , test_fw_config->file_offset); + len +=scnprintf (buf + len,PAGE_SIZE - len, +"partial:\t\t%s\n" , + test_fw_config->partial ?"true" :"false" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"sync_direct:\t\t%s\n" , test_fw_config->sync_direct ?"true" :"false" ); - len +=scnprintf (buf+len,PAGE_SIZE - len, + len +=scnprintf (buf + len,PAGE_SIZE - len,"read_fw_idx:\t%u\n" , test_fw_config->read_fw_idx);mutex_unlock (&test_fw_mutex);
@@ -315,6 +331,30 @@ static ssize_t test_dev_config_show_bool(char *buf, bool val)return snprintf (buf,PAGE_SIZE ,"%d\n" , val); } +static int test_dev_config_update_size_t (const char *buf, +size_t size, +size_t *cfg) +{ +int ret; +long new; + + ret =kstrtol (buf,10 , &new); +if (ret) +return ret; + +mutex_lock (&test_fw_mutex); + *(size_t *)cfg = new; +mutex_unlock (&test_fw_mutex); + +/* Always return full write size even if we didn't consume all */ +return size; +} + +static ssize_t test_dev_config_show_size_t (char *buf,size_t val) +{ +return snprintf (buf,PAGE_SIZE ,"%zu\n" , val); +} +static ssize_t test_dev_config_show_int (char *buf,int val) {return snprintf (buf,PAGE_SIZE ,"%d\n" , val);
@@ -400,6 +440,83 @@ static ssize_t config_into_buf_show(struct device *dev, }static DEVICE_ATTR_RW (config_into_buf); +static ssize_t config_buf_size_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +int rc; + +mutex_lock (&test_fw_mutex); +if (test_fw_config->reqs) { +pr_err ("Must call release_all_firmware prior to changing config\n" ); + rc = -EINVAL ; +mutex_unlock (&test_fw_mutex); + goto out; + } +mutex_unlock (&test_fw_mutex); + + rc =test_dev_config_update_size_t (buf, count, + &test_fw_config->buf_size); + +out: +return rc; +} + +static ssize_t config_buf_size_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return test_dev_config_show_size_t (buf, test_fw_config->buf_size); +} +static DEVICE_ATTR_RW (config_buf_size); + +static ssize_t config_file_offset_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +int rc; + +mutex_lock (&test_fw_mutex); +if (test_fw_config->reqs) { +pr_err ("Must call release_all_firmware prior to changing config\n" ); + rc = -EINVAL ; +mutex_unlock (&test_fw_mutex); + goto out; + } +mutex_unlock (&test_fw_mutex); + + rc =test_dev_config_update_size_t (buf, count, + &test_fw_config->file_offset); + +out: +return rc; +} + +static ssize_t config_file_offset_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return test_dev_config_show_size_t (buf, test_fw_config->file_offset); +} +static DEVICE_ATTR_RW (config_file_offset); + +static ssize_t config_partial_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +return test_dev_config_update_bool (buf, + count, + &test_fw_config->partial); +} + +static ssize_t config_partial_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return test_dev_config_show_bool (buf, test_fw_config->partial); +} +static DEVICE_ATTR_RW (config_partial); +static ssize_t config_sync_direct_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count)
@@ -655,11 +772,21 @@ static int test_fw_run_batch_request(void *data)if (!test_buf)return -ENOSPC ; - req->rc =request_firmware_into_buf (&req->fw, - req->name, - req->dev, - test_buf, -TEST_FIRMWARE_BUF_SIZE ); +if (test_fw_config->partial) + req->rc =request_partial_firmware_into_buf + (&req->fw, + req->name, + req->dev, + test_buf, + test_fw_config->buf_size, + test_fw_config->file_offset); +else + req->rc =request_firmware_into_buf + (&req->fw, + req->name, + req->dev, + test_buf, + test_fw_config->buf_size);if (!req->fw)kfree (test_buf); }else {
@@ -932,6 +1059,9 @@ static struct attribute *test_dev_attrs[] = {TEST_FW_DEV_ATTR (config_name),TEST_FW_DEV_ATTR (config_num_requests),TEST_FW_DEV_ATTR (config_into_buf), +TEST_FW_DEV_ATTR (config_buf_size), +TEST_FW_DEV_ATTR (config_file_offset), +TEST_FW_DEV_ATTR (config_partial),TEST_FW_DEV_ATTR (config_sync_direct),TEST_FW_DEV_ATTR (config_send_uevent), TEST_FW_DEV_ATTR(config_read_fw_idx),
tools/testing/selftests/firmware/fw_filesystem.sh +91 -0
127 lines not on this page · raw message
Signature · 1 line
2.25.1