Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1507172rdh; Mon, 25 Sep 2023 15:16:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEIn5k4lOgUoySdpDFTZ7cLvzXc6BYwBGryxduI24WBM/T4KpgM9bCww9BBJ/JMi9y9u9Dd X-Received: by 2002:a17:902:bc47:b0:1c0:afda:540b with SMTP id t7-20020a170902bc4700b001c0afda540bmr6405525plz.8.1695680176644; Mon, 25 Sep 2023 15:16:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695680176; cv=none; d=google.com; s=arc-20160816; b=uOLGEPfQmhjhJtnNweKIlanJ3EO5S4bxCc9vF4k4Hnzs4soVnJXrnJYbovY7dRHN9J 7ghIqxdI+DJi/ARebPuxUxGBEnyg0AyiHLy6ry200JSCQqmzqd8KyQfB17avOxf8PemP wWM0NX+dfOO+V+OOTnWfp28Muc/04ge93zlgFpL1h5pm+8fxnN4SqShqnVEg2q1Mb6nJ qmp3HBZLdux0vxxK9L+soeicC/qoioAJzqHF6W26VVIk+a2yYaN30giXA75WmLXtdmG0 SIBaU4MNDKyciNWZ2TGMNEs/S868n9w4P0+afOYpyDpEkmqWgVFDNP0gOcaeiqI1Ybdl AqfA== 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=8eSsSBJ8Vq4Qi6ZMfaWUsInrqRpqyqKIrqUaEPasYuI=; fh=j0lO7Te/OhW4IC4gpJLJrDYEwNJZIp/H/q2RGUDkjXE=; b=RFygbyY4IL7QY6Ihh7UGAz01rkXhXzThQ6c2NxS9lMhz8dv0AjycvzgDCNz1CMjR9a aI818E8Kntj8WUqa/JidYytLSoeHmIQ8lgO7WdC8inmUrpU+laRGZZgmsD5Lo7oxn5ER kMNcNWr9GaTtJ4733DPXPnPw0Z2sTVBgnVH5MpnVXN9Xposxa2zy4gsf5rtDlwGq7y5R t8rN603lx3QPN7K2faBngvC+0K93m04LtaT80J8vj7aXYAVJJC3cH4xgrNtBgi5tWImf A0DegyISXy7Qyk8MqS99P+aiTULSfFMmPKGveWNk17+n6m+sxoOPluvuSSwz+cmoNMYW 0q4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BI5J06qj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id i4-20020a17090332c400b001b8b437b01esi8006293plr.350.2023.09.25.15.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 15:16:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BI5J06qj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 92200803EE0A; Mon, 25 Sep 2023 11:02:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229735AbjIYSCO (ORCPT + 99 others); Mon, 25 Sep 2023 14:02:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbjIYSCN (ORCPT ); Mon, 25 Sep 2023 14:02:13 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1D9810E for ; Mon, 25 Sep 2023 11:02:06 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1c5ff5f858dso21371245ad.2 for ; Mon, 25 Sep 2023 11:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695664926; x=1696269726; 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=8eSsSBJ8Vq4Qi6ZMfaWUsInrqRpqyqKIrqUaEPasYuI=; b=BI5J06qjkQ7ljgnowfX42qKmMVLVqjKY9nFTkGMX+7kt2qyLHxaU8AHwc8CMMbQTHq EMkXck8eAHwtyetfitVwakO3E8PSKHpQ/NASXyVOj4TgzGVaeJlfZe1bE7oTHtmX9kCt /s5Zl8J292KOITX6AqFJbfYlWR2kjXivcBexo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695664926; x=1696269726; 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=8eSsSBJ8Vq4Qi6ZMfaWUsInrqRpqyqKIrqUaEPasYuI=; b=XWl2foavIE1vuNjbiVu6PUY+ksayEqDMHUjv5E2ujolPx4OJgfiQBtslVPqDNekdT9 4Lu1xDVTvpVz3kw7vsuNroFcmcg5P8p1uBu/HAECNabklSeHBqiL+aI9pFF3YBgLbKYu vEKkCyp5o7TBw9hfgGycgRriInk3Yvdhz4wfxa6vm4sZ/iTwZCQ8ctRpyHiEm/+qt2W7 Beu7GsWEcWNj/vYTTx6McO8LpFzPP9Eb1J4nQU2jGktNT4+UL6+XPIBHmysAsjoA9yQn 0xA0HqclQH3FiI24N00fe7uCWaaonaGOi+32UCqnWZGnI5Jwb9M0Hzb5dkEtuyaAtxUs T1tQ== X-Gm-Message-State: AOJu0YwSb9KwXaSKTQBgpktePju9RkuNUf7bVWGO55nZXXvT30r5I5rG BW62ZXNHwn0/2uec/UoOLiVYEA== X-Received: by 2002:a17:903:2308:b0:1c6:2acc:62ea with SMTP id d8-20020a170903230800b001c62acc62eamr1069374plh.57.1695664926254; Mon, 25 Sep 2023 11:02:06 -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 a11-20020a170902eccb00b001bb28b9a40dsm9161300plh.11.2023.09.25.11.02.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 11:02:05 -0700 (PDT) Date: Mon, 25 Sep 2023 11:02:05 -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 v4] hwmon: (acpi_power_meter) replace open-coded kmemdup_nul Message-ID: <202309251101.1544F5F@keescook> References: <20230925-strncpy-drivers-hwmon-acpi_power_meter-c-v4-1-3bac7534f10f@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230925-strncpy-drivers-hwmon-acpi_power_meter-c-v4-1-3bac7534f10f@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 morse.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 (morse.vger.email [0.0.0.0]); Mon, 25 Sep 2023 11:02:18 -0700 (PDT) On Mon, Sep 25, 2023 at 03:43:23AM +0000, Justin Stitt wrote: > `strncpy` is deprecated for use on NUL-terminated destination strings [1]. > > Let's refactor this kcalloc() + strncpy() into a kmemdup_nul() which has > more obvious behavior and is less error prone. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt > --- > Changes in v4: > - drop +1 from length arg (thanks Kees) > - reword subject line (thanks Kees) > - rebase onto 6465e260f4879080 > - Link to v3: https://lore.kernel.org/r/20230921-strncpy-drivers-hwmon-acpi_power_meter-c-v3-1-307552c6ec3f@google.com > > Changes in v3: > - refactor to use kmemdup_nul() (thanks Thomas and Kees) > - change commit msg to reflect ^ > - rebase onto 2cf0f71562387282 > - Link to v2: https://lore.kernel.org/r/20230919-strncpy-drivers-hwmon-acpi_power_meter-c-v2-1-8348432d6442@google.com > > Changes in v2: > - use memcpy over strscpy (thanks Kees) > - Link to v1: https://lore.kernel.org/r/20230914-strncpy-drivers-hwmon-acpi_power_meter-c-v1-1-905297479fe8@google.com > --- > drivers/hwmon/acpi_power_meter.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/drivers/hwmon/acpi_power_meter.c b/drivers/hwmon/acpi_power_meter.c > index fa28d447f0df..c13b5c8a0433 100644 > --- a/drivers/hwmon/acpi_power_meter.c > +++ b/drivers/hwmon/acpi_power_meter.c > @@ -796,14 +796,13 @@ static int read_capabilities(struct acpi_power_meter_resource *resource) > goto error; > } > > - *str = kcalloc(element->string.length + 1, sizeof(u8), > - GFP_KERNEL); > + *str = kmemdup_nul(element->string.pointer, element->string.length, > + GFP_KERNEL); This whitespace looks weird -- I'd expect this to line up with "element", like this: > + *str = kmemdup_nul(element->string.pointer, element->string.length, > + GFP_KERNEL); Otherwise, yes, looks correct. -- Kees Cook