index-pack: Detect SHA-1 hash collisions to avoid replacing objects
When indexing a pack file coming in from the network there may
be an object contained in the pack that we already have. Its
only safe to retain that object in our newly stored pack if it
exactly matches the content we already have on disk. Storing
a different content for the same object name runs the risk of
allowing a malicious attacker to replace an object in our store.
Data validation is performed the first time we have the object
in memory, which occurs when we first compute its SHA-1. This
is the earliest opportunity we have to validate that there is
no collision, and it reduces the number of times we need to do
a read for an object. However our memory usage when we process
a whole object is now increased as the collision test is done
by loading the entire object into memory. Future improvements
may permit a streaming compare of the objects.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>