From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69E9AC4363D 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 D0CA822255 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 D0CA822255 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) Received: by kanga.kvack.org (Postfix, from userid 40) id E6DC66B0070; Wed, 14 Oct 2020 23:37:17 -0400 (EDT) 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-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-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 Content-Transfer-Encoding: quoted-printable 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=3DZF"): BUG test (Tainted: G B ): Right Redzone overwritten -------------------------------------------------------------------------= ---- INFO: 0xffff957ead1c05de-0xffff957ead1c05df @offset=3D1502. First byte 0x= 1a instead of 0xbb INFO: Slab 0xffffef3950b47000 objects=3D170 used=3D170 fp=3D0x00000000000= 00000 flags=3D0x8000000000000200 INFO: Object 0xffff957ead1c05d8 @offset=3D1496 fp=3D0xffff957ead1c0620 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 =3D size; =20 - 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 >=3D s->inuse means free * pointer is outside of the object is used in the --=20 2.25.1