Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1645463lqa; Mon, 29 Apr 2024 14:58:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWGZ5CjvyET6pLrsmkNH0sZV3IpCnWsWhGWANqXAHrkRDUH2kVCrc4bmvSHutG0X1m1wVAMa8K2Rg5sa85o8gHFjQa0UfrQ3gDx7fPWyQ== X-Google-Smtp-Source: AGHT+IEZBacFQccMSHZ+n9uEHn4lEmDchrJxXxbwMkWGPFMD0kyToaQztBuDnCEgSvA58tI3DnJz X-Received: by 2002:a05:6402:348b:b0:56d:f7ce:e879 with SMTP id v11-20020a056402348b00b0056df7cee879mr9233594edc.37.1714427915874; Mon, 29 Apr 2024 14:58:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714427915; cv=pass; d=google.com; s=arc-20160816; b=k84SdRASf4gQuaf4b5WjXCe5WNxF2y2IU/D7mKiIcaIPgKpnsYy2UGxspJYY9kQYFv BGSl82aRmY5sazy1OuUE4iw/3Q4tC/UPIqOJFBeu6xAaCkbheAyJVr8KgZkD2mHeGa8x L5xcyKFFqx+rXt2+NqpupI70Z8czE+Wj/y1jFKGEr97FZNQnjSGhzfq3KQgfkvUNbHbU M5CwUvD6zUGlB9txZk1YsEB27vC+TE4sR9ldpJHqjdhiNvS1c+29LlMLXRSKzuV0evRQ 8IV3vXeKUBmIkrdPZApSPSXGe5kVbXX7gnjMQP8raMGXEguunY8yJBoKM62QsjM07Xpr bFIQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Pt4pmVDe1gHqk8GSYJUn0euBaNvq8Bf6JSXO1Xsdf5U=; fh=Sxhz/eHCWnL0UgHoDwd9y9RpVxP5st7V2j5DLx50lfI=; b=HqlDIrxCSYlLs+LOq06IdM1/EyfXLabgs2WxryZ4o0ErOA7X/hcBfLAvlNyQriQKDr Uh/90eLJpy8AQKUBYNHfldhuoV4GFnZuajmSfSD6XcixBZEbRJN/afpakgU8+NDQRszf OzgqjJSr42cGbxuXEoJEIN0Mia2rFzX5olOAGYKiJGcAZZI884EtpkJ/eHdVoaQjGKGa wXZ0gMAEwN2uX7OKMBsRBbj31H0ENCyrK3PAcYuQcGr/wYIdtTH5ishY1QlnueVWDrGL 22CghUavH8TPLdP6igBRu7CKgDRUHC4Qv6FHcW+c9RsbgBjC0hDkySh0fRZE/Bc8mTIi 403g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZDD1p7Ek; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-163083-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163083-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u2-20020a509502000000b00572207fd7e2si8361855eda.184.2024.04.29.14.58.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 14:58:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163083-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZDD1p7Ek; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-163083-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163083-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 99CDC1F227A6 for ; Mon, 29 Apr 2024 21:58:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CF3DD19069A; Mon, 29 Apr 2024 21:58:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ZDD1p7Ek" Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12B84190682 for ; Mon, 29 Apr 2024 21:58:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714427909; cv=none; b=qJ/+BCUJfCO0nhOQ11DKr5HiE0uzeDUyz16ASRZJzbKLP5nDnB7VUkBGfMXiSiJdg8BOOsRybezIpBOsx4Kl1+2dGPOQzBwsaeHpF23o9jL2F43x0qc0jLZlb1fxlHQNemgAq1oPGFzXc75EwNVtRjE7uBrEuP4JXCtolDos/iA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714427909; c=relaxed/simple; bh=i6lqpid3/ifH983zglzaRmJlc7nvZzdwdzL3SlBT1GI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uSDMls2euWv4SmRQ5T8Qi1hpKRb3WhzJAShFzx5MBi5oNIWNFCpvEueyYUNS1RSifJWrRvYyq79IJlXwpXgKdpPii2Mjczy9Hbz2gqmLj0W6U+kL9kfikvdnLiCPJ8BFgmLFUyggM5hExo0jHn1pWHBzXzZgUrEeAz9mlaT8Idg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=ZDD1p7Ek; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6f28bb6d747so4276257b3a.3 for ; Mon, 29 Apr 2024 14:58:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714427906; x=1715032706; 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=Pt4pmVDe1gHqk8GSYJUn0euBaNvq8Bf6JSXO1Xsdf5U=; b=ZDD1p7EkJ56UE59hBbkIXdaJny5SS7tlrgfC95duBrz9Jy/R1boGAkiR3UTX8rLp3l CD5bhyv129qrcu3CneUU2vuGss+Nj5M+f8MMPk3Cvu5PMnE4s1Y8FYNDfCgz2jlR1Gu6 MQkbj65WMSZJ2GGDsfviDw40TgIDVVVQgynXQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714427906; x=1715032706; 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=Pt4pmVDe1gHqk8GSYJUn0euBaNvq8Bf6JSXO1Xsdf5U=; b=NlH7WlySUEbuS8xfKsatrdMT2OjkR6xFQtFs3TxGNY0GVzIsDTdzGLZUuPvHdqkh6d mp7ns19QdZLDFv/ye1Lqw3xUDfrh6dh8/6ZA99tD2MVFuyLOz23eBzuFCEcg6wb5at7y DyW+mK9hCPRj2/rgBeB6Y6VzwIcszKRdaWsiZ5Im3U4sKVcpu+TwWiF29jCncpUcDwwI ZEYvUODTpQNY928nEyAtOUM5zlqM6t2shKv3+wPhtH+EixSEAVgPrU2d9C6fJTMw4tQE simWshJyEC2pTS2QqZq8wS5UPFVgWs9dMbSSW7Kap68JtXcH27Dw0ApDrkSCb0DC+Jj1 yiDA== X-Forwarded-Encrypted: i=1; AJvYcCXZsODdZN/4dICzk1qlCBmk5uVg9ORscet8iJoMyjAuBj3R+C5pF+El059WyE5OrsY6y2mGk2bleBt1UoQhK+hSvOx9IPnebj6HLImU X-Gm-Message-State: AOJu0Yz0SPZz2FliLs/mokxQTIvK2UMAkOL8B1pOYQIPSYW3A77XUd40 l1R87sa1K1k0LN+FRvqyJQUo6YfSLBvKPvwxrPH4k9tJSdx5+2vt8cHM8wE/lw== X-Received: by 2002:a05:6a00:2315:b0:6ec:ec8f:d588 with SMTP id h21-20020a056a00231500b006ecec8fd588mr13038861pfh.16.1714427906391; Mon, 29 Apr 2024 14:58:26 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id ff3-20020a056a002f4300b006eab6f3d8a9sm19709557pfb.207.2024.04.29.14.58.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 14:58:25 -0700 (PDT) Date: Mon, 29 Apr 2024 14:58:25 -0700 From: Kees Cook To: Justin Stitt Cc: "Rafael J. Wysocki" , Len Brown , Pavel Machek , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] PM: hibernate: replace deprecated strncpy with strscpy Message-ID: <202404291457.8E79DC5@keescook> References: <20240429-strncpy-kernel-power-hibernate-c-v1-1-8688f492d3e6@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240429-strncpy-kernel-power-hibernate-c-v1-1-8688f492d3e6@google.com> On Mon, Apr 29, 2024 at 08:50:30PM +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 kernel config option is simply assigned with the resume_file > buffer. It should be NUL-terminated but not necessarily NUL-padded as > per its further usage with other string apis: > | static int __init find_resume_device(void) > | { > | if (!strlen(resume_file)) > | return -ENOENT; > | > | pm_pr_dbg("Checking hibernation image partition %s\n", resume_file); > > Use strscpy [2] as it guarantees NUL-termination on the destination > buffer. Specifically, use the new 2-argument version of strscpy() > introduced in Commit e6584c3964f2f ("string: Allow 2-argument > strscpy()"). > > 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. > > Found with: $ rg "strncpy\(" > --- > kernel/power/hibernate.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c > index 43b1a82e800c..0a213f69a9e4 100644 > --- a/kernel/power/hibernate.c > +++ b/kernel/power/hibernate.c > @@ -1361,7 +1361,7 @@ static int __init resume_setup(char *str) > if (noresume) > return 1; > > - strncpy(resume_file, str, 255); > + strscpy(resume_file, str); > return 1; > } > Yup, this looks correct to me. resume_file is: static char resume_file[256] = CONFIG_PM_STD_PARTITION; Reviewed-by: Kees Cook -- Kees Cook