From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB920CDB465 for ; Sat, 14 Oct 2023 16:13:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233186AbjJNQNb (ORCPT ); Sat, 14 Oct 2023 12:13:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230016AbjJNQNa (ORCPT ); Sat, 14 Oct 2023 12:13:30 -0400 Received: from vps0.lunn.ch (vps0.lunn.ch [156.67.10.101]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A4A0A2; Sat, 14 Oct 2023 09:13:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lunn.ch; s=20171124; h=In-Reply-To:Content-Disposition:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:From:Sender:Reply-To:Subject: Date:Message-ID:To:Cc:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Content-Disposition:In-Reply-To:References; bh=mzqMfzRxzG70Knwvi6tm0b5cGlVoxpozVCu8YLy3yG8=; b=0xP1chpQpytJmfkOYJUkI+Hvv4 4GO8LpdIGkE3fvVpwIfyzJn4nTntAloMj1KN06CCFJECEXAI/KW0+lrg27r/RXbg2LlJua3sHeRId 1eRBFJJQhorhTd0eDdQk6ZAWLwHa2dgjDHeIO9GesIrAT2YkDP8bjB4H4bA7xIp1NZq8=; Received: from andrew by vps0.lunn.ch with local (Exim 4.94.2) (envelope-from ) id 1qrhGQ-002BXN-2v; Sat, 14 Oct 2023 18:13:18 +0200 Date: Sat, 14 Oct 2023 18:13:18 +0200 From: Andrew Lunn To: Justin Stitt Cc: Florian Fainelli , "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] net: cpmac: replace deprecated strncpy with strscpy Message-ID: <985a2962-c327-4115-bc59-b1bc896be9b3@lunn.ch> References: <20231012-strncpy-drivers-net-ethernet-ti-cpmac-c-v1-1-f0d430c9949f@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231012-strncpy-drivers-net-ethernet-ti-cpmac-c-v1-1-f0d430c9949f@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 12, 2023 at 08:53: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. > > We expect mdio_bus_id to be NUL-terminated based on its use with > snprint(): > | snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT, > | mdio_bus_id, phy_id); > > Moreover, as this is the only use of mdio_bus_id, we can see that > NUL-padding is not required. > > Considering the above, a suitable replacement is `strscpy` [2] due to > the fact that it guarantees NUL-termination on the destination buffer > without unnecessarily NUL-padding. > > Note that for the replacement involving cpmac_mii->id, the source > buffer's length is equal to MII_BUS_ID_SIZE which could result in a > buffer overread. However, there is no buffer overread since "cpmac-1" > (the string copied into cpmac_mii->id) is smaller than MII_BUS_ID_SIZE > thus meaning the previous usage of strncpy() here did _not_ have any > overread bugs. Nonetheless, let's still favor strscpy() over strncpy(). > > 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 Reviewed-by: Andrew Lunn Andrew