Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755544AbbBPOoQ (ORCPT ); Mon, 16 Feb 2015 09:44:16 -0500 Received: from mailout2.w1.samsung.com ([210.118.77.12]:60553 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752636AbbBPOoO (ORCPT ); Mon, 16 Feb 2015 09:44:14 -0500 X-AuditID: cbfec7f4-b7f126d000001e9a-e5-54e201a9e3a6 Message-id: <54E20238.3090902@samsung.com> Date: Mon, 16 Feb 2015 17:44:08 +0300 From: Andrey Ryabinin User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-version: 1.0 To: Rusty Russell , linux-kernel@vger.kernel.org Cc: Dmitry Vyukov , Konstantin Serebryany , Dmitry Chernenkov , Andrey Konovalov , Yuri Gribov , Konstantin Khlebnikov , Sasha Levin , Christoph Lameter , Joonsoo Kim , Andrew Morton , Dave Hansen , Andi Kleen , x86@kernel.org, linux-mm@kvack.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Michal Marek , "open list:KERNEL BUILD + fi..." Subject: Re: [PATCH v11 19/19] kasan: enable instrumentation of global variables References: <1404905415-9046-1-git-send-email-a.ryabinin@samsung.com> <1422985392-28652-1-git-send-email-a.ryabinin@samsung.com> <1422985392-28652-20-git-send-email-a.ryabinin@samsung.com> <87a90ea7ge.fsf@rustcorp.com.au> In-reply-to: <87a90ea7ge.fsf@rustcorp.com.au> Content-type: text/plain; charset=utf-8 Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrOIsWRmVeSWpSXmKPExsVy+t/xy7orGR+FGHyco2Lxe+9MVos569ew WRy59p3d4vq3N4wWn14+YLR4/vAhu8WEh23sFtM2ilus7G5ms9j+7C2TxcrOB6wWf3btYLK4 vGsOm8W9Nf9ZLS4dWMBk0bLvApPFzWkXWCwWH7nNbPHu2WRmi82bpjJb/NjwmNVB1GP+zo+M Hjtn3WX3WLCp1GPxnpdMHptWdbJ5bPo0id2j6+0VJo93586xe5yY8ZvF48mV6UweH5/eYvF4 v+8qm8eKDSeYPc4sOMLu8XmTnMeJli+sAQJRXDYpqTmZZalF+nYJXBkX7v9hLPgrUdH2PrKB 8bRIFyMnh4SAicSZ3gmMELaYxIV769m6GLk4hASWMkpMfrGLGcJpZpLYcnkaK0gVr4CWxKzu X2wgNouAqsTTr+fBbDYBPYl/s7aD2aICERLzj71mhqgXlPgx+R4LiC0i4C5xu3EFWJxZ4BSr xLU2ZxBbWCBAouHQXKhljxkl9u68DNbAKaArcWxVN1CCA6hBXWLKlFyIXnmJzWveMk9gFJiF ZMUshKpZSKoWMDKvYhRNLU0uKE5KzzXUK07MLS7NS9dLzs/dxAiJ7C87GBcfszrEKMDBqMTD uyHsQYgQa2JZcWXuIUYJDmYlEd5l7x+GCPGmJFZWpRblxxeV5qQWH2Jk4uCUamCcGXRSNTsn 6KKSoV28mKsXa8pjd12+2D2PHR4dyc1c4n5zl4T9b/7JAhu2vGR2UPW/k+uZy6Au++tciWs1 D4OmT6D8p7tzT3H36TfdOzQrt3vOH+kv7zLL6nQLvq/yjIkK2HprwccbJx62h0dKSpncDNv+ YbNjqFAI/7vO5W/YHrV+vG4mUa7EUpyRaKjFXFScCAAgsM4nygIAAA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3366 Lines: 81 On 02/16/2015 05:58 AM, Rusty Russell wrote: > Andrey Ryabinin writes: >> This feature let us to detect accesses out of bounds of >> global variables. This will work as for globals in kernel >> image, so for globals in modules. Currently this won't work >> for symbols in user-specified sections (e.g. __init, __read_mostly, ...) >> >> The idea of this is simple. Compiler increases each global variable >> by redzone size and add constructors invoking __asan_register_globals() >> function. Information about global variable (address, size, >> size with redzone ...) passed to __asan_register_globals() so we could >> poison variable's redzone. >> >> This patch also forces module_alloc() to return 8*PAGE_SIZE aligned >> address making shadow memory handling ( kasan_module_alloc()/kasan_module_free() ) >> more simple. Such alignment guarantees that each shadow page backing >> modules address space correspond to only one module_alloc() allocation. > > Hmm, I understand why you only fixed x86, but it's weird. > > I think MODULE_ALIGN belongs in linux/moduleloader.h, and every arch > should be fixed up to use it (though you could leave that for later). > > Might as well fix the default implementation at least. > >> @@ -49,8 +49,15 @@ void kasan_krealloc(const void *object, size_t new_size); >> void kasan_slab_alloc(struct kmem_cache *s, void *object); >> void kasan_slab_free(struct kmem_cache *s, void *object); >> >> +#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) >> + >> +int kasan_module_alloc(void *addr, size_t size); >> +void kasan_module_free(void *addr); >> + >> #else /* CONFIG_KASAN */ >> >> +#define MODULE_ALIGN 1 > > Hmm, that should be PAGE_SIZE (we assume that in several places). > >> @@ -1807,6 +1808,7 @@ static void unset_module_init_ro_nx(struct module *mod) { } >> void __weak module_memfree(void *module_region) >> { >> vfree(module_region); >> + kasan_module_free(module_region); >> } > > This looks racy (memory reuse?). Perhaps try other order? > You are right, it's racy. Concurrent kasan_module_alloc() could fail because kasan_module_free() wasn't called/finished yet, so whole module_alloc() will fail and module loading will fail. However, I just find out that this race is not the worst problem here. When vfree(addr) called in interrupt context, memory at addr will be reused for storing 'struct llist_node': void vfree(const void *addr) { ... if (unlikely(in_interrupt())) { struct vfree_deferred *p = this_cpu_ptr(&vfree_deferred); if (llist_add((struct llist_node *)addr, &p->list)) schedule_work(&p->wq); In this case we have to free shadow *after* freeing 'module_region', because 'module_region' is still used in llist_add() and in free_work() latter. free_work() (in mm/vmalloc.c) processes list in LIFO order, so to free shadow after freeing 'module_region' kasan_module_free(module_region); should be called before vfree(module_region); It will be racy still, but this is not so bad as potential crash that we have now. Honestly, I have no idea how to fix this race nicely. Any suggestions? -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/