nvdimm: ndtest: reject wrapped config-data offsets
PATCH 1/1 nvdimm: ndtest: reject wrapped config-data offsets
The ndtest provider validates get/set config-data requests by adding the ioctl-provided offset and length and comparing the result against LABEL_SIZE. That addition can wrap, so an offset such as U32_MAX with a one-byte length passes validation and then copies from or to label_area + U32_MAX.
Validate the command buffer shape, then validate the offset first and validate the length against the remaining label area so wrapped ranges are rejected before the copy. Report the rejection through the command status field so the DIMM ioctl ABI returns a nonzero command status instead of faulting.
--- tools/testing/nvdimm/test/ndtest.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
tools/testing/nvdimm/test/ndtest.c +18 -2
@@ -207,9 +207,15 @@ static int ndtest_config_get(struct ndtest_dimm *p, unsigned int buf_len, {unsigned int len; -if ((hdr->in_offset + hdr->in_length) >LABEL_SIZE ) +if (buf_len <sizeof (*hdr) || hdr->in_length > buf_len -sizeof (*hdr))return -EINVAL ; +if (hdr->in_offset >LABEL_SIZE || + hdr->in_length >LABEL_SIZE - hdr->in_offset) { + hdr->status = -EINVAL ; +return 0 ; + } + hdr->status =0 ; len =min (hdr->in_length,LABEL_SIZE - hdr->in_offset);memcpy (hdr->out_buf, p->label_area + hdr->in_offset, len);
@@ -221,10 +227,20 @@ static int ndtest_config_set(struct ndtest_dimm *p, unsigned int buf_len, struct nd_cmd_set_config_hdr *hdr) {unsigned int len; +u32 *status; -if ((hdr->in_offset + hdr->in_length) >LABEL_SIZE ) +if (buf_len <sizeof (*hdr) +sizeof (*status) || + hdr->in_length > buf_len -sizeof (*hdr) -sizeof (*status))return -EINVAL ; + status = (void *)hdr +sizeof (*hdr) + hdr->in_length; +if (hdr->in_offset >LABEL_SIZE || + hdr->in_length >LABEL_SIZE - hdr->in_offset) { + *status = -EINVAL ; +return 0 ; + } + + *status =0 ; len =min (hdr->in_length,LABEL_SIZE - hdr->in_offset);memcpy (p->label_area + hdr->in_offset, hdr->in_buf, len);
Signature · 1 line
2.43.0
Alison Schofield
maintainerQuoted 11 lines · Samuel Moelius
The ndtest provider validates get/set config-data requests by adding the ioctl-provided offset and length and comparing the result against
LABEL_SIZE. That addition can wrap, so an offset such asU32_MAXwith a one-byte length passes validation and then copies from or tolabel_area+U32_MAX.Validate the command buffer shape, then validate the offset first and validate the length against the remaining label area so wrapped ranges are rejected before the copy. Report the rejection through the command status field so the DIMM ioctl ABI returns a nonzero command status instead of faulting.
Hi Samuel,
Sorry Samuel I just found this one hiding in the backlog today. Some comments inline -
Also wondering about testing of this?
-- Alison
Quoted 13 lines
Assisted-by Codex:gpt-5.5-cyber-previewSigned-off-by Samuel Moelius <[email protected]>--- tools/testing/nvdimm/test/ndtest.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)tools/testing/nvdimm/test/ndtest.c +0
-0@@ -207,9 +207,15 @@ static int ndtest_config_get(struct ndtest_dimm *p, unsigned int buf_len, {
tools/testing/nvdimm/test/ndtest.c static int ndtest_config_get(struct ndtest_dimm *p, unsigned int buf_len, · line 216
Quoted 10 lines
tools/testing/nvdimm/test/ndtest.c +5
-1unsigned int len; -if ((hdr->in_offset + hdr->in_length) >LABEL_SIZE ) +if (buf_len <sizeof (*hdr) || hdr->in_length > buf_len -sizeof (*hdr))return -EINVAL ; +if (hdr->in_offset >LABEL_SIZE || + hdr->in_length >LABEL_SIZE - hdr->in_offset) { + hdr->status = -EINVAL ; +return 0 ;
This reports the failure thru the cmd status but waht about the in-kenrel callers? nvdimm_[get|set]_config_data() check cmd_rc not the status field. Would an out of range request appear to succeed on those paths? Can we preserve the error for those callers?
tools/testing/nvdimm/test/ndtest.c static int ndtest_config_get(struct ndtest_dimm *p, unsigned int buf_len, · line 220
Quoted 4 lines
tools/testing/nvdimm/test/ndtest.c +2
-0+ } + hdr->status =0 ; len =min (hdr->in_length,LABEL_SIZE - hdr->in_offset);
At this point I think min is redundant. hdr->in_length is guaranteed to fit in the remaining label area.
tools/testing/nvdimm/test/ndtest.c static int ndtest_config_set(struct ndtest_dimm *p, unsigned int buf_len, · line 236
Quoted 13 lines
tools/testing/nvdimm/test/ndtest.c +4
-1memcpy (hdr->out_buf, p->label_area + hdr->in_offset, len);@@ -221,10 +227,20 @@ static int ndtest_config_set(struct ndtest_dimm *p, unsigned int buf_len, struct nd_cmd_set_config_hdr *hdr) {unsigned int len; +u32 *status; -if ((hdr->in_offset + hdr->in_length) >LABEL_SIZE ) +if (buf_len <sizeof (*hdr) +sizeof (*status) || + hdr->in_length > buf_len -sizeof (*hdr) -sizeof (*status))return -EINVAL ; + status = (void *)hdr +sizeof (*hdr) + hdr->in_length;
Will that status filed be naturally aligned? Should this use put_aligned()?
tools/testing/nvdimm/test/ndtest.c static int ndtest_config_set(struct ndtest_dimm *p, unsigned int buf_len, · line 244
Quoted 8 lines
tools/testing/nvdimm/test/ndtest.c +7
-0+if (hdr->in_offset >LABEL_SIZE || + hdr->in_length >LABEL_SIZE - hdr->in_offset) { + *status = -EINVAL ; +return 0 ; + } + + *status =0 ; len =min (hdr->in_length,LABEL_SIZE - hdr->in_offset);
Like above, min is needless.
Quoted 6 lines
tools/testing/nvdimm/test/ndtest.c +0
-0memcpy (p->label_area + hdr->in_offset, hdr->in_buf, len);Signature · 1 line
2.43.0