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 46BE7C433EF for ; Tue, 28 Sep 2021 13:37:42 +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 C4A7E60FF2 for ; Tue, 28 Sep 2021 13:37:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C4A7E60FF2 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 F317983394; Tue, 28 Sep 2021 15:36:21 +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="Lm1q6FCc"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 47F5B831E3; Tue, 28 Sep 2021 15:35:43 +0200 (CEST) Received: from mail-oo1-xc34.google.com (mail-oo1-xc34.google.com [IPv6:2607:f8b0:4864:20::c34]) (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 B34B383321 for ; Tue, 28 Sep 2021 15:34:10 +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-oo1-xc34.google.com with SMTP id w6-20020a4aa446000000b002b5795cca03so4603732ool.6 for ; Tue, 28 Sep 2021 06:34:10 -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=ah1KQ8fszC0HDfkhr2ZscxFEsf4dukARwKzcdXX8TGA=; b=Lm1q6FCczXZykVlRNtX8VOoViBVUHSdDtxW7rMPmUw3a0LshqJR8Ora/10FpNRdJO2 DBKyA9c/IueLGU3+Zicpp1q7qU7l0tMZnFxJwWOWs76OfyckTC/QIOtWnvKC2e4ArFSD mdxfg8zXTc5075CT2MHqm7Abb1aY322wO9mNkRsgurlxts165+eOJgE3V2UIli25k76r ZQBCxtS7bOVBmNWN2V/pgg3R9rgvMJ4m30TTe3xwj417EsLb1tpcNrL03Ugzw3U7iEEe H1dbRjY1ffdotBvvFZW5KPswmDEY6GmsmpB5Tm+RBlMSQmMjaKn1w6zXTFWTd18bgjVx pFVg== 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=ah1KQ8fszC0HDfkhr2ZscxFEsf4dukARwKzcdXX8TGA=; b=eLdgTdT2kjcTiLP+sndw/xUqGDEEmsTCwKoO27pEO9nElnEfrxnlN80mkLCGgcrBkZ PgeqbgvOOGrogrfE5cDhVf8csCi+oOalvTFeIZ0pu0hg/kPT2aOgBK5iGLQ3QN2pfqxx 0VMeKFkvZXuYRpMsgJCxJA7wA2ajrvqes/z+lXVFHCDo3VVKcqaHU46Xrw5yIv9+3MEx xtwO46uWo2E8LXhFCuWZHa/CD1lSTmU1sVgzmZ5yCw4q9w/3zIBTrC7344CGSdBOnAgq TICmbs04lTjQUcc4YrPD/QABwr/7Z9id7U5b6y/sSryKVXgTQbhuD+lpi3R7HBcsRemm GDxQ== X-Gm-Message-State: AOAM531I6AQQgKKLz0Zpa/qWgSJUV6hnZsA/Qni5e9iQNvFcIYKG/DnD 7CqW6rb5JL4tMT9Q7ukVA3YAPol1rtR8zcTGTAg= X-Google-Smtp-Source: ABdhPJxfzsNSeh4VZDblnKG0uim/E3qBDuByLKA6wRNVR8WE9/nPNylJUOxrTsVFj2E35haWHCDfrU2IfAUbg+jFv0I= X-Received: by 2002:a4a:e292:: with SMTP id k18mr4970983oot.80.1632836049187; Tue, 28 Sep 2021 06:34:09 -0700 (PDT) MIME-Version: 1.0 References: <20210927112205.301876-1-vladimir.oltean@nxp.com> <20210927112205.301876-24-vladimir.oltean@nxp.com> In-Reply-To: <20210927112205.301876-24-vladimir.oltean@nxp.com> From: Ramon Fried Date: Tue, 28 Sep 2021 16:33:57 +0300 Message-ID: Subject: Re: [PATCH 23/25] net: dsa: ensure port names are NULL-terminated after DSA_PORT_NAME_LENGTH 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 DSA_PORT_NAME_LENGTH - 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 > --- > net/dsa-uclass.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/net/dsa-uclass.c b/net/dsa-uclass.c > index 9b8ae1e82b92..8db0de686e2a 100644 > --- a/net/dsa-uclass.c > +++ b/net/dsa-uclass.c > @@ -221,7 +221,7 @@ static int dsa_port_of_to_pdata(struct udevice *pdev) > > label = ofnode_read_string(dev_ofnode(pdev), "label"); > if (label) > - strncpy(port_pdata->name, label, DSA_PORT_NAME_LENGTH); > + strlcpy(port_pdata->name, label, DSA_PORT_NAME_LENGTH); > > eth_pdata = dev_get_plat(pdev); > eth_pdata->priv_pdata = port_pdata; > @@ -433,7 +433,7 @@ static int dsa_post_bind(struct udevice *dev) > struct dsa_port_pdata *port_pdata; > > port_pdata = dev_get_parent_plat(pdev); > - strncpy(port_pdata->name, name, DSA_PORT_NAME_LENGTH); > + strlcpy(port_pdata->name, name, DSA_PORT_NAME_LENGTH); > pdev->name = port_pdata->name; > } > > -- > 2.25.1 > Reviewed-by: Ramon Fried