From patchwork Thu Oct 15 03:37:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11838587 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B4C771580 for ; Thu, 15 Oct 2020 03:37:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 69C0B22257 for ; Thu, 15 Oct 2020 03:37:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="SLL26Mxq" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 69C0B22257 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 D0FAB6B0072; Wed, 14 Oct 2020 23:37:19 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C763C6B0073; Wed, 14 Oct 2020 23:37:19 -0400 (EDT) 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 B98656B0074; Wed, 14 Oct 2020 23:37:19 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0093.hostedemail.com [216.40.44.93]) by kanga.kvack.org (Postfix) with ESMTP id 850486B0072 for ; Wed, 14 Oct 2020 23:37:19 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 1E7881EF1 for ; Thu, 15 Oct 2020 03:37:19 +0000 (UTC) X-FDA: 77372749398.15.bee15_501859627211 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id E9EB51814B0C1 for ; Thu, 15 Oct 2020 03:37:18 +0000 (UTC) X-Spam-Summary: 1,0,0,2c8fdd6c6108084f,d41d8cd98f00b204,keescook@chromium.org,,RULES_HIT:2:41:69:355:379:541:800:960:966:967:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1605:1606:1730:1747:1777:1792:1801:2196:2198:2199:2200:2393:2525:2559:2563:2682:2685:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4119:4321:4385:4605:5007:6119:6261:6653:6742:7264:7875:7903:7904:8784:9025:9121:10004:11026:11232:11233:11473:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12679:12683:12739:12740:12895:12986:13161:13229:13894:14096:14394:21080:21220:21444:21451:21524:21627:21939:21990:30004:30054:30069:30070:30075,0,RBL:209.85.210.194:@chromium.org:.lbl8.mailshell.net-66.100.201.201 62.2.0.100;04ygwgsfk8rucy69hsfupihxonjxhyc5xqgi81uqqpjdwxnds5y7z9kukpst3gz.rxfxtzmtw19jxfo9itwm71jcifdzt64i3y71q88ikb9yu3rpewbwzdryae3rwwf.k-lbl8.mailshell.net-223.238.255.100,Cac heIP:non X-HE-Tag: bee15_501859627211 X-Filterd-Recvd-Size: 8188 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Thu, 15 Oct 2020 03:37:18 +0000 (UTC) Received: by mail-pf1-f194.google.com with SMTP id n14so1133194pff.6 for ; Wed, 14 Oct 2020 20:37:18 -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=oE8671fGdc7a/ra0xKjfKpYochKuO322S/sLHjna38o=; b=SLL26MxqLUcSnVrzPPc8gZDG5nPisgFw+JOp2uG+hiEng9js/7mqB0nxf35Tyi2S6Q OFVkrwpHysGEtXORm5Pew42lpCgG1xEiXWRIf0hb9R9BZJPDMYF081NW3O+JjJ+mfjZ2 hvCYBkxSNm3g5s6s6XJS0x8Mr6HYpttqhdhJo= 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=oE8671fGdc7a/ra0xKjfKpYochKuO322S/sLHjna38o=; b=ZQ8N9GOaDXy7zHmOKOXkDRTOmScpB8+BOlAvai9OD9311PX4fZJVaEGVm1vtP936aK yyb6V+L90981gBICVi8yNkxLypLGyG6f3Y/a/X86+LL1/hf1LQ7ULOMgh+ajyy4mZqen CpC+A5Zza4RjfVa69wA15oheSoIb/D7vKeFnMXHzOXarPUZXDtU5Ml2f3CyJmnhzDWpY 4+SUfbWsW1id4jbRtiKt4RgLxumwa0LvDfFVdRzTR1PLz2SR2nWBSO/mv+JEQUymIF8V c7FICZuUF8uleCWXpsOYVbuMnxdtg7VA6T1eUjWJ3Da0yh+JLYIShr0+aO+UlLYryo4e ynHw== X-Gm-Message-State: AOAM533M/iOEhVGMwu3yK3Uben3uD7xxBRX3zE+lw9qI7KiYkQ1CEmlH 7/yvcFw5Q+qpNBIH1PzP7mysQg== X-Google-Smtp-Source: ABdhPJxemA8WjaemcI4SUPAgMwduvheXLqyw4BgpfqXRgW9RfNWCCl3QpfT4dnDHY30ExHKaHqU6JQ== X-Received: by 2002:a63:cc42:: with SMTP id q2mr1820442pgi.216.1602733037644; Wed, 14 Oct 2020 20:37:17 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id o4sm1077806pjp.37.2020.10.14.20.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 20:37:14 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Vlastimil Babka , Christoph Lameter , Waiman Long , Marco Elver , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3 1/3] mm/slub: Clarify verification reporting Date: Wed, 14 Oct 2020 20:37:10 -0700 Message-Id: <20201015033712.1491731-2-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201015033712.1491731-1-keescook@chromium.org> References: <20201015033712.1491731-1-keescook@chromium.org> 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: 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 289d231cee97..77c7a3331eda 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 6d3574013b2f..f4f1d63f0ab9 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -698,15 +698,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); @@ -718,7 +718,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(); @@ -895,11 +895,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 { @@ -914,7 +914,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 Thu Oct 15 03:37:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11838581 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8CE4C1580 for ; Thu, 15 Oct 2020 03:37:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 14B5B22257 for ; Thu, 15 Oct 2020 03:37:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Y7RwMlnT" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 14B5B22257 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 EBBF86B006E; Wed, 14 Oct 2020 23:37:17 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E6DC66B0070; Wed, 14 Oct 2020 23:37:17 -0400 (EDT) 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 D5BE46B0071; Wed, 14 Oct 2020 23:37:17 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0172.hostedemail.com [216.40.44.172]) by kanga.kvack.org (Postfix) with ESMTP id AB2566B006E for ; Wed, 14 Oct 2020 23:37:17 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2FF63180AD807 for ; Thu, 15 Oct 2020 03:37:17 +0000 (UTC) X-FDA: 77372749314.05.joke48_1d10fbe27211 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 1552718014D06 for ; Thu, 15 Oct 2020 03:37:17 +0000 (UTC) X-Spam-Summary: 1,0,0,a37a5f84139add2a,d41d8cd98f00b204,keescook@chromium.org,,RULES_HIT:41:69:355:379:541:800:960:966:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1542:1711:1730:1747:1777:1792:1801:2194:2196:2198:2199:2200:2201:2393:2559:2562:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3874:4250:4321:4385:4605:5007:6119:6261:6653:6742:7264:7903:7904:8603:8784:10004:11026:11233:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12683:12740:12895:12986:13161:13192:13229:13870:13894:14096:14110:14181:14394:14721:21063:21080:21220:21444:21451:21611:21627:30054:30075,0,RBL:209.85.215.194:@chromium.org:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04yr5sxmu8tigucqdut8mj397asqqycrenw6mcs6utafgj4j3bge3c1p5948t7h.qbtpaz1c8my7wg6ktwx5i8t7rykpid6m1m18899pb4ihx75w5575i9h7wqhthbr.n-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:25 ,LUA_SUM X-HE-Tag: joke48_1d10fbe27211 X-Filterd-Recvd-Size: 5709 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf21.hostedemail.com (Postfix) with ESMTP for ; Thu, 15 Oct 2020 03:37:16 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id x13so991897pgp.7 for ; Wed, 14 Oct 2020 20:37:16 -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=rKpXkxl6yJIU4tRkHvXmPNNxYUSkKNgxeKQKUdkfeI4=; b=Y7RwMlnTp+htb3EhE553XcSxYY00imMzkms1Q4ltiGTIPjbmMrFB9MsRfPZ8yGBGPR 2GQwJoxN0tNXCP7H7TODKP0OefvZGiXFMH33q68Fvyig5D2hvk6Li16EejMb2cAqJYRv V9A/iQbWQ0cYjMEagNkFayGL8XGicbm+FsUQY= 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=rKpXkxl6yJIU4tRkHvXmPNNxYUSkKNgxeKQKUdkfeI4=; b=VwHJeKgMryzS9Msd9/3RydWYbLUmVILc4ZNCvyEIuwWrhv8rG8I/xo1pXHwReKilcH 10VSsq8S5VM+6Pl2Gyz64eFhF7TmsyAWUxlw6Saa/X7OYEbZ2CcpxO74sVc+qnWgjzER b5dmkuCN9z8lZd300XXirSEwyvOa4Ix+7BUoZWd+GDKAMEhzPcmqz5cRvBTdHvMu3XRD NRLxiIx7FX7Wd6ZjV4Do15MHgk8HixsLGKrZN7ZXQruSPkJkdvO5jtNxtyn6TswDUlQU efEHnrcGNAzlFbqlvo5PMl/sRFRJwvrds/Je83A2/e+DKBsYFtEolPEHqcJ7Y5kJ9uFd cizw== X-Gm-Message-State: AOAM533/FIV/bPZajfGRxAG6gpgw71rJ8cF54XVENeV5qoN0oxR+Tuh/ UAWgeBZhRCb/04cKCTYLFkuTsw== X-Google-Smtp-Source: ABdhPJyoubbBOjnFUbsWxFOasUJwB58/UjuuTINRsJ1mzvqelRDL/1DXYNuIQElP2sRZyoYFfT/RKQ== X-Received: by 2002:aa7:8216:0:b029:142:2501:3968 with SMTP id k22-20020aa782160000b029014225013968mr2244959pfi.45.1602733035675; Wed, 14 Oct 2020 20:37:15 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 78sm1157099pfz.211.2020.10.14.20.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 20:37:14 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , stable@vger.kernel.org, Christoph Lameter , Vlastimil Babka , Waiman Long , Marco Elver , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3 2/3] mm/slub: Fix redzoning for small allocations Date: Wed, 14 Oct 2020 20:37:11 -0700 Message-Id: <20201015033712.1491731-3-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201015033712.1491731-1-keescook@chromium.org> References: <20201015033712.1491731-1-keescook@chromium.org> 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: 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. (This object_size is not actually considered valid, as tested by kmem_cache_sanity_check() under CONFIG_DEBUG_VM. This is being added for extra robustness, since it IS possible to build kernels where this is allowed -- why keep foot-guns around?) (Note that no caches in 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/slub.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index f4f1d63f0ab9..752fad36522c 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3682,15 +3682,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 Thu Oct 15 03:37:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11838585 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0A1A31580 for ; Thu, 15 Oct 2020 03:37:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A251522257 for ; Thu, 15 Oct 2020 03:37:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="cSRzfwSv" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A251522257 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 4103A6B0071; Wed, 14 Oct 2020 23:37:19 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3926E6B0072; Wed, 14 Oct 2020 23:37:19 -0400 (EDT) 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 1754A6B0073; Wed, 14 Oct 2020 23:37:19 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0102.hostedemail.com [216.40.44.102]) by kanga.kvack.org (Postfix) with ESMTP id D81206B0071 for ; Wed, 14 Oct 2020 23:37:18 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 7FE511EE6 for ; Thu, 15 Oct 2020 03:37:18 +0000 (UTC) X-FDA: 77372749356.05.dress09_4604c2b27211 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 6024A18014D06 for ; Thu, 15 Oct 2020 03:37:18 +0000 (UTC) X-Spam-Summary: 1,0,0,3abf25bfff0b2c09,d41d8cd98f00b204,keescook@chromium.org,,RULES_HIT:41:69:355:379:541:800:960:965:966:967:973:988:989:1260:1311:1314:1345:1437:1515:1535:1543:1711:1730:1747:1777:1792:1801:2194:2196:2198:2199:2200:2201:2393:2525:2559:2565:2570:2682:2685:2703:2731:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3355:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4117:4250:4321:4385:4390:4395:4605:5007:6261:7264:7901:7903:7904:8603:8784:8957:9025:10004:11658:12740:13153:13192:13221:13228:13229,0,RBL:209.85.215.194:@chromium.org:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04y8hrqjgetagox7wjw6siurt93mgyp5ouadarhsgoyjnzc5q3biheym8n5dj4r.ed1otfgj954zbmofwng7kt75bh1aunesfh3ayzjyjb4nce6rdwh1p3eouikpj3s.4-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: dress09_4604c2b27211 X-Filterd-Recvd-Size: 6724 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Thu, 15 Oct 2020 03:37:17 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id g29so1010398pgl.2 for ; Wed, 14 Oct 2020 20:37:17 -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=SurWNoEuD6TTtB4XFp88vfsS4MGv6DgcHJa9LNU4sRM=; b=cSRzfwSvHvjyHkoYE/K38T5Ur3yGQeAFQ/A0BVo05yNGOGjLBV7RmE2tnUgaWIJZcm gJCezP1zrirgCcF787B66uPhC8K1r2moXDD1eVOS3GEn7XGMiaIh5afWbiuFJbQx7EDS fTDqiysEC79UP8Y/HxFBu2Q2r7ifekHzFspcE= 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=SurWNoEuD6TTtB4XFp88vfsS4MGv6DgcHJa9LNU4sRM=; b=iSKBZquv5kVxEzXtudBHH5hPcOKw1PSt2Ov4xz0A6uV8DrJBCwCqH2zCegvybocnYY gGNSv8KY20eeF5pc00s+KdetGRXhOMYcjM4/fUQ7uQ0nXLpDaOG05d7ql94E3POV7wrn 7S3Ym47VEIYE3LXcE7of3DiJ4wFFF+CFXoCawmnebUerbaZxVb61pxT5h49meo/Kieb8 Bg0ajfEF7u8VCiiJZ7O80cisDNNFDlR3wSK1ydPjp32erMxRZU3dwUDWkyFHmRbQOPRV RcOst5Tlm4K/bltEYrhgMyTAbGqsnHouivS7MWR0/VwKmmridp6QqpJNPpd7y2CvGEzo gwEw== X-Gm-Message-State: AOAM532cms2V76JqauGsZRMfgZAFAG/c2TqlZzvUTtlhZMzpIGcV5Nvy J0s0hmkJStr47SR/zhmZvGpNZA== X-Google-Smtp-Source: ABdhPJxtMQurAs24PVDe3ClRON3VZXKCxnFyNNGqQxzQkVllW9GeD0AsKT82EWzmD3Jg8GfZs9INug== X-Received: by 2002:a63:5c5e:: with SMTP id n30mr1749232pgm.54.1602733036990; Wed, 14 Oct 2020 20:37:16 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id t6sm1204756pfl.50.2020.10.14.20.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 20:37:14 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Marco Elver , stable@vger.kernel.org, Vlastimil Babka , Christoph Lameter , Waiman Long , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3 3/3] mm/slub: Actually fix freelist pointer vs redzoning Date: Wed, 14 Oct 2020 20:37:12 -0700 Message-Id: <20201015033712.1491731-4-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201015033712.1491731-1-keescook@chromium.org> References: <20201015033712.1491731-1-keescook@chromium.org> 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: 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 in this size range are known to exist in the kernel currently.) Reported-by: Marco Elver 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 752fad36522c..6f115e56c5d0 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3637,7 +3637,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; /* @@ -3646,13 +3645,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 /* @@ -3678,7 +3670,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; @@ -3701,13 +3693,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