Received: by 10.223.164.202 with SMTP id h10csp2730451wrb; Mon, 27 Nov 2017 23:51:20 -0800 (PST) X-Google-Smtp-Source: AGs4zMa07eDJJdVxU/aiBmgTX07v5iLygtdRc4+/SRbtmpizxlc8AKiz57kmTvs26eAf7mik26dH X-Received: by 10.159.218.76 with SMTP id x12mr37804201plv.285.1511855480002; Mon, 27 Nov 2017 23:51:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511855479; cv=none; d=google.com; s=arc-20160816; b=ALk40kkAM4j8HQCf4LRJkbAuP7bwUjk6T7hgu3uTcFN+RYLOTRpieorevVvqMd+G3R HyqSHGapWWEIqZZRPnN4L0TQGQOI/ZlhZ9TrAFivj7DxRDnYF/nn04ucICV/UA6D8BUZ aqsI77lzYli/exggP+WqITe21MwdwF2gu4HNCQuLw8V1ZtyLX4vNPcidUz+Gn1B1hiEj d+PXv0E/jgSN7MEnOmnErR6PcLPWL3bNPbLpHLfJHv8H4GZAL3LCkMrUzJ0i2zyUptdd 48N9gpynXPS1UVU9hqz+dDaxUHI9maNTQGeSWA4jkTsnBzd7WxAVpusWiFH2y98t8Hlf Ydlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=QOu58vluoWHuI5fh/HCr0+JXSxI1Io5YQrmOhGlLtFs=; b=sJeEwa15SSH07u5hia/tTwmmpH/OB7IURVtg8sB79zNsSwcPFDd/KO65DjSmQCfAom rEjgshvxWEkIrG9inzuIGh0mGR3xIpM4Ejaf1nAwUWEVnNc0+UHkUZafBLIua86ApSR+ Q6Lw00Y34vOD1nmeP7QcJlVzYOT79OaI/7Wtlu1/Je+pBo3QXXES+Oyjg9wbrTqr5Nig JrERGyVB11HuOBfVr5mcIX1CR+7WIpkmlAOTjU5uJcZ78GbcuSvji/5ayh1Xu6WwGeRZ 9/fO+B4QYHiZHE/VjGb2cylR+wuNlSm5NpecCPL4s616aMOWqE6mPnucjsdf1VPCXmQs Xxzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gq9Ik6rf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i1si24463741plt.238.2017.11.27.23.51.08; Mon, 27 Nov 2017 23:51:19 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gq9Ik6rf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752465AbdK1HuU (ORCPT + 78 others); Tue, 28 Nov 2017 02:50:20 -0500 Received: from mail-pl0-f65.google.com ([209.85.160.65]:32774 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752433AbdK1HuO (ORCPT ); Tue, 28 Nov 2017 02:50:14 -0500 Received: by mail-pl0-f65.google.com with SMTP id a12so10185285pll.0 for ; Mon, 27 Nov 2017 23:50:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QOu58vluoWHuI5fh/HCr0+JXSxI1Io5YQrmOhGlLtFs=; b=gq9Ik6rfacaSwTw1DyuOtS7Ed6betwspI9Umm0K9y/GyFthKKQeVn2mNzGQmBEzxlq OvjZlFjmfKEIaf4z4cPTFG6JSOIFrnmyK80976CCzta03mbpvikt7IU9aCK1CJg70viO RDkcFC6B/51aSb9v5honStxPJ0TMQIWvFbTp3xbBJaZCw1C620WZYuZhzo0Mj2riS9gz sCP7hbYH92+Aqe/aFL/UMNFft5PTyG1M3gHmbUQzHz87ASlDRFLCi0Rpf5SwsXwHtzRu oACFXMiKx9mUDQ7v8d4h4U+Qq9eME/L46aQKhAzZIb6871x5J+90uEPv4x0zcA33JAwb Z1mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QOu58vluoWHuI5fh/HCr0+JXSxI1Io5YQrmOhGlLtFs=; b=dxO0qA2enVI+U7HfUs33/uxyyi+Il6kfp5MNnZ8/jJsdI75BRDMKfGpIQQs/OiLa/h N1jQOA8hhzuCoPMKEigM/VXPAnxhfruxNSUQrMWhSy/V3cN8V3gSF1+Ui7kGd4+cHL4P w3mGYl9wGNpDJi3Jo01DNkavLYblgpIT2ZtMJyKfrlMl1gtANJmkWfMllcCWDEqvFN4t XjvEV6zObfJa9EVYsmLiM0pn3BbJxGSh7whA4L67LrYprx6M3fJ/u1VItY2o8TJWjNO6 3jZK2sNERFYCJPBefHWZBHaKxF1/L66L7WYKDw/XiLwwtcT/nto4fMBYEXMxWHHMRrAC KKKw== X-Gm-Message-State: AJaThX750pV44ZGhistTEBXIFOSxJEaoNL+hOR//2jnVCMJWtusabr5Q esGcwK/LsMDmmL2TKVTRPnM= X-Received: by 10.84.231.2 with SMTP id f2mr40427362plk.256.1511855414053; Mon, 27 Nov 2017 23:50:14 -0800 (PST) Received: from localhost.localdomain ([124.56.155.17]) by smtp.gmail.com with ESMTPSA id 67sm39403946pfz.171.2017.11.27.23.50.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 27 Nov 2017 23:50:13 -0800 (PST) From: js1304@gmail.com X-Google-Original-From: iamjoonsoo.kim@lge.com To: Andrew Morton Cc: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Namhyung Kim , Wengang Wang , Joonsoo Kim Subject: [PATCH 16/18] mm/vchecker: support allocation caller filter Date: Tue, 28 Nov 2017 16:48:51 +0900 Message-Id: <1511855333-3570-17-git-send-email-iamjoonsoo.kim@lge.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1511855333-3570-1-git-send-email-iamjoonsoo.kim@lge.com> References: <1511855333-3570-1-git-send-email-iamjoonsoo.kim@lge.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joonsoo Kim kmalloc() is used everywhere in the kernel and it doesn't distiniguish the callers since it doesn't much help to efficiently manage the memory. However, there is a difference in the view of the debugging. A bug usually happens on the objects allocated by specific allocation caller. So, it is useful to distiniguish them. Let's call it as a same class object. This patch implements an allocation caller filter to distiniguish the class. With it, vchecker can be applied only to a specific class and debugging it could be possible. Note that it's not easy to distiniguish allocation caller of existing allocated memory. Therefore, existing allocated memory will not be included to debugging target if allocation caller filter is enabled. If it is really required, feel free to ask me. I have a rough idea to implement it. Signed-off-by: Joonsoo Kim --- lib/Kconfig.kasan | 1 + mm/kasan/vchecker.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index d3552f3..4b8e748 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -63,5 +63,6 @@ config VCHECKER happens at the area. depends on KASAN && DEBUG_FS + select KALLSYMS endif diff --git a/mm/kasan/vchecker.c b/mm/kasan/vchecker.c index 918f05a..9f2b164 100644 --- a/mm/kasan/vchecker.c +++ b/mm/kasan/vchecker.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "vchecker.h" #include "../slab.h" @@ -28,6 +29,7 @@ struct vchecker { bool enabled; unsigned int callstack_depth; + struct list_head alloc_filter_list; struct list_head cb_list; }; @@ -75,6 +77,12 @@ struct vchecker_callstack_arg { bool enabled; }; +struct vchecker_alloc_filter { + unsigned long begin; + unsigned long end; + struct list_head list; +}; + static struct dentry *debugfs_root; static struct vchecker_type vchecker_types[VCHECKER_TYPE_MAX]; static DEFINE_MUTEX(vchecker_meta); @@ -149,6 +157,7 @@ void vchecker_kmalloc(struct kmem_cache *s, const void *object, size_t size, { struct vchecker *checker; struct vchecker_cb *cb; + struct vchecker_alloc_filter *af; rcu_read_lock(); checker = s->vchecker_cache.checker; @@ -157,6 +166,18 @@ void vchecker_kmalloc(struct kmem_cache *s, const void *object, size_t size, return; } + if (list_empty(&checker->alloc_filter_list)) + goto mark; + + list_for_each_entry(af, &checker->alloc_filter_list, list) { + if (af->begin <= ret_ip && ret_ip < af->end) + goto mark; + } + + rcu_read_unlock(); + return; + +mark: list_for_each_entry(cb, &checker->cb_list, list) { kasan_poison_shadow(object + cb->begin, round_up(cb->end - cb->begin, @@ -476,9 +497,13 @@ static ssize_t enable_write(struct file *filp, const char __user *ubuf, /* * After this operation, it is guaranteed that there is no user * left that accesses checker's cb list if vchecker is disabled. + * Don't mark the object if alloc_filter is enabled. We cannot + * know the allocation caller at this moment. */ synchronize_sched(); - vchecker_enable_cache(s, enable); + if (!enable || + list_empty(&s->vchecker_cache.checker->alloc_filter_list)) + vchecker_enable_cache(s, enable); mutex_unlock(&vchecker_meta); return cnt; @@ -556,6 +581,99 @@ static const struct file_operations callstack_depth_fops = { .release = single_release, }; +static int alloc_filter_show(struct seq_file *f, void *v) +{ + char name[KSYM_NAME_LEN]; + struct kmem_cache *s = f->private; + struct vchecker *checker = s->vchecker_cache.checker; + struct vchecker_alloc_filter *af; + + mutex_lock(&vchecker_meta); + list_for_each_entry(af, &checker->alloc_filter_list, list) { + if (!lookup_symbol_name(af->begin, name)) + seq_printf(f, "%s: ", name); + seq_printf(f, "0x%lx - 0x%lx\n", af->begin, af->end); + } + mutex_unlock(&vchecker_meta); + + return 0; +} + +static int alloc_filter_open(struct inode *inode, struct file *file) +{ + return single_open(file, alloc_filter_show, inode->i_private); +} + +static void remove_alloc_filters(struct vchecker *checker) +{ + struct vchecker_alloc_filter *af, *tmp; + + list_for_each_entry_safe(af, tmp, &checker->alloc_filter_list, list) { + list_del(&af->list); + kfree(af); + } +} + +static ssize_t alloc_filter_write(struct file *filp, const char __user *ubuf, + size_t cnt, loff_t *ppos) +{ + char filter_chars[KSYM_NAME_LEN]; + struct kmem_cache *s = file_inode(filp)->i_private; + struct vchecker *checker = s->vchecker_cache.checker; + unsigned long begin; + unsigned long size; + struct vchecker_alloc_filter *af = NULL; + + if (cnt >= KSYM_NAME_LEN || cnt == 0) + return -EINVAL; + + if (copy_from_user(&filter_chars, ubuf, cnt)) + return -EFAULT; + + if (isspace(filter_chars[0])) + goto change; + + filter_chars[cnt - 1] = '\0'; + begin = kallsyms_lookup_name(filter_chars); + if (!begin) + return -EINVAL; + + kallsyms_lookup_size_offset(begin, &size, NULL); + + af = kzalloc(sizeof(*af), GFP_KERNEL); + if (!af) + return -ENOMEM; + + af->begin = begin; + af->end = begin + size; + +change: + mutex_lock(&vchecker_meta); + if (checker->enabled || !list_empty(&checker->cb_list)) { + mutex_unlock(&vchecker_meta); + kfree(af); + + return -EINVAL; + } + + if (af) + list_add_tail(&af->list, &checker->alloc_filter_list); + else + remove_alloc_filters(checker); + + mutex_unlock(&vchecker_meta); + + return cnt; +} + +static const struct file_operations alloc_filter_fops = { + .open = alloc_filter_open, + .write = alloc_filter_write, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + static int init_value(struct kmem_cache *s, struct vchecker_cb *cb, char *buf, size_t cnt) { @@ -865,6 +983,7 @@ static void free_vchecker(struct kmem_cache *s) if (!s->vchecker_cache.checker) return; + remove_alloc_filters(s->vchecker_cache.checker); for (i = 0; i < ARRAY_SIZE(vchecker_types); i++) remove_cbs(s, &vchecker_types[i]); kfree(s->vchecker_cache.checker); @@ -895,6 +1014,7 @@ static int alloc_vchecker(struct kmem_cache *s) return -ENOMEM; checker->callstack_depth = VCHECKER_STACK_DEPTH; + INIT_LIST_HEAD(&checker->alloc_filter_list); INIT_LIST_HEAD(&checker->cb_list); s->vchecker_cache.checker = checker; @@ -922,6 +1042,10 @@ static int register_debugfs(struct kmem_cache *s) &callstack_depth_fops)) return -ENOMEM; + if (!debugfs_create_file("alloc_filter", 0600, dir, s, + &alloc_filter_fops)) + return -ENOMEM; + for (i = 0; i < ARRAY_SIZE(vchecker_types); i++) { t = &vchecker_types[i]; if (!debugfs_create_file(t->name, 0600, dir, s, t->fops)) -- 2.7.4 From 1585293735199311531@xxx Tue Nov 28 07:25:19 +0000 2017 X-GM-THRID: 1585292609619656123 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread