packages feed

nova-cache-0.14.0.1 / src / NovaCache / Xz.hs

-- | Bounded xz decompression for untrusted cache data.---- cache.nixos.org serves NARs xz-compressed, and substitution-- decompresses bytes that arrive from the network BEFORE any hash can-- vouch for them, so the decoder must not be steerable into unbounded-- allocation.  The consumer knows the narinfo's declared NarSize-- before decompressing: decompression takes that bound and fails past-- it ('xzMaxOutputBytes'), so a small compressed input cannot expand-- to arbitrary memory ahead of the hash check.  The decoder's own-- state is capped as well ('xzMaxDecoderMemoryBytes') - upstream-- passes no limit there; the divergence is deliberate hardening and-- the cap is a parameter.---- Concatenated streams decode as one output, matching upstream's-- @LZMA_CONCATENATED@ decoder in libutil's compression sink.---- This module lives in the public @nova-cache:xz@ sublibrary.  The-- @xz@ dependency bundles liblzma's C sources (via @xz-clib@), so no-- system library is needed on any platform - but it is still an extra C-- build that consumers without foreign-cache needs should not pay-- for, and a default-on compression dependency broke downstream-- installs once already (0.5.0.0).  Consumers that substitute from-- foreign caches depend on @nova-cache:xz@; everyone else never-- builds it.module NovaCache.Xz  ( XzLimits (..),    defaultXzDecoderMemoryBytes,    XzError (..),    truncatedInputMessage,    decompress,    withXzSource,  )whereimport qualified Codec.Compression.Lzma as Lzmaimport Control.Exception (Exception, SomeException, throwIO, try)-- decompressST runs in lazy ST (the upstream package's own lazy-- API drives it the same way); the driver's accumulator bangs and-- guard-before-recurse keep the bound checks strict regardless.import Control.Monad.ST.Lazy (runST)import Data.ByteString (ByteString)import qualified Data.ByteString as BSimport Data.IORef (IORef, newIORef, readIORef, writeIORef)import Data.Word (Word64)-- ----------------------------------------------------------------------------- Limits-- ----------------------------------------------------------------------------- | What a decode run may cost.  Both bounds are inclusive: output of-- exactly 'xzMaxOutputBytes' passes, one byte more fails - a narinfo's-- NarSize is exact, so the declared size itself must be reachable.data XzLimits = XzLimits  { -- | Maximum decompressed output, in bytes: the narinfo's declared    -- NarSize.    xzMaxOutputBytes :: !Word64,    -- | Maximum decoder-state memory liblzma may allocate.  Decoder    -- memory tracks the stream's declared dictionary size, an    -- attacker-chosen number read from the compressed header.    xzMaxDecoderMemoryBytes :: !Word64  }  deriving (Eq, Show)-- | A decoder-memory cap for callers without an opinion: 1 GiB.  The-- largest standard preset (@xz -9@) declares a 64 MiB dictionary and-- needs about 65 MiB to decode, so this refuses only hand-rolled-- dictionaries past 1 GiB.  Upstream passes no limit at all; a-- consumer matching that exactly can pass 'maxBound'.defaultXzDecoderMemoryBytes :: Word64defaultXzDecoderMemoryBytes = 1024 * 1024 * 1024-- ----------------------------------------------------------------------------- Errors-- ----------------------------------------------------------------------------- | Everything a bounded decode can refuse.  The pure 'decompress'-- returns these in 'Left'; the pull source behind 'withXzSource'-- throws them (see the 'Exception' instance).data XzError  = -- | The compressed stream is malformed, truncated, or carries    -- trailing garbage (liblzma's status, rendered).    XzStreamError !String  | -- | Decompressed output would exceed the bound (carried here).    XzOutputOverBound !Word64  | -- | The stream declares a dictionary needing more decoder memory    -- than the bound (carried here).    XzMemoryOverBound !Word64  deriving (Eq, Show)-- | Thrown by the pull source 'withXzSource' hands its continuation;-- a chunk convention has no error channel, and a throwing pull-- composes with consumers built around one (the store's streaming-- write cleans up via its exception path).instance Exception XzError-- ----------------------------------------------------------------------------- Pure bounded decode-- ----------------------------------------------------------------------------- | Decompress one xz blob under the given limits.  Output stops-- accumulating the moment it would pass the bound, so a-- high-expansion input costs at most the bound plus one decoder-- buffer, never what it claims to hold.decompress :: XzLimits -> ByteString -> Either XzError ByteStringdecompress limits input = runST $ do  start <- Lzma.decompressST (decompressParams limits)  drive (Just input) 0 [] start  where    bound = xzMaxOutputBytes limits    drive pending !produced acc step = case step of      -- The whole input feeds on the first request; the second request      -- gets the empty string, liblzma's end-of-input signal.      Lzma.DecompressInputRequired supply -> case pending of        Just bytes -> drive Nothing produced acc =<< supply bytes        Nothing -> drive Nothing produced acc =<< supply BS.empty      Lzma.DecompressOutputAvailable out next ->        case growWithinBound bound produced out of          Nothing -> pure (Left (XzOutputOverBound bound))          Just grown -> drive pending grown (out : acc) =<< next      Lzma.DecompressStreamEnd leftover        | BS.null leftover -> pure (Right (BS.concat (reverse acc)))        | otherwise -> pure (Left (XzStreamError trailingDataMessage))      Lzma.DecompressStreamError ret -> pure (Left (mapRet limits ret))-- ----------------------------------------------------------------------------- Streaming bounded decode (IO boundary)-- ----------------------------------------------------------------------------- | What the pull source is doing between calls.  The 'IORef' holding-- this is the module's one piece of mutable state - the same-- deliberate, documented boundary as the streaming NAR source.data XzSourceState  = XzStreaming !(Lzma.DecompressStream IO) !Word64  | XzDrained  | -- | A pull failed; the exception is held so every later pull    -- re-throws it.  Collapsing failure into 'XzDrained' would let a    -- consumer that catches the first throw pull once more and read    -- the empty chunk - the clean-end signal - presenting truncated    -- output as complete.  Held at 'SomeException', not 'XzError':    -- the compressed source throwing mid-pull leaves the transfer    -- just as unfinishable as a decoder error does.    XzFailed !SomeException-- | Decompress a chunk source into a chunk source, under the limits.-- The continuation's pull yields decompressed chunks; the empty chunk-- means end of output and repeats on further pulls.  The compressed-- source follows the same convention on its side.  Pairs with the-- incremental NAR parser and hashing, so a substituter can fetch,-- decompress, hash, and unpack in one bounded pass.---- Limit violations and malformed input are thrown as 'XzError' from-- the pull; once a pull has let any exception escape - decoder error-- or the compressed source failing - every later pull re-throws the-- same exception.---- Despite the bracket-shaped name there is no bracket to run: the-- binding ("Codec.Compression.Lzma") runs @lzma_end@ itself on the-- clean end path and otherwise frees the decoder through the-- stream's ForeignPtr finalizer - the lifecycle the ecosystem's-- zlib and bzlib bindings use for the same job.  A pull that-- throws, or a consumer that exits early, holds the decoder state-- (bounded by 'xzMaxDecoderMemoryBytes') until a GC runs the-- finalizer.  The binding keeps @endLzmaStream@ in a hidden module,-- so nothing here can end a live stream early.  Once xz exports it,-- this becomes a real bracket that ends the stream on every exit.withXzSource :: XzLimits -> IO ByteString -> (IO ByteString -> IO a) -> IO awithXzSource limits compressedSource consume = do  start <- Lzma.decompressIO (decompressParams limits)  stateRef <- newIORef (XzStreaming start 0)  consume (pullDecompressed limits compressedSource stateRef)-- | Produce the next decompressed chunk.pullDecompressed :: XzLimits -> IO ByteString -> IORef XzSourceState -> IO ByteStringpullDecompressed limits compressedSource stateRef = dispatch =<< readIORef stateRef  where    bound = xzMaxOutputBytes limits    dispatch XzDrained = pure BS.empty    dispatch (XzFailed failure) = throwIO failure    dispatch (XzStreaming step produced) = do      outcome <- tryPull (advance step produced)      case outcome of        Left failure -> do          writeIORef stateRef (XzFailed failure)          throwIO failure        Right chunk -> pure chunk    advance step produced = case step of      Lzma.DecompressInputRequired supply -> do        chunk <- compressedSource        next <- supply chunk        advance next produced      Lzma.DecompressOutputAvailable out nextAction ->        case growWithinBound bound produced out of          Nothing -> throwIO (XzOutputOverBound bound)          Just grown -> do            next <- nextAction            writeIORef stateRef (XzStreaming next grown)            -- liblzma may hand back an empty buffer at stream            -- boundaries; returning it would read as end of output.            if BS.null out              then advance next grown              else pure out      Lzma.DecompressStreamEnd leftover        | BS.null leftover -> do            writeIORef stateRef XzDrained            pure BS.empty        | otherwise -> throwIO (XzStreamError trailingDataMessage)      Lzma.DecompressStreamError ret -> throwIO (mapRet limits ret)-- | 'try' at 'SomeException', monomorphic so the catch-all needs no-- annotation at the call site.  Any exception escaping a pull - the-- compressed source failing included - leaves the transfer-- unfinishable, and the only sound later answer is the same failure-- again, so the caller latches whatever this catches.tryPull :: IO ByteString -> IO (Either SomeException ByteString)tryPull = try-- ----------------------------------------------------------------------------- Shared decoder machinery-- ----------------------------------------------------------------------------- | Decoder parameters under the limits: concatenated-stream decoding-- as upstream, memory capped, everything else at the library default.decompressParams :: XzLimits -> Lzma.DecompressParamsdecompressParams limits =  Lzma.defaultDecompressParams    { Lzma.decompressConcatenated = True,      Lzma.decompressMemLimit = xzMaxDecoderMemoryBytes limits    }-- | Total output after one more chunk, if it stays within the-- inclusive bound.  The pure driver and the streaming pull both decide-- the boundary here, so output of exactly the bound - a narinfo's-- NarSize is exact - passes in both paths by construction.growWithinBound :: Word64 -> Word64 -> ByteString -> Maybe Word64growWithinBound bound produced chunk  | grown > bound = Nothing  | otherwise = Just grown  where    grown = produced + fromIntegral (BS.length chunk)-- | Map liblzma's status to the error vocabulary.  The binding hands-- over the raw 'Lzma.LzmaRet'; shown as-is, a zero-byte input would-- fail with the message @LzmaRetOK@ - which reads as success - so the-- terminal statuses get real diagnoses instead.mapRet :: XzLimits -> Lzma.LzmaRet -> XzErrormapRet limits ret = case ret of  Lzma.LzmaRetMemlimitError -> XzMemoryOverBound (xzMaxDecoderMemoryBytes limits)  -- Input exhausted mid-stream: the binding reports LzmaRetOK when  -- the decoder was still content at end of input (a zero-byte input  -- lands here) and LzmaRetBufError when it could make no further  -- progress; both mean the input ran out before the stream did.  Lzma.LzmaRetOK -> XzStreamError truncatedInputMessage  Lzma.LzmaRetBufError -> XzStreamError truncatedInputMessage  Lzma.LzmaRetFormatError -> XzStreamError formatErrorMessage  Lzma.LzmaRetDataError -> XzStreamError dataErrorMessage  Lzma.LzmaRetOptionsError -> XzStreamError optionsErrorMessage  Lzma.LzmaRetUnsupportedCheck -> XzStreamError unsupportedCheckMessage  Lzma.LzmaRetMemError -> XzStreamError decoderAllocationMessage  -- LzmaRetStreamEnd, LzmaRetGetCheck, LzmaRetProgError never reach  -- the error path under this module's parameters; if the binding  -- surfaces one anyway, name it honestly rather than invent a cause.  other -> XzStreamError (unexpectedStatusPrefix ++ show other)-- | Diagnosis for input that ends before the xz stream does.  A-- zero-byte input and a truncated download both land here; exported so-- consumers can match the condition without parsing prose.truncatedInputMessage :: StringtruncatedInputMessage = "compressed input is empty or truncated before the end of the xz stream"trailingDataMessage :: StringtrailingDataMessage = "trailing data after the xz stream"formatErrorMessage :: StringformatErrorMessage = "input is not an xz stream (magic bytes not recognized)"dataErrorMessage :: StringdataErrorMessage = "corrupt xz stream"optionsErrorMessage :: StringoptionsErrorMessage = "xz stream declares unsupported filter options"unsupportedCheckMessage :: StringunsupportedCheckMessage = "xz stream declares an unsupported integrity check"decoderAllocationMessage :: StringdecoderAllocationMessage = "decoder memory allocation failed"unexpectedStatusPrefix :: StringunexpectedStatusPrefix = "unexpected liblzma status: "