linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ajay Garg <ajaygargnsit@gmail.com>
To: Bean Huo <huobean@gmail.com>
Cc: ulf.hansson@linaro.org, adrian.hunter@intel.com,
	linux-mmc@vger.kernel.org, linux-kernel@vger.kernel.org,
	beanhuo@micron.com
Subject: Re: [PATCH v1 1/2] mmc-utils: Use memcpy instead of strncpy
Date: Mon, 15 Nov 2021 17:07:45 +0530	[thread overview]
Message-ID: <CAHP4M8W=EE+v7LeORkLSPBChQQjmALNR-Nm+GY6TkUjE4qiiDw@mail.gmail.com> (raw)
In-Reply-To: <937514c9cbb3dd86a659d3fff11fb0f6fa3de8e9.camel@gmail.com>

> I don't understand how above memcpy() overflow/underflow/out-of-bounds?
> would you please provide more specific reason?
>
> memcpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
>
> here lbuf is a char array lbuf[10], and ext_csd is a __u8 array, __u8
> ext_csd[512].

Ok, in the given information parameters, it might work fine at runtime.

But still, using 8 as the magic number makes the code illegible for a
third-party. Plus the code is also unoptimised, eg :

i)
If ext = "abc", then we need to copy (3 + 1) bytes.
However, currently memcpy would copy (8 + 1) bytes.

ii)
If ext = "abcdefghijklmnopqrst", then we need to copy (9 + 1) bytes.
However, currently memcpy would copy (8 + 1) bytes.


> Yes, but please remember that mmc-utils is mainly used for embedded
> platforms, they are not easy/inconvenient to update to the latest
> library to support these two APIs(strlcpy needs libbsd-dev, and strscpy
> needs some one else.).  If we use strlcpy or strscpy, mmc-utils will
> not be portable. Do you know any other API that can be used and make
> code more portable and simpler?
>

Hmm, you can always start adding code locally in your codebase.
Anyways, if you *must* use only "already available code", snprintf is
an alternative.

snprintf(lbuf, sizeof(lbuf), (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);

  reply	other threads:[~2021-11-15 11:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-14 20:43 [PATCH v1 0/2] Two change for mmc-utils Bean Huo
2021-11-14 20:43 ` [PATCH v1 1/2] mmc-utils: Use memcpy instead of strncpy Bean Huo
2021-11-15  7:09   ` Ajay Garg
2021-11-15 10:49     ` Bean Huo
2021-11-15 11:37       ` Ajay Garg [this message]
2021-11-15  8:38   ` Avri Altman
2021-11-14 20:43 ` [PATCH v1 2/2] mmc-utils: Add note for CMDQ_MODE_EN runtime value Bean Huo
2021-11-15  9:07   ` Avri Altman
2021-11-15 15:08   ` Ulf Hansson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHP4M8W=EE+v7LeORkLSPBChQQjmALNR-Nm+GY6TkUjE4qiiDw@mail.gmail.com' \
    --to=ajaygargnsit@gmail.com \
    --cc=adrian.hunter@intel.com \
    --cc=beanhuo@micron.com \
    --cc=huobean@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mmc@vger.kernel.org \
    --cc=ulf.hansson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).