From patchwork Fri Oct 9 19:54: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: 11828125 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 CC9B51580 for ; Fri, 9 Oct 2020 19:54:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 94091225A9 for ; Fri, 9 Oct 2020 19:54:49 +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="ZCPdEfES" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 94091225A9 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 277196B0120; Fri, 9 Oct 2020 15:54:21 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1D6D26B0123; Fri, 9 Oct 2020 15:54:21 -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 0EAD96B0124; Fri, 9 Oct 2020 15:54:21 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0221.hostedemail.com [216.40.44.221]) by kanga.kvack.org (Postfix) with ESMTP id D5FAC6B0120 for ; Fri, 9 Oct 2020 15:54:20 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 6C0B0362E for ; Fri, 9 Oct 2020 19:54:20 +0000 (UTC) X-FDA: 77353438680.30.hot78_3b02783271e3 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id 4AA0A180B3C85 for ; Fri, 9 Oct 2020 19:54:20 +0000 (UTC) X-Spam-Summary: 1,0,0,4d0d98905f022bc5,d41d8cd98f00b204,keescook@chromium.org,,RULES_HIT:41:69:355:379:541:800:960:965:966:967:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1543:1711:1730:1747:1777:1792:1801:2194:2196:2198:2199:2200:2201:2393:2525:2559:2564:2682:2685:2731:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142: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:6653:6742:7264:7901:7903:7904:8603:8784:8957:9025:10004:11026:11233:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12683:12696:12737:12740:12895:12986:13153:13192:13221:13228:13229:13894:14096:14181:14394:14721:21080:21220:21444:21451:21611:21627:21749:21811:21939:30025:30034:30054:30075,0,RBL:209.85.210.193:@chromium.org:.lbl8.mailshell.net-66.100.201.201 62.2.0.100;04yg3a1p4aqguu3ajo7jqydbs8a1jyp5ouadarhsgoyjnzc5q3biheym8n5d7ke.sxhh83sj954zbmofwng7kt75b5nzim8amch71a ha7wukpu X-HE-Tag: hot78_3b02783271e3 X-Filterd-Recvd-Size: 6455 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Fri, 9 Oct 2020 19:54:19 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id f19so7718886pfj.11 for ; Fri, 09 Oct 2020 12:54:19 -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=JMYL2FCSaXkjGdwauYSRLvBY1B8xfDKXMqCzfo1TOs8=; b=ZCPdEfEStPdr8MBrgInFcmzphM88HeVkiLW5HeK18RfWZaUwWiD8BL6dyzwKs/1dnc OvyIRwqQEbyYL8lhOTaDweFLQpNL6qYjO+KCfCOzwMLQR7sRdWc8JlusCWKLJbLLqBrG AzMwws/+ykogo4TXKjb/cxA2ap5hUNDuUtxA4= 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=JMYL2FCSaXkjGdwauYSRLvBY1B8xfDKXMqCzfo1TOs8=; b=KA0/W/l4dEbgWGMKmPv8TVmcIN+qynEI+G1fw6ExkG6DyohxUfNNE71qooNBT/dOKX 5BVHfxuRWZqMa1DE3+okoh/6Svjs0L9vnWE5x443E4+VctCvUrSwCdKEbrqxPLNThJjo Kzf42L5w0uD2BI+jN8+E6xW8NF5KPKAPbCaKl8DAhK9Aam190C1KzruiYRYSPk90U1ha 1x661DC5+yg4NWruClBhaKU3zJ5KSWl6mryRO8xJHC5L0AIZRK+PrzqgDmwStMWkPtMW 5Qa3g9C/M/u85XlK3KcG/S8YgAFuK+RZz405bAnIMGXd5iMbLr1ay47Ga59i4FTgp8kD QxvQ== X-Gm-Message-State: AOAM530kXU7i1UgobAxvuUkQIDoYphLPLw2FeZL0m7PQmbO32FlF4qtE V9QijSlaL0CD94ACZZNuoefo6w== X-Google-Smtp-Source: ABdhPJy1JWcE3giDZXo2NocSb/TmztEuw95HJz8DBEL5swgLDRHJMERTIzgeXrC7UsA6aCc8bZxDOQ== X-Received: by 2002:a17:90a:1596:: with SMTP id m22mr6456795pja.189.1602273258702; Fri, 09 Oct 2020 12:54:18 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 8sm11767961pfn.54.2020.10.09.12.54.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Oct 2020 12:54:16 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Marco Elver , Jonathan Corbet , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 3/3] mm/slub: Actually fix freelist pointer vs redzoning Date: Fri, 9 Oct 2020 12:54:11 -0700 Message-Id: <20201009195411.4018141-4-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201009195411.4018141-1-keescook@chromium.org> References: <20201009195411.4018141-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 their freelist pointer written beyond s->object_size, causing the redzone to corrupt 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. 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) 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 --- 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