Skip to content

Commit

Permalink
stored: fix memory corruption in label.cc
Browse files Browse the repository at this point in the history
pointer lifetime in WriteNewVolumeLabelToDev()
Previously, in WriteNewVolumeLabelToDev() a copy of the pointer
dcr->block was saved and then used as a parameter to EmptyBlock(). This
pointer's lifetime ends in dev->SetLabelBlockSize() when the previously
written device's block size was larger than the label block size.
This patch removes the copy and uses dcr->block directly, so the updated
pointer is used.
  • Loading branch information
arogge committed Sep 30, 2020
1 parent 9a0d1e7 commit 2cf9db2
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions core/src/stored/label.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,17 @@ int ReadDevVolumeLabel(DeviceControlRecord* dcr)
static bool WriteVolumeLabelToBlock(DeviceControlRecord* dcr)
{
Device* dev = dcr->dev;
DeviceBlock* block = dcr->block;
DeviceRecord rec;
JobControlRecord* jcr = dcr->jcr;

Dmsg0(130, "write Label in WriteVolumeLabelToBlock()\n");

rec.data = GetMemory(SER_LENGTH_Volume_Label);
EmptyBlock(block); /* Volume label always at beginning */
EmptyBlock(dcr->block); /* Volume label always at beginning */

CreateVolumeLabelRecord(dcr, dev, &rec);

block->BlockNumber = 0;
dcr->block->BlockNumber = 0;
if (!WriteRecordToBlock(dcr, &rec)) {
FreePoolMemory(rec.data);
Jmsg1(jcr, M_FATAL, 0,
Expand Down Expand Up @@ -356,7 +355,6 @@ bool WriteNewVolumeLabelToDev(DeviceControlRecord* dcr,
DeviceRecord* rec;
JobControlRecord* jcr = dcr->jcr;
Device* dev = dcr->dev;
DeviceBlock* block = dcr->block;

/*
* Set the default blocksize to read the label
Expand Down Expand Up @@ -403,7 +401,7 @@ bool WriteNewVolumeLabelToDev(DeviceControlRecord* dcr,
goto bail_out;
}

EmptyBlock(block);
EmptyBlock(dcr->block);
if (!dev->rewind(dcr)) {
Dmsg2(130, "Bad status on %s from rewind: ERR=%s\n", dev->print_name(),
dev->print_errmsg());
Expand Down

0 comments on commit 2cf9db2

Please sign in to comment.