Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4531666rdb; Fri, 15 Sep 2023 05:20:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEem+R8oQiu8xEKkzjoiB3EmG1mbrrrBde0yJ9Wx97H9eprMsmBw9//zR6VnTREXhrJn8X3 X-Received: by 2002:a05:6358:5e02:b0:133:7c4:e752 with SMTP id q2-20020a0563585e0200b0013307c4e752mr1647576rwn.26.1694780401821; Fri, 15 Sep 2023 05:20:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694780401; cv=none; d=google.com; s=arc-20160816; b=WLCj6T7e/Fw+QP9ziHRY9paSO17VsYdIlK3W2tw2OtVgdrMjjdQHTCjmWKnnHLsxJj GDcUgW9kHYlNygi6vgN2jO1HP84OzbzUe5gISBUQ9LfLWqAT7RI5oZYh2dFYuiNiubDe w/o43qfN0b8NgoIPoMn9yHK+QGvBr8k1K3zPBgN7dFg0zjdQaS3xCem4CVgL4U/SW/5z u+cxUgrLzIjFFTlS8wP80/HkmHlBcWJzOKjYa2kj3zkPnRxuOBWUt6o2/EZ6nnVf+2sL fZwb6nKWGj2R/xmGCxNjMn0AedD/w1HelBOUgNrh6t8NVWwjwoiJ1oFxEn0lpMMIAcqO eKGg== 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=NmkmWy8HC0PB4d8aL+vtauicOqu3O/LrjvNIY4PdPFI=; fh=j0lO7Te/OhW4IC4gpJLJrDYEwNJZIp/H/q2RGUDkjXE=; b=WdeYRGYUXp+a+x4yIQqqDR5jMZ/27lvIhERkPRWVBqdM+x9H6Fqh+mOVXWGZvIy+PM PYXbDo8aD7kPhw265lCyuVwATaUXjRurQU8cEtOYWwnHPVT7ue3QZUqSUnQSqq+oEu7C G091aBqOAQo5ufmoGkx0ppd3tPI4Ft2UkRkbzQe4rNackFM7onBY9XlgOe0mKFui6a9l bcskH7jKHK32s15JWuNRCPMcElMyHankT9c7z2zKNpzQN0a/eOkfzrnD6DAJsl37PbLt JXgAHgGUEJ+hk+U4emsPxlOnSIwIxBsnZyC02c4/RvMvQr9M1IN+xiL8klL53ILg37Ft SQdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=muXShcMN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id bk13-20020a056a02028d00b0056418c0864csi3074586pgb.456.2023.09.15.05.20.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 05:20:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=muXShcMN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 36A5F82F7FB5; Thu, 14 Sep 2023 22:17:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229803AbjIOFRj (ORCPT + 99 others); Fri, 15 Sep 2023 01:17:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbjIOFRh (ORCPT ); Fri, 15 Sep 2023 01:17:37 -0400 Received: from mail-oo1-xc2f.google.com (mail-oo1-xc2f.google.com [IPv6:2607:f8b0:4864:20::c2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C708F2711 for ; Thu, 14 Sep 2023 22:17:32 -0700 (PDT) Received: by mail-oo1-xc2f.google.com with SMTP id 006d021491bc7-572a7141434so963046eaf.2 for ; Thu, 14 Sep 2023 22:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1694755052; x=1695359852; 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=NmkmWy8HC0PB4d8aL+vtauicOqu3O/LrjvNIY4PdPFI=; b=muXShcMNAoTCIXmobni5kuwx5Ye5EVB5tT5IjVRYHw61A6162M3dyfTvs4Zvy8wL5T k8jvzC4OEBuRiOK4dJnxS2HY85hoT8adZgYz7xMA2A9umGV0SDJueZUgNvtKdZFwHhP4 mo/y4ntORGgfSgFBd/yevG4NsMJovrzGnkT9M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694755052; x=1695359852; 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=NmkmWy8HC0PB4d8aL+vtauicOqu3O/LrjvNIY4PdPFI=; b=Y86wz3bcVeavsPn4z6+uSQgBUjVScl5AUir4AhlhZ+/OPD2/Czb21V40YHmUe3jcRr mcCVLi03Q0HKE47lKlGi8irTxxXUURdSyhmb+xXuN4zMEU63wHh4fS+IeKAfDe8+VDNG yCD+/E5WEYRgsVKEn6BtwOrg3kj6K7SSprpgKStEXEETa2taMnvxrxJjovoqqpTPdJuw lMGF8ORkdFCP1uth7BgDDkhkNy5AL7FMZNLBLNW90NJIl06M25g/GfGRoOQ8ek4/DA4Z mD01oVkOZjP9woAZKIr93RF0QjEfWCnEz+1V25yZD1BwFCDKZHqTIcneaxgTWpnfBxlx 6rnA== X-Gm-Message-State: AOJu0YyUtHeN747DTg5Oqlco+OnrH4yT0zQQQ/+pUAbzWAIqjNr1Tz9O iBu03XU99ae2kQfhoI5lHZ4XNQ== X-Received: by 2002:a05:6358:1cb:b0:141:10f4:1cbc with SMTP id e11-20020a05635801cb00b0014110f41cbcmr645790rwa.14.1694755052061; Thu, 14 Sep 2023 22:17:32 -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 q7-20020a63ae07000000b005634343cd9esm1977741pgf.44.2023.09.14.22.17.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 22:17:31 -0700 (PDT) Date: Thu, 14 Sep 2023 22:17:31 -0700 From: Kees Cook To: Justin Stitt Cc: Jean Delvare , Guenter Roeck , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] hwmon: refactor deprecated strncpy Message-ID: <202309142216.D52AFDF@keescook> References: <20230914-strncpy-drivers-hwmon-acpi_power_meter-c-v1-1-905297479fe8@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230914-strncpy-drivers-hwmon-acpi_power_meter-c-v1-1-905297479fe8@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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 lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 14 Sep 2023 22:17:51 -0700 (PDT) On Thu, Sep 14, 2023 at 10:50:15PM +0000, Justin Stitt wrote: > `strncpy` is deprecated for use on NUL-terminated destination strings [1]. > > 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. > > 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 > --- > drivers/hwmon/acpi_power_meter.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/hwmon/acpi_power_meter.c b/drivers/hwmon/acpi_power_meter.c > index fa28d447f0df..9bafe7c85ff7 100644 > --- a/drivers/hwmon/acpi_power_meter.c > +++ b/drivers/hwmon/acpi_power_meter.c > @@ -803,7 +803,7 @@ static int read_capabilities(struct acpi_power_meter_resource *resource) > goto error; > } > > - strncpy(*str, element->string.pointer, element->string.length); > + strscpy(*str, element->string.pointer, element->string.length); This will truncate the destination by 1 training character. This is better replaced with just memcpy() (all the size information has already been calculated and %NUL termination space made in the kcalloc() above it. -Kees > str++; > } > > > --- > base-commit: 3669558bdf354cd352be955ef2764cde6a9bf5ec > change-id: 20230914-strncpy-drivers-hwmon-acpi_power_meter-c-c9f2d8053bef > > Best regards, > -- > Justin Stitt > -- Kees Cook