From patchwork Wed Feb 13 13:58:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10809977 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0E8981575 for ; Wed, 13 Feb 2019 13:58:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F03EE2D048 for ; Wed, 13 Feb 2019 13:58:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E45342D0CB; Wed, 13 Feb 2019 13:58:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 550862D0CE for ; Wed, 13 Feb 2019 13:58:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09ACB8E0005; Wed, 13 Feb 2019 08:58:41 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0245A8E0004; Wed, 13 Feb 2019 08:58:40 -0500 (EST) 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 D96D78E0005; Wed, 13 Feb 2019 08:58:40 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by kanga.kvack.org (Postfix) with ESMTP id 7E6FC8E0001 for ; Wed, 13 Feb 2019 08:58:40 -0500 (EST) Received: by mail-wr1-f69.google.com with SMTP id e14so893972wrt.12 for ; Wed, 13 Feb 2019 05:58:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=9CMPZjRzPxA9yWJGkQniQIkHzZh/Nj3CjJ78bKTFu6c=; b=ZUHw0Sf36YTGKvYyp1GDn8E2vb4SeDlMfI61s6EQcflLnxnF5GdckHlemlvJZT+w4x 7ph4JG4izzIfdDR7OvTKKQoWyW5RKyt8NRVP4WIWmHtA95wgkCLBIwFS0AbndmVb9/pd DAisVUwT2LlJLFNEbBB8RWeyGq8GxJvNS7qipoeBNmwK3ex41MTLxLlamEhAHD9nMXOp C3Q50gvzlVhMsvQ9rzQBAJEhGWO7cY9Wcc380qSId0BJtRq4rKyWO1pJAI5uX3b+p0PL dH4OLNB8Fq7Rrhtmvlng3LieV/z4nm5xbawfRlTua+HC24TEnvDe2TBYFmrVVkMCqTnG PHOg== X-Gm-Message-State: AHQUAubH8Be8cnTNrxIOs1hFhKdvvXJkfTaSmoUDbRgxobCYZ4J3VQuw X+HDNJgkG339AHc73Pd8FAhL85MdiGUOFFM+ambzVo+3z6kPwSZNp4WTZDTAsLUmS6iX3MqLXpj TbiCJNG3G9vEAo6RUZrlgEhSklioT870FV5jAnxOc4BGpV6UXJ0wiMDU0lH+z28ILhtt2Bk7vgt c+7ThKRayWN8eSsmGXsCvC3CXjpbbnOwp7b4o490N9vS515YUIDJSQA3IZ9+VWIbB2gNf6Y4B70 QDWvl6lE3Iz1Msx2j4jOytkDAqZ2Q85R1is3FSMtDNBkH4Cs09D4zjHGFNtRZaAb1Knt0D8XNhN NP8LK9lGYIOvR0XNE7bdUV0SvDEBzi+aoQ/Jn8A4DTlgmLjAzIIik1T03kcX0qwVnyG4169aHJb w X-Received: by 2002:adf:ba8e:: with SMTP id p14mr519693wrg.230.1550066319942; Wed, 13 Feb 2019 05:58:39 -0800 (PST) X-Received: by 2002:adf:ba8e:: with SMTP id p14mr519636wrg.230.1550066318903; Wed, 13 Feb 2019 05:58:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550066318; cv=none; d=google.com; s=arc-20160816; b=lwO/TUmTiF7+LmPrhYZZAu8wh9wuNLJKl0ymxG1heiT1RLLaCHT9IGYWueJ8aoI9Ff Sl41S84hDeVbyeO0eLwdpthe0X5O512jdcU8jDx9VZMBsvci1heL/PEeC78XtOXw/Z53 UtqWkkW6JZuB0zWNvly3Ug/1mrux3jTX4ZMhNevVgyV48iHDW5e9En1z4OGtSqVsKko2 hpyuX/loOudth3YWCZVex+SIGjw5G1kg0WVqSlPVT6gj/+iec6yTnaRr+xZOeZ727/SG 1dS/yCUYvP7ebwCZHDwaDqKVMJGrGVBnbAq8XK9susso4dgEB+v5hBlOtJndCyP1WVra mGtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=9CMPZjRzPxA9yWJGkQniQIkHzZh/Nj3CjJ78bKTFu6c=; b=a+5EtBmDjiNhoC/L631XP5gwI6fJRPFlRLnaPsGIPjFhceBA3TD527+GCdjLc2kKGy YmgUM2GwDLMBC4IIYVy529x8OEI4/oMHV4dxuiyUBZEnbxwvGELliF9jI8iVNn1v/e5a 2HdVlRT5czSawiWH4FrX81xjKlxbDcaSl919OclFlAdkUrRtJBYDywkVWSO1unpBzGrk Dvx1JeGLYMywy2QOzDCBFSDMyk6YSips2/l6HQlkcWAATnbm7Fl5FK33yt678q6BSF+n ALiqn0ZfGZutm01dtgT+pwvl1kCgTZ4oc97HVSIdaOFEGJuf7BBBaiWb8I0M6hs583wf XTTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kjGmPdlV; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id t4sor10762748wrb.12.2019.02.13.05.58.38 for (Google Transport Security); Wed, 13 Feb 2019 05:58:38 -0800 (PST) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kjGmPdlV; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9CMPZjRzPxA9yWJGkQniQIkHzZh/Nj3CjJ78bKTFu6c=; b=kjGmPdlVp+EE4yXdF9V3m6/2pdOxIjI9TduQHD7wABfQbumO8w8jJZdrEMDwclarVk iY0wzGb7Ynkr3XDwqbdfc68tu241EJwzZYcn574KEMW6H2Ena9s7bPizAO1iE1ape44f SbMHhf6qkqVNBoBSnoFxtmEXkrwxA2hsftnxvbjJRufiTDfsRNiZToeLWX/BTEAm7MIc gVgL6t0qdYp+zmRLiNvcT+c2K0u+y32Fv7D52wNM4/tWi34dFOlgb+1paUdGEsVH5Lzm fY6zoUWLYU5VWoaucELpS87NkXcA4DSD3g1hgc4wezyVoTnt6RNGf0GuJ8GGoD/qApT/ uhEA== X-Google-Smtp-Source: AHgI3IZeu8tyGHdGBePmSzE7Ptbmd1qACcvY/ml8RA3mou9xZXpuJmWZsOr8DOL9M1p61Emj+W1b7A== X-Received: by 2002:adf:f845:: with SMTP id d5mr551808wrq.113.1550066318266; Wed, 13 Feb 2019 05:58:38 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:13:8ce:d7fa:9f4c:492]) by smtp.gmail.com with ESMTPSA id v9sm11195866wrt.82.2019.02.13.05.58.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 05:58:36 -0800 (PST) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Qian Cai , Vincenzo Frascino , Kostya Serebryany , Evgeniy Stepanov , Andrey Konovalov Subject: [PATCH v2 1/5] kasan: fix assigning tags twice Date: Wed, 13 Feb 2019 14:58:26 +0100 Message-Id: X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog In-Reply-To: References: MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP When an object is kmalloc()'ed, two hooks are called: kasan_slab_alloc() and kasan_kmalloc(). Right now we assign a tag twice, once in each of the hooks. Fix it by assigning a tag only in the former hook. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 73c9cbfdedf4..09b534fbba17 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -361,10 +361,15 @@ void kasan_poison_object_data(struct kmem_cache *cache, void *object) * get different tags. */ static u8 assign_tag(struct kmem_cache *cache, const void *object, - bool init, bool krealloc) + bool init, bool keep_tag) { - /* Reuse the same tag for krealloc'ed objects. */ - if (krealloc) + /* + * 1. When an object is kmalloc()'ed, two hooks are called: + * kasan_slab_alloc() and kasan_kmalloc(). We assign the + * tag only in the first one. + * 2. We reuse the same tag for krealloc'ed objects. + */ + if (keep_tag) return get_tag(object); /* @@ -405,12 +410,6 @@ void * __must_check kasan_init_slab_obj(struct kmem_cache *cache, return (void *)object; } -void * __must_check kasan_slab_alloc(struct kmem_cache *cache, void *object, - gfp_t flags) -{ - return kasan_kmalloc(cache, object, cache->object_size, flags); -} - static inline bool shadow_invalid(u8 tag, s8 shadow_byte) { if (IS_ENABLED(CONFIG_KASAN_GENERIC)) @@ -467,7 +466,7 @@ bool kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip) } static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object, - size_t size, gfp_t flags, bool krealloc) + size_t size, gfp_t flags, bool keep_tag) { unsigned long redzone_start; unsigned long redzone_end; @@ -485,7 +484,7 @@ static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object, KASAN_SHADOW_SCALE_SIZE); if (IS_ENABLED(CONFIG_KASAN_SW_TAGS)) - tag = assign_tag(cache, object, false, krealloc); + tag = assign_tag(cache, object, false, keep_tag); /* Tag is ignored in set_tag without CONFIG_KASAN_SW_TAGS */ kasan_unpoison_shadow(set_tag(object, tag), size); @@ -498,10 +497,16 @@ static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object, return set_tag(object, tag); } +void * __must_check kasan_slab_alloc(struct kmem_cache *cache, void *object, + gfp_t flags) +{ + return __kasan_kmalloc(cache, object, cache->object_size, flags, false); +} + void * __must_check kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size, gfp_t flags) { - return __kasan_kmalloc(cache, object, size, flags, false); + return __kasan_kmalloc(cache, object, size, flags, true); } EXPORT_SYMBOL(kasan_kmalloc); From patchwork Wed Feb 13 13:58:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10809983 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E8377922 for ; Wed, 13 Feb 2019 13:58:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D52D42D072 for ; Wed, 13 Feb 2019 13:58:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C8D452D0CB; Wed, 13 Feb 2019 13:58:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 603842D072 for ; Wed, 13 Feb 2019 13:58:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70C578E0006; Wed, 13 Feb 2019 08:58:42 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5F0F78E0004; Wed, 13 Feb 2019 08:58:42 -0500 (EST) 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 48F768E0006; Wed, 13 Feb 2019 08:58:42 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by kanga.kvack.org (Postfix) with ESMTP id E61078E0004 for ; Wed, 13 Feb 2019 08:58:41 -0500 (EST) Received: by mail-wr1-f71.google.com with SMTP id f4so895230wrj.11 for ; Wed, 13 Feb 2019 05:58:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=xu9/37gH7GsD7XGP+9mY66oknw0IDCzNaIitxBdfe0s=; b=jsjJdVuiaOPGCklx9bhkleye3ga5AMKY4qdyZa3ybev1YyTiakVQCfoDCdjqtksyL7 LSCOtfbFDtclUe/5w7zrdsCLYSiRtBZ/MF4Zg1o36PaQO40lXF8iPZa0Ybr29QGS7KvH nzKNirShen1FbF6lh8yHBJtz9DkHr3i1SF7/JAzPND6dFRZTP76AJeV9t1Gr+bjWd0wn uysDCqtHlxp0hKq54rvkunVIXmAtvBxsG1GU4/YO97FQSKVhzRJzyMvhK9d3q/ItzM/J Rk/OxVU9Gub3plwtcjOgt3UOSb83oimhT++V2OM66UuKblCQ04oo/XQGRhZTJ7kG+WBR kJZg== X-Gm-Message-State: AHQUAuYbpHRJEfm7cx+rwomjWmKQwVBIUgZFGNQezxEMhBcO7Kq1WlgJ Ba1g6JArEpTpVqCY8SjW4sNcfw2kvdNQjL2n15tkWHX490wPXRiAzUUeszD+kidOJl0fVe4s3+x i2UG87t+FaHqMZbv0svZQvZUjY3v9y/l1llyNZd/ElB9AHlaJuHC59gf8tsgJYi3F9jmtX8W4gs 6mXjIWPuFVhgVTJEPOjHJvgb/WY4/3lzZhP2JX/wvJApvbF5e3WBagmsybtubLox0a7Rt8wUYO5 3XDxPbfiXvBI9WKRZq+2k+w+MGNWMMfIyXjfybvjojEZ41T/BhSRACKMquzRndwmvXYR5JHAOQz 2r4B5YPQWADV6wxQjC5oAB/i9o1hY5WbDT7rawiNeHwhJs/524JXm6cSXMRolXHTGz2jBxhyUpS t X-Received: by 2002:a5d:5042:: with SMTP id h2mr558010wrt.12.1550066321462; Wed, 13 Feb 2019 05:58:41 -0800 (PST) X-Received: by 2002:a5d:5042:: with SMTP id h2mr557969wrt.12.1550066320605; Wed, 13 Feb 2019 05:58:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550066320; cv=none; d=google.com; s=arc-20160816; b=a63m3YYEOYIORY1bRZkWd/D/zB6kqK7kwFC/fEp6T6v3ZQaoxaUhoTxE/rre+eJI8G aQBt9i4zwyzu6hTGh1BlQcbox3wHChvhK2G1hiRnTiUsn8Z4xA4cxJCQjsvFuCFapboR bCBFvXmfkBlG9gWnJlJSYBc7Qg45GrhHx1uk6Traww3OqXF5e5Eth2d21b0+dbO7vUqo MvOIsL3FOXu2Wmdcx+pCWenRpE7zqVGZVtn6mMxkbdM7mnwMfyTg+lKwoAUty61nNnE7 dqcYptHScPz/KZn6MjfXSPeauEMS/W8vMjwzm3IzR/eblgs+B201RE7bYHkBaHdQ+lPf DWRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=xu9/37gH7GsD7XGP+9mY66oknw0IDCzNaIitxBdfe0s=; b=MxmOiqR/+KAoTVt+a86ACiQPgvWGsWHmBq7MDrBjjgxgDVWEDdAxHM5zHbHHmCzA7/ MnG05kfpYeFybh25VFMymP7SfUWOvVngTnBNtuXfyrYJcgiIlQgxOA1ZY/mTaHd8OXo3 Dp6CQ5g9W+6qLfTJ2+OZJVH44hrA1ntd4YW8tUOXQ7NJG/usdWiESsHjgholeEDkcbQx bqp4FEW/a177gedg47jH3dLnBdQ+hAeOYS2afYUm8q79yGK4otpzPKpLNXHA6lmCCryg mtFcPsvfkj5fxVc1HxaGtQ+7J84efefvuD9bNL7OKMCsgZQ6LyOSyJNkK1Sv7BTckUJg UCzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LZuoEBFV; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id l23sor4019733wmc.28.2019.02.13.05.58.40 for (Google Transport Security); Wed, 13 Feb 2019 05:58:40 -0800 (PST) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LZuoEBFV; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xu9/37gH7GsD7XGP+9mY66oknw0IDCzNaIitxBdfe0s=; b=LZuoEBFVaWnAkKQXjENcMdC3sNGWcdA3HmBu8uls/pLTXjoBa2auXv/R80mygqnJQK +SvuJyoS3RyiqUvljTK731WoOBraKMvm6D7Ej1H743G6t7ktuiXiZnPZ744s2D7m4blq YsPq91p3hRdIGpL64p86i0Zmw0/8MuTO7L5usonaP4RP0hzKXLLRg6SWUHk+GxH5mzF6 nEOg3Q7g/RhGy5whyZE7hQLMEdyr+kpaB9E7eB/EB8r10C5v2/1R+XGFej1BMSkovI23 Njrmc4/xiClA2MC9bLpqQFPYw8oovAHIcLvHhDmRkbxPAvK5ByKy6i5x2WkPLbxGYv4N efiA== X-Google-Smtp-Source: AHgI3IYJPHef5SXRQikCmTo4N6D/LQvbU/67LalinnNI2TP41Ugpf9msJPWIIiTh0Rkcfztcv7jCLA== X-Received: by 2002:a7b:c205:: with SMTP id x5mr442568wmi.3.1550066319928; Wed, 13 Feb 2019 05:58:39 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:13:8ce:d7fa:9f4c:492]) by smtp.gmail.com with ESMTPSA id v9sm11195866wrt.82.2019.02.13.05.58.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 05:58:38 -0800 (PST) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Qian Cai , Vincenzo Frascino , Kostya Serebryany , Evgeniy Stepanov , Andrey Konovalov Subject: [PATCH v2 2/5] kasan, kmemleak: pass tagged pointers to kmemleak Date: Wed, 13 Feb 2019 14:58:27 +0100 Message-Id: X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog In-Reply-To: References: MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP Right now we call kmemleak hooks before assigning tags to pointers in KASAN hooks. As a result, when an objects gets allocated, kmemleak sees a differently tagged pointer, compared to the one it sees when the object gets freed. Fix it by calling KASAN hooks before kmemleak's ones. Reported-by: Qian Cai Signed-off-by: Andrey Konovalov --- mm/slab.h | 6 ++---- mm/slab_common.c | 2 +- mm/slub.c | 3 ++- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index 4190c24ef0e9..638ea1b25d39 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -437,11 +437,9 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, flags &= gfp_allowed_mask; for (i = 0; i < size; i++) { - void *object = p[i]; - - kmemleak_alloc_recursive(object, s->object_size, 1, + p[i] = kasan_slab_alloc(s, p[i], flags); + kmemleak_alloc_recursive(p[i], s->object_size, 1, s->flags, flags); - p[i] = kasan_slab_alloc(s, object, flags); } if (memcg_kmem_enabled()) diff --git a/mm/slab_common.c b/mm/slab_common.c index 81732d05e74a..fe524c8d0246 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1228,8 +1228,8 @@ void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) flags |= __GFP_COMP; page = alloc_pages(flags, order); ret = page ? page_address(page) : NULL; - kmemleak_alloc(ret, size, 1, flags); ret = kasan_kmalloc_large(ret, size, flags); + kmemleak_alloc(ret, size, 1, flags); return ret; } EXPORT_SYMBOL(kmalloc_order); diff --git a/mm/slub.c b/mm/slub.c index 1e3d0ec4e200..4a3d7686902f 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1374,8 +1374,9 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node, */ static inline void *kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags) { + ptr = kasan_kmalloc_large(ptr, size, flags); kmemleak_alloc(ptr, size, 1, flags); - return kasan_kmalloc_large(ptr, size, flags); + return ptr; } static __always_inline void kfree_hook(void *x) From patchwork Wed Feb 13 13:58:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10809985 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 064CB1399 for ; Wed, 13 Feb 2019 13:58:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E609D2D072 for ; Wed, 13 Feb 2019 13:58:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D74632D0D0; Wed, 13 Feb 2019 13:58:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E8F32D072 for ; Wed, 13 Feb 2019 13:58:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5F2908E0007; Wed, 13 Feb 2019 08:58:44 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 530D38E0004; Wed, 13 Feb 2019 08:58:44 -0500 (EST) 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 32F018E0007; Wed, 13 Feb 2019 08:58:44 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by kanga.kvack.org (Postfix) with ESMTP id D00878E0004 for ; Wed, 13 Feb 2019 08:58:43 -0500 (EST) Received: by mail-wm1-f70.google.com with SMTP id o5so968602wmf.9 for ; Wed, 13 Feb 2019 05:58:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=S1MEkkxvqC9R885OkIU7HQfuqGvidJi6l2IAbLfaU7c=; b=iWhh3xjjWRt3fvxSam81l8902ISfuE/0f/hKOCstbSZJ6T75DAepIU5/RfpFZ9I7bd sRLPT08EzJkMTtkqNr6QmU6Srk1YlaaZGwC5SiBzyjln35MUQdP4OE4xaJMJYipdZqse cXmUHs/0l8EPHPZFa/D1WHGfMQmaN3HzzraNgZAmhHJib+xIXgeuRhf3dQ1EooJofjjb /6+lidcIajOMR4VMjQ1OTDjz28tT3+4jRtv06k60bk0nuDhQDZ/DsxXjxW2fNZCwdPOa ECmLY4sWclinitXbtKsIRVlPMjYer+wvksf6e3ZVnzW/c9m+pyYTTboXuU/cHE0TTsS0 bDIg== X-Gm-Message-State: AHQUAubDU9/3dGY36s4by/LydR/AXrDzFACEHOKICUzRHqeuxtlb3PHV FV47YojRFiExj1RfV3LqKr+BI/aXjcNvt6CbpPqsBWcczFPo44P5xG+KEofE5GXVmRNamIR+Nlv AuNe9R6LkG4Um7Y6mk42Gg5awSmuc8XK8PbLmshBrg83f8PuXjtQZIJc55fdLqIkTOmDoGcnnlW nUpCFkAJ9dMhnU0r6cloWCFkOLyJspsmnsaNrIhmaAa7wtizi7tpYwwU40DxXsO5jevhEqDLFuj lT+YeFXnHuqzyR00TazBSHkmHxLqhHfVLnFcakcUzXMT3QnUOIEh4NxS37znGOCrMo+V8rBD6NU VMdvwWb4kJo9qNdQLcODB75dJhpFzi2II0+PlzQ2Dt+2VtTb9KgmgfxsPd3whSKk/vMH83cyWEI R X-Received: by 2002:a1c:4155:: with SMTP id o82mr427849wma.122.1550066323258; Wed, 13 Feb 2019 05:58:43 -0800 (PST) X-Received: by 2002:a1c:4155:: with SMTP id o82mr427790wma.122.1550066322247; Wed, 13 Feb 2019 05:58:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550066322; cv=none; d=google.com; s=arc-20160816; b=DzwqiRaAEaQBwBqmgsctK6qF6riDf34iblAxpcv1Em/sNft9itjbratPEXW+FBbJM+ 15A9G/VOQdUUOjSH8Se6frqhD24TnN5WqkhnEbHjSnggbGvYLQUST5f9sDyRnw16Yd4c qLWoFSkG2Q5POaNYqWDfk/FYdZkPbFLE4Edt+CRuROHoU9rMhEAwvHES5R3JtP8dfHfO emvqYc8nmyr65x937Y63Rc8Z484HnKgxcLOSwn1qip+vrGPa/a15XG5ki4Dj8f+klkoA 36mCjHAS2yT28wFHkoNioJGvwrO+LE/ZQZwgipAlG61vsjTEBPv1FHO1oe0pqvJT+pv1 MjZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=S1MEkkxvqC9R885OkIU7HQfuqGvidJi6l2IAbLfaU7c=; b=giy+WOqKbGtT/HZF8RvcD9UdvWBavaekhLzzRVBHXzyaPIP/EjyRoxfFtPDOo4nAQm bmWpiwzaShqUsvXunDuy+B4cTLX+4ev4V3uwj3fz6JWezCJX6XzHuwLqEXo5jKsGsm1Q Q4+9F8QKb5zJA9nCnsCZznUsgbVe1UeuyBdi+l9g2RZGbziFk7zU3BjIxHinSBXY3Hff K6WUINafI/DvY8fYb4LbUEN/B6Ck5gMgFmJeiaTmkRe8ImAhk871fcdWUEMEIXv2tFBU 0SzGATV2AcCMlcdmOkx5uVSzSOAeXXpIXvgL3cj4rgMc15EZIfyoPLuxYjHnDJyL4NFs HMKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=p4gWTZtr; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id x19sor3281783wmh.19.2019.02.13.05.58.42 for (Google Transport Security); Wed, 13 Feb 2019 05:58:42 -0800 (PST) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=p4gWTZtr; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S1MEkkxvqC9R885OkIU7HQfuqGvidJi6l2IAbLfaU7c=; b=p4gWTZtrw9t1J2YewM74IoZolPi7VQOnG/RK8ZQZiZ/MwIJe/QojBdLDC0S26B8FQG sJ4Fc7OS2U+qrzDTtBlUkcxXKRCWNf1c5K/bS7z16u9lokG1ooOi7P77c7/vamnDG7Df 2d918grgBusikS9jEYCuEuUdFT/Necs62nCZY3mg2ogWOz/b528vJ27LAPrsdjujFfzC S36qTciiQccAXgi+hQRaoQrYY5G/+zZLgQq10wBRmjFy+iVN02dNNsOk8FgKCjX97iFc cA1T6HSt+VFQ0dTj0xVahAedsFj4iI5IeV6B7tiQWfAl9TJtccR3FaUza3yCVly+NVX7 V90A== X-Google-Smtp-Source: AHgI3IaTRjRBOpfqDQyCSkM0FCxrdwo/Gj5shg1xg359f+Atts3bPUEzKs/gM3ivkCgMfSuFGNws2w== X-Received: by 2002:a1c:cc01:: with SMTP id h1mr494171wmb.18.1550066321635; Wed, 13 Feb 2019 05:58:41 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:13:8ce:d7fa:9f4c:492]) by smtp.gmail.com with ESMTPSA id v9sm11195866wrt.82.2019.02.13.05.58.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 05:58:40 -0800 (PST) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Qian Cai , Vincenzo Frascino , Kostya Serebryany , Evgeniy Stepanov , Andrey Konovalov Subject: [PATCH v2 3/5] kmemleak: account for tagged pointers when calculating pointer range Date: Wed, 13 Feb 2019 14:58:28 +0100 Message-Id: <16e887d442986ab87fe87a755815ad92fa431a5f.1550066133.git.andreyknvl@google.com> X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog In-Reply-To: References: MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP kmemleak keeps two global variables, min_addr and max_addr, which store the range of valid (encountered by kmemleak) pointer values, which it later uses to speed up pointer lookup when scanning blocks. With tagged pointers this range will get bigger than it needs to be. This patch makes kmemleak untag pointers before saving them to min_addr and max_addr and when performing a lookup. Signed-off-by: Andrey Konovalov Tested-by: Qian Cai Acked-by: Catalin Marinas --- mm/kmemleak.c | 10 +++++++--- mm/slab.h | 1 + mm/slab_common.c | 1 + mm/slub.c | 1 + 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index f9d9dc250428..707fa5579f66 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -574,6 +574,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, unsigned long flags; struct kmemleak_object *object, *parent; struct rb_node **link, *rb_parent; + unsigned long untagged_ptr; object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp)); if (!object) { @@ -619,8 +620,9 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, write_lock_irqsave(&kmemleak_lock, flags); - min_addr = min(min_addr, ptr); - max_addr = max(max_addr, ptr + size); + untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); + min_addr = min(min_addr, untagged_ptr); + max_addr = max(max_addr, untagged_ptr + size); link = &object_tree_root.rb_node; rb_parent = NULL; while (*link) { @@ -1333,6 +1335,7 @@ static void scan_block(void *_start, void *_end, unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER); unsigned long *end = _end - (BYTES_PER_POINTER - 1); unsigned long flags; + unsigned long untagged_ptr; read_lock_irqsave(&kmemleak_lock, flags); for (ptr = start; ptr < end; ptr++) { @@ -1347,7 +1350,8 @@ static void scan_block(void *_start, void *_end, pointer = *ptr; kasan_enable_current(); - if (pointer < min_addr || pointer >= max_addr) + untagged_ptr = (unsigned long)kasan_reset_tag((void *)pointer); + if (untagged_ptr < min_addr || untagged_ptr >= max_addr) continue; /* diff --git a/mm/slab.h b/mm/slab.h index 638ea1b25d39..384105318779 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -438,6 +438,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, flags &= gfp_allowed_mask; for (i = 0; i < size; i++) { p[i] = kasan_slab_alloc(s, p[i], flags); + /* As p[i] might get tagged, call kmemleak hook after KASAN. */ kmemleak_alloc_recursive(p[i], s->object_size, 1, s->flags, flags); } diff --git a/mm/slab_common.c b/mm/slab_common.c index fe524c8d0246..f9d89c1b5977 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1229,6 +1229,7 @@ void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) page = alloc_pages(flags, order); ret = page ? page_address(page) : NULL; ret = kasan_kmalloc_large(ret, size, flags); + /* As ret might get tagged, call kmemleak hook after KASAN. */ kmemleak_alloc(ret, size, 1, flags); return ret; } diff --git a/mm/slub.c b/mm/slub.c index 4a3d7686902f..f5a451c49190 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1375,6 +1375,7 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node, 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); return ptr; } From patchwork Wed Feb 13 13:58:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10809987 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 98B871399 for ; Wed, 13 Feb 2019 13:58:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 814232BDF3 for ; Wed, 13 Feb 2019 13:58:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 72FE52A14D; Wed, 13 Feb 2019 13:58:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F0CC32CB06 for ; Wed, 13 Feb 2019 13:58:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0D3A8E0008; Wed, 13 Feb 2019 08:58:45 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B250B8E0004; Wed, 13 Feb 2019 08:58:45 -0500 (EST) 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 973AE8E0008; Wed, 13 Feb 2019 08:58:45 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by kanga.kvack.org (Postfix) with ESMTP id 3C0288E0004 for ; Wed, 13 Feb 2019 08:58:45 -0500 (EST) Received: by mail-wm1-f70.google.com with SMTP id f6so573398wmj.5 for ; Wed, 13 Feb 2019 05:58:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ZJ19SdNCKgDIE486SghXxp8EeRTbnWgedDgd49Ot8LY=; b=rOCyXrJ5edRybFI5HvuoAhZ2U9ffEDN6LHA0EUuNmqEy4iBBoXQsd1JIQAwm2bGkvr UIriduDGz3AOuEKcPfX/w2mMX20MpYJwks778mIehzX3ut9A+UXd9xDoBXtkmCqD5trk xN3qll8Kr2F/gpwvN5WP8DrFrzl5GLLrBehoyX7q1OoMp0WEZtUmdOx5x7k3P3s23geA QXTIYz5KeO3ij4BK2/6s1dU+IdoMn8d4XXEjKX8tgLNw/NkbilS2jlkOKLKIdm7gZfho 6oalHjriYZGZQRzF9FQ0cRRWsUgVuie4ndW1aPkTs4zsNlURSdQZg+StQqhK1w9hHOFi tIxw== X-Gm-Message-State: AHQUAuYfWTH/kYRMkWQbMqMtVryZ5Ak/8wpitRVsaebKUFQQcL1mW4hp KAGkpxMYmZp0ITrKW2Li8mJWZlGGsGbGQnPwwZuroK1P5Yh6FUFE11r9v7cfzlv25bcz1xpfabY oeH7d6baYXrn9Vx9C0R//lr2ksJJlrB3wAFI5omYiziiknQscibIvz1CXORkIDGUMNILSaGhWlv geGfHCtZLUJPr7mnWg/Wj7H3SWjwFtnpoCXfxSpkTJOhhXKOOUQ8If4G9CE3vPyJQ5B1CrqhLHA N2ephMocPQbqqdCg1rF7gZAitEQJtMjL3OX91Gip/jXSIN07Qo7/LYQCTgHHyx3OyhqpKXtuAQs 2tYzeEsvaSxA8C0JY4ywQU/3/vQDcFU3Uxzih/KtnXe32mn9Tp1N9izj7j8Px8pGmiDDs7/9Ec4 Q X-Received: by 2002:adf:9083:: with SMTP id i3mr540493wri.124.1550066324822; Wed, 13 Feb 2019 05:58:44 -0800 (PST) X-Received: by 2002:adf:9083:: with SMTP id i3mr540454wri.124.1550066324046; Wed, 13 Feb 2019 05:58:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550066324; cv=none; d=google.com; s=arc-20160816; b=dj03Sz8podvpPinK31TY1WSpFuC31fIdc8T+BqGG3RNajTBe+Q4ZTKsqSy6Bl0wmEy 0YQHnN4a9N2VAxLZppBtJ/4+4CkaaWMAbCVRkd8TDdc+8gi3ECWliElQSQ0U/TdvpIpc zQlTRDPYofTWPsmGo8BMnTrGjFywEKxo24xl8bq2L3ze5k8rGqOKdNtyLXCBPf8zIbxk iAphQfBl/45vG8NpPr0HvvQG6p9aQ4kPGDvYOHFDPYCecwFsjyHD+gF0BbnRp4gF/CIp vRXIDV4pXTGto9nzzLT+n4eWj9MfMuMaJ4SP27dEmOmiQ7kY4VutF4inHgD3PWOLfzMN Qwrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=ZJ19SdNCKgDIE486SghXxp8EeRTbnWgedDgd49Ot8LY=; b=eCZPGlo7uT9AtJMkIfi0PbyFD19jNLbD6Q0c/vXfM2AmWbmQ0byLJk0EmQdaV3jGph pvfj2q6NDU91gbctt9ausR5FznkiI0Oa08rsTxc/g3pbbF4giEG+O3OCU+WMAoHFUjSz NBQW4AcXUmqJrIb8p4Tko5geOFG684xz68MCSA/x8sPreTiMCnaOiqwHbNdL85pe7nd3 miV5jZzWS2NtA4PTJTJ+zI4oe6IKt1FP15or2Ijms9gU6yc6NKZtg/bMoT19ZcKO3NU3 dm7AJQuJZOrVbFj4+jdkK1eVGYLxEJXjRX/x7+6DswwxzJUFYfY8INWDLucsBXCcPZy0 ARqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=htKdKO8v; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id g4sor3917406wmk.5.2019.02.13.05.58.43 for (Google Transport Security); Wed, 13 Feb 2019 05:58:44 -0800 (PST) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=htKdKO8v; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZJ19SdNCKgDIE486SghXxp8EeRTbnWgedDgd49Ot8LY=; b=htKdKO8vBTjshwQ3AOXHQnliRn0Kc/VxXw/fuELJfZIt5p2O5gZSUsF3Jlhe0FkTeZ 3M/UhAc0stoT14pdlauSdJf2FZTh0Sda9yQQCgez0E7owtrrPruZySyeFqqo69LLAa8H Q360545gVBI9qQ0ZQOYy0wij5E76UXsK6xgkZD+UHJID4L9I0q/hCt3G60FVtLZjmbnO 45X4MQAtsfktah0kAQEZQVq2fT8Eoa6gR/MTOKnKgIlOj9Ols0BjUSyAfB1rhUGlXexd ZJQtEIYllF/hRJwCKNFLRog1j08jsXuRQHVzq18Mj/7Z883WDpVYDrnfrCElgy3Db0sh PWCw== X-Google-Smtp-Source: AHgI3IZzwEwCq280Wk1OdXfBbHFirTmNRy3Kd9bMAGui2G/AoloTA3zMirRTtinqrouFWX4qc+C4Uw== X-Received: by 2002:a7b:cc86:: with SMTP id p6mr438129wma.32.1550066323461; Wed, 13 Feb 2019 05:58:43 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:13:8ce:d7fa:9f4c:492]) by smtp.gmail.com with ESMTPSA id v9sm11195866wrt.82.2019.02.13.05.58.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 05:58:42 -0800 (PST) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Qian Cai , Vincenzo Frascino , Kostya Serebryany , Evgeniy Stepanov , Andrey Konovalov Subject: [PATCH v2 4/5] kasan, slub: move kasan_poison_slab hook before page_address Date: Wed, 13 Feb 2019 14:58:29 +0100 Message-Id: X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog In-Reply-To: References: MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP With tag based KASAN page_address() looks at the page flags to see whether the resulting pointer needs to have a tag set. Since we don't want to set a tag when page_address() is called on SLAB pages, we call page_kasan_tag_reset() in kasan_poison_slab(). However in allocate_slab() page_address() is called before kasan_poison_slab(). Fix it by changing the order. Signed-off-by: Andrey Konovalov --- mm/slub.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index f5a451c49190..a7e7c7f719f9 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1075,6 +1075,16 @@ static void setup_object_debug(struct kmem_cache *s, struct page *page, init_tracking(s, object); } +static void setup_page_debug(struct kmem_cache *s, void *addr, int order) +{ + if (!(s->flags & SLAB_POISON)) + return; + + metadata_access_enable(); + memset(addr, POISON_INUSE, PAGE_SIZE << order); + metadata_access_disable(); +} + static inline int alloc_consistency_checks(struct kmem_cache *s, struct page *page, void *object, unsigned long addr) @@ -1330,6 +1340,8 @@ slab_flags_t kmem_cache_flags(unsigned int object_size, #else /* !CONFIG_SLUB_DEBUG */ static inline void setup_object_debug(struct kmem_cache *s, struct page *page, void *object) {} +static inline void setup_page_debug(struct kmem_cache *s, + void *addr, int order) {} static inline int alloc_debug_processing(struct kmem_cache *s, struct page *page, void *object, unsigned long addr) { return 0; } @@ -1643,12 +1655,11 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) if (page_is_pfmemalloc(page)) SetPageSlabPfmemalloc(page); - start = page_address(page); + kasan_poison_slab(page); - if (unlikely(s->flags & SLAB_POISON)) - memset(start, POISON_INUSE, PAGE_SIZE << order); + start = page_address(page); - kasan_poison_slab(page); + setup_page_debug(s, start, order); shuffle = shuffle_freelist(s, page); From patchwork Wed Feb 13 13:58:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10809989 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 75CE4922 for ; Wed, 13 Feb 2019 13:58:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 618EB2CC7F for ; Wed, 13 Feb 2019 13:58:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 53B952CC84; Wed, 13 Feb 2019 13:58:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E06122CC7F for ; Wed, 13 Feb 2019 13:58:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F194B8E0009; Wed, 13 Feb 2019 08:58:47 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id ECAFA8E0004; Wed, 13 Feb 2019 08:58:47 -0500 (EST) 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 D934A8E0009; Wed, 13 Feb 2019 08:58:47 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by kanga.kvack.org (Postfix) with ESMTP id 7D0BD8E0004 for ; Wed, 13 Feb 2019 08:58:47 -0500 (EST) Received: by mail-wm1-f72.google.com with SMTP id q126so974037wme.7 for ; Wed, 13 Feb 2019 05:58:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=72pG1JXx9pTwwdTmZ4N/9abSJyq1vghQRBkOj2XfeKw=; b=lZTrKL6NL0Z5AyrjN5jRN5w5crCfYDxR+kbpMUHzyau5t+yaZKj8jHNR4IGamjiqAi +b5gSlxM8yf57d/SeG7iTi0Ajq78y61Md+v2B26QKINRcv4lIlWsbp9hYIlDdhpQi5iA vOb1/Nz5E00FF1UoLXkfHsGJVspPMyigsUr091Mi6TcW5nN9auYlZ+Nr0J33xF+kC4tP 35v7/4Be8ToLRpU2J7EFzzPPBPoWGr6G2N+PDRH3hAIyISIt0ulj6JsfigJrsOfnfhz7 Tk1ifXDiDWFe6PAb4NebS/S/n7xrWfkrsQZrRyNP+dTYRcky2Aerzf2SPT91E5PLeY2y gjOQ== X-Gm-Message-State: AHQUAuZspOH2bVHV3jVEh0L3W93M7YiSPa7HRJLNNET8J3mEi9gzjM2o LpET8+w3A5G7KsSdHUo5ojBdWH6Q/mHCX2+B4yWNcH+iJTBtUo6zR4q4Lc2ORtufiJCNQt2WMXV cnpWFNqKrLIzT1j9K5f1rroE7WG1D+aREGkYTrX6WE8mGsj11iLKYNxpeFBW/66dQuUJzN9Iz3P eKgyKxJXAXd5n9G8TNHuG+9CFUWpJF3f9uI5W10ujfh3o2pwXhxWlTMOxvwXPHB5ul7pmzNmQ6F lhI8ZUUKu6ubvVIhHsrPp7IxZtCuUiMBgBv9mMqiGzRp6gZSEF22Qbru+J2BwUU94TdTRDXAmOj i/M6Pu3PgGKcl59KmOj7ixiSW1WiknC+0SrQMlYFd7kmkL4bzc3dP0J0iCOsUeRVjNxvFnh093t 5 X-Received: by 2002:adf:efc2:: with SMTP id i2mr557319wrp.44.1550066326965; Wed, 13 Feb 2019 05:58:46 -0800 (PST) X-Received: by 2002:adf:efc2:: with SMTP id i2mr557268wrp.44.1550066326044; Wed, 13 Feb 2019 05:58:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550066326; cv=none; d=google.com; s=arc-20160816; b=cIVVmSSyeGiXqUz01XkI53qob1IN9DcxeFZ94vumoBJ4FtXO+Bzv0VnvGX4AlY2AlE r30ZTLH/YYyj82RyTHCqnx2tw4GfWRACxoryANdLfbmsffu686BPAXZ9sx3JPkM9/W2z blPAKEZZgN73bx7AOdcdq7rgiCPK/IWyFMKr9rbgabTIGbedcxK55DtKg3gOOI7zY537 /Jf2Xuki3d139vgSZ+0asBlkbD4PvnLIvOftXgy1norUtwF/jdonAQTsjFfQGAv98E3M snaQ/kRL/R7KcyZK+fc63ysbu/Y26N+K0zHsrW5vs+LFnMUPzBCzKeZ0VN6dQzyTI67R /knA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=72pG1JXx9pTwwdTmZ4N/9abSJyq1vghQRBkOj2XfeKw=; b=Nve/aGn5KlwnFTtgiToO9OQOaZlePXhe8MABi50BCTjrgVUVqcmmZCaa/p3nXL43Wp osp4o+E+J39UlGoi3nF2+MjTcsgEAZgsUhfB/Qim7xvBsaD8BcdHBzY7kBsP+eajq9sy p2h0cO7qhZ33206HevMB1v8DeY1T3bE1lrhqebhYj0lMFtud3KP9n/jvdem08NyC3jw4 CqAFFz0qW2GCuRcIXwInLXE3s4mh+CKihp1qJy/k21LW/0GcbfsZqx7CCjAyDz/bchJY fWrUHiSeCQrSQtN2u0gx97oTltXJa+rb5mFUbUtLeQqlAk08pilVAUPRfb4hpV6fVxbZ JGbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="v/FPUkyA"; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id h9sor1349619wrs.5.2019.02.13.05.58.45 for (Google Transport Security); Wed, 13 Feb 2019 05:58:46 -0800 (PST) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="v/FPUkyA"; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=72pG1JXx9pTwwdTmZ4N/9abSJyq1vghQRBkOj2XfeKw=; b=v/FPUkyA+O8nQlrSyFo2Do+uc3abWZyy1VkLzISNPUV32Z/g2SLr6DHt8F+aP9bCca X2sNVYq2Mqqq8c4uFELjHM1pNYrSQZrmWsTqpS/+W0+iqYB1rcMY2GrZOVdPmvBcbP/3 TIuUgcggk7jOZIwAgIkAqMisw0W50xiT50cphfZzTkQ2G5LO/8PFp9yGtIX8z1FDtrfz 5V3QXmRkOhUlo00MvEKLq0FWK99shrVA2r3UxY28O6wNA+OSaq9GYNIN0lofvzfZaEYB 3RivTm0ZInQzS2WCLBpGCahZRFX00KCUTqm1Q4ZU/LpgjOmS6s3zupxYOq/uEpYz2SNu 7IsQ== X-Google-Smtp-Source: AHgI3Ia8YJ65gI15fHc64dlpvyIVgpUQffLxwgRbwKBMOlnrlali2Rni0LlhcKEeodCN68fd3eD1yg== X-Received: by 2002:a05:6000:1107:: with SMTP id z7mr492470wrw.87.1550066325440; Wed, 13 Feb 2019 05:58:45 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:13:8ce:d7fa:9f4c:492]) by smtp.gmail.com with ESMTPSA id v9sm11195866wrt.82.2019.02.13.05.58.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 05:58:44 -0800 (PST) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Qian Cai , Vincenzo Frascino , Kostya Serebryany , Evgeniy Stepanov , Andrey Konovalov Subject: [PATCH v2 5/5] kasan, slub: fix conflicts with CONFIG_SLAB_FREELIST_HARDENED Date: Wed, 13 Feb 2019 14:58:30 +0100 Message-Id: X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog In-Reply-To: References: MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP CONFIG_SLAB_FREELIST_HARDENED hashes freelist pointer with the address of the object where the pointer gets stored. With tag based KASAN we don't account for that when building freelist, as we call set_freepointer() with the first argument untagged. This patch changes the code to properly propagate tags throughout the loop. Reported-by: Qian Cai Signed-off-by: Andrey Konovalov --- mm/slub.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index a7e7c7f719f9..80da3a40b74d 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -303,11 +303,6 @@ static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) __p < (__addr) + (__objects) * (__s)->size; \ __p += (__s)->size) -#define for_each_object_idx(__p, __idx, __s, __addr, __objects) \ - for (__p = fixup_red_left(__s, __addr), __idx = 1; \ - __idx <= __objects; \ - __p += (__s)->size, __idx++) - /* Determine object index from a given position */ static inline unsigned int slab_index(void *p, struct kmem_cache *s, void *addr) { @@ -1664,17 +1659,16 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) shuffle = shuffle_freelist(s, page); if (!shuffle) { - for_each_object_idx(p, idx, s, start, page->objects) { - if (likely(idx < page->objects)) { - next = p + s->size; - next = setup_object(s, page, next); - set_freepointer(s, p, next); - } else - set_freepointer(s, p, NULL); - } start = fixup_red_left(s, start); start = setup_object(s, page, start); page->freelist = start; + for (idx = 0, p = start; idx < page->objects - 1; idx++) { + next = p + s->size; + next = setup_object(s, page, next); + set_freepointer(s, p, next); + p = next; + } + set_freepointer(s, p, NULL); } page->inuse = page->objects;