Skip to content

Commit

Permalink
[attribute] When updating attribution, zero out the anchor/sentinel p…
Browse files Browse the repository at this point in the history
…rior to placing the new value in

This ensures that attribution codes that are shorter than the anchor/sentinel will not be mangled.
  • Loading branch information
bhearsum committed Sep 19, 2023
1 parent b7e8a81 commit 0950317
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dmg/attribution.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions test/attribution_shorter_than_sentinel.t
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0950317

Please sign in to comment.