From patchwork Fri Oct 18 09:42:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 11198001 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C075113B1 for ; Fri, 18 Oct 2019 09:44:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 83D2020820 for ; Fri, 18 Oct 2019 09:44:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="J2WCHWzm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 83D2020820 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6F5DE8E002D; Fri, 18 Oct 2019 05:44:08 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6A81E8E0003; Fri, 18 Oct 2019 05:44:08 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 594FF8E002D; Fri, 18 Oct 2019 05:44:08 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0109.hostedemail.com [216.40.44.109]) by kanga.kvack.org (Postfix) with ESMTP id 30F918E0003 for ; Fri, 18 Oct 2019 05:44:08 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id C6FAEEFB8 for ; Fri, 18 Oct 2019 09:44:07 +0000 (UTC) X-FDA: 76056419334.14.deer47_7b4e08c9c2e27 X-Spam-Summary: 2,0,0,b0e4dfe231ede290,d41d8cd98f00b204,3zompxqykcbmz41wxaz77z4x.v75416dg-553etv3.7az@flex--glider.bounces.google.com,:glider@google.com:akpm@linux-foundation.org:vegard.nossum@oracle.com:dvyukov@google.com:,RULES_HIT:2:41:152:355:379:541:800:960:966:968:973:988:989:1260:1277:1313:1314:1345:1359:1431:1437:1516:1518:1535:1593:1594:1605:1606:1730:1747:1777:1792:2194:2196:2198:2199:2200:2201:2393:2553:2559:2562:2693:2731:3138:3139:3140:3141:3142:3152:3865:3866:3867:3868:3870:3871:3872:3874:4119:4250:4321:4385:5007:6119:6120:6261:6653:7875:7901:7903:7904:8603:8660:9592:9969:10004:11026:11232:11473:11657:11658:11914:12043:12291:12296:12297:12438:12555:12683:12895:12986:13148:13230:13846:14096:14097:14394:14659:21080:21212:21365:21444:21451:21611:21627:21740:21966:30012:30029:30054:30055:30062:30064:30070:30090,0,RBL:209.85.128.73:@flex--glider.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCac he:0,MSF X-HE-Tag: deer47_7b4e08c9c2e27 X-Filterd-Recvd-Size: 8547 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Fri, 18 Oct 2019 09:44:07 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id l184so928341wmf.6 for ; Fri, 18 Oct 2019 02:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Vasm+6WQlcurV54BG5ONxEH4GDxu9i5ZUwZTO8dWfq0=; b=J2WCHWzmVVMQLS1RcLEL8bAYf5B9KJpTW8sfYE1j5zUZemeeL5Tdm+EaahEvtOwEMc KItJKPXYpdKzbvzciZMKALP8xKSpCqtNdAoLUs9bpz8nUWldvT3pqof74Zx9QERre7Zu kTSTVkEF5wOoXq+E5Qr9Lgg2jZqkVdu7Iw2vLw9rqrnq2dSFZ0okhw/4GIzENCc3xFW/ 0KgwM0s1tM0/lWIJWp5bTqJoNDZYDUjYl12HM2Qur5ETBYMcJhFGz6MdaK/qfL4jTSnl Yb8OFGo6JWBWGFEheewlokGAnMp1nTxMtBzB+OgNIzzhlr7BgPFfHIo51wVA/X0qUqt+ 432g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Vasm+6WQlcurV54BG5ONxEH4GDxu9i5ZUwZTO8dWfq0=; b=tUM/B5EuI68eKFMYYWgrJj02sneqL1TFFEALOD3oggxVkcRIXS8EYlaFGd4nuvHsN4 cpnssjbXWVsooubOM8IKYxe3jh88XqxysuW2zeX4kT+5ewzoWHuJ4yaISYfzUoSXxlzN CAcTcfhqGi4MgK+VyuZOt86qUIMW4yc7LMxVaM3qyNuYshaQ879FOHRom9kFE5SIiCm4 bWHdjkGAnheSfogLcjTZVClhjuboASOmVQMVCQc7c3V9CVc9ovzBGSKTZFzN83ggrtZx x8ost782BN3TJlgGKzd//7pmIj8l3FueAdAV7KbdAKxYEpJ6L/vHbGjKLMtr4CbL2Wjb joQQ== X-Gm-Message-State: APjAAAXQQv6DmPEBMgQOFz8DCC05oGKvqH3V+aTVRVxvBz1irX89q6lU f4FkiVYK2j5QdUDs5JlhPRY/X4bFke0= X-Google-Smtp-Source: APXvYqwNIBr0Zabn63fCWF6pyd1cMBhmB6hr9DUzshyIFRNrwpKErjkjvjFV6sY6IN3G8ZAKBlwp1QTRBAo= X-Received: by 2002:adf:ef0f:: with SMTP id e15mr7291869wro.312.1571391846092; Fri, 18 Oct 2019 02:44:06 -0700 (PDT) Date: Fri, 18 Oct 2019 11:42:56 +0200 In-Reply-To: <20191018094304.37056-1-glider@google.com> Message-Id: <20191018094304.37056-19-glider@google.com> Mime-Version: 1.0 References: <20191018094304.37056-1-glider@google.com> X-Mailer: git-send-email 2.23.0.866.gb869b98d4c-goog Subject: [PATCH RFC v1 18/26] kmsan: mm: call KMSAN hooks from SLUB code From: glider@google.com To: Alexander Potapenko Cc: Andrew Morton , Vegard Nossum , Dmitry Vyukov , linux-mm@kvack.org X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In order to report uninitialized memory coming from heap allocations KMSAN has to poison them unless they're created with __GFP_ZERO. It's handy that we need KMSAN hooks in the places where init_on_alloc/init_on_free initialization is performed. Signed-off-by: Alexander Potapenko To: Alexander Potapenko Cc: Andrew Morton Cc: Vegard Nossum Cc: Dmitry Vyukov Cc: linux-mm@kvack.org --- Change-Id: I51103b7981d3aabed747d0c85cbdc85568665871 --- mm/slub.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 3d63ae320d31..3d6d4c63446e 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include /* KMSAN_INIT_VALUE */ #include #include #include @@ -285,17 +287,27 @@ static void prefetch_freepointer(const struct kmem_cache *s, void *object) prefetch(object + s->offset); } +/* + * When running under KMSAN, get_freepointer_safe() may return an uninitialized + * pointer value in the case the current thread loses the race for the next + * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in + * slab_alloc_node() will fail, so the uninitialized value won't be used, but + * KMSAN will still check all arguments of cmpxchg because of imperfect + * handling of inline assembly. + * To work around this problem, use KMSAN_INIT_VALUE() to force initialize the + * return value of get_freepointer_safe(). + */ static inline void *get_freepointer_safe(struct kmem_cache *s, void *object) { unsigned long freepointer_addr; void *p; if (!debug_pagealloc_enabled()) - return get_freepointer(s, object); + return KMSAN_INIT_VALUE(get_freepointer(s, object)); freepointer_addr = (unsigned long)object + s->offset; probe_kernel_read(&p, (void **)freepointer_addr, sizeof(p)); - return freelist_ptr(s, p, freepointer_addr); + return KMSAN_INIT_VALUE(freelist_ptr(s, p, freepointer_addr)); } static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) @@ -1390,6 +1402,7 @@ static inline void *kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags) ptr = kasan_kmalloc_large(ptr, size, flags); /* As ptr might get tagged, call kmemleak hook after KASAN. */ kmemleak_alloc(ptr, size, 1, flags); + kmsan_kmalloc_large(ptr, size, flags); return ptr; } @@ -1397,6 +1410,7 @@ static __always_inline void kfree_hook(void *x) { kmemleak_free(x); kasan_kfree_large(x, _RET_IP_); + kmsan_kfree_large(x); } static __always_inline bool slab_free_hook(struct kmem_cache *s, void *x) @@ -1453,6 +1467,12 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s, } while (object != old_tail); } + do { + object = next; + next = get_freepointer(s, object); + kmsan_slab_free(s, object); + } while (object != old_tail); + /* * Compiler cannot detect this function can be removed if slab_free_hook() * evaluates to nothing. Thus, catch all relevant config debug options here. @@ -2769,6 +2789,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object) memset(object, 0, s->object_size); + kmsan_slab_alloc(s, object, gfpflags); slab_post_alloc_hook(s, gfpflags, 1, &object); return object; @@ -2797,6 +2818,7 @@ void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) void *ret = slab_alloc(s, gfpflags, _RET_IP_); trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags); ret = kasan_kmalloc(s, ret, size, gfpflags); + return ret; } EXPORT_SYMBOL(kmem_cache_alloc_trace); @@ -2809,7 +2831,6 @@ void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node) trace_kmem_cache_alloc_node(_RET_IP_, ret, s->object_size, s->size, gfpflags, node); - return ret; } EXPORT_SYMBOL(kmem_cache_alloc_node); @@ -2825,6 +2846,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *s, size, s->size, gfpflags, node); ret = kasan_kmalloc(s, ret, size, gfpflags); + return ret; } EXPORT_SYMBOL(kmem_cache_alloc_node_trace); @@ -3150,7 +3172,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, void **p) { struct kmem_cache_cpu *c; - int i; + int i, j; /* memcg and kmem_cache debug support */ s = slab_pre_alloc_hook(s, flags); @@ -3188,11 +3210,11 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, /* Clear memory outside IRQ disabled fastpath loop */ if (unlikely(slab_want_init_on_alloc(flags, s))) { - int j; - for (j = 0; j < i; j++) memset(p[j], 0, s->object_size); } + for (j = 0; j < i; j++) + kmsan_slab_alloc(s, p[j], flags); /* memcg and kmem_cache debug support */ slab_post_alloc_hook(s, flags, size, p); @@ -3793,6 +3815,7 @@ static int __init setup_slub_min_objects(char *str) __setup("slub_min_objects=", setup_slub_min_objects); +__no_sanitize_memory void *__kmalloc(size_t size, gfp_t flags) { struct kmem_cache *s; @@ -5698,6 +5721,7 @@ static char *create_unique_id(struct kmem_cache *s) p += sprintf(p, "%07u", s->size); BUG_ON(p > name + ID_STR_LENGTH - 1); + kmsan_unpoison_shadow(name, p - name); return name; } @@ -5847,6 +5871,7 @@ static int sysfs_slab_alias(struct kmem_cache *s, const char *name) al->name = name; al->next = alias_list; alias_list = al; + kmsan_unpoison_shadow(al, sizeof(struct saved_alias)); return 0; }