fix(security): prevent image decompression bomb OOM denial of service (#47010)
Signed-off-by: jperezde <jperezde@redhat.com>
This commit is contained in:
@@ -79,6 +79,7 @@ if TYPE_CHECKING:
|
||||
VLLM_MAX_AUDIO_CLIP_FILESIZE_MB: int = 25
|
||||
VLLM_MAX_AUDIO_DECODE_DURATION_S: int = 600
|
||||
VLLM_MAX_AUDIO_PREPROCESS_WORKERS: int = max(1, min(os.cpu_count() or 1, 2))
|
||||
VLLM_MAX_IMAGE_PIXELS: int = 178_956_970
|
||||
VLLM_VIDEO_LOADER_BACKEND: str = "opencv"
|
||||
VLLM_MEDIA_CONNECTOR: str = "http"
|
||||
VLLM_MM_HASHER_ALGORITHM: str = "blake3"
|
||||
@@ -954,6 +955,13 @@ environment_variables: dict[str, Callable[[], Any]] = {
|
||||
str(max(1, min(os.cpu_count() or 1, 2))),
|
||||
)
|
||||
),
|
||||
# Maximum decoded image size in pixels. Small compressed images can
|
||||
# expand into gigabytes of raster memory. This limit is enforced before
|
||||
# decoding so the memory is never allocated. Default matches PIL's
|
||||
# built-in 2x decompression-bomb threshold (~179M pixels, ~680 MB RGB).
|
||||
"VLLM_MAX_IMAGE_PIXELS": lambda: int(
|
||||
os.getenv("VLLM_MAX_IMAGE_PIXELS", "178956970")
|
||||
),
|
||||
# Backend for Video IO — selects the frame-sampling algorithm.
|
||||
# - "opencv": uniform sampling.
|
||||
# - "opencv_dynamic": duration-aware dynamic sampling.
|
||||
@@ -2083,6 +2091,7 @@ def compile_factors() -> dict[str, object]:
|
||||
"VLLM_MAX_AUDIO_CLIP_FILESIZE_MB",
|
||||
"VLLM_MAX_AUDIO_DECODE_DURATION_S",
|
||||
"VLLM_MAX_AUDIO_PREPROCESS_WORKERS",
|
||||
"VLLM_MAX_IMAGE_PIXELS",
|
||||
"VLLM_VIDEO_LOADER_BACKEND",
|
||||
"VLLM_MEDIA_CONNECTOR",
|
||||
"VLLM_OBJECT_STORAGE_SHM_BUFFER_NAME",
|
||||
|
||||
@@ -9,6 +9,7 @@ import pybase64
|
||||
import torch
|
||||
from PIL import Image
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm.utils.serial_utils import tensor2base64
|
||||
|
||||
from ..image import convert_image_mode, normalize_image, rgba_to_rgb
|
||||
@@ -72,6 +73,14 @@ class ImageMediaIO(MediaIO[Image.Image]):
|
||||
def load_bytes(self, data: bytes) -> MediaWithBytes[Image.Image]:
|
||||
try:
|
||||
image = Image.open(BytesIO(data))
|
||||
w, h = image.size
|
||||
max_pixels = envs.VLLM_MAX_IMAGE_PIXELS
|
||||
if max_pixels > 0 and w * h > max_pixels:
|
||||
raise ValueError(
|
||||
f"Image dimensions {w}x{h} ({w * h} pixels) exceed "
|
||||
f"the maximum of {max_pixels} pixels. Set "
|
||||
f"VLLM_MAX_IMAGE_PIXELS to increase this limit."
|
||||
)
|
||||
image = normalize_image(image)
|
||||
image.load()
|
||||
image = self._convert_image_mode(image)
|
||||
|
||||
@@ -13,6 +13,7 @@ import numpy as np
|
||||
import numpy.typing as npt
|
||||
import torch
|
||||
|
||||
from vllm import envs
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.import_utils import PlaceholderModule
|
||||
from vllm.utils.mem_constants import MiB_bytes
|
||||
@@ -88,6 +89,18 @@ def get_video_loader_backend_for_processor(
|
||||
return VIDEO_LOADER_REGISTRY.get_backend_for_video_processor(video_processor)
|
||||
|
||||
|
||||
def _check_frame_pixel_limit(width: int, height: int) -> None:
|
||||
"""Reject video frames exceeding VLLM_MAX_IMAGE_PIXELS before decoding."""
|
||||
max_pixels = envs.VLLM_MAX_IMAGE_PIXELS
|
||||
if max_pixels > 0 and width * height > max_pixels:
|
||||
raise ValueError(
|
||||
f"Video frame dimensions {width}x{height} "
|
||||
f"({width * height} pixels) exceed the maximum of "
|
||||
f"{max_pixels} pixels. Set VLLM_MAX_IMAGE_PIXELS to "
|
||||
f"increase this limit."
|
||||
)
|
||||
|
||||
|
||||
def resize_video(frames: npt.NDArray, size: tuple[int, int]) -> npt.NDArray:
|
||||
num_frames, _, _, channels = frames.shape
|
||||
new_height, new_width = size
|
||||
@@ -733,6 +746,7 @@ class PyNvVideoCodecVideoBackendMixin:
|
||||
temp_file.write(data)
|
||||
|
||||
gpu_source = cls._read_source_metadata(temp_path, nvc)
|
||||
_check_frame_pixel_limit(gpu_source.width, gpu_source.height)
|
||||
source = cls._prepare_source(gpu_source.source)
|
||||
frame_idx = cls.compute_frames_index_to_sample(
|
||||
source=source, target=target, **kwargs
|
||||
@@ -835,6 +849,10 @@ class VideoBackend(
|
||||
|
||||
if backend == "opencv":
|
||||
cap = cls.open_video_capture(data)
|
||||
_check_frame_pixel_limit(
|
||||
int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
||||
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
|
||||
)
|
||||
source = cls._prepare_source(cls.get_video_metadata(cap))
|
||||
frame_idx = cls.compute_frames_index_to_sample(
|
||||
source=source, target=target, **kwargs
|
||||
@@ -850,6 +868,8 @@ class VideoBackend(
|
||||
"frame_recovery is only available for `opencv` backend"
|
||||
)
|
||||
with av.open(BytesIO(data)) as container:
|
||||
stream = container.streams.video[0]
|
||||
_check_frame_pixel_limit(stream.width, stream.height)
|
||||
source = cls._prepare_source(cls.get_metadata(container))
|
||||
frame_idx = cls.compute_frames_index_to_sample(
|
||||
source=source, target=target, **kwargs
|
||||
@@ -1609,6 +1629,10 @@ class Molmo2VideoBackend(VideoLoader, OpenCVVideoBackendMixin):
|
||||
**kwargs,
|
||||
) -> tuple[npt.NDArray, dict[str, Any]]:
|
||||
cap = cls.open_video_capture(data)
|
||||
_check_frame_pixel_limit(
|
||||
int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
||||
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
|
||||
)
|
||||
|
||||
source = OpenCVVideoBackendMixin.get_video_metadata(cap)
|
||||
target = VideoTargetMetadata(
|
||||
@@ -1758,6 +1782,10 @@ class OpenCVDynamicOpenPanguVideoBackend(VideoLoader, OpenCVVideoBackendMixin):
|
||||
Tuple of (frames_array, metadata_dict)
|
||||
"""
|
||||
cap = cls.open_video_capture(data)
|
||||
_check_frame_pixel_limit(
|
||||
int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
||||
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
|
||||
)
|
||||
|
||||
source = OpenCVVideoBackendMixin.get_video_metadata(cap)
|
||||
|
||||
|
||||
@@ -44,12 +44,6 @@ AUDIO_CONTEXT = "<so_embedding>"
|
||||
# MAX_FRAMES = 16
|
||||
DEFAULT_NUM_TILES = 12
|
||||
|
||||
# Configure PIL to handle large images without warnings
|
||||
# This prevents DecompressionBombWarning for legitimate large images
|
||||
Image.MAX_IMAGE_PIXELS = None # Disable the limit entirely
|
||||
# Alternative: Set a specific higher limit
|
||||
# Image.MAX_IMAGE_PIXELS = 300000000 # ~300M pixels
|
||||
|
||||
|
||||
def calculate_timestamps(
|
||||
indices: list[int] | torch.Tensor,
|
||||
|
||||
@@ -10,12 +10,6 @@ from vllm.tokenizers.hf import HfTokenizer
|
||||
|
||||
from .internvl import InternVLImageProcessor, InternVLProcessor
|
||||
|
||||
# Configure PIL to handle large images without warnings
|
||||
# This prevents DecompressionBombWarning for legitimate large images
|
||||
Image.MAX_IMAGE_PIXELS = None # Disable the limit entirely
|
||||
# Alternative: Set a specific higher limit
|
||||
# Image.MAX_IMAGE_PIXELS = 300000000 # ~300M pixels
|
||||
|
||||
|
||||
def build_transform(input_size: int):
|
||||
return T.Compose(
|
||||
|
||||
Reference in New Issue
Block a user