Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10634894imu; Thu, 6 Dec 2018 04:28:21 -0800 (PST) X-Google-Smtp-Source: AFSGD/XW2IEN4JjwL+If6Mq0pf5VLGxt/Bnm5lMhWS49ObXZXVGuFewqz3B5OqfE/GtRioLIH5Lc X-Received: by 2002:a63:d70e:: with SMTP id d14mr24146329pgg.159.1544099301664; Thu, 06 Dec 2018 04:28:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544099301; cv=none; d=google.com; s=arc-20160816; b=NptfcIALqyVNEHDEMFqD9WT+4uXRN6RmUpCEI+md2HLqUH8E/czOjKzewEhYPBB80q 5jVlSbfM6ifpMqqi+F6fGYIgCUSImBAr8KWZ1OqcLvt29QAXexkhxu6daS0yNhfVlo6y Q+H30Bn3afJysy4bXxViG1E9B4arv6nMJHSmDMxaeEX0UEi9amHVgrriRYa5j1ECguCR gWgIFmeIoNBoT6iCUTTokEoeLdh83JxrKu4W5VwyY7+EcPBnLC0yM6BHqpGdiqebGjFk aUSETV1N/jRTxBbthoOveywn+hFKpbW+3gWq4yIIEpoBfsn2Jvff8WLy8zYvBBhiiHyk WaCw== 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=sISq/ZW0TVJBTVtL8WDFsSQvFMzwvGrKZuo3jHEW0rs=; b=0/9DJhYGeE1XdgOd9TqJzDS6/HeA7jPxaClTUZgSxykzK5/MosTok+HWI8qOvPmvfe r2/Yfv1h7QMCJMltoGW4ySO+zx5oaykvATdN8zoblm3paPGPo1LgjOoUDFssSwJsw8/n e8OpyVRS7/9DEdEzvMGFFe5A8PPXE/vWdgChfL5+v3HbfNh4tTdmyje5U85P5NUc1p/W y/MBMj3Hm2Ou4HnA8wsvt7JOEafIPUTo7xBUv+um2hjW29huizpWAFM+tgNBxXqh7IOM g+tUleD3AHtX42MqrtgKMR/UfCSD7/5JP20fOtE80s9axh0/W3Wv+bb8+BA5S/mlY6FP 7a5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EIaG8FbV; 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 10si192551pgl.30.2018.12.06.04.28.05; Thu, 06 Dec 2018 04:28:21 -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=@google.com header.s=20161025 header.b=EIaG8FbV; 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 S1729926AbeLFM0l (ORCPT + 99 others); Thu, 6 Dec 2018 07:26:41 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:36116 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729681AbeLFMZK (ORCPT ); Thu, 6 Dec 2018 07:25:10 -0500 Received: by mail-wm1-f65.google.com with SMTP id a18so852282wmj.1 for ; Thu, 06 Dec 2018 04:25:08 -0800 (PST) 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=sISq/ZW0TVJBTVtL8WDFsSQvFMzwvGrKZuo3jHEW0rs=; b=EIaG8FbVvjfEZBwPVnSoxYhmFaPSG+vHC9ub8Q9UvflzLci7qWq1MIvHhHMYHp+qDe aYCb2A46h0HyozZYwd+MH09I2ZtN17SqYJSvjpXS3OWlMZyrAYkp8oRdU0Rvsn1ii2PH hgxdrby1RcW8uBjfYxUDDMAMeg/GAfy+QZp72weuTc9DFFG8WxesnWT3ce5AlEqG1XPu NUxxu1xI3NGg4i+djtH88GpUdqSqviOaUnu2vrK/1hHU/TEC5YZsyp6TnaTdAvOQTaUe zkHqUUKT1p1wYmKjUILEhMirxb9nhAqh+8GNSNz+rwKkyZke8WRAAJA6DaJN86coRzJI SjIA== 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=sISq/ZW0TVJBTVtL8WDFsSQvFMzwvGrKZuo3jHEW0rs=; b=Jn2nwR/RxKUsTIO04mvKW700XbJU8vASaiIyI001imhFbwAN3LMy/AYZxJ+RTNXjCG luOWXzqv1mDm6VLtT5Uq0HSRwl8Mi496+jjIrI7ZlGA/shXkalKmDQjMmnq7oGQWrRYC KVonX9prEatb8WTRbGuG+ICuRx5bOGMRnGz3E6ig0pWNYC2SfcrF9p1AnBmV4iJQdBJw lhV+KVx1t+WGlDA6ukblGpup8lvT8g9wv1rXTfNkatTjtq8+G587YgC+XQf+Sbk0Vygk WTMoRdDnn3ZWbZACJdwG8EPjEnjenfrw8pTn9mzVJfkp9oW2lDANB1tfzYAQa8qPeaCA rdow== X-Gm-Message-State: AA+aEWYXr7q/kgiueKWXqj15zBeeigbgMrlrDVqlQhqTKXkzGLO7is+s f3U52qK10yJ0Ezokw5MWG7PIqA== X-Received: by 2002:a1c:e088:: with SMTP id x130mr18266740wmg.23.1544099108033; Thu, 06 Dec 2018 04:25:08 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:3180:41f8:3010:ff61]) by smtp.gmail.com with ESMTPSA id j8sm339988wrt.40.2018.12.06.04.25.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Dec 2018 04:25:07 -0800 (PST) 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 v13 12/25] kasan: preassign tags to objects with ctors or SLAB_TYPESAFE_BY_RCU Date: Thu, 6 Dec 2018 13:24:30 +0100 Message-Id: X-Mailer: git-send-email 2.20.0.rc1.387.gf8505762e3-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. Reviewed-by: Andrey Ryabinin Reviewed-by: Dmitry Vyukov 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 26f60a22e5e0..27859fb39889 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 e739d46600b9..08740c3f3745 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1451,16 +1451,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; } /* @@ -1568,16 +1569,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; @@ -1599,7 +1600,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; @@ -1651,13 +1652,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.20.0.rc1.387.gf8505762e3-goog