RFC: banning device driver reserved resources from /dev/mem
From: Arjan van de Ven <[email protected]>
Date: Sun, 5 Oct 2008 18:00:15 -0700
Subject: [PATCH] resource: don't allow /dev/mem access reserved resources
Device drivers that use pci_request_regions() (and similar APIs) have a reasonable expectation that they are the only ones accessing their device. As part of the e1000e hunt, we were afraid that some userland (X or some bootsplash stuff) was mapping the MMIO region, that the driver thought it had exclusively, via /dev/mem.
This patch adds, to the existing config option to restrict /dev/mem, the reserved regions to the "banned from /dev/mem use" list, so now both kernel memory and device-exclusive MMIO regions are banned.
The introduced iomem_is_reserved() function is also planned to be used for other patches in 2.6.28 (pci_ioremap) so is exported here as part of being introduced.
--- arch/x86/mm/init_32.c | 2 ++ arch/x86/mm/init_64.c | 2 ++ include/linux/ioport.h | 1 + kernel/resource.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 0 deletions(-)
arch/x86/mm/init_32.c +2 -0
@@ -329,6 +329,8 @@ int devmem_is_allowed(unsigned long pagenr) {if (pagenr <=256 )return 1 ; +if (iomem_is_reserved (pagenr <<PAGE_SHIFT )) +return 0 ;if (!page_is_ram (pagenr))return 1 ;return 0 ;
arch/x86/mm/init_64.c +2 -0
@@ -892,6 +892,8 @@ int devmem_is_allowed(unsigned long pagenr) {if (pagenr <=256 )return 1 ; +if (iomem_is_reserved (pagenr <<PAGE_SHIFT )) +return 0 ;if (!page_is_ram (pagenr))return 1 ;return 0 ;
include/linux/ioport.h +1 -0
@@ -170,6 +170,7 @@ extern struct resource * __devm_request_region(struct device *dev,extern void __devm_release_region (struct device *dev,struct resource *parent,resource_size_t start,resource_size_t n);extern int iomem_map_sanity_check (resource_size_t addr,unsigned long size); +extern int iomem_is_reserved (u64 addr);#endif /* __ASSEMBLY__ */ #endif /* _LINUX_IOPORT_H */
kernel/resource.c +32 -0
@@ -864,3 +864,35 @@ int iomem_map_sanity_check(resource_size_t addr, unsigned long size)return err; } + +/* +* check if an address is reserved in the iomem resource tree +* returns 1 if reserved, 0 if not reserved. +*/ +int iomem_is_reserved (u64 addr) +{ +struct resource *p = &iomem_resource; +int err =0 ; +loff_t l; +int size=PAGE_SIZE ; + +read_lock (&resource_lock); +for (p = p->child; p ; p =r_next (NULL , p, &l)) { +/* +* We can probably skip the resources without +* IORESOURCE_IO attribute? +*/ +if (p->start >= addr + size) +continue ; +if (p->end < addr) +continue ; +if (p->flags &IORESOURCE_BUSY ) { + err =1 ; +break ; + } + } +read_unlock (&resource_lock); + +return err; +} +EXPORT_SYMBOL (iomem_is_reserved);
Signature · 7 lines
1.5.5.1 -- Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 31 lines · Arjan van de Ven
From: Arjan van de Ven <[email protected]>
Date: Sun, 5 Oct 2008 18:00:15 -0700
Subject: [PATCH] resource: don't allow/dev/memaccess reserved resourcesDevice drivers that use
pci_request_regions()(and similar APIs) have a reasonable expectation that they are the only ones accessing their device. As part of the e1000e hunt, we were afraid that some userland (X or some bootsplash stuff) was mapping the MMIO region, that the driver thought it had exclusively, via/dev/mem.This patch adds, to the existing config option to restrict
/dev/mem, the reserved regions to the "banned from/dev/memuse" list, so now both kernel memory and device-exclusive MMIO regions are banned.The introduced
iomem_is_reserved()function is also planned to be used for other patches in 2.6.28 (pci_ioremap) so is exported here as part of being introduced.Signed-of-by Arjan van de Ven <[email protected]>--- arch/x86/mm/init_32.c | 2 ++ arch/x86/mm/init_64.c | 2 ++ include/linux/ioport.h | 1 + kernel/resource.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 0 deletions(-)arch/x86/mm/init_32.c +0
-0
arch/x86/mm/init_32.c int devmem_is_allowed(unsigned long pagenr) · line 333
Quoted 5 lines
arch/x86/mm/init_32.c +2
-0@@ -329,6 +329,8 @@ int devmem_is_allowed(unsigned long pagenr) {if (pagenr <=256 )return 1 ; +if (iomem_is_reserved (pagenr <<PAGE_SHIFT )) +return 0 ;
looks good and useful to me. One small request: could you please stick a big fat WARN_ONCE() into this codepath as well?
and it's properly dependent on CONFIG_STRICT_DEVMEM=y [which is default-off], so it's not a legacy ABI breaker either.
another small detail:
Quoted 5 lines
arch/x86/mm/init_32.c +5
-0+int iomem_is_reserved (u64 addr) +{ +struct resource *p = &iomem_resource; +int err =0 ; +loff_t l;
arch/x86/mm/init_32.c int devmem_is_allowed(unsigned long pagenr)
Quoted 10 lines
arch/x86/mm/init_32.c +10
-0+int size=PAGE_SIZE ; + +read_lock (&resource_lock); +for (p = p->child; p ; p =r_next (NULL , p, &l)) { +/* +* We can probably skip the resources without +* IORESOURCE_IO attribute? +*/ +if (p->start >= addr + size) +continue ;
do we want to skip all resources that are not IORESOURCE_MEM? Same holds for iomem_map_sanity_check(), introduced in tip/core/resources:
379daf6: IO resources, x86: ioremap sanity check to catch mapping requests exceeding the BAR sizes
on which you seem to have based iomem_is_reserved().
Perhaps even base both iomem_map_sanity_check() and iomem_is_reserved() on a single helper function, and unify the iterator and the overlap check? The two have a very similar purpose.
Ingo
Quoted 4 lines · On Mon, 6 Oct 2008 07:23:37 +0200 Ingo Molnar <[email protected]> wrote:
Quoted 4 lines
kernel/resource.c +3
-0+int iomem_is_reserved (u64 addr) +{ +struct resource *p = &iomem_resource;
...
Quoted 6 lines
kernel/resource.c int iomem_map_sanity_check(resource_size_t addr, unsigned long size) · line 886
Quoted 3 lines
kernel/resource.c +3
-0+*/ +if (p->start >= addr + size) +continue ;do we want to skip all resources that are not
IORESOURCE_MEM? Same holds foriomem_map_sanity_check(), introduced in tip/core/resources:
if we have non-IORESOURCE_MEM resources in the iomem_resource tree.... then we have bigger problems.
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 3 lines
This patch adds, to the existing config option to restrict
/dev/mem, the reserved regions to the "banned from/dev/memuse" list, so now both kernel memory and device-exclusive MMIO regions are banned.
This breaks a whole class of diagnostic and debug tools which quite intentionally dump the MMIO register space of devices that are live.
Plus it doesn't actually work - if I have /dev/mem open and mmapped you don't pull the pages from under me....
The problem with the way this is going is the more you take from /dev/mem the sooner someone is forced to add /dev/mem-proper which undoes it all again so they can get work done.
It's certainly a useful debug aid to know about such things but I don't believe it's something you should ban.
Alan
Quoted 10 lines · Alan Cox
Quoted 3 lines
This patch adds, to the existing config option to restrict
/dev/mem, the reserved regions to the "banned from/dev/memuse" list, so now both kernel memory and device-exclusive MMIO regions are banned.This breaks a whole class of diagnostic and debug tools which quite intentionally dump the MMIO register space of devices that are live.
Plus it doesn't actually work - if I have
/dev/memopen and mmapped you don't pull the pages from under me....
Quoted 6 lines
The problem with the way this is going is the more you take from
/dev/memthe sooner someone is forced to add/dev/mem-properwhich undoes it all again so they can get work done.It's certainly a useful debug aid to know about such things but I don't believe it's something you should ban.
Exactly,
I think the proper solution is to make /dev/mem pure diagnostic tool.
Drivers should use /sys mappings of pci devices to access their iomem
, or even better they should not touch pci mappings
directly at all, but talk to kernel, and if xorg needs access to video memory it should talk to drm and get its mapping from there.
Best regards,
Maxim Levitsky
Quoted 2 lines
I think the proper solution is to make
/dev/mempure diagnostic tool. Drivers should use /sys mappings of pci devices to access their iomem
Only a tiny fraction of devices in the world are PCI, and those platforms where you most what /dev/mem type debugging tools are often those without PCI
Quoted 3 lines
, or even better they should not touch pci mappings directly at all, but talk to kernel, and if xorg needs access to video memory it should talk to drm and get its mapping from there.
Most video cards don't use DRM, but the kernel does have the same video memory mapped for text consoles.
Alan
Quoted 7 lines · On Mon, 6 Oct 2008 14:48:35 +0100 Alan Cox <[email protected]> wrote:
Quoted 3 lines
I think the proper solution is to make
/dev/mempure diagnostic tool. Drivers should use /sys mappings of pci devices to access their iomemOnly a tiny fraction of devices in the world are PCI, and those platforms where you most what
/dev/memtype debugging tools are often those without PCI
if you looked at the patch it's x86 only, and only active if you set CONFIG_STRICT_DEVMEM.
(if you don't select CONFIG_STRICT_DEVMEM, which is off by default, you get unlimited access as usualy)
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 8 lines
Quoted 3 lines
Only a tiny fraction of devices in the world are PCI, and those platforms where you most what
/dev/memtype debugging tools are often those without PCIif you looked at the patch it's x86 only, and only active if you set
CONFIG_STRICT_DEVMEM.
(if you don't selectCONFIG_STRICT_DEVMEM, which is off by default, you get unlimited access as usualy)
Lots of non PCI devices on x86 systems as well.
And the video problem is a big one for the x86 case as the VGA ISA window is used by both the kernel and X without a helper driver.
Alan
Quoted 9 lines · On Mon, 6 Oct 2008 15:01:22 +0100 Alan Cox <[email protected]> wrote:
Quoted 8 lines
Quoted 3 lines
Only a tiny fraction of devices in the world are PCI, and those platforms where you most what
/dev/memtype debugging tools are often those without PCIif you looked at the patch it's x86 only, and only active if you set
CONFIG_STRICT_DEVMEM.
(if you don't selectCONFIG_STRICT_DEVMEM, which is off by default, you get unlimited access as usualy)
Quoted 4 lines
Lots of non PCI devices on x86 systems as well.
And the video problem is a big one for the x86 case as the VGA ISA window is used by both the kernel and X without a helper driver.
no argument, except that the kernel doesn't do request_region() on it to expect exclusivity (so the patch doesn't do anything on this region)
Now having said that, if the DRM layer does request_region on the MMIO bars, we might need a flag that explicitly says "this is intended for sharing with userspace" for this known case; not too hard, I'll check with Dave Airlie.
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 2 lines
no argument, except that the kernel doesn't do
request_region()on it to expect exclusivity (so the patch doesn't do anything on this region)
Its on the TODO list for the vt fixes
Quoted 4 lines
Now having said that, if the DRM layer does
request_regionon the MMIO bars, we might need a flag that explicitly says "this is intended for sharing with userspace" for this known case; not too hard, I'll check with Dave Airlie.
DRM isn't the problem. In the DRM case the DRM can provide the mappings and manage them. In the non DRM case its messier but for the moment probably happens by luck to be ok
I still think your restrictive /dev/mem model is wrong. I think it comes about because your /dev/mem restrictions are for multiple conflicting purposes.
The root of that is the 'vaguely annoy root kit writers for 5 minutes' reasoning which erroneously leads to trying to a compile time option, combined some would argue with a 'screw people hacking hardware in userspace who should provide drivers' view.
Now the root kit writer one has minimal credence now as it seems the root kit writers already adapted. The hardware hacking people have uio frameworks and should yes be encouraged to use proper drivers.
So there isn't a real reason to make it compile time. Far saner would be for the /dev/mem restrictions to be flags in sysfs. Even if you stick to the view about making life harder for rootkit authors then it should be sysfs restrictions with an irrevocability bit (eg 0 = off 1 = on , 2 = on (forever))
A flag per feature of this kind then lets you set the different policies including the highly useful '/dev/mem, I don't think so' policy flag for server boxes.
Quoted 6 lines · On Mon, 6 Oct 2008 15:14:11 +0100 Alan Cox <[email protected]> wrote:
Quoted 3 lines
no argument, except that the kernel doesn't do
request_region()on it to expect exclusivity (so the patch doesn't do anything on this region)Its on the TODO list for the vt fixes
Quoted 8 lines
Quoted 4 lines
Now having said that, if the DRM layer does
request_regionon the MMIO bars, we might need a flag that explicitly says "this is intended for sharing with userspace" for this known case; not too hard, I'll check with Dave Airlie.DRM isn't the problem. In the DRM case the DRM can provide the mappings and manage them. In the non DRM case its messier but for the moment probably happens by luck to be ok
I'll add a "share resource with userland" option to allow us to make this desire explicit for the cases we want this (and can tolerate concurrent accesses)
Quoted 4 lines
I still think your restrictive
/dev/memmodel is wrong. I think it comes about because your/dev/memrestrictions are for multiple conflicting purposes.
for me it is a goal to have /dev/mem do as little as possible while allowing the "normal" uses. This is to help SELinux to have sane policy rather than "X still has perms to own the whole box" etc.
Quoted 5 lines
The root of that is the 'vaguely annoy root kit writers for 5 minutes' reasoning which erroneously leads to trying to a compile time option, combined some would argue with a 'screw people hacking hardware in userspace who should provide drivers' view.
this patch absolutely has nothing to do with rootkits; really. it came out of chasing e1000e with the "eh who maps our e1000e bar from userspace" scare. Followed by thinking "if the driver requests exclusivity the kernel should try to grant that".
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 3 lines
I'll add a "share resource with userland" option to allow us to make this desire explicit for the cases we want this (and can tolerate concurrent accesses)
For debug tools that would be almost all drivers, and for distribution use that needs to be a runtime selection.
For the non share-resource-with case what occurs if /dev/mem is active when the driver is loaded ?
Quoted 3 lines
for me it is a goal to have
/dev/memdo as little as possible while allowing the "normal" uses. This is to help SELinux to have sane policy rather than "X still has perms to own the whole box" etc.
Then it should be runtime configurable
Quoted 3 lines
it came out of chasing e1000e with the "eh who maps our e1000e bar from userspace" scare. Followed by thinking "if the driver requests exclusivity the kernel should try to grant that".
Only if you can then configure that *policy* decision at runtime in user space. Otherwise you create shackles that simply harm debug work and make it harder for distributions to ship the feature enabled by default (which is the ideal case).
Alan
Quoted 3 lines
For debug tools that would be almost all drivers, and for distribution use that needs to be a runtime selection.
is boottime (read: kernel parameter) ok ?
it would totally make sense to have a kernel boot parameter that disables all /dev/mem sanity checks.
Quoted 3 lines
For the non share-resource-with case what occurs if
/dev/memis active when the driver is loaded ?
that's a hard case but probably not the most interesting case; drivers get loaded early thankfully.
(yes revoke is hard ;( )
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 7 lines · On Mon, 6 Oct 2008 07:57:38 -0700 Arjan van de Ven <[email protected]> wrote:
Quoted 3 lines
For debug tools that would be almost all drivers, and for distribution use that needs to be a runtime selection.
is boottime (read: kernel parameter) ok ?
it would totally make sense to have a kernel boot parameter that disables all/dev/memsanity checks.
A boot parameter probably catches most cases yes.
Quoted 3 lines
that's a hard case but probably not the most interesting case; drivers get loaded early thankfully.
(yes revoke is hard ;( )
Yeah .. I was hoping you'd write it ;)