Add KernelMemorySanitizer infrastructure
Subsystems (37)
Re: [PATCH RFC v3 23/36] kmsan: call KMSAN hooks where needed
In reply to [email protected]Quoted 3 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Insert KMSAN hooks that check for potential memory errors and/or make necessary bookkeeping changes:
I think it makes sense to split this patch into 2+ parts, the first one adds hooks for internal KMSAN bookkeeping:
Quoted 7 lines
- allocate/split/deallocate metadata pages in alloc_pages()/split_page()/free_page();
- clear page shadow and origins inclear_page(),copy_user_highpage();
- copy page metadata incopy_highpage(),wp_page_copy();
- handle vmap()/vunmap()/iounmap();
- handle task creation and deletion;
- call softirq entry/exit hooks inkernel/softirq.c;
And the other ones do other things:
Quoted 493 lines
- initialize result of
vscnprintf()invprintk_store();
- check/initialize memory sent to/read from USB, I2C, and networkSigned-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Wolfram Sang <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---v2:
- dropped call tokmsan_handle_vprintk, updated comment inprintk.cv3:
- putKMSAN_INIT_VALUEon a separate line invprintk_store()
- dropped call tokmsan_handle_i2c_transfer()Change-Id: I1250a928d9263bf71fdaa067a070bdee686ef47b
--- arch/x86/include/asm/page_64.h | 13 +++++++++++++ arch/x86/mm/ioremap.c | 3 +++ drivers/usb/core/urb.c | 2 ++ include/linux/highmem.h | 4 ++++ kernel/exit.c | 2 ++ kernel/fork.c | 2 ++ kernel/kthread.c | 2 ++ kernel/printk/printk.c | 6 ++++++ kernel/softirq.c | 5 +++++ lib/ioremap.c | 5 +++++ mm/compaction.c | 9 +++++++++ mm/gup.c | 3 +++ mm/memory.c | 2 ++ mm/page_alloc.c | 16 ++++++++++++++++ mm/vmalloc.c | 23 +++++++++++++++++++++-- net/sched/sch_generic.c | 2 ++ 16 files changed, 97 insertions(+), 2 deletions(-)arch/x86/include/asm/page_64.h +13
-0@@ -44,14 +44,27 @@ void clear_page_orig(void *page);void clear_page_rep (void *page);void clear_page_erms (void *page); +/* This is an assembly header, avoid including too much of kmsan.h */ +#ifdef CONFIG_KMSAN +void kmsan_clear_page (void *page_addr); +#endif +__no_sanitize_memory static inline void clear_page(void *page) { +#ifdef CONFIG_KMSAN +/* alternative_call_2() changes |page|. */ +void *page_copy = page; +#endif alternative_call_2 (clear_page_orig, clear_page_rep,X86_FEATURE_REP_GOOD , clear_page_erms,X86_FEATURE_ERMS ,"=D" (page), "0" (page) : "cc", "memory", "rax", "rcx"); +#ifdef CONFIG_KMSAN +/* Clear KMSAN shadow for the pages that have it. */ +kmsan_clear_page (page_copy); +#endif }void copy_page (void *to,void *from);arch/x86/mm/ioremap.c +3
-0@@ -7,6 +7,7 @@* (C) Copyright 1995 1996 Linus Torvalds */ +#include <linux/kmsan.h> #include <linux/memblock.h> #include <linux/init.h> #include <linux/io.h> @@ -451,6 +452,8 @@ void iounmap(volatile void __iomem *addr)return ; } +kmsan_iounmap_page_range ((unsigned long )addr, + (unsigned long )addr +get_vm_area_size (p));free_memtype (p->phys_addr, p->phys_addr +get_vm_area_size (p));/* Finally remove it */ drivers/usb/core/urb.c +2
-0@@ -8,6 +8,7 @@#include <linux/bitops.h> #include <linux/slab.h> #include <linux/log2.h> +#include <linux/kmsan-checks.h> #include <linux/usb.h> #include <linux/wait.h> #include <linux/usb/hcd.h> @@ -401,6 +402,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)URB_SETUP_MAP_SINGLE |URB_SETUP_MAP_LOCAL | URB_DMA_SG_COMBINED); urb->transfer_flags |= (is_out ?URB_DIR_OUT :URB_DIR_IN ); +kmsan_handle_urb (urb, is_out);if (xfertype !=USB_ENDPOINT_XFER_CONTROL && dev->state <USB_STATE_CONFIGURED )include/linux/highmem.h +4
-0@@ -5,6 +5,7 @@#include <linux/fs.h> #include <linux/kernel.h> #include <linux/bug.h> +#include <linux/kmsan.h> #include <linux/mm.h> #include <linux/uaccess.h> #include <linux/hardirq.h> @@ -255,6 +256,8 @@ static inline void copy_user_highpage(struct page *to, struct page *from, vfrom =kmap_atomic (from); vto =kmap_atomic (to);copy_user_page (vto, vfrom, vaddr, to); +/* User pages don't have shadow, just clear the destination. */ +kmsan_clear_page (page_address (to));kunmap_atomic (vto);kunmap_atomic (vfrom); }@@ -270,6 +273,7 @@ static inline void copy_highpage(struct page *to, struct page *from) vfrom =kmap_atomic (from); vto =kmap_atomic (to);copy_page (vto, vfrom); +kmsan_copy_page_meta (to, from);kunmap_atomic (vto);kunmap_atomic (vfrom); }kernel/exit.c +2
-0@@ -60,6 +60,7 @@#include <linux/writeback.h> #include <linux/shm.h> #include <linux/kcov.h> +#include <linux/kmsan.h> #include <linux/random.h> #include <linux/rcuwait.h> #include <linux/compat.h> @@ -719,6 +720,7 @@ void __noreturn do_exit(long code)profile_task_exit (tsk);kcov_task_exit (tsk); +kmsan_task_exit (tsk);WARN_ON (blk_needs_flush_plug (tsk));kernel/fork.c +2
-0@@ -37,6 +37,7 @@#include <linux/fdtable.h> #include <linux/iocontext.h> #include <linux/key.h> +#include <linux/kmsan.h> #include <linux/binfmts.h> #include <linux/mman.h> #include <linux/mmu_notifier.h> @@ -931,6 +932,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)account_kernel_stack (tsk,1 );kcov_task_init (tsk); +kmsan_task_create (tsk);#ifdef CONFIG_FAULT_INJECTION tsk->fail_nth =0 ;kernel/kthread.c +2
-0@@ -17,6 +17,7 @@#include <linux/unistd.h> #include <linux/file.h> #include <linux/export.h> +#include <linux/kmsan.h> #include <linux/mutex.h> #include <linux/slab.h> #include <linux/freezer.h> @@ -350,6 +351,7 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),set_cpus_allowed_ptr (task, cpu_all_mask); }kfree (create); +kmsan_task_create (task);return task; }kernel/printk/printk.c +6
-0@@ -1915,6 +1915,12 @@ int vprintk_store(int facility, int level,* prefix which might be passed-in as a parameter. */ text_len =vscnprintf (text,sizeof (textbuf), fmt, args); +/* +* If any of vscnprintf() arguments is uninitialized, KMSAN will report +* one or more errors and also probably mark text_len as uninitialized. +* Initialize |text_len| to prevent the errors from spreading further. +*/ + text_len =KMSAN_INIT_VALUE (text_len);/* mark and strip a trailing newline */ if (text_len && text[text_len-1 ] =='\n' ) {kernel/softirq.c +5
-0@@ -11,6 +11,7 @@#include <linux/export.h> #include <linux/kernel_stat.h> +#include <linux/kmsan.h> #include <linux/interrupt.h> #include <linux/init.h> #include <linux/mm.h> @@ -370,7 +371,9 @@ static inline void invoke_softirq(void)* it is the irq stack, because it should be near empty * at this stage. */ +kmsan_softirq_enter ();__do_softirq (); +kmsan_softirq_exit ();#else /* * Otherwise, irq_exit() is called on the task stack that can @@ -600,7 +603,9 @@ static void run_ksoftirqd(unsigned int cpu)* We can safely run softirq on inline stack, as we are not deep * in the task stack here. */ +kmsan_softirq_enter ();__do_softirq (); +kmsan_softirq_exit ();local_irq_enable ();cond_resched ();return ;lib/ioremap.c +5
-0@@ -6,6 +6,7 @@* * (C) Copyright 1995 1996 Linus Torvalds */ +#include <linux/kmsan.h> #include <linux/vmalloc.h> #include <linux/mm.h> #include <linux/sched.h> @@ -214,6 +215,8 @@ int ioremap_page_range(unsigned long addr,unsigned long start;unsigned long next;int err; +unsigned long old_addr = addr; +phys_addr_t old_phys_addr = phys_addr;might_sleep ();BUG_ON (addr >= end);@@ -228,6 +231,8 @@ int ioremap_page_range(unsigned long addr, }while (pgd++, phys_addr += (next - addr), addr = next, addr != end);flush_cache_vmap (start, end); +if (!err) +kmsan_ioremap_page_range (old_addr, end, old_phys_addr, prot);return err; }mm/compaction.c +9
-0@@ -84,6 +84,15 @@ static void split_map_pages(struct list_head *list)for (i =0 ; i < nr_pages; i++) {list_add (&page->lru, &tmp_list); +#ifdef CONFIG_KMSAN +/* +* TODO(glider): we may lose the metadata when copying +* something to these pages. Need to allocate shadow +* and origin pages here instead. +*/ + page->shadow =NULL ; + page->origin =NULL ; +#endif page++; } }mm/gup.c +3
-0@@ -4,6 +4,7 @@#include <linux/err.h> #include <linux/spinlock.h> +#include <linux/kmsan.h> #include <linux/mm.h> #include <linux/memremap.h> #include <linux/pagemap.h> @@ -2349,6 +2350,7 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write, gup_fast_permitted(start, end)) {local_irq_save (flags);gup_pgd_range (start, end, write ?FOLL_WRITE :0 , pages, &nr); +kmsan_gup_pgd_range (pages, nr);local_irq_restore (flags); }@@ -2418,6 +2420,7 @@ int get_user_pages_fast(unsigned long start, int nr_pages, gup_fast_permitted(start, end)) {local_irq_disable ();gup_pgd_range (addr, end, gup_flags, pages, &nr); +kmsan_gup_pgd_range (pages, nr);local_irq_enable (); ret = nr; }mm/memory.c +2
-0@@ -51,6 +51,7 @@#include <linux/highmem.h> #include <linux/pagemap.h> #include <linux/memremap.h> +#include <linux/kmsan.h> #include <linux/ksm.h> #include <linux/rmap.h> #include <linux/export.h> @@ -2328,6 +2329,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)if (!new_page) goto oom;cow_user_page (new_page, old_page, vmf->address, vma); +kmsan_copy_page_meta (new_page, old_page); }if (mem_cgroup_try_charge_delay (new_page, mm,GFP_KERNEL , &memcg, false))mm/page_alloc.c +16
-0@@ -26,6 +26,8 @@#include <linux/compiler.h> #include <linux/kernel.h> #include <linux/kasan.h> +#include <linux/kmsan.h> +#include <linux/kmsan-checks.h> #include <linux/module.h> #include <linux/suspend.h> #include <linux/pagevec.h> @@ -1133,6 +1135,7 @@ static __always_inline bool free_pages_prepare(struct page *page,VM_BUG_ON_PAGE (PageTail (page), page);trace_mm_page_free (page, order); +kmsan_free_page (page, order);/* * Check tail pages before head page information is cleared to @@ -3121,6 +3124,7 @@ void split_page(struct page *page, unsigned int order)VM_BUG_ON_PAGE (PageCompound (page), page);VM_BUG_ON_PAGE (!page_count (page), page); +kmsan_split_page (page, order);for (i =1 ; i < (1 << order); i++)set_page_refcounted (page + i);split_page_owner (page, order);@@ -3253,6 +3257,13 @@ static struct page *rmqueue_pcplist(struct zone *preferred_zone,/* * Allocate a page from the given zone. Use pcplists for order-0 allocations. */ +/* +* TODO(glider): rmqueue() may call __msan_poison_alloca() through a call to +* set_pfnblock_flags_mask(). If __msan_poison_alloca() attempts to allocate +* pages for the stack depot, it may call rmqueue() again, which will result +* in a deadlock. +*/ +__no_sanitize_memory static inline struct page *rmqueue(struct zone *preferred_zone, struct zone *zone, unsigned int order,@@ -4781,6 +4792,11 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,trace_mm_page_alloc (page, order, alloc_mask, ac.migratetype); +if (page) +if (kmsan_alloc_page (page, order, gfp_mask)) { +__free_pages (page, order); + page =NULL ; + }return page; }EXPORT_SYMBOL (__alloc_pages_nodemask);mm/vmalloc.c +21
-2@@ -29,6 +29,7 @@#include <linux/rcupdate.h> #include <linux/pfn.h> #include <linux/kmemleak.h> +#include <linux/kmsan.h> #include <linux/atomic.h> #include <linux/compiler.h> #include <linux/llist.h> @@ -119,7 +120,8 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end) }while (p4d++, addr = next, addr != end); } -static void vunmap_page_range (unsigned long addr,unsigned long end) +/* Exported for KMSAN, visible in mm/kmsan/kmsan.h only. */ +void __vunmap_page_range (unsigned long addr,unsigned long end) {pgd_t *pgd;unsigned long next;@@ -133,6 +135,12 @@ static void vunmap_page_range(unsigned long addr, unsigned long end)vunmap_p4d_range (pgd, addr, next); }while (pgd++, addr = next, addr != end); } +EXPORT_SYMBOL (__vunmap_page_range); +static void vunmap_page_range (unsigned long addr,unsigned long end) +{ +kmsan_vunmap_page_range (addr, end); +__vunmap_page_range (addr, end); +}static int vmap_pte_range (pmd_t *pmd,unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,int *nr)@@ -216,8 +224,11 @@ static int vmap_p4d_range(pgd_t *pgd, unsigned long addr,* will have pfns corresponding to the "pages" array. * * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] +* +* This function is exported for use in KMSAN, but is only declared in KMSAN +* headers. */ -static int vmap_page_range_noflush (unsigned long start,unsigned long end, +int __vmap_page_range_noflush (unsigned long start,unsigned long end,pgprot_t prot,struct page **pages) {pgd_t *pgd;@@ -237,6 +248,14 @@ static int vmap_page_range_noflush(unsigned long start, unsigned long end,return nr; } +EXPORT_SYMBOL (__vmap_page_range_noflush); + +static int vmap_page_range_noflush (unsigned long start,unsigned long end, +pgprot_t prot,struct page **pages) +{ +kmsan_vmap_page_range_noflush (start, end, prot, pages); +return __vmap_page_range_noflush (start, end, prot, pages); +}static int vmap_page_range (unsigned long start,unsigned long end,pgprot_t prot,struct page **pages)net/sched/sch_generic.c +2
-1@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/types.h> #include <linux/kernel.h> +#include <linux/kmsan-checks.h> #include <linux/sched.h> #include <linux/string.h> #include <linux/errno.h> @@ -659,6 +660,7 @@ static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc) }else { qdisc->empty = true; } +kmsan_check_skb (skb);return skb; } --2.24.0.432.g9d3f5f5b63-goog
Alexander Potapenko
maintainerRe: [PATCH RFC v3 23/36] kmsan: call KMSAN hooks where needed
Quoted 9 lines · Andrey Konovalov
Quoted 3 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Insert KMSAN hooks that check for potential memory errors and/or make necessary bookkeeping changes:
I think it makes sense to split this patch into 2+ parts, the first one adds hooks for internal KMSAN bookkeeping:
Done in v4.
[email protected]
maintainerRFC PATCH v3 24/36 kmsan: disable instrumentation of certain functions
In reply to [email protected]Some functions are called from handwritten assembly, and therefore don't have their arguments' metadata fully set up by the instrumentation code. Mark them with __no_sanitize_memory to avoid false positives from spreading further.
Certain functions perform task switching, so that the value of |current| is different as they proceed. Because KMSAN state pointer is only read once at the beginning of the function, touching it after |current| has changed may be dangerous.
To: Alexander Potapenko <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- removed TODOs from comments
Change-Id: I684d23dac5a22eb0a4cea71993cb934302b17cea
--- arch/x86/entry/common.c | 1 + arch/x86/include/asm/irq_regs.h | 1 + arch/x86/include/asm/syscall_wrapper.h | 1 + arch/x86/kernel/apic/apic.c | 1 + arch/x86/kernel/dumpstack_64.c | 1 + arch/x86/kernel/process_64.c | 5 +++++ arch/x86/kernel/traps.c | 12 ++++++++++-- arch/x86/kernel/uprobes.c | 7 ++++++- kernel/profile.c | 1 + kernel/sched/core.c | 6 ++++++ 10 files changed, 33 insertions(+), 3 deletions(-)
arch/x86/entry/common.c +1 -0
@@ -275,6 +275,7 @@ __visible inline void syscall_return_slowpath(struct pt_regs *regs) }#ifdef CONFIG_X86_64 +__no_sanitize_memory __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs) {struct thread_info *ti;
arch/x86/include/asm/irq_regs.h +1 -0
@@ -14,6 +14,7 @@ DECLARE_PER_CPU(struct pt_regs *, irq_regs); +__no_sanitize_memory static inline struct pt_regs *get_irq_regs(void) {return __this_cpu_read (irq_regs);
arch/x86/include/asm/syscall_wrapper.h +1 -0
@@ -159,6 +159,7 @@ ALLOW_ERROR_INJECTION(__x64_sys##name, ERRNO); \ static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \ static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\ + __no_sanitize_memory \ asmlinkage long __x64_sys##name(const struct pt_regs *regs) \ { \ return __se_sys##name(SC_X86_64_REGS_TO_ARGS(x,__VA_ARGS__));\
arch/x86/kernel/apic/apic.c +1 -0
@@ -1118,6 +1118,7 @@ static void local_apic_timer_interrupt(void)* [ if a single-CPU system runs an SMP kernel then we call the local * interrupt as well. Thus we cannot inline the local irq ... ] */ +__no_sanitize_memory /* |regs| may be uninitialized */ __visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs) {struct pt_regs *old_regs =set_irq_regs (regs);
arch/x86/kernel/dumpstack_64.c +1 -0
@@ -143,6 +143,7 @@ static bool in_irq_stack(unsigned long *stack, struct stack_info *info)return true; } +__no_sanitize_memory int get_stack_info(unsigned long *stack, struct task_struct *task,struct stack_info *info,unsigned long *visit_mask) {
arch/x86/kernel/process_64.c +5 -0
@@ -500,6 +500,11 @@ void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp)* Kprobes not supported here. Set the probe on schedule instead. * Function graph tracer not supported too. */ +/* +* Avoid touching KMSAN state or reporting anything here, as __switch_to() does +* weird things with tasks. +*/ +__no_sanitize_memory __visible __notrace_funcgraph struct task_struct *__switch_to (struct task_struct *prev_p,struct task_struct *next_p) {
arch/x86/kernel/traps.c +10 -2
@@ -618,7 +618,10 @@ NOKPROBE_SYMBOL(do_int3);* Help handler running on a per-cpu (IST or entry trampoline) stack * to switch to the normal thread stack if the interrupted code was in * user mode. The actual stack switch is done in entry_64.S +* +* This function switches the registers - don't instrument it with KMSAN! */ +__no_sanitize_memory asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs) {struct pt_regs *regs = (struct pt_regs *)this_cpu_read (cpu_current_top_of_stack) -1 ;
@@ -634,6 +637,11 @@ struct bad_iret_stack { }; asmlinkage __visible notrace +/* +* Dark magic happening here, let's not instrument this function. +* Also avoid copying any metadata by using raw __memmove(). +*/ +__no_sanitize_memorystruct bad_iret_stack *fixup_bad_iret (struct bad_iret_stack *s) {/*
@@ -648,10 +656,10 @@ struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) (struct bad_iret_stack *)this_cpu_read (cpu_tss_rw.x86_tss.sp0) -1 ;/* Copy the IRET target to the new stack. */ -memmove (&new_stack->regs.ip, (void *)s->regs.sp,5 *8 ); +__memmove (&new_stack->regs.ip, (void *)s->regs.sp,5 *8 );/* Copy the remainder of the stack from the current stack. */ - memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip)); + __memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));BUG_ON (!user_mode (&new_stack->regs));return new_stack;
arch/x86/kernel/uprobes.c +6 -1
@@ -8,6 +8,7 @@* Jim Keniston */ #include <linux/kernel.h> +#include <linux/kmsan-checks.h> #include <linux/sched.h> #include <linux/ptrace.h> #include <linux/uprobes.h>
@@ -997,9 +998,13 @@ int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)int arch_uprobe_exception_notify (struct notifier_block *self,unsigned long val,void *data) {struct die_args *args = data; -struct pt_regs *regs = args->regs; +struct pt_regs *regs;int ret =NOTIFY_DONE ; +kmsan_unpoison_shadow (args,sizeof (*args)); + regs = args->regs; +if (regs) +kmsan_unpoison_shadow (regs,sizeof (*regs));/* We are only interested in userspace traps */ if (regs && !user_mode (regs))return NOTIFY_DONE ;
kernel/profile.c +1 -0
@@ -399,6 +399,7 @@ void profile_hits(int type, void *__pc, unsigned int nr_hits) }EXPORT_SYMBOL_GPL (profile_hits); +__no_sanitize_memory void profile_tick(int type) {struct pt_regs *regs =get_irq_regs ();
kernel/sched/core.c +6 -0
@@ -475,6 +475,7 @@ void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task)put_task_struct (task); } +__no_sanitize_memory /* context switching here */ void wake_up_q(struct wake_q_head *head) {struct wake_q_node *node = head->first;
@@ -3180,6 +3181,7 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev,* past. prev == current is still correct but we need to recalculate this_rq * because prev may have moved to another CPU. */ +__no_sanitize_memory /* |current| changes here */ static struct rq *finish_task_switch(struct task_struct *prev) __releases(rq->lock) {
@@ -3986,6 +3988,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)* * WARNING: must be called with preemption disabled! */ +__no_sanitize_memory /* |current| changes here */ static void __sched notrace __schedule(bool preempt) {struct task_struct *prev, *next;
@@ -4605,6 +4608,7 @@ int task_prio(const struct task_struct *p)* * Return: 1 if the CPU is currently idle. 0 otherwise. */ +__no_sanitize_memory /* nothing to report here */ int idle_cpu(int cpu) {struct rq *rq =cpu_rq (cpu);
@@ -6544,6 +6548,7 @@ static struct kmem_cache *task_group_cache __read_mostly;DECLARE_PER_CPU (cpumask_var_t, load_balance_mask);DECLARE_PER_CPU (cpumask_var_t, select_idle_mask); +__no_sanitize_memory void __init sched_init(void) {unsigned long ptr =0 ;
@@ -6716,6 +6721,7 @@ static inline int preempt_count_equals(int preempt_offset)return (nested == preempt_offset); } +__no_sanitize_memory /* expect the arguments to be initialized */ void __might_sleep(const char *file, int line, int preempt_offset) {/*
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Re: [PATCH RFC v3 24/36] kmsan: disable instrumentation of certain functions
Quoted 76 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Some functions are called from handwritten assembly, and therefore don't have their arguments' metadata fully set up by the instrumentation code. Mark them with
__no_sanitize_memoryto avoid false positives from spreading further.
Certain functions perform task switching, so that the value of |current| is different as they proceed. Because KMSAN state pointer is only read once at the beginning of the function, touching it after |current| has changed may be dangerous.Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- removed TODOs from commentsChange-Id: I684d23dac5a22eb0a4cea71993cb934302b17cea
--- arch/x86/entry/common.c | 1 + arch/x86/include/asm/irq_regs.h | 1 + arch/x86/include/asm/syscall_wrapper.h | 1 + arch/x86/kernel/apic/apic.c | 1 + arch/x86/kernel/dumpstack_64.c | 1 + arch/x86/kernel/process_64.c | 5 +++++ arch/x86/kernel/traps.c | 12 ++++++++++-- arch/x86/kernel/uprobes.c | 7 ++++++- kernel/profile.c | 1 + kernel/sched/core.c | 6 ++++++ 10 files changed, 33 insertions(+), 3 deletions(-)arch/x86/entry/common.c +1
-0@@ -275,6 +275,7 @@ __visible inline void syscall_return_slowpath(struct pt_regs *regs) }#ifdef CONFIG_X86_64 +__no_sanitize_memory __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs) {struct thread_info *ti;arch/x86/include/asm/irq_regs.h +1
-0@@ -14,6 +14,7 @@ DECLARE_PER_CPU(struct pt_regs *, irq_regs); +__no_sanitize_memory static inline struct pt_regs *get_irq_regs(void) {return __this_cpu_read (irq_regs);arch/x86/include/asm/syscall_wrapper.h +1
-0@@ -159,6 +159,7 @@ ALLOW_ERROR_INJECTION(__x64_sys##name, ERRNO); \ static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \ static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\ + __no_sanitize_memory \ asmlinkage long __x64_sys##name(const struct pt_regs *regs) \ { \ return __se_sys##name(SC_X86_64_REGS_TO_ARGS(x,__VA_ARGS__));\arch/x86/kernel/apic/apic.c +0
-0
arch/x86/kernel/apic/apic.c static void local_apic_timer_interrupt(void) · line 1121
Quoted 4 lines
arch/x86/kernel/apic/apic.c +1
-0@@ -1118,6 +1118,7 @@ static void local_apic_timer_interrupt(void)* [ if a single-CPU system runs an SMP kernel then we call the local * interrupt as well. Thus we cannot inline the local irq ... ] */ +__no_sanitize_memory/* |regs| may be uninitialized */
The comment style around __no_sanitize_memory looks very different for different call sites, perhaps it makes sense to unify it somehow.
Quoted 59 lines
arch/x86/kernel/apic/apic.c +0
-0__visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs) {struct pt_regs *old_regs =set_irq_regs (regs);arch/x86/kernel/dumpstack_64.c +1
-0@@ -143,6 +143,7 @@ static bool in_irq_stack(unsigned long *stack, struct stack_info *info)return true; } +__no_sanitize_memory int get_stack_info(unsigned long *stack, struct task_struct *task,struct stack_info *info,unsigned long *visit_mask) {arch/x86/kernel/process_64.c +5
-0@@ -500,6 +500,11 @@ void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp)* Kprobes not supported here. Set the probe on schedule instead. * Function graph tracer not supported too. */ +/* +* Avoid touching KMSAN state or reporting anything here, as __switch_to() does +* weird things with tasks. +*/ +__no_sanitize_memory __visible __notrace_funcgraph struct task_struct *__switch_to (struct task_struct *prev_p,struct task_struct *next_p) {arch/x86/kernel/traps.c +8
-0@@ -618,7 +618,10 @@ NOKPROBE_SYMBOL(do_int3);* Help handler running on a per-cpu (IST or entry trampoline) stack * to switch to the normal thread stack if the interrupted code was in * user mode. The actual stack switch is done in entry_64.S +* +* This function switches the registers - don't instrument it with KMSAN! */ +__no_sanitize_memory asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs) {struct pt_regs *regs = (struct pt_regs *)this_cpu_read (cpu_current_top_of_stack) -1 ;@@ -634,6 +637,11 @@ struct bad_iret_stack { }; asmlinkage __visible notrace +/* +* Dark magic happening here, let's not instrument this function. +* Also avoid copying any metadata by using raw __memmove(). +*/ +__no_sanitize_memorystruct bad_iret_stack *fixup_bad_iret (struct bad_iret_stack *s) {/*
arch/x86/kernel/traps.c struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) · line 662
Quoted 9 lines
arch/x86/kernel/traps.c +2
-2@@ -648,10 +656,10 @@ struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) (struct bad_iret_stack *)this_cpu_read (cpu_tss_rw.x86_tss.sp0) -1 ;/* Copy the IRET target to the new stack. */ -memmove (&new_stack->regs.ip, (void *)s->regs.sp,5 *8 ); +__memmove (&new_stack->regs.ip, (void *)s->regs.sp,5 *8 );/* Copy the remainder of the stack from the current stack. */ - memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip)); + __memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));
Looks like this change should go into a separate patch.
Quoted 17 lines
arch/x86/kernel/traps.c +0
-0BUG_ON (!user_mode (&new_stack->regs));return new_stack;arch/x86/kernel/uprobes.c +1
-0@@ -8,6 +8,7 @@* Jim Keniston */ #include <linux/kernel.h> +#include <linux/kmsan-checks.h> #include <linux/sched.h> #include <linux/ptrace.h> #include <linux/uprobes.h> @@ -997,9 +998,13 @@ int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)int arch_uprobe_exception_notify (struct notifier_block *self,unsigned long val,void *data)
arch/x86/kernel/uprobes.c int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) · line 1007
Quoted 10 lines
arch/x86/kernel/uprobes.c +5
-1{struct die_args *args = data; -struct pt_regs *regs = args->regs; +struct pt_regs *regs;int ret =NOTIFY_DONE ; +kmsan_unpoison_shadow (args,sizeof (*args)); + regs = args->regs; +if (regs) +kmsan_unpoison_shadow (regs,sizeof (*regs));
This one as well.
Quoted 70 lines
arch/x86/kernel/uprobes.c +0
-0/* We are only interested in userspace traps */ if (regs && !user_mode (regs))return NOTIFY_DONE ;kernel/profile.c +1
-0@@ -399,6 +399,7 @@ void profile_hits(int type, void *__pc, unsigned int nr_hits) }EXPORT_SYMBOL_GPL (profile_hits); +__no_sanitize_memory void profile_tick(int type) {struct pt_regs *regs =get_irq_regs ();kernel/sched/core.c +6
-1@@ -475,6 +475,7 @@ void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task)put_task_struct (task); } +__no_sanitize_memory /* context switching here */ void wake_up_q(struct wake_q_head *head) {struct wake_q_node *node = head->first;@@ -3180,6 +3181,7 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev,* past. prev == current is still correct but we need to recalculate this_rq * because prev may have moved to another CPU. */ +__no_sanitize_memory /* |current| changes here */ static struct rq *finish_task_switch(struct task_struct *prev) __releases(rq->lock) {@@ -3986,6 +3988,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)* * WARNING: must be called with preemption disabled! */ +__no_sanitize_memory /* |current| changes here */ static void __sched notrace __schedule(bool preempt) {struct task_struct *prev, *next;@@ -4605,6 +4608,7 @@ int task_prio(const struct task_struct *p)* * Return: 1 if the CPU is currently idle. 0 otherwise. */ +__no_sanitize_memory /* nothing to report here */ int idle_cpu(int cpu) {struct rq *rq =cpu_rq (cpu);@@ -6544,6 +6548,7 @@ static struct kmem_cache *task_group_cache __read_mostly;DECLARE_PER_CPU (cpumask_var_t, load_balance_mask);DECLARE_PER_CPU (cpumask_var_t, select_idle_mask); +__no_sanitize_memory void __init sched_init(void) {unsigned long ptr =0 ;@@ -6716,6 +6721,7 @@ static inline int preempt_count_equals(int preempt_offset)return (nested == preempt_offset); } +__no_sanitize_memory /* expect the arguments to be initialized */ void __might_sleep(const char *file, int line, int preempt_offset) {/* -- 2.24.0.432.g9d3f5f5b63-goog
Alexander Potapenko
maintainerRe: [PATCH RFC v3 24/36] kmsan: disable instrumentation of certain functions
Quoted 2 lines
The comment style around
__no_sanitize_memorylooks very different for different call sites, perhaps it makes sense to unify it somehow.
Done in v4.
Quoted 15 lines
Quoted 15 lines
arch/x86/kernel/traps.c +6
-1+/* +* Dark magic happening here, let's not instrument this function. +* Also avoid copying any metadata by using raw __memmove(). +*/ +__no_sanitize_memory struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) {/* @@ -648,10 +656,10 @@ struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) (struct bad_iret_stack *)this_cpu_read (cpu_tss_rw.x86_tss.sp0) -1 ;/* Copy the IRET target to the new stack. */ -memmove (&new_stack->regs.ip, (void *)s->regs.sp,5 *8 ); +__memmove (&new_stack->regs.ip, (void *)s->regs.sp,5 *8 );
Quoted 5 lines
arch/x86/kernel/traps.c struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) · line 662
Quoted 3 lines
arch/x86/kernel/traps.c +1
-1/* Copy the remainder of the stack from the current stack. */ - memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip)); + __memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));Looks like this change should go into a separate patch.
I disagree. Both the __no_sanitize_memory annotation and __memmove() calls serve the same purpose: avoid calling KMSAN code from fixup_bad_iret().
I don't think it makes sense to separate them.
Quoted 6 lines
arch/x86/kernel/uprobes.c int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) · line 1007
Quoted 4 lines
arch/x86/kernel/uprobes.c +4
-0+kmsan_unpoison_shadow (args,sizeof (*args)); + regs = args->regs; +if (regs) +kmsan_unpoison_shadow (regs,sizeof (*regs));This one as well.
Done in v4.
[email protected]
maintainerRFC PATCH v3 25/36 kmsan: unpoison |tlb| in arch_tlb_gather_mmu()
In reply to [email protected]This is a hack to reduce stackdepot pressure.
To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
Change-Id: I22a201e7e4f67ed74f8129072f12e5351b26103a
--- mm/mmu_gather.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
mm/mmu_gather.c +10 -0
@@ -1,6 +1,7 @@#include <linux/gfp.h> #include <linux/highmem.h> #include <linux/kernel.h> +#include <linux/kmsan-checks.h> #include <linux/mmdebug.h> #include <linux/mm_types.h> #include <linux/pagemap.h>
@@ -206,6 +207,15 @@ void tlb_flush_mmu(struct mmu_gather *tlb)void tlb_gather_mmu (struct mmu_gather *tlb,struct mm_struct *mm,unsigned long start,unsigned long end) { +/* +* TODO(glider): struct mmu_gather contains 7 1-bit fields packed into a +* 32-bit unsigned int value. The remaining 25 bits remain uninitialized +* and are never used, but KMSAN updates the origin for them in +* zap_pXX_range() in mm/memory.c, thus creating very long origin +* chains. This is technically correct, but consumes too much memory. +* Unpoisoning the whole structure will prevent creating such chains. +*/ +kmsan_unpoison_shadow (tlb,sizeof (*tlb)); tlb->mm = mm;/* Is it from 0 to ~0? */
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Re: [PATCH RFC v3 25/36] kmsan: unpoison |tlb| in arch_tlb_gather_mmu()
Quoted 2 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
This is a hack to reduce stackdepot pressure.
I think this patch needs a better description :) Like in the comment below.
Quoted 27 lines
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
Change-Id: I22a201e7e4f67ed74f8129072f12e5351b26103a
--- mm/mmu_gather.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/mmu_gather.c +1
-0@@ -1,6 +1,7 @@#include <linux/gfp.h> #include <linux/highmem.h> #include <linux/kernel.h> +#include <linux/kmsan-checks.h> #include <linux/mmdebug.h> #include <linux/mm_types.h> #include <linux/pagemap.h>
mm/mmu_gather.c void tlb_flush_mmu(struct mmu_gather *tlb) · line 211
Quoted 5 lines
mm/mmu_gather.c +2
-0@@ -206,6 +207,15 @@ void tlb_flush_mmu(struct mmu_gather *tlb)void tlb_gather_mmu (struct mmu_gather *tlb,struct mm_struct *mm,unsigned long start,unsigned long end) { +/* +* TODO(glider): struct mmu_gather contains 7 1-bit fields packed into a
Remove TODO?
Quoted 13 lines
mm/mmu_gather.c +7
-1+* 32-bit unsigned int value. The remaining 25 bits remain uninitialized +* and are never used, but KMSAN updates the origin for them in +* zap_pXX_range() in mm/memory.c, thus creating very long origin +* chains. This is technically correct, but consumes too much memory. +* Unpoisoning the whole structure will prevent creating such chains. +*/ +kmsan_unpoison_shadow (tlb,sizeof (*tlb)); tlb->mm = mm;/* Is it from 0 to ~0? */ --2.24.0.432.g9d3f5f5b63-goog
Alexander Potapenko
maintainerRe: [PATCH RFC v3 25/36] kmsan: unpoison |tlb| in arch_tlb_gather_mmu()
Quoted 33 lines · Andrey Konovalov
Quoted 2 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
This is a hack to reduce stackdepot pressure.
I think this patch needs a better description :) Like in the comment below.
Quoted 26 lines
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
Change-Id: I22a201e7e4f67ed74f8129072f12e5351b26103a
--- mm/mmu_gather.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/mmu_gather.c +1
-0@@ -1,6 +1,7 @@#include <linux/gfp.h> #include <linux/highmem.h> #include <linux/kernel.h> +#include <linux/kmsan-checks.h> #include <linux/mmdebug.h> #include <linux/mm_types.h> #include <linux/pagemap.h>
Quoted 8 lines
mm/mmu_gather.c void tlb_flush_mmu(struct mmu_gather *tlb) · line 211
Quoted 6 lines
mm/mmu_gather.c +2
-0@@ -206,6 +207,15 @@ void tlb_flush_mmu(struct mmu_gather *tlb)void tlb_gather_mmu (struct mmu_gather *tlb,struct mm_struct *mm,unsigned long start,unsigned long end) { +/* +* TODO(glider): struct mmu_gather contains 7 1-bit fields packed into a Remove TODO?
Will do, thanks!
Quoted 13 lines
Quoted 13 lines
mm/mmu_gather.c +7
-1+* 32-bit unsigned int value. The remaining 25 bits remain uninitialized +* and are never used, but KMSAN updates the origin for them in +* zap_pXX_range() in mm/memory.c, thus creating very long origin +* chains. This is technically correct, but consumes too much memory. +* Unpoisoning the whole structure will prevent creating such chains. +*/ +kmsan_unpoison_shadow (tlb,sizeof (*tlb)); tlb->mm = mm;/* Is it from 0 to ~0? */ --2.24.0.432.g9d3f5f5b63-goog
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
[email protected]
maintainerRFC PATCH v3 26/36 kmsan: use __msan_memcpy() where possible.
In reply to [email protected]Unless stated otherwise (by explicitly calling __memcpy()) we want all memcpy() calls to call __msan_memcpy() so that shadow and origin values are updated accordingly.
Bootloader must still the default string functions to avoid crashes.
To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- use default string functions in the bootloader
Change-Id: Ib2512ce5aa8d457453dd38caa12f58f002166813
--- arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/string_64.h | 9 ++++++++- include/linux/compiler.h | 9 ++++++++- include/linux/string.h | 2 ++ 4 files changed, 19 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/misc.h +1 -0
@@ -12,6 +12,7 @@#undef CONFIG_PARAVIRT_XXL#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN +#undef CONFIG_KMSAN/* cpu_feature_enabled() cannot be used this early */ #define USE_EARLY_PGTABLE_L5
arch/x86/include/asm/string_64.h +8 -1
@@ -11,7 +11,13 @@function. */ #define __HAVE_ARCH_MEMCPY 1 +#if defined(CONFIG_KMSAN ) +#undef memcpy +/* __msan_memcpy() is defined in compiler.h */ +#define memcpy (dst, src, len) __msan_memcpy(dst, src, len) +#else extern void *memcpy (void *to,const void *from,size_t len); +#endif extern void *__memcpy (void *to,const void *from,size_t len);#define __HAVE_ARCH_MEMSET
@@ -64,7 +70,8 @@ char *strcpy(char *dest, const char *src);char *strcat (char *dest,const char *src);int strcmp (const char *cs,const char *ct); -#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__) +#if (defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__)) || \ + (defined(CONFIG_KMSAN ) && !defined(__SANITIZE_MEMORY__))/* * For files that not instrumented (e.g. mm/slub.c) we
include/linux/compiler.h +8 -1
@@ -179,6 +179,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,#include <uapi/linux/types.h> +#ifdef CONFIG_KMSAN +void *__msan_memcpy (void *dst,const void *src,u64 size); +#define __DO_MEMCPY (res, p, size) __msan_memcpy(res, p, size) +#else +#define __DO_MEMCPY (res, p, size) __builtin_memcpy(res, p, size) +#endif +#define __READ_ONCE_SIZE \ ({ \ switch (size) { \
@@ -188,7 +195,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,case 8 : *(__u64 *)res = *(volatile __u64 *)p;break ; \default : \barrier (); \ -__builtin_memcpy ((void *)res, (const void *)p, size); \ +__DO_MEMCPY ((void *)res, (const void *)p, size); \barrier (); \ } \ })
include/linux/string.h +2 -0
@@ -363,6 +363,7 @@ __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)return __builtin_memset (p, c, size); } +#ifndef CONFIG_KMSAN __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) {size_t p_size =__builtin_object_size (p,0 );
@@ -377,6 +378,7 @@ __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)fortify_panic (__func__);return __builtin_memcpy (p, q, size); } +#endif __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) {
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Re: [PATCH RFC v3 26/36] kmsan: use __msan_memcpy() where possible.
Quoted 4 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Unless stated otherwise (by explicitly calling
__memcpy()) we want allmemcpy()calls to call__msan_memcpy()so that shadow and origin values are updated accordingly.
Why do we only do this for memcpy() but not for memove() and others?
Quoted 2 lines
Bootloader must still the default string functions to avoid crashes.
must still use
Quoted 108 lines
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- use default string functions in the bootloaderChange-Id: Ib2512ce5aa8d457453dd38caa12f58f002166813
--- arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/string_64.h | 9 ++++++++- include/linux/compiler.h | 9 ++++++++- include/linux/string.h | 2 ++ 4 files changed, 19 insertions(+), 2 deletions(-)arch/x86/boot/compressed/misc.h +1
-0@@ -12,6 +12,7 @@#undef CONFIG_PARAVIRT_XXL#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN +#undef CONFIG_KMSAN/* cpu_feature_enabled() cannot be used this early */ #define USE_EARLY_PGTABLE_L5 arch/x86/include/asm/string_64.h +8
-1@@ -11,7 +11,13 @@function. */ #define __HAVE_ARCH_MEMCPY 1 +#if defined(CONFIG_KMSAN ) +#undef memcpy +/* __msan_memcpy() is defined in compiler.h */ +#define memcpy (dst, src, len) __msan_memcpy(dst, src, len) +#else extern void *memcpy (void *to,const void *from,size_t len); +#endif extern void *__memcpy (void *to,const void *from,size_t len);#define __HAVE_ARCH_MEMSET@@ -64,7 +70,8 @@ char *strcpy(char *dest, const char *src);char *strcat (char *dest,const char *src);int strcmp (const char *cs,const char *ct); -#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__) +#if (defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__)) || \ + (defined(CONFIG_KMSAN ) && !defined(__SANITIZE_MEMORY__))/* * For files that not instrumented (e.g. mm/slub.c) we include/linux/compiler.h +8
-1@@ -179,6 +179,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,#include <uapi/linux/types.h> +#ifdef CONFIG_KMSAN +void *__msan_memcpy (void *dst,const void *src,u64 size); +#define __DO_MEMCPY (res, p, size) __msan_memcpy(res, p, size) +#else +#define __DO_MEMCPY (res, p, size) __builtin_memcpy(res, p, size) +#endif +#define __READ_ONCE_SIZE \ ({ \ switch (size) { \@@ -188,7 +195,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,case 8 : *(__u64 *)res = *(volatile __u64 *)p;break ; \default : \barrier (); \ -__builtin_memcpy ((void *)res, (const void *)p, size); \ +__DO_MEMCPY ((void *)res, (const void *)p, size); \barrier (); \ } \ })include/linux/string.h +2
-1@@ -363,6 +363,7 @@ __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)return __builtin_memset (p, c, size); } +#ifndef CONFIG_KMSAN __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) {size_t p_size =__builtin_object_size (p,0 );@@ -377,6 +378,7 @@ __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)fortify_panic (__func__);return __builtin_memcpy (p, q, size); } +#endif __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) { --2.24.0.432.g9d3f5f5b63-goog
Alexander Potapenko
maintainerRe: [PATCH RFC v3 26/36] kmsan: use __msan_memcpy() where possible.
Quoted 8 lines · Andrey Konovalov
Quoted 4 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Unless stated otherwise (by explicitly calling
__memcpy()) we want allmemcpy()calls to call__msan_memcpy()so that shadow and origin values are updated accordingly.Why do we only do this for
memcpy()but not formemove()and others?
Hm, interesting.
Looks like I simply forgot to add memset() and memmove(). Could have costed us some false negatives.
Quoted 4 lines
Quoted 2 lines
Bootloader must still the default string functions to avoid crashes.
must still use
Ack
Quoted 109 lines
Quoted 109 lines
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- use default string functions in the bootloaderChange-Id: Ib2512ce5aa8d457453dd38caa12f58f002166813
--- arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/string_64.h | 9 ++++++++- include/linux/compiler.h | 9 ++++++++- include/linux/string.h | 2 ++ 4 files changed, 19 insertions(+), 2 deletions(-)arch/x86/boot/compressed/misc.h +1
-0@@ -12,6 +12,7 @@#undef CONFIG_PARAVIRT_XXL#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN +#undef CONFIG_KMSAN/* cpu_feature_enabled() cannot be used this early */ #define USE_EARLY_PGTABLE_L5 arch/x86/include/asm/string_64.h +8
-1@@ -11,7 +11,13 @@function. */ #define __HAVE_ARCH_MEMCPY 1 +#if defined(CONFIG_KMSAN ) +#undef memcpy +/* __msan_memcpy() is defined in compiler.h */ +#define memcpy (dst, src, len) __msan_memcpy(dst, src, len) +#else extern void *memcpy (void *to,const void *from,size_t len); +#endif extern void *__memcpy (void *to,const void *from,size_t len);#define __HAVE_ARCH_MEMSET@@ -64,7 +70,8 @@ char *strcpy(char *dest, const char *src);char *strcat (char *dest,const char *src);int strcmp (const char *cs,const char *ct); -#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__) +#if (defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__)) || \ + (defined(CONFIG_KMSAN ) && !defined(__SANITIZE_MEMORY__))/* * For files that not instrumented (e.g. mm/slub.c) we include/linux/compiler.h +8
-1@@ -179,6 +179,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,#include <uapi/linux/types.h> +#ifdef CONFIG_KMSAN +void *__msan_memcpy (void *dst,const void *src,u64 size); +#define __DO_MEMCPY (res, p, size) __msan_memcpy(res, p, size) +#else +#define __DO_MEMCPY (res, p, size) __builtin_memcpy(res, p, size) +#endif +#define __READ_ONCE_SIZE \ ({ \ switch (size) { \@@ -188,7 +195,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,case 8 : *(__u64 *)res = *(volatile __u64 *)p;break ; \default : \barrier (); \ -__builtin_memcpy ((void *)res, (const void *)p, size); \ +__DO_MEMCPY ((void *)res, (const void *)p, size); \barrier (); \ } \ })include/linux/string.h +2
-1@@ -363,6 +363,7 @@ __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)return __builtin_memset (p, c, size); } +#ifndef CONFIG_KMSAN __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) {size_t p_size =__builtin_object_size (p,0 );@@ -377,6 +378,7 @@ __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)fortify_panic (__func__);return __builtin_memcpy (p, q, size); } +#endif __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) { --2.24.0.432.g9d3f5f5b63-goog
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
[email protected]
maintainerRFC PATCH v3 27/36 kmsan: hooks for copy_to_user() and friends
In reply to [email protected]Memory that is copied from userspace must be unpoisoned. Before copying memory to userspace, check it and report an error if it contains uninitialized bits.
To: Alexander Potapenko <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- fixed compilation errors reported by kbuild test bot
Change-Id: I38428b9c7d1909b8441dcec1749b080494a7af99
--- arch/x86/include/asm/uaccess.h | 12 ++++++++++++ include/asm-generic/cacheflush.h | 7 ++++++- include/asm-generic/uaccess.h | 12 ++++++++++-- include/linux/uaccess.h | 32 +++++++++++++++++++++++++++----- lib/iov_iter.c | 6 ++++++ lib/usercopy.c | 6 +++++- 6 files changed, 66 insertions(+), 9 deletions(-)
arch/x86/include/asm/uaccess.h +12 -0
@@ -6,6 +6,7 @@*/ #include <linux/compiler.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #include <linux/string.h> #include <asm/asm.h> #include <asm/page.h>
@@ -174,6 +175,7 @@ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL)) ASM_CALL_CONSTRAINT \ : "0" (ptr), "i" (sizeof(*(ptr)))); \ (x) = (__force __typeof__(*(ptr))) __val_gu; \ +kmsan_unpoison_shadow (&(x),sizeof (*(ptr))); \__builtin_expect (__ret_gu,0 ); \ })
@@ -248,6 +250,7 @@ extern void __put_user_8(void);__chk_user_ptr (ptr); \might_fault (); \ __pu_val = x; \ +kmsan_check_memory (&(__pu_val),sizeof (*(ptr))); \switch (sizeof (*(ptr))) { \case 1 : \__put_user_x (1 , __pu_val, ptr, __ret_pu); \
@@ -270,7 +273,9 @@ extern void __put_user_8(void);#define __put_user_size (x, ptr, size, label) \ do { \ + __typeof__(*(ptr)) __pus_val = x; \ __chk_user_ptr(ptr); \ + kmsan_check_memory(&(__pus_val), size); \ switch (size) { \ case 1: \ __put_user_goto(x, ptr, "b", "b", "iq", label); \
@@ -295,7 +300,10 @@ do { \*/ #define __put_user_size_ex (x, ptr, size) \ do { \ + __typeof__(*(ptr)) __puse_val; \ __chk_user_ptr(ptr); \ + __puse_val = x; \ + kmsan_check_memory(&(__puse_val), size); \ switch (size) { \ case 1: \ __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
@@ -363,6 +371,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 ) #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
@@ -413,6 +422,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 ) #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
@@ -428,11 +438,13 @@ do { \#define __put_user_nocheck (x, ptr, size) \ ({ \ __label__ __pu_label; \ + __typeof__(*(ptr)) __pun_val = x; \ int __pu_err = -EFAULT; \ __typeof__(*(ptr)) __pu_val = (x); \ __typeof__(ptr) __pu_ptr = (ptr); \ __typeof__(size) __pu_size = (size); \ __uaccess_begin(); \ + kmsan_check_memory(&(__pun_val), size); \ __put_user_size(__pu_val, __pu_ptr, __pu_size, __pu_label); \ __pu_err = 0; \ __pu_label: \
include/asm-generic/cacheflush.h +6 -1
@@ -4,6 +4,7 @@/* Keep includes the same across arches. */ #include <linux/mm.h> +#include <linux/kmsan-checks.h> #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 0
@@ -72,10 +73,14 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end)#define copy_to_user_page (vma, page, vaddr, dst, src, len) \ do { \ + kmsan_check_memory(src, len); \ memcpy(dst, src, len); \ flush_icache_user_range(vma, page, vaddr, len); \ } while (0)#define copy_from_user_page (vma, page, vaddr, dst, src, len) \ - memcpy(dst, src, len) + do { \ + memcpy(dst, src, len); \ + kmsan_unpoison_shadow(dst, len); \ + } while (0)#endif /* __ASM_CACHEFLUSH_H */
include/asm-generic/uaccess.h +10 -2
@@ -142,7 +142,11 @@ static inline int __access_ok(unsigned long addr, unsigned long size)static inline int __put_user_fn (size_t size,void __user *ptr,void *x) { -return unlikely (raw_copy_to_user (ptr, x, size)) ? -EFAULT :0 ; +int n; + + n =raw_copy_to_user (ptr, x, size); +kmsan_copy_to_user (ptr, x, size, n); +return unlikely (n) ? -EFAULT :0 ; }#define __put_user_fn (sz, u, k) __put_user_fn(sz, u, k)
@@ -203,7 +207,11 @@ extern int __put_user_bad(void) __attribute__((noreturn));#ifndef __get_user_fnstatic inline int __get_user_fn (size_t size,const void __user *ptr,void *x) { -return unlikely (raw_copy_from_user (x, ptr, size)) ? -EFAULT :0 ; +int copied, to_copy = size; + + copied =raw_copy_from_user (x, ptr, size); +kmsan_unpoison_shadow (x, to_copy - copied); +return unlikely (copied) ? -EFAULT :0 ; }#define __get_user_fn (sz, u, k) __get_user_fn(sz, u, k)
include/linux/uaccess.h +27 -5
@@ -5,6 +5,7 @@#include <linux/sched.h> #include <linux/thread_info.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #define uaccess_kernel () segment_eq(get_fs(), KERNEL_DS)
@@ -58,18 +59,26 @@static __always_inline __must_check unsigned long __copy_from_user_inatomic (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_write (to, n);check_object_size (to, n, false); -return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }static __always_inline __must_check unsigned long __copy_from_user (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();kasan_check_write (to, n);check_object_size (to, n, false); -return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }/**
@@ -88,29 +97,39 @@ __copy_from_user(void *to, const void __user *from, unsigned long n)static __always_inline __must_check unsigned long __copy_to_user_inatomic (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }static __always_inline __must_check unsigned long __copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }#ifdef INLINE_COPY_FROM_USER static inline __must_check unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n; +might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);
@@ -125,10 +144,13 @@ _copy_from_user(void *, const void __user *, unsigned long);static inline __must_check unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }
lib/iov_iter.c +6 -0
@@ -137,18 +137,24 @@static int copyout (void __user *to,const void *from,size_t n) { +size_t to_copy = n; +if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }static int copyin (void *to,const void __user *from,size_t n) { +size_t to_copy = n; +if (access_ok (from, n)) {kasan_check_write (to, n); n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); }return n; }
lib/usercopy.c +5 -1
@@ -1,4 +1,5 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/kmsan-checks.h> #include <linux/uaccess.h> #include <linux/bitops.h>
@@ -7,11 +8,12 @@#ifndef INLINE_COPY_FROM_USER unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n;might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);
@@ -23,10 +25,12 @@ EXPORT_SYMBOL(_copy_from_user);#ifndef INLINE_COPY_TO_USER unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n;might_fault ();if (likely (access_ok (to, n))) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Re: [PATCH RFC v3 27/36] kmsan: hooks for copy_to_user() and friends
Quoted 64 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Memory that is copied from userspace must be unpoisoned. Before copying memory to userspace, check it and report an error if it contains uninitialized bits.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- fixed compilation errors reported by kbuild test botChange-Id: I38428b9c7d1909b8441dcec1749b080494a7af99
--- arch/x86/include/asm/uaccess.h | 12 ++++++++++++ include/asm-generic/cacheflush.h | 7 ++++++- include/asm-generic/uaccess.h | 12 ++++++++++-- include/linux/uaccess.h | 32 +++++++++++++++++++++++++++----- lib/iov_iter.c | 6 ++++++ lib/usercopy.c | 6 +++++- 6 files changed, 66 insertions(+), 9 deletions(-)arch/x86/include/asm/uaccess.h +5
-0@@ -6,6 +6,7 @@*/ #include <linux/compiler.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #include <linux/string.h> #include <asm/asm.h> #include <asm/page.h> @@ -174,6 +175,7 @@ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL)) ASM_CALL_CONSTRAINT \ : "0" (ptr), "i" (sizeof(*(ptr)))); \ (x) = (__force __typeof__(*(ptr))) __val_gu; \ +kmsan_unpoison_shadow (&(x),sizeof (*(ptr))); \__builtin_expect (__ret_gu,0 ); \ })@@ -248,6 +250,7 @@ extern void __put_user_8(void);__chk_user_ptr (ptr); \might_fault (); \ __pu_val = x; \ +kmsan_check_memory (&(__pu_val),sizeof (*(ptr))); \switch (sizeof (*(ptr))) { \case 1 : \__put_user_x (1 , __pu_val, ptr, __ret_pu); \@@ -270,7 +273,9 @@ extern void __put_user_8(void);#define __put_user_size (x, ptr, size, label) \ do { \ + __typeof__(*(ptr)) __pus_val = x; \ __chk_user_ptr(ptr); \ + kmsan_check_memory(&(__pus_val), size); \ switch (size) { \ case 1: \ __put_user_goto(x, ptr, "b", "b", "iq", label); \
arch/x86/include/asm/uaccess.h do { \ · line 303
Quoted 4 lines
arch/x86/include/asm/uaccess.h +1
-0@@ -295,7 +300,10 @@ do { \*/ #define __put_user_size_ex (x, ptr, size) \ do { \ + __typeof__(*(ptr)) __puse_val; \
Can we do = x here?
Quoted 23 lines
arch/x86/include/asm/uaccess.h +4
-0__chk_user_ptr (ptr); \ + __puse_val = x; \ +kmsan_check_memory (&(__puse_val), size); \switch (size) { \case 1 : \__put_user_asm_ex (x, ptr,"b" ,"b" ,"iq" ); \@@ -363,6 +371,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 ) #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \@@ -413,6 +422,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 ) #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
arch/x86/include/asm/uaccess.h do { \ · line 441
Quoted 4 lines
arch/x86/include/asm/uaccess.h +1
-0@@ -428,11 +438,13 @@ do { \#define __put_user_nocheck (x, ptr, size) \ ({ \ __label__ __pu_label; \ + __typeof__(*(ptr)) __pun_val = x; \
Not sure if this matters, but two lines below do (x).
Also, why can't we use __pu_val instead of defining __pun_val?
Quoted 87 lines
arch/x86/include/asm/uaccess.h +1
-0int __pu_err = -EFAULT ; \ __typeof__(*(ptr)) __pu_val = (x); \__typeof__ (ptr) __pu_ptr = (ptr); \__typeof__ (size) __pu_size = (size); \__uaccess_begin (); \ +kmsan_check_memory (&(__pun_val), size); \__put_user_size (__pu_val, __pu_ptr, __pu_size, __pu_label); \ __pu_err =0 ; \ __pu_label: \include/asm-generic/cacheflush.h +6
-1@@ -4,6 +4,7 @@/* Keep includes the same across arches. */ #include <linux/mm.h> +#include <linux/kmsan-checks.h> #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 0@@ -72,10 +73,14 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end)#define copy_to_user_page (vma, page, vaddr, dst, src, len) \ do { \ + kmsan_check_memory(src, len); \ memcpy(dst, src, len); \ flush_icache_user_range(vma, page, vaddr, len); \ } while (0)#define copy_from_user_page (vma, page, vaddr, dst, src, len) \ - memcpy(dst, src, len) + do { \ + memcpy(dst, src, len); \ + kmsan_unpoison_shadow(dst, len); \ + } while (0)#endif /* __ASM_CACHEFLUSH_H */ include/asm-generic/uaccess.h +10
-2@@ -142,7 +142,11 @@ static inline int __access_ok(unsigned long addr, unsigned long size)static inline int __put_user_fn (size_t size,void __user *ptr,void *x) { -return unlikely (raw_copy_to_user (ptr, x, size)) ? -EFAULT :0 ; +int n; + + n =raw_copy_to_user (ptr, x, size); +kmsan_copy_to_user (ptr, x, size, n); +return unlikely (n) ? -EFAULT :0 ; }#define __put_user_fn (sz, u, k) __put_user_fn(sz, u, k)@@ -203,7 +207,11 @@ extern int __put_user_bad(void) __attribute__((noreturn));#ifndef __get_user_fnstatic inline int __get_user_fn (size_t size,const void __user *ptr,void *x) { -return unlikely (raw_copy_from_user (x, ptr, size)) ? -EFAULT :0 ; +int copied, to_copy = size; + + copied =raw_copy_from_user (x, ptr, size); +kmsan_unpoison_shadow (x, to_copy - copied); +return unlikely (copied) ? -EFAULT :0 ; }#define __get_user_fn (sz, u, k) __get_user_fn(sz, u, k)include/linux/uaccess.h +3
-0@@ -5,6 +5,7 @@#include <linux/sched.h> #include <linux/thread_info.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #define uaccess_kernel () segment_eq(get_fs(), KERNEL_DS)@@ -58,18 +59,26 @@static __always_inline __must_check unsigned long __copy_from_user_inatomic (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_write (to, n);check_object_size (to, n, false);
include/linux/uaccess.h line 74
Quoted 10 lines
include/linux/uaccess.h +4
-1-return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }static __always_inline __must_check unsigned long __copy_from_user (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n;
This is confusing. I think we need a var for raw_copy_from_user() return value instead. Same in functions above and below.
Quoted 137 lines
include/linux/uaccess.h +20
-4+might_fault ();kasan_check_write (to, n);check_object_size (to, n, false); -return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }/** @@ -88,29 +97,39 @@ __copy_from_user(void *to, const void __user *from, unsigned long n)static __always_inline __must_check unsigned long __copy_to_user_inatomic (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }static __always_inline __must_check unsigned long __copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }#ifdef INLINE_COPY_FROM_USER static inline __must_check unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n; +might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);@@ -125,10 +144,13 @@ _copy_from_user(void *, const void __user *, unsigned long);static inline __must_check unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }lib/iov_iter.c +6
-0@@ -137,18 +137,24 @@static int copyout (void __user *to,const void *from,size_t n) { +size_t to_copy = n; +if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }static int copyin (void *to,const void __user *from,size_t n) { +size_t to_copy = n; +if (access_ok (from, n)) {kasan_check_write (to, n); n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); }return n; }lib/usercopy.c +5
-2@@ -1,4 +1,5 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/kmsan-checks.h> #include <linux/uaccess.h> #include <linux/bitops.h> @@ -7,11 +8,12 @@#ifndef INLINE_COPY_FROM_USER unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n;might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);@@ -23,10 +25,12 @@ EXPORT_SYMBOL(_copy_from_user);#ifndef INLINE_COPY_TO_USER unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n;might_fault ();if (likely (access_ok (to, n))) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; } --2.24.0.432.g9d3f5f5b63-goog
Alexander Potapenko
maintainerRe: [PATCH RFC v3 27/36] kmsan: hooks for copy_to_user() and friends
Quoted 57 lines · Andrey Konovalov
Quoted 55 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Memory that is copied from userspace must be unpoisoned. Before copying memory to userspace, check it and report an error if it contains uninitialized bits.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- fixed compilation errors reported by kbuild test botChange-Id: I38428b9c7d1909b8441dcec1749b080494a7af99
--- arch/x86/include/asm/uaccess.h | 12 ++++++++++++ include/asm-generic/cacheflush.h | 7 ++++++- include/asm-generic/uaccess.h | 12 ++++++++++-- include/linux/uaccess.h | 32 +++++++++++++++++++++++++++----- lib/iov_iter.c | 6 ++++++ lib/usercopy.c | 6 +++++- 6 files changed, 66 insertions(+), 9 deletions(-)arch/x86/include/asm/uaccess.h +3
-0@@ -6,6 +6,7 @@*/ #include <linux/compiler.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #include <linux/string.h> #include <asm/asm.h> #include <asm/page.h> @@ -174,6 +175,7 @@ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL)) ASM_CALL_CONSTRAINT \ : "0" (ptr), "i" (sizeof(*(ptr)))); \ (x) = (__force __typeof__(*(ptr))) __val_gu; \ +kmsan_unpoison_shadow (&(x),sizeof (*(ptr))); \__builtin_expect (__ret_gu,0 ); \ })@@ -248,6 +250,7 @@ extern void __put_user_8(void);__chk_user_ptr (ptr); \might_fault (); \ __pu_val = x; \ +kmsan_check_memory (&(__pu_val),sizeof (*(ptr))); \switch (sizeof (*(ptr))) { \case 1 : \__put_user_x (1 , __pu_val, ptr, __ret_pu); \@@ -270,7 +273,9 @@ extern void __put_user_8(void);
Quoted 15 lines
arch/x86/include/asm/uaccess.h do { \ · line 303
Quoted 13 lines
arch/x86/include/asm/uaccess.h +3
-0#define __put_user_size (x, ptr, size, label) \ do { \ + __typeof__(*(ptr)) __pus_val = x; \ __chk_user_ptr(ptr); \ + kmsan_check_memory(&(__pus_val), size); \ switch (size) { \ case 1: \ __put_user_goto(x, ptr, "b", "b", "iq", label); \@@ -295,7 +300,10 @@ do { \*/ #define __put_user_size_ex (x, ptr, size) \ do { \ + __typeof__(*(ptr)) __puse_val; \Can we do = x here?
Yes. Fixed, thanks!
Quoted 21 lines
Quoted 21 lines
arch/x86/include/asm/uaccess.h +4
-0__chk_user_ptr (ptr); \ + __puse_val = x; \ +kmsan_check_memory (&(__puse_val), size); \switch (size) { \case 1 : \__put_user_asm_ex (x, ptr,"b" ,"b" ,"iq" ); \@@ -363,6 +371,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 ) #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \@@ -413,6 +422,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 )
Quoted 8 lines
arch/x86/include/asm/uaccess.h do { \ · line 441
Quoted 6 lines
arch/x86/include/asm/uaccess.h +1
-0#define __get_user_asm_ex (x, addr, itype, rtype, ltype) \@@ -428,11 +438,13 @@ do { \#define __put_user_nocheck (x, ptr, size) \ ({ \ __label__ __pu_label; \ + __typeof__(*(ptr)) __pun_val = x; \Not sure if this matters, but two lines below do (x).
Right.
Quoted 1 line
Also, why can't we use
__pu_valinstead of defining__pun_val?
Will do.
Quoted 94 lines
Quoted 94 lines
arch/x86/include/asm/uaccess.h +1
-0int __pu_err = -EFAULT ; \ __typeof__(*(ptr)) __pu_val = (x); \__typeof__ (ptr) __pu_ptr = (ptr); \__typeof__ (size) __pu_size = (size); \__uaccess_begin (); \ +kmsan_check_memory (&(__pun_val), size); \__put_user_size (__pu_val, __pu_ptr, __pu_size, __pu_label); \ __pu_err =0 ; \ __pu_label: \include/asm-generic/cacheflush.h +6
-1@@ -4,6 +4,7 @@/* Keep includes the same across arches. */ #include <linux/mm.h> +#include <linux/kmsan-checks.h> #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 0@@ -72,10 +73,14 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end)#define copy_to_user_page (vma, page, vaddr, dst, src, len) \ do { \ + kmsan_check_memory(src, len); \ memcpy(dst, src, len); \ flush_icache_user_range(vma, page, vaddr, len); \ } while (0)#define copy_from_user_page (vma, page, vaddr, dst, src, len) \ - memcpy(dst, src, len) + do { \ + memcpy(dst, src, len); \ + kmsan_unpoison_shadow(dst, len); \ + } while (0)#endif /* __ASM_CACHEFLUSH_H */ include/asm-generic/uaccess.h +10
-2@@ -142,7 +142,11 @@ static inline int __access_ok(unsigned long addr, unsigned long size)static inline int __put_user_fn (size_t size,void __user *ptr,void *x) { -return unlikely (raw_copy_to_user (ptr, x, size)) ? -EFAULT :0 ; +int n; + + n =raw_copy_to_user (ptr, x, size); +kmsan_copy_to_user (ptr, x, size, n); +return unlikely (n) ? -EFAULT :0 ; }#define __put_user_fn (sz, u, k) __put_user_fn(sz, u, k)@@ -203,7 +207,11 @@ extern int __put_user_bad(void) __attribute__((noreturn));#ifndef __get_user_fnstatic inline int __get_user_fn (size_t size,const void __user *ptr,void *x) { -return unlikely (raw_copy_from_user (x, ptr, size)) ? -EFAULT :0 ; +int copied, to_copy = size; + + copied =raw_copy_from_user (x, ptr, size); +kmsan_unpoison_shadow (x, to_copy - copied); +return unlikely (copied) ? -EFAULT :0 ; }#define __get_user_fn (sz, u, k) __get_user_fn(sz, u, k)include/linux/uaccess.h +6
-1@@ -5,6 +5,7 @@#include <linux/sched.h> #include <linux/thread_info.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #define uaccess_kernel () segment_eq(get_fs(), KERNEL_DS)@@ -58,18 +59,26 @@static __always_inline __must_check unsigned long __copy_from_user_inatomic (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_write (to, n);check_object_size (to, n, false); -return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }
Quoted 7 lines
include/linux/uaccess.h line 74
Quoted 4 lines
include/linux/uaccess.h +1
-0static __always_inline __must_check unsigned long __copy_from_user (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n;This is confusing. I think we need a var for
raw_copy_from_user()return value instead. Same in functions above and below.
raw_copy_from_user() returns the number of bytes _not_ copied from userspace. So in the case it returns 0 we need to unpoison to_copy bytes.
Quoted 137 lines
Quoted 137 lines
include/linux/uaccess.h +20
-4+might_fault ();kasan_check_write (to, n);check_object_size (to, n, false); -return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }/** @@ -88,29 +97,39 @@ __copy_from_user(void *to, const void __user *from, unsigned long n)static __always_inline __must_check unsigned long __copy_to_user_inatomic (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }static __always_inline __must_check unsigned long __copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }#ifdef INLINE_COPY_FROM_USER static inline __must_check unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n; +might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);@@ -125,10 +144,13 @@ _copy_from_user(void *, const void __user *, unsigned long);static inline __must_check unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }lib/iov_iter.c +6
-0@@ -137,18 +137,24 @@static int copyout (void __user *to,const void *from,size_t n) { +size_t to_copy = n; +if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }static int copyin (void *to,const void __user *from,size_t n) { +size_t to_copy = n; +if (access_ok (from, n)) {kasan_check_write (to, n); n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); }return n; }lib/usercopy.c +5
-2@@ -1,4 +1,5 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/kmsan-checks.h> #include <linux/uaccess.h> #include <linux/bitops.h> @@ -7,11 +8,12 @@#ifndef INLINE_COPY_FROM_USER unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n;might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);@@ -23,10 +25,12 @@ EXPORT_SYMBOL(_copy_from_user);#ifndef INLINE_COPY_TO_USER unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n;might_fault ();if (likely (access_ok (to, n))) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; } --2.24.0.432.g9d3f5f5b63-goog
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH RFC v3 27/36] kmsan: hooks for copy_to_user() and friends
Quoted 208 lines · Alexander Potapenko
Quoted 57 lines · Andrey Konovalov
Quoted 55 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Memory that is copied from userspace must be unpoisoned. Before copying memory to userspace, check it and report an error if it contains uninitialized bits.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
v3:
- fixed compilation errors reported by kbuild test botChange-Id: I38428b9c7d1909b8441dcec1749b080494a7af99
--- arch/x86/include/asm/uaccess.h | 12 ++++++++++++ include/asm-generic/cacheflush.h | 7 ++++++- include/asm-generic/uaccess.h | 12 ++++++++++-- include/linux/uaccess.h | 32 +++++++++++++++++++++++++++----- lib/iov_iter.c | 6 ++++++ lib/usercopy.c | 6 +++++- 6 files changed, 66 insertions(+), 9 deletions(-)arch/x86/include/asm/uaccess.h +3
-0@@ -6,6 +6,7 @@*/ #include <linux/compiler.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #include <linux/string.h> #include <asm/asm.h> #include <asm/page.h> @@ -174,6 +175,7 @@ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL)) ASM_CALL_CONSTRAINT \ : "0" (ptr), "i" (sizeof(*(ptr)))); \ (x) = (__force __typeof__(*(ptr))) __val_gu; \ +kmsan_unpoison_shadow (&(x),sizeof (*(ptr))); \__builtin_expect (__ret_gu,0 ); \ })@@ -248,6 +250,7 @@ extern void __put_user_8(void);__chk_user_ptr (ptr); \might_fault (); \ __pu_val = x; \ +kmsan_check_memory (&(__pu_val),sizeof (*(ptr))); \switch (sizeof (*(ptr))) { \case 1 : \__put_user_x (1 , __pu_val, ptr, __ret_pu); \@@ -270,7 +273,9 @@ extern void __put_user_8(void);Quoted 15 lines
arch/x86/include/asm/uaccess.h do { \ · line 303
Quoted 13 lines
arch/x86/include/asm/uaccess.h +3
-0#define __put_user_size (x, ptr, size, label) \ do { \ + __typeof__(*(ptr)) __pus_val = x; \ __chk_user_ptr(ptr); \ + kmsan_check_memory(&(__pus_val), size); \ switch (size) { \ case 1: \ __put_user_goto(x, ptr, "b", "b", "iq", label); \@@ -295,7 +300,10 @@ do { \*/ #define __put_user_size_ex (x, ptr, size) \ do { \ + __typeof__(*(ptr)) __puse_val; \Can we do = x here?
Yes. Fixed, thanks!
Quoted 21 lines
Quoted 21 lines
arch/x86/include/asm/uaccess.h +4
-0__chk_user_ptr (ptr); \ + __puse_val = x; \ +kmsan_check_memory (&(__puse_val), size); \switch (size) { \case 1 : \__put_user_asm_ex (x, ptr,"b" ,"b" ,"iq" ); \@@ -363,6 +371,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 ) #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \@@ -413,6 +422,7 @@ do { \default : \ (x) =__get_user_bad (); \ } \ +kmsan_unpoison_shadow (&(x), size); \ }while (0 )Quoted 8 lines
arch/x86/include/asm/uaccess.h do { \ · line 441
Quoted 6 lines
arch/x86/include/asm/uaccess.h +1
-0#define __get_user_asm_ex (x, addr, itype, rtype, ltype) \@@ -428,11 +438,13 @@ do { \#define __put_user_nocheck (x, ptr, size) \ ({ \ __label__ __pu_label; \ + __typeof__(*(ptr)) __pun_val = x; \Not sure if this matters, but two lines below do (x).
Right.
Quoted 1 line
Also, why can't we use
__pu_valinstead of defining__pun_val?Will do.
Quoted 94 lines
Quoted 94 lines
arch/x86/include/asm/uaccess.h +1
-0int __pu_err = -EFAULT ; \ __typeof__(*(ptr)) __pu_val = (x); \__typeof__ (ptr) __pu_ptr = (ptr); \__typeof__ (size) __pu_size = (size); \__uaccess_begin (); \ +kmsan_check_memory (&(__pun_val), size); \__put_user_size (__pu_val, __pu_ptr, __pu_size, __pu_label); \ __pu_err =0 ; \ __pu_label: \include/asm-generic/cacheflush.h +6
-1@@ -4,6 +4,7 @@/* Keep includes the same across arches. */ #include <linux/mm.h> +#include <linux/kmsan-checks.h> #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 0@@ -72,10 +73,14 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end)#define copy_to_user_page (vma, page, vaddr, dst, src, len) \ do { \ + kmsan_check_memory(src, len); \ memcpy(dst, src, len); \ flush_icache_user_range(vma, page, vaddr, len); \ } while (0)#define copy_from_user_page (vma, page, vaddr, dst, src, len) \ - memcpy(dst, src, len) + do { \ + memcpy(dst, src, len); \ + kmsan_unpoison_shadow(dst, len); \ + } while (0)#endif /* __ASM_CACHEFLUSH_H */ include/asm-generic/uaccess.h +10
-2@@ -142,7 +142,11 @@ static inline int __access_ok(unsigned long addr, unsigned long size)static inline int __put_user_fn (size_t size,void __user *ptr,void *x) { -return unlikely (raw_copy_to_user (ptr, x, size)) ? -EFAULT :0 ; +int n; + + n =raw_copy_to_user (ptr, x, size); +kmsan_copy_to_user (ptr, x, size, n); +return unlikely (n) ? -EFAULT :0 ; }#define __put_user_fn (sz, u, k) __put_user_fn(sz, u, k)@@ -203,7 +207,11 @@ extern int __put_user_bad(void) __attribute__((noreturn));#ifndef __get_user_fnstatic inline int __get_user_fn (size_t size,const void __user *ptr,void *x) { -return unlikely (raw_copy_from_user (x, ptr, size)) ? -EFAULT :0 ; +int copied, to_copy = size; + + copied =raw_copy_from_user (x, ptr, size); +kmsan_unpoison_shadow (x, to_copy - copied); +return unlikely (copied) ? -EFAULT :0 ; }#define __get_user_fn (sz, u, k) __get_user_fn(sz, u, k)include/linux/uaccess.h +6
-1@@ -5,6 +5,7 @@#include <linux/sched.h> #include <linux/thread_info.h> #include <linux/kasan-checks.h> +#include <linux/kmsan-checks.h> #define uaccess_kernel () segment_eq(get_fs(), KERNEL_DS)@@ -58,18 +59,26 @@static __always_inline __must_check unsigned long __copy_from_user_inatomic (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_write (to, n);check_object_size (to, n, false); -return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }Quoted 7 lines
include/linux/uaccess.h line 74
Quoted 4 lines
include/linux/uaccess.h +1
-0static __always_inline __must_check unsigned long __copy_from_user (void *to,const void __user *from,unsigned long n) { +unsigned long to_copy = n;This is confusing. I think we need a var for
raw_copy_from_user()return value instead. Same in functions above and below.
Quoted 3 lines
raw_copy_from_user()returns the number of bytes _not_ copied from userspace. So in the case it returns 0 we need to unpoisonto_copybytes.
My point was that reasiggning the value of n to the return value of raw_copy_from_user(), but saving the original n into to_copy is confusing. It would be easier to read this code, is n was left as is, and there was a new var for raw_copy_from_user() return value.
Quoted 151 lines
Quoted 129 lines
Quoted 129 lines
include/linux/uaccess.h +20
-4+might_fault ();kasan_check_write (to, n);check_object_size (to, n, false); -return raw_copy_from_user (to, from, n); + n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); +return n; }/** @@ -88,29 +97,39 @@ __copy_from_user(void *to, const void __user *from, unsigned long n)static __always_inline __must_check unsigned long __copy_to_user_inatomic (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }static __always_inline __must_check unsigned long __copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();kasan_check_read (from, n);check_object_size (from, n, true); -return raw_copy_to_user (to, from, n); + n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user ((const void *)to, from, to_copy, n); +return n; }#ifdef INLINE_COPY_FROM_USER static inline __must_check unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n; +might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);@@ -125,10 +144,13 @@ _copy_from_user(void *, const void __user *, unsigned long);static inline __must_check unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n; +might_fault ();if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }lib/iov_iter.c +6
-0@@ -137,18 +137,24 @@static int copyout (void __user *to,const void *from,size_t n) { +size_t to_copy = n; +if (access_ok (to, n)) {kasan_check_read (from, n); n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; }static int copyin (void *to,const void __user *from,size_t n) { +size_t to_copy = n; +if (access_ok (from, n)) {kasan_check_write (to, n); n =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - n); }return n; }lib/usercopy.c +4
-1@@ -1,4 +1,5 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/kmsan-checks.h> #include <linux/uaccess.h> #include <linux/bitops.h> @@ -7,11 +8,12 @@#ifndef INLINE_COPY_FROM_USER unsigned long _copy_from_user (void *to,const void __user *from,unsigned long n) { -unsigned long res = n; +unsigned long res = n, to_copy = n;might_fault ();if (likely (access_ok (from, n))) {kasan_check_write (to, n); res =raw_copy_from_user (to, from, n); +kmsan_unpoison_shadow (to, to_copy - res); }if (unlikely (res))memset (to + (n - res),0 , res);@@ -23,10 +25,12 @@ EXPORT_SYMBOL(_copy_from_user);#ifndef INLINE_COPY_TO_USER unsigned long _copy_to_user (void __user *to,const void *from,unsigned long n) { +unsigned long to_copy = n;might_fault ();if (likely (access_ok (to, n))) {kasan_check_read (from, n);Quoted 8 lines
Quoted 8 lines
lib/usercopy.c +1
-1n =raw_copy_to_user (to, from, n); +kmsan_copy_to_user (to, from, to_copy, n); }return n; } --2.24.0.432.g9d3f5f5b63-goog
--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Alexander Potapenko
maintainerRe: [PATCH RFC v3 27/36] kmsan: hooks for copy_to_user() and friends
Quoted 5 lines
My point was that reasiggning the value of n to the return value of
raw_copy_from_user(), but saving the original n intoto_copyis confusing. It would be easier to read this code, is n was left as is, and there was a new var forraw_copy_from_user()return value.
Ok, will do in v4.
Thanks!
[email protected]
maintainerRFC PATCH v3 28/36 kmsan: enable KMSAN builds
In reply to [email protected]Make KMSAN usable by adding the necessary Makefile bits and calling KMSAN initialization routines (kmsan_initialize_shadow() and kmsan_initialize()) from init/main.c
To: Alexander Potapenko <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
This patch was previously called "kmsan: Changing existing files to enable KMSAN builds". Logically unrelated parts of it were split away.
Change-Id: I37e0b7f2d2f2b0aeac5753ff9d6b411485fc374e
--- Makefile | 3 ++- init/main.c | 3 +++ mm/Makefile | 1 + scripts/Makefile.lib | 6 ++++++ 4 files changed, 12 insertions(+), 1 deletion(-)
Makefile +2 -1
@@ -478,7 +478,7 @@ export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_KMSAN export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
@@ -898,6 +898,7 @@ KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none) endif include scripts/Makefile.kasan +include scripts/Makefile.kmsan include scripts/Makefile.extrawarn include scripts/Makefile.ubsan
init/main.c +3 -0
@@ -32,6 +32,7 @@#include <linux/nmi.h> #include <linux/percpu.h> #include <linux/kmod.h> +#include <linux/kmsan.h> #include <linux/vmalloc.h> #include <linux/kernel_stat.h> #include <linux/start_kernel.h>
@@ -554,6 +555,7 @@ static void __init mm_init(void)*/ page_ext_init_flatmem ();report_meminit (); +kmsan_initialize_shadow ();mem_init ();kmem_cache_init ();kmemleak_init ();
@@ -625,6 +627,7 @@ asmlinkage __visible void __init start_kernel(void)sort_main_extable ();trap_init ();mm_init (); +kmsan_initialize ();ftrace_init ();
mm/Makefile +1 -0
@@ -71,6 +71,7 @@ obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o obj-$(CONFIG_KASAN) += kasan/ +obj-$(CONFIG_KMSAN) += kmsan/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-$(CONFIG_MEMTEST) += memtest.o
scripts/Makefile.lib +6 -0
@@ -140,6 +140,12 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE)) endif +ifeq ($(CONFIG_KMSAN),y) +_c_flags += $(if $(patsubst n%,, \ + $(KMSAN_SANITIZE_$(basetarget).o)$(KMSAN_SANITIZE)y), \ + $(CFLAGS_KMSAN)) +endif + ifeq ($(CONFIG_UBSAN),y) _c_flags += $(if $(patsubst n%,, \ $(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Re: [PATCH RFC v3 28/36] kmsan: enable KMSAN builds
Quoted 4 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Make KMSAN usable by adding the necessary
Makefilebits and calling KMSAN initialization routines (kmsan_initialize_shadow()andkmsan_initialize()) frominit/main.c
I think it makes sense to split these two parts into separate patches as well.
Quoted 102 lines
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
This patch was previously called "kmsan: Changing existing files to enable KMSAN builds". Logically unrelated parts of it were split away.Change-Id: I37e0b7f2d2f2b0aeac5753ff9d6b411485fc374e
--- Makefile | 3 ++- init/main.c | 3 +++ mm/Makefile | 1 + scripts/Makefile.lib | 6 ++++++ 4 files changed, 12 insertions(+), 1 deletion(-)Makefile +2
-1@@ -478,7 +478,7 @@ export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_KMSAN export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL@@ -898,6 +898,7 @@ KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none) endif include scripts/Makefile.kasan +include scripts/Makefile.kmsan include scripts/Makefile.extrawarn include scripts/Makefile.ubsaninit/main.c +3
-0@@ -32,6 +32,7 @@#include <linux/nmi.h> #include <linux/percpu.h> #include <linux/kmod.h> +#include <linux/kmsan.h> #include <linux/vmalloc.h> #include <linux/kernel_stat.h> #include <linux/start_kernel.h> @@ -554,6 +555,7 @@ static void __init mm_init(void)*/ page_ext_init_flatmem ();report_meminit (); +kmsan_initialize_shadow ();mem_init ();kmem_cache_init ();kmemleak_init ();@@ -625,6 +627,7 @@ asmlinkage __visible void __init start_kernel(void)sort_main_extable ();trap_init ();mm_init (); +kmsan_initialize ();ftrace_init ();mm/Makefile +1
-0@@ -71,6 +71,7 @@ obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o obj-$(CONFIG_KASAN) += kasan/ +obj-$(CONFIG_KMSAN) += kmsan/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-$(CONFIG_MEMTEST) += memtest.oscripts/Makefile.lib +6
-1@@ -140,6 +140,12 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE)) endif +ifeq ($(CONFIG_KMSAN),y) +_c_flags += $(if $(patsubst n%,, \ + $(KMSAN_SANITIZE_$(basetarget).o)$(KMSAN_SANITIZE)y), \ + $(CFLAGS_KMSAN)) +endif + ifeq ($(CONFIG_UBSAN),y) _c_flags += $(if $(patsubst n%,, \ $(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \ --2.24.0.432.g9d3f5f5b63-goog
Alexander Potapenko
maintainerRe: [PATCH RFC v3 28/36] kmsan: enable KMSAN builds
Quoted 9 lines · Andrey Konovalov
Quoted 4 lines · On Fri, Nov 22, 2019 at 12:27 PM <[email protected]> wrote:
Make KMSAN usable by adding the necessary
Makefilebits and calling KMSAN initialization routines (kmsan_initialize_shadow()andkmsan_initialize()) frominit/main.cI think it makes sense to split these two parts into separate patches as well.
Done in v4.
[email protected]
maintainerRFC PATCH v3 29/36 kmsan: handle /dev/[u]random
In reply to [email protected]The random number generator may use uninitialized memory, but it may not return uninitialized values. Unpoison the output buffer in _extract_crng() to prevent false reports.
To: Alexander Potapenko <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: "Theodore Ts'o" <[email protected]>
Cc: Dmitry Torokhov <[email protected]>
Cc: Martin K. Petersen <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Eric Van Hensbergen <[email protected]>
Cc: Takashi Iwai <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: [email protected]
---
This patch was previously known as "kmsan: unpoisoning buffers from devices etc.", but it turned out to be possible to drop most of the annotations from that patch, so it only relates to /dev/random now.
Change-Id: Id460e7a86ce564f1357469f53d0c7410ca08f0e9
--- drivers/char/random.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/char/random.c +6 -0
@@ -320,6 +320,7 @@#include <linux/fs.h> #include <linux/genhd.h> #include <linux/interrupt.h> +#include <linux/kmsan-checks.h> #include <linux/mm.h> #include <linux/nodemask.h> #include <linux/spinlock.h>
@@ -1061,6 +1062,11 @@ static void _extract_crng(struct crng_state *crng,spin_lock_irqsave (&crng->lock, flags);if (arch_get_random_long (&v)) crng->state[14 ] ^= v; +/* +* Regardless of where the random data comes from, KMSAN should treat +* it as initialized. +*/ +kmsan_unpoison_shadow (crng->state,sizeof (crng->state));chacha20_block (&crng->state[0 ], out);if (crng->state[12 ] ==0 ) crng->state[13 ]++;
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
[email protected]
maintainerRFC PATCH v3 30/36 kmsan: virtio: check/unpoison scatterlist in vring_map_one_sg()
In reply to [email protected]If vring doesn't use the DMA API, KMSAN is unable to tell whether the memory is initialized by hardware. Explicitly call kmsan_handle_dma() from vring_map_one_sg() in this case to prevent false positives.
---
Change-Id: Icc8678289b7084139320fc503898a67aa9803458
--- drivers/virtio/virtio_ring.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/virtio/virtio_ring.c +9 -1
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/hrtimer.h> #include <linux/dma-mapping.h> +#include <linux/kmsan-checks.h> #include <xen/xen.h> #ifdef DEBUG
@@ -326,8 +327,15 @@ static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg, enum dma_data_direction direction) { -if (!vq->use_dma_api) +if (!vq->use_dma_api) { +/* +* If DMA is not used, KMSAN doesn't know that the scatterlist +* is initialized by the hardware. Explicitly check/unpoison it +* depending on the direction. +*/ +kmsan_handle_dma (sg_virt (sg), sg->length, direction);return (dma_addr_t )sg_phys (sg); + }/* * We can't use dma_map_sg, because we don't use scatterlists in
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
[email protected]
maintainerRFC PATCH v3 31/36 kmsan: disable strscpy() optimization under KMSAN
In reply to [email protected]Disable the efficient 8-byte reading under KMSAN to avoid false positives.
To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]
---
Change-Id: I25d1acf5c3df6eff85894cd94f5ddbe93308271c
--- lib/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
lib/string.c +4 -1
@@ -186,7 +186,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count)if (count ==0 ||WARN_ON_ONCE (count >INT_MAX ))return -E2BIG ; -#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS +/** +* Disable the efficient 8-byte reading under KMSAN to avoid false positives. +*/ +#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) && !defined(CONFIG_KMSAN )/* * If src is unaligned, don't cross a page boundary, * since we don't know if the next page is mapped.
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Marco Elver
reviewerRe: [PATCH RFC v3 31/36] kmsan: disable strscpy() optimization under KMSAN
Quoted 21 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
Disable the efficient 8-byte reading under KMSAN to avoid false positives.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
Change-Id: I25d1acf5c3df6eff85894cd94f5ddbe93308271c
--- lib/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)lib/string.c +0
-0
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 189
Quoted 5 lines
lib/string.c +1
-1@@ -186,7 +186,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count)if (count ==0 ||WARN_ON_ONCE (count >INT_MAX ))return -E2BIG ; -#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS +/**
Why a doc comment?
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 191
Quoted 2 lines
lib/string.c +2
-0+* Disable the efficient 8-byte reading under KMSAN to avoid false positives. +*/
AFAIK the CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS case is about unaligned accesses crossing page boundaries. In the #else case it's still going to do word-at-a-time if both src and dest are aligned, so the comment above is somewhat inaccurate.
Quoted 7 lines
lib/string.c +1
-1+#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) && !defined(CONFIG_KMSAN )/* * If src is unaligned, don't cross a page boundary, * since we don't know if the next page is mapped. -- 2.24.0.432.g9d3f5f5b63-goog
Alexander Potapenko
maintainerRe: [PATCH RFC v3 31/36] kmsan: disable strscpy() optimization under KMSAN
Quoted 26 lines · Marco Elver
Quoted 24 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
Disable the efficient 8-byte reading under KMSAN to avoid false positives.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
Change-Id: I25d1acf5c3df6eff85894cd94f5ddbe93308271c
--- lib/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)lib/string.c +0
-0@@ -186,7 +186,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count)if (count ==0 ||WARN_ON_ONCE (count >INT_MAX ))return -E2BIG ;
Quoted 4 lines
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 189
Quoted 2 lines
lib/string.c +1
-1-#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS +/** Why a doc comment?
Will fix, thanks!
Quoted 7 lines
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 191
Quoted 2 lines
lib/string.c +2
-0+* Disable the efficient 8-byte reading under KMSAN to avoid false positives. +*/ AFAIK the
CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESScase is about unaligned accesses crossing page boundaries. In the #else case it's still going to do word-at-a-time if both src and dest are aligned, so the comment above is somewhat inaccurate.
Yes, this makes little sense.
Reading word-at-a-time shouldn't induce any errors, although it may generate redundant stack IDs for values that will never be used. I'll try to drop this patch.
Quoted 7 lines
Quoted 7 lines
lib/string.c +1
-1+#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) && !defined(CONFIG_KMSAN )/* * If src is unaligned, don't cross a page boundary, * since we don't know if the next page is mapped. -- 2.24.0.432.g9d3f5f5b63-goog
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Alexander Potapenko
maintainerRe: [PATCH RFC v3 31/36] kmsan: disable strscpy() optimization under KMSAN
Quoted 36 lines · Alexander Potapenko
Quoted 26 lines · Marco Elver
Quoted 24 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
Disable the efficient 8-byte reading under KMSAN to avoid false positives.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
Change-Id: I25d1acf5c3df6eff85894cd94f5ddbe93308271c
--- lib/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)lib/string.c +0
-0@@ -186,7 +186,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count)if (count ==0 ||WARN_ON_ONCE (count >INT_MAX ))return -E2BIG ;Quoted 4 lines
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 189
Quoted 2 lines
lib/string.c +1
-1-#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS +/** Why a doc comment?
Will fix, thanks!
Quoted 3 lines
Quoted 2 lines
lib/string.c +2
-0+* Disable the efficient 8-byte reading under KMSAN to avoid false positives. +*/
Quoted 8 lines
Quoted 4 lines
AFAIK the
CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESScase is about unaligned accesses crossing page boundaries. In the #else case it's still going to do word-at-a-time if both src and dest are aligned, so the comment above is somewhat inaccurate.Yes, this makes little sense.
Reading word-at-a-time shouldn't induce any errors, although it may generate redundant stack IDs for values that will never be used. I'll try to drop this patch.
Turns out the patch is still needed, as read_word_at_a_time may read uninitialized bytes which are then used in comparisons. I've changed the patch to always set max=0 under KMSAN:
https://github.com/google/kmsan/commit/3ff43863bf53dd871a3d4dc4fbb2a76d79b4db4f
Will include this version in v4 series.
Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
lib/string.c +1
-1+#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) && !defined(CONFIG_KMSAN )/* * If src is unaligned, don't cross a page boundary, * since we don't know if the next page is mapped. -- 2.24.0.432.g9d3f5f5b63-goog
--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Marco Elver
reviewerRe: [PATCH RFC v3 31/36] kmsan: disable strscpy() optimization under KMSAN
Quoted 46 lines · Alexander Potapenko
Quoted 36 lines · Alexander Potapenko
Quoted 26 lines · Marco Elver
Quoted 24 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
Disable the efficient 8-byte reading under KMSAN to avoid false positives.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
Change-Id: I25d1acf5c3df6eff85894cd94f5ddbe93308271c
--- lib/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)lib/string.c +0
-0@@ -186,7 +186,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count)if (count ==0 ||WARN_ON_ONCE (count >INT_MAX ))return -E2BIG ;Quoted 4 lines
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 189
Quoted 2 lines
lib/string.c +1
-1-#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS +/** Why a doc comment?
Will fix, thanks!
Quoted 3 lines
Quoted 2 lines
lib/string.c +2
-0+* Disable the efficient 8-byte reading under KMSAN to avoid false positives. +*/ Quoted 8 lines
Quoted 4 lines
AFAIK the
CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESScase is about unaligned accesses crossing page boundaries. In the #else case it's still going to do word-at-a-time if both src and dest are aligned, so the comment above is somewhat inaccurate.Yes, this makes little sense.
Reading word-at-a-time shouldn't induce any errors, although it may generate redundant stack IDs for values that will never be used. I'll try to drop this patch.
Quoted 5 lines
Turns out the patch is still needed, as
read_word_at_a_timemay read uninitialized bytes which are then used in comparisons. I've changed the patch to always set max=0 under KMSAN:
https://github.com/google/kmsan/commit/3ff43863bf53dd871a3d4dc4fbb2a76d79b4db4f
Will include this version in v4 series.
So was the previous version working because all strscpy where this was relevant were unaligned?
Re new patch: I think here it should be trivial to use 'if (IS_ENABLED(CONFIG_KMSAN))' instead of #ifdef.
Thanks,
-- Marco
Quoted 35 lines
Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
lib/string.c +1
-1+#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) && !defined(CONFIG_KMSAN )/* * If src is unaligned, don't cross a page boundary, * since we don't know if the next page is mapped. -- 2.24.0.432.g9d3f5f5b63-goog
--
Alexander Potapenko
Software EngineerQuoted 7 lines
Google Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Alexander Potapenko
maintainerRe: [PATCH RFC v3 31/36] kmsan: disable strscpy() optimization under KMSAN
Quoted 48 lines · Marco Elver
Quoted 46 lines · Alexander Potapenko
Quoted 44 lines · Alexander Potapenko
Quoted 26 lines · Marco Elver
Quoted 24 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
Disable the efficient 8-byte reading under KMSAN to avoid false positives.
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: [email protected]---
Change-Id: I25d1acf5c3df6eff85894cd94f5ddbe93308271c
--- lib/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)lib/string.c +0
-0@@ -186,7 +186,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count)if (count ==0 ||WARN_ON_ONCE (count >INT_MAX ))return -E2BIG ;Quoted 4 lines
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 189
Quoted 2 lines
lib/string.c +1
-1-#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS +/** Why a doc comment?
Will fix, thanks!
Quoted 7 lines
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 191
Quoted 2 lines
lib/string.c +2
-0+* Disable the efficient 8-byte reading under KMSAN to avoid false positives. +*/ AFAIK the
CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESScase is about unaligned accesses crossing page boundaries. In the #else case it's still going to do word-at-a-time if both src and dest are aligned, so the comment above is somewhat inaccurate.Yes, this makes little sense.
Reading word-at-a-time shouldn't induce any errors, although it may generate redundant stack IDs for values that will never be used. I'll try to drop this patch.
Quoted 8 lines
Quoted 5 lines
Turns out the patch is still needed, as
read_word_at_a_timemay read uninitialized bytes which are then used in comparisons. I've changed the patch to always set max=0 under KMSAN:
https://github.com/google/kmsan/commit/3ff43863bf53dd871a3d4dc4fbb2a76d79b4db4f
Will include this version in v4 series.So was the previous version working because all strscpy where this was relevant were unaligned?
Right.
Quoted 2 lines
Re new patch: I think here it should be trivial to use 'if (
IS_ENABLED(CONFIG_KMSAN))' instead of #ifdef.
Yes, sorry, I always forget about IS_ENABLED.
Quoted 38 lines
Thanks,
-- MarcoQuoted 35 lines
Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
lib/string.c +1
-1+#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) && !defined(CONFIG_KMSAN )/* * If src is unaligned, don't cross a page boundary, * since we don't know if the next page is mapped. -- 2.24.0.432.g9d3f5f5b63-goog
--
Alexander Potapenko
Software EngineerQuoted 7 lines
Google Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
[email protected]
maintainerRFC PATCH v3 32/36 kmsan: add iomap support
In reply to [email protected]Functions from lib/iomap.c interact with hardware, so KMSAN must ensure that:
- every read function returns an initialized value
- every write function checks values before sending them to hardware.
---
Change-Id: Iacd96265e56398d8c111637ddad3cad727e48c8d
--- lib/iomap.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
lib/iomap.c +40 -0
@@ -6,6 +6,7 @@*/ #include <linux/pci.h> #include <linux/io.h> +#include <linux/kmsan-checks.h> #include <linux/export.h>
@@ -70,26 +71,31 @@ static void bad_io_access(unsigned long port, const char *access)#define mmio_read64be (addr) swab64(readq(addr))#endif +__no_sanitize_memory unsigned int ioread8(void __iomem *addr) { IO_COND(addr, return inb(port), return readb(addr));return 0xff ; } +__no_sanitize_memory unsigned int ioread16(void __iomem *addr) { IO_COND(addr, return inw(port), return readw(addr));return 0xffff ; } +__no_sanitize_memory unsigned int ioread16be(void __iomem *addr) { IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));return 0xffff ; } +__no_sanitize_memory unsigned int ioread32(void __iomem *addr) { IO_COND(addr, return inl(port), return readl(addr));return 0xffffffff ; } +__no_sanitize_memory unsigned int ioread32be(void __iomem *addr) { IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
@@ -142,18 +148,21 @@ static u64 pio_read64be_hi_lo(unsigned long port)return lo | (hi <<32 ); } +__no_sanitize_memory u64 ioread64_lo_hi(void __iomem *addr) { IO_COND(addr, return pio_read64_lo_hi(port), return readq(addr));return 0xffffffffffffffffULL ; } +__no_sanitize_memory u64 ioread64_hi_lo(void __iomem *addr) { IO_COND(addr, return pio_read64_hi_lo(port), return readq(addr));return 0xffffffffffffffffULL ; } +__no_sanitize_memory u64 ioread64be_lo_hi(void __iomem *addr) { IO_COND(addr, return pio_read64be_lo_hi(port),
@@ -161,6 +170,7 @@ u64 ioread64be_lo_hi(void __iomem *addr)return 0xffffffffffffffffULL ; } +__no_sanitize_memory u64 ioread64be_hi_lo(void __iomem *addr) { IO_COND(addr, return pio_read64be_hi_lo(port),
@@ -188,22 +198,32 @@ EXPORT_SYMBOL(ioread64be_hi_lo);void iowrite8 (u8 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,outb (val,port),writeb (val, addr)); }void iowrite16 (u16 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,outw (val,port),writew (val, addr)); }void iowrite16be (u16 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,pio_write16be (val,port),mmio_write16be (val, addr)); }void iowrite32 (u32 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,outl (val,port),writel (val, addr)); }void iowrite32be (u32 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,pio_write32be (val,port),mmio_write32be (val, addr)); }EXPORT_SYMBOL (iowrite8);
@@ -239,24 +259,32 @@ static void pio_write64be_hi_lo(u64 val, unsigned long port)void iowrite64_lo_hi (u64 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,pio_write64_lo_hi (val, port),writeq (val, addr)); }void iowrite64_hi_lo (u64 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,pio_write64_hi_lo (val, port),writeq (val, addr)); }void iowrite64be_lo_hi (u64 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,pio_write64be_lo_hi (val, port),mmio_write64be (val, addr)); }void iowrite64be_hi_lo (u64 val,void __iomem *addr) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (&val,sizeof (val));IO_COND (addr,pio_write64be_hi_lo (val, port),mmio_write64be (val, addr)); }
@@ -328,14 +356,20 @@ static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)void ioread8_rep (void __iomem *addr,void *dst,unsigned long count) {IO_COND (addr,insb (port,dst,count),mmio_insb (addr, dst, count)); +/* KMSAN must treat values read from devices as initialized. */ +kmsan_unpoison_shadow (dst, count); }void ioread16_rep (void __iomem *addr,void *dst,unsigned long count) {IO_COND (addr,insw (port,dst,count),mmio_insw (addr, dst, count)); +/* KMSAN must treat values read from devices as initialized. */ +kmsan_unpoison_shadow (dst, count *2 ); }void ioread32_rep (void __iomem *addr,void *dst,unsigned long count) {IO_COND (addr,insl (port,dst,count),mmio_insl (addr, dst, count)); +/* KMSAN must treat values read from devices as initialized. */ +kmsan_unpoison_shadow (dst, count *4 ); }EXPORT_SYMBOL (ioread8_rep);EXPORT_SYMBOL (ioread16_rep);
@@ -343,14 +377,20 @@ EXPORT_SYMBOL(ioread32_rep);void iowrite8_rep (void __iomem *addr,const void *src,unsigned long count) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);IO_COND (addr,outsb (port, src, count),mmio_outsb (addr, src, count)); }void iowrite16_rep (void __iomem *addr,const void *src,unsigned long count) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);IO_COND (addr,outsw (port, src, count),mmio_outsw (addr, src, count)); }void iowrite32_rep (void __iomem *addr,const void *src,unsigned long count) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);IO_COND (addr,outsl (port, src,count),mmio_outsl (addr, src, count)); }EXPORT_SYMBOL (iowrite8_rep);
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Marco Elver
reviewerRe: [PATCH RFC v3 32/36] kmsan: add iomap support
Quoted 24 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
Functions from
lib/iomap.cinteract with hardware, so KMSAN must ensure that:
- every read function returns an initialized value
- every write function checks values before sending them to hardware.Signed-off-by Alexander Potapenko <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Darrick J. Wong <[email protected]>Cc Vegard Nossum <[email protected]>Cc Dmitry Vyukov <[email protected]>---
Change-Id: Iacd96265e56398d8c111637ddad3cad727e48c8d
--- lib/iomap.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)lib/iomap.c +0
-0@@ -6,6 +6,7 @@
[...]
lib/iomap.c EXPORT_SYMBOL(ioread32_rep); · line 387
Quoted 10 lines
void
iowrite8_rep(void __iomem *addr, const void *src, unsigned long count){lib/iomap.c +4
-0+/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);IO_COND (addr,outsb (port, src, count),mmio_outsb (addr, src, count)); }void iowrite16_rep (void __iomem *addr,const void *src,unsigned long count) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);
Should this be 'count * 2' ?
lib/iomap.c
Quoted 6 lines
IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));}
voidiowrite32_rep(void __iomem *addr, const void *src, unsigned long count){lib/iomap.c +2
-0+/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);
Should this be 'count * 4' ?
Thanks,
-- Marco
Alexander Potapenko
maintainerRe: [PATCH RFC v3 32/36] kmsan: add iomap support
Quoted 38 lines · Marco Elver
Quoted 24 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
Functions from
lib/iomap.cinteract with hardware, so KMSAN must ensure that:
- every read function returns an initialized value
- every write function checks values before sending them to hardware.Signed-off-by Alexander Potapenko <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Darrick J. Wong <[email protected]>Cc Vegard Nossum <[email protected]>Cc Dmitry Vyukov <[email protected]>---
Change-Id: Iacd96265e56398d8c111637ddad3cad727e48c8d
--- lib/iomap.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)lib/iomap.c +0
-0@@ -6,6 +6,7 @@[...]
lib/iomap.c EXPORT_SYMBOL(ioread32_rep); · line 387
Quoted 10 lines
void
iowrite8_rep(void __iomem *addr, const void *src, unsigned long count){lib/iomap.c +4
-0+/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);IO_COND (addr,outsb (port, src, count),mmio_outsb (addr, src, count)); }void iowrite16_rep (void __iomem *addr,const void *src,unsigned long count) { +/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);
Quoted 1 line
Should this be 'count * 2' ?
Good catch, thanks!
Will fix in v4.
Quoted 9 lines
lib/iomap.c
Quoted 7 lines
IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));}
voidiowrite32_rep(void __iomem *addr, const void *src, unsigned long count){lib/iomap.c +2
-0+/* Make sure uninitialized memory isn't copied to devices. */ +kmsan_check_memory (src, count);Should this be 'count * 4' ?
Ditto
Quoted 2 lines
Thanks,
-- Marco
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
[email protected]
maintainerRFC PATCH v3 33/36 kmsan: dma: unpoison memory mapped by dma_direct_map_page()
In reply to [email protected]KMSAN doesn't know about DMA memory writes performed by devices. We unpoison such memory when it's mapped to avoid false positive reports.
---
Change-Id: Ib1019ed531fea69f88b5cdec3d1e27403f2f3d64
--- kernel/dma/direct.c | 1 + 1 file changed, 1 insertion(+)
kernel/dma/direct.c +1 -0
@@ -341,6 +341,7 @@ dma_addr_t dma_direct_map_page(struct device *dev, struct page *page,report_addr (dev, dma_addr, size);return DMA_MAPPING_ERROR ; } +kmsan_handle_dma (page_address (page) + offset, size, dir);if (!dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_SKIP_CPU_SYNC ))arch_sync_dma_for_device (dev, phys, size, dir);
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
[email protected]
maintainerRFC PATCH v3 34/36 kmsan: disable physical page merging in biovec
In reply to [email protected]KMSAN metadata for consequent physical pages may be inconsequent, therefore accessing such pages together may lead to metadata corruption.
We disable merging pages in biovec to prevent such corruptions.
To: Alexander Potapenko <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: [email protected]
---
Change-Id: Id2f2babaf662ac44675c4f2790f4a80ddc328fa7
--- block/blk.h | 7 +++++++ 1 file changed, 7 insertions(+)
block/blk.h +7 -0
@@ -78,6 +78,13 @@ static inline bool biovec_phys_mergeable(struct request_queue *q,phys_addr_t addr1 =page_to_phys (vec1->bv_page) + vec1->bv_offset;phys_addr_t addr2 =page_to_phys (vec2->bv_page) + vec2->bv_offset; +#ifdef CONFIG_KMSAN +/* +* Merging consequent physical pages may not work correctly under KMSAN +* if their metadata pages aren't consequent. Just disable merging. +*/ +return false; +#endif if (addr1 + vec1->bv_len != addr2)return false;if (xen_domain () && !xen_biovec_phys_mergeable (vec1, vec2->bv_page))
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Marco Elver
reviewerRe: [PATCH RFC v3 34/36] kmsan: disable physical page merging in biovec
Quoted 26 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
KMSAN metadata for consequent physical pages may be inconsequent, therefore accessing such pages together may lead to metadata corruption.
We disable merging pages in biovec to prevent such corruptions.Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: [email protected]
---Change-Id: Id2f2babaf662ac44675c4f2790f4a80ddc328fa7
--- block/blk.h | 7 +++++++ 1 file changed, 7 insertions(+)block/blk.h +0
-0
block/blk.h static inline bool biovec_phys_mergeable(struct request_queue *q, · line 81
Quoted 4 lines
block/blk.h +1
-0@@ -78,6 +78,13 @@ static inline bool biovec_phys_mergeable(struct request_queue *q,phys_addr_t addr1 =page_to_phys (vec1->bv_page) + vec1->bv_offset;phys_addr_t addr2 =page_to_phys (vec2->bv_page) + vec2->bv_offset; +#ifdef CONFIG_KMSAN
Prefer 'if (IS_ENABLED(CONFIG_KMSAN))'.
Thanks,
-- Marco
Alexander Potapenko
maintainerRe: [PATCH RFC v3 34/36] kmsan: disable physical page merging in biovec
Quoted 31 lines · Marco Elver
Quoted 29 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
KMSAN metadata for consequent physical pages may be inconsequent, therefore accessing such pages together may lead to metadata corruption.
We disable merging pages in biovec to prevent such corruptions.Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: [email protected]
---Change-Id: Id2f2babaf662ac44675c4f2790f4a80ddc328fa7
--- block/blk.h | 7 +++++++ 1 file changed, 7 insertions(+)block/blk.h +0
-0@@ -78,6 +78,13 @@ static inline bool biovec_phys_mergeable(struct request_queue *q,phys_addr_t addr1 =page_to_phys (vec1->bv_page) + vec1->bv_offset;phys_addr_t addr2 =page_to_phys (vec2->bv_page) + vec2->bv_offset;
Quoted 3 lines
block/blk.h static inline bool biovec_phys_mergeable(struct request_queue *q, · line 81
Quoted 1 line
block/blk.h +1
-0+#ifdef CONFIG_KMSAN Prefer 'if (
IS_ENABLED(CONFIG_KMSAN))'.
WBD in v4.
Quoted 2 lines
Thanks,
-- Marco
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
[email protected]
maintainerRFC PATCH v3 35/36 kmsan: ext4: skip block merging logic in ext4_mpage_readpages for KMSAN
In reply to [email protected]KMSAN doesn't allow treating adjacent memory pages as such, if they were allocated by different alloc_pages() calls. ext4_mpage_readpages() however does so: adjacent pages end up being passed together to dma_direct_map_sg().
To prevent this, jump directly to the buffer_head-based read function in KMSAN builds.
---
Change-Id: I54ae8af536626a988e6398ff18a06c179b0ce034
--- fs/ext4/readpage.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/ext4/readpage.c +11 -0
@@ -252,6 +252,17 @@ int ext4_mpage_readpages(struct address_space *mapping,if (page_has_buffers (page)) goto confused; +#if defined(CONFIG_KMSAN ) +/* +* The following code may treat adjacent pages allocated +* separately as a bigger contiguous allocation. +* KMSAN doesn't allow this, as the corresponding metadata +* pages may be separated. +* Skip this complex logic for KMSAN builds. +*/ + goto confused; +#endif + block_in_file = (sector_t )page->index << (PAGE_SHIFT - blkbits); last_block = block_in_file + nr_pages * blocks_per_page; last_block_in_file = (ext4_readpage_limit(inode) +
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Robin Murphy
reviewerRe: [PATCH RFC v3 35/36] kmsan: ext4: skip block merging logic in ext4_mpage_readpages for KMSAN
Quoted 4 lines · On 22/11/2019 11:26 am, [email protected] wrote:
KMSAN doesn't allow treating adjacent memory pages as such, if they were allocated by different
alloc_pages()calls.ext4_mpage_readpages()however does so: adjacent pages end up being passed together todma_direct_map_sg().
Urgh, there are definitely more places where physically-contiguous pages are coalesced into single scatterlist entries - see sg_alloc_table_from_pages() for instance. I wouldn't be surprised if there are further open-coded versions hiding out in various other drivers/subsystems too. Unless I've misunderstood, this seems like quite an invasive limitation :(
Robin.
Quoted 39 lines
To prevent this, jump directly to the buffer_head-based read function in KMSAN builds.
Signed-off-by Alexander Potapenko <[email protected]>Cc "Theodore Ts'o" <[email protected]>Cc Andreas Dilger <[email protected]>Cc Vegard Nossum <[email protected]>Cc Dmitry Vyukov <[email protected]>---
Change-Id: I54ae8af536626a988e6398ff18a06c179b0ce034
--- fs/ext4/readpage.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/ext4/readpage.c +11
-0@@ -252,6 +252,17 @@ int ext4_mpage_readpages(struct address_space *mapping,if (page_has_buffers (page)) goto confused; +#if defined(CONFIG_KMSAN ) +/* +* The following code may treat adjacent pages allocated +* separately as a bigger contiguous allocation. +* KMSAN doesn't allow this, as the corresponding metadata +* pages may be separated. +* Skip this complex logic for KMSAN builds. +*/ + goto confused; +#endif + block_in_file = (sector_t )page->index << (PAGE_SHIFT - blkbits); last_block = block_in_file + nr_pages * blocks_per_page; last_block_in_file = (ext4_readpage_limit(inode) +
Alexander Potapenko
maintainerRe: [PATCH RFC v3 35/36] kmsan: ext4: skip block merging logic in ext4_mpage_readpages for KMSAN
Quoted 7 lines · Robin Murphy
Quoted 4 lines · On 22/11/2019 11:26 am, [email protected] wrote:
KMSAN doesn't allow treating adjacent memory pages as such, if they were allocated by different
alloc_pages()calls.ext4_mpage_readpages()however does so: adjacent pages end up being passed together todma_direct_map_sg().
Quoted 6 lines
Urgh, there are definitely more places where physically-contiguous pages are coalesced into single scatterlist entries - see
sg_alloc_table_from_pages()for instance. I wouldn't be surprised if there are further open-coded versions hiding out in various otherdrivers/subsystemstoo. Unless I've misunderstood, this seems like quite an invasive limitation :(
You're right.
Other places haven't fired off so far, so I was just unaware of them, but I'm anticipating more.
There are two possible solutions to that problem:
1. Allocate shadow and origin pages at fixed offset from the kernel page. This is what we already do for vmalloc, but not for page_alloc(), as it turned out to be quite hard.
Ideas on how to implement this approach are still welcome, because it'll simplify the rest of the KMSAN runtime a lot.
2. Make all accesses touching non-contiguous pages access dummy shadow pages instead, so that such accesses don't produce any uninitialized values.
This is quite controversial, as it may prevent true positives from being reported.
Quoted 41 lines
Robin.
Quoted 39 lines
To prevent this, jump directly to the buffer_head-based read function in KMSAN builds.
Signed-off-by Alexander Potapenko <[email protected]>Cc "Theodore Ts'o" <[email protected]>Cc Andreas Dilger <[email protected]>Cc Vegard Nossum <[email protected]>Cc Dmitry Vyukov <[email protected]>---
Change-Id: I54ae8af536626a988e6398ff18a06c179b0ce034
--- fs/ext4/readpage.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/ext4/readpage.c +11
-0@@ -252,6 +252,17 @@ int ext4_mpage_readpages(struct address_space *mapping,if (page_has_buffers (page)) goto confused; +#if defined(CONFIG_KMSAN ) +/* +* The following code may treat adjacent pages allocated +* separately as a bigger contiguous allocation. +* KMSAN doesn't allow this, as the corresponding metadata +* pages may be separated. +* Skip this complex logic for KMSAN builds. +*/ + goto confused; +#endif + block_in_file = (sector_t )page->index << (PAGE_SHIFT - blkbits); last_block = block_in_file + nr_pages * blocks_per_page; last_block_in_file = (ext4_readpage_limit(inode) +
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Marco Elver
reviewerRe: [PATCH RFC v3 35/36] kmsan: ext4: skip block merging logic in ext4_mpage_readpages for KMSAN
In reply to [email protected]Quoted 26 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
KMSAN doesn't allow treating adjacent memory pages as such, if they were allocated by different
alloc_pages()calls.ext4_mpage_readpages()however does so: adjacent pages end up being passed together todma_direct_map_sg().
To prevent this, jump directly to the buffer_head-based read function in KMSAN builds.Signed-off-by Alexander Potapenko <[email protected]>Cc "Theodore Ts'o" <[email protected]>Cc Andreas Dilger <[email protected]>Cc Vegard Nossum <[email protected]>Cc Dmitry Vyukov <[email protected]>---
Change-Id: I54ae8af536626a988e6398ff18a06c179b0ce034
--- fs/ext4/readpage.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/ext4/readpage.c +0
-0
fs/ext4/readpage.c int ext4_mpage_readpages(struct address_space *mapping, · line 255
Quoted 4 lines
fs/ext4/readpage.c +1
-0@@ -252,6 +252,17 @@ int ext4_mpage_readpages(struct address_space *mapping,if (page_has_buffers (page)) goto confused; +#if defined(CONFIG_KMSAN )
Prefer 'if (IS_ENABLED(CONFIG_KMSAN))'.
Thanks,
-- Marco
Alexander Potapenko
maintainerRe: [PATCH RFC v3 35/36] kmsan: ext4: skip block merging logic in ext4_mpage_readpages for KMSAN
Quoted 31 lines · Marco Elver
Quoted 29 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
KMSAN doesn't allow treating adjacent memory pages as such, if they were allocated by different
alloc_pages()calls.ext4_mpage_readpages()however does so: adjacent pages end up being passed together todma_direct_map_sg().
To prevent this, jump directly to the buffer_head-based read function in KMSAN builds.Signed-off-by Alexander Potapenko <[email protected]>Cc "Theodore Ts'o" <[email protected]>Cc Andreas Dilger <[email protected]>Cc Vegard Nossum <[email protected]>Cc Dmitry Vyukov <[email protected]>---
Change-Id: I54ae8af536626a988e6398ff18a06c179b0ce034
--- fs/ext4/readpage.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/ext4/readpage.c +0
-0@@ -252,6 +252,17 @@ int ext4_mpage_readpages(struct address_space *mapping,if (page_has_buffers (page)) goto confused;
Quoted 3 lines
fs/ext4/readpage.c int ext4_mpage_readpages(struct address_space *mapping, · line 255
Quoted 1 line
fs/ext4/readpage.c +1
-0+#if defined(CONFIG_KMSAN )Prefer 'if (
IS_ENABLED(CONFIG_KMSAN))'.
Done in v4.
Quoted 2 lines
Thanks,
-- Marco
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
[email protected]
maintainerRFC PATCH v3 36/36 net: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN
In reply to [email protected]This is needed to allow memory tools like KASAN and KMSAN see the memory accesses from the checksum code. Without CONFIG_GENERIC_CSUM the tools can't see memory accesses originating from handwritten assembly code.
For KASAN it's a question of detecting more bugs, for KMSAN using the C implementation also helps avoid false positives originating from seemingly uninitialized checksum values.
To: Alexander Potapenko <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Randy Dunlap <[email protected]>
Cc: [email protected]
---
v2:
- dropped the "default n" (as requested by Randy Dunlap)
Change-Id: I645e2c097253a8d5717ad87e2e2df6f6f67251f3
--- arch/x86/Kconfig | 4 ++++ arch/x86/include/asm/checksum.h | 10 +++++++--- arch/x86/lib/Makefile | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/Kconfig +4 -0
@@ -272,6 +272,10 @@ config GENERIC_ISA_DMA def_bool y depends on ISA_DMA_API +config GENERIC_CSUM + bool + default y if KMSAN || KASAN + config GENERIC_BUG def_bool y depends on BUG
arch/x86/include/asm/checksum.h +7 -3
@@ -1,6 +1,10 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifdef CONFIG_X86_32 -# include <asm/checksum_32.h> +#ifdef CONFIG_GENERIC_CSUM +# include <asm-generic/checksum.h> #else -# include <asm/checksum_64.h> +# ifdef CONFIG_X86_32 +# include <asm/checksum_32.h> +# else +# include <asm/checksum_64.h> +# endif #endif
arch/x86/lib/Makefile +2 -0
@@ -55,7 +55,9 @@ endif lib-$(CONFIG_X86_USE_3DNOW) += mmx_32.o else obj-y += iomap_copy_64.o +ifneq ($(CONFIG_GENERIC_CSUM),y) lib-y += csum-partial_64.o csum-copy_64.o csum-wrappers_64.o +endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.o
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
Marco Elver
reviewerRe: [PATCH RFC v3 36/36] net: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN
Quoted 8 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
This is needed to allow memory tools like KASAN and KMSAN see the memory accesses from the checksum code. Without
CONFIG_GENERIC_CSUMthe tools can't see memory accesses originating from handwritten assembly code.
For KASAN it's a question of detecting more bugs, for KMSAN using the C implementation also helps avoid false positives originating from seemingly uninitialized checksum values.
The files touched are only in x86. The title mentions 'net' -- is this still accurate?
Quoted 26 lines
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Randy Dunlap <[email protected]>
Cc: [email protected]
---v2:
- dropped the "default n" (as requested by Randy Dunlap)Change-Id: I645e2c097253a8d5717ad87e2e2df6f6f67251f3
--- arch/x86/Kconfig | 4 ++++ arch/x86/include/asm/checksum.h | 10 +++++++--- arch/x86/lib/Makefile | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-)arch/x86/Kconfig +0
-0
arch/x86/Kconfig config GENERIC_ISA_DMA · line 277
Quoted 6 lines
arch/x86/Kconfig +3
-0@@ -272,6 +272,10 @@ config GENERIC_ISA_DMA def_bool y depends on ISA_DMA_API +config GENERIC_CSUM + bool + default y if KMSAN || KASAN
When would it be desirable to use GENERIC_CSUM without KMSAN or KASAN?
If it is generally a bad idea to disable GENERIC_CSUM with KMSAN and KASAN, does it make more sense to just remove this config option and unconditionally (if CONFIG_K{A,M}SAN) use the asm-generic version in checksum.h?
This would simplify this patch and avoid introducing a config option that will likely never be set explicitly.
Quoted 26 lines
arch/x86/Kconfig +0
-0config GENERIC_BUG def_bool y depends on BUGarch/x86/include/asm/checksum.h +7
-3@@ -1,6 +1,10 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifdef CONFIG_X86_32 -# include <asm/checksum_32.h> +#ifdef CONFIG_GENERIC_CSUM +# include <asm-generic/checksum.h> #else -# include <asm/checksum_64.h> +# ifdef CONFIG_X86_32 +# include <asm/checksum_32.h> +# else +# include <asm/checksum_64.h> +# endif #endif arch/x86/lib/Makefile +0
-0
arch/x86/lib/Makefile endif · line 63
Quoted 9 lines
arch/x86/lib/Makefile +2
-0@@ -55,7 +55,9 @@ endif lib-$(CONFIG_X86_USE_3DNOW) += mmx_32.o else obj-y += iomap_copy_64.o +ifneq ($(CONFIG_GENERIC_CSUM),y) lib-y += csum-partial_64.o csum-copy_64.o csum-wrappers_64.o +endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.o
Thanks,
-- Marco
Alexander Potapenko
maintainerRe: [PATCH RFC v3 36/36] net: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN
Quoted 11 lines · Marco Elver
Quoted 8 lines · On Fri, 22 Nov 2019 at 12:28, <[email protected]> wrote:
This is needed to allow memory tools like KASAN and KMSAN see the memory accesses from the checksum code. Without
CONFIG_GENERIC_CSUMthe tools can't see memory accesses originating from handwritten assembly code.
For KASAN it's a question of detecting more bugs, for KMSAN using the C implementation also helps avoid false positives originating from seemingly uninitialized checksum values.
Quoted 2 lines
The files touched are only in x86. The title mentions 'net' -- is this still accurate?
Not sure :/
I'll probably replace it with "x86:", as other patches to this file don't mention "net".
Quoted 35 lines
Quoted 26 lines
Signed-off-by Alexander Potapenko <[email protected]>To: Alexander Potapenko <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Randy Dunlap <[email protected]>
Cc: [email protected]
---v2:
- dropped the "default n" (as requested by Randy Dunlap)Change-Id: I645e2c097253a8d5717ad87e2e2df6f6f67251f3
--- arch/x86/Kconfig | 4 ++++ arch/x86/include/asm/checksum.h | 10 +++++++--- arch/x86/lib/Makefile | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-)arch/x86/Kconfig +0
-0arch/x86/Kconfig config GENERIC_ISA_DMA · line 277
Quoted 6 lines
arch/x86/Kconfig +3
-0@@ -272,6 +272,10 @@ config GENERIC_ISA_DMA def_bool y depends on ISA_DMA_API +config GENERIC_CSUM + bool + default y if KMSAN || KASANWhen would it be desirable to use
GENERIC_CSUMwithout KMSAN or KASAN?
Quoted 7 lines
If it is generally a bad idea to disable
GENERIC_CSUMwith KMSAN and KASAN, does it make more sense to just remove this config option and unconditionally (if CONFIG_K{A,M}SAN) use the asm-generic version inchecksum.h?This would simplify this patch and avoid introducing a config option that will likely never be set explicitly.
There is already a similar option for MIPS and m68k, so I think it's better to be consistent with them.
Quoted 38 lines
Quoted 26 lines
arch/x86/Kconfig +0
-0config GENERIC_BUG def_bool y depends on BUGarch/x86/include/asm/checksum.h +7
-3@@ -1,6 +1,10 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifdef CONFIG_X86_32 -# include <asm/checksum_32.h> +#ifdef CONFIG_GENERIC_CSUM +# include <asm-generic/checksum.h> #else -# include <asm/checksum_64.h> +# ifdef CONFIG_X86_32 +# include <asm/checksum_32.h> +# else +# include <asm/checksum_64.h> +# endif #endif arch/x86/lib/Makefile +0
-0arch/x86/lib/Makefile endif · line 63
Quoted 9 lines
arch/x86/lib/Makefile +2
-0@@ -55,7 +55,9 @@ endif lib-$(CONFIG_X86_USE_3DNOW) += mmx_32.o else obj-y += iomap_copy_64.o +ifneq ($(CONFIG_GENERIC_CSUM),y) lib-y += csum-partial_64.o csum-copy_64.o csum-wrappers_64.o +endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.oThanks,
-- Marco
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Marco Elver
reviewerQuoted 241 lines · On Fri, 22 Nov 2019 at 12:26, <[email protected]> wrote:
KernelMemorySanitizer (KMSAN) is a detector of errors related to uses of uninitialized memory. It relies on compile-time Clang instrumentation (similar to MSan in the userspace:
https://clang.llvm.org/docs/MemorySanitizer.html)
and tracks the state of every bit of kernel memory, being able to report an error if uninitialized value is used in a condition, dereferenced or copied to userspace, USB or network.KMSAN has reported more than 200 bugs in the past two years, most of them with the help of syzkaller (http://syzkaller.appspot.com).
The proposed patchset contains KMSAN runtime implementation together with small changes to other subsystems needed to make KMSAN work. The latter changes fall into several categories:
- nice-to-have features that are independent from KMSAN but simplify its implementation (stackdepot changes,CONFIG_GENERIC_CSUMetc.);
-Kconfigchanges that prohibit options incompatible with KMSAN;
- calls to KMSAN runtime functions that help KMSAN do the bookkeeping (e.g. tell it to allocate, copy or delete the metadata);
- calls to KMSAN runtime functions that tell KMSAN to check memory escaping the kernel for uninitialized values. These are required to increase the number of true positive error reports;
- calls to runtime functions that tell KMSAN to ignore certain memory ranges to avoid false negative reports. Most certainly there can be better ways to deal with every such report.This patchset allows one to boot and run a defconfig+KMSAN kernel on a QEMU without known major false positives. It however doesn't guarantee there are no false positives in drivers of certain devices or less tested subsystems, although KMSAN is actively tested on syzbot with quite a rich config.
One may find it handy to review these patches in Gerrit:
https://linux-review.googlesource.com/c/linux/kernel/git/torvalds/linux/+/1081
I've ensured the Change-Id: tags stay away from commit descriptions.The patchset was generated relative to Linux v5.4-rc5.
I also apologize for not sending every patch in the previous series to all recipients of patches from that series.
Note: checkpatch.pl complains a lot about the use of
BUG_ONin KMSAN source. I don't have a strong opinion on this, but KMSAN is a debugging tool, so any runtime invariant violation in it renders the tool useless. Therefore it doesn't make much sense to not terminate after a bug in KMSAN.Alexander Potapenko (36):
stackdepot: checkdepot_indexbefore accessing the stack slab
stackdepot: build with -fno-builtin
kasan: stackdepot: movefilter_irq_stacks()tostackdepot.c
stackdepot: reserve 5 extra bits indepot_stack_handle_t
kmsan: add ReST documentation
kmsan: gfp: introduce__GFP_NO_KMSAN_SHADOW
kmsan: introduce__no_sanitize_memoryand__SANITIZE_MEMORY__
kmsan: reduce vmalloc space
kmsan: add KMSAN bits to struct page and structtask_struct
kmsan: add KMSAN runtime
kmsan: stackdepot: don't allocate KMSAN metadata for stackdepot
kmsan: defineREAD_ONCE_NOCHECK()
kmsan: makeREAD_ONCE_TASK_STACK()return initialized values
kmsan: x86: sync metadata pages on page fault
kmsan: add tests for KMSAN
crypto: kmsan: disable accelerated configs under KMSAN
kmsan: x86: disableUNWINDER_ORCunder KMSAN
kmsan: disableLOCK_DEBUGGING_SUPPORT
kmsan: x86/asm: add KMSAN hooks toentry_64.S
kmsan: x86: increase stack sizes in KMSAN builds
kmsan: disable KMSAN instrumentation for certain kernel parts
kmsan: mm: call KMSAN hooks from SLUB code
kmsan: call KMSAN hooks where needed
kmsan: disable instrumentation of certain functions
kmsan: unpoison |tlb| inarch_tlb_gather_mmu()
kmsan: use__msan_memcpy()where possible.
kmsan: hooks forcopy_to_user()and friends
kmsan: enable KMSAN builds
kmsan: handle /dev/[u]random
kmsan: virtio: check/unpoison scatterlist invring_map_one_sg()
kmsan: disablestrscpy()optimization under KMSAN
kmsan: add iomap support
kmsan: dma: unpoison memory mapped bydma_direct_map_page()
kmsan: disable physical page merging in biovec
kmsan: ext4: skip block merging logic inext4_mpage_readpagesfor
KMSAN
net: kasan: kmsan: supportCONFIG_GENERIC_CSUMon x86, enable it for
KASAN/KMSANTo: Alexander Potapenko <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Andreas Dilger <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Darrick J. Wong <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Dmitry Torokhov <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Eric Biggers <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Eric Van Hensbergen <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Harry Wentland <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Ilya Leoshkevich <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Marek Szyprowski <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Martin K. Petersen <[email protected]>
Cc: Martin Schwidefsky <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Qian Cai <[email protected]>
Cc: Randy Dunlap <[email protected]>
Cc: Robin Murphy <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Takashi Iwai <[email protected]>
Cc: "Theodore Ts'o" <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Vasily Gorbik <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Wolfram Sang <[email protected]>
Cc: [email protected]Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kmsan.rst | 418 ++++++++++++++++++ Makefile | 3 +- arch/x86/Kconfig | 5 + arch/x86/Kconfig.debug | 3 + arch/x86/boot/Makefile | 2 + arch/x86/boot/compressed/Makefile | 2 + arch/x86/boot/compressed/misc.h | 1 + arch/x86/entry/common.c | 1 + arch/x86/entry/entry_64.S | 16 + arch/x86/entry/vdso/Makefile | 3 + arch/x86/include/asm/checksum.h | 10 +- arch/x86/include/asm/irq_regs.h | 1 + arch/x86/include/asm/kmsan.h | 117 +++++ arch/x86/include/asm/page_64.h | 13 + arch/x86/include/asm/page_64_types.h | 12 +- arch/x86/include/asm/pgtable_64_types.h | 15 + arch/x86/include/asm/string_64.h | 9 +- arch/x86/include/asm/syscall_wrapper.h | 1 + arch/x86/include/asm/uaccess.h | 12 + arch/x86/include/asm/unwind.h | 9 +- arch/x86/kernel/Makefile | 4 + arch/x86/kernel/apic/apic.c | 1 + arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/dumpstack_64.c | 1 + arch/x86/kernel/process_64.c | 5 + arch/x86/kernel/traps.c | 12 +- arch/x86/kernel/uprobes.c | 7 +- arch/x86/lib/Makefile | 2 + arch/x86/mm/Makefile | 2 + arch/x86/mm/fault.c | 20 + arch/x86/mm/ioremap.c | 3 + arch/x86/realmode/rm/Makefile | 2 + block/blk.h | 7 + crypto/Kconfig | 52 +++ drivers/char/random.c | 6 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/usb/core/urb.c | 2 + drivers/virtio/virtio_ring.c | 10 +- fs/ext4/readpage.c | 11 + include/asm-generic/cacheflush.h | 7 +- include/asm-generic/uaccess.h | 12 +- include/linux/compiler-clang.h | 8 + include/linux/compiler-gcc.h | 5 + include/linux/compiler.h | 13 +- include/linux/gfp.h | 4 +- include/linux/highmem.h | 4 + include/linux/kmsan-checks.h | 122 +++++ include/linux/kmsan.h | 143 ++++++ include/linux/mm_types.h | 9 + include/linux/sched.h | 5 + include/linux/stackdepot.h | 10 + include/linux/string.h | 2 + include/linux/uaccess.h | 32 +- init/main.c | 3 + kernel/Makefile | 1 + kernel/dma/direct.c | 1 + kernel/exit.c | 2 + kernel/fork.c | 2 + kernel/kthread.c | 2 + kernel/printk/printk.c | 6 + kernel/profile.c | 1 + kernel/sched/core.c | 6 + kernel/softirq.c | 5 + lib/Kconfig.debug | 5 + lib/Kconfig.kmsan | 22 + lib/Makefile | 6 + lib/iomap.c | 40 ++ lib/ioremap.c | 5 + lib/iov_iter.c | 6 + lib/stackdepot.c | 69 ++- lib/string.c | 5 +- lib/test_kmsan.c | 231 ++++++++++ lib/usercopy.c | 6 +- mm/Makefile | 1 + mm/compaction.c | 9 + mm/gup.c | 3 + mm/kasan/common.c | 23 - mm/kmsan/Makefile | 4 + mm/kmsan/kmsan.c | 563 ++++++++++++++++++++++++ mm/kmsan/kmsan.h | 146 ++++++ mm/kmsan/kmsan_entry.c | 118 +++++ mm/kmsan/kmsan_hooks.c | 422 ++++++++++++++++++ mm/kmsan/kmsan_init.c | 88 ++++ mm/kmsan/kmsan_instr.c | 259 +++++++++++ mm/kmsan/kmsan_report.c | 133 ++++++ mm/kmsan/kmsan_shadow.c | 543 +++++++++++++++++++++++ mm/kmsan/kmsan_shadow.h | 30 ++ mm/memory.c | 2 + mm/mmu_gather.c | 10 + mm/page_alloc.c | 16 + mm/slub.c | 34 +- mm/vmalloc.c | 23 +- net/sched/sch_generic.c | 2 + scripts/Makefile.kmsan | 12 + scripts/Makefile.lib | 6 + 96 files changed, 3983 insertions(+), 67 deletions(-) create mode 100644 Documentation/dev-tools/kmsan.rst create mode 100644 arch/x86/include/asm/kmsan.h create mode 100644 include/linux/kmsan-checks.h create mode 100644 include/linux/kmsan.h create mode 100644 lib/Kconfig.kmsan create mode 100644 lib/test_kmsan.c create mode 100644 mm/kmsan/Makefile create mode 100644 mm/kmsan/kmsan.c create mode 100644 mm/kmsan/kmsan.h
Quoted 8 lines
create mode 100644 mm/kmsan/kmsan_entry.c create mode 100644 mm/kmsan/kmsan_hooks.c create mode 100644 mm/kmsan/kmsan_init.c create mode 100644 mm/kmsan/kmsan_instr.c create mode 100644 mm/kmsan/kmsan_report.c create mode 100644 mm/kmsan/kmsan_shadow.c create mode 100644 mm/kmsan/kmsan_shadow.h create mode 100644 scripts/Makefile.kmsan
There are currently lots of UACCESS warnings -- should the core runtime functions be added to the whitelist in tools/objtool/check.c?
Thanks,
-- Marco
Alexander Potapenko
maintainerQuoted 252 lines · Marco Elver
Quoted 241 lines · On Fri, 22 Nov 2019 at 12:26, <[email protected]> wrote:
KernelMemorySanitizer (KMSAN) is a detector of errors related to uses of uninitialized memory. It relies on compile-time Clang instrumentation (similar to MSan in the userspace:
https://clang.llvm.org/docs/MemorySanitizer.html)
and tracks the state of every bit of kernel memory, being able to report an error if uninitialized value is used in a condition, dereferenced or copied to userspace, USB or network.KMSAN has reported more than 200 bugs in the past two years, most of them with the help of syzkaller (http://syzkaller.appspot.com).
The proposed patchset contains KMSAN runtime implementation together with small changes to other subsystems needed to make KMSAN work. The latter changes fall into several categories:
- nice-to-have features that are independent from KMSAN but simplify its implementation (stackdepot changes,CONFIG_GENERIC_CSUMetc.);
-Kconfigchanges that prohibit options incompatible with KMSAN;
- calls to KMSAN runtime functions that help KMSAN do the bookkeeping (e.g. tell it to allocate, copy or delete the metadata);
- calls to KMSAN runtime functions that tell KMSAN to check memory escaping the kernel for uninitialized values. These are required to increase the number of true positive error reports;
- calls to runtime functions that tell KMSAN to ignore certain memory ranges to avoid false negative reports. Most certainly there can be better ways to deal with every such report.This patchset allows one to boot and run a defconfig+KMSAN kernel on a QEMU without known major false positives. It however doesn't guarantee there are no false positives in drivers of certain devices or less tested subsystems, although KMSAN is actively tested on syzbot with quite a rich config.
One may find it handy to review these patches in Gerrit:
https://linux-review.googlesource.com/c/linux/kernel/git/torvalds/linux/+/1081
I've ensured the Change-Id: tags stay away from commit descriptions.The patchset was generated relative to Linux v5.4-rc5.
I also apologize for not sending every patch in the previous series to all recipients of patches from that series.
Note: checkpatch.pl complains a lot about the use of
BUG_ONin KMSAN source. I don't have a strong opinion on this, but KMSAN is a debugging tool, so any runtime invariant violation in it renders the tool useless. Therefore it doesn't make much sense to not terminate after a bug in KMSAN.Alexander Potapenko (36):
stackdepot: checkdepot_indexbefore accessing the stack slab
stackdepot: build with -fno-builtin
kasan: stackdepot: movefilter_irq_stacks()tostackdepot.c
stackdepot: reserve 5 extra bits indepot_stack_handle_t
kmsan: add ReST documentation
kmsan: gfp: introduce__GFP_NO_KMSAN_SHADOW
kmsan: introduce__no_sanitize_memoryand__SANITIZE_MEMORY__
kmsan: reduce vmalloc space
kmsan: add KMSAN bits to struct page and structtask_struct
kmsan: add KMSAN runtime
kmsan: stackdepot: don't allocate KMSAN metadata for stackdepot
kmsan: defineREAD_ONCE_NOCHECK()
kmsan: makeREAD_ONCE_TASK_STACK()return initialized values
kmsan: x86: sync metadata pages on page fault
kmsan: add tests for KMSAN
crypto: kmsan: disable accelerated configs under KMSAN
kmsan: x86: disableUNWINDER_ORCunder KMSAN
kmsan: disableLOCK_DEBUGGING_SUPPORT
kmsan: x86/asm: add KMSAN hooks toentry_64.S
kmsan: x86: increase stack sizes in KMSAN builds
kmsan: disable KMSAN instrumentation for certain kernel parts
kmsan: mm: call KMSAN hooks from SLUB code
kmsan: call KMSAN hooks where needed
kmsan: disable instrumentation of certain functions
kmsan: unpoison |tlb| inarch_tlb_gather_mmu()
kmsan: use__msan_memcpy()where possible.
kmsan: hooks forcopy_to_user()and friends
kmsan: enable KMSAN builds
kmsan: handle /dev/[u]random
kmsan: virtio: check/unpoison scatterlist invring_map_one_sg()
kmsan: disablestrscpy()optimization under KMSAN
kmsan: add iomap support
kmsan: dma: unpoison memory mapped bydma_direct_map_page()
kmsan: disable physical page merging in biovec
kmsan: ext4: skip block merging logic inext4_mpage_readpagesfor
KMSAN
net: kasan: kmsan: supportCONFIG_GENERIC_CSUMon x86, enable it for
KASAN/KMSANTo: Alexander Potapenko <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Andreas Dilger <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Darrick J. Wong <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Dmitry Torokhov <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Eric Biggers <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Eric Van Hensbergen <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Harry Wentland <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Ilya Leoshkevich <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Marek Szyprowski <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Martin K. Petersen <[email protected]>
Cc: Martin Schwidefsky <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Qian Cai <[email protected]>
Cc: Randy Dunlap <[email protected]>
Cc: Robin Murphy <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Takashi Iwai <[email protected]>
Cc: "Theodore Ts'o" <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Vasily Gorbik <[email protected]>
Cc: Vegard Nossum <[email protected]>
Cc: Wolfram Sang <[email protected]>
Cc: [email protected]Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kmsan.rst | 418 ++++++++++++++++++ Makefile | 3 +- arch/x86/Kconfig | 5 + arch/x86/Kconfig.debug | 3 + arch/x86/boot/Makefile | 2 + arch/x86/boot/compressed/Makefile | 2 + arch/x86/boot/compressed/misc.h | 1 + arch/x86/entry/common.c | 1 + arch/x86/entry/entry_64.S | 16 + arch/x86/entry/vdso/Makefile | 3 + arch/x86/include/asm/checksum.h | 10 +- arch/x86/include/asm/irq_regs.h | 1 + arch/x86/include/asm/kmsan.h | 117 +++++ arch/x86/include/asm/page_64.h | 13 + arch/x86/include/asm/page_64_types.h | 12 +- arch/x86/include/asm/pgtable_64_types.h | 15 + arch/x86/include/asm/string_64.h | 9 +- arch/x86/include/asm/syscall_wrapper.h | 1 + arch/x86/include/asm/uaccess.h | 12 + arch/x86/include/asm/unwind.h | 9 +- arch/x86/kernel/Makefile | 4 + arch/x86/kernel/apic/apic.c | 1 + arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/dumpstack_64.c | 1 + arch/x86/kernel/process_64.c | 5 + arch/x86/kernel/traps.c | 12 +- arch/x86/kernel/uprobes.c | 7 +- arch/x86/lib/Makefile | 2 + arch/x86/mm/Makefile | 2 + arch/x86/mm/fault.c | 20 + arch/x86/mm/ioremap.c | 3 + arch/x86/realmode/rm/Makefile | 2 + block/blk.h | 7 + crypto/Kconfig | 52 +++ drivers/char/random.c | 6 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/usb/core/urb.c | 2 + drivers/virtio/virtio_ring.c | 10 +- fs/ext4/readpage.c | 11 + include/asm-generic/cacheflush.h | 7 +- include/asm-generic/uaccess.h | 12 +- include/linux/compiler-clang.h | 8 + include/linux/compiler-gcc.h | 5 + include/linux/compiler.h | 13 +- include/linux/gfp.h | 4 +- include/linux/highmem.h | 4 + include/linux/kmsan-checks.h | 122 +++++ include/linux/kmsan.h | 143 ++++++ include/linux/mm_types.h | 9 + include/linux/sched.h | 5 + include/linux/stackdepot.h | 10 + include/linux/string.h | 2 + include/linux/uaccess.h | 32 +- init/main.c | 3 + kernel/Makefile | 1 + kernel/dma/direct.c | 1 + kernel/exit.c | 2 + kernel/fork.c | 2 + kernel/kthread.c | 2 + kernel/printk/printk.c | 6 + kernel/profile.c | 1 + kernel/sched/core.c | 6 + kernel/softirq.c | 5 + lib/Kconfig.debug | 5 + lib/Kconfig.kmsan | 22 + lib/Makefile | 6 + lib/iomap.c | 40 ++ lib/ioremap.c | 5 + lib/iov_iter.c | 6 + lib/stackdepot.c | 69 ++- lib/string.c | 5 +- lib/test_kmsan.c | 231 ++++++++++ lib/usercopy.c | 6 +- mm/Makefile | 1 + mm/compaction.c | 9 + mm/gup.c | 3 + mm/kasan/common.c | 23 - mm/kmsan/Makefile | 4 + mm/kmsan/kmsan.c | 563 ++++++++++++++++++++++++ mm/kmsan/kmsan.h | 146 ++++++ mm/kmsan/kmsan_entry.c | 118 +++++ mm/kmsan/kmsan_hooks.c | 422 ++++++++++++++++++ mm/kmsan/kmsan_init.c | 88 ++++ mm/kmsan/kmsan_instr.c | 259 +++++++++++ mm/kmsan/kmsan_report.c | 133 ++++++ mm/kmsan/kmsan_shadow.c | 543 +++++++++++++++++++++++ mm/kmsan/kmsan_shadow.h | 30 ++ mm/memory.c | 2 + mm/mmu_gather.c | 10 + mm/page_alloc.c | 16 + mm/slub.c | 34 +- mm/vmalloc.c | 23 +- net/sched/sch_generic.c | 2 + scripts/Makefile.kmsan | 12 + scripts/Makefile.lib | 6 + 96 files changed, 3983 insertions(+), 67 deletions(-) create mode 100644 Documentation/dev-tools/kmsan.rst create mode 100644 arch/x86/include/asm/kmsan.h create mode 100644 include/linux/kmsan-checks.h create mode 100644 include/linux/kmsan.h create mode 100644 lib/Kconfig.kmsan create mode 100644 lib/test_kmsan.c create mode 100644 mm/kmsan/Makefile create mode 100644 mm/kmsan/kmsan.c create mode 100644 mm/kmsan/kmsan.hQuoted 8 lines
create mode 100644 mm/kmsan/kmsan_entry.c create mode 100644 mm/kmsan/kmsan_hooks.c create mode 100644 mm/kmsan/kmsan_init.c create mode 100644 mm/kmsan/kmsan_instr.c create mode 100644 mm/kmsan/kmsan_report.c create mode 100644 mm/kmsan/kmsan_shadow.c create mode 100644 mm/kmsan/kmsan_shadow.h create mode 100644 scripts/Makefile.kmsan
Quoted 2 lines
There are currently lots of UACCESS warnings -- should the core runtime functions be added to the whitelist in
tools/objtool/check.c?
I've tried to do this properly, but failed.
What I consider to be an intuitive solution is to add KMSAN interface functions (e.g. __msan_metadata_ptr_for_load_X) to the whitelist in tools/objtool/check.c.
None of these functions is expected to touch userspace memory, but they can be called in the uaccess context, as the compiler adds them to every memory access.
Turns out however it's not enough to whitelist those functions, as they are viral: after whitelisting them I get warnings about their callees.
The list of functions called by e.g. kmsan_get_shadow_origin_ptr() include __warn_printk() and other non-KMSAN stuff, so I have to call user_access_save()/user_access_restore() to keep those warnings from spreading.
But this slows the whole runtime down by ~50% for no reason.
Quoted 2 lines
Thanks,
-- Marco
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg