Received: by 10.223.164.202 with SMTP id h10csp2730850wrb; Mon, 27 Nov 2017 23:51:54 -0800 (PST) X-Google-Smtp-Source: AGs4zMaSbciuMBdWRVOxFNMoBp6E8Ypx35cf9m/+poj+k04ionqpaliGXrRi9z2fOhgDCduEk/0r X-Received: by 10.99.45.4 with SMTP id t4mr39754317pgt.254.1511855514165; Mon, 27 Nov 2017 23:51:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511855514; cv=none; d=google.com; s=arc-20160816; b=Mh9PNsTi8pDUFL2NC3+dE7NhIaBrO6TpNv57mp8PU5nGX179VvWzjbsJjaDkZCEtm/ HahXINJQC9hG1LpJaLM2dBPNNWDEwBvm2nYm7Hk5gasuVtoCCMCbKIesB9JMhuWahT2c NAgT27X6H+ufJsw+44MpL2Pz01SIV2ZFPpJm3gwkrUAPHhyTtOaZzZi3lNy871HyqNZu FihY2k0ftd1Y/LUjb38vDaNulMka4WqunNSatyx8uVichHXi6PFOURbj3dSyxovHx8bR 6STemJMoOGOjD83zGW3T7tR8C/jrgzhd0cqtNPMOP1l0XyBAtyntDrfFjpR2Mts6RDaE xCZg== 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=4cTiqgV6aAsyAOw92AK9/DHnSE0FSR53AlvVq2qXFV8=; b=WSQ1qy3kSrx1EGaGVlLrPYBJgX1zGHRmN2sc3vlzE7rtuWX1gib5PCgKeNA8sVjozP 2odHblczzgbUQ4nd1rFW8l76KRvBImyt+4G0BqXMeDDJ9ngDPXn6JvbX7DCLNtshOUK4 9eYBDD5QCVit+DUBMdWy0l/RkGjgz+ZUNx9DIDjre8b0Dng5ZcsbaHYwS5p0IEEHQ1tD 4ZiD2MUib/CLnfigvY5v4alslCZssrzlIbgkXOJkTSEzMb1bh0SKN9AjXdbaUvuLdFgU olUV6HRFoob7tjphi++DtYaHmQEGODGp8pfRjY23SuWA5x70zfu1nFXwhxE48uwj0QcQ 0l3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T4MWFtXT; 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 s28si17548558pgo.597.2017.11.27.23.51.42; Mon, 27 Nov 2017 23:51:54 -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=T4MWFtXT; 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 S1752207AbdK1Htk (ORCPT + 78 others); Tue, 28 Nov 2017 02:49:40 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:39524 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752127AbdK1Hth (ORCPT ); Tue, 28 Nov 2017 02:49:37 -0500 Received: by mail-pf0-f195.google.com with SMTP id l24so18610285pfj.6 for ; Mon, 27 Nov 2017 23:49:37 -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=4cTiqgV6aAsyAOw92AK9/DHnSE0FSR53AlvVq2qXFV8=; b=T4MWFtXTFP5mIntDGClztfO3DEh3m2ol6ujrRZNp09fZ2pzLvfLnOcG5313GcV4LMD 6Jghn+CPRqa1F7BLCU//oxuqtsfV9OKlomgSu76zgHE9HkOv3mUTrm3GXZyKuSL+UNDD nX7E8c6vho7iuc41ia9hTtwEJzqUjFd1j3D5OEWVsYzOVKWRAItiznTPjSgjJ/CdIbRb k5xma12l6tIXFxp0zLuIKij8oYK+Z7CSG60M6z/CQO7O8wp6noLqgTF6ACj7anX0Kx6W rQzY4+XOWbPX8uL3rwE9F7JcIAU5AdVT4qxQ7EUG8R/wR+ShZPUe5t7nKYm1UFcmVVPi xKpQ== 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=4cTiqgV6aAsyAOw92AK9/DHnSE0FSR53AlvVq2qXFV8=; b=LtGk1giavK8geZzKgS4ou5qE7chNU9V2OM0o6YHvJaDqHrHZOXlbVPxRoSJZ4h+TGq O/xgHZ0OAlPSrRLExA8hbTXQzbNEjqaByIB8UH3tiQqPtAjT/hF0Ng7e3jO+8BkTQPKW pgqNuwRwaIWMZGRFn2tKrnm12I7jUv+wpLK684mh5dvVxwSaN9xgtjYSBwORZOQ0b3wy NjOE9vj8CVesCgoOvgk8d1y1QcF6k5C2RFIRalfb0cqkO7gbsjEjvpOiJvgKBHqlRAf9 XvlmYwrV7d53EbNHCFr1cUoCgmOhID2z0UnCLJeas9pp12OCHljfNTpymSpHhv88ho3N eOtA== X-Gm-Message-State: AJaThX4c2aCwqaRif667Rhl5NfokUa8dO/dy7ronwKV+8Xvm1gHzFe5u 56MG8cf2qI/NLWN+BnDiUgQ= X-Received: by 10.101.78.18 with SMTP id r18mr39576853pgt.289.1511855376611; Mon, 27 Nov 2017 23:49:36 -0800 (PST) Received: from localhost.localdomain ([124.56.155.17]) by smtp.gmail.com with ESMTPSA id 67sm39403946pfz.171.2017.11.27.23.49.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 27 Nov 2017 23:49:35 -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 05/18] vchecker: store/report callstack of value writer Date: Tue, 28 Nov 2017 16:48:40 +0900 Message-Id: <1511855333-3570-6-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 The purpose of the value checker is finding invalid user writing invalid value at the moment that the value is written. However, there is not enough infrastructure so that we cannot easily detect this case in time. However, by following way, we can emulate similar effect. 1. Store callstack when memory is written. 2. If check is failed when next access happen due to invalid written value from previous write, report previous write-access callstack It will caught offending user properly. Following output "Invalid writer:" part is the result of this patch. We find the invalid value writer at workfn_old_obj+0x14/0x50. [ 49.400673] ================================================================== [ 49.402297] BUG: VCHECKER: invalid access in workfn_old_obj+0x14/0x50 [vchecker_test] at addr ffff88002e9dc000 [ 49.403899] Write of size 8 by task kworker/0:2/465 [ 49.404538] value checker for offset 0 ~ 8 at ffff88002e9dc000 [ 49.405374] (mask 0xffff value 7) invalid value 7 [ 49.406016] Invalid writer: [ 49.406302] workfn_old_obj+0x14/0x50 [vchecker_test] [ 49.406973] process_one_work+0x3b5/0x9f0 [ 49.407463] worker_thread+0x87/0x750 [ 49.407895] kthread+0x1b2/0x200 [ 49.408252] ret_from_fork+0x24/0x30 [ 49.408723] Allocated by task 1326: [ 49.409126] kasan_kmalloc+0xb9/0xe0 [ 49.409571] kmem_cache_alloc+0xd1/0x250 [ 49.410046] 0xffffffffa00c8157 [ 49.410389] do_one_initcall+0x82/0x1cf [ 49.410851] do_init_module+0xe7/0x333 [ 49.411296] load_module+0x406b/0x4b40 [ 49.411745] SYSC_finit_module+0x14d/0x180 [ 49.412247] do_syscall_64+0xf0/0x340 [ 49.412674] return_from_SYSCALL_64+0x0/0x75 [ 49.413276] Freed by task 0: [ 49.413566] (stack is not available) [ 49.414034] The buggy address belongs to the object at ffff88002e9dc000 which belongs to the cache vchecker_test of size 8 [ 49.415708] The buggy address is located 0 bytes inside of 8-byte region [ffff88002e9dc000, ffff88002e9dc008) [ 49.417148] ================================================================== Correct implementation needs more modifications to various layers so it is postponed until feasibility is proved. Signed-off-by: Joonsoo Kim --- mm/kasan/vchecker.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/mm/kasan/vchecker.c b/mm/kasan/vchecker.c index be0f0cd..2e9f461 100644 --- a/mm/kasan/vchecker.c +++ b/mm/kasan/vchecker.c @@ -16,11 +16,14 @@ #include #include #include +#include #include "vchecker.h" #include "../slab.h" #include "kasan.h" +#define VCHECKER_STACK_DEPTH (16) + struct vchecker { bool enabled; struct list_head cb_list; @@ -32,7 +35,7 @@ enum vchecker_type_num { }; struct vchecker_data { - void *dummy; + depot_stack_handle_t write_handle; }; struct vchecker_type { @@ -281,6 +284,24 @@ bool vchecker_check(unsigned long addr, size_t size, return vchecker_poisoned((void *)addr, size); } +static noinline depot_stack_handle_t save_stack(void) +{ + unsigned long entries[VCHECKER_STACK_DEPTH]; + struct stack_trace trace = { + .nr_entries = 0, + .entries = entries, + .max_entries = VCHECKER_STACK_DEPTH, + .skip = 0 + }; + + save_stack_trace(&trace); + if (trace.nr_entries != 0 && + trace.entries[trace.nr_entries-1] == ULONG_MAX) + trace.nr_entries--; + + return depot_save_stack(&trace, GFP_NOWAIT); +} + static ssize_t vchecker_type_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos, enum vchecker_type_num type) @@ -474,17 +495,35 @@ static void fini_value(struct vchecker_cb *cb) kfree(cb->arg); } +static void show_value_stack(struct vchecker_data *data) +{ + struct stack_trace trace; + + if (!data->write_handle) + return; + + pr_err("Invalid writer:\n"); + depot_fetch_stack(data->write_handle, &trace); + print_stack_trace(&trace, 0); + pr_err("\n"); +} + static void show_value(struct kmem_cache *s, struct seq_file *f, struct vchecker_cb *cb, void *object) { struct vchecker_value_arg *arg = cb->arg; + struct vchecker_data *data; if (f) seq_printf(f, "(mask 0x%llx value %llu) invalid value %llu\n\n", arg->mask, arg->value, arg->value & arg->mask); - else + else { + data = (void *)object + s->vchecker_cache.data_offset; + pr_err("(mask 0x%llx value %llu) invalid value %llu\n\n", arg->mask, arg->value, arg->value & arg->mask); + show_value_stack(data); + } } static bool check_value(struct kmem_cache *s, struct vchecker_cb *cb, @@ -492,14 +531,29 @@ static bool check_value(struct kmem_cache *s, struct vchecker_cb *cb, unsigned long begin, unsigned long end) { struct vchecker_value_arg *arg; + struct vchecker_data *data; u64 value; + depot_stack_handle_t handle; + + if (!write) + goto check; + + handle = save_stack(); + if (!handle) { + pr_err("VCHECKER: %s: fail at addr %p\n", __func__, object); + dump_stack(); + } + data = (void *)object + s->vchecker_cache.data_offset; + data->write_handle = handle; + +check: arg = cb->arg; value = *(u64 *)(object + begin); - if ((value & arg->mask) != (arg->value & arg->mask)) - return true; + if ((value & arg->mask) == (arg->value & arg->mask)) + return false; - return false; + return true; } static int value_show(struct seq_file *f, void *v) -- 2.7.4 From 1585295364970989155@xxx Tue Nov 28 07:51:13 +0000 2017 X-GM-THRID: 1585295364970989155 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread