Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp1696669pxu; Thu, 8 Oct 2020 19:33:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBaSYN8x0xKs1GCw7ddrLvWHv9J1RLPpWM5JjPDzAsGrx1FgL3aYBApcNxfGKHQoIwtC0/ X-Received: by 2002:a17:906:1c50:: with SMTP id l16mr11745585ejg.144.1602210780838; Thu, 08 Oct 2020 19:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602210780; cv=none; d=google.com; s=arc-20160816; b=noMp1oorNO5QXjGrKTWVTJNTQJTlqxs3aupxG3c/ZckFiO27viGy79SQQMiEgL59cU 8SbwMNs94g1s5iZ4Z6eV1kAg7MLtfSksMe9atb9FaWkS5jyTrC1LSALRIlAI0Vbc1TNp XVkLKTbdq8ED72YPEucHegVqJkIt2feCN/r+eYYwP0acQTb/zVOGtOmY5VuzekcS/vws INJSeD1hWNTRDy62pVJsPJ7BOq1k+XOFSy3lLiEDyFLgSf0osobl0xVUopGsRxDfZNjB tUYsMLx/J7ozz6p9MX82PJCgPK0ts3fb2Net+2Fs6pymlOF1/cZHUZA7nKMe1JRMUBD9 ko/A== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=MRZxCY82VT8AbmDDW9cNgiSjhXieUNCL4lvDa6bH9cQ=; b=u5CMgIzcC/vjRmkRzYb0KsiA6isbcr/poCG95pxLmwZ2pICOiWKN8S7J2oyDnOIwVE eva2o/yEwCEuzNsRBRt7iKFMgP+DJExDsk04Qy88C/UHt5oigA3TAtc4ZfshmUkh8lUV dCYEcYDgLzxUaZXySSKrMe6sEg9uXjumwoElTJwSz1TKhApkxCoMvWHZT6xeVaPjNSZ2 Y64F48QxtQ5SRiq8nJXNoKan8yPAEUlKoiT5O5TnH46uGfr7SHQ1TS6iq6sMwyEBkoCj anXyp9a9ed87beuHkTzR7XsTnDnVxEBJMUrLM9lP351+VMnGaXLUc2sUzCij2w0bgLyN nUdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=k2l02j+q; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 65si5003439edc.222.2020.10.08.19.32.38; Thu, 08 Oct 2020 19:33:00 -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=@chromium.org header.s=google header.b=k2l02j+q; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731048AbgJHXeu (ORCPT + 99 others); Thu, 8 Oct 2020 19:34:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725979AbgJHXet (ORCPT ); Thu, 8 Oct 2020 19:34:49 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DB0EC0613D4 for ; Thu, 8 Oct 2020 16:34:49 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id y1so12581plp.6 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=d6J04TsOWLRPXHvgXu3+NId/q5CeCezrUa8vOAybSODPEHOyaMOFqdeUawWg2Q+CgU DJRJKro2bJIA9+TUXfHp0uTWVBVt3tgWQ0+bB2kKTbNg6vjI6lwaN9Is3s3QRuaQB06j aji9FbzjWkU653mRQpJNJBTnmpjQQgO8LesQZa3Vz3zDALWss9EAYoq01+adJnhpjccE tT2MZXiUL0zUDo5rV+tT6YjVJhlhkU8p2o29RY8anzzpKTLyQ5G9RhGOm+3sbA8TcJtM HUoqPoJgWBCXz90sqp8bNzyDKki11exSs4aBiF8EqEkdOlmlEb2SvCCpKcjr9GrsYOKh sg5Q== X-Gm-Message-State: AOAM533EskGOoP+LxNpkvfkfmK9lknFXfSGX+0JQAYTDQpQFwLypKHOY Zh0Ogl6vW78cwc+rsTjNO1AYug== 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 -- 2.25.1