Fix DoS via sample-rate forgery bypassing audio decode duration guard (#49948)
Signed-off-by: jperezde <jperezde@redhat.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
37fbf52084
commit
640a09086d
@@ -126,6 +126,7 @@ class SpeechToTextBaseServing(GenerateBaseServing):
|
||||
|
||||
self.max_audio_filesize_mb = envs.VLLM_MAX_AUDIO_CLIP_FILESIZE_MB
|
||||
self.max_audio_decode_duration_s: int = envs.VLLM_MAX_AUDIO_DECODE_DURATION_S
|
||||
self.max_audio_decode_bytes: int = envs.VLLM_MAX_AUDIO_DECODE_BYTES
|
||||
if self.model_cls.supports_segment_timestamp:
|
||||
self.tokenizer = cast(
|
||||
PreTrainedTokenizerBase,
|
||||
@@ -180,6 +181,7 @@ class SpeechToTextBaseServing(GenerateBaseServing):
|
||||
sr=self.asr_config.sample_rate,
|
||||
mono=True,
|
||||
max_duration_s=self.max_audio_decode_duration_s,
|
||||
max_decode_bytes=self.max_audio_decode_bytes,
|
||||
)
|
||||
except ValueError:
|
||||
raise
|
||||
|
||||
@@ -77,6 +77,7 @@ if TYPE_CHECKING:
|
||||
VLLM_MEDIA_LOADING_THREAD_COUNT: int = 8
|
||||
VLLM_MAX_AUDIO_CLIP_FILESIZE_MB: int = 25
|
||||
VLLM_MAX_AUDIO_DECODE_DURATION_S: int = 600
|
||||
VLLM_MAX_AUDIO_DECODE_BYTES: int = 268_435_456
|
||||
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"
|
||||
@@ -986,6 +987,14 @@ environment_variables: dict[str, Callable[[], Any]] = {
|
||||
"VLLM_MAX_AUDIO_DECODE_DURATION_S": lambda: int(
|
||||
os.getenv("VLLM_MAX_AUDIO_DECODE_DURATION_S", "600")
|
||||
),
|
||||
# Maximum float32 PCM bytes that audio decoding may allocate.
|
||||
# Guards against sample-rate forgery where an attacker inflates
|
||||
# the header sample rate to bypass the duration guard while the
|
||||
# actual frame count still causes a multi-GiB allocation.
|
||||
# Default is 256 MiB (sufficient for 600s mono 48 kHz float32).
|
||||
"VLLM_MAX_AUDIO_DECODE_BYTES": lambda: int(
|
||||
os.getenv("VLLM_MAX_AUDIO_DECODE_BYTES", "268435456")
|
||||
),
|
||||
# Maximum number of worker threads used for STT preprocessing. The default
|
||||
# intentionally caps at 2 because that performed best in profiling.
|
||||
# https://github.com/vllm-project/vllm/pull/44612#issuecomment-4662757781
|
||||
@@ -2206,6 +2215,7 @@ def compile_factors() -> dict[str, object]:
|
||||
"VLLM_MEDIA_LOADING_THREAD_COUNT",
|
||||
"VLLM_MAX_AUDIO_CLIP_FILESIZE_MB",
|
||||
"VLLM_MAX_AUDIO_DECODE_DURATION_S",
|
||||
"VLLM_MAX_AUDIO_DECODE_BYTES",
|
||||
"VLLM_MAX_AUDIO_PREPROCESS_WORKERS",
|
||||
"VLLM_MAX_IMAGE_PIXELS",
|
||||
"VLLM_VIDEO_LOADER_BACKEND",
|
||||
|
||||
@@ -13,6 +13,7 @@ import vllm.envs as envs
|
||||
from vllm.logger import init_logger
|
||||
from vllm.multimodal.audio import resample_audio_pyav
|
||||
from vllm.utils.import_utils import PlaceholderModule
|
||||
from vllm.utils.mem_constants import MiB_bytes
|
||||
from vllm.utils.serial_utils import tensor2base64
|
||||
from vllm.utils.sparse_utils import check_sparse_tensor_invariants_threadsafe
|
||||
|
||||
@@ -50,6 +51,7 @@ def load_audio_pyav(
|
||||
sr: float | None = 22050,
|
||||
mono: bool = True,
|
||||
max_duration_s: float | None = None,
|
||||
max_decode_bytes: int | None = None,
|
||||
) -> tuple[npt.NDArray, float]:
|
||||
"""Load an audio file using PyAV (FFmpeg), returning float32 mono waveform.
|
||||
|
||||
@@ -103,6 +105,7 @@ def load_audio_pyav(
|
||||
int(sr * max_duration_s) if max_duration_s is not None else None
|
||||
)
|
||||
total_samples = 0
|
||||
total_decode_bytes = 0
|
||||
|
||||
chunks: list[npt.NDArray] = []
|
||||
needs_resampling = not math.isclose(
|
||||
@@ -122,10 +125,12 @@ def load_audio_pyav(
|
||||
for out_frame in resampler.resample(frame):
|
||||
arr = out_frame.to_ndarray()
|
||||
total_samples += arr.shape[-1]
|
||||
total_decode_bytes += arr.nbytes
|
||||
chunks.append(arr)
|
||||
else:
|
||||
arr = frame.to_ndarray()
|
||||
total_samples += arr.shape[-1]
|
||||
total_decode_bytes += arr.nbytes
|
||||
chunks.append(arr)
|
||||
|
||||
if max_samples is not None and total_samples > max_samples:
|
||||
@@ -136,6 +141,18 @@ def load_audio_pyav(
|
||||
f"VLLM_MAX_AUDIO_DECODE_DURATION_S to "
|
||||
f"increase this limit."
|
||||
)
|
||||
if (
|
||||
max_decode_bytes is not None
|
||||
and total_decode_bytes > max_decode_bytes
|
||||
):
|
||||
raise ValueError(
|
||||
f"Audio decode exceeded "
|
||||
f"{max_decode_bytes / MiB_bytes:.0f} MiB memory "
|
||||
f"limit ({total_decode_bytes / MiB_bytes:.0f} MiB "
|
||||
f"decoded so far). Set "
|
||||
f"VLLM_MAX_AUDIO_DECODE_BYTES to increase this "
|
||||
f"limit."
|
||||
)
|
||||
except (ValueError, ImportError):
|
||||
raise
|
||||
except Exception as e:
|
||||
@@ -160,6 +177,7 @@ def load_audio_soundfile(
|
||||
sr: float | None = 22050,
|
||||
mono: bool = True,
|
||||
max_duration_s: float | None = None,
|
||||
max_decode_bytes: int | None = None,
|
||||
) -> tuple[np.ndarray, int]:
|
||||
"""Load audio via soundfile"""
|
||||
with soundfile.SoundFile(path) as f:
|
||||
@@ -174,6 +192,16 @@ def load_audio_soundfile(
|
||||
f"VLLM_MAX_AUDIO_DECODE_DURATION_S to "
|
||||
f"increase this limit."
|
||||
)
|
||||
if max_decode_bytes is not None:
|
||||
estimated_bytes = f.frames * f.channels * np.dtype(np.float32).itemsize
|
||||
if estimated_bytes > max_decode_bytes:
|
||||
raise ValueError(
|
||||
f"Audio would allocate {estimated_bytes / MiB_bytes:.0f} "
|
||||
f"MiB of PCM ({f.frames} frames x {f.channels} channels"
|
||||
f" x 4B), exceeding the "
|
||||
f"{max_decode_bytes / MiB_bytes:.0f} MiB limit. Set "
|
||||
f"VLLM_MAX_AUDIO_DECODE_BYTES to increase this limit."
|
||||
)
|
||||
y = f.read(dtype="float32", always_2d=False).T
|
||||
|
||||
if mono and y.ndim > 1:
|
||||
@@ -191,10 +219,15 @@ def load_audio(
|
||||
sr: float | None = 22050,
|
||||
mono: bool = True,
|
||||
max_duration_s: float | None = None,
|
||||
max_decode_bytes: int | None = None,
|
||||
):
|
||||
try:
|
||||
return load_audio_soundfile(
|
||||
path, sr=sr, mono=mono, max_duration_s=max_duration_s
|
||||
path,
|
||||
sr=sr,
|
||||
mono=mono,
|
||||
max_duration_s=max_duration_s,
|
||||
max_decode_bytes=max_decode_bytes,
|
||||
)
|
||||
except ImportError as exc:
|
||||
# soundfile (or resampy) is not installed — fall through to pyav.
|
||||
@@ -212,7 +245,13 @@ def load_audio(
|
||||
if isinstance(path, BytesIO):
|
||||
path.seek(0)
|
||||
try:
|
||||
return load_audio_pyav(path, sr=sr, mono=mono, max_duration_s=max_duration_s)
|
||||
return load_audio_pyav(
|
||||
path,
|
||||
sr=sr,
|
||||
mono=mono,
|
||||
max_duration_s=max_duration_s,
|
||||
max_decode_bytes=max_decode_bytes,
|
||||
)
|
||||
except ImportError:
|
||||
raise # Let PlaceholderModule's message ("install vllm[audio]") propagate.
|
||||
except Exception as pyav_exc:
|
||||
@@ -241,6 +280,7 @@ class AudioMediaIO(MediaIO[tuple[npt.NDArray, float]]):
|
||||
BytesIO(data),
|
||||
sr=None,
|
||||
max_duration_s=envs.VLLM_MAX_AUDIO_DECODE_DURATION_S,
|
||||
max_decode_bytes=envs.VLLM_MAX_AUDIO_DECODE_BYTES,
|
||||
)
|
||||
|
||||
def load_base64(
|
||||
@@ -255,6 +295,7 @@ class AudioMediaIO(MediaIO[tuple[npt.NDArray, float]]):
|
||||
filepath,
|
||||
sr=None,
|
||||
max_duration_s=envs.VLLM_MAX_AUDIO_DECODE_DURATION_S,
|
||||
max_decode_bytes=envs.VLLM_MAX_AUDIO_DECODE_BYTES,
|
||||
)
|
||||
|
||||
def encode_base64(
|
||||
|
||||
Reference in New Issue
Block a user