mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-22 18:40:43 +03:00
Address theoretical uninitialized variable usage in zstream
Coverity has long complained about the checksum being uninitialized if
an END record is processed before its BEGIN record. This should not
happen, but there was no code to check for it. I had left this unfixed
since it was a low priority issue, but then
9f4ede63d2 added another instance of this.
I am making an effort to "hold the line" to keep new coverity defect
reports from going unaddressed, so I find myself forced to fix this much
earlier than I had originally planned to address it.
The solution is to maintain a counter and a flag. Then use VERIFY
statements to verify the following runtime constraints:
* Every record either has a corresponding BEGIN record, is a BEGIN
record or is the end of stream END record for replication streams.
* BEGIN records cannot be nested. i.e. There must be an END record
before another BEGIN record may be seen.
Failure to meet these constraints will cause the program to exit.
This is sufficient to ensure that the checksum is never accessed when
uninitialized.
Reported-by: Coverity (CID 1524578)
Reported-by: Coverity (CID 1524633)
Reported-by: Coverity (CID 1527295)
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Damian Szuberski <szuberskidamian@gmail.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #14176
This commit is contained in:
@@ -158,6 +158,8 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
}
|
||||
|
||||
fletcher_4_init();
|
||||
int begin = 0;
|
||||
boolean_t seen = B_FALSE;
|
||||
while (sfread(drr, sizeof (*drr), stdin) != 0) {
|
||||
struct drr_write *drrw;
|
||||
uint64_t payload_size = 0;
|
||||
@@ -174,6 +176,8 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
case DRR_BEGIN:
|
||||
{
|
||||
ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
|
||||
VERIFY0(begin++);
|
||||
seen = B_TRUE;
|
||||
|
||||
int sz = drr->drr_payloadlen;
|
||||
if (sz != 0) {
|
||||
@@ -191,6 +195,13 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
case DRR_END:
|
||||
{
|
||||
struct drr_end *drre = &drr->drr_u.drr_end;
|
||||
/*
|
||||
* We would prefer to just check --begin == 0, but
|
||||
* replication streams have an end of stream END
|
||||
* record, so we must avoid tripping it.
|
||||
*/
|
||||
VERIFY3B(seen, ==, B_TRUE);
|
||||
begin--;
|
||||
/*
|
||||
* Use the recalculated checksum, unless this is
|
||||
* the END record of a stream package, which has
|
||||
@@ -204,6 +215,7 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
case DRR_OBJECT:
|
||||
{
|
||||
struct drr_object *drro = &drr->drr_u.drr_object;
|
||||
VERIFY3S(begin, ==, 1);
|
||||
|
||||
if (drro->drr_bonuslen > 0) {
|
||||
payload_size = DRR_OBJECT_PAYLOAD_SIZE(drro);
|
||||
@@ -215,12 +227,14 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
case DRR_SPILL:
|
||||
{
|
||||
struct drr_spill *drrs = &drr->drr_u.drr_spill;
|
||||
VERIFY3S(begin, ==, 1);
|
||||
payload_size = DRR_SPILL_PAYLOAD_SIZE(drrs);
|
||||
(void) sfread(buf, payload_size, stdin);
|
||||
break;
|
||||
}
|
||||
|
||||
case DRR_WRITE_BYREF:
|
||||
VERIFY3S(begin, ==, 1);
|
||||
fprintf(stderr,
|
||||
"Deduplicated streams are not supported\n");
|
||||
exit(1);
|
||||
@@ -228,6 +242,7 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
|
||||
case DRR_WRITE:
|
||||
{
|
||||
VERIFY3S(begin, ==, 1);
|
||||
drrw = &thedrr.drr_u.drr_write;
|
||||
payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
|
||||
ENTRY *p;
|
||||
@@ -321,6 +336,7 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
|
||||
case DRR_WRITE_EMBEDDED:
|
||||
{
|
||||
VERIFY3S(begin, ==, 1);
|
||||
struct drr_write_embedded *drrwe =
|
||||
&drr->drr_u.drr_write_embedded;
|
||||
payload_size =
|
||||
@@ -332,6 +348,7 @@ zstream_do_decompress(int argc, char *argv[])
|
||||
case DRR_FREEOBJECTS:
|
||||
case DRR_FREE:
|
||||
case DRR_OBJECT_RANGE:
|
||||
VERIFY3S(begin, ==, 1);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user