Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3943520pxj; Mon, 21 Jun 2021 09:54:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGG5ps5Tx5MazW8TAWRIURosgc+KbWigm3mE4FJigbPuEmFOWvrLivUM3uNunJsV+4wfcE X-Received: by 2002:a05:6402:3581:: with SMTP id y1mr5257274edc.31.1624294449023; Mon, 21 Jun 2021 09:54:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624294449; cv=none; d=google.com; s=arc-20160816; b=i+DGujeevAQZ0zrZ8ojEpa0HR5YSVlE/zCaJ+plysB7Hd5xkRbxoILREXyJ6I4s09l QlDqLDQjoE+DS2RRj7MVbckfrSXpD5mwyviLhgVElJi5+oNBnRaFU6L/tYzzXjjYWRKL MG3iLLgJ14OK8XOEd9xYleNFH7lhoi1GcA/vilGFgU9W8vzXYqRsIIIx22NO/UPuQSu7 qrO0XeZXCx93h/9BeEyE4Et8nJzGgquoyOTPVnveKOx7brx0fjhdyOQuQdVZBz9P+Aw3 M7/foHz6uPsf2YsCxjL3/T9g8HyR109ILCMSGqGgmmN6qeKbkEeEHhJe5vtjV/DjbY2m qVTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=rjO2mjJV2Os4Qc9PIN0/Zs0WW1eX2HC3feyaNqLikIM=; b=yM04P0yNr91TM/n/WnKnBqGaAniACmHak8hB6l8At1y6DHwzOSvPTiJVJFOP+/Gy1W V6eSUc5++3SqdWSuUr1LIvjaL5t4TrA8X11dGSccU38P9wecju7B0sSkHMyDaUlubEwj UHJYmL5DMQqCUvT7KyJm342o/rKQzusrXMElnVV3fsjJ5H1oUHxyNNmUX2nsTfSC0jhf GKwuh7XgkmbeW2llf2JepnPkSwe3t+ssmA3IPoXUcKfYb6l3y44Td8Kms5mmaMhPwB51 evhx/Z23Qry/CSsWotKIvSL+0AnAVZ593np10Tl/03Y5BOw31Mr3dLw9USVt+S2Dz16v 5EYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0AvetW79; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dz7si17072608edb.68.2021.06.21.09.53.47; Mon, 21 Jun 2021 09:54:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0AvetW79; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232679AbhFUQzM (ORCPT + 99 others); Mon, 21 Jun 2021 12:55:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:42832 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232103AbhFUQv2 (ORCPT ); Mon, 21 Jun 2021 12:51:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E265E61353; Mon, 21 Jun 2021 16:35:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624293323; bh=YgiavgVtfru7ZlRQvDgOvx1ZWwQ7LtCOV7mbGf/JrwA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0AvetW79u5yAgqv3iNaSlJtPtDus7igk0mxwGnB6pvk64ILQ9T0zS5YTHYMZaUWNM ET31iJ8LVozebvWABlE5UizdmvcLqT30m8rt1Rw3iyQ4YO+YRY9CV3iv4LDl2leMTY 3za8TTD7VFD/WneNaEHJQBPZ4e0a42THaVvYyQB8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kees Cook , Marco Elver , "Lin, Zhenpeng" , Vlastimil Babka , Christoph Lameter , David Rientjes , Joonsoo Kim , Pekka Enberg , Roman Gushchin , Andrew Morton , Linus Torvalds Subject: [PATCH 5.12 171/178] mm/slub: actually fix freelist pointer vs redzoning Date: Mon, 21 Jun 2021 18:16:25 +0200 Message-Id: <20210621154928.561039763@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154921.212599475@linuxfoundation.org> References: <20210621154921.212599475@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook commit e41a49fadbc80b60b48d3c095d9e2ee7ef7c9a8e upstream. 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.) Link: https://lkml.kernel.org/r/20210608183955.280836-4-keescook@chromium.org Link: https://lore.kernel.org/linux-mm/20200807160627.GA1420741@elver.google.com/ Link: https://lore.kernel.org/lkml/0f7dd7b2-7496-5e2d-9488-2ec9f8e90441@suse.cz/Fixes: 89b83f282d8b (slub: avoid redzone when choosing freepointer location) Link: https://lore.kernel.org/lkml/CANpmjNOwZ5VpKQn+SYWovTkFB4VsT-RPwyENBmaK0dLcpqStkA@mail.gmail.com Signed-off-by: Kees Cook Reported-by: Marco Elver Reported-by: "Lin, Zhenpeng" Tested-by: Marco Elver Acked-by: Vlastimil Babka Cc: Christoph Lameter Cc: David Rientjes Cc: Joonsoo Kim Cc: Pekka Enberg Cc: Roman Gushchin Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/slub.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) --- a/mm/slub.c +++ b/mm/slub.c @@ -3687,7 +3687,6 @@ static int calculate_sizes(struct kmem_c { slab_flags_t flags = s->flags; unsigned int size = s->object_size; - unsigned int freepointer_area; unsigned int order; /* @@ -3696,13 +3695,6 @@ static int calculate_sizes(struct kmem_c * 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 /* @@ -3728,7 +3720,7 @@ static int calculate_sizes(struct kmem_c /* * 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; @@ -3751,13 +3743,13 @@ static int calculate_sizes(struct kmem_c */ 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