use hmac in balanced preferred content

This deals with the possible security problem that someone could make an
unusually low UUID and generate keys that are all constructed to hash to
a number that, mod the number of repositories in the group, == 0.
So balanced preferred content would always put those keys in the
repository with the low UUID as long as the group contains the
number of repositories that the attacker anticipated.
Presumably the attacker than holds the data for ransom? Dunno.

Anyway, the partial solution is to use HMAC (sha256) with all the UUIDs
combined together as the "secret", and the key as the "message". Now any
change in the set of UUIDs in a group will invalidate the attacker's
constructed keys from hashing to anything in particular.

Given that there are plenty of other things someone can do if they can
write to the repository -- including modifying preferred content so only
their repository wants files, and numcopies so other repositories drom
them -- this seems like safeguard enough.

Note that, in balancedPicker, combineduuids is memoized.
This commit is contained in:
Joey Hess 2024-08-10 16:32:54 -04:00
parent bde58e6c71
commit bd5affa362
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
10 changed files with 68 additions and 47 deletions

View file

@ -37,7 +37,6 @@ import Git.Types (RefDate(..))
import Utility.Glob
import Utility.HumanTime
import Utility.DataUnits
import Utility.Hash
import qualified Database.Keys
import qualified Utility.RawFilePath as R
import Backend
@ -48,8 +47,6 @@ import qualified Data.Set as S
import qualified Data.Map as M
import qualified System.FilePath.ByteString as P
import System.PosixCompat.Files (accessTime, isSymbolicLink)
import qualified Data.ByteArray as BA
import Data.Bits (shiftL)
{- Some limits can look at the current status of files on
- disk, or in the annex. This allows controlling which happens. -}
@ -594,14 +591,13 @@ limitBalanced mu getgroupmap groupname = do
limitFullyBalanced :: Maybe UUID -> Annex GroupMap -> MkLimit Annex
limitFullyBalanced mu getgroupmap groupname = Right $ MatchFiles
{ matchAction = const $ checkKey $ \key -> do
groupmembers <- fromMaybe S.empty
. M.lookup (toGroup groupname)
. uuidsByGroup
<$> getgroupmap
gm <- getgroupmap
let groupmembers = fromMaybe S.empty $
M.lookup g (uuidsByGroup gm)
-- TODO free space checking
return $ case mu of
Just u -> u == pickBalanced key groupmembers
Nothing -> False
return $ case (mu, M.lookup g (balancedPickerByGroup gm)) of
(Just u, Just picker) -> u == picker groupmembers key
_ -> False
, matchNeedsFileName = False
, matchNeedsFileContent = False
, matchNeedsKey = True
@ -609,22 +605,7 @@ limitFullyBalanced mu getgroupmap groupname = Right $ MatchFiles
, matchDesc = "fullybalanced" =? groupname
}
where
pickBalanced :: Key -> S.Set UUID -> UUID
pickBalanced key s =
let m = fromIntegral (S.size s)
n = keyToInteger key
in S.elemAt (fromIntegral (n `mod` m)) s
{- Converts a Key into a stable Integer.
-
- The SHA2 hash of the key is used to constrain the size of the Integer
- and to get an even distribution.
-}
keyToInteger :: Key -> Integer
keyToInteger key =
foldl' (\i b -> (i `shiftL` 8) + fromIntegral b) 0 $
BA.unpack (sha2_256s (serializeKey' key))
g = toGroup groupname
{- Adds a limit to skip files not using a specified key-value backend. -}
addInBackend :: String -> Annex ()