From patchwork Thu Aug 9 19:21:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10561821 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 68D4A1057 for ; Thu, 9 Aug 2018 19:23:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 567D926490 for ; Thu, 9 Aug 2018 19:23:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 48F2F2B907; Thu, 9 Aug 2018 19:23:24 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id F0CC326490 for ; Thu, 9 Aug 2018 19:23:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Ad7TZdZNP8sDIRCL7D7+RExOrOE5006I9o79VVi+tbg=; b=HIiOG4QSYfrh5T ql0wA7Z5CrR9Rys/skFksm0A/YHca0REOxI2oTtAwpl5UpKwMiU4bwm2SZu8+GvlGJCzG0wXCGTqn reIcbYDseFque7bXawnIAR0mbwu2VXkgD9n51WqUt+zh4TlgOFlb4TkinvqJe74EIm5Hlnt45xME+ 30ggMvVlez+wpFeozvRmwYOVYG6PEIz8/KvYLdjvHqPbUVLvPoj8W0jAW9M7mL3Zu0PtoYR3eyJ7l pYrCFnFPWeK8nHWrYdx6KO4Ucupca8l0lkkRGmjffAMwRAia6DXWfAtnNyhkzbxdvALP2A3KeZ0Aq Fbk1gE7DN0ePxlp3xIIA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fnqWg-00029f-KL; Thu, 09 Aug 2018 19:23:14 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fnqV6-00007E-LG for linux-arm-kernel@lists.infradead.org; Thu, 09 Aug 2018 19:21:40 +0000 Received: by mail-wr1-x443.google.com with SMTP id u12-v6so6089905wrr.4 for ; Thu, 09 Aug 2018 12:21:29 -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=VF0nRIiNwsRgtxZxCam3wcoWu6FgQBt9JWN6ZbYEIxE=; b=QpCmIiPMHe6+oM4RZShxaxZZNbTQRBzBBSRaY11nA6pHh6Cplzw0n7Bv2ZKvLSc+Z4 4UHKKdRN140+D0usZ8bvkHnWnG8txA0JlSKLpWAQfuza/22n8fTJiK0/0pYWU3wYztfI imwCswDdjh8G72+vHnwhhEr+krHLWPfXvoJeBVl8horIV9DgQ3aUKbkBKZWci/UtBDYA PL0EQx54VrD05gyV+JikLNQvat9nDkHWRVe9t+VM5PtRt2mCIrCx1YRfqVdZ9scLkicl DbwqIS5YQJecOXU2pSRkfwTgR/fmR0bIo8aT7DX//C7AsNUp8s3K7DafoAwaXJJicyGk TwKA== 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=VF0nRIiNwsRgtxZxCam3wcoWu6FgQBt9JWN6ZbYEIxE=; b=VQUqmpnN2+7FKtcY6Ygtv1yqH6ZecA6YaKuTv8QAzraPb5D5EIkR5t3hBFkag/FrES 6v3T3mOnM8pHfxa7zfyN1cRURHZOgJ4Veqmnw1g6MDrTJ+yV/A0bFiQWSe0Xx5/dfT7W OksyMRIRaBFNb8YWi8tNaxrUwLkZPsmaGhRRibtp6BM0I80FQsXwFso/8D4fWAy0SDRa EcNp0l/983qJ/fMI9Z0cTCc3k13XWuFViKRZqRj5guONz5QHMSMjI6j0B2MrTNO+umnZ 7Mq3UJeNsVJRxtgy9bwo3o9CCmrksNXo7+GFBubsBuvyn7XUlHpcVv3Z7VUhVLlVDUHk ou8g== X-Gm-Message-State: AOUpUlHxAfg4ZC1UxTTKQS1OZTFZlmja9JWvofUztwrCo0f9dZpWgCAZ TmesUWKgmubwOGIOC3uyhxvqfg== X-Google-Smtp-Source: AA+uWPxhYsId7VaUHrC78gT2n+1+K1ulTL3bbqXgHVcHJ0gFRtLR022OJpGJB7i55vRAGW0nyD8Atg== X-Received: by 2002:a5d:6401:: with SMTP id z1-v6mr2271039wru.64.1533842488063; Thu, 09 Aug 2018 12:21:28 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id o14-v6sm14738797wmd.35.2018.08.09.12.21.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Aug 2018 12:21:27 -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 Subject: [PATCH v5 08/18] khwasan: preassign tags to objects with ctors or SLAB_TYPESAFE_BY_RCU Date: Thu, 9 Aug 2018 21:21:00 +0200 Message-Id: <625d42d5cb7f20bb54ce7af2c4b87910b1474c74.1533842385.git.andreyknvl@google.com> X-Mailer: git-send-email 2.18.0.597.ga71716f1ad-goog In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180809_122137_048106_223CFD7A X-CRM114-Status: GOOD ( 17.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vishwath Mohan , Chintan Pandya , Jacob Bramley , Jann Horn , Ruben Ayrapetyan , Andrey Konovalov , Lee Smith , Kostya Serebryany , Mark Brand , Ramana Radhakrishnan , Evgeniy Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org 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. Signed-off-by: Andrey Konovalov --- mm/slab.c | 6 +++++- mm/slub.c | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mm/slab.c b/mm/slab.c index 6fdca9ec2ea4..3b4227059f2e 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -403,7 +403,11 @@ static inline struct kmem_cache *virt_to_cache(const void *obj) static inline void *index_to_obj(struct kmem_cache *cache, struct page *page, unsigned int idx) { - return page->s_mem + cache->size * idx; + void *obj; + + obj = page->s_mem + cache->size * idx; + obj = khwasan_preset_slab_tag(cache, idx, obj); + return obj; } /* diff --git a/mm/slub.c b/mm/slub.c index 8fa21afcd3fb..a891bc49dc38 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1532,12 +1532,14 @@ 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 = khwasan_preset_slub_tag(s, 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 = khwasan_preset_slub_tag(s, next); set_freepointer(s, cur, next); cur = next; } @@ -1614,8 +1616,10 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) shuffle = shuffle_freelist(s, page); if (!shuffle) { + start = khwasan_preset_slub_tag(s, start); for_each_object_idx(p, idx, s, start, page->objects) { setup_object(s, page, p); + p = khwasan_preset_slub_tag(s, p); if (likely(idx < page->objects)) set_freepointer(s, p, p + s->size); else