Page MenuHomeFreeBSD

D62291.1796323983.diff
No OneTemporary

Authored By
Unknown
Size
13 KB
Referenced Files
None
Subscribers
None

D62291.1796323983.diff

diff --git a/lib/libcam/tests/Makefile b/lib/libcam/tests/Makefile
--- a/lib/libcam/tests/Makefile
+++ b/lib/libcam/tests/Makefile
@@ -1,5 +1,6 @@
ATF_TESTS_C+= libcam_test
ATF_TESTS_C+= cam_test
+ATF_TESTS_C+= scsi_all_test
LIBADD+= cam
diff --git a/lib/libcam/tests/scsi_all_test.c b/lib/libcam/tests/scsi_all_test.c
new file mode 100644
--- /dev/null
+++ b/lib/libcam/tests/scsi_all_test.c
@@ -0,0 +1,81 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ */
+
+/* Tests functions in sys/cam/scsi/scsi_all.h */
+
+#include <sys/types.h>
+#include <string.h>
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+#include <cam/scsi/scsi_all.h>
+
+#include <atf-c.h>
+
+ATF_TC_WITHOUT_HEAD(find_mode_page_6_bounds);
+ATF_TC_BODY(find_mode_page_6_bounds, tc)
+{
+ uint8_t buf[24];
+ struct scsi_mode_header_6 *hdr = (struct scsi_mode_header_6 *)buf;
+
+ memset(buf, 0, sizeof(buf));
+
+ /* One block descriptor, the common case. */
+ hdr->blk_desc_len = 8;
+ ATF_CHECK_EQ(&buf[12], find_mode_page_6(hdr, sizeof(buf), 12));
+
+ /* No block descriptor. */
+ hdr->blk_desc_len = 0;
+ ATF_CHECK_EQ(&buf[4], find_mode_page_6(hdr, sizeof(buf), 12));
+
+ /* The page is one byte short of fitting. */
+ hdr->blk_desc_len = 9;
+ ATF_CHECK_EQ(NULL, find_mode_page_6(hdr, sizeof(buf), 12));
+
+ /* The page starts at the end of the buffer. */
+ hdr->blk_desc_len = 20;
+ ATF_CHECK_EQ(&buf[24], find_mode_page_6(hdr, sizeof(buf), 0));
+ ATF_CHECK_EQ(NULL, find_mode_page_6(hdr, sizeof(buf), 1));
+
+ /* The block descriptor alone runs off the end. */
+ hdr->blk_desc_len = 255;
+ ATF_CHECK_EQ(NULL, find_mode_page_6(hdr, sizeof(buf), 0));
+ ATF_CHECK_EQ(NULL, find_mode_page_6(hdr, sizeof(buf), 12));
+}
+
+ATF_TC_WITHOUT_HEAD(find_mode_page_10_bounds);
+ATF_TC_BODY(find_mode_page_10_bounds, tc)
+{
+ uint8_t buf[28];
+ struct scsi_mode_header_10 *hdr = (struct scsi_mode_header_10 *)buf;
+
+ memset(buf, 0, sizeof(buf));
+
+ /* One block descriptor, the common case. */
+ scsi_ulto2b(8, hdr->blk_desc_len);
+ ATF_CHECK_EQ(&buf[16], find_mode_page_10(hdr, sizeof(buf), 12));
+
+ /* No block descriptor. */
+ scsi_ulto2b(0, hdr->blk_desc_len);
+ ATF_CHECK_EQ(&buf[8], find_mode_page_10(hdr, sizeof(buf), 12));
+
+ /* The page is one byte short of fitting. */
+ scsi_ulto2b(9, hdr->blk_desc_len);
+ ATF_CHECK_EQ(NULL, find_mode_page_10(hdr, sizeof(buf), 12));
+
+ /* The block descriptor alone runs off the end. */
+ scsi_ulto2b(65535, hdr->blk_desc_len);
+ ATF_CHECK_EQ(NULL, find_mode_page_10(hdr, sizeof(buf), 0));
+ ATF_CHECK_EQ(NULL, find_mode_page_10(hdr, sizeof(buf), 12));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, find_mode_page_6_bounds);
+ ATF_TP_ADD_TC(tp, find_mode_page_10_bounds);
+
+ return (atf_no_error());
+}
diff --git a/sbin/camcontrol/modeedit.c b/sbin/camcontrol/modeedit.c
--- a/sbin/camcontrol/modeedit.c
+++ b/sbin/camcontrol/modeedit.c
@@ -608,12 +608,15 @@
if (cdb_len == 6) {
struct scsi_mode_header_6 *mh =
(struct scsi_mode_header_6 *)data;
- mph = find_mode_page_6(mh);
+ mph = find_mode_page_6(mh, sizeof(data), sizeof(*mphsp));
} else {
struct scsi_mode_header_10 *mh =
(struct scsi_mode_header_10 *)data;
- mph = find_mode_page_10(mh);
+ mph = find_mode_page_10(mh, sizeof(data), sizeof(*mphsp));
}
+ if (mph == NULL)
+ errx(EX_IOERR, "mode page does not fit past the block "
+ "descriptors");
if ((mph->page_code & SMPH_SPF) == 0) {
mode_pars = (uint8_t *)(mph + 1);
len = mph->page_length;
@@ -702,7 +705,7 @@
uint8_t *mode_pars; /* Pointer to modepage params. */
struct scsi_mode_page_header *mph;
struct scsi_mode_page_header_sp *mphsp;
- size_t len, hlen, mphlen;
+ size_t dlen, len, hlen, mphlen;
/* Make sure that something changed before continuing. */
if (! editlist_changed)
@@ -729,32 +732,43 @@
struct scsi_mode_header_6 *mh =
(struct scsi_mode_header_6 *)data;
hlen = sizeof(*mh);
+ mph = find_mode_page_6(mh, sizeof(data), sizeof(*mphsp));
+ if (mph == NULL)
+ errx(EX_IOERR, "mode page does not fit past the block "
+ "descriptors");
/* Eliminate block descriptors. */
if (mh->blk_desc_len > 0) {
- bcopy(find_mode_page_6(mh), mh + 1,
- mh->data_length + 1 - hlen -
- mh->blk_desc_len);
+ dlen = MIN((size_t)mh->data_length + 1, sizeof(data));
+ if (dlen > hlen + mh->blk_desc_len)
+ bcopy(mph, mh + 1,
+ dlen - hlen - mh->blk_desc_len);
mh->blk_desc_len = 0;
+ mph = (struct scsi_mode_page_header *)(mh + 1);
}
mh->data_length = 0; /* Reserved for MODE SELECT command. */
if (device->pd_type != T_SEQUENTIAL)
mh->dev_spec = 0; /* See comment above */
- mph = find_mode_page_6(mh);
} else {
struct scsi_mode_header_10 *mh =
(struct scsi_mode_header_10 *)data;
hlen = sizeof(*mh);
+ mph = find_mode_page_10(mh, sizeof(data), sizeof(*mphsp));
+ if (mph == NULL)
+ errx(EX_IOERR, "mode page does not fit past the block "
+ "descriptors");
/* Eliminate block descriptors. */
if (scsi_2btoul(mh->blk_desc_len) > 0) {
- bcopy(find_mode_page_10(mh), mh + 1,
- scsi_2btoul(mh->data_length) + 1 - hlen -
- scsi_2btoul(mh->blk_desc_len));
+ dlen = MIN((size_t)scsi_2btoul(mh->data_length) + 1,
+ sizeof(data));
+ if (dlen > hlen + scsi_2btoul(mh->blk_desc_len))
+ bcopy(mph, mh + 1, dlen - hlen -
+ scsi_2btoul(mh->blk_desc_len));
scsi_ulto2b(0, mh->blk_desc_len);
+ mph = (struct scsi_mode_page_header *)(mh + 1);
}
scsi_ulto2b(0, mh->data_length); /* Reserved for MODE SELECT. */
if (device->pd_type != T_SEQUENTIAL)
mh->dev_spec = 0; /* See comment above */
- mph = find_mode_page_10(mh);
}
if ((mph->page_code & SMPH_SPF) == 0) {
mphlen = sizeof(*mph);
@@ -1013,12 +1027,15 @@
if (cdb_len == 6) {
struct scsi_mode_header_6 *mh =
(struct scsi_mode_header_6 *)data;
- mph = find_mode_page_6(mh);
+ mph = find_mode_page_6(mh, sizeof(data), sizeof(*mphsp));
} else {
struct scsi_mode_header_10 *mh =
(struct scsi_mode_header_10 *)data;
- mph = find_mode_page_10(mh);
+ mph = find_mode_page_10(mh, sizeof(data), sizeof(*mphsp));
}
+ if (mph == NULL)
+ errx(EX_IOERR, "mode page does not fit past the block "
+ "descriptors");
if ((mph->page_code & SMPH_SPF) == 0) {
mode_pars = (uint8_t *)(mph + 1);
len = mph->page_length;
diff --git a/sys/cam/scsi/scsi_all.h b/sys/cam/scsi/scsi_all.h
--- a/sys/cam/scsi/scsi_all.h
+++ b/sys/cam/scsi/scsi_all.h
@@ -4527,29 +4527,35 @@
}
/*
- * Given the pointer to a returned mode sense buffer, return a pointer to
- * the start of the first mode page.
+ * Given the pointer to a returned mode sense buffer holding buf_len readable
+ * bytes, return a pointer to the start of the first mode page, or NULL if the
+ * block descriptor length the target reported does not leave page_len bytes
+ * of page in the buffer.
*/
static __inline void *
-find_mode_page_6(struct scsi_mode_header_6 *mode_header)
+find_mode_page_6(struct scsi_mode_header_6 *mode_header, u_int buf_len,
+ u_int page_len)
{
- void *page_start;
+ u_int off;
- page_start = (void *)((uint8_t *)&mode_header[1] +
- mode_header->blk_desc_len);
+ off = sizeof(*mode_header) + mode_header->blk_desc_len;
+ if (off > buf_len || buf_len - off < page_len)
+ return (NULL);
- return(page_start);
+ return ((uint8_t *)mode_header + off);
}
static __inline void *
-find_mode_page_10(struct scsi_mode_header_10 *mode_header)
+find_mode_page_10(struct scsi_mode_header_10 *mode_header, u_int buf_len,
+ u_int page_len)
{
- void *page_start;
+ u_int off;
- page_start = (void *)((uint8_t *)&mode_header[1] +
- scsi_2btoul(mode_header->blk_desc_len));
+ off = sizeof(*mode_header) + scsi_2btoul(mode_header->blk_desc_len);
+ if (off > buf_len || buf_len - off < page_len)
+ return (NULL);
- return(page_start);
+ return ((uint8_t *)mode_header + off);
}
void scsi_format_sense_devd(struct ccb_scsiio *csio, struct sbuf *sb);
diff --git a/sys/cam/scsi/scsi_cd.c b/sys/cam/scsi/scsi_cd.c
--- a/sys/cam/scsi/scsi_cd.c
+++ b/sys/cam/scsi/scsi_cd.c
@@ -1693,11 +1693,13 @@
union cd_pages *page;
if (mode_params->cdb_size == 10)
- page = (union cd_pages *)find_mode_page_10(
- (struct scsi_mode_header_10 *)mode_params->mode_buf);
+ page = find_mode_page_10(
+ (struct scsi_mode_header_10 *)mode_params->mode_buf,
+ mode_params->alloc_len, sizeof(*page));
else
- page = (union cd_pages *)find_mode_page_6(
- (struct scsi_mode_header_6 *)mode_params->mode_buf);
+ page = find_mode_page_6(
+ (struct scsi_mode_header_6 *)mode_params->mode_buf,
+ mode_params->alloc_len, sizeof(*page));
return (page);
}
@@ -2806,7 +2808,13 @@
header6 = (struct scsi_mode_header_6 *)params->mode_buf;
header10 = (struct scsi_mode_header_10 *)params->mode_buf;
- page_header = find_mode_page_6(header6);
+ page_header = find_mode_page_6(header6, params->alloc_len,
+ sizeof(*page_header));
+ if (page_header == NULL) {
+ xpt_print(periph->path, "mode page does not fit past "
+ "the block descriptors\n");
+ return (0);
+ }
page_num = page_header->page_code;
blk_desc_len = header6->blk_desc_len;
@@ -3115,6 +3123,10 @@
"%d < returned length %d\n", page, data->alloc_len,
data_len);
error = ENOSPC;
+ } else if (cdgetpage(data) == NULL) {
+ xpt_print(periph->path, "modepage %d does not fit "
+ "past the block descriptors\n", page);
+ error = ENOSPC;
}
}
return (error);
diff --git a/sys/cam/scsi/scsi_ch.c b/sys/cam/scsi/scsi_ch.c
--- a/sys/cam/scsi/scsi_ch.c
+++ b/sys/cam/scsi/scsi_ch.c
@@ -608,11 +608,16 @@
mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
- ea = (struct page_element_address_assignment *)
- find_mode_page_6(mode_header);
-
- if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
-
+ ea = find_mode_page_6(mode_header,
+ csio->dxfer_len - csio->resid, sizeof(*ea));
+
+ if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
+ ea == NULL) {
+ xpt_print(periph->path, "element address assignment "
+ "page does not fit in the returned data\n");
+ cam_periph_invalidate(periph);
+ } else if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
+ CAM_REQ_CMP) {
softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
@@ -1610,8 +1615,15 @@
}
}
- ea = (struct page_element_address_assignment *)
- find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
+ ea = find_mode_page_6(mode_buffer,
+ ccb->csio.dxfer_len - ccb->csio.resid, sizeof(*ea));
+ if (ea == NULL) {
+ xpt_print(periph->path, "chgetparams: element address "
+ "assignment page does not fit in the returned data\n");
+ xpt_release_ccb(ccb);
+ free(mode_buffer, M_SCSICH);
+ return (EIO);
+ }
softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
@@ -1673,10 +1685,15 @@
}
}
+ cap = find_mode_page_6(mode_buffer,
+ ccb->csio.dxfer_len - ccb->csio.resid, sizeof(*cap));
xpt_release_ccb(ccb);
-
- cap = (struct page_device_capabilities *)
- find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
+ if (cap == NULL) {
+ xpt_print(periph->path, "chgetparams: device capabilities "
+ "page does not fit in the returned data\n");
+ free(mode_buffer, M_SCSICH);
+ return (EIO);
+ }
bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
diff --git a/sys/cam/scsi/scsi_da.c b/sys/cam/scsi/scsi_da.c
--- a/sys/cam/scsi/scsi_da.c
+++ b/sys/cam/scsi/scsi_da.c
@@ -5455,8 +5455,6 @@
priority = done_ccb->ccb_h.pinfo.priority;
csio = &done_ccb->csio;
sense_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
- cache_page = (struct scsi_caching_page *)(csio->data_ptr +
- sizeof(struct scsi_mode_header_6) + sense_hdr->blk_desc_len);
if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
/*
@@ -5469,8 +5467,9 @@
* request a bigger buffer). The devices that have problems are
* typically cheap USB thumb drives.
*/
- if (sense_hdr->data_length + 1 <
- sense_hdr->blk_desc_len + sizeof(*cache_page)) {
+ cache_page = find_mode_page_6(sense_hdr,
+ csio->dxfer_len - csio->resid, sizeof(*cache_page));
+ if (cache_page == NULL) {
if (bootverbose)
xpt_print(done_ccb->ccb_h.path,
"CACHE PAGE TOO SHORT data len %d desc len %d\n",
diff --git a/sys/cam/scsi/scsi_xpt.c b/sys/cam/scsi/scsi_xpt.c
--- a/sys/cam/scsi/scsi_xpt.c
+++ b/sys/cam/scsi/scsi_xpt.c
@@ -1458,12 +1458,11 @@
mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
struct scsi_control_page *page;
- uint8_t *offset;
- offset = ((uint8_t *)&mode_hdr[1])
- + mode_hdr->blk_desc_len;
- page = (struct scsi_control_page *)offset;
- path->device->queue_flags = page->queue_flags;
+ page = find_mode_page_6(mode_hdr,
+ csio->dxfer_len - csio->resid, sizeof(*page));
+ if (page != NULL)
+ path->device->queue_flags = page->queue_flags;
} else if (cam_periph_error(done_ccb, 0,
SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
goto outr;

File Metadata

Mime Type
text/plain
Expires
Thu, Dec 3, 6:53 PM (18 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42188544
Default Alt Text
D62291.1796323983.diff (13 KB)

Event Timeline