Skip to content

Commit 7e0165b

Browse files
committed
Merge branch 'akpm' (patches from Andrew)
Merge fixes from Andrew Morton: "6 fixes" * emailed patches from Andrew Morton <[email protected]>: lib/Kconfig.debug: fix some messed up configurations mm: vmscan: protect shrinker idr replace with CONFIG_MEMCG kasan: don't assume percpu shadow allocations will succeed kasan: use apply_to_existing_page_range() for releasing vmalloc shadow mm/memory.c: add apply_to_existing_page_range() helper kasan: fix crashes on access to memory mapped by vm_map_ram()
2 parents 5f096c0 + 045f6d7 commit 7e0165b

File tree

7 files changed

+258
-163
lines changed

7 files changed

+258
-163
lines changed

include/linux/kasan.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,23 @@ static inline void *kasan_reset_tag(const void *addr)
205205
#endif /* CONFIG_KASAN_SW_TAGS */
206206

207207
#ifdef CONFIG_KASAN_VMALLOC
208-
int kasan_populate_vmalloc(unsigned long requested_size,
209-
struct vm_struct *area);
210-
void kasan_poison_vmalloc(void *start, unsigned long size);
208+
int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
209+
void kasan_poison_vmalloc(const void *start, unsigned long size);
210+
void kasan_unpoison_vmalloc(const void *start, unsigned long size);
211211
void kasan_release_vmalloc(unsigned long start, unsigned long end,
212212
unsigned long free_region_start,
213213
unsigned long free_region_end);
214214
#else
215-
static inline int kasan_populate_vmalloc(unsigned long requested_size,
216-
struct vm_struct *area)
215+
static inline int kasan_populate_vmalloc(unsigned long start,
216+
unsigned long size)
217217
{
218218
return 0;
219219
}
220220

221-
static inline void kasan_poison_vmalloc(void *start, unsigned long size) {}
221+
static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
222+
{ }
223+
static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
224+
{ }
222225
static inline void kasan_release_vmalloc(unsigned long start,
223226
unsigned long end,
224227
unsigned long free_region_start,

include/linux/mm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,9 @@ static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags)
26212621
typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data);
26222622
extern int apply_to_page_range(struct mm_struct *mm, unsigned long address,
26232623
unsigned long size, pte_fn_t fn, void *data);
2624+
extern int apply_to_existing_page_range(struct mm_struct *mm,
2625+
unsigned long address, unsigned long size,
2626+
pte_fn_t fn, void *data);
26242627

26252628
#ifdef CONFIG_PAGE_POISONING
26262629
extern bool page_poisoning_enabled(void);

lib/Kconfig.debug

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,55 @@ config PROVIDE_OHCI1394_DMA_INIT
14831483

14841484
See Documentation/debugging-via-ohci1394.txt for more information.
14851485

1486+
source "samples/Kconfig"
1487+
1488+
config ARCH_HAS_DEVMEM_IS_ALLOWED
1489+
bool
1490+
1491+
config STRICT_DEVMEM
1492+
bool "Filter access to /dev/mem"
1493+
depends on MMU && DEVMEM
1494+
depends on ARCH_HAS_DEVMEM_IS_ALLOWED
1495+
default y if PPC || X86 || ARM64
1496+
help
1497+
If this option is disabled, you allow userspace (root) access to all
1498+
of memory, including kernel and userspace memory. Accidental
1499+
access to this is obviously disastrous, but specific access can
1500+
be used by people debugging the kernel. Note that with PAT support
1501+
enabled, even in this case there are restrictions on /dev/mem
1502+
use due to the cache aliasing requirements.
1503+
1504+
If this option is switched on, and IO_STRICT_DEVMEM=n, the /dev/mem
1505+
file only allows userspace access to PCI space and the BIOS code and
1506+
data regions. This is sufficient for dosemu and X and all common
1507+
users of /dev/mem.
1508+
1509+
If in doubt, say Y.
1510+
1511+
config IO_STRICT_DEVMEM
1512+
bool "Filter I/O access to /dev/mem"
1513+
depends on STRICT_DEVMEM
1514+
help
1515+
If this option is disabled, you allow userspace (root) access to all
1516+
io-memory regardless of whether a driver is actively using that
1517+
range. Accidental access to this is obviously disastrous, but
1518+
specific access can be used by people debugging kernel drivers.
1519+
1520+
If this option is switched on, the /dev/mem file only allows
1521+
userspace access to *idle* io-memory ranges (see /proc/iomem) This
1522+
may break traditional users of /dev/mem (dosemu, legacy X, etc...)
1523+
if the driver using a given range cannot be disabled.
1524+
1525+
If in doubt, say Y.
1526+
1527+
menu "$(SRCARCH) Debugging"
1528+
1529+
source "arch/$(SRCARCH)/Kconfig.debug"
1530+
1531+
endmenu
1532+
1533+
menu "Kernel Testing and Coverage"
1534+
14861535
source "lib/kunit/Kconfig"
14871536

14881537
config NOTIFIER_ERROR_INJECTION
@@ -1643,10 +1692,6 @@ config FAULT_INJECTION_STACKTRACE_FILTER
16431692
help
16441693
Provide stacktrace filter for fault-injection capabilities
16451694

1646-
endmenu # "Kernel Testing and Coverage"
1647-
1648-
menu "Kernel Testing and Coverage"
1649-
16501695
config ARCH_HAS_KCOV
16511696
bool
16521697
help
@@ -2130,52 +2175,7 @@ config MEMTEST
21302175
memtest=17, mean do 17 test patterns.
21312176
If you are unsure how to answer this question, answer N.
21322177

2133-
source "samples/Kconfig"
2134-
2135-
config ARCH_HAS_DEVMEM_IS_ALLOWED
2136-
bool
2137-
2138-
config STRICT_DEVMEM
2139-
bool "Filter access to /dev/mem"
2140-
depends on MMU && DEVMEM
2141-
depends on ARCH_HAS_DEVMEM_IS_ALLOWED
2142-
default y if PPC || X86 || ARM64
2143-
---help---
2144-
If this option is disabled, you allow userspace (root) access to all
2145-
of memory, including kernel and userspace memory. Accidental
2146-
access to this is obviously disastrous, but specific access can
2147-
be used by people debugging the kernel. Note that with PAT support
2148-
enabled, even in this case there are restrictions on /dev/mem
2149-
use due to the cache aliasing requirements.
2150-
2151-
If this option is switched on, and IO_STRICT_DEVMEM=n, the /dev/mem
2152-
file only allows userspace access to PCI space and the BIOS code and
2153-
data regions. This is sufficient for dosemu and X and all common
2154-
users of /dev/mem.
2155-
2156-
If in doubt, say Y.
21572178

2158-
config IO_STRICT_DEVMEM
2159-
bool "Filter I/O access to /dev/mem"
2160-
depends on STRICT_DEVMEM
2161-
---help---
2162-
If this option is disabled, you allow userspace (root) access to all
2163-
io-memory regardless of whether a driver is actively using that
2164-
range. Accidental access to this is obviously disastrous, but
2165-
specific access can be used by people debugging kernel drivers.
2166-
2167-
If this option is switched on, the /dev/mem file only allows
2168-
userspace access to *idle* io-memory ranges (see /proc/iomem) This
2169-
may break traditional users of /dev/mem (dosemu, legacy X, etc...)
2170-
if the driver using a given range cannot be disabled.
2171-
2172-
If in doubt, say Y.
2173-
2174-
menu "$(SRCARCH) Debugging"
2175-
2176-
source "arch/$(SRCARCH)/Kconfig.debug"
2177-
2178-
endmenu
21792179

21802180
config HYPERV_TESTING
21812181
bool "Microsoft Hyper-V driver testing"
@@ -2184,4 +2184,6 @@ config HYPERV_TESTING
21842184
help
21852185
Select this option to enable Hyper-V vmbus testing.
21862186

2187+
endmenu # "Kernel Testing and Coverage"
2188+
21872189
endmenu # Kernel hacking

mm/kasan/common.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,17 @@ static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr,
778778
return 0;
779779
}
780780

781-
int kasan_populate_vmalloc(unsigned long requested_size, struct vm_struct *area)
781+
int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
782782
{
783783
unsigned long shadow_start, shadow_end;
784784
int ret;
785785

786-
shadow_start = (unsigned long)kasan_mem_to_shadow(area->addr);
786+
if (!is_vmalloc_or_module_addr((void *)addr))
787+
return 0;
788+
789+
shadow_start = (unsigned long)kasan_mem_to_shadow((void *)addr);
787790
shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE);
788-
shadow_end = (unsigned long)kasan_mem_to_shadow(area->addr +
789-
area->size);
791+
shadow_end = (unsigned long)kasan_mem_to_shadow((void *)addr + size);
790792
shadow_end = ALIGN(shadow_end, PAGE_SIZE);
791793

792794
ret = apply_to_page_range(&init_mm, shadow_start,
@@ -797,10 +799,6 @@ int kasan_populate_vmalloc(unsigned long requested_size, struct vm_struct *area)
797799

798800
flush_cache_vmap(shadow_start, shadow_end);
799801

800-
kasan_unpoison_shadow(area->addr, requested_size);
801-
802-
area->flags |= VM_KASAN;
803-
804802
/*
805803
* We need to be careful about inter-cpu effects here. Consider:
806804
*
@@ -843,12 +841,23 @@ int kasan_populate_vmalloc(unsigned long requested_size, struct vm_struct *area)
843841
* Poison the shadow for a vmalloc region. Called as part of the
844842
* freeing process at the time the region is freed.
845843
*/
846-
void kasan_poison_vmalloc(void *start, unsigned long size)
844+
void kasan_poison_vmalloc(const void *start, unsigned long size)
847845
{
846+
if (!is_vmalloc_or_module_addr(start))
847+
return;
848+
848849
size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
849850
kasan_poison_shadow(start, size, KASAN_VMALLOC_INVALID);
850851
}
851852

853+
void kasan_unpoison_vmalloc(const void *start, unsigned long size)
854+
{
855+
if (!is_vmalloc_or_module_addr(start))
856+
return;
857+
858+
kasan_unpoison_shadow(start, size);
859+
}
860+
852861
static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr,
853862
void *unused)
854863
{
@@ -948,6 +957,7 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
948957
{
949958
void *shadow_start, *shadow_end;
950959
unsigned long region_start, region_end;
960+
unsigned long size;
951961

952962
region_start = ALIGN(start, PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
953963
region_end = ALIGN_DOWN(end, PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
@@ -970,9 +980,11 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
970980
shadow_end = kasan_mem_to_shadow((void *)region_end);
971981

972982
if (shadow_end > shadow_start) {
973-
apply_to_page_range(&init_mm, (unsigned long)shadow_start,
974-
(unsigned long)(shadow_end - shadow_start),
975-
kasan_depopulate_vmalloc_pte, NULL);
983+
size = shadow_end - shadow_start;
984+
apply_to_existing_page_range(&init_mm,
985+
(unsigned long)shadow_start,
986+
size, kasan_depopulate_vmalloc_pte,
987+
NULL);
976988
flush_tlb_kernel_range((unsigned long)shadow_start,
977989
(unsigned long)shadow_end);
978990
}

0 commit comments

Comments
 (0)