Skip to content

libnvme: account for the appended spaces in libnvmf_get_entity_version() - #3918

Open
prabhakarpujeri wants to merge 1 commit into
linux-nvme:masterfrom
prabhakarpujeri:fix-entity-version-bounds
Open

libnvme: account for the appended spaces in libnvmf_get_entity_version()#3918
prabhakarpujeri wants to merge 1 commit into
linux-nvme:masterfrom
prabhakarpujeri:fix-entity-version-bounds

Conversation

@prabhakarpujeri

Copy link
Copy Markdown

Problem

Port of linux-nvme/libnvme#1136. libnvmf_get_entity_version() rests on the invariant num_bytes + bufsz == capacity maintained by read_file(), but each of the three ' ' separators is appended to buffer without checking or decrementing bufsz. After such an append the invariant is off by one, so with proc content long enough to fill the buffer the next separator lands one byte past the allocation, and the following min(len, bufsz) + memcpy() still trusts the inflated bufsz (one more byte of overrun then).

Fix

Guard the separator appends with if (bufsz) and decrement bufsz in step so the invariant holds, keeping the trailing memset(&buffer[num_bytes], 0, bufsz) in bounds.

Testing

  • Full meson suite green (99 pass; crypto-util skips here for missing OpenSSL, also on pristine master).
  • Arithmetic/invariant verified by review against the 1.x PR; the overrun requires unusually long /proc/sys/kernel/osrelease content so it is rare in practice, but the function's contract becomes provable.

Port of linux-nvme/libnvme#1136.  read_file() keeps the
num_bytes + bufsz == capacity invariant across its writes, but each
of the three ' ' separators is appended without checking or
decrementing bufsz.  After the append the invariant is off by one;
with proc files long enough to fill the buffer the next separator
write lands one byte beyond the caller's allocation, and the
following min(len, bufsz) memcpy still trusts the inflated bufsz.

Guard the separator appends with bufsz and decrement as needed so
the invariant holds, keeping the trailing memset in bounds too.

Signed-off-by: Prabhakar Pujeri <prabhakar.pujeri@dell.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant