From 0950317f39576a6506e62f1772435a86554a11b2 Mon Sep 17 00:00:00 2001 From: Ben Hearsum Date: Mon, 21 Aug 2023 21:30:32 -0400 Subject: [PATCH] [attribute] When updating attribution, zero out the anchor/sentinel prior to placing the new value in This ensures that attribution codes that are shorter than the anchor/sentinel will not be mangled. --- dmg/attribution.c | 6 +++++ test/attribution_shorter_than_sentinel.t | 33 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test/attribution_shorter_than_sentinel.t diff --git a/dmg/attribution.c b/dmg/attribution.c index ad29910..ca8d21e 100644 --- a/dmg/attribution.c +++ b/dmg/attribution.c @@ -274,6 +274,12 @@ int updateAttribution(AbstractFile* abstractIn, AbstractFile* abstractOut, const printf("anchorOffset: 0x%llx\n", anchorOffset); ASSERT(rawAnchor + dataLen <= rawBuffer + attributionResource->rawLength, "data too long!"); + // Zero out the anchor area, in case the data is shorter than the anchor + // Note that we're taking the strlen of `rawAnchor` and not the `anchor` + // passed in. This ensures that the entire anchor is zero'ed, even if only + // a prefix of it was provided. + memset((void *)rawAnchor, 0, strlen(rawAnchor)); + // Copy the new data into the same spot the anchor was in memcpy((void *)rawAnchor, data, dataLen); // Write the new block. diff --git a/test/attribution_shorter_than_sentinel.t b/test/attribution_shorter_than_sentinel.t new file mode 100644 index 0000000..d25cf25 --- /dev/null +++ b/test/attribution_shorter_than_sentinel.t @@ -0,0 +1,33 @@ +Make sure we have a fresh build: + + $ export BUILDDIR=$TESTDIR/../build + $ cd $BUILDDIR + $ make 2> /dev/null >/dev/null + $ cd $CRAMTMP + +Prepare content and the attribution code: + $ mkdir stagedir + $ echo "content-a" >> stagedir/a + $ sentinel="__MOZILLA__attribution-code" + $ attribution_code="short" + +Create the filesystem with the sentinel value in the attribute: + + $ mkdir output + $ cp $TESTDIR/empty.hfs output/short.hfs + $ $BUILDDIR/hfs/hfsplus output/short.hfs add stagedir/a a + $ $BUILDDIR/hfs/hfsplus output/short.hfs setattr a attr-key "$sentinel" + +Build a DMG: + + $ $BUILDDIR/dmg/dmg build output/short.hfs output/short.dmg "$sentinel" >/dev/null + +Now attribute: + + $ $BUILDDIR/dmg/dmg attribute output/short.dmg output/short-attributed.dmg "__MOZILLA__" "$attribution_code" >/dev/null + +Extract the HFS from the attributed DMG, and check to see if the code is correct: + + $ $BUILDDIR/dmg/dmg extract output/short-attributed.dmg output/short-attributed.hfs >/dev/null + $ $BUILDDIR/hfs/hfsplus output/short-attributed.hfs getattr a attr-key | tr -d '\0' + short (no-eol)