From patchwork Fri Oct 9 19:54: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: 11828123 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 5B0881744 for ; Fri, 9 Oct 2020 19:54:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 197EF22B48 for ; Fri, 9 Oct 2020 19:54:47 +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="Npi7NjSN" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 197EF22B48 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 1FCF16B011E; Fri, 9 Oct 2020 15:54:20 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 186C16B0120; Fri, 9 Oct 2020 15:54:20 -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 ECAC36B0122; Fri, 9 Oct 2020 15:54:19 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0175.hostedemail.com [216.40.44.175]) by kanga.kvack.org (Postfix) with ESMTP id BC3846B0120 for ; Fri, 9 Oct 2020 15:54:19 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 68BE51EF1 for ; Fri, 9 Oct 2020 19:54:19 +0000 (UTC) X-FDA: 77353438638.14.duck96_270f5d2271e3 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 466E51822987B for ; Fri, 9 Oct 2020 19:54:19 +0000 (UTC) X-Spam-Summary: 1,0,0,040e62dd07d6fbc8,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:2693:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3874:4250:4321:4385:4605:5007: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.214.196:@chromium.org:.lbl8.mailshell.net-66.100.201.201 62.2.0.100;04yfcub7t15w5br8b5ykayfzxim84yc6f8m97ncmxqgmrqehgqqs8r63pxkufu8.gegnf854nneprbh7fx1n413wzcpufsmpcoawtrmejp3pd75w5575i9w7yeqxdts.k-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: duck96_270f5d2271e3 X-Filterd-Recvd-Size: 5472 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Fri, 9 Oct 2020 19:54:18 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id d6so4941686plo.13 for ; Fri, 09 Oct 2020 12:54: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=F22bFMhtVAhmXTuR4/1X/bDcNMMhExWItCxCe23EWIU=; b=Npi7NjSNb1UGyIYVCTZp0CJo5fFQ9smww6dV+Q25wTc2HAzHcAD9HniVj0B4XPRH// WdZghGsaQYl4k4yEDWekvGgx7hMKJxtl08sxOC5IV13cK3peE4jZDdmUo1v8Gsb9t2VX nc+sYxt+BqlRIIjWtu6LTDNQCGVeM9TAfj5iQ= 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=F22bFMhtVAhmXTuR4/1X/bDcNMMhExWItCxCe23EWIU=; b=asYcAJcQl4cRZL2i+hP+5H10h0GuCwnAdp2UdXEpWZGY1YRKn1/il2p4rOKB0Z4yeu QRZoJZPNhEdbsyW6xa2Nn61wGb/yFERQoJTUUHFkrt46uMp7gjcABWwFs/MdkfSPgM3r xmCMq/otYMd33kCItuCJXWu2FVEtxfw0WfrJkH1o/oGtqCLFbI+SQsrNPov3oOTjXYiM 4FqGPEv3wsUeccSvTvBChcEltKoReUrS8yIHDvFsQ4OvDDAP2i4tuWTEfTlsYO0PjgZy zQBVepX6KZBAad3vSa4aRtl0Pi5ldc2gZeP7HdCoeprYmyiZ51+QpmxZWUnN+WGadU66 7sNA== X-Gm-Message-State: AOAM533GfKn9ttTP0Z8xtcHO+ljnYDHaWxWCxw1KnRBK+T1CDHwztGfx v/e04SY7T0WC1Q0jAcocBO5AxhRR95Zraw== X-Google-Smtp-Source: ABdhPJz5BOO9kgJYxc5gMsHxfsWRC4HsO7I3VpQgWqsf3q4eDcCpfAaC13NDroJ8WTncKDv0pVy62w== X-Received: by 2002:a17:902:b283:b029:d3:d779:94fd with SMTP id u3-20020a170902b283b02900d3d77994fdmr13103470plr.8.1602273258032; 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 w143sm11701672pfc.31.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 , stable@vger.kernel.org, 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 2/3] mm/slub: Fix redzoning for small allocations Date: Fri, 9 Oct 2020 12:54:10 -0700 Message-Id: <20201009195411.4018141-3-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: 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 will be overwritten by redzoning (e.g. with boot param "slub_debug=ZV"): 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. (Note that no caches with such a size 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