From patchwork Tue Jun 8 18:39:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 12307929 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D3EEC4743D for ; Tue, 8 Jun 2021 18:40:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0F9CF6148E for ; Tue, 8 Jun 2021 18:40:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0F9CF6148E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 539E06B0070; Tue, 8 Jun 2021 14:40:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4EC006B0071; Tue, 8 Jun 2021 14:40:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2ED776B0073; Tue, 8 Jun 2021 14:40:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0141.hostedemail.com [216.40.44.141]) by kanga.kvack.org (Postfix) with ESMTP id DB1A76B0072 for ; Tue, 8 Jun 2021 14:40:00 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 822AE75A7 for ; Tue, 8 Jun 2021 18:40:00 +0000 (UTC) X-FDA: 78231420960.29.ABEF5EF Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by imf25.hostedemail.com (Postfix) with ESMTP id 3A0B06000143 for ; Tue, 8 Jun 2021 18:39:57 +0000 (UTC) Received: by mail-pj1-f45.google.com with SMTP id g4so2983012pjk.0 for ; Tue, 08 Jun 2021 11:39:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yzft9dd0vSeYhbqzTlKmJNFC+BFjIqdFg8djl4vNOCs=; b=fKMg+T/GcvrJgmMBWMGLmhO7JM2Xskyd6kNdHclQ3ImCktIasIAU5yt+BTLNsoSOOJ h24tzt0WugDC+e7wiA/4raF3/lwiO3VRJffQ2Y2Yat7/sOR/YDFfd85fZi0J1akU5ll9 fCYo/kHqTICHDpRz99Q5bvKDHAId4ERFLYEXQ= 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=yzft9dd0vSeYhbqzTlKmJNFC+BFjIqdFg8djl4vNOCs=; b=F5WMmydnYsdIOth2+vHQs2w5/z8XO7I4VHqMVjyri7xsiNcKypY5Hix3LvJvbaILF2 GbXx9LJY7qREr0Z3pQYZFGxBkPdmZaWVob92CsPIPY39tulHwaWVeuwjpvyzeZtLua+W nk0QpbBXd2yXFUROr3k/vlpOg9eRycWcXN0l8SpHaQRxgtGM/dYKUJPHQgSfJk/8yDgU VOVVKM++XpFB1nE/xhTfaOzM36Hdil9lm8N9KUbHQVUYpSEhx2aY6zTvsVnmqA/YzdYR B5avsReAII1JoGKn27OoVCX1sexM3rOIRo0/jtCPuDxDPPdFebeaQaTKBGlUkwF/tCaS ePIA== X-Gm-Message-State: AOAM532XSrk0W+5uaYNEODfa6HfohwOxnVK0U3R7XckEiOpbc6q9rd2C Wy8O1jjresu2roJAz5yeYXQwWA== X-Google-Smtp-Source: ABdhPJwS3KPZAsqhT8J29mTedkNx6p7Ke/oarm+JiBj6cVpii3q7g//5NCUpMwmE0mWzBsO5367gFQ== X-Received: by 2002:a17:90b:354:: with SMTP id fh20mr27778700pjb.67.1623177599221; Tue, 08 Jun 2021 11:39:59 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id x125sm11185015pfx.201.2021.06.08.11.39.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Jun 2021 11:39:57 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Vlastimil Babka , Marco Elver , Christoph Lameter , "Lin, Zhenpeng" , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 1/3] mm/slub: Clarify verification reporting Date: Tue, 8 Jun 2021 11:39:53 -0700 Message-Id: <20210608183955.280836-2-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210608183955.280836-1-keescook@chromium.org> References: <20210608183955.280836-1-keescook@chromium.org> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; g=178be2f9d2e784ee9bf12a0f196176c115be9fbb; i=U+vADmko1G4lZ39LsoUZb95Wrx2rzv1r6guaqRqH67M=; m=KNPYS4ZBPM+uSQxSrL5WKoyUoDfTLfQIvACRYbVbr6o=; p=oVLORPkeeRwgt0gc55edlRvCMc3IvI98MP3RDST0Gds= X-Patch-Sig: m=pgp; i=keescook@chromium.org; s=0x0x8972F4DFDC6DC026; b=iQIzBAABCgAdFiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAmC/uXoACgkQiXL039xtwCZwew/+M13 4wKVrclvxFWQ7U4l1afq0WF/fGE8hgsFeBYQox+dbgIZbDFA56wtNup/udYkNnkiz1muhxBERvtpx V/1Ap4LRawWE41JgshDMfF7c8rJRoy+lolYEjMbXFvs/YxH6TSqFqHXMfy70QRfraczox8kT8mwaB 3l6enWQxhMCeTygt27lZ+F417VXJD/bM1zyLrC3FE0JJ9dJ/uuiE7ZzTMY8DMLgpu+VoSwNqWL4zR lUfNYyxwQz5/3puRkpungw97NK5d14+Ug+tcWImkEGsbPD2iD6Rlra8JVpGa5XTSsqlBOu8Mrqo/q UmqQalgmoWOamnvCiAaBI24YHpLU8pQq178JqK2O0owPhbC8mlNpTEVgZ43GF1hkDhhPQ/QvwRz0Z XC+1WyhwjUT9FRHHhPhRWOgxpY5Q8illryl21dK+9IbuLYRyX1s6Nh4fERJRqltrb6hKHePAUbGzD i5Nh9dwYX4skY48Oui7IL5I+KZsycdwLSXDfQNvN+MmqVmN35gOMJYvq6Kd+LFCDwzAamQyyXUTPp OkUA89+s5SQ958kNjTvicCs7xe5hmRareo/aslj2P9P//WAzu0x0AjGvBXx9/NZ9f0z9/spWcxI4+ Qtjy5K0z/S5nGC6E4tBifF99GPbi0/Y3ge/0NMroeVZrLwbkWQV2KuCPMP/hB14c= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3A0B06000143 X-Stat-Signature: k5mxz38wu4nwsdycizhrgus9yjejh3ji Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="fKMg+T/G"; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf25.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.45 as permitted sender) smtp.mailfrom=keescook@chromium.org X-HE-Tag: 1623177597-567179 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: Instead of repeating "Redzone" and "Poison", clarify which sides of those zones got tripped. Additionally fix column alignment in the trailer. Before: BUG test (Tainted: G B ): Redzone overwritten ... Redzone (____ptrval____): bb bb bb bb bb bb bb bb ........ Object (____ptrval____): f6 f4 a5 40 1d e8 ...@.. Redzone (____ptrval____): 1a aa .. Padding (____ptrval____): 00 00 00 00 00 00 00 00 ........ After: BUG test (Tainted: G B ): Right Redzone overwritten ... Redzone (____ptrval____): bb bb bb bb bb bb bb bb ........ Object (____ptrval____): f6 f4 a5 40 1d e8 ...@.. Redzone (____ptrval____): 1a aa .. Padding (____ptrval____): 00 00 00 00 00 00 00 00 ........ The earlier commits that slowly resulted in the "Before" reporting were: d86bd1bece6f ("mm/slub: support left redzone") ffc79d288000 ("slub: use print_hex_dump") 2492268472e7 ("SLUB: change error reporting format to follow lockdep loosely") Signed-off-by: Kees Cook Acked-by: Vlastimil Babka Link: https://lore.kernel.org/lkml/cfdb11d7-fb8e-e578-c939-f7f5fb69a6bd@suse.cz/ --- Documentation/vm/slub.rst | 10 +++++----- mm/slub.c | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Documentation/vm/slub.rst b/Documentation/vm/slub.rst index 03f294a638bd..d3028554b1e9 100644 --- a/Documentation/vm/slub.rst +++ b/Documentation/vm/slub.rst @@ -181,7 +181,7 @@ SLUB Debug output Here is a sample of slub debug output:: ==================================================================== - BUG kmalloc-8: Redzone overwritten + BUG kmalloc-8: Right Redzone overwritten -------------------------------------------------------------------- INFO: 0xc90f6d28-0xc90f6d2b. First byte 0x00 instead of 0xcc @@ -189,10 +189,10 @@ Here is a sample of slub debug output:: INFO: Object 0xc90f6d20 @offset=3360 fp=0xc90f6d58 INFO: Allocated in get_modalias+0x61/0xf5 age=53 cpu=1 pid=554 - Bytes b4 0xc90f6d10: 00 00 00 00 00 00 00 00 5a 5a 5a 5a 5a 5a 5a 5a ........ZZZZZZZZ - Object 0xc90f6d20: 31 30 31 39 2e 30 30 35 1019.005 - Redzone 0xc90f6d28: 00 cc cc cc . - Padding 0xc90f6d50: 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZ + Bytes b4 (0xc90f6d10): 00 00 00 00 00 00 00 00 5a 5a 5a 5a 5a 5a 5a 5a ........ZZZZZZZZ + Object (0xc90f6d20): 31 30 31 39 2e 30 30 35 1019.005 + Redzone (0xc90f6d28): 00 cc cc cc . + Padding (0xc90f6d50): 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZ [] dump_trace+0x63/0x1eb [] show_trace_log_lvl+0x1a/0x2f diff --git a/mm/slub.c b/mm/slub.c index 3f96e099817a..f91d9fe7d0d8 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -712,15 +712,15 @@ static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p) p, p - addr, get_freepointer(s, p)); if (s->flags & SLAB_RED_ZONE) - print_section(KERN_ERR, "Redzone ", p - s->red_left_pad, + print_section(KERN_ERR, "Redzone ", p - s->red_left_pad, s->red_left_pad); else if (p > addr + 16) print_section(KERN_ERR, "Bytes b4 ", p - 16, 16); - print_section(KERN_ERR, "Object ", p, + print_section(KERN_ERR, "Object ", p, min_t(unsigned int, s->object_size, PAGE_SIZE)); if (s->flags & SLAB_RED_ZONE) - print_section(KERN_ERR, "Redzone ", p + s->object_size, + print_section(KERN_ERR, "Redzone ", p + s->object_size, s->inuse - s->object_size); off = get_info_end(s); @@ -732,7 +732,7 @@ static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p) if (off != size_from_object(s)) /* Beginning of the filler is the free pointer */ - print_section(KERN_ERR, "Padding ", p + off, + print_section(KERN_ERR, "Padding ", p + off, size_from_object(s) - off); dump_stack(); @@ -909,11 +909,11 @@ static int check_object(struct kmem_cache *s, struct page *page, u8 *endobject = object + s->object_size; if (s->flags & SLAB_RED_ZONE) { - if (!check_bytes_and_report(s, page, object, "Redzone", + if (!check_bytes_and_report(s, page, object, "Left Redzone", object - s->red_left_pad, val, s->red_left_pad)) return 0; - if (!check_bytes_and_report(s, page, object, "Redzone", + if (!check_bytes_and_report(s, page, object, "Right Redzone", endobject, val, s->inuse - s->object_size)) return 0; } else { @@ -928,7 +928,7 @@ static int check_object(struct kmem_cache *s, struct page *page, if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) && (!check_bytes_and_report(s, page, p, "Poison", p, POISON_FREE, s->object_size - 1) || - !check_bytes_and_report(s, page, p, "Poison", + !check_bytes_and_report(s, page, p, "End Poison", p + s->object_size - 1, POISON_END, 1))) return 0; /* From patchwork Tue Jun 8 18:39:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 12307927 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D693CC47082 for ; Tue, 8 Jun 2021 18:40:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5C6E761613 for ; Tue, 8 Jun 2021 18:40:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5C6E761613 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0CD616B006E; Tue, 8 Jun 2021 14:40:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 07F986B0070; Tue, 8 Jun 2021 14:40:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE95A6B0073; Tue, 8 Jun 2021 14:40:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0116.hostedemail.com [216.40.44.116]) by kanga.kvack.org (Postfix) with ESMTP id 91C436B006E for ; Tue, 8 Jun 2021 14:40:00 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 333FB181AEF2A for ; Tue, 8 Jun 2021 18:40:00 +0000 (UTC) X-FDA: 78231420960.39.C188E3E Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf14.hostedemail.com (Postfix) with ESMTP id 46DDFC00CBDC for ; Tue, 8 Jun 2021 18:39:56 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id mp5-20020a17090b1905b029016dd057935fso6439562pjb.5 for ; Tue, 08 Jun 2021 11:39:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WlWwfWm11ZwbBadKiVuyPJC6XajjDuI6+AslLyW5z08=; b=QbVPX1GwRQCu14OD+ZSs2LvfKRFfPgURgMXB+3uhrM/YY9d5aFspnwPaBDccVOrIo3 M/ZaPHnxbHRRFmowOSMtUgWnXm5nZFjOXby4MFQMPrwDLtFIuAZtOBAm93mdLHCsqb/Q 8okr0y42mrVxXiHgVb+CdogkSVVsRGOhRyTxE= 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=WlWwfWm11ZwbBadKiVuyPJC6XajjDuI6+AslLyW5z08=; b=Y+7r8/r2N9C+asNX1/P7bslbVFLrUzsgMbTo7kMxVQTBmeLiadNf5DatoFvsVdBOUq t56fVQb96KUDAW9q1/F1IE56Gg2c61Zp6k4/a2Z/RZuptFkzppiZiC2kbjdnbwbmPiEc LF3ohPSMtYFg47VTrk/sm/6t3A0jWabUMo0NMwdIi3WT0iOvV8XlNEmwlW9VSBVGP7UM 3Mcwn6mHib8WDwr7PO8jgkV+zFbj5ozYTWqzJHZpWv1CAOjIRUSxVi0clfoyc4JB0V8g 9wvFU+3kQRsJHyj8zMpO4MEdam1up+QWBwvRQnvAY4cM+IHLpGWFpSXF3y5rXvOd4jab wgVw== X-Gm-Message-State: AOAM5327zz5b+by43R9L8tHSl4ccttluR8V8C6i+lamIRQpQfGu5R0nH jLUCEfrPe1BTy5Q6U9VdiZD8wg== X-Google-Smtp-Source: ABdhPJzqy4n4WdYTLIGeM6Q8LDAjzOhL4aPsd5U04TX5u6JdmHUAEsG/77ua5jo+MiWEvpvtD6yw4A== X-Received: by 2002:a17:902:c1d2:b029:101:656b:8c06 with SMTP id c18-20020a170902c1d2b0290101656b8c06mr1015590plc.77.1623177598937; Tue, 08 Jun 2021 11:39:58 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c5sm9354668pfn.144.2021.06.08.11.39.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Jun 2021 11:39:58 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , stable@vger.kernel.org, Vlastimil Babka , Marco Elver , Christoph Lameter , "Lin, Zhenpeng" , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 2/3] mm/slub: Fix redzoning for small allocations Date: Tue, 8 Jun 2021 11:39:54 -0700 Message-Id: <20210608183955.280836-3-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210608183955.280836-1-keescook@chromium.org> References: <20210608183955.280836-1-keescook@chromium.org> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; g=4d07ab25d78303866d0d97043ac7d3b3c89a188b; i=/4wP7Z7hf8NRJqx5cO57ZcO310lU091PUfj+vstIw5Q=; m=ZkE5m/qyaah1o2CK6u/oGOeYjX1Qohn3GAXR8LvKXFY=; p=QHACCNDXDPZn3tQzC59FK9Xid+U/e7qsN2m1YshAXh8= X-Patch-Sig: m=pgp; i=keescook@chromium.org; s=0x0x8972F4DFDC6DC026; b=iQIzBAABCgAdFiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAmC/uXsACgkQiXL039xtwCZiyRAAlgt u28QImlU8Sk7zOkeBTH8PG+BF6ltMWSFMOWluk38QI6ysgrJyZuz8nPqyhlY4LbKmPZlM5dDzLrEl WFazxogGESoFjztYt2+fvWgirR7F4Ovi7zR3GOPg/U8Z74iwOWWjlL4Ps59MVV/4MtPihc+t6rtLS el+ck+DdHro3yhcPxc55bj2WQEoz5L+fXpqDzijkcDiSYKu8oYfRKNAXM4zjUPbuEebemG7C5W8ir gm5za0N1wQMZW6NnS4/7IQGfMkxVlrNsRHE8G34iWXP669r75O7EMGkfAoBmCh1SitVfGLkOhYWj3 YHDRwVy0ymZEpifJNKrxfokXFfNXSN9hrKi/vjSLA1alN8WFHkduOBu/daDHCsOmC9cbcYWuX7sEd nrQGxfzVvX3SYAOwTkbpxstr3E1QBwCMr+YzUYWNZr9ttwA1YAiWsZYMBnI6V1OY6mTmWJWPMy9fs bUSkP5OxEMCDR+DlHXFB7l6Q8RokenOAtQZlloq/rO0YAjDOJ+qe51052Iq5bbPB0ISUcIANpyruz R8xovxgPxcIKTYwaISm3YbbyxU44O9ZjZoTK53DHKQiCGXtOSTKXs3bO9Xvl3U6KuSuu++JONo0xY q9NXQPUYRi7+o754GbK2a5C5BAJQwms6BS5Q2uVWSOe3ji3RCxhTfQRuUkxHE5pY= Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=QbVPX1Gw; spf=pass (imf14.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.47 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Stat-Signature: dgaxqwy15pijxngfrtpwd5jmeipyrhzh X-Rspamd-Queue-Id: 46DDFC00CBDC X-Rspamd-Server: rspam06 X-HE-Tag: 1623177596-196683 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: The redzone area for SLUB exists between s->object_size and s->inuse (which is at least the word-aligned object_size). If a cache were created with an object_size smaller than sizeof(void *), the in-object stored freelist pointer would overwrite the redzone (e.g. with boot param "slub_debug=ZF"): BUG test (Tainted: G B ): Right Redzone overwritten ----------------------------------------------------------------------------- INFO: 0xffff957ead1c05de-0xffff957ead1c05df @offset=1502. First byte 0x1a instead of 0xbb INFO: Slab 0xffffef3950b47000 objects=170 used=170 fp=0x0000000000000000 flags=0x8000000000000200 INFO: Object 0xffff957ead1c05d8 @offset=1496 fp=0xffff957ead1c0620 Redzone (____ptrval____): bb bb bb bb bb bb bb bb ........ Object (____ptrval____): f6 f4 a5 40 1d e8 ...@.. Redzone (____ptrval____): 1a aa .. Padding (____ptrval____): 00 00 00 00 00 00 00 00 ........ Store the freelist pointer out of line when object_size is smaller than sizeof(void *) and redzoning is enabled. Additionally remove the "smaller than sizeof(void *)" check under CONFIG_DEBUG_VM in kmem_cache_sanity_check() as it is now redundant: SLAB and SLOB both handle small sizes. (Note that no caches within this size range are known to exist in the kernel currently.) Fixes: 81819f0fc828 ("SLUB core") Cc: stable@vger.kernel.org Signed-off-by: Kees Cook --- mm/slab_common.c | 3 +-- mm/slub.c | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index a4a571428c51..7cab77655f11 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -97,8 +97,7 @@ EXPORT_SYMBOL(kmem_cache_size); #ifdef CONFIG_DEBUG_VM static int kmem_cache_sanity_check(const char *name, unsigned int size) { - if (!name || in_interrupt() || size < sizeof(void *) || - size > KMALLOC_MAX_SIZE) { + if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) { pr_err("kmem_cache_create(%s) integrity check failed\n", name); return -EINVAL; } diff --git a/mm/slub.c b/mm/slub.c index f91d9fe7d0d8..f58cfd456548 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3734,15 +3734,17 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) */ s->inuse = size; - if (((flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) || - s->ctor)) { + if ((flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) || + ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) || + s->ctor) { /* * Relocate free pointer after the object if it is not * permitted to overwrite the first word of the object on * kmem_cache_free. * * This is the case if we do RCU, have a constructor or - * destructor or are poisoning the objects. + * destructor, are poisoning the objects, or are + * redzoning an object smaller than sizeof(void *). * * The assumption that s->offset >= s->inuse means free * pointer is outside of the object is used in the From patchwork Tue Jun 8 18:39:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 12307931 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40074C47082 for ; Tue, 8 Jun 2021 18:40:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EA25D614A5 for ; Tue, 8 Jun 2021 18:40:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EA25D614A5 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BF3C86B0071; Tue, 8 Jun 2021 14:40:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7EBF6B0072; Tue, 8 Jun 2021 14:40:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86CB66B0073; Tue, 8 Jun 2021 14:40:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0005.hostedemail.com [216.40.44.5]) by kanga.kvack.org (Postfix) with ESMTP id 398126B0071 for ; Tue, 8 Jun 2021 14:40:02 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id C63E298BA for ; Tue, 8 Jun 2021 18:40:01 +0000 (UTC) X-FDA: 78231421002.16.8A62BF3 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf27.hostedemail.com (Postfix) with ESMTP id BE9B28019358 for ; Tue, 8 Jun 2021 18:39:57 +0000 (UTC) Received: by mail-pl1-f173.google.com with SMTP id v11so2678336ply.6 for ; Tue, 08 Jun 2021 11:40:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Uwf15MYLwxoOU1qMfGZ6a+IKuws7ocGxDIxFsZ4OFug=; b=ZmhNQaEiRD+PqJ+xrZH8xI+iuIxofJdRY4W5A/5T7XGrwZdn/iYKbCqRjkoO6VyeJC 8iOK+dhExpTRhmv1VGWbtvi9J6JBjS6DXHKz9T69rq+p+sdxt0rTvOPLXXk01OUb6R0U HTSYdglOuWFKjcdgHPtRlL5/e5zFab4FEbXZ8= 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=Uwf15MYLwxoOU1qMfGZ6a+IKuws7ocGxDIxFsZ4OFug=; b=uAmibBC820QrlMjPEUAr4RCb8V2sQFN8yaoTtRNafdXSudizt/J3mCAec3phR9gOPx KwMkxra0+o6PjiwGHtwTdGAtviMC+jOkBgWrj8fjgALZ/8ezEoA5jTouyL5yuJ1yaBSv ESNs0NvKVPIrJ7GyCxT37mv4S39Ly99g08cNQ51PcTfmztFz9ckPHX3CbytEjMxUaQop XzLCYzJx8uvPdfLf/g9wukQAknZoVzPv8jxi3/hfvMsuuQI9Eg70tz+N3Ik5/Dp40xof rngMrhj/M7Id4je9q4Sv8d/UzZkuHMZQpD4wvdc4BH1SJChkoKYfXHp39jTfwBUN4PvV FBKQ== X-Gm-Message-State: AOAM530J30DysJu8jLogKT0z4drb4Sd4s7d/VTbdKOnbotEISB+nlWo+ GEU8dij1Xpz4Xv+to0JAZg5+iw== X-Google-Smtp-Source: ABdhPJyIRKmRRu2AYmJbx93jMDBAJ+npfATbrE3k5tSb2maqGENQ2XEQjv+jCNUyOXMkz7bKz3mtwA== X-Received: by 2002:a17:90a:cf12:: with SMTP id h18mr26723016pju.131.1623177600473; Tue, 08 Jun 2021 11:40:00 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id x2sm6930010pfp.155.2021.06.08.11.39.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Jun 2021 11:39:58 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Marco Elver , "Lin, Zhenpeng" , stable@vger.kernel.org, Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 3/3] mm/slub: Actually fix freelist pointer vs redzoning Date: Tue, 8 Jun 2021 11:39:55 -0700 Message-Id: <20210608183955.280836-4-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210608183955.280836-1-keescook@chromium.org> References: <20210608183955.280836-1-keescook@chromium.org> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; g=258aaf7fc047e39c606cfd78c6cb30ea0743ac4f; i=9r8xlkNM+5K11owYVXEwhB43FxFLIosDS833zHgjUAw=; m=Uy+f2vNqhL2FSdVsV3KF5XKnxC9QUfLtorR17aD8dZE=; p=6aeBvOTccD42eyT82fqBR4aM+xkklg7H28x6pgmR2f0= X-Patch-Sig: m=pgp; i=keescook@chromium.org; s=0x0x8972F4DFDC6DC026; b=iQIzBAABCgAdFiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAmC/uXsACgkQiXL039xtwCZ8sw/5Aa5 oJlATMma0eyvLqBYz/VPZi0O91RhNhjLkzEC3UfMrsVk/DhG2h6PDnO70twLT2F4Pwq6BxupMP8xX vcQxlyw8m0wgL63mgczYCW2Y6zTP+nzGaTwjCY62voKzT/8KEaxt116eQTgveWgViqsdAU6KFDV7e xZDNsPPz8+y19OcDl4zHQUWmXkLOEComZnmT3AaNTHfbFRUn0k5NOpIK6vgB8mBQs0stN3PvBu6js 6Sr0L0eggxneKqkpFadWuW8cBlEXVHxGTqNxHgFFJv2GCWtCUsbSGqMhosJc05devWKzYSgzcjqUO +peNN+Ip7myqDGge3KlDRk9c5TMFxPpEsHrDfsBJHk5VGKn9JEEt5OoqGE2Iz3oVhaMfCuhJ+VB2S tWHHQqdBTHQOh4Ma96uEktGPlWhNGCnw+Z3Tyct6EjlnKQj/sRX+8xMGyn90p6UwXNhaE2ek1ulAC PBJeZOO9PhmVufmxx+WUUv7P5zV++PlVpIM3IWDiByHXwX8yO0z9qRN/ZTy/6LcpnVpb6YnFiRvAF N87lCQq+LykDFpMCpodukzVCgGB8+oilN7llyRY7j87jsg7Zip4MM5aEd/x8o7lcbIVJXJh7RbnzF OxZ3nLJT1mgwUg8HFsdx62qb8gFnNzT+idnsCTNZCiBxhtBlrXensTlmN+1wdsRA= Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ZmhNQaEi; spf=pass (imf27.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.173 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Stat-Signature: weikxza1satur35zdo9njrm1mjtusq3d X-Rspamd-Queue-Id: BE9B28019358 X-Rspamd-Server: rspam06 X-HE-Tag: 1623177597-846603 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: It turns out that SLUB redzoning ("slub_debug=Z") checks from s->object_size rather than from s->inuse (which is normally bumped to make room for the freelist pointer), so a cache created with an object size less than 24 would have the freelist pointer written beyond s->object_size, causing the redzone to be corrupted by the freelist pointer. This was very visible with "slub_debug=ZF": BUG test (Tainted: G B ): Right Redzone overwritten ----------------------------------------------------------------------------- INFO: 0xffff957ead1c05de-0xffff957ead1c05df @offset=1502. First byte 0x1a instead of 0xbb INFO: Slab 0xffffef3950b47000 objects=170 used=170 fp=0x0000000000000000 flags=0x8000000000000200 INFO: Object 0xffff957ead1c05d8 @offset=1496 fp=0xffff957ead1c0620 Redzone (____ptrval____): bb bb bb bb bb bb bb bb ........ Object (____ptrval____): 00 00 00 00 00 f6 f4 a5 ........ Redzone (____ptrval____): 40 1d e8 1a aa @.... Padding (____ptrval____): 00 00 00 00 00 00 00 00 ........ Adjust the offset to stay within s->object_size. (Note that no caches of in this size range are known to exist in the kernel currently.) Reported-by: Marco Elver Reported-by: "Lin, Zhenpeng" Link: https://lore.kernel.org/linux-mm/20200807160627.GA1420741@elver.google.com/ Fixes: 89b83f282d8b (slub: avoid redzone when choosing freepointer location) Cc: stable@vger.kernel.org Tested-by: Marco Elver Link: https://lore.kernel.org/lkml/CANpmjNOwZ5VpKQn+SYWovTkFB4VsT-RPwyENBmaK0dLcpqStkA@mail.gmail.com Signed-off-by: Kees Cook Acked-by: Vlastimil Babka Link: https://lore.kernel.org/lkml/0f7dd7b2-7496-5e2d-9488-2ec9f8e90441@suse.cz/ --- mm/slub.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index f58cfd456548..fe30df460fad 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3689,7 +3689,6 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) { slab_flags_t flags = s->flags; unsigned int size = s->object_size; - unsigned int freepointer_area; unsigned int order; /* @@ -3698,13 +3697,6 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) * the possible location of the free pointer. */ size = ALIGN(size, sizeof(void *)); - /* - * This is the area of the object where a freepointer can be - * safely written. If redzoning adds more to the inuse size, we - * can't use that portion for writing the freepointer, so - * s->offset must be limited within this for the general case. - */ - freepointer_area = size; #ifdef CONFIG_SLUB_DEBUG /* @@ -3730,7 +3722,7 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) /* * With that we have determined the number of bytes in actual use - * by the object. This is the potential offset to the free pointer. + * by the object and redzoning. */ s->inuse = size; @@ -3753,13 +3745,13 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) */ s->offset = size; size += sizeof(void *); - } else if (freepointer_area > sizeof(void *)) { + } else { /* * Store freelist pointer near middle of object to keep * it away from the edges of the object to avoid small * sized over/underflows from neighboring allocations. */ - s->offset = ALIGN(freepointer_area / 2, sizeof(void *)); + s->offset = ALIGN_DOWN(s->object_size / 2, sizeof(void *)); } #ifdef CONFIG_SLUB_DEBUG