Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp785661imm; Fri, 21 Sep 2018 08:15:58 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZdDmQfgmJG8rCyX5/ZbQIF4roVJsGUKwedhDpSyLT0Md+LqUGXMO2MAdwUorLLKC7xhHCw X-Received: by 2002:a17:902:59c7:: with SMTP id d7-v6mr8865590plj.142.1537542958749; Fri, 21 Sep 2018 08:15:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537542958; cv=none; d=google.com; s=arc-20160816; b=In4YReEPMuosgWWkUprYVtJ0ZsR9LM7vBk46Py/Mdk1mUixyqOyMoje0YlP7dBLxyp qK9QxCaEkIiYyupt6KG7y6ajiKipIWGSIEk84yETSGn3RrtrTiDb0OYy4R6p/a6Lhy+/ xe0r7xwlMMOw7/GOWKLVvKuv1iNBWzTeG1NAFFZ0si14FWsvpRZvFqlJ524WTxJF4Sq8 xS9FhHjKRfCnJfeqkL03LnYPBWOQodjsLzg/RhsYL/onlrUCNEO+iWVyrL3+R1FAssDN cEqvAwVSe6rnnizBbzmhBCHUq3HzSU1jmMxug5QjRK3g2BI2UL2Fan3G390chcZJs0RW iRlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mjTJMND2InMYoPSUJVvHZluvejsOaOH9BQkiQvHv+tc=; b=ps45TkcbastqMt7kflsXpEynEz5i0Pggg/YkVGLNETYBWxY3QddSdgb7ydafT6i7Hk FyaPdefSe+xmfHtGMvgTV6480WB6SLADgbSwtLwJaia4Q3BvncTA07hfoKHsBunlUFVr LLWxiPbr2bvhenqG9Zv+IxayvKsQwSFtiOUH0ZJ01cPIa+edksoOPE++Oa2CMLhZpx4B yvDbZPhJwMYBePeBX5DlSAi4MwZx0E5xUkPRJvIGwiln83svWQBD37m7zsvNCBYeoFJw PdLi3kmnfqZ+b01gilAoY9xE0zNIvU+VKbd8zeEGKQbNySphZ0G34rqo87s0BK3br50A Ob+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=iP8uqVhJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bc3-v6si26162250plb.214.2018.09.21.08.15.34; Fri, 21 Sep 2018 08:15:58 -0700 (PDT) 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=@google.com header.s=20161025 header.b=iP8uqVhJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390999AbeIUVDX (ORCPT + 99 others); Fri, 21 Sep 2018 17:03:23 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:42617 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390865AbeIUVDW (ORCPT ); Fri, 21 Sep 2018 17:03:22 -0400 Received: by mail-wr1-f65.google.com with SMTP id v17-v6so13207962wrr.9 for ; Fri, 21 Sep 2018 08:14:02 -0700 (PDT) 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=mjTJMND2InMYoPSUJVvHZluvejsOaOH9BQkiQvHv+tc=; b=iP8uqVhJGEXg6tZRqMuU9Q7HePNsCduoH/q7Aw1X/zYOwPA0bolayCWyXePfTy7zJp vCrPFVZA3OfJblBv7fEQ5Wj64KRmCWAF1iMIV+qyobMXa/WsvTJOtSp+vFKnbrVbriEP 0KCjR1cLDCo3eHlhUpiVkjZaW+hlmNewj8FTFsjYGn63b0wWJdAePM2mBJR+xBnLO53q bmw++1JEHafERQZ7w/pyE3pv7akhaNozJHz/rBu1JHSvly1AqKW2UlHQqYmZHxPb+smO DZR1w1WqwZ7hm4SnzIEY1xcHQqixqSxPeEe5Djkg99PW60K6BMEknQmTgfoIbYPvrP05 XzkQ== 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:mime-version:content-transfer-encoding; bh=mjTJMND2InMYoPSUJVvHZluvejsOaOH9BQkiQvHv+tc=; b=cNE9z5eWREvCcBlSW3xzQ8AkmrcFqxeiSvvZxodOnzHWL6kVlqHTPgAwUtjU8Wc8yV CfXz4JWDJtZZ9rrIiTVjFC2nXKLo3z0ETubATjf8zpgJOJHD/080xqoSJrwrorYMVYow c80Y1CFW8saS1TsY/aMyh6WBhSjXt7Owl7nHC3i9bKyHCM00JFnYZhEtocshLlsYjeCc kCdXtNz8dqQ10H5/yS1me/lIEFfA+Q/f7Mm4L9tXvjo7U550ZIUZp5QUyxNy93owd5HE rFjcrlDKY2jpK06thTh+qUcQF2aVg4dzgCnBGShepu12zOgYwAZgUV3UqYjC/IDRyaO/ 1UXQ== X-Gm-Message-State: APzg51CifhdQI9HnhyV7i/urOcktzrO5PXR7jCpaQ3ZOzJDqolRbtz1w ZTN1LcYeZ5daQ1yi7BCnWCMiRg== X-Received: by 2002:a5d:438d:: with SMTP id i13-v6mr39162425wrq.156.1537542842008; Fri, 21 Sep 2018 08:14:02 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id e7-v6sm27990271wru.46.2018.09.21.08.14.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 08:14:01 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Will Deacon , Christoph Lameter , Andrew Morton , Mark Rutland , Nick Desaulniers , Marc Zyngier , Dave Martin , Ard Biesheuvel , "Eric W . Biederman" , Ingo Molnar , Paul Lawrence , Geert Uytterhoeven , Arnd Bergmann , "Kirill A . Shutemov" , Greg Kroah-Hartman , Kate Stewart , Mike Rapoport , kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sparse@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org Cc: Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Jann Horn , Mark Brand , Chintan Pandya , Vishwath Mohan , Andrey Konovalov Subject: [PATCH v9 09/20] kasan: preassign tags to objects with ctors or SLAB_TYPESAFE_BY_RCU Date: Fri, 21 Sep 2018 17:13:31 +0200 Message-Id: <9ea379b38a763adeae0e43638a9769c96eea767f.1537542735.git.andreyknvl@google.com> X-Mailer: git-send-email 2.19.0.444.g18242da7ef-goog In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org An object constructor can initialize pointers within this objects based on the address of the object. Since the object address might be tagged, we need to assign a tag before calling constructor. The implemented approach is to assign tags to objects with constructors when a slab is allocated and call constructors once as usual. The downside is that such object would always have the same tag when it is reallocated, so we won't catch use-after-frees on it. Also pressign tags for objects from SLAB_TYPESAFE_BY_RCU caches, since they can be validy accessed after having been freed. Signed-off-by: Andrey Konovalov --- mm/slab.c | 2 +- mm/slub.c | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index 6fdca9ec2ea4..fe0ddf08aa2c 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -2574,7 +2574,7 @@ static void cache_init_objs(struct kmem_cache *cachep, for (i = 0; i < cachep->num; i++) { objp = index_to_obj(cachep, page, i); - kasan_init_slab_obj(cachep, objp); + objp = kasan_init_slab_obj(cachep, objp); /* constructor could break poison info */ if (DEBUG == 0 && cachep->ctor) { diff --git a/mm/slub.c b/mm/slub.c index c4d5f4442ff1..75fc76e42a1e 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1413,16 +1413,17 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s, #endif } -static void setup_object(struct kmem_cache *s, struct page *page, +static void *setup_object(struct kmem_cache *s, struct page *page, void *object) { setup_object_debug(s, page, object); - kasan_init_slab_obj(s, object); + object = kasan_init_slab_obj(s, object); if (unlikely(s->ctor)) { kasan_unpoison_object_data(s, object); s->ctor(object); kasan_poison_object_data(s, object); } + return object; } /* @@ -1530,16 +1531,16 @@ static bool shuffle_freelist(struct kmem_cache *s, struct page *page) /* First entry is used as the base of the freelist */ cur = next_freelist_entry(s, page, &pos, start, page_limit, freelist_count); + cur = setup_object(s, page, cur); page->freelist = cur; for (idx = 1; idx < page->objects; idx++) { - setup_object(s, page, cur); next = next_freelist_entry(s, page, &pos, start, page_limit, freelist_count); + next = setup_object(s, page, next); set_freepointer(s, cur, next); cur = next; } - setup_object(s, page, cur); set_freepointer(s, cur, NULL); return true; @@ -1561,7 +1562,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) struct page *page; struct kmem_cache_order_objects oo = s->oo; gfp_t alloc_gfp; - void *start, *p; + void *start, *p, *next; int idx, order; bool shuffle; @@ -1613,13 +1614,16 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) if (!shuffle) { for_each_object_idx(p, idx, s, start, page->objects) { - setup_object(s, page, p); - if (likely(idx < page->objects)) - set_freepointer(s, p, p + s->size); - else + 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); } - page->freelist = fixup_red_left(s, start); + start = fixup_red_left(s, start); + start = setup_object(s, page, start); + page->freelist = start; } page->inuse = page->objects; -- 2.19.0.444.g18242da7ef-goog