Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1618510rdb; Mon, 2 Oct 2023 15:50:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTkck/hvPRtv4jEA8KMl6AbCTpeSc41NBO3W2H9HPuQ/EyCNA453nCyMi+RxaKvQJ2Wzei X-Received: by 2002:a67:f24f:0:b0:44e:89bd:9a5c with SMTP id y15-20020a67f24f000000b0044e89bd9a5cmr9462207vsm.10.1696287038722; Mon, 02 Oct 2023 15:50:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696287038; cv=none; d=google.com; s=arc-20160816; b=y2l1Tx3qULTOc4y35IVyEGZUaTaBl0WqPiqfsV4oqRbEAyM3WBlLfD26/C4JU6E8Yo vwDmJTFGuq1hSwulvqhSK+klZWpLLuUjSCuA0McY26ANmGBQxf9fpcfZ0pdY+28Kojuc tKRF4ZPF2uTbem8jlwiwi7FIVSyP9oV78urP7qS+ct9Wu9liFiazLC2wzss98HxWB9do McM9kXwYw6CpxKeWWAsmyFoxN1g0M/WjRcpPAbXwVVaEA3is9dOQ4Wxlbdg9p1qjQOYS w9dvVi8diNkawweo+iLpZGRC2aZgvcVUpz4Ern66FLlobOlSkeL7oa/h8cMECrMrSEmh zDQQ== 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=iOYDr52agfsu91xM8m2iaWWGVv9lXmRJ4r8U24TP67A=; fh=sYA5e6I9500gpItt2xaM++GG/qFhdiaA2l9Z0I6ojGw=; b=km6WrrsNaKJ8LFgZfYb/fTldduok9fIPOSye52GHfngVXOcSrmah5lD/eMeD3BIRg1 Dpz6SV0wT9Ui2iqFY/D9jB2pEH3TBfDUx3UtOhxyEWS4MdrhzYe7zguQ/IJxwLNiyshQ 91wEFHu9Hv23/c+BIHNRiwNZCxxAI0nUPHK6IgnqpaP3vaGNimsaasu+jTC7QgJG0Hiz 3iGoYF70eNaw1144UAb7i21cNPRB1RhMK7ELF6gQ9rWQpkUebG3UTjc6D1E1Ec831czB seWRI96jAkykU0/ms22KsE+jMk0D73cc/6I32u+CSYTheAeyxqrh8ITcsf7IShf/5xFi JuaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TBeNGCIc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id v3-20020a655c43000000b005859bd29c5bsi9005789pgr.723.2023.10.02.15.50.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 15:50:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TBeNGCIc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id A8DFD80320ED; Mon, 2 Oct 2023 11:01:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231499AbjJBSBK (ORCPT + 99 others); Mon, 2 Oct 2023 14:01:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231334AbjJBSBH (ORCPT ); Mon, 2 Oct 2023 14:01:07 -0400 Received: from mail-yb1-xb2c.google.com (mail-yb1-xb2c.google.com [IPv6:2607:f8b0:4864:20::b2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10086AC for ; Mon, 2 Oct 2023 11:01:05 -0700 (PDT) Received: by mail-yb1-xb2c.google.com with SMTP id 3f1490d57ef6-d81b42a3108so28187276.1 for ; Mon, 02 Oct 2023 11:01:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696269664; x=1696874464; 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=iOYDr52agfsu91xM8m2iaWWGVv9lXmRJ4r8U24TP67A=; b=TBeNGCIcbZHJfViQR0KyBosia+sj+2WtfbBFvMHF/D9rLetrPqPiTyi3yBEFvywoUY 46KAqMzA2jFHYyEoMdQHaCjIySFW8+y2jJ53ruLBZE1DdGaHbN4ImNjkLi+G9Tjppkqp Z+6aHB58WdKYcLmtuyCPEYw06PtMcAVirMc/E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696269664; x=1696874464; 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=iOYDr52agfsu91xM8m2iaWWGVv9lXmRJ4r8U24TP67A=; b=FH2oFktLIUQWVBltZPHZZ7ym7a2ajxBTyykHfLkftsQbb9WGeF6FDXJOmEtF3piPkl 5cWXsqINYnnOSusVlgRCDGPls9PSt+3mI3XJ7krBbMm9OG6cvO8B/MsyAgDT4IjkeYEe IneNFxHT2t2dAVLfYauz+OpwTTPK4YWpY8qK+5DEEknVn8jP6gzde2iFLkhXRRuE4wEf EWMfYCjHdezFGfo0ZBqM+45JKYS+NZk4xNMOI7ERUbk61gKw86wHJTNb/oEInOjhR8Wp YsltO8QAmVKg4tVMPjJdyw0n8Mow9hbmegsuqZjpz3NK19lCOWrwEPg6PG0+Fyvt2VMx PFvQ== X-Gm-Message-State: AOJu0YzpfVc1zR1TOse85DFcyndIJQWgq9dnMyKoQIpzMw+rgCrsf1/h Ym8cv6fP5IviJMr+DXH3NuaC1A== X-Received: by 2002:a25:d6d2:0:b0:d80:4533:9556 with SMTP id n201-20020a25d6d2000000b00d8045339556mr12092359ybg.63.1696269664239; Mon, 02 Oct 2023 11:01:04 -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 f18-20020a637552000000b005777a50c14csm19617226pgn.0.2023.10.02.11.01.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 11:01:03 -0700 (PDT) Date: Mon, 2 Oct 2023 11:01:02 -0700 From: Kees Cook To: Justin Stitt Cc: Brad Warrum , Ritu Agarwal , Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] ibmvmc: replace deprecated strncpy with strscpy Message-ID: <202310021058.C9E46875@keescook> References: <20230927-strncpy-drivers-misc-ibmvmc-c-v1-1-29f56cd3a269@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230927-strncpy-drivers-misc-ibmvmc-c-v1-1-29f56cd3a269@google.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Mon, 02 Oct 2023 11:01:19 -0700 (PDT) On Wed, Sep 27, 2023 at 05:52:14AM +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. > > A suitable replacement is `strscpy` [2] due to the fact that it > guarantees NUL-termination on the destination buffer without > unnecessarily NUL-padding. This is weird -- I think hmc_id needs to be marked __nonstring, and the commit log should talk about how it appears to be not %NUL terminated. The bounce buffer is weird -- it seems like hmc_id could be made a regular C string by being changed to MHC_ID_LEN + 1, and then everything would work correctly without needing a bounce buffer, etc. (Well, it would need explicit %NUL termination after the copy_from_user()). But if that refactor doesn't sound right to maintainers, then I'd agree that this patch is fine (though capturing the __nonstring bit might be nice): Reviewed-by: Kees Cook -Kees > > 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 > Signed-off-by: Justin Stitt > --- > Note: build-tested only. > --- > drivers/misc/ibmvmc.c | 4 +--- > 1 file changed, 1 insertion(+), 3 deletions(-) > > diff --git a/drivers/misc/ibmvmc.c b/drivers/misc/ibmvmc.c > index 2101eb12bcba..6d9ed9325f9f 100644 > --- a/drivers/misc/ibmvmc.c > +++ b/drivers/misc/ibmvmc.c > @@ -1249,9 +1249,7 @@ static long ibmvmc_ioctl_sethmcid(struct ibmvmc_file_session *session, > return -EIO; > } > > - /* Make sure buffer is NULL terminated before trying to print it */ > - memset(print_buffer, 0, HMC_ID_LEN + 1); > - strncpy(print_buffer, hmc->hmc_id, HMC_ID_LEN); > + strscpy(print_buffer, hmc->hmc_id, sizeof(print_buffer)); > pr_info("ibmvmc: sethmcid: Set HMC ID: \"%s\"\n", print_buffer); > > memcpy(buffer->real_addr_local, hmc->hmc_id, HMC_ID_LEN); > > --- > base-commit: 6465e260f48790807eef06b583b38ca9789b6072 > change-id: 20230927-strncpy-drivers-misc-ibmvmc-c-534349716fa4 > > Best regards, > -- > Justin Stitt > -- Kees Cook