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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD960C433F5 for ; Tue, 28 Sep 2021 13:39:46 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6204060FC2 for ; Tue, 28 Sep 2021 13:39:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6204060FC2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8A0BB82DA7; Tue, 28 Sep 2021 15:39:44 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="FLGqZGiq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4725782D88; Tue, 28 Sep 2021 15:39:42 +0200 (CEST) Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DD22C83335 for ; Tue, 28 Sep 2021 15:33:12 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=rfried.dev@gmail.com Received: by mail-oi1-x232.google.com with SMTP id x124so29934996oix.9 for ; Tue, 28 Sep 2021 06:33:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=gJorT7oHP7iyJRWZ9HteIHb0gi36XSbG7TJo2mnX/Ac=; b=FLGqZGiqeHL0s5duI2aX7n6KCsG850WyWODvRNhzOs1ZsCFDkTCHTyba63kr3NfoYh Gdl4QbQfUBZncXDlzF/0QVXQvR+08fOWrX+p04BW8nrqCVzGnxQ+u7ovoRV0sA4byQPF Qya91lXuJPlsfbKcY8gOVXwcuqhQ2bpjCElNwZYMSnZe8LyFHUPreP4vR0NdleXMJpeG ZEbAKqJEpyN6Aqoc2fEhfmrSqQr4MQxSb2B26ahPSo6XlPBidzuDKac0Et/pCgC9Psic ePmosGB0MBODCkByuaRZphupZlZeXiISzXc0CD6qAOKCFwGT1w7kk1Bt5XtTbcI80CiQ dhJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=gJorT7oHP7iyJRWZ9HteIHb0gi36XSbG7TJo2mnX/Ac=; b=khXyMbRw+lRjU9ZjWh8RdB6uo1vLIVvf1QyycweoznzEG8IE8YyAOZDMsoHUknp/KN y0QwhOov2pMveqs2cjOMeT7xh5Hnf0YFwBGWyPSfqzl7oQiezz9cKon4F2YtL/rXGyZM xKZxIJ6VOCOHvHskcHy5IeWLgx2LQhYPw7qVUc+JdFYn/QjGTGEK1+743ZYTGnM0MSji OQK5NxHqHe5pPGTCFhWRyHQYetOPVFboGbafMudTu7v0vnn7/mDW9BNfTL2Y8Pr1v1rO LsahE9ewv9lmiBKvNvF7kEZEPe9jMzYcVhF/fCRMBs1k5afKY+tVxaMycEmerMoYEpMu 9Iuw== X-Gm-Message-State: AOAM530Ov6Vd5FbK1ILT3S6u/+39XV/LMmz2I5DgB2uFogoa0fTWIvH1 3aRABDSyZg205jfXDev7I/l/YuCvehSrkJB6IbM= X-Google-Smtp-Source: ABdhPJyp4EEVNHfZiU1gIcwkvTE47hpiyPXVm2o6GIi9Y95IB72zkMi3hP2EhcPOMj3oes9TRrEL8ad3nB9KnUgb3GU= X-Received: by 2002:a05:6808:c4:: with SMTP id t4mr3560638oic.22.1632835991532; Tue, 28 Sep 2021 06:33:11 -0700 (PDT) MIME-Version: 1.0 References: <20210927112205.301876-1-vladimir.oltean@nxp.com> <20210927112205.301876-15-vladimir.oltean@nxp.com> In-Reply-To: <20210927112205.301876-15-vladimir.oltean@nxp.com> From: Ramon Fried Date: Tue, 28 Sep 2021 16:33:00 +0300 Message-ID: Subject: Re: [PATCH 14/25] net: dsa: felix: ensure mii_bus->name is NULL terminated after MDIO_NAME_LEN truncation To: Vladimir Oltean Cc: U-Boot Mailing List , Joe Hershberger Content-Type: text/plain; charset="UTF-8" X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean On Mon, Sep 27, 2021 at 2:22 PM Vladimir Oltean wrote: > > strncpy() simply bails out when copying a source string whose size > exceeds the destination string size, potentially leaving the destination > string unterminated. > > One possible way to address is to pass MDIO_NAME_LEN - 1 and a > previously zero-initialized destination string, but this is more > difficult to maintain. > > The chosen alternative is to use strlcpy(), which properly limits the > copy len in the (srclen >= size) case to "size - 1", and which is also > more efficient than the strncpy() byte-by-byte implementation by using > memcpy. The destination string returned by strlcpy() is always NULL > terminated. > > Signed-off-by: Vladimir Oltean > --- > drivers/net/mscc_eswitch/felix_switch.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/net/mscc_eswitch/felix_switch.c b/drivers/net/mscc_eswitch/felix_switch.c > index 6aa79784460d..4c2e57755967 100644 > --- a/drivers/net/mscc_eswitch/felix_switch.c > +++ b/drivers/net/mscc_eswitch/felix_switch.c > @@ -258,7 +258,7 @@ static void felix_init(struct udevice *dev) > priv->imdio.read = felix_mdio_read; > priv->imdio.write = felix_mdio_write; > priv->imdio.priv = priv->imdio_base + FELIX_PM_IMDIO_BASE; > - strncpy(priv->imdio.name, dev->name, MDIO_NAME_LEN); > + strlcpy(priv->imdio.name, dev->name, MDIO_NAME_LEN); > > /* set up CPU port */ > out_le32(base + FELIX_QSYS_SYSTEM_EXT_CPU_CFG, > @@ -303,7 +303,7 @@ static int felix_probe(struct udevice *dev) > mii_bus->read = felix_mdio_read; > mii_bus->write = felix_mdio_write; > mii_bus->priv = priv->imdio_base + FELIX_PM_IMDIO_BASE; > - strncpy(mii_bus->name, dev->name, MDIO_NAME_LEN); > + strlcpy(mii_bus->name, dev->name, MDIO_NAME_LEN); > mdio_register(mii_bus); > } > > -- > 2.25.1 > Reviewed-by: Ramon Fried