From nobody Sun Jun 14 11:27:13 2026 Received: from mail-vs1-f52.google.com (mail-vs1-f52.google.com [209.85.217.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFDEE3EF67E for ; Thu, 2 Apr 2026 16:23:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775147005; cv=none; b=NYLqeR5MZA/4dmK4gwg8KDlvuQo676+Wx5RHR6eq9Q+RLArD2kYDUkdLrYxtOanHkb6RKoiH7dU1zEu9C35v8LHuRvW1wMs++yatzfS4yTEiiphI6LfnuO+z/ElOlgQMRdEvRLpblkhWaEPNcUjjdQiommozKxdljbuZkoUSico= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775147005; c=relaxed/simple; bh=3A9KGYgeMUK2GEJvMz06J7Dl9SaVyCfHnRlIITPKZ3E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jcv9cgdDm+wAhrSIVdRHFL9p0R+v5eOujetvCbWVZ4N8gWB1lZfZPslBWpWsuRIfYjA8c0EG10jY9ZTM7j5akk5nKj2NDbAggFetJ2OAuodNzS/FvW7OqmOMvOFl3hb4sDXZ5X0NoGEmoypjUcfAFj7Qog7dyCfjjZh0i6us1NQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=I3ENU/fA; arc=none smtp.client-ip=209.85.217.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="I3ENU/fA" Received: by mail-vs1-f52.google.com with SMTP id ada2fe7eead31-605048a9c94so278131137.0 for ; Thu, 02 Apr 2026 09:23:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775147000; x=1775751800; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oQdnjdTuHD1u/JHVHdnYj1E1ndYIkWP99vNZk5JKW3I=; b=I3ENU/fAUva9w93Mmo9L4LlCOgyK5ppccR+W7kakCDRCFbM8WuAxHRvQbL3RwmgPjX LNn+FRE3A9WMVrEh3//tgjzYl5qT4z1nMaDIgl8obYuoxBoIAaqLlwqxIyoLrEAvbKLz v3a5O/3OvNTcHivZIjeDbDG6ZspYyZHxUlpokbXqHymptpstd5l7EGmdxY0WIvxzpPZ5 Bt85BbFUt3FHa8G4zMR+zWbBVJpWdjAEJPAin5J5hekF5hlOAW8JBb+pgrD90yzFZ746 4tcts7oOYhfSgb2Z4jYvCNPho30WormOff1h6GWrEn94RP/FlqBG2ar2qdG277LzUDa4 M6bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775147000; x=1775751800; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=oQdnjdTuHD1u/JHVHdnYj1E1ndYIkWP99vNZk5JKW3I=; b=am4vIMBV4L3JKPdFqpWa8bHzfJBMj+xQupFN7H5X7eSTT783FwaIlOXk4YqGfxQDOy pbL2BlCNdJz3aXYtydAicdBjAkiLcww7FH/SQXiPd4gBSWitYcT+70GxNnQdFemPL9jD OkHBruk5SicVyfSlxREs4LQSjN7fqFd/2YFUazt+NsXmSSIWMpuRJI4Vq5ZNJoRsvORo RaOvRh616+NKcMl6Q+wmfz8T4KYFumkSKpKJPagHOSUUEpoyTWOY5bBv0qbuDL+eYyRb dQGpuQeEl3pIcWe2eGMqyy6p1BZpYsc2xOob246M1tmZwCVMuJMWg3zv2q7gNycHUgln ql0Q== X-Forwarded-Encrypted: i=1; AJvYcCUiT24wAV56dLVOrtkA/OJP26fAlpGqPqJ9Xi/JN7Am7K0efYhf4TKFtd+gNL99/0vAMeOGuDafs++DaHg=@vger.kernel.org X-Gm-Message-State: AOJu0YzFTDBqmzlVFD3008+4akgNLkBehTIeW0tjJ4Tk9tT1vDGVjC2/ dpaqomOS0qSklOVBiKgJqRw/PTZuBbx/GUZvbK6I0goku+wT7cRfrpEk X-Gm-Gg: ATEYQzzlU+tsBmL49GMY5ka/jFUb4l9UU9rZjPw9Rzsx8zoKwu5vmFMrLEKcu+XVPpe XaNVpZYhfwqKllZY9OCGC1VWZ/g5FxGhuDownglBP320t26Qo2T84m2ZbMhsn0i+yVaio5hEzrD BpbyKLvkIVc2JYXZl3OxTfwBrbNSzI+FlPuL6uyXR41wjizRHtF4+0beLRbg28cp5ruHKJue7cG 0W7YLOCmiW1ffCsTuSbWnEvyBfhyTxiENYeRXWSpDJbuJr8IdKomcgWq5i+4pfB/OHPNdqUgOIl 5bHWpZaGHLIHD+Xwmx7VKqeRUejW8UZNq4pR3VBEipk3XPxZsEvmWzspTsenNG5f3nltGrOuigy f+Xs7G/HR22pcDPFsjVLox3roET5KxXGGTCzmeCVipuRcfandyaO2VajDlgC0AUQz3mCEcfMXle +SsPLkVc2hIBhrHllciGPkoxdK X-Received: by 2002:a05:6102:b14:b0:602:9b21:eee7 with SMTP id ada2fe7eead31-605682b9af3mr3456400137.35.1775147000487; Thu, 02 Apr 2026 09:23:20 -0700 (PDT) Received: from localhost.localdomain ([2a09:bac5:6d70:aa::11:17b]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-953fba6da8bsm3648919241.10.2026.04.02.09.23.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2026 09:23:18 -0700 (PDT) From: Sebastian Alba Vives To: linux-fpga@vger.kernel.org Cc: yilun.xu@linux.intel.com, conor.dooley@microchip.com, mdf@kernel.org, linux-kernel@vger.kernel.org, Sebastian Josue Alba Vives , stable@vger.kernel.org Subject: [PATCH v3] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() Date: Thu, 2 Apr 2026 10:23:01 -0600 Message-ID: <20260402162302.3804617-1-sebasjosue84@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260402125446.3776153-3-sebasjosue84@gmail.com> References: <20260402125446.3776153-3-sebasjosue84@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sebastian Josue Alba Vives mpf_ops_parse_header() reads several fields from the bitstream file and uses them as offsets and sizes without validating them against the buffer size, leading to multiple out-of-bounds read vulnerabilities: 1. There is no check that count is large enough to read header_size at MPF_HEADER_SIZE_OFFSET (24). Add a minimum count check. 2. When header_size (u8 from file) is 0, the expression *(buf + header_size - 1) reads one byte before the buffer. Return -EINVAL since retrying with a larger buffer cannot fix a zero header_size. 3. In the block lookup loop, block_id_offset and block_start_offset advance by MPF_LOOKUP_TABLE_RECORD_SIZE (9) each iteration with blocks_num (u8) controlling the count. With a small buffer, these offsets exceed count, causing OOB reads via get_unaligned_le32(). Return -EAGAIN with updated header_size so the retry mechanism can request a larger buffer. 4. components_num is read from MPF_DATA_SIZE_OFFSET without checking that the offset is within bounds. Add a bounds check. 5. components_size_start (from file) and component_size_byte_num (derived from components_num, u16 from file) are used as offsets into buf without validation. On 32-bit architectures, their sum could overflow, bypassing the bounds check. Add an overflow check before the bounds comparison. Add bounds checks for all five cases. Fixes: 5f8d4a9008307 ("fpga: microchip-spi: add Microchip MPF FPGA manager") Cc: stable@vger.kernel.org Signed-off-by: Sebastian Alba Vives --- Changes in v3: - Add overflow check for 32-bit architectures in component size loop - Add bounds check for MPF_DATA_SIZE_OFFSET read - Update info->header_size before returning -EAGAIN in block loop so the retry mechanism can request a larger buffer Changes in v2: - Return -EINVAL instead of -EAGAIN for header_size =3D=3D 0 - Return -EAGAIN instead of -EINVAL in block lookup loop - Add count check before reading at MPF_HEADER_SIZE_OFFSET drivers/fpga/microchip-spi.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c index 6134cea..10be986 100644 --- a/drivers/fpga/microchip-spi.c +++ b/drivers/fpga/microchip-spi.c @@ -115,7 +115,13 @@ static int mpf_ops_parse_header(struct fpga_manager *m= gr, return -EINVAL; } =20 + if (count < MPF_HEADER_SIZE_OFFSET + 1) + return -EINVAL; + header_size =3D *(buf + MPF_HEADER_SIZE_OFFSET); + if (!header_size) + return -EINVAL; + if (header_size > count) { info->header_size =3D header_size; return -EAGAIN; @@ -139,6 +145,12 @@ static int mpf_ops_parse_header(struct fpga_manager *m= gr, bitstream_start =3D 0; =20 while (blocks_num--) { + if (block_id_offset >=3D count || + block_start_offset + sizeof(u32) > count) { + info->header_size =3D block_start_offset + sizeof(u32); + return -EAGAIN; + } + block_id =3D *(buf + block_id_offset); block_start =3D get_unaligned_le32(buf + block_start_offset); =20 @@ -175,6 +187,9 @@ static int mpf_ops_parse_header(struct fpga_manager *mg= r, * to each other. Image header should be extended by now up to where * actual bitstream starts, so no need for overflow check anymore. */ + if (MPF_DATA_SIZE_OFFSET + sizeof(u16) > count) + return -EINVAL; + components_num =3D get_unaligned_le16(buf + MPF_DATA_SIZE_OFFSET); =20 for (i =3D 0; i < components_num; i++) { @@ -183,6 +198,11 @@ static int mpf_ops_parse_header(struct fpga_manager *m= gr, component_size_byte_off =3D (i * MPF_BITS_PER_COMPONENT_SIZE) % BITS_PER_BYTE; =20 + if (components_size_start + component_size_byte_num < components_size_st= art || + components_size_start + component_size_byte_num + + sizeof(u32) > count) + return -EINVAL; + component_size =3D get_unaligned_le32(buf + components_size_start + component_size_byte_num); --=20 2.43.0