Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1154898rdg; Wed, 11 Oct 2023 16:49:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE98a8a2JnwBSiHRaFxmu4bgTRYoGsV6HKU2ZFTLF6YUiuRCQDcraxLKf/NFK11oOEV9AOM X-Received: by 2002:a05:6a20:5513:b0:156:e1ce:d4a1 with SMTP id ko19-20020a056a20551300b00156e1ced4a1mr19187084pzb.9.1697068148698; Wed, 11 Oct 2023 16:49:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697068148; cv=none; d=google.com; s=arc-20160816; b=JxjMF2rFQLpiFRGTZ41Z4nliDCs0H/j4XIs33xhk0n223Ha8VwzXF7ftR1Cf4CT8QX 1f812j0aGFuy6RMFRF9gN6BULnbUpviHC/bokKFyVo44V8CxVR15cCHSXwhrYw0YBuOf ZEtKrPhFDabkMuq1Q2KgD8d1CIsd/TdWasYZnWPsHixpvrz+hTpRBz8Bh0EYQDTnKNoP HHOvzyk5rJw8AXpd2A0VKj0sf1JQuhnmmkmP4Phqw8lwOgnXuwT2AnOMWED7gVfPNQuo PI2WtYFTxhMT0HC+UQ4wtqjrFmh9WASEYyMgt3VMktlHr4/2rrrDljTMR3DykzANbjdb 3tdQ== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=3fZzO6YTaJV0qoi4jnsT51ZDdw52HIuE18FHuP2cBJo=; fh=3VrYcK38MEwCLsOJ9atfSLkdGvPT0zwsBXGmjBBsDFw=; b=XwQ3NWUzrXGNnRBGGd1YsMOfkZwg9zTTdpcuabGhFBKkH57pzdUAmdlO8Qrevmco3G oJ/rHHKFfbfpOMV+yDARNUI6PO5QKPVQ+ZrogEQpvWOr8GLFzkSrywcvfUD7teSNXdnn nBO4INXmKjIGPj5Jpd6wLTTZth32DnTSfmZVdKpP6VV+UhAojINe0KdkaIoPLFzdxMsx iw2lPA6YdQ42K68gTG+KoIPaFVBbAfQimUTjQzSuupLgCIoNW3Nzj092/A70zRIjPHyw SbSLuEHri3Xwj08beS86NE9Q5pYvpOTyfhgnF0UJ102Ma0A82VPqv2v9RcVnHKZpLy6g nYeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GWs8H4GP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id 9-20020a170902c14900b001c432da203asi756167plj.270.2023.10.11.16.49.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 16:49:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GWs8H4GP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id D961680DD37A; Wed, 11 Oct 2023 16:49:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233850AbjJKXso (ORCPT + 99 others); Wed, 11 Oct 2023 19:48:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233277AbjJKXsn (ORCPT ); Wed, 11 Oct 2023 19:48:43 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85B139E for ; Wed, 11 Oct 2023 16:48:40 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-690bd8f89baso299324b3a.2 for ; Wed, 11 Oct 2023 16:48:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697068120; x=1697672920; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=3fZzO6YTaJV0qoi4jnsT51ZDdw52HIuE18FHuP2cBJo=; b=GWs8H4GPfMM5mzErD7aVR49hUflIbzB6Mp5gyjZR0kOnPtXM1QXkmieGE4hQV0CLHN LilARLXa2LoksKSwjKnIpM3PNootQSj1CDm1jgy1L9/abt7WLNK2iNtsWqb/p1MptsVI lW7tK7Ki797w20bhiyHu3Eb3QlbUB7AqpQq1s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697068120; x=1697672920; h=in-reply-to: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=3fZzO6YTaJV0qoi4jnsT51ZDdw52HIuE18FHuP2cBJo=; b=MFNLyIYDgVoTWzOmBYXMuxXS28AfzI6//6XZIbJIdEDtWJcX1R+FPdp6nyacSIRO6U aJFVejIpt4gM11CbNBL8SMjfbYvCEfHEFo1Rh7Bx7F2Q0EdAb0KgO5IjaffGPiZ9/Ay2 kHX0NK2RkBx+0x+XayeAyayQuoHSFAYPDRWfd+AGoHtPcX3Kgx5kTllCgFGGtxoQlCTh zeP2f0RfXuG0m7k6QEx4XndiBu/BUf5G1NDvjW8Z1F7BGv1ELuTYmChKADR0bcP2xTu5 L/UmKaEyJOmJkvw/TNiH7L079HW466jpCTVg8b3Vz1nW9WlB+98AHcPLiUQOABAn1YCR Gbvg== X-Gm-Message-State: AOJu0YyYM4Hx+SKJBMIszd7YeoIx07NYHDEb7SvmEF2pVE5WB+P8X6Bc Fuhh91TUruGhZSkl/30bk9JErw== X-Received: by 2002:a05:6a00:1388:b0:68c:a81:4368 with SMTP id t8-20020a056a00138800b0068c0a814368mr23630936pfg.6.1697068119984; Wed, 11 Oct 2023 16:48:39 -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 ka14-20020a056a00938e00b006a680745c8bsm5308310pfb.125.2023.10.11.16.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 16:48:39 -0700 (PDT) Date: Wed, 11 Oct 2023 16:48:37 -0700 From: Kees Cook To: Justin Stitt Cc: Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] qed: replace uses of strncpy Message-ID: <202310111636.94AD8172F@keescook> References: <20231011-strncpy-drivers-net-ethernet-qlogic-qed-qed_debug-c-v1-1-60c9ca2d54a2@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231011-strncpy-drivers-net-ethernet-qlogic-qed-qed_debug-c-v1-1-60c9ca2d54a2@google.com> X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 11 Oct 2023 16:49:05 -0700 (PDT) X-Spam-Level: ** On Wed, Oct 11, 2023 at 10:20:10PM +0000, Justin Stitt wrote: > strncpy() is deprecated for use on NUL-terminated destination strings > [1] and as such we should prefer more robust and less ambiguous string > interfaces. > > This patch eliminates three uses of strncpy(): > > Firstly, `dest` is expected to be NUL-terminated which is evident by the > manual setting of a NUL-byte at size - 1. For this use specifically, > strscpy() is a viable replacement due to the fact that it guarantees > NUL-termination on the destination buffer. > > The next two changes utilizes snprintf() to make the copying behavior > more obvious. Previously, strncpy() was used to overwrite the first 3 > characters of mem_name and type_name by setting a length argument less > than the size of the buffers themselves. This enables, in a roundabout > way, creating a string like "ASD_BIG_RAM" or "ASD_RAM". Let's just use > snprintf() with a precision specifier to hold the name prefix to exactly > 3 characters long. > > To be clear, there are no buffer overread bugs in the current code as > the sizes and offsets are carefully managed such that buffers are > NUL-terminated. However, with these changes, the code is now more robust > and less ambiguous (and hopefully easier to read). > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Cc: Kees Cook > Signed-off-by: Justin Stitt > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > drivers/net/ethernet/qlogic/qed/qed_debug.c | 14 ++++++++------ > 1 file changed, 8 insertions(+), 6 deletions(-) > > diff --git a/drivers/net/ethernet/qlogic/qed/qed_debug.c b/drivers/net/ethernet/qlogic/qed/qed_debug.c > index cdcead614e9f..0a4fd1b04353 100644 > --- a/drivers/net/ethernet/qlogic/qed/qed_debug.c > +++ b/drivers/net/ethernet/qlogic/qed/qed_debug.c > @@ -3192,8 +3192,8 @@ static u32 qed_grc_dump_big_ram(struct qed_hwfn *p_hwfn, > { > struct dbg_tools_data *dev_data = &p_hwfn->dbg_info; > u32 block_size, ram_size, offset = 0, reg_val, i; > - char mem_name[12] = "???_BIG_RAM"; > - char type_name[8] = "???_RAM"; > + char mem_name[12]; > + char type_name[8]; > struct big_ram_defs *big_ram; > > big_ram = &s_big_ram_defs[big_ram_id]; > @@ -3204,8 +3204,11 @@ static u32 qed_grc_dump_big_ram(struct qed_hwfn *p_hwfn, > BIT(big_ram->is_256b_bit_offset[dev_data->chip_id]) ? 256 > : 128; > > - strncpy(type_name, big_ram->instance_name, BIG_RAM_NAME_LEN); > - strncpy(mem_name, big_ram->instance_name, BIG_RAM_NAME_LEN); > + snprintf(mem_name, sizeof(mem_name), "%.*s_BIG_RAM", > + BIG_RAM_NAME_LEN, big_ram->instance_name); > + > + snprintf(type_name, sizeof(type_name), "%.*s_RAM", > + BIG_RAM_NAME_LEN, big_ram->instance_name); I actually think just replacing strncpy with memcpy makes more sense and is what this code intended. It already assumes big_ram->instance_name is always 3 characters (which it is): static struct big_ram_defs s_big_ram_defs[] = { {"BRB", MEM_GROUP_BRB_MEM, MEM_GROUP_BRB_RAM, ... {"BTB", MEM_GROUP_BTB_MEM, MEM_GROUP_BTB_RAM, ... {"BMB", MEM_GROUP_BMB_MEM, MEM_GROUP_BMB_RAM, ... So just copy those three bytes -- no need to call into sprintf for it. > > /* Dump memory header */ > offset += qed_grc_dump_mem_hdr(p_hwfn, > @@ -6359,8 +6362,7 @@ static void qed_read_str_from_buf(void *buf, u32 *offset, u32 size, char *dest) > { > const char *source_str = &((const char *)buf)[*offset]; > > - strncpy(dest, source_str, size); > - dest[size - 1] = '\0'; > + strscpy(dest, source_str, size); > *offset += size; > } This one looks right to me. -- Kees Cook