2022-09-29 02:34:14

by Huacai Chen

[permalink] [raw]
Subject: [PATCH] LoongArch: Support access filter to /dev/mem interface

Accidental access to /dev/mem is obviously disastrous, but specific
access can be used by people debugging the kernel. So select GENERIC_
LIB_DEVMEM_IS_ALLOWED, as well as define ARCH_HAS_VALID_PHYS_ADDR_RANGE
and related helpers, to support access filter to /dev/mem interface.

Signed-off-by: Weihao Li <[email protected]>
Signed-off-by: Huacai Chen <[email protected]>
---
arch/loongarch/Kconfig | 1 +
arch/loongarch/include/asm/io.h | 4 ++++
arch/loongarch/mm/mmap.c | 28 ++++++++++++++++++++++++++++
3 files changed, 33 insertions(+)

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index fcb5f9489ffd..9c36eb29096a 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -70,6 +70,7 @@ config LOONGARCH
select GENERIC_LIB_CMPDI2
select GENERIC_LIB_LSHRDI3
select GENERIC_LIB_UCMPDI2
+ select GENERIC_LIB_DEVMEM_IS_ALLOWED
select GENERIC_PCI_IOMAP
select GENERIC_SCHED_CLOCK
select GENERIC_SMP_IDLE_THREAD
diff --git a/arch/loongarch/include/asm/io.h b/arch/loongarch/include/asm/io.h
index 999944ea1cea..398d1a7b3dd6 100644
--- a/arch/loongarch/include/asm/io.h
+++ b/arch/loongarch/include/asm/io.h
@@ -107,4 +107,8 @@ extern void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t

#include <asm-generic/io.h>

+#define ARCH_HAS_VALID_PHYS_ADDR_RANGE
+extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
+extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
+
#endif /* _ASM_IO_H */
diff --git a/arch/loongarch/mm/mmap.c b/arch/loongarch/mm/mmap.c
index 381a569635a9..71d45bdffc9e 100644
--- a/arch/loongarch/mm/mmap.c
+++ b/arch/loongarch/mm/mmap.c
@@ -3,6 +3,7 @@
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
*/
#include <linux/export.h>
+#include <linux/memblock.h>
#include <linux/mm.h>
#include <linux/mman.h>

@@ -116,3 +117,30 @@ int __virt_addr_valid(volatile void *kaddr)
return pfn_valid(PFN_DOWN(PHYSADDR(kaddr)));
}
EXPORT_SYMBOL_GPL(__virt_addr_valid);
+
+/*
+ * You really shouldn't be using read() or write() on /dev/mem. This might go
+ * away in the future.
+ */
+int valid_phys_addr_range(phys_addr_t addr, size_t size)
+{
+ /*
+ * Check whether addr is covered by a memory region without the
+ * MEMBLOCK_NOMAP attribute, and whether that region covers the
+ * entire range. In theory, this could lead to false negatives
+ * if the range is covered by distinct but adjacent memory regions
+ * that only differ in other attributes. However, few of such
+ * attributes have been defined, and it is debatable whether it
+ * follows that /dev/mem read() calls should be able traverse
+ * such boundaries.
+ */
+ return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr);
+}
+
+/*
+ * Do not allow /dev/mem mappings beyond the supported physical range.
+ */
+int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
+{
+ return !(((pfn << PAGE_SHIFT) + size) & ~(GENMASK_ULL(cpu_pabits, 0)));
+}
--
2.31.1


2022-09-29 05:38:33

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] LoongArch: Support access filter to /dev/mem interface

Hi Huacai,

I love your patch! Perhaps something to improve:

[auto build test WARNING on soc/for-next]
[also build test WARNING on linus/master v6.0-rc7 next-20220928]
[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#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Huacai-Chen/LoongArch-Support-access-filter-to-dev-mem-interface/20220929-101532
base: https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
config: loongarch-allyesconfig
compiler: loongarch64-linux-gcc (GCC) 12.1.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
# https://github.com/intel-lab-lkp/linux/commit/ebb5f48a499f12b42fe885735d3678914667fb73
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Huacai-Chen/LoongArch-Support-access-filter-to-dev-mem-interface/20220929-101532
git checkout ebb5f48a499f12b42fe885735d3678914667fb73
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch SHELL=/bin/bash arch/loongarch/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

>> arch/loongarch/mm/mmap.c:125:5: warning: no previous prototype for 'valid_phys_addr_range' [-Wmissing-prototypes]
125 | int valid_phys_addr_range(phys_addr_t addr, size_t size)
| ^~~~~~~~~~~~~~~~~~~~~
>> arch/loongarch/mm/mmap.c:143:5: warning: no previous prototype for 'valid_mmap_phys_addr_range' [-Wmissing-prototypes]
143 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~


vim +/valid_phys_addr_range +125 arch/loongarch/mm/mmap.c

120
121 /*
122 * You really shouldn't be using read() or write() on /dev/mem. This might go
123 * away in the future.
124 */
> 125 int valid_phys_addr_range(phys_addr_t addr, size_t size)
126 {
127 /*
128 * Check whether addr is covered by a memory region without the
129 * MEMBLOCK_NOMAP attribute, and whether that region covers the
130 * entire range. In theory, this could lead to false negatives
131 * if the range is covered by distinct but adjacent memory regions
132 * that only differ in other attributes. However, few of such
133 * attributes have been defined, and it is debatable whether it
134 * follows that /dev/mem read() calls should be able traverse
135 * such boundaries.
136 */
137 return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr);
138 }
139
140 /*
141 * Do not allow /dev/mem mappings beyond the supported physical range.
142 */
> 143 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (3.18 kB)
config (329.43 kB)
Download all attachments