firmware: add request_partial_firmware_into_buf
Subsystems (16)
Scott Branden
maintainerv10 cover firmware: add request_partial_firmware_into_buf
This patch series adds partial read support via a new call request_partial_firmware_into_buf.
Such support is needed when the whole file is not needed and/or only a smaller portion of the file will fit into allocated memory at any one time.
In order to accept the enhanced API it has been requested that kernel selftests and upstreamed driver utilize the API enhancement and so are included in this patch series.
Also in this patch series is the addition of a new Broadcom VK driver utilizing the new request_firmware_into_buf enhanced API.
Further comment followed to add IMA support of the partial reads originating from request_firmware_into_buf calls. And another request to move existing kernel_read_file* functions to its own include file.
Changes from v9:
- add patch to move existing kernel_read_file* to its own include file
- driver fixes
Changes from v8:
- correct compilation error when CONFIG_FW_LOADER not defined
Changes from v7:
- removed swiss army knife kernel_pread_* style approach
and simply add offset parameter in addition to those needed in kernel_read_* functions thus removing need for kernel_pread enum
Changes from v6:
- update ima_post_read_file check on IMA_FIRMWARE_PARTIAL_READ
- adjust new driver i2c-slave-eeprom.c use of request_firmware_into_buf
- remove an extern
Changes from v5:
- add IMA FIRMWARE_PARTIAL_READ support
- change kernel pread flags to enum
- removed legacy support from driver
- driver fixes
Changes from v4:
- handle reset issues if card crashes
- allow driver to have min required msix
- add card utilization information
Changes from v3:
- fix sparse warnings
- fix printf format specifiers for size_t
- fix 32-bit cross-compiling reports 32-bit shifts
- use readl/writel,_relaxed to access pci ioremap memory, removed memory barriers and volatile keyword with such change
- driver optimizations for interrupt/poll functionalities
Changes from v2:
- remove unnecessary code and mutex locks in lib/test_firmware.c
- remove VK_IOCTL_ACCESS_BAR support from driver and use pci sysfs instead
- remove bitfields
- remove Kconfig default m
- adjust formatting and some naming based on feedback
- fix error handling conditions
- use appropriate return codes
- use memcpy_toio instead of direct access to PCIE bar
Scott Branden (9):
fs: move kernel_read_file* to its own include file
fs: introduce kernel_pread_file* support
firmware: add request_partial_firmware_into_buf
test_firmware: add partial read support for request_firmware_into_buf
firmware: test partial file reads of request_partial_firmware_into_buf
bcm-vk: add bcm_vk UAPI
misc: bcm-vk: add Broadcom VK driver
MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver
ima: add FIRMWARE_PARTIAL_READ support
MAINTAINERS | 7 + drivers/base/firmware_loader/firmware.h | 5 + drivers/base/firmware_loader/main.c | 80 +- drivers/misc/Kconfig | 1 + drivers/misc/Makefile | 1 + drivers/misc/bcm-vk/Kconfig | 29 + drivers/misc/bcm-vk/Makefile | 11 + drivers/misc/bcm-vk/bcm_vk.h | 419 +++++ drivers/misc/bcm-vk/bcm_vk_dev.c | 1357 +++++++++++++++ drivers/misc/bcm-vk/bcm_vk_msg.c | 1504 +++++++++++++++++ drivers/misc/bcm-vk/bcm_vk_msg.h | 211 +++ drivers/misc/bcm-vk/bcm_vk_sg.c | 275 +++ drivers/misc/bcm-vk/bcm_vk_sg.h | 61 + drivers/misc/bcm-vk/bcm_vk_tty.c | 352 ++++ fs/exec.c | 92 +- include/linux/firmware.h | 12 + include/linux/fs.h | 39 - include/linux/ima.h | 1 + include/linux/kernel_read_file.h | 69 + include/linux/security.h | 1 + include/uapi/linux/misc/bcm_vk.h | 99 ++ kernel/kexec_file.c | 1 + kernel/module.c | 1 + lib/test_firmware.c | 154 +- security/integrity/digsig.c | 1 + security/integrity/ima/ima_fs.c | 1 + security/integrity/ima/ima_main.c | 25 +- security/integrity/ima/ima_policy.c | 1 + security/loadpin/loadpin.c | 1 + security/security.c | 1 + security/selinux/hooks.c | 1 + .../selftests/firmware/fw_filesystem.sh | 80 + 32 files changed, 4802 insertions(+), 91 deletions(-) create mode 100644 drivers/misc/bcm-vk/Kconfig create mode 100644 drivers/misc/bcm-vk/Makefile create mode 100644 drivers/misc/bcm-vk/bcm_vk.h create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c create mode 100644 include/linux/kernel_read_file.h create mode 100644 include/uapi/linux/misc/bcm_vk.h
Signature · 1 line
2.17.1
Scott Branden
maintainerPATCH v10 1/9 fs: move kernel_read_file* to its own include file
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 | 39 ---------------------- include/linux/ima.h | 1 + include/linux/kernel_read_file.h | 52 +++++++++++++++++++++++++++++ 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, 65 insertions(+), 39 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 -39
@@ -2857,45 +2857,6 @@ static inline void i_readcount_inc(struct inode *inode)#endif extern int do_pipe_flags (int *,int ); -#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) \ - 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 *);extern ssize_t kernel_write (struct file *,const void *,size_t ,loff_t *);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 +52 -0
52 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/blkdev.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.17.1
Re: [PATCH v10 1/9] fs: move kernel_read_file* to its own include file
Quoted 18 lines · Scott Branden
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.Suggested-by Christoph Hellwig <[email protected]>Signed-off-by Scott Branden <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- drivers/base/firmware_loader/main.c | 1 + fs/exec.c | 1 + include/linux/fs.h | 39 ---------------------- include/linux/ima.h | 1 + include/linux/kernel_read_file.h | 52 +++++++++++++++++++++++++++++ include/linux/security.h | 1 + kernel/kexec_file.c | 1 + kernel/module.c | 1 + security/integrity/digsig.c | 1 +
Quoted 8 lines
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, 65 insertions(+), 39 deletions(-) create mode 100644 include/linux/kernel_read_file.h
This looks like too many files are getting touched. If it got added to security.h, very few of the above .c files will need it explicitly added (maybe none). You can test future versions of this change with an allmodconfig build and make sure you have a matching .o for each .c file that calls kernel_read_file(). :)
But otherwise, sure, seems good.
Signature · 1 line
Kees Cook
Scott Branden
maintainerRe: [PATCH v10 1/9] fs: move kernel_read_file* to its own include file
Quoted 27 lines · Kees Cook
Quoted 25 lines · Scott Branden
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.Suggested-by Christoph Hellwig <[email protected]>Signed-off-by Scott Branden <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- drivers/base/firmware_loader/main.c | 1 + fs/exec.c | 1 + include/linux/fs.h | 39 ---------------------- include/linux/ima.h | 1 + include/linux/kernel_read_file.h | 52 +++++++++++++++++++++++++++++ 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, 65 insertions(+), 39 deletions(-)Quoted 1 line
create mode 100644
include/linux/kernel_read_file.h
Quoted 3 lines
This looks like too many files are getting touched. If it got added to
security.h, very few of the above .c files will need it explicitly added (maybe none).
Some people want the header file added to each file that uses it, others want it in a common header file. I tried to add it to each file that uses it. But if the other approach is to be followed that could be done.
Quoted 6 lines
You can test future versions of this change with an allmodconfig build and make sure you have a matching .o for each .c file that calls
kernel_read_file(). :)But otherwise, sure, seems good.
Scott Branden
maintainerPATCH v10 2/9 fs: introduce kernel_pread_file* support
In reply to Scott BrandenAdd kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file.
--- fs/exec.c | 93 ++++++++++++++++++++++++-------- include/linux/kernel_read_file.h | 17 ++++++ 2 files changed, 87 insertions(+), 23 deletions(-)
fs/exec.c +70 -23
@@ -928,10 +928,14 @@ struct file *open_exec(const char *name) }EXPORT_SYMBOL (open_exec); -int kernel_read_file (struct file *file,void **buf,loff_t *size, -loff_t max_size,enum kernel_read_file_id id) -{ -loff_t i_size, pos; +int kernel_pread_file (struct file *file,void **buf,loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) +{ +loff_t alloc_size; +loff_t buf_pos; +loff_t read_end; +loff_t i_size;ssize_t bytes =0 ;int ret;
@@ -951,21 +955,32 @@ 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)) { + +/* Default read to end of file */ + read_end = i_size; + +/* Allow reading partial portion of file */ +if ((id ==READING_FIRMWARE_PARTIAL_READ ) && + (i_size > (pos + max_size))) + read_end = pos + max_size; + + alloc_size = read_end - pos; +if (i_size >SIZE_MAX || (max_size >0 && alloc_size > max_size)) { ret = -EFBIG ; goto out; } -if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) - *buf =vmalloc (i_size); +if ((id !=READING_FIRMWARE_PARTIAL_READ ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER )) + *buf =vmalloc (alloc_size);if (!*buf) { ret = -ENOMEM ; goto out; } - pos =0 ; -while (pos < i_size) { - bytes =kernel_read (file, *buf + pos, i_size - pos, &pos); + buf_pos =0 ; +while (pos < read_end) { + bytes =kernel_read (file, *buf + buf_pos, read_end - pos, &pos);if (bytes <0 ) { ret = bytes; goto out_free;
@@ -973,20 +988,23 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,if (bytes ==0 )break ; + + buf_pos += bytes; } -if (pos != i_size) { +if (pos != read_end) { ret = -EIO ; goto out_free; } - ret =security_kernel_post_read_file (file, *buf, i_size, id); + ret =security_kernel_post_read_file (file, *buf, alloc_size, id);if (!ret) *size = pos; out_free:if (ret <0 ) { -if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) { +if ((id !=READING_FIRMWARE_PARTIAL_READ ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER )) {vfree (*buf); *buf =NULL ; }
@@ -996,10 +1014,18 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,allow_write_access (file);return ret; } + +int kernel_read_file (struct file *file,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id id) +{ +return kernel_pread_file (file, buf, size, max_size,0 , id); +}EXPORT_SYMBOL_GPL (kernel_read_file); -int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, -loff_t max_size,enum kernel_read_file_id id) +int kernel_pread_file_from_path (const char *path,void **buf, +loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct file *file;int ret;
@@ -1011,15 +1037,22 @@ 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_pread_file (file, buf, size, max_size, pos, id);fput (file);return ret; } + +int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id id) +{ +return kernel_pread_file_from_path (path, buf, size, max_size,0 , id); +}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, -enum kernel_read_file_id id) +int kernel_pread_file_from_path_initns (const char *path,void **buf, +loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct file *file;struct path root;
@@ -1037,14 +1070,22 @@ 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_pread_file (file, buf, size, max_size, pos, id);fput (file);return ret; } + +int kernel_read_file_from_path_initns (const char *path,void **buf, +loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +return kernel_pread_file_from_path_initns (path, buf, size, max_size,0 , id); +}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, -enum kernel_read_file_id id) +int kernel_pread_file_from_fd (int fd,void **buf,loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct fd f =fdget (fd);int ret = -EBADF ;
@@ -1052,11 +1093,17 @@ 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_pread_file (f.file, buf, size, max_size, pos, id); out:fdput (f);return ret; } + +int kernel_read_file_from_fd (int fd,void **buf,loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +return kernel_pread_file_from_fd (fd, buf, size, max_size,0 , id); +}EXPORT_SYMBOL_GPL (kernel_read_file_from_fd); #if defined(CONFIG_HAVE_AOUT) || defined(CONFIG_BINFMT_FLAT) || \
include/linux/kernel_read_file.h +17 -0
@@ -8,6 +8,7 @@#define __kernel_read_file_id (id) \ id(UNKNOWN, unknown) \ id(FIRMWARE, firmware) \ + id(FIRMWARE_PARTIAL_READ, firmware) \ id(FIRMWARE_PREALLOC_BUFFER, firmware) \ id(FIRMWARE_EFI_EMBEDDED, firmware) \ id(MODULE, kernel-module) \
@@ -36,15 +37,31 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)return kernel_read_file_str[id]; } +int kernel_pread_file (struct file *file, +void **buf,loff_t *size,loff_t pos, +loff_t max_size, +enum kernel_read_file_id id);int kernel_read_file (struct file *file,void **buf,loff_t *size,loff_t max_size,enum kernel_read_file_id id); +int kernel_pread_file_from_path (const char *path, +void **buf,loff_t *size,loff_t pos, +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,enum kernel_read_file_id id); +int kernel_pread_file_from_path_initns (const char *path, +void **buf,loff_t *size,loff_t pos, +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,enum kernel_read_file_id id); +int kernel_pread_file_from_fd (int fd, +void **buf,loff_t *size,loff_t pos, +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,enum kernel_read_file_id id);
Signature · 1 line
2.17.1
Re: [PATCH v10 2/9] fs: introduce kernel_pread_file* support
Quoted 35 lines · Scott Branden
Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file.
Signed-off-by Scott Branden <[email protected]>--- fs/exec.c | 93 ++++++++++++++++++++++++-------- include/linux/kernel_read_file.h | 17 ++++++ 2 files changed, 87 insertions(+), 23 deletions(-)fs/exec.c +8
-4@@ -928,10 +928,14 @@ struct file *open_exec(const char *name) }EXPORT_SYMBOL (open_exec); -int kernel_read_file (struct file *file,void **buf,loff_t *size, -loff_t max_size,enum kernel_read_file_id id) -{ -loff_t i_size, pos; +int kernel_pread_file (struct file *file,void **buf,loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) +{ +loff_t alloc_size; +loff_t buf_pos; +loff_t read_end; +loff_t i_size;ssize_t bytes =0 ;int ret;@@ -951,21 +955,32 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, ret = -EINVAL ; goto out;
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 965
Quoted 10 lines
fs/exec.c +8
-1} -if (i_size >SIZE_MAX || (max_size >0 && i_size > max_size)) { + +/* Default read to end of file */ + read_end = i_size; + +/* Allow reading partial portion of file */ +if ((id ==READING_FIRMWARE_PARTIAL_READ ) && + (i_size > (pos + max_size))) + read_end = pos + max_size;
There's no need to involve "id" here. There are other signals about what's happening (i.e. pos != 0, max_size != i_size, etc).
Quoted 6 lines
fs/exec.c +3
-0+ + alloc_size = read_end - pos; +if (i_size >SIZE_MAX || (max_size >0 && alloc_size > max_size)) { ret = -EFBIG ; goto out; }
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 979
Quoted 10 lines
fs/exec.c +3
-2-if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) - *buf =vmalloc (i_size); +if ((id !=READING_FIRMWARE_PARTIAL_READ ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER )) + *buf =vmalloc (alloc_size);if (!*buf) { ret = -ENOMEM ; goto out; }
The id usage here was a mistake in upstream, and the series I sent is trying to clean that up.
Greg, it seems this series is going to end up in your tree due to it being drivers/misc? I guess I need to direct my series to Greg then, but get LSM folks Acks.
Quoted 18 lines
fs/exec.c +5
-3- pos =0 ; -while (pos < i_size) { - bytes =kernel_read (file, *buf + pos, i_size - pos, &pos); + buf_pos =0 ; +while (pos < read_end) { + bytes =kernel_read (file, *buf + buf_pos, read_end - pos, &pos);if (bytes <0 ) { ret = bytes; goto out_free;@@ -973,20 +988,23 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,if (bytes ==0 )break ; + + buf_pos += bytes; }
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 1002
Quoted 10 lines
fs/exec.c +2
-2-if (pos != i_size) { +if (pos != read_end) { ret = -EIO ; goto out_free; } - ret =security_kernel_post_read_file (file, *buf, i_size, id); + ret =security_kernel_post_read_file (file, *buf, alloc_size, id);if (!ret) *size = pos;
This call cannot be inside kernel_pread_file(): any future LSMs will see a moving window of contents, etc. It'll need to be in kernel_read_file() proper.
Quoted 90 lines
fs/exec.c +35
-11out_free:if (ret <0 ) { -if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) { +if ((id !=READING_FIRMWARE_PARTIAL_READ ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER )) {vfree (*buf); *buf =NULL ; }@@ -996,10 +1014,18 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,allow_write_access (file);return ret; } + +int kernel_read_file (struct file *file,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id id) +{ +return kernel_pread_file (file, buf, size, max_size,0 , id); +}EXPORT_SYMBOL_GPL (kernel_read_file); -int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, -loff_t max_size,enum kernel_read_file_id id) +int kernel_pread_file_from_path (const char *path,void **buf, +loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct file *file;int ret;@@ -1011,15 +1037,22 @@ 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_pread_file (file, buf, size, max_size, pos, id);fput (file);return ret; } + +int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id id) +{ +return kernel_pread_file_from_path (path, buf, size, max_size,0 , id); +}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, -enum kernel_read_file_id id) +int kernel_pread_file_from_path_initns (const char *path,void **buf, +loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct file *file;struct path root;@@ -1037,14 +1070,22 @@ 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_pread_file (file, buf, size, max_size, pos, id);fput (file);return ret; } + +int kernel_read_file_from_path_initns (const char *path,void **buf, +loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +return kernel_pread_file_from_path_initns (path, buf, size, max_size,0 , id); +}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, -enum kernel_read_file_id id) +int kernel_pread_file_from_fd (int fd,void **buf,loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct fd f =fdget (fd);int ret = -EBADF ;@@ -1052,11 +1093,17 @@ 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_pread_file (f.file, buf, size, max_size, pos, id); out:
fs/exec.c int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size, · line 1107
Quoted 10 lines
fs/exec.c +6
-0fdput (f);return ret; } + +int kernel_read_file_from_fd (int fd,void **buf,loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +return kernel_pread_file_from_fd (fd, buf, size, max_size,0 , id); +}EXPORT_SYMBOL_GPL (kernel_read_file_from_fd);
For each of these execution path, the mapping to LSM hooks is:
- all path must call security_kernel_read_file(file, id) before reading (this appears to be fine as-is in your series).
- anything doing a "full" read needs to call security_kernel_post_read_file() with the file and full buffer, size, etc (so all the kernel_read_file*() paths). I imagine instead of adding 3 copy/pasted versions of this, it may be possible to refactor the helpers into a single core "full" caller that takes struct file, or doing some logic in kernel_pread_file() that notices it has the entire file in the buffer and doing the call then.
As an example of what I mean about doing the call, here's how I might imagine it for one of the paths if it took struct file:
int kernel_read_file_from_file(struct file *file, void **buf, loff_t *size,
loff_t max_size, enum kernel_read_file_id id)
{
int ret;
ret = kernel_pread_file_from_file(file, buf, size, max_size, 0, id);
if (ret)
return ret;
return security_kernel_post_read_file(file, buf, *size, id);
}include/linux/kernel_read_file.h line 13
Quoted 13 lines
fs/exec.c +0
-0#if defined(CONFIG_HAVE_AOUT) || defined(CONFIG_BINFMT_FLAT) || \include/linux/kernel_read_file.h +1
-0@@ -8,6 +8,7 @@#define __kernel_read_file_id (id) \ id(UNKNOWN, unknown) \ id(FIRMWARE, firmware) \ + id(FIRMWARE_PARTIAL_READ, firmware) \ id(FIRMWARE_PREALLOC_BUFFER, firmware) \ id(FIRMWARE_EFI_EMBEDDED, firmware) \
And again, sorry that this was in here as a misleading example.
Quoted 23 lines
include/linux/kernel_read_file.h +12
-0id (MODULE , kernel-module) \@@ -36,15 +37,31 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)return kernel_read_file_str[id]; } +int kernel_pread_file (struct file *file, +void **buf,loff_t *size,loff_t pos, +loff_t max_size, +enum kernel_read_file_id id);int kernel_read_file (struct file *file,void **buf,loff_t *size,loff_t max_size,enum kernel_read_file_id id); +int kernel_pread_file_from_path (const char *path, +void **buf,loff_t *size,loff_t pos, +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,enum kernel_read_file_id id); +int kernel_pread_file_from_path_initns (const char *path, +void **buf,loff_t *size,loff_t pos, +loff_t max_size, +enum kernel_read_file_id id);
include/linux/kernel_read_file.h static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id) · line 67
Quoted 10 lines
include/linux/kernel_read_file.h +4
-0int kernel_read_file_from_path_initns (const char *path,void **buf,loff_t *size,loff_t max_size,enum kernel_read_file_id id); +int kernel_pread_file_from_fd (int fd, +void **buf,loff_t *size,loff_t pos, +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,enum kernel_read_file_id id);
I remain concerned that adding these helpers will lead a poor interaction with LSMs, but I guess I get to hold my tongue. :)
Signature · 1 line
Kees Cook
Mimi Zohar
maintainerRe: [PATCH v10 2/9] fs: introduce kernel_pread_file* support
Quoted 8 lines · Kees Cook
Quoted 8 lines
fs/exec.c +3
-1@@ -951,21 +955,32 @@ 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)) { + + /* Default read to end of file */ + read_end = i_size;
Quoted 8 lines
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 965
Quoted 5 lines
fs/exec.c +5
-0+ + /* Allow reading partial portion of file */ + if ((id == READING_FIRMWARE_PARTIAL_READ) && + (i_size > (pos + max_size))) + read_end = pos + max_size;There's no need to involve "id" here. There are other signals about what's happening (i.e. pos != 0,
max_size!=i_size, etc).
Both the pre and post security kernel_read_file hooks are called here, but there isn't enough information being passed to the LSM/IMA to be able to different which hook is applicable. One method of providing that additional information is by enumeration. The other option would be to pass some additional information.
For example, on the post kernel_read_file hook, the file is read once into memory. IMA calculates the firmware file hash based on the buffer contents. On the pre kernel_read_file hook, IMA would need to read the entire file, calculating the file hash. Both methods of calculating the file hash work, but the post hook is more efficient.
Mimi
Scott Branden
maintainerRe: [PATCH v10 2/9] fs: introduce kernel_pread_file* support
In reply to Kees CookQuoted 33 lines · Kees Cook
Quoted 32 lines · Scott Branden
Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file.
Signed-off-by Scott Branden <[email protected]>--- fs/exec.c | 93 ++++++++++++++++++++++++-------- include/linux/kernel_read_file.h | 17 ++++++ 2 files changed, 87 insertions(+), 23 deletions(-)fs/exec.c +8
-4@@ -928,10 +928,14 @@ struct file *open_exec(const char *name) }EXPORT_SYMBOL (open_exec); -int kernel_read_file (struct file *file,void **buf,loff_t *size, -loff_t max_size,enum kernel_read_file_id id) -{ -loff_t i_size, pos; +int kernel_pread_file (struct file *file,void **buf,loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) +{ +loff_t alloc_size; +loff_t buf_pos; +loff_t read_end; +loff_t i_size;ssize_t bytes =0 ;int ret;
Quoted 15 lines
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 965
Quoted 13 lines
fs/exec.c +8
-1@@ -951,21 +955,32 @@ 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)) { + +/* Default read to end of file */ + read_end = i_size; + +/* Allow reading partial portion of file */ +if ((id ==READING_FIRMWARE_PARTIAL_READ ) && + (i_size > (pos + max_size))) + read_end = pos + max_size;There's no need to involve "id" here. There are other signals about what's happening (i.e. pos != 0,
max_size!=i_size, etc).
There are other signals other than the fact that kernel_read_file requires
the entire file to be read while kernel_pread_file allows partial files to be read.
So if you do a pread at pos = 0 you need another key to indicate it is "ok" if max_size < i_size.
If id == READING_FIRMWARE_PARTIAL_READ is removed (and we want to share 99% of the code
between kernel_read_file and kernel_pread_file then I need to add another parameter to a common function
called between these functions. And adding another parameter was rejected previously in the review as a "swiss army knife approach" by another reviewer. I am happy to add it back in because it is necessary to share code and differentiate whether we are performing a partial read or not.
Quoted 8 lines
Quoted 8 lines
fs/exec.c +3
-0+ + alloc_size = read_end - pos; +if (i_size >SIZE_MAX || (max_size >0 && alloc_size > max_size)) { ret = -EFBIG ; goto out; }
Quoted 11 lines
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 979
Quoted 9 lines
fs/exec.c +3
-2-if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) - *buf =vmalloc (i_size); +if ((id !=READING_FIRMWARE_PARTIAL_READ ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER )) + *buf =vmalloc (alloc_size);if (!*buf) { ret = -ENOMEM ; goto out; }The id usage here was a mistake in upstream, and the series I sent is trying to clean that up.
I see that cleanup and it works fine with the pread. Other than I need some sort of key to share code and indicate whether it is "ok" to do a partial read of the file or not.
Quoted 29 lines
Greg, it seems this series is going to end up in your tree due to it being
drivers/misc? I guess I need to direct my series to Greg then, but get LSM folks Acks.Quoted 24 lines
fs/exec.c +6
-4- pos =0 ; -while (pos < i_size) { - bytes =kernel_read (file, *buf + pos, i_size - pos, &pos); + buf_pos =0 ; +while (pos < read_end) { + bytes =kernel_read (file, *buf + buf_pos, read_end - pos, &pos);if (bytes <0 ) { ret = bytes; goto out_free;@@ -973,20 +988,23 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,if (bytes ==0 )break ; + + buf_pos += bytes; } -if (pos != i_size) { +if (pos != read_end) { ret = -EIO ; goto out_free; }
Quoted 7 lines
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 1002
Quoted 4 lines
fs/exec.c +1
-1- ret =security_kernel_post_read_file (file, *buf, i_size, id); + ret =security_kernel_post_read_file (file, *buf, alloc_size, id);if (!ret) *size = pos;This call cannot be inside
kernel_pread_file(): any future LSMs will see a moving window of contents, etc. It'll need to be inkernel_read_file()proper.
If IMA still passes (after testing my next patch series with your changes and my modifications) I will need some more help here.
Quoted 169 lines
Quoted 91 lines
fs/exec.c +35
-11out_free:if (ret <0 ) { -if (id !=READING_FIRMWARE_PREALLOC_BUFFER ) { +if ((id !=READING_FIRMWARE_PARTIAL_READ ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER )) {vfree (*buf); *buf =NULL ; }@@ -996,10 +1014,18 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,allow_write_access (file);return ret; } + +int kernel_read_file (struct file *file,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id id) +{ +return kernel_pread_file (file, buf, size, max_size,0 , id); +}EXPORT_SYMBOL_GPL (kernel_read_file); -int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, -loff_t max_size,enum kernel_read_file_id id) +int kernel_pread_file_from_path (const char *path,void **buf, +loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct file *file;int ret;@@ -1011,15 +1037,22 @@ 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_pread_file (file, buf, size, max_size, pos, id);fput (file);return ret; } + +int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id id) +{ +return kernel_pread_file_from_path (path, buf, size, max_size,0 , id); +}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, -enum kernel_read_file_id id) +int kernel_pread_file_from_path_initns (const char *path,void **buf, +loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct file *file;struct path root;@@ -1037,14 +1070,22 @@ 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_pread_file (file, buf, size, max_size, pos, id);fput (file);return ret; } + +int kernel_read_file_from_path_initns (const char *path,void **buf, +loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +return kernel_pread_file_from_path_initns (path, buf, size, max_size,0 , id); +}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, -enum kernel_read_file_id id) +int kernel_pread_file_from_fd (int fd,void **buf,loff_t *size, +loff_t max_size,loff_t pos, +enum kernel_read_file_id id) {struct fd f =fdget (fd);int ret = -EBADF ;@@ -1052,11 +1093,17 @@ 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_pread_file (f.file, buf, size, max_size, pos, id); out:fs/exec.c int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size, · line 1107
Quoted 10 lines
fs/exec.c +6
-0fdput (f);return ret; } + +int kernel_read_file_from_fd (int fd,void **buf,loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +return kernel_pread_file_from_fd (fd, buf, size, max_size,0 , id); +}EXPORT_SYMBOL_GPL (kernel_read_file_from_fd);For each of these execution path, the mapping to LSM hooks is:
- all path must call
security_kernel_read_file(file, id)before reading (this appears to be fine as-is in your series).- anything doing a "full" read needs to call
security_kernel_post_read_file()with the file and full buffer, size, etc (so all the kernel_read_file*() paths). I imagine instead of adding 3 copy/pasted versions of this, it may be possible to refactor the helpers into a single core "full" caller that takes struct file, or doing some logic inkernel_pread_file()that notices it has the entire file in the buffer and doing the call then.
As an example of what I mean about doing the call, here's how I might imagine it for one of the paths if it took struct file:int kernel_read_file_from_file(struct file *file, void **buf,
loff_t*size,
loff_tmax_size, enumkernel_read_file_idid){ int ret; ret = kernel_pread_file_from_file(file, buf, size, max_size, 0, id); if (ret) return ret; return security_kernel_post_read_file(file, buf, *size, id); }include/linux/kernel_read_file.h line 13
Quoted 13 lines
fs/exec.c +0
-0#if defined(CONFIG_HAVE_AOUT) || defined(CONFIG_BINFMT_FLAT) || \include/linux/kernel_read_file.h +1
-0@@ -8,6 +8,7 @@#define __kernel_read_file_id (id) \ id(UNKNOWN, unknown) \ id(FIRMWARE, firmware) \ + id(FIRMWARE_PARTIAL_READ, firmware) \ id(FIRMWARE_PREALLOC_BUFFER, firmware) \ id(FIRMWARE_EFI_EMBEDDED, firmware) \And again, sorry that this was in here as a misleading example.
Quoted 27 lines
include/linux/kernel_read_file.h +13
-0id (MODULE , kernel-module) \@@ -36,15 +37,31 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)return kernel_read_file_str[id]; } +int kernel_pread_file (struct file *file, +void **buf,loff_t *size,loff_t pos, +loff_t max_size, +enum kernel_read_file_id id);int kernel_read_file (struct file *file,void **buf,loff_t *size,loff_t max_size,enum kernel_read_file_id id); +int kernel_pread_file_from_path (const char *path, +void **buf,loff_t *size,loff_t pos, +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,enum kernel_read_file_id id); +int kernel_pread_file_from_path_initns (const char *path, +void **buf,loff_t *size,loff_t pos, +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,enum kernel_read_file_id id); +int kernel_pread_file_from_fd(int fd,
Quoted 8 lines
include/linux/kernel_read_file.h static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id) · line 67
Quoted 6 lines
include/linux/kernel_read_file.h +3
-0+ void **buf, loff_t *size, loff_t pos, + 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,enum kernel_read_file_id id);I remain concerned that adding these helpers will lead a poor interaction with LSMs, but I guess I get to hold my tongue. :)
We could add pread functions that are "unsafe" in nature instead then?
As I certainly do not need any integrity checks on the file for my driver. The real check is done by the card the data is loaded to whether is passes the linux security checks or not. And then, if someone does want to do something "safe" with preads another kernel_read_file_securelock/unlock could be added for those that need security for their partial reads?
Quoted 1 line
Scott Branden
maintainerRe: [PATCH v10 2/9] fs: introduce kernel_pread_file* support
Hi Kees,
one more comment below.
Quoted 110 lines · Scott Branden
Quoted 39 lines · Kees Cook
Quoted 38 lines · Scott Branden
Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file.
Signed-off-by Scott Branden <[email protected]>---
fs/exec.c | 93
++++++++++++++++++++++++--------
include/linux/kernel_read_file.h| 17 ++++++2 files changed, 87 insertions(+), 23 deletions(-)fs/exec.c +8
-3@@ -928,10 +928,14 @@ struct file *open_exec(const char *name) } EXPORT_SYMBOL(open_exec); -int kernel_read_file(struct file *file, void **buf, loff_t *size, - loff_t max_size, enum kernel_read_file_id id) -{ - loff_t i_size, pos; +int kernel_pread_file(struct file *file, void **buf, loff_t *size, + loff_t max_size, loff_t pos, + enum kernel_read_file_id id) +{ + loff_t alloc_size; + loff_t buf_pos; + loff_t read_end; + loff_t i_size; ssize_t bytes = 0; int ret; @@ -951,21 +955,32 @@ 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)) { +
+ /* Default read to end of file */Quoted 8 lines
Quoted 6 lines
+
read_end=i_size;
+
+ /* Allow reading partial portion of file */
+ if ((id ==READING_FIRMWARE_PARTIAL_READ) &&
+ (i_size> (pos +max_size)))
+read_end= pos +max_size;There's no need to involve "id" here. There are other signals about what's happening (i.e. pos != 0,
max_size!=i_size, etc).There are other signals other than the fact that
kernel_read_filerequires
the entire file to be read whilekernel_pread_fileallows partial files to be read.
So if you do a pread at pos = 0 you need another key to indicate it is "ok" ifmax_size<i_size.
If id ==READING_FIRMWARE_PARTIAL_READis removed (and we want to share 99% of the code
betweenkernel_read_fileandkernel_pread_filethen I need to add another parameter to a common function
called between these functions. And adding another parameter was rejected previously in the review as a "swiss army knife approach" by another reviewer. I am happy to add it back in because it is necessary to share code and differentiate whether we are performing a partial read or not.Quoted 17 lines
Quoted 17 lines
+
+alloc_size=read_end- pos;
+ if (i_size>SIZE_MAX|| (max_size> 0 &&alloc_size>max_size)) {
ret =-EFBIG;
goto out;
}
- if (id !=READING_FIRMWARE_PREALLOC_BUFFER) - *buf =vmalloc(i_size);
+ if ((id !=READING_FIRMWARE_PARTIAL_READ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER)) + *buf =vmalloc(alloc_size);
if (!*buf) {
ret =-ENOMEM;
goto out;
}Quoted 2 lines
The id usage here was a mistake in upstream, and the series I sent is trying to clean that up.
I see that cleanup and it works fine with the pread. Other than I need some sort of key to share code and indicate whether it is "ok" to do a partial read of the file or not.
Quoted 26 lines
Greg, it seems this series is going to end up in your tree due to it being
drivers/misc? I guess I need to direct my series to Greg then, but get LSM folks Acks.Quoted 21 lines
- pos = 0;
- while (pos <i_size) {
- bytes =kernel_read(file, *buf + pos, i_size - pos, &pos); +buf_pos= 0;
+ while (pos <read_end) {
+ bytes =kernel_read(file, *buf + buf_pos, read_end - pos, &pos);
if (bytes < 0) {
ret = bytes;
gotoout_free;fs/exec.c +3
-0@@ -973,20 +988,23 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, if (bytes == 0) break; + + buf_pos += bytes; } - if (pos != i_size) { + if (pos != read_end) { ret = -EIO; goto out_free;Quoted 8 lines
fs/exec.c int kernel_read_file(struct file *file, void **buf, loff_t *size, · line 1002
Quoted 5 lines
fs/exec.c +1
-0} - ret = security_kernel_post_read_file(file, *buf, i_size, id); + ret = security_kernel_post_read_file(file, *buf, alloc_size, id); if (!ret) *size = pos;This call cannot be inside
kernel_pread_file(): any future LSMs will see a moving window of contents, etc. It'll need to be inkernel_read_file()proper.
Quoted 179 lines
If IMA still passes (after testing my next patch series with your changes and my modifications) I will need some more help here.
Quoted 177 lines
Quoted 101 lines
out_free:
if (ret < 0) {
- if (id !=READING_FIRMWARE_PREALLOC_BUFFER) { + if ((id !=READING_FIRMWARE_PARTIAL_READ) && + (id !=READING_FIRMWARE_PREALLOC_BUFFER)) {vfree(*buf);
*buf = NULL;
}fs/exec.c +10
-1@@ -996,10 +1014,18 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, allow_write_access(file); return ret; } + +int kernel_read_file (struct file *file,void **buf,loff_t *size, + loff_t max_size, enum kernel_read_file_id id) +{ + return kernel_pread_file(file, buf, size, max_size, 0, id); +} EXPORT_SYMBOL_GPL(kernel_read_file); -int kernel_read_file_from_path(const char *path, void **buf, loff_t *size, - loff_t max_size, enum kernel_read_file_id id) +int kernel_pread_file_from_path(const char *path, void **buf, + loff_t *size, + loff_t max_size, loff_t pos, + enum kernel_read_file_id id) { struct file *file;int ret;
fs/exec.c +11
-2@@ -1011,15 +1037,22 @@ 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_pread_file(file, buf, size, max_size, pos, id); fput(file); return ret; } + +int kernel_read_file_from_path (const char *path,void **buf,loff_t *size, + loff_t max_size, enum kernel_read_file_id id) +{ + return kernel_pread_file_from_path(path, buf, size, max_size,0 , id); +} 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, - enum kernel_read_file_id id) +int kernel_pread_file_from_path_initns(const char *path, void **buf, + loff_t *size, + loff_t max_size, loff_t pos, + enum kernel_read_file_id id){
struct file *file;
struct path root;fs/exec.c +11
-1@@ -1037,14 +1070,22 @@ 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_pread_file(file, buf, size, max_size, pos, id); fput(file); return ret; } + +int kernel_read_file_from_path_initns (const char *path,void **buf, + loff_t *size, loff_t max_size, + enum kernel_read_file_id id) +{ + return kernel_pread_file_from_path_initns(path, buf, size, max_size,0 , id); +} 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, - enum kernel_read_file_id id) +int kernel_pread_file_from_fd(int fd, void **buf, loff_t *size, + loff_t max_size, loff_t pos, + enum kernel_read_file_id id) {struct fd f = fdget(fd);
int ret =-EBADF;fs/exec.c +1
-0@@ -1052,11 +1093,17 @@ 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_pread_file(f.file, buf, size, max_size, pos, id);out:
fdput(f);
return ret;
}fs/exec.c +2
-0+ +int kernel_read_file_from_fd(int fd, void **buf, loff_t *size,
loff_tmax_size,
+ enumkernel_read_file_idid)fs/exec.c +3
-0+{ + return kernel_pread_file_from_fd(fd, buf, size, max_size, 0, id); +}Quoted 1 line
EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);For each of these execution path, the mapping to LSM hooks is:
- all path must call
security_kernel_read_file(file, id)before reading (this appears to be fine as-is in your series).- anything doing a "full" read needs to call
security_kernel_post_read_file()with the file and full buffer, size, etc (so all the kernel_read_file*() paths). I imagine instead of adding 3 copy/pasted versions of this, it may be possible to refactor the helpers into a single core "full" caller that takes struct file, or doing some logic inkernel_pread_file()that notices it has the entire file in the buffer and doing the call then.
As an example of what I mean about doing the call, here's how I might imagine it for one of the paths if it took struct file:int
kernel_read_file_from_file(struct file *file, void **buf, loff_t *size, loff_t max_size, enum kernel_read_file_id id){
int ret;ret =
kernel_pread_file_from_file(file, buf, size, max_size, 0, id); if (ret)
return ret;
returnsecurity_kernel_post_read_file(file, buf, *size, id); }include/linux/kernel_read_file.h line 13
Quoted 12 lines
#if
defined(CONFIG_HAVE_AOUT)||defined(CONFIG_BINFMT_FLAT)|| \include/linux/kernel_read_file.h +1
-0@@ -8,6 +8,7 @@ #define __kernel_read_file_id(id) \ id(UNKNOWN, unknown) \ id(FIRMWARE, firmware) \ + id(FIRMWARE_PARTIAL_READ, firmware) \ id(FIRMWARE_PREALLOC_BUFFER, firmware) \ id(FIRMWARE_EFI_EMBEDDED, firmware) \And again, sorry that this was in here as a misleading example.
Quoted 32 lines
include/linux/kernel_read_file.h +15
-0id(MODULE, kernel-module) \@@ -36,15 +37,31 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id) return kernel_read_file_str[id]; } +int kernel_pread_file(struct file *file, + void **buf, loff_t *size, loff_t pos, + loff_t max_size, + enum kernel_read_file_id id); int kernel_read_file(struct file *file, void **buf, loff_t *size, loff_t max_size, enum kernel_read_file_id id); +int kernel_pread_file_from_path (const char *path, + void **buf, loff_t *size, loff_t pos, + 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, enum kernel_read_file_id id); +int kernel_pread_file_from_path_initns (const char *path, + void **buf, loff_t *size, loff_t pos, + 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, enum kernel_read_file_id id); +int kernel_pread_file_from_fd (int fd, + void **buf, loff_t *size, loff_t pos, + 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,Quoted 1 line
enum
kernel_read_file_idid);I remain concerned that adding these helpers will lead a poor interaction with LSMs, but I guess I get to hold my tongue. :)
I only need kernel_pread_file and kernel_pread_file_from_path_initns. kernel_pread_file_from_fd and kernel_pread_file_from_path were only added for completeness. And are really only helper functions called by their kernel_read_file* counterparts at this time. So they can be removed from this patch if that helps?
Quoted 5 lines
We could add pread functions that are "unsafe" in nature instead then?
As I certainly do not need any integrity checks on the file for my driver. The real check is done by the card the data is loaded to whether is passes the linux security checks or not. And then, if someone does want to do something "safe" with preads another kernel_read_file_securelock/unlock could be added for those that need security for their partial reads?Quoted 1 line
Scott Branden
maintainerPATCH v10 3/9 firmware: add request_partial_firmware_into_buf
In reply to Scott BrandenAdd request_partial_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.
--- drivers/base/firmware_loader/firmware.h | 5 ++ drivers/base/firmware_loader/main.c | 79 +++++++++++++++++++------ include/linux/firmware.h | 12 ++++ 3 files changed, 79 insertions(+), 17 deletions(-)
drivers/base/firmware_loader/firmware.h +5 -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,8 @@ 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;struct page **pages;
drivers/base/firmware_loader/main.c +62 -17
181 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.17.1
kernel test robot
build botI love your patch! Yet something to improve:
Hi Scott,
I love your patch! Yet something to improve:
[auto build test ERROR on next-20200706]
[also build test ERROR on v5.8-rc4]
[cannot apply to char-misc/char-misc-testing integrity/next-integrity driver-core/driver-core-testing linus/master v5.8-rc4 v5.8-rc3 v5.8-rc2] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Scott-Branden/firmware-add-request_partial_firmware_into_buf/20200707-072632 base: 5680d14d59bddc8bcbc5badf00dbbd4374858497 config: powerpc-rhel-kconfig (attached as .config) compiler: powerpc64le-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=powerpc
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>, old ones prefixed by <<):
>> ERROR: modpost: "kernel_pread_file_from_path_initns" [drivers/base/firmware_loader/firmware_class.ko] undefined!
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 3 lines · Scott Branden
Add
request_partial_firmware_into_bufto allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.
Just tear out the differing "id" and just use FW_OPT_PARTIAL and I think if Luis is happy, you're all set.
Signature · 1 line
Kees Cook
Scott Branden
maintainerQuoted 7 lines · Kees Cook
Quoted 3 lines · Scott Branden
Add
request_partial_firmware_into_bufto allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.Just tear out the differing "id" and just use
FW_OPT_PARTIALand I think if Luis is happy, you're all set.
I hope so. Also, I will need to call kernel_pread_file_from_path_initns() if FW_OPT_PARTIAL is set
and kernel_read_file_from_path_initns() otherwise to avoid a swiss army-knife approach of calling a common function with multiple parameters.
Scott Branden
maintainerPATCH v10 4/9 test_firmware: add partial read support for request_firmware_into_buf
In reply to Scott BrandenAdd 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 madefile_offset: to indicate offset into file to request
--- lib/test_firmware.c | 154 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 142 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)
@@ -650,11 +767,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 {
@@ -927,6 +1054,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),
Signature · 1 line
2.17.1
Re: [PATCH v10 4/9] test_firmware: add partial read support for request_firmware_into_buf
Quoted 7 lines · Scott Branden
Add additional hooks to
test_firmwareto pass in support for partial file read usingrequest_firmware_into_buf.buf_size: size of buffer to request firmware into
partial: indicates that a partial file request is being madefile_offset: to indicate offset into file to requestSigned-off-by Scott Branden <[email protected]>
I am a fan of tests. :) If Luis gives an Ack here, you're good.
Signature · 1 line
Kees Cook
Scott Branden
maintainerRe: [PATCH v10 4/9] test_firmware: add partial read support for request_firmware_into_buf
Quoted 9 lines · Kees Cook
Quoted 7 lines · Scott Branden
Add additional hooks to
test_firmwareto pass in support for partial file read usingrequest_firmware_into_buf.buf_size: size of buffer to request firmware into
partial: indicates that a partial file request is being madefile_offset: to indicate offset into file to requestSigned-off-by Scott Branden <[email protected]>I am a fan of tests. :) If Luis gives an Ack here, you're good.
There were not even any tests for request_firmware_into_buf before I started this partial read support. Fortunately those base changes have already been accepted so I think this change is a simple addition to those accepted patches.
Quoted 1 line
Scott Branden
maintainerPATCH v10 5/9 firmware: test partial file reads of request_partial_firmware_into_buf
In reply to Scott BrandenAdd firmware tests for partial file reads ofrequest_partial_firmware_into_buf.
--- .../selftests/firmware/fw_filesystem.sh | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+)
tools/testing/selftests/firmware/fw_filesystem.sh +80 -0
@@ -149,6 +149,26 @@ config_unset_into_buf() echo 0 > $DIR/config_into_buf } +config_set_buf_size() +{ + echo $1 > $DIR/config_buf_size +} + +config_set_file_offset() +{ + echo $1 > $DIR/config_file_offset +} + +config_set_partial() +{ + echo 1 > $DIR/config_partial +} + +config_unset_partial() +{ + echo 0 > $DIR/config_partial +} + config_set_sync_direct() { echo 1 > $DIR/config_sync_direct
@@ -207,6 +227,35 @@ read_firmwares() done } +read_partial_firmwares() +{ + if [ "$(cat $DIR/config_into_buf)" == "1" ]; then + fwfile="${FW_INTO_BUF}" + else + fwfile="${FW}" + fi + + if [ "$1" = "xzonly" ]; then + fwfile="${fwfile}-orig" + fi + + # Strip fwfile down to match partial offset and length + partial_data="$(cat $fwfile)" + partial_data="${partial_data:$2:$3}" + + for i in $(seq 0 3); do + config_set_read_fw_idx $i + + read_firmware="$(cat $DIR/read_firmware)" + + # Verify the contents are what we expect. + if [ $read_firmware != $partial_data ]; then + echo "request #$i: partial firmware was not loaded" >&2 + exit 1 + fi + done +} + read_firmwares_expect_nofile() { for i in $(seq 0 3); do
@@ -319,6 +368,21 @@ test_batched_request_firmware_into_buf() echo "OK" } +test_batched_request_partial_firmware_into_buf() +{ + echo -n "Batched request_partial_firmware_into_buf() $2 off=$3 size=$4 try #$1: " + config_reset + config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME + config_set_into_buf + config_set_partial + config_set_buf_size $4 + config_set_file_offset $3 + config_trigger_sync + read_partial_firmwares $2 $3 $4 + release_all_firmware + echo "OK" +} + test_batched_request_firmware_direct() { echo -n "Batched request_firmware_direct() $2 try #$1: "
@@ -371,6 +435,22 @@ for i in $(seq 1 5); do test_batched_request_firmware_into_buf $i normal done +for i in $(seq 1 5); do + test_batched_request_partial_firmware_into_buf $i normal 0 10 +done + +for i in $(seq 1 5); do + test_batched_request_partial_firmware_into_buf $i normal 0 5 +done + +for i in $(seq 1 5); do + test_batched_request_partial_firmware_into_buf $i normal 1 6 +done + +for i in $(seq 1 5); do + test_batched_request_partial_firmware_into_buf $i normal 2 10 +done + for i in $(seq 1 5); do test_batched_request_firmware_direct $i normal done
Signature · 1 line
2.17.1
Scott Branden
maintainerPATCH v10 6/9 bcm-vk: add bcm_vk UAPI
In reply to Scott BrandenAdd user space api for bcm-vk driver.
--- include/uapi/linux/misc/bcm_vk.h | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 include/uapi/linux/misc/bcm_vk.h
include/uapi/linux/misc/bcm_vk.h new file +99 -0
@@ -0,0 +1,99 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */ +/* +* Copyright 2018-2020 Broadcom. +*/ + +#ifndef __UAPI_LINUX_MISC_BCM_VK_H +#define __UAPI_LINUX_MISC_BCM_VK_H + +#include <linux/ioctl.h> +#include <linux/types.h> + +#define BCM_VK_MAX_FILENAME 64 + +struct vk_image { +__u32 type;/* Type of image */ +#define VK_IMAGE_TYPE_BOOT1 1/* 1st stage (load to SRAM) */ +#define VK_IMAGE_TYPE_BOOT2 2/* 2nd stage (load to DDR) */ +char filename[BCM_VK_MAX_FILENAME ];/* Filename of image */ +}; + +struct vk_reset { +__u32 arg1; +__u32 arg2; +}; + +#define VK_MAGIC 0x5e + +/* Load image to Valkyrie */ +#define VK_IOCTL_LOAD_IMAGE _IOW(VK_MAGIC, 0x2, struct vk_image) + +/* Send Reset to Valkyrie */ +#define VK_IOCTL_RESET _IOW(VK_MAGIC, 0x4, struct vk_reset) + +/* +* message block - basic unit in the message where a message's size is always +* N x sizeof(basic_block) +*/ +struct vk_msg_blk { +__u8 function_id; +#define VK_FID_TRANS_BUF 5 +#define VK_FID_SHUTDOWN 8 +__u8 size; +__u16 trans_id;/* transport id, queue & msg_id */ +__u32 context_id; +__u32 args[2 ]; +#define VK_CMD_PLANES_MASK 0x000f/* number of planes to up/download */ +#define VK_CMD_UPLOAD 0x0400/* memory transfer to vk */ +#define VK_CMD_DOWNLOAD 0x0500/* memory transfer from vk */ +#define VK_CMD_MASK 0x0f00/* command mask */ +}; + +#define VK_BAR_FWSTS 0x41c +#define VK_BAR_COP_FWSTS 0x428 +/* VK_FWSTS definitions */ +#define VK_FWSTS_RELOCATION_ENTRY BIT(0) +#define VK_FWSTS_RELOCATION_EXIT BIT(1) +#define VK_FWSTS_INIT_START BIT(2) +#define VK_FWSTS_ARCH_INIT_DONE BIT(3) +#define VK_FWSTS_PRE_KNL1_INIT_DONE BIT(4) +#define VK_FWSTS_PRE_KNL2_INIT_DONE BIT(5) +#define VK_FWSTS_POST_KNL_INIT_DONE BIT(6) +#define VK_FWSTS_INIT_DONE BIT(7) +#define VK_FWSTS_APP_INIT_START BIT(8) +#define VK_FWSTS_APP_INIT_DONE BIT(9) +#define VK_FWSTS_MASK 0xffffffff +#define VK_FWSTS_READY (VK_FWSTS_INIT_START | \ + VK_FWSTS_ARCH_INIT_DONE | \ + VK_FWSTS_PRE_KNL1_INIT_DONE | \ + VK_FWSTS_PRE_KNL2_INIT_DONE | \ + VK_FWSTS_POST_KNL_INIT_DONE | \ + VK_FWSTS_INIT_DONE | \ + VK_FWSTS_APP_INIT_START | \ + VK_FWSTS_APP_INIT_DONE) +/* Deinit */ +#define VK_FWSTS_APP_DEINIT_START BIT(23) +#define VK_FWSTS_APP_DEINIT_DONE BIT(24) +#define VK_FWSTS_DRV_DEINIT_START BIT(25) +#define VK_FWSTS_DRV_DEINIT_DONE BIT(26) +#define VK_FWSTS_RESET_DONE BIT(27) +#define VK_FWSTS_DEINIT_TRIGGERED (VK_FWSTS_APP_DEINIT_START | \ + VK_FWSTS_APP_DEINIT_DONE | \ + VK_FWSTS_DRV_DEINIT_START | \ + VK_FWSTS_DRV_DEINIT_DONE) +/* Last nibble for reboot reason */ +#define VK_FWSTS_RESET_REASON_SHIFT 28 +#define VK_FWSTS_RESET_REASON_MASK (0xf << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_SYS_PWRUP (0x0 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_MBOX_DB (0x1 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_M7_WDOG (0x2 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_TEMP (0x3 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_PCI_FLR (0x4 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_PCI_HOT (0x5 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_PCI_WARM (0x6 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_PCI_COLD (0x7 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_L1 (0x8 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_L0 (0x9 << VK_FWSTS_RESET_REASON_SHIFT) +#define VK_FWSTS_RESET_UNKNOWN (0xf << VK_FWSTS_RESET_REASON_SHIFT) + +#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
Signature · 1 line
2.17.1
Scott Branden
maintainerPATCH v10 7/9 misc: bcm-vk: add Broadcom VK driver
In reply to Scott BrandenAdd Broadcom VK driver offload engine.
This driver interfaces to the VK PCIe offload engine to perform should offload functions as video transcoding on multiple streams in parallel. VK device is booted from files loaded using request_firmware_into_buf mechanism. After booted card status is updated and messages can then be sent to the card.
Such messages contain scatter gather list of addresses
to pull data from the host to perform operations on.
--- drivers/misc/Kconfig | 1 + drivers/misc/Makefile | 1 + drivers/misc/bcm-vk/Kconfig | 29 + drivers/misc/bcm-vk/Makefile | 11 + drivers/misc/bcm-vk/bcm_vk.h | 419 +++++++++ drivers/misc/bcm-vk/bcm_vk_dev.c | 1357 +++++++++++++++++++++++++++ drivers/misc/bcm-vk/bcm_vk_msg.c | 1504 ++++++++++++++++++++++++++++++ drivers/misc/bcm-vk/bcm_vk_msg.h | 211 +++++ drivers/misc/bcm-vk/bcm_vk_sg.c | 275 ++++++ drivers/misc/bcm-vk/bcm_vk_sg.h | 61 ++ drivers/misc/bcm-vk/bcm_vk_tty.c | 352 +++++++ 11 files changed, 4221 insertions(+) create mode 100644 drivers/misc/bcm-vk/Kconfig create mode 100644 drivers/misc/bcm-vk/Makefile create mode 100644 drivers/misc/bcm-vk/bcm_vk.h create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c
drivers/misc/Kconfig +1 -0
@@ -469,6 +469,7 @@ source "drivers/misc/genwqe/Kconfig" source "drivers/misc/echo/Kconfig" source "drivers/misc/cxl/Kconfig" source "drivers/misc/ocxl/Kconfig" +source "drivers/misc/bcm-vk/Kconfig" source "drivers/misc/cardreader/Kconfig" source "drivers/misc/habanalabs/Kconfig" source "drivers/misc/uacce/Kconfig"
drivers/misc/Makefile +1 -0
@@ -52,6 +52,7 @@ obj-$(CONFIG_ECHO) += echo/ obj-$(CONFIG_CXL_BASE) += cxl/ obj-$(CONFIG_PCI_ENDPOINT_TEST) += pci_endpoint_test.o obj-$(CONFIG_OCXL) += ocxl/ +obj-$(CONFIG_BCM_VK) += bcm-vk/ obj-y += cardreader/ obj-$(CONFIG_PVPANIC) += pvpanic.o obj-$(CONFIG_HABANA_AI) += habanalabs/
drivers/misc/bcm-vk/Kconfig new file +29 -0
@@ -0,0 +1,29 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Broadcom VK device +# +config BCM_VK + tristate "Support for Broadcom VK Accelerators" + depends on PCI_MSI + help + Select this option to enable support for Broadcom + VK Accelerators. VK is used for performing + specific offload processing. + This driver enables userspace programs to access these + accelerators via /dev/bcm-vk.N devices. + + If unsure, say N. + +if BCM_VK + +config BCM_VK_QSTATS + bool "VK Queue Statistics" + help + Turn on to enable Queue Statistics. + These are useful for debugging purposes. + Some performance loss by enabling this debug config. + For properly operating PCIe hardware no need to enable this. + + If unsure, say N. + +endif
drivers/misc/bcm-vk/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for Broadcom VK driver +# + +obj-$(CONFIG_BCM_VK) += bcm_vk.o +bcm_vk-objs := \ + bcm_vk_dev.o \ + bcm_vk_msg.o \ + bcm_vk_sg.o \ + bcm_vk_tty.o
drivers/misc/bcm-vk/bcm_vk.h new file +419 -0
419 lines not on this page · raw message
drivers/misc/bcm-vk/bcm_vk_dev.c new file +1357 -0
1,357 lines not on this page · raw message
drivers/misc/bcm-vk/bcm_vk_msg.c new file +1504 -0
1,170 lines not on this page · raw message
drivers/misc/bcm-vk/bcm_vk_msg.h new file +211 -0
211 lines not on this page · raw message
drivers/misc/bcm-vk/bcm_vk_sg.c new file +275 -0
275 lines not on this page · raw message
drivers/misc/bcm-vk/bcm_vk_sg.h new file +61 -0
61 lines not on this page · raw message
drivers/misc/bcm-vk/bcm_vk_tty.c new file +352 -0
352 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.17.1
Part of the diff is not shown here. View the raw message.
Re: [PATCH v10 7/9] misc: bcm-vk: add Broadcom VK driver
Quoted 11 lines · Scott Branden
Add Broadcom VK driver offload engine.
This driver interfaces to the VK PCIe offload engine to perform should offload functions as video transcoding on multiple streams in parallel. VK device is booted from files loaded usingrequest_firmware_into_bufmechanism. After booted card status is updated and messages can then be sent to the card.
Such messages contain scatter gather list of addresses
to pull data from the host to perform operations on.Signed-off-by Scott Branden <[email protected]>Signed-off-by Desmond Yan <[email protected]>
nit: your S-o-b chain doesn't make sense (I would expect you at the end since you're sending it and showing as the Author). Is it Co-developed-by?
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by
Quoted 11 lines
[...]
drivers/misc/bcm-vk/bcm_vk_dev.c new file +10
-0+ + max_buf =SZ_4M ; + bufp =dma_alloc_coherent (dev, + max_buf, + &boot_dma_addr,GFP_KERNEL ); +if (!bufp) { +dev_err (dev,"Error allocating 0x%zx\n" , max_buf); + ret = -ENOMEM ; + goto err_buf_out; + }
drivers/misc/bcm-vk/bcm_vk_dev.c line 538
Quoted 10 lines
drivers/misc/bcm-vk/bcm_vk_dev.c new file +10
-0+ +bcm_vk_buf_notify (vk, bufp, boot_dma_addr, max_buf); + }else { +dev_err (dev,"Error invalid image type 0x%x\n" , load_type); + ret = -EINVAL ; + goto err_buf_out; + } + + ret =request_partial_firmware_into_buf (&fw, filename, dev, + bufp, max_buf,0 );
Unless I don't understand what's happening here, this needs to be reordered if you're going to keep Mimi happy and disallow the device being able to see the firmware before it has been verified. (i.e. please load the firmware before mapping DMA across the buffer.)
Signature · 1 line
Kees Cook
Scott Branden
maintainerRe: [PATCH v10 7/9] misc: bcm-vk: add Broadcom VK driver
Quoted 10 lines · Kees Cook
Quoted 9 lines · Scott Branden
Add Broadcom VK driver offload engine.
This driver interfaces to the VK PCIe offload engine to perform should offload functions as video transcoding on multiple streams in parallel. VK device is booted from files loaded usingrequest_firmware_into_bufmechanism. After booted card status is updated and messages can then be sent to the card.
Such messages contain scatter gather list of addresses
to pull data from the host to perform operations on.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Scott Branden <[email protected]>Signed-off-by Desmond Yan <[email protected]>nit: your S-o-b chain doesn't make sense (I would expect you at the end since you're sending it and showing as the Author). Is it Co-developed-by?
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by
Yes, Co-developed-by. Will adjust.
Quoted 18 lines
Quoted 18 lines
[...]
drivers/misc/bcm-vk/bcm_vk_dev.c new file +16
-0+ + max_buf =SZ_4M ; + bufp =dma_alloc_coherent (dev, + max_buf, + &boot_dma_addr,GFP_KERNEL ); +if (!bufp) { +dev_err (dev,"Error allocating 0x%zx\n" , max_buf); + ret = -ENOMEM ; + goto err_buf_out; + } + +bcm_vk_buf_notify (vk, bufp, boot_dma_addr, max_buf); + }else { +dev_err (dev,"Error invalid image type 0x%x\n" , load_type); + ret = -EINVAL ; + goto err_buf_out;
Quoted 8 lines
drivers/misc/bcm-vk/bcm_vk_dev.c line 538
Quoted 4 lines
drivers/misc/bcm-vk/bcm_vk_dev.c new file +4
-0+ } + + ret =request_partial_firmware_into_buf (&fw, filename, dev, + bufp, max_buf,0 );Unless I don't understand what's happening here, this needs to be reordered if you're going to keep Mimi happy and disallow the device being able to see the firmware before it has been verified. (i.e. please load the firmware before mapping DMA across the buffer.)
I don't understand your concern here. We request partial firmware into a buffer that we allocated. After loading it we signal the card the firmware has been loaded into that memory region. The card then pulls the data into its internal memory. And, authenticates it.
Even if the card randomly read and writes to that buffer it shouldn't matter to the linux kernel security subsystem.
It passed the security check already when placed in the buffer.
If there is a concern could we add an "nosecurity" request_partial_firmware_into_buf instead as there is no need for any security on this particular request?
kernel test robot
build botRe: [PATCH v10 7/9] misc: bcm-vk: add Broadcom VK driver
In reply to Scott BrandenI love your patch! Yet something to improve:
Hi Scott,
I love your patch! Yet something to improve:
[auto build test ERROR on next-20200706]
[also build test ERROR on v5.8-rc5]
[cannot apply to char-misc/char-misc-testing integrity/next-integrity driver-core/driver-core-testing linus/master v5.8-rc4 v5.8-rc3 v5.8-rc2] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Scott-Branden/firmware-add-request_partial_firmware_into_buf/20200707-072632 base: 5680d14d59bddc8bcbc5badf00dbbd4374858497 config: alpha-allyesconfig (attached as .config) compiler: alpha-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=alpha
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
drivers/misc/bcm-vk/bcm_vk_dev.c:138:5: warning: no previous prototype for 'bcm_vk_intf_ver_chk' [-Wmissing-prototypes]
138 | int bcm_vk_intf_ver_chk(struct bcm_vk *vk)
| ^~~~~~~~~~~~~~~~~~~
drivers/misc/bcm-vk/bcm_vk_dev.c: In function 'bcm_vk_probe':
>> drivers/misc/bcm-vk/bcm_vk_dev.c:1161:8: error: implicit declaration of function 'bcm_vk_sysfs_init'; did you mean 'bcm_vk_msg_init'? [-Werror=implicit-function-declaration]
1161 | err = bcm_vk_sysfs_init(pdev, misc_device);
| ^~~~~~~~~~~~~~~~~
| bcm_vk_msg_init
drivers/misc/bcm-vk/bcm_vk_dev.c: In function 'bcm_vk_remove':
>> drivers/misc/bcm-vk/bcm_vk_dev.c:1266:2: error: implicit declaration of function 'bcm_vk_sysfs_exit'; did you mean 'bcm_vk_tty_exit'? [-Werror=implicit-function-declaration]
1266 | bcm_vk_sysfs_exit(pdev, misc_device);
| ^~~~~~~~~~~~~~~~~
| bcm_vk_tty_exitcc1: some warnings being treated as errors
vim +1161 drivers/misc/bcm-vk/bcm_vk_dev.c
1004
1005 static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1006 {
1007 int err;
1008 int i;
1009 int id;
1010 int irq;
1011 char name[20];
1012 struct bcm_vk *vk;
1013 struct device *dev = &pdev->dev;
1014 struct miscdevice *misc_device;
1015 uint32_t boot_status;
1016
1017 /* allocate vk structure which is tied to kref for freeing */
1018 vk = kzalloc(sizeof(*vk), GFP_KERNEL);
1019 if (!vk)
1020 return -ENOMEM;
1021
1022 kref_init(&vk->kref);
1023 if (nr_ib_sgl_blk > BCM_VK_IB_SGL_BLK_MAX) {
1024 dev_warn(dev, "Inband SGL blk %d limited to max %d\n",
1025 nr_ib_sgl_blk, BCM_VK_IB_SGL_BLK_MAX);
1026 nr_ib_sgl_blk = BCM_VK_IB_SGL_BLK_MAX;
1027 }
1028 vk->ib_sgl_size = nr_ib_sgl_blk * VK_MSGQ_BLK_SIZE;
1029 vk->pdev = pdev;
1030 mutex_init(&vk->mutex);
1031
1032 err = pci_enable_device(pdev);
1033 if (err) {
1034 dev_err(dev, "Cannot enable PCI device\n");
1035 return err;
1036 }
1037
1038 err = pci_request_regions(pdev, DRV_MODULE_NAME);
1039 if (err) {
1040 dev_err(dev, "Cannot obtain PCI resources\n");
1041 goto err_disable_pdev;
1042 }
1043
1044 /* make sure DMA is good */
1045 err = dma_set_mask_and_coherent(&pdev->dev,
1046 DMA_BIT_MASK(BCM_VK_DMA_BITS));
1047 if (err) {
1048 dev_err(dev, "failed to set DMA mask\n");
1049 goto err_disable_pdev;
1050 }
1051
1052 /* The tdma is a scratch area for some DMA testings. */
1053 if (nr_scratch_pages) {
1054 vk->tdma_vaddr = dma_alloc_coherent
1055 (dev,
1056 nr_scratch_pages * PAGE_SIZE,
1057 &vk->tdma_addr, GFP_KERNEL);
1058 if (!vk->tdma_vaddr) {
1059 err = -ENOMEM;
1060 goto err_disable_pdev;
1061 }
1062 }
1063
1064 pci_set_master(pdev);
1065 pci_set_drvdata(pdev, vk);
1066
1067 irq = pci_alloc_irq_vectors(pdev,
1068 1,
1069 VK_MSIX_IRQ_MAX,
1070 PCI_IRQ_MSI | PCI_IRQ_MSIX);
1071
1072 if (irq < VK_MSIX_IRQ_MIN_REQ) {
1073 dev_err(dev, "failed to get min %d MSIX interrupts, irq(%d)\n",
1074 VK_MSIX_IRQ_MIN_REQ, irq);
1075 err = (irq >= 0) ? -EINVAL : irq;
1076 goto err_disable_pdev;
1077 }
1078
1079 dev_info(dev, "Number of IRQs %d allocated - requested(%d).\n",
1080 irq, VK_MSIX_IRQ_MAX);
1081
1082 for (i = 0; i < MAX_BAR; i++) {
1083 /* multiple by 2 for 64 bit BAR mapping */
1084 vk->bar[i] = pci_ioremap_bar(pdev, i * 2);
1085 if (!vk->bar[i]) {
1086 dev_err(dev, "failed to remap BAR%d\n", i);
1087 goto err_iounmap;
1088 }
1089 }
1090
1091 for (vk->num_irqs = 0;
1092 vk->num_irqs < VK_MSIX_MSGQ_MAX;
1093 vk->num_irqs++) {
1094 err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
1095 bcm_vk_msgq_irqhandler,
1096 IRQF_SHARED, DRV_MODULE_NAME, vk);
1097 if (err) {
1098 dev_err(dev, "failed to request msgq IRQ %d for MSIX %d\n",
1099 pdev->irq + vk->num_irqs, vk->num_irqs + 1);
1100 goto err_irq;
1101 }
1102 }
1103 /* one irq for notification from VK */
1104 err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
1105 bcm_vk_notf_irqhandler,
1106 IRQF_SHARED, DRV_MODULE_NAME, vk);
1107 if (err) {
1108 dev_err(dev, "failed to request notf IRQ %d for MSIX %d\n",
1109 pdev->irq + vk->num_irqs, vk->num_irqs + 1);
1110 goto err_irq;
1111 }
1112 vk->num_irqs++;
1113
1114 for (i = 0;
1115 (i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq);
1116 i++, vk->num_irqs++) {
1117 err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
1118 bcm_vk_tty_irqhandler,
1119 IRQF_SHARED, DRV_MODULE_NAME, vk);
1120 if (err) {
1121 dev_err(dev, "failed request tty IRQ %d for MSIX %d\n",
1122 pdev->irq + vk->num_irqs, vk->num_irqs + 1);
1123 goto err_irq;
1124 }
1125 vk->tty[i].irq_enabled = true;
1126 }
1127
1128 id = ida_simple_get(&bcm_vk_ida, 0, 0, GFP_KERNEL);
1129 if (id < 0) {
1130 err = id;
1131 dev_err(dev, "unable to get id\n");
1132 goto err_irq;
1133 }
1134
1135 vk->misc_devid = id;
1136 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
1137 misc_device = &vk->miscdev;
1138 misc_device->minor = MISC_DYNAMIC_MINOR;
1139 misc_device->name = kstrdup(name, GFP_KERNEL);
1140 if (!misc_device->name) {
1141 err = -ENOMEM;
1142 goto err_ida_remove;
1143 }
1144 misc_device->fops = &bcm_vk_fops,
1145
1146 err = misc_register(misc_device);
1147 if (err) {
1148 dev_err(dev, "failed to register device\n");
1149 goto err_kfree_name;
1150 }
1151
1152 err = bcm_vk_msg_init(vk);
1153 if (err) {
1154 dev_err(dev, "failed to init msg queue info\n");
1155 goto err_misc_deregister;
1156 }
1157
1158 /* sync other info */
1159 bcm_vk_sync_card_info(vk);
1160
> 1161 err = bcm_vk_sysfs_init(pdev, misc_device);
1162 if (err)
1163 goto err_misc_deregister;
1164
1165 /* register for panic notifier */
1166 vk->panic_nb.notifier_call = bcm_vk_on_panic;
1167 atomic_notifier_chain_register(&panic_notifier_list,
1168 &vk->panic_nb);
1169
1170 snprintf(name, sizeof(name), KBUILD_MODNAME ".%d_ttyVK", id);
1171 err = bcm_vk_tty_init(vk, name);
1172 if (err)
1173 goto err_unregister_panic_notifier;
1174
1175 /*
1176 * lets trigger an auto download. We don't want to do it serially here
1177 * because at probing time, it is not supposed to block for a long time.
1178 */
1179 boot_status = vkread32(vk, BAR_0, BAR_BOOT_STATUS);
1180 if (auto_load) {
1181 if ((boot_status & BOOT_STATE_MASK) == BROM_RUNNING) {
1182 if (bcm_vk_trigger_autoload(vk))
1183 goto err_bcm_vk_tty_exit;
1184 } else {
1185 dev_info(dev,
1186 "Auto-load skipped - BROM not in proper state (0x%x)\n",
1187 boot_status);
1188 }
1189 }
1190
1191 /* enable hb */
1192 bcm_vk_hb_init(vk);
1193
1194 dev_info(dev, "BCM-VK:%u created, 0x%p\n", id, vk);
1195
1196 return 0;
1197
1198 err_bcm_vk_tty_exit:
1199 bcm_vk_tty_exit(vk);
1200
1201 err_unregister_panic_notifier:
1202 atomic_notifier_chain_unregister(&panic_notifier_list,
1203 &vk->panic_nb);
1204
1205 err_misc_deregister:
1206 misc_deregister(misc_device);
1207
1208 err_kfree_name:
1209 kfree(misc_device->name);
1210 misc_device->name = NULL;
1211
1212 err_ida_remove:
1213 ida_simple_remove(&bcm_vk_ida, id);
1214
1215 err_irq:
1216 for (i = 0; i < vk->num_irqs; i++)
1217 devm_free_irq(dev, pci_irq_vector(pdev, i), vk);
1218
1219 pci_disable_msix(pdev);
1220 pci_disable_msi(pdev);
1221
1222 err_iounmap:
1223 for (i = 0; i < MAX_BAR; i++) {
1224 if (vk->bar[i])
1225 pci_iounmap(pdev, vk->bar[i]);
1226 }
1227 pci_release_regions(pdev);
1228
1229 err_disable_pdev:
1230 pci_free_irq_vectors(pdev);
1231 pci_disable_device(pdev);
1232
1233 return err;
1234 }
1235
1236 void bcm_vk_release_data(struct kref *kref)
1237 {
1238 struct bcm_vk *vk = container_of(kref, struct bcm_vk, kref);
1239
1240 /* use raw print, as dev is gone */
1241 pr_info("BCM-VK:%d release data 0x%p\n", vk->misc_devid, vk);
1242 kfree(vk);
1243 }
1244
1245 static void bcm_vk_remove(struct pci_dev *pdev)
1246 {
1247 int i;
1248 struct bcm_vk *vk = pci_get_drvdata(pdev);
1249 struct miscdevice *misc_device = &vk->miscdev;
1250
1251 bcm_vk_hb_deinit(vk);
1252
1253 /*
1254 * Trigger a reset to card and wait enough time for UCODE to rerun,
1255 * which re-initialize the card into its default state.
1256 * This ensures when driver is re-enumerated it will start from
1257 * a completely clean state.
1258 */
1259 bcm_vk_trigger_reset(vk);
1260 usleep_range(BCM_VK_UCODE_BOOT_US, BCM_VK_UCODE_BOOT_MAX_US);
1261
1262 /* unregister panic notifier */
1263 atomic_notifier_chain_unregister(&panic_notifier_list,
1264 &vk->panic_nb);
1265
> 1266 bcm_vk_sysfs_exit(pdev, misc_device); 1267
1268 bcm_vk_msg_remove(vk);
1269 bcm_vk_tty_exit(vk);
1270
1271 if (vk->tdma_vaddr)
1272 dma_free_coherent(&pdev->dev, nr_scratch_pages * PAGE_SIZE,
1273 vk->tdma_vaddr, vk->tdma_addr);
1274
1275 /* remove if name is set which means misc dev registered */
1276 if (misc_device->name) {
1277 misc_deregister(misc_device);
1278 kfree(misc_device->name);
1279 ida_simple_remove(&bcm_vk_ida, vk->misc_devid);
1280 }
1281 for (i = 0; i < vk->num_irqs; i++)
1282 devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), vk);
1283
1284 pci_disable_msix(pdev);
1285 pci_disable_msi(pdev);
1286
1287 cancel_work_sync(&vk->wq_work);
1288 destroy_workqueue(vk->wq_thread);
1289 cancel_work_sync(&vk->tty_wq_work);
1290 destroy_workqueue(vk->tty_wq_thread);
1291
1292 for (i = 0; i < MAX_BAR; i++) {
1293 if (vk->bar[i])
1294 pci_iounmap(pdev, vk->bar[i]);
1295 }
1296
1297 dev_info(&pdev->dev, "BCM-VK:%d released\n", vk->misc_devid);
1298
1299 pci_release_regions(pdev);
1300 pci_free_irq_vectors(pdev);
1301 pci_disable_device(pdev);
1302
1303 kref_put(&vk->kref, bcm_vk_release_data);
1304 }
1305
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Scott Branden
maintainerPATCH v10 8/9 MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver
In reply to Scott BrandenAdd maintainer entry for new Broadcom VK Driver
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -3662,6 +3662,13 @@ L: [email protected] S: Supported F: drivers/net/ethernet/broadcom/tg3.* +BROADCOM VK DRIVER +M: Scott Branden <[email protected]> +L: [email protected] +S: Supported +F: drivers/misc/bcm-vk/ +F: include/uapi/linux/misc/bcm_vk.h + BROCADE BFA FC SCSI DRIVER M: Anil Gurumurthy <[email protected]> M: Sudarsana Kalluru <[email protected]>
Signature · 1 line
2.17.1
Scott Branden
maintainerPATCH v10 9/9 ima: add FIRMWARE_PARTIAL_READ support
In reply to Scott BrandenAdd FIRMWARE_PARTIAL_READ support for integrity
measurement on partial reads of firmware files.
--- security/integrity/ima/ima_main.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
security/integrity/ima/ima_main.c +23 -1
@@ -611,6 +611,9 @@ void ima_post_path_mknod(struct dentry *dentry)*/ int ima_read_file (struct file *file,enum kernel_read_file_id read_id) { +enum ima_hooks func; +u32 secid; +/* * READING_FIRMWARE_PREALLOC_BUFFER *
@@ -619,11 +622,27 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id)* of IMA's signature verification any more than when using two * buffers? */ -return 0 ; +if (read_id !=READING_FIRMWARE_PARTIAL_READ ) +return 0 ; + +if (!file) { +if ((ima_appraise &IMA_APPRAISE_FIRMWARE ) && + (ima_appraise &IMA_APPRAISE_ENFORCE )) { +pr_err ("Prevent firmware loading_store.\n" ); +return -EACCES ;/* INTEGRITY_UNKNOWN */ + } +return 0 ; + } + + 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 ] = { [READING_FIRMWARE ] =FIRMWARE_CHECK , + [READING_FIRMWARE_PARTIAL_READ ] =FIRMWARE_CHECK , [READING_FIRMWARE_PREALLOC_BUFFER ] =FIRMWARE_CHECK , [READING_MODULE ] =MODULE_CHECK , [READING_KEXEC_IMAGE ] =KEXEC_KERNEL_CHECK ,
@@ -650,6 +669,9 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,enum ima_hooks func;u32 secid; +if (read_id ==READING_FIRMWARE_PARTIAL_READ ) +return 0 ; +if (!file && read_id ==READING_FIRMWARE ) {if ((ima_appraise &IMA_APPRAISE_FIRMWARE ) && (ima_appraise &IMA_APPRAISE_ENFORCE )) {
Signature · 1 line
2.17.1
Re: [PATCH v10 9/9] ima: add FIRMWARE_PARTIAL_READ support
Quoted 2 lines · Scott Branden
Add
FIRMWARE_PARTIAL_READsupport for integrity
measurement on partial reads of firmware files.
Hi,
Several versions ago I'd suggested that the LSM infrastructure handle the "full read" semantics so that individual LSMs don't need to each duplicate the same efforts. As it happens, only IMA is impacted (SELinux ignores everything except modules, and LoadPin only cares about origin not contents).
Next is the problem that enum kernel_read_file_id is an object TYPE enum, not a HOW enum. (And it seems I missed the addition of READING_FIRMWARE_PREALLOC_BUFFER, which may share a similar problem.) That it's a partial read doesn't change _what_ you're reading: that's an internal API detail. What happens when I attempt to do a partial read of a kexec image? I'll use kernel_pread_file() and pass READING_KEXEC_IMAGE, but the LSMs will have no idea it's a partial read.
Finally, what keeps the contents of the file from changing between the first call (which IMA will read the entire file for) and the next reads which will bypass IMA? I'd suggested that the open file must have writes disabled on it (as execve() does).
So, please redesign this:
- do not add an enum
- make the file unwritable for the life of having the handle open
- make the "full read" happen as part of the first partial read so the LSMs don't have to reimplement everything
-Kees
Signature · 1 line
Kees Cook
Scott Branden
maintainerRe: [PATCH v10 9/9] ima: add FIRMWARE_PARTIAL_READ support
Hi Kees,
You and others are certainly more experts in the filesystem and security infrastructure of the kernel.
What I am trying to accomplish is a simple operation:
request part of a file into a buffer rather than the whole file. If someone could add such support I would be more than happy to use it.
This has now bubbled into many other designs issues in the existing codebase. I will need more details on your comments - see below.
Quoted 10 lines · Kees Cook
Quoted 2 lines · Scott Branden
Add
FIRMWARE_PARTIAL_READsupport for integrity
measurement on partial reads of firmware files.Hi,
Several versions ago I'd suggested that the LSM infrastructure handle the "full read" semantics so that individual LSMs don't need to each duplicate the same efforts. As it happens, only IMA is impacted (SELinux ignores everything except modules, and LoadPin only cares about origin not contents).
Does your patch series "Fix misused kernel_read_file() enums" handle this because this suggestion is outside the scope of my change?
Quoted 7 lines
Next is the problem that enum
kernel_read_file_idis an object TYPE enum, not a HOW enum. (And it seems I missed the addition ofREADING_FIRMWARE_PREALLOC_BUFFER, which may share a similar problem.) That it's a partial read doesn't change _what_ you're reading: that's an internal API detail. What happens when I attempt to do a partial read of a kexec image?
It does not appear there is any user of partial reads of kexec images? I have been informed by Greg K-H to not add apis that are not used so such support doesn't make sense to add at this time.
Quoted 2 lines
I'll use
kernel_pread_file()and passREADING_KEXEC_IMAGE,
but the LSMs will have no idea it's a partial read.
The addition I am adding is for request_partial_firmware_into_buf. In order to do so it adds internal support for partial reads of firmware files, not kexec image.
The above seems outside the scope of my patch?
Quoted 4 lines
Finally, what keeps the contents of the file from changing between the first call (which IMA will read the entire file for) and the next reads which will bypass IMA?
The request is for a partial read. IMA ensures the whole file integrity even though I only do a partial read. The next partial read will re-read and check integrity of file.
Quoted 2 lines
I'd suggested that the open file must have writes
disabled on it (asexecve()does).
The file will be reopened and integrity checked on the next partial read (if there is one).
So I don't think there is any change to be made here.
If writes aren't already disabled for a whole file read then that is something that needs to be fixed in the existing code.
Quoted 3 lines
So, please redesign this:
- do not add an enum
I used existing infrastructure provided by Mimi but now looks like it will have to fit with your patches from yesterday.
Quoted 1 line
- make the file unwritable for the life of having the handle open
It's no different than a full file read so no change to be made here.
Quoted 2 lines
- make the "full read" happen as part of the first partial read so the LSMs don't have to reimplement everything
Each partial read is an individual operation so I think a "full read" is performed every time if your security IMA is enabled. If someone wants to add a file lock and then partial reads in the kernel then that would be different than what is needed by the kernel driver.
Quoted 3 lines
-Kees
Regards,
Scott
Re: [PATCH v10 9/9] ima: add FIRMWARE_PARTIAL_READ support
Quoted 5 lines · Scott Branden
You and others are certainly more experts in the filesystem and security infrastructure of the kernel.
What I am trying to accomplish is a simple operation:
request part of a file into a buffer rather than the whole file. If someone could add such support I would be more than happy to use it.
Sure, and I totally understand that, but as it happens, no one has stepped up with spare time to do that work. Since you're the person with the need for the API, it falls to you to do it. And I understand what feature creep feels like (I needed to fix one design problem[1] with timers, and I spent months sending hundreds of patches). Some times you get lucky and it's easy, and sometimes you end up touching something that needs a LOT of work to refactor before you can make your desired change work well with the rest of the kernel and be maintainable by other people into the future.
Quick tangent: I can't find in the many many threads where you explain how large these firmware images are and why existing kernel memory allocations are insufficient to load them. How large are these[2] files?
/lib/firmware/vk-boot1-bcm958401m2.ecdsa.bin/lib/firmware/vk-boot2-bcm958401m2_a72.ecdsa.bin
For me, the requirements for partial read support are these things, which are the characteristics of the existing API:
- the LSM must be able to validate the entire file contents before any data is available to any reader. (Which was pointed out back in August 2019[3].) And "any" reader includes having a DMA window open on the memory range used for reading the contents (which was pointed out at by Mimi[4] but went unanswered and remains broken still in this v10, but I will comment separately on that.)
- the integrity of the file contents must be maintained between validation and delivery (currently this is handled internally via disallow_writes()).
Quoted 2 lines
This has now bubbled into many other designs issues in the existing codebase.
Correct -- this is one of the many difficulties of contributing to a large and complex code base with many maintainers. There can be a lot of requirements for the code that have nothing to do with seemingly more narrow areas of endeavour.
Quoted 8 lines
I will need more details on your comments - see below.
Quoted 5 lines · Kees Cook
Quoted 2 lines · Scott Branden
Add
FIRMWARE_PARTIAL_READsupport for integrity
measurement on partial reads of firmware files.Hi,
Quoted 8 lines
Quoted 5 lines
Several versions ago I'd suggested that the LSM infrastructure handle the "full read" semantics so that individual LSMs don't need to each duplicate the same efforts. As it happens, only IMA is impacted (SELinux ignores everything except modules, and LoadPin only cares about origin not contents).
Does your patch series "Fix misused
kernel_read_file()enums" handle this because this suggestion is outside the scope of my change?
My proposed patch series cleans up a number of mistakes that were made to the kernel_read_file() API, and helps clarify my point about the enums being used for *what*, and not *how* or *where*, which needs to be fixed in this series and shouldn't be a big deal (I will reply to individual patches).
Quoted 10 lines
Quoted 6 lines
Next is the problem that enum
kernel_read_file_idis an object TYPE enum, not a HOW enum. (And it seems I missed the addition ofREADING_FIRMWARE_PREALLOC_BUFFER, which may share a similar problem.) That it's a partial read doesn't change _what_ you're reading: that's an internal API detail. What happens when I attempt to do a partial read of a kexec image?It does not appear there is any user of partial reads of kexec images? I have been informed by Greg K-H to not add apis that are not used so such support doesn't make sense to add at this time.
But you are proposing a generic API enhancement that any other user in the kernel may end up using. Just because the bcm-vk driver is the only user now, and IMA is the only LSM performing content analysis, it doesn't mean that there won't be another driver added later, nor another LSM. In fact, the new BPF LSM means that anything exposed by LSM hooks is now available for analysis.
Quoted 6 lines
Quoted 2 lines
I'll use
kernel_pread_file()and passREADING_KEXEC_IMAGE,
but the LSMs will have no idea it's a partial read.The addition I am adding is for
request_partial_firmware_into_buf. In order to do so it adds internal support for partial reads of firmware files,
not kexec image.
Yes, but you're changing kernel_read_file() APIs to do it. There are plenty of users of that API. Maybe they would like to also use partial reads?
$ git grep kernel_read_file | wc -l 77
Quoted 1 line
The above seems outside the scope of my patch?
Unfortunately, it is not. Part of your responsibility as a kernel developer making API changes/additions is that those changes need to interact correctly with the rest of the kernel (and be maintainable).
Quoted 7 lines
Quoted 3 lines
Finally, what keeps the contents of the file from changing between the first call (which IMA will read the entire file for) and the next reads which will bypass IMA?
The request is for a partial read. IMA ensures the whole file integrity even though I only do a partial read.
The next partial read will re-read and check integrity of file.
So, while terribly inefficient, I guess this approach is tenable. It means that each partial read will trigger a full read for LSMs that care about the hook.
So, to that end, I wonder why IMA doesn't do this for all file types?
It also means that we won't have a strict pairing of security_kernel_read_file() to security_kernel_post_read_file() in the LSMs, but it seems that only IMA currently explicitly cares about this (or maybe not at all).
I'm not entirely happy about using this design, but it does appear sufficient.
Quoted 7 lines
Quoted 2 lines
I'd suggested that the open file must have writes
disabled on it (asexecve()does).The file will be reopened and integrity checked on the next partial read (if there is one).
So I don't think there is any change to be made here.
If writes aren't already disabled for a whole file read then that is something that needs to be fixed in the existing code.
My suggestion quoted here was operating on the idea that whole-file verification wasn't happening on every partial read, so this isn't a problem in that case.
Quoted 5 lines
Quoted 3 lines
So, please redesign this:
- do not add an enumI used existing infrastructure provided by Mimi but now looks like it will have to fit with your patches from yesterday.
Right, this won't be hard. I will send a v2 of my patches to clarify the purpose of the 3 file content hooks (load_data, read_file, post_read_file), which might need renaming...
Quoted 2 lines
Quoted 1 line
- make the file unwritable for the life of having the handle open
It's no different than a full file read so no change to be made here.
Correct.
Quoted 7 lines
Quoted 2 lines
- make the "full read" happen as part of the first partial read so the LSMs don't have to reimplement everything
Each partial read is an individual operation so I think a "full read" is performed every time
if your security IMA is enabled. If someone wants to add a file lock and then partial reads in the kernel
then that would be different than what is needed by the kernel driver.
So, given that Mimi is (I think?) satisfied with your approach here, I can't realistically complain. I still don't like the idea of each LSM needing to perform it's full read loop for the contents, but so be it:
IMA will have the code, SELinux doesn't care (yet), and LoadPin doesn't care about contents.
-Kees
[1] https://lore.kernel.org/lkml/20170808003345.GA107289@beast/
git log --oneline --grep 'Convert timer' --author "Kees Cook" | wc -l
271[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
Signature · 1 line
Kees Cook
Scott Branden
maintainerRe: [PATCH v10 9/9] ima: add FIRMWARE_PARTIAL_READ support
Quoted 15 lines · Kees Cook
Quoted 5 lines · Scott Branden
You and others are certainly more experts in the filesystem and security infrastructure of the kernel.
What I am trying to accomplish is a simple operation:
request part of a file into a buffer rather than the whole file. If someone could add such support I would be more than happy to use it.Sure, and I totally understand that, but as it happens, no one has stepped up with spare time to do that work. Since you're the person with the need for the API, it falls to you to do it. And I understand what feature creep feels like (I needed to fix one design problem[1] with timers, and I spent months sending hundreds of patches). Some times you get lucky and it's easy, and sometimes you end up touching something that needs a LOT of work to refactor before you can make your desired change work well with the rest of the kernel and be maintainable by other people into the future.
Quoted 5 lines
Quick tangent: I can't find in the many many threads where you explain how large these firmware images are and why existing kernel memory allocations are insufficient to load them. How large are these[2] files?
/lib/firmware/vk-boot1-bcm958401m2.ecdsa.bin
This is on the order of a few MB at most.
Quoted 1 line
/lib/firmware/vk-boot2-bcm958401m2_a72.ecdsa.bin
Some of these images are current 250MB. At we anticipate them growing to 512MB.
And, we do have systems with the driver loading 16 cards in parallel with no requirement that they are the same images (although loading 16 different images to 16 different cards would be strange but possible).
Quoted 7 lines
For me, the requirements for partial read support are these things, which are the characteristics of the existing API:
- the LSM must be able to validate the entire file contents before any data is available to any reader. (Which was pointed out back in August 2019[3].)
I thought this was addressed in patch v6 "ima: aad FIRMWARE_PARTIAL_READ support"
https://lkml.org/lkml/2020/6/5/1126
(although implementation not to your liking in current review)?
Quoted 8 lines
And "any" reader includes having a DMA window open
on the memory range used for reading the contents (which was pointed out at by Mimi[4] but went unanswered and remains broken still in this v10, but I will comment separately on that.)- the integrity of the file contents must be maintained between validation and delivery (currently this is handled internally via
disallow_writes()).
I don't understand what you are staying here: I am request a partial firmware read into a buf.
At the time the partial firmware is read into a buf it is validated by the security module if such integrity checks are enabled.
If, at another time I wish to request another partial firmware into a buffer (could be the same part of the file or a different part of a file or from another file), the integrity check is performed again and the portion of the file I request should be put into the buffer. If a lock on a file is needed by somebody between these partial reads that is a different API and out of the scope of my patch series.
Quoted 6 lines
Quoted 2 lines
This has now bubbled into many other designs issues in the existing codebase.
Correct -- this is one of the many difficulties of contributing to a large and complex code base with many maintainers. There can be a lot of requirements for the code that have nothing to do with seemingly more narrow areas of endeavour.
Thanks at least for helping with guidance, I see your review is thought out and hopefully we can come to a conclusion as I feel we are fairly close with your changes.
Quoted 68 lines
Quoted 9 lines
I will need more details on your comments - see below.
Quoted 5 lines · Kees Cook
Quoted 2 lines · Scott Branden
Add
FIRMWARE_PARTIAL_READsupport for integrity
measurement on partial reads of firmware files.Hi,
Quoted 7 lines
Quoted 5 lines
Several versions ago I'd suggested that the LSM infrastructure handle the "full read" semantics so that individual LSMs don't need to each duplicate the same efforts. As it happens, only IMA is impacted (SELinux ignores everything except modules, and LoadPin only cares about origin not contents).
Does your patch series "Fix misused
kernel_read_file()enums" handle this because this suggestion is outside the scope of my change?My proposed patch series cleans up a number of mistakes that were made to the
kernel_read_file()API, and helps clarify my point about the enums being used for *what*, and not *how* or *where*, which needs to be fixed in this series and shouldn't be a big deal (I will reply to individual patches).Quoted 9 lines
Quoted 6 lines
Next is the problem that enum
kernel_read_file_idis an object TYPE enum, not a HOW enum. (And it seems I missed the addition ofREADING_FIRMWARE_PREALLOC_BUFFER, which may share a similar problem.) That it's a partial read doesn't change _what_ you're reading: that's an internal API detail. What happens when I attempt to do a partial read of a kexec image?It does not appear there is any user of partial reads of kexec images? I have been informed by Greg K-H to not add apis that are not used so such support doesn't make sense to add at this time.
But you are proposing a generic API enhancement that any other user in the kernel may end up using. Just because the bcm-vk driver is the only user now, and IMA is the only LSM performing content analysis, it doesn't mean that there won't be another driver added later, nor another LSM. In fact, the new BPF LSM means that anything exposed by LSM hooks is now available for analysis.
Quoted 6 lines
Quoted 2 lines
I'll use
kernel_pread_file()and passREADING_KEXEC_IMAGE,
but the LSMs will have no idea it's a partial read.The addition I am adding is for
request_partial_firmware_into_buf. In order to do so it adds internal support for partial reads of firmware files,
not kexec image.Yes, but you're changing
kernel_read_file()APIs to do it. There are plenty of users of that API. Maybe they would like to also use partial reads?$ git grep kernel_read_file | wc -l 77Quoted 1 line
The above seems outside the scope of my patch?
Unfortunately, it is not. Part of your responsibility as a kernel developer making API changes/additions is that those changes need to interact correctly with the rest of the kernel (and be maintainable).
Quoted 6 lines
Quoted 3 lines
Finally, what keeps the contents of the file from changing between the first call (which IMA will read the entire file for) and the next reads which will bypass IMA?
The request is for a partial read. IMA ensures the whole file integrity even though I only do a partial read.
The next partial read will re-read and check integrity of file.So, while terribly inefficient, I guess this approach is tenable. It means that each partial read will trigger a full read for LSMs that care about the hook.
So, to that end, I wonder why IMA doesn't do this for all file types?
Quoted 7 lines
It also means that we won't have a strict pairing of
security_kernel_read_file()tosecurity_kernel_post_read_file()in the LSMs, but it seems that only IMA currently explicitly cares about this (or maybe not at all).I'm not entirely happy about using this design, but it does appear sufficient.
Yes, terribly inefficient, but if somebody wants to do some optimization they are welcome to it.
In fact, I want to call a partial read of a file with NO security. Can I add such a call instead?
If we did that now then the inefficient read of the file multiple times to authenticate it each time wouldn't be introduced. In my use case the linux kernel security on the file is quite meaningless and a waste of time to even perform. Whether the file has been compromised, corrupt or otherwise the image is validated by the hardware after the image is loaded to the card to ensure it passes authentication.
Quoted 12 lines
Quoted 8 lines
Quoted 3 lines
I'd suggested that the open file must have writes
disabled on it (asexecve()does).The file will be reopened and integrity checked on the next partial read (if there is one).
So I don't think there is any change to be made here.
If writes aren't already disabled for a whole file read then that is something that needs to be fixed in the existing code.My suggestion quoted here was operating on the idea that whole-file verification wasn't happening on every partial read, so this isn't a problem in that case.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
So, please redesign this:
- do not add an enumI used existing infrastructure provided by Mimi but now looks like it will have to fit with your patches from yesterday.
Right, this won't be hard. I will send a v2 of my patches to clarify the purpose of the 3 file content hooks (
load_data,read_file,post_read_file), which might need renaming...
I see your cleanup and merged with it. Will need to test everything again.
Quoted 12 lines
Quoted 3 lines
Quoted 2 lines
- make the file unwritable for the life of having the handle open
It's no different than a full file read so no change to be made here.
Correct.
Quoted 7 lines
Quoted 2 lines
- make the "full read" happen as part of the first partial read so the LSMs don't have to reimplement everything
Each partial read is an individual operation so I think a "full read" is performed every time
if your security IMA is enabled. If someone wants to add a file lock and then partial reads in the kernel
then that would be different than what is needed by the kernel driver.
Quoted 5 lines
So, given that Mimi is (I think?) satisfied with your approach here, I can't realistically complain. I still don't like the idea of each LSM needing to perform it's full read loop for the contents, but so be it:
IMA will have the code, SELinux doesn't care (yet), and LoadPin doesn't care about contents.
I obviously am not aware of all of the security hooks and architecture in place but I do see your cleanups as a simplification over what was there.
Quoted 10 lines
-Kees
[1] https://lore.kernel.org/lkml/20170808003345.GA107289@beast/
git log --oneline --grep 'Convert timer' --author "Kees Cook" | wc -l 271[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
Quoted 9 lines · Scott Branden
This patch series adds partial read support via a new call
request_partial_firmware_into_buf.
Such support is needed when the whole file is not needed and/or only a smaller portion of the file will fit into allocated memory at any one time.
In order to accept the enhanced API it has been requested that kernel selftests and upstreamed driver utilize the API enhancement and so are included in this patch series.
Quoted 6 lines
Also in this patch series is the addition of a new Broadcom VK driver utilizing the new
request_firmware_into_bufenhanced API.Further comment followed to add IMA support of the partial reads originating from
request_firmware_into_bufcalls. And another request to move existing kernel_read_file* functions to its own include file.
Do you have any way to separate the VK drivers submission from the request_partial_firmware_into_buf() work that you are doing? It looks like it is going to require quite a few iterations of this patch set for the firmware/fs/IMA part to be ironed out, so if you could get your driver separated out, it might help you achieve partial success here.
Signature · 1 line
Florian
Scott Branden
maintainerHi Florian,
Quoted 14 lines · Florian Fainelli
Quoted 12 lines · Scott Branden
This patch series adds partial read support via a new call
request_partial_firmware_into_buf.
Such support is needed when the whole file is not needed and/or only a smaller portion of the file will fit into allocated memory at any one time.
In order to accept the enhanced API it has been requested that kernel selftests and upstreamed driver utilize the API enhancement and so are included in this patch series.Also in this patch series is the addition of a new Broadcom VK driver utilizing the new
request_firmware_into_bufenhanced API.
Quoted 8 lines
Quoted 3 lines
Further comment followed to add IMA support of the partial reads originating from
request_firmware_into_bufcalls. And another request to move existing kernel_read_file* functions to its own include file.Do you have any way to separate the VK drivers submission from the
request_partial_firmware_into_buf()work that you are doing? It looks like it is going to require quite a few iterations of this patch set for thefirmware/fs/IMApart to be ironed out, so if you could get your driver separated out, it might help you achieve partial success here.
Originally I did not submit the driver.
But Greg K-H rejected the pread support unless there was an actual user in the kernel. Hence the need to submit this all in the patch series.