From patchwork Tue Nov 6 17:30:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10670943 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 42E8413BF for ; Tue, 6 Nov 2018 17:31:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A3172ABCA for ; Tue, 6 Nov 2018 17:31:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1CA4A2ABD4; Tue, 6 Nov 2018 17:31:22 +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=unavailable 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 97B1C2ABCA for ; Tue, 6 Nov 2018 17:31:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B484F6B035D; Tue, 6 Nov 2018 12:31:00 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AFAFA6B035E; Tue, 6 Nov 2018 12:31:00 -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 973526B035F; Tue, 6 Nov 2018 12:31:00 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by kanga.kvack.org (Postfix) with ESMTP id 409886B035D for ; Tue, 6 Nov 2018 12:31:00 -0500 (EST) Received: by mail-wr1-f72.google.com with SMTP id 37-v6so12407149wrb.15 for ; Tue, 06 Nov 2018 09:31:00 -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=T48YNHXSNi4eeu2bzn4PMsbpUkqEeLL24cz/k9DQXN4=; b=mD6Kpzev24OdskLrQfQ8kXvr7S8QQk2V96o0hG4dg6qqIOEyyBfxJMg8qpGhBU7ShN wdS9ga5pD7JIjW/bz0rdB0gQ2GK9fDA56kzTUze11TOSOXW/DcmvlLsrkGb/f/WrgX/8 lFoRniHkijS4KRcUKjB4VMEbMepE1FjdgKhpDQIx/UqLYUXLWyJpvGnprrTUEkkXzH0H 1db72iacxU0zxOzgxOA5nM/Jg1gxW8S7MDVdKrhcqlK4CtqmjMHN4YtHtnupIVeQZ1p7 iDybKpD6UTlLVAJBzy+ZWaFIRMfxA4y9C+Vgi8EueJkZDQ5Y+zGm78EL/12rcs2X/+0Y NFZA== X-Gm-Message-State: AGRZ1gIb+Un0/a29BLGs6I8LigUAeXbQT8EZMe8euhvzyKe0ztGwWjyS /LVfWTQ4Qcw9abts01DMkelWa1t4X/6bQ5pBVPblQpSH1kIuh9MKsBbN2SddlhoetB+NjNqR5YY E5nsZxIooHiry+nZMoNvmsNoX2l3I1CXEJBNtw9pOOX8cp6NiVXzmPpwBeTM0kYEqRIHZkKftMt S78ck3SWzJcVIdik/Yn5VIexUYph3yKW2yYAJ2SeIBQjGDEcpGTCBXgMVEV8fUBtD0PNGar1Vqn vPkLCXC8dPg8iHYsfEEDJDTQ/dazIPhCTzDesCdIcKPmZ/UygLVoragnCxKEmmU2sE/892nFvbF W89kWjPrLUcYjt0uxqO+YNdNaijR3GCa8/7z9YjnVHMzbLCsetJsHsnH8yZIYsPb2PiIvadZrIW f X-Received: by 2002:a5d:6a8d:: with SMTP id s13-v6mr9397405wru.179.1541525459792; Tue, 06 Nov 2018 09:30:59 -0800 (PST) X-Received: by 2002:a5d:6a8d:: with SMTP id s13-v6mr9397334wru.179.1541525458573; Tue, 06 Nov 2018 09:30:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541525458; cv=none; d=google.com; s=arc-20160816; b=dKsgdBUfrcC06TVC/Y1LtGdT2/AQGSohrhTxohvb3o5NfKSUBIpyTH36URM1C4UHUg SKz+NCEr4vEj4AynFiUiUXU4/5UtscWLgir7m1ezDeOfnOdSofHwrrhjyyoJj/vaKk89 8HosQi5fzDpqR+JD93WoqAd/R1bMCNvUNmz8rCGFdWfexLPQowxchgKN/3jKtAG+6Yyp DRaFWi/3JRb+DDRIPTf057JQj/SYrm0862jC+lVH+FOeAxTOSe2VO5p8UYa9V9auBMOU +ebzdEBszFIjYRNgM55v6Nt4Qp1Vem8uYCsYZgu9331NXzx0zGfixUM86jGzSJUcQpnH yMzg== 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=T48YNHXSNi4eeu2bzn4PMsbpUkqEeLL24cz/k9DQXN4=; b=Dbh4vWtloNXm5fCxTcqXufMesgW+3/zi+s7wIgeTWUv10HXaf317iLjwqm5H/w9kpW HkMASVajYmmBfxUlnANzG8DB+xsuuAlftWFGrUMF6QXEvKeu8rE0S6gSv7Diy7pHGy9J Q2Pt4IEBtb5lQ+aW068CCsLkctnVDdxDBSZj5q7Ab2eIaCR83BFyjAt1wz3U5ZxKOKvU safXwbHeQU12rA8IyfWeJ8FFMPV8/8MXegCmt+mf4rwSgE3hAC3ueZ5apyIHCk5C+lWW +WkL6vsuzX3qZRfijVlVINl2MP7Uy04Cs1x/U9wzpq4dlZ/PeKVkei5RMMWtak0n+MuO fNcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=loc8gPpA; 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 p127-v6sor1697443wmd.28.2018.11.06.09.30.58 for (Google Transport Security); Tue, 06 Nov 2018 09:30:58 -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=loc8gPpA; 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=T48YNHXSNi4eeu2bzn4PMsbpUkqEeLL24cz/k9DQXN4=; b=loc8gPpA6BsyBfsLQZ1xJJK8SKgPFvND8p3yzI24gRT2R2tnyR8tmqImgBFESqB8u6 PsKYO6u6js23G9tUx/zXdhJPXl+StChQkny+zenERtzcQKXZX8ErExSxCBBX0yJgxY9q ixayJsgrtswgOvbM6wpAqylRzFKJAPeUvLDsu3CzamxYGtDIKdwUQIQqlWUq2a9OtTnM YJ3/ly3iHAfyh/CxczjrHcrrDPYqCFrsso3DtMOk5fQuf3lw29WfInkABtCJROdU18/4 tb44qp9/xEdYSkh/b4qqUwewkJgv1o4YKZIPZTgkYWer1OOoqUZO6eD68PC/TJpFaWzl /5Zg== X-Google-Smtp-Source: AJdET5cxhc/meAOXt3ESBWUaU6vT9ON5JhSvHyq74Qc/JQ1ollGs24FteiQAUYp/Kw4/4V5LhOwRHw== X-Received: by 2002:a1c:d78e:: with SMTP id o136-v6mr2759533wmg.124.1541525458005; Tue, 06 Nov 2018 09:30:58 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id z17-v6sm16328416wrm.65.2018.11.06.09.30.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 09:30:57 -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 v10 10/22] kasan: preassign tags to objects with ctors or SLAB_TYPESAFE_BY_RCU Date: Tue, 6 Nov 2018 18:30:25 +0100 Message-Id: X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0-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 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;