Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp28076rdb; Thu, 1 Feb 2024 00:36:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IF1KxNQ6GXoFH08HhNZg68EZd+QPcqm3Mp7gFbtM2Jjh1+Mg0bfgWfNZgXO4WeJEO8pH1Ju X-Received: by 2002:a05:620a:22c:b0:784:b12:ba1c with SMTP id u12-20020a05620a022c00b007840b12ba1cmr1845666qkm.78.1706776566608; Thu, 01 Feb 2024 00:36:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706776566; cv=pass; d=google.com; s=arc-20160816; b=adXy9m9qKIZD12Lx1Oy+vUiztAAGCDmwPutY/AgyRERjNAKVl/lKPs+rP5bOTzlLym E0BuB3H5PVPuw5QRgjxQc5qANfV9/aZj40JTos4V441IfZFhRxg/pg8Mw1f+joYksx1A Z2s1ZVlyl7Go/WPTJnv3E46i2/95IHse7X0J82XKNew/BGh08c/1pdqP7ZTvcJYuoit1 H4N5t7pjom8eZy8xmqhEBtbAjv0AkMm1Lx9reJhWTpCt60NSSRpBVCwIqRKP3WrJlt4h spBAY9u9pYJ9gIP1ARzN6L1qXEmg6MWj5qoPbjk3PZPG/LTWFz63lqgXsheqwUsUvjc7 XO2Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=yDijPStjyNK8y9wipx4jasC/bHAVPAFiOvOL8ps5ECY=; fh=1PI/gNWG86BeEigf3ZJyVjAQQVRfdNo1XInhUYQBKqE=; b=iyvqIASQ7ZyTb0DLNRr1owa0IracAv+yBWmb3DqjdzFRWnKxxHrc0wIDDnLJU+YGSe ZmcbLVDYqY3O/62d4VrXBu/NZDxISe7T2Y568vorNgLtfXqHAU18OVdUtff5Hfoec+1j OC7ZJ6dczrVIckp+rgm2WdRUkzyXfmtsUZwVT8NvkU0biTWAeTs5wei/9yDGvWe8xbFL 5NSIJRMV7uu9zHu7eKMVPKROnc718HZx/2UYQR+9HaluIwhSNGrxRearLpE8ekuAVWqi kEDJ5LQnmPvXGum/7JW3VAIMOYwu8ZxaQARAbVwZ2ah6JQ2zJfsK/ZZRoge2OW9q9BJ0 Bcyg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bEBBu6Qo; arc=pass (i=1 spf=pass spfdomain=flex--elver.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47783-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47783-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVpm+LrYkrcE94UwUrPoUcqG+ppxOVR5ilHxOqTCYlxtwpEtoESvz9CqZq17irl5/lE3AJ5MOUsMsLVm1pw/S4KcuIMuUfT2ym6FhQYGg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id j20-20020a05620a289400b007831ec4ff84si15371368qkp.454.2024.02.01.00.36.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 00:36:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47783-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bEBBu6Qo; arc=pass (i=1 spf=pass spfdomain=flex--elver.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47783-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47783-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C33C41C260F6 for ; Thu, 1 Feb 2024 08:35:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2FADB15A4A2; Thu, 1 Feb 2024 08:35:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bEBBu6Qo" Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8444D2E842 for ; Thu, 1 Feb 2024 08:35:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706776523; cv=none; b=pVSLoRUj1fq7pDbmNqZ3LFP7OGuzLhRCzkCh+dNZHV0aghlePX2zbtPD/iMZ53Ig01J1yWJawc9TB8bfoXzwgIhpn/nJDXsUgjC/uNZVXWHOPVV7UJCyCcZ4vc93xU1O7FaxU0BttYmACSzESN79e7xCZOuEKIqUGaa+/OILzqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706776523; c=relaxed/simple; bh=iHyEQ2KXdIyR0Q46AEjColJDwQljhOsmrS2qirjC+9Y=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=DxNRpwSg3krKzetXwnhLnm313v+alu8OMJKINMCmiwwkxzdLFnM8slL2rqpRjhUuud66avBVyf1bzNj9vzx6HOtlkfvBvLnnJIm7vO4CijqZG1wodv1SLqD5WvvHH7O89EPoK+0sfa5Uph4tJAIqXjd4Etzp6EJRq4U2UIJbYPw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--elver.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bEBBu6Qo; arc=none smtp.client-ip=209.85.208.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--elver.bounces.google.com Received: by mail-ed1-f73.google.com with SMTP id 4fb4d7f45d1cf-55f20f0013eso342763a12.0 for ; Thu, 01 Feb 2024 00:35:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706776519; x=1707381319; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=yDijPStjyNK8y9wipx4jasC/bHAVPAFiOvOL8ps5ECY=; b=bEBBu6QoutCoqsLWpsngjv+oo1yNFPTXbsHeqVzC7HM3RN4rH1SnXEq29VItKpzdjO K7TnMqpRcwhLXX1L2Fvip1yZ8I9sLg4NExhCzEzXynF6Axsde5Frve/XzJiyozAhcPvv +dV+vjalQNWfniGr9du8JuDMtF/qEEXVOan5eynlIhxLjaMEpLWYv9OtYicw8LaCdjuP g12Mafv3mW5x6ymVg+bYFPLTbFqIyfStQJ/jJtLSq0PsQUrj2Qv/PKl8k2lxTDw/FQ7R ttc2KqWYhigd9wb8ai8bA9Js1BZnCFIvROvd1IHW7PopvfDrx2z0HrC6bAndYgsj/4K9 NmeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706776519; x=1707381319; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=yDijPStjyNK8y9wipx4jasC/bHAVPAFiOvOL8ps5ECY=; b=EyN/63UoocmDtTBI/htpswll2fXMeHpMstqO6LOjJxp3DeeC10nLD6N2tsAmboL2gl FbEy7oqByLKktuvfYJYgCv3I3/D6p3xUBexPi/R40LJguSgpAN4G0IzAGLH/0p0u1g/1 YEvH8IFsmoCkXiu6hgoPlmmq6kNvug+1bp/ZiZ9K+H0NLd5NB1240VQV02fOCQrEy50f 3Gv4rV0GidwyESYHTonsaiHAnuSBsQlRpKRru8R+97MzXARaagYii2Jw2hxkMtYnOf0I q+g+u5MKcFyWgH1UiOeJ1PaatrA4QtoY48M8Nj9rSqeB95+yE4CePPjq3k1f1C/1fRab 9DxA== X-Gm-Message-State: AOJu0YygyCx6zRsN9GW8wsOn1/JDj/1OJcYh5tkEOO3HbSBeblf7IqBl MIP4rMO+NeYsMahwLng51llE6TzaEAPcxcpJEO7LxkutvUJ23iSLgV75vU7vGlTMi1NYjw8G1g= = X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:c945:1806:ff53:36fa]) (user=elver job=sendgmr) by 2002:a05:6402:44:b0:55f:2bd3:a896 with SMTP id f4-20020a056402004400b0055f2bd3a896mr10422edu.3.1706776519344; Thu, 01 Feb 2024 00:35:19 -0800 (PST) Date: Thu, 1 Feb 2024 09:31:35 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240201083259.1734865-1-elver@google.com> Subject: [PATCH -mm] stackdepot: do not use flex_array_size() in memcpy() From: Marco Elver To: elver@google.com, Andrew Morton Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, Stephen Rothwell , "Gustavo A . R . Silva" , Kees Cook Content-Type: text/plain; charset="UTF-8" Since 113a61863ecb ("Makefile: Enable -Wstringop-overflow globally") string overflow checking is enabled by default. Unfortunately the compiler still isn't smart enough to always see that the size will never overflow. Specifically, in stackdepot, we have this before memcpy()'ing a stacktrace: if (nr_entries > CONFIG_STACKDEPOT_MAX_FRAMES) nr_entries = CONFIG_STACKDEPOT_MAX_FRAMES; ... memcpy(stack->entries, entries, flex_array_size(stack, entries, nr_entries)); Where 'entries' is an array of unsigned long, and STACKDEPOT_MAX_FRAMES is 64 by default (configurable up to 256), thus the maximum size in bytes (on 32-bit) would be 1024. For some reason the compiler (GCC 13.2.0) assumes that an overflow may be possible and flex_array_size() can return SIZE_MAX (4294967295 on 32-bit), resulting in this warning: In function 'depot_alloc_stack', inlined from 'stack_depot_save_flags' at lib/stackdepot.c:688:4: arch/x86/include/asm/string_32.h:150:25: error: '__builtin_memcpy' specified bound 4294967295 exceeds maximum object size 2147483647 [-Werror=stringop-overflow=] 150 | #define memcpy(t, f, n) __builtin_memcpy(t, f, n) | ^~~~~~~~~~~~~~~~~~~~~~~~~ lib/stackdepot.c:459:9: note: in expansion of macro 'memcpy' 459 | memcpy(stack->entries, entries, flex_array_size(stack, entries, nr_entries)); | ^~~~~~ cc1: all warnings being treated as errors Silence the false positive warning by inlining the multiplication ourselves. Link: https://lore.kernel.org/all/20240201135747.18eca98e@canb.auug.org.au/ Fixes: d869d3fb362c ("stackdepot: use variable size records for non-evictable entries") Reported-by: Stephen Rothwell Signed-off-by: Marco Elver Cc: Gustavo A. R. Silva Cc: Kees Cook --- lib/stackdepot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 8f3b2c84ec2d..e6047f58ad62 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -456,7 +456,7 @@ depot_alloc_stack(unsigned long *entries, int nr_entries, u32 hash, depot_flags_ stack->hash = hash; stack->size = nr_entries; /* stack->handle is already filled in by depot_pop_free_pool(). */ - memcpy(stack->entries, entries, flex_array_size(stack, entries, nr_entries)); + memcpy(stack->entries, entries, nr_entries * sizeof(entries[0])); if (flags & STACK_DEPOT_FLAG_GET) { refcount_set(&stack->count, 1); -- 2.43.0.429.g432eaa2c6b-goog