mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
To: Jennifer Berringer <jberring@redhat.com>,
	Sebastian Reichel <sre@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Maxime Ripard <mripard@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org
Subject: Re: [PATCH v2 1/3] nvmem: core: improve range check for nvmem_cell_write()
Date: Wed, 30 Oct 2024 11:43:32 +0000	[thread overview]
Message-ID: <a75f6b72-ea52-46a4-8790-13a4084d53b9@linaro.org> (raw)
In-Reply-To: <b22ce4a7-8480-4d4a-b2c3-0d70c3e05c00@redhat.com>



On 29/10/2024 21:31, Jennifer Berringer wrote:
> On 10/29/24 13:55, Srinivas Kandagatla wrote:
>> if (!nvmem || nvmem->read_only || len != cell->bytes)
>>      return -EINVAL;
>>
>> Does this work?
>>
>> --srini
> 
> I decided against this because it seems potentially useful to allow len to be less than cell->bytes when bit_offset is nonzero. I assumed that was the purpose of the original "cell->bit_offset == 0".
> 
I don't think we support this case.

The reason why this check was initially added is,

If we have bit_offset as non zero or nbits set, cell->bytes is can be 
different to the actual space that is available in the cell, Ex: 2 bits 
with offset of 7 might end up taking 2 bytes. So the existing check is 
correct as it is and valid for cases where the bit_offset is 0.

In this particular case the right solution to the issue is to add more 
sanity checks in case bit_offset is non zero.


This change should help, can you pl  try it.

---------------------------->cut<-----------------------------
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 90c46f6e465d..e6d91a9a9dc5 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1780,6 +1780,9 @@ static int __nvmem_cell_entry_write(struct 
nvmem_cell_entry *cell, void *buf, si
                 return -EINVAL;

         if (cell->bit_offset || cell->nbits) {
+               if (BITS_TO_BYTES(cell->nbits) != len)
+                       return -EINVAL;
+
                 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
                 if (IS_ERR(buf))
                         return PTR_ERR(buf);
---------------------------->cut<-----------------------------

thanks,
srini



> For example, if a cell entry has the following field values
>      { .bit_offset = 4, .nbits = 8, .bytes = 2, ...}
> then it would make sense to call nvmem_cell_write() with len=1 in order to write 8 bits. To allow that, I used "len > cell->bytes" instead of "!=" later in this function:
> 
>>> @@ -1780,9 +1779,13 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
>>>            return -EINVAL;
>>>          if (cell->bit_offset || cell->nbits) {
>>> +        if (len > cell->bytes)
>>> +            return -EINVAL;
>>>            buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
>>>            if (IS_ERR(buf))
>>>                return PTR_ERR(buf);
>>> +    } else if (len != cell->bytes) {
>>> +        return -EINVAL;
>>>        }
> 
> If you disagree with my reasoning then yes, your suggestion works and I can use that instead of what I wrote. None of the current in-tree callers of this function rely on that possibility I described.
> 
> Thank you for the feedback.
> 
> -Jennifer
> 

  reply	other threads:[~2024-10-30 11:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-24 15:40 [PATCH v2 0/3] nvmem: fix out-of-bounds write Jennifer Berringer
2024-10-24 15:40 ` [PATCH v2 1/3] nvmem: core: improve range check for nvmem_cell_write() Jennifer Berringer
2024-10-29 17:55   ` Srinivas Kandagatla
2024-10-29 21:31     ` Jennifer Berringer
2024-10-30 11:43       ` Srinivas Kandagatla [this message]
2024-10-24 15:40 ` [PATCH v2 2/3] nvmem: core: add nvmem_cell_write_variable_u32() Jennifer Berringer
2024-10-24 15:40 ` [PATCH v2 3/3] power: reset: nvmem-reboot-mode: fix write for small cells Jennifer Berringer

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=a75f6b72-ea52-46a4-8790-13a4084d53b9@linaro.org \
    --to=srinivas.kandagatla@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jberring@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mripard@kernel.org \
    --cc=sre@kernel.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

all inboxes | Powered by JetHome®