From patchwork Thu Oct 8 23:34:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11824735 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 EF3336CA for ; Thu, 8 Oct 2020 23:34:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9E28A2224D for ; Thu, 8 Oct 2020 23:34:52 +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="k2l02j+q" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9E28A2224D 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 7FF7A900002; Thu, 8 Oct 2020 19:34:51 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7B04E6B005D; Thu, 8 Oct 2020 19:34:51 -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 67925900002; Thu, 8 Oct 2020 19:34:51 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id 3BD766B005C for ; Thu, 8 Oct 2020 19:34:51 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B36BD1EE6 for ; Thu, 8 Oct 2020 23:34:50 +0000 (UTC) X-FDA: 77350365540.26.seat90_3c02ae4271dc Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id 8BB611804B66B for ; Thu, 8 Oct 2020 23:34:50 +0000 (UTC) X-Spam-Summary: 1,0,0,4076d00c07d42746,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:1605:1711:1730:1747:1777:1792:1801:2194:2196:2198:2199:2200:2201:2393:2525:2559:2563:2682:2685:2731:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142: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:7264:7901:7903:7904:8603:8784:9025:10004:11026:11233:11473:11658:11914:12043:12296:12297:12517:12519:12555:12679:12683:12696:12737:12740:12895:12986:13153:13192:13221:13228:13229:13894:14096:14181:14394:14721:21063:21080:21220:21444:21451:21611:21627:21749:21811:21939:30025:30034:30054:30075,0,RBL:209.85.214.196:@chromium.org:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04yg4pe9epnxdtq1kyfrztj6x4ynmypy4pgp35nr5su8fojni7gmbuaura4zkki.t3msyrkewmyby1db1mt638abzn9hyzxfd89z8rr81ox9juwgdoc7d ozj9cn9i X-HE-Tag: seat90_3c02ae4271dc X-Filterd-Recvd-Size: 6674 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Thu, 8 Oct 2020 23:34:49 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id h2so3531657pll.11 for ; Thu, 08 Oct 2020 16:34:49 -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:mime-version :content-transfer-encoding; bh=MRZxCY82VT8AbmDDW9cNgiSjhXieUNCL4lvDa6bH9cQ=; b=k2l02j+qVTgPRzl0YEB+iSSs1OzEALMQl31r6XgVZU/yO4vFIlyi/eqIIu/eZ2jIXL oD6Ai7UmXysgGKc6ayu+h5ZHYMxGqlk/cmIoX3T89xR0Ad7c3hY+n5cJC1Kv4L8ZAc3l 5J341dy3JiQZ5XmfItqHSYWmpOXA3bVvHSkpk= 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:mime-version :content-transfer-encoding; bh=MRZxCY82VT8AbmDDW9cNgiSjhXieUNCL4lvDa6bH9cQ=; b=KTqCtsmL5cgv2siRHYcdo7RGsMRmaCDDn8Wmak1/0u6kdpBhfHs14tPXYi9IAacdLP M0xCWeY3Co6v/+6WfthXhFWCqKRrWrB2Ja4sY+LJFLYNzAiHq6hCvljKXzAuG48Z7oBO hkBqBdutqUnRJh4yDG6V6EP3W/Mq05A1Oo7bHzXgUmxJ4sOJuT4SYYuk1rVXS7YMWDFh XA5BCK91lNXHKjuwoSa8QuMbdrjVxkLJI2SnTBh8uEjYmiLFR9BgVrsc272hFhkHVjfn KvCo1Lf6pHCBU41YiWirSuoJae7tcImwFZuthq/1EIV2+P+5GUZX8fkUuv9S4Tn/HkBm TM/Q== X-Gm-Message-State: AOAM533722USUKeVkKVRHwTQrAs7DlD67klNT+HoyCADtwQoV4a3+J0+ zKtqDHtq0FwkffUXialxy7ZQ9Q== X-Google-Smtp-Source: ABdhPJyexpe9/8XvCs+4d0A3Z2MTbhqPh7RXZQkKLPSCe9cckWw8dLV1aj2qH4TGZOwGLSFuLsmsAw== X-Received: by 2002:a17:902:8d8e:b029:d3:df0c:60a0 with SMTP id v14-20020a1709028d8eb02900d3df0c60a0mr9331844plo.35.1602200088961; Thu, 08 Oct 2020 16:34:48 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c21sm8660051pfo.139.2020.10.08.16.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Oct 2020 16:34:48 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Marco Elver , stable@vger.kernel.org, Pekka Enberg , Waiman Long , Christoph Lameter , David Rientjes , Joonsoo Kim , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] slub: Actually fix freelist pointer vs redzoning Date: Thu, 8 Oct 2020 16:34:43 -0700 Message-Id: <20201008233443.3335464-1-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 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 ): 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 there appear to be no such small-sized caches 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 Signed-off-by: Kees Cook Tested-by: Marco Elver --- mm/slub.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 68c02b2eecd9..979f5da26992 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3641,7 +3641,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; /* @@ -3650,13 +3649,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 /* @@ -3682,7 +3674,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; @@ -3694,7 +3686,8 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) * 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 @@ -3703,13 +3696,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