ada: Fix intra-object buffer overread of identify strings

In the ATA/ATAPI spec these are space-padded fixed-length strings with
no NUL-terminator (and byte swapped). When performing the identify we
call ata_param_fixup to swap the bytes back to be in order, strip any
leading/trailing spaces and coalesce consecutive spaces, padding with
NULs. However, if the input has no padding spaces, the fixed-up strings
are still not NUL-terminated. This causes two issues. The first is that
strlcpy will truncate the string by replacing the final byte with a NUL.
The second is that strlcpy will keep reading src until it finds a NUL in
order to calculate the return value, which is defined as the length of
src (so that callers can then compare it with the dsize input to see if
the input string was truncated), thereby reading past the end of the
buffer and into whatever adjacent fields are in the structure. In
practice there's a NUL byte somewhere in the structure, but on CHERI
with subobject bounds enabled in the compiler this overread will be
detected and trap as a bounds violation.

Note this matches ata_xpt's aprobedone, which does a bcopy to a
malloc'ed buffer and manually NUL-terminates it for the CAM path's
device's serial_num.

Found by:	CHERI
Reviewed by:	imp, scottl
Differential Revision:	https://reviews.freebsd.org/D32567
This commit is contained in:
Jessica Clarke 2021-10-24 19:48:59 +01:00
parent 29863d1eff
commit f350bc1dd3

View File

@ -3424,6 +3424,7 @@ adasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd)
u_int64_t lbasize48;
u_int32_t lbasize;
u_int maxio, d_flags;
size_t tmpsize;
dp->secsize = ata_logical_sector_size(&cgd->ident_data);
if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
@ -3487,10 +3488,25 @@ adasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd)
softc->flags |= ADA_FLAG_UNMAPPEDIO;
}
softc->disk->d_flags = d_flags;
strlcpy(softc->disk->d_descr, cgd->ident_data.model,
MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
/*
* ata_param_fixup will strip trailing padding spaces and add a NUL,
* but if the field has no padding (as is common for serial numbers)
* there will still be no NUL terminator. We cannot use strlcpy, since
* it keeps reading src until it finds a NUL in order to compute the
* return value (and will truncate the final character due to having a
* single dsize rather than separate ssize and dsize), and strncpy does
* not add a NUL to the destination if it reaches the character limit.
*/
tmpsize = MIN(sizeof(softc->disk->d_descr) - 1,
sizeof(cgd->ident_data.model));
memcpy(softc->disk->d_descr, cgd->ident_data.model, tmpsize);
softc->disk->d_descr[tmpsize] = '\0';
tmpsize = MIN(sizeof(softc->disk->d_ident) - 1,
sizeof(cgd->ident_data.serial));
memcpy(softc->disk->d_ident, cgd->ident_data.serial, tmpsize);
softc->disk->d_ident[tmpsize] = '\0';
softc->disk->d_sectorsize = softc->params.secsize;
softc->disk->d_mediasize = (off_t)softc->params.sectors *