firmware_loader: load files from the mount namespace of init
Subsystems (4)
PATCH 1/1 firmware_loader: load files from the mount namespace of init
Hi,
I have an experimental setup where almost every possible system service (even early startup ones) runs in separate namespace, using a dedicated, minimal file system. In process of minimizing the contents of the file systems with regards to modules and firmware files, I noticed that in my system, the firmware files are loaded from three different mount namespaces, those of systemd-udevd, init and systemd-networkd. The logic of the source namespace is not very clear, it seems to depend on the driver, but the namespace of the current process is used.
So, this patch tries to make things a bit clearer and changes the loading of firmware files only from the mount namespace of init. This may also improve security, though I think that using firmware files as attack vector could be too impractical anyway.
Later, it might make sense to make the mount namespace configurable, for example with a new file in/proc/sys/kernel/firmware_config/. That would allow a dedicated file system only for firmware files and those need not be present anywhere else. This configurability would make more sense if made also for kernel modules and /sbin/modprobe. Modules are already loaded from init namespace (usermodehelper uses kthreadd namespace) except when directly loaded by systemd-udevd.
-Topi
From 8adbf10d6b5a5e011b83fcf952e5dd4b4f2b749e Mon Sep 17 00:00:00 2001
From: Topi Miettinen <[email protected]>
Date: Fri, 17 Jan 2020 19:56:45 +0200
Subject: [PATCH] firmware_loader: load files from the mount namespace of init
Instead of using the mount namespace of the current process to load firmware files, use the mount namespace of init process.
--- drivers/base/firmware_loader/main.c | 6 ++++-- fs/exec.c | 25 +++++++++++++++++++++++++ include/linux/fs.h | 2 ++ 3 files changed, 31 insertions(+), 2 deletions(-)
drivers/base/firmware_loader/main.c +4 -2
@@ -493,8 +493,10 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv, } fw_priv->size =0 ; - rc =kernel_read_file_from_path (path, &buffer, &size, - msize, id); + +/* load firmware files from the mount namespace of init */ + rc =kernel_read_file_from_path_initns (path, &buffer, + &size, msize, id);if (rc) {if (rc != -ENOENT ) dev_warn(device, "loading %s failed with error %d\n",
fs/exec.c +25 -0
@@ -981,6 +981,31 @@ int kernel_read_file_from_path(const char *path, void **buf, loff_t *size, }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) +{ +struct file *file; +struct path root; +int ret; + +if (!path || !*path) +return -EINVAL ; + +task_lock (&init_task); +get_fs_root (init_task.fs, &root); +task_unlock (&init_task); + + file =file_open_root (root.dentry, root.mnt, path,O_RDONLY ,0 ); +path_put (&root); +if (IS_ERR (file)) +return PTR_ERR (file); + + ret =kernel_read_file (file, buf, size, max_size, id); +fput (file); +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) {
include/linux/fs.h +2 -0
@@ -2994,6 +2994,8 @@ 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 *);
Signature · 1 line
2.24.1
Greg Kroah-Hartman
maintainerQuoted 11 lines · Topi Miettinen
Hi,
I have an experimental setup where almost every possible system service (even early startup ones) runs in separate namespace, using a dedicated, minimal file system. In process of minimizing the contents of the file systems with regards to modules and firmware files, I noticed that in my system, the firmware files are loaded from three different mount namespaces, those of systemd-udevd, init and systemd-networkd. The logic of the source namespace is not very clear, it seems to depend on the driver, but the namespace of the current process is used.
Quoted 4 lines
So, this patch tries to make things a bit clearer and changes the loading of firmware files only from the mount namespace of init. This may also improve security, though I think that using firmware files as attack vector could be too impractical anyway.
I like this, but:
Quoted 8 lines
Later, it might make sense to make the mount namespace configurable, for example with a new file in
/proc/sys/kernel/firmware_config/. That would allow a dedicated file system only for firmware files and those need not be present anywhere else. This configurability would make more sense if made also for kernel modules and/sbin/modprobe. Modules are already loaded from init namespace (usermodehelper uses kthreadd namespace) except when directly loaded by systemd-udevd.
I think you answered your question of why firmware is loaded from the namespace of systemd-udevd at times, it happens due to a module being asked to be loaded which then called out and asked for firmware as part of its probe process.
Now saying that the firmware load namespace is going to be tied always to the modprobe namespace is problematic, as we can't guarantee that will always happen for all bus and driver types.
So resetting this all back to the init namespace seems to make sense to me, and odds are it will not break anything.
But, as you are adding a new firmware feature, any chance you can write an additional test to the firmware self-tests so that we can verify that this really is working the way you are saying it does, so we can trust it and verify it doesn't break in the future?
thanks,
greg k-h
Quoted 28 lines · Greg Kroah-Hartman
Quoted 11 lines · Topi Miettinen
Hi,
I have an experimental setup where almost every possible system service (even early startup ones) runs in separate namespace, using a dedicated, minimal file system. In process of minimizing the contents of the file systems with regards to modules and firmware files, I noticed that in my system, the firmware files are loaded from three different mount namespaces, those of systemd-udevd, init and systemd-networkd. The logic of the source namespace is not very clear, it seems to depend on the driver, but the namespace of the current process is used.
Quoted 4 lines
So, this patch tries to make things a bit clearer and changes the loading of firmware files only from the mount namespace of init. This may also improve security, though I think that using firmware files as attack vector could be too impractical anyway.
I like this, but:
Quoted 8 lines
Later, it might make sense to make the mount namespace configurable, for example with a new file in
/proc/sys/kernel/firmware_config/. That would allow a dedicated file system only for firmware files and those need not be present anywhere else. This configurability would make more sense if made also for kernel modules and/sbin/modprobe. Modules are already loaded from init namespace (usermodehelper uses kthreadd namespace) except when directly loaded by systemd-udevd.
Quoted 4 lines
I think you answered your question of why firmware is loaded from the namespace of systemd-udevd at times, it happens due to a module being asked to be loaded which then called out and asked for firmware as part of its probe process.
r8169 requests firmware only when opening the device, so the firmware is loaded from systemd-networkd namespace.
Quoted 11 lines
Now saying that the firmware load namespace is going to be tied always to the modprobe namespace is problematic, as we can't guarantee that will always happen for all bus and driver types.
So resetting this all back to the init namespace seems to make sense to me, and odds are it will not break anything.
But, as you are adding a new firmware feature, any chance you can write an additional test to the firmware self-tests so that we can verify that this really is working the way you are saying it does, so we can trust it and verify it doesn't break in the future?
OK, sent v2 of the patch with the tests. They assume a writable /lib/firmware, is that OK? Maybe I should change that to overmount it temporarily with a writable tmpfs instead.
-Topi
kbuild test robot
build botThank you for the patch! Yet something to improve:
Hi Topi,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on driver-core/driver-core-testing]
[also build test ERROR on linux/master linus/master v5.5-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Topi-Miettinen/firmware_loader-load-files-from-the-mount-namespace-of-init/20200118-100042 base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git a37f4958f7b63d2b3cd17a76151fdfc29ce1da5f config: mips-markeins_defconfig (attached as .config) compiler: mips-linux-gcc (GCC) 7.5.0
reproduce:
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
GCC_VERSION=7.5.0 make.cross ARCH=mips
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
>> ERROR: "kernel_read_file_from_path_initns" [drivers/base/firmware_loader/firmware_class.ko] undefined!
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation