Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp37280rwd; Tue, 30 May 2023 15:50:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ584uN2cso8MkB7x/FZ63II+RQZDvLomtxe4+G0JE1ZkrYMN6cUrUbNroE7mLWv2ZfIsahM X-Received: by 2002:a17:903:245:b0:1ae:6290:26d with SMTP id j5-20020a170903024500b001ae6290026dmr3867220plh.7.1685487005586; Tue, 30 May 2023 15:50:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685487005; cv=none; d=google.com; s=arc-20160816; b=Jhf9EOZUsKIzISOKl8FI1E/gNdFkXK6/AZ1QAYFKIDG93RAq8m7wxXiqwHzQUI9PT6 F4WlcNbncCWtKGW6D/9NCX60wNRnSU+JsNYYhaoLiIn0aGcop4eaG9zHfuuhX76SNmrC hKgLZ+c9+VxKKZBKXMq2EYgo+Vk+vf7W0kGZJ7DvXI1i+90qyrTZkhuafdymYc6oQXBZ sBnAwm6Nia8uenQp0GCSL/G3YvY+KO2BYAAIdP61mSQdz7scH6lQCfaaUnB6zbkV1FtP xZLMZB141HPJhWoSnsFVd37jfs6HbG0mo8zGBUE1iYaVVo6ah96F+B2NEP3pHPvktrkR RoSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Lpvk61m8kV2Aob9IF7acV7Nqq1ETfXj91v2zUEXV06I=; b=UuxFjg42l5P9luZ6QRT1KHk/mw+QVxAyZyRvYgbFe2DgTWiAdSmUrHk9nSsTBNdjZ9 65t4CkAy2ilzztW9MBtxx2Lx3+18uQus4TGp6kW3LOWndA0kR8gPsUDY8CYtIeTpayBe spPw5b5VEd5euH5aHyQVCnFwbGowdlTQUY1ykK3RSq5XSL+E+C1i46L235Vl3OPxlsDh d/moulkfNwzSZDgE7wb9cf86px4TDJ5AiCnSpg/cPMW+fl0zf+l6d9wAHngR4+43gKDv YqtrhRFzVlSV8QjWK27GhoNoWi2wkTbF6oriWhGll9ktt/d2dqs8KPeXjhueyCRvzxxf f9bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FyuD76bD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j14-20020a170903024e00b001a6a636eb6dsi12868208plh.215.2023.05.30.15.49.49; Tue, 30 May 2023 15:50:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FyuD76bD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229866AbjE3Wov (ORCPT + 99 others); Tue, 30 May 2023 18:44:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230224AbjE3Wot (ORCPT ); Tue, 30 May 2023 18:44:49 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AF36107 for ; Tue, 30 May 2023 15:44:47 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-25676b4fb78so1912382a91.2 for ; Tue, 30 May 2023 15:44:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1685486686; x=1688078686; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=Lpvk61m8kV2Aob9IF7acV7Nqq1ETfXj91v2zUEXV06I=; b=FyuD76bD6Wx498UScEb7wg6Rz8mBirQ2uKMnwVjGlWl+JnnfoMVLYTLTLb6Dz7eCCx 9sg+TmgYzSf87RA5ZS9sdKb8F3kyfoDx2e0u0CYpJVOUANBRnVDrnYwILvBO14qAm2XC 0OdiEZ2im2nHX8lMWTLHHqtRq9xra53m1vgBI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685486686; x=1688078686; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Lpvk61m8kV2Aob9IF7acV7Nqq1ETfXj91v2zUEXV06I=; b=MEl1f5sRruOY2PFqujG7AL/3lVFdsDkxgKNeRreQ8d+VchPyys9y25Fc54sAHP5TYV fS7sK26OO6GyadKbdqjEjNl3dcfQASTmMDsUUqZovJSTJiskvsTMOYuVgoFsbX6RE6ci XA9lyZpCRi8xsBZvPwSYsfmZyZytqOfUrJeGWDQRKF3SU/V9e1dD+nEetRIF87nj1UzM jT10tgPs5SNGpoB38VqshJQ2w9EC1h2nu9w9iZ4ornsfiHvyfQT90o98YSrSAVsKqnDd kGsgGzVDWUy4THDZeF8fg0gCOuAZ7yc6i/jdiR9qHRsVZQ3y1hds0k3s/IA0Qwqb2BID Af/w== X-Gm-Message-State: AC+VfDzsGRLeIcJYzXZ2503cMNGqFT+6x08NMYB7YGQZGLZa+ExLipB4 aGwQjbxvz8ySMQ4iYwtg7b/E4A== X-Received: by 2002:a17:90a:f2c3:b0:256:7e70:309c with SMTP id gt3-20020a17090af2c300b002567e70309cmr3803420pjb.44.1685486686640; Tue, 30 May 2023 15:44:46 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id x2-20020a17090aa38200b0025069c8a151sm9285560pjp.53.2023.05.30.15.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 15:44:46 -0700 (PDT) Date: Tue, 30 May 2023 15:44:45 -0700 From: Kees Cook To: James Bottomley Cc: "Gustavo A. R. Silva" , James Smart , Dick Kennedy , "Martin K. Petersen" , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH][next] scsi: lpfc: Avoid -Wstringop-overflow warning Message-ID: <202305301529.1EEA11B@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 30, 2023 at 05:36:06PM -0400, James Bottomley wrote: > On Tue, 2023-05-30 at 15:30 -0600, Gustavo A. R. Silva wrote: > > Avoid confusing the compiler about possible negative sizes. > > Use size_t instead of int for variables size and copied. > > > > Address the following warning found with GCC-13: > > In function ‘lpfc_debugfs_ras_log_data’, > >     inlined from ‘lpfc_debugfs_ras_log_open’ at > > drivers/scsi/lpfc/lpfc_debugfs.c:2271:15: > > drivers/scsi/lpfc/lpfc_debugfs.c:2210:25: warning: ‘memcpy’ specified > > bound between 18446744071562067968 and 18446744073709551615 exceeds > > maximum object size 9223372036854775807 [-Wstringop-overflow=] > >  2210 |                         memcpy(buffer + copied, dmabuf->virt, > >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > >  2211 |                                size - copied - 1); > >       |                                ~~~~~~~~~~~~~~~~~~ > > > > This looks like a compiler bug to me and your workaround would have us > using unsigned types everywhere for sizes, which seems wrong. There > are calls which return size or error for which we have ssize_t and that > type has to be usable in things like memcpy, so the compiler must be > fixed or the warning disabled. The compiler is (correctly) noticing that the calculation involving "size" (from which "copied" is set) could go negative. The "unsigned types everywhere" is a slippery slope argument that doesn't apply: this is fixing a specific case of a helper taking a size that is never expected to go negative in multiple places (open-coded multiplication, vmalloc, lpfc_debugfs_ras_log_data, etc). It should be bounds checked at the least... struct lpfc_hba { ... uint32_t cfg_ras_fwlog_buffsize; ... }; lpfc_debugfs_ras_log_open(): ... struct lpfc_hba *phba = inode->i_private; int size; ... size = LPFC_RAS_MIN_BUFF_POST_SIZE * phba->cfg_ras_fwlog_buffsize; debug->buffer = vmalloc(size); ... debug->len = lpfc_debugfs_ras_log_data(phba, debug->buffer, size); ... lpfc_debugfs_ras_log_data(): ... if ((copied + LPFC_RAS_MAX_ENTRY_SIZE) >= (size - 1)) { memcpy(buffer + copied, dmabuf->virt, size - copied - 1); Honestly, the "if" above is the weirdest part, and perhaps that should just be adjusted instead: if (size <= LPFC_RAS_MAX_ENTRY_SIZE) return -ENOMEM; ... if (size - copied <= LPFC_RAS_MAX_ENTRY_SIZE) { memcpy(..., size - copied - 1); copied += size - copied - 1; break; } ... } return copied; -- Kees Cook