From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 B94D038AC6A for ; Thu, 2 Jul 2026 08:27:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782980872; cv=none; b=rgBw5uwPc20xskHzyBz7wsslByfQDkkvb8inAoFwSf7ykGof9Lm3leuAG8jKN2erJ28vAAPaK1b8j9eSCJJgO716oIzG7/Tjydo4nTi3+fYnzzqrhT1wSkDHSyDw+24Wynl3i0mZsjis7BhT+sw/bZUfUQtblYgTEHBenL2XfxM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782980872; c=relaxed/simple; bh=Qek9Yemwl4vUaqsBqUb9Ho8bk1UMP/x4qTPBJwubhaE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=c8QdQPkKFLnsT9yBMqPNlAYfyHXwlbk83CyeL6wn/b2fZ8sepQqlwE6V0P0OQUhePAEthIS7cK72LO587Dfsh+AG17IvKnlY5ZM9r+UDwg7Zxe0Wc1WIAB3BMlG47eNrw1NguYnoTCtOyOZ62LpO9BHV+/dVFSHmbsBiJgo8NSo= 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=p/c1TStR; arc=none smtp.client-ip=209.85.216.43 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="p/c1TStR" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-3804e42ad5fso1584115a91.2 for ; Thu, 02 Jul 2026 01:27:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782980870; x=1783585670; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qAwSQ1LknlyOEsts4qb8f+gHcGdJkzpR6pYbN5IdNQg=; b=p/c1TStR/xb7nIJhm48p5zLcSnbPaAdghWqATY6UEVJgV099wYmkAk53A0LreQOYpB FHJNDlirAc6LR13R4ZMCcQYSWtkln491Vo1+d10oUE3KKJ4hJx7x6ipwvKW6+3G4BOLa fycFDZw8sfGpVoSxoMJJYp3Lcl5Lhex1ncCWIfVIHFcl1PE8NqkpBM+ra9BYGZZqLDSw /GMFlF9P4N3bB03n2/E4jVmy7Cs+Nx2FWbb5s2ZxULAlt7dtDFS5MAzQ2FsDKzLWrTVL UQtzqLHUh71Bfxe5oluOemz7T9NdLDO7yL162e3gsi0Dh4JxIwaALUtKfcxL+1WjAhUn wtYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782980870; x=1783585670; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qAwSQ1LknlyOEsts4qb8f+gHcGdJkzpR6pYbN5IdNQg=; b=RdzcnhRJT3YynQWKDt/Cagv6UL3iJAelDPqO6l+TUc/vmS8RtuBvI9bEg/RQ6mGnNf 3v7P4nwTssgjOEhX54WY8pvR3TjyEecOOh4F++A6eS6XjUrXcbb+cFrCXYMoIesS4dH1 VEzoGcn/XYrRLmfe8iXzON7iQkKaLnNk+Dssxykhu3HtOTjtTyWzbN2Cy53bgsegMUhv DciebKl5cSLfe1QcRONX4MMFcrZ/IXP1/J29/r3CK/PRwve4hDya9xNTJ27e5MIseenR hXjxvWtd1Ubw4AvyDUmOedEnZjwgvIQK8OMFtV1Fq3SIpbueAxCDklqLCINVqM1q02rf QK1Q== X-Forwarded-Encrypted: i=1; AHgh+Romsl0JFRkbHA+edQw92TMynBX3YZpAFWtVzu1goKr6eNdxfNxjbYKVAivfXh6EeAb09baMFyq7wZludac=@vger.kernel.org X-Gm-Message-State: AOJu0Yz94iBrNgCF0m64Qaue8HZEpL99E8eG/fCpKaMdX4y++csnJmSp KbA0m+P0/9zP6L1T0ynHW/hlsK5O+JNHWMx7UQt9F/8FPg/5yyUL9KNr X-Gm-Gg: AfdE7cnTN+s/3gIOBY7ENA4bXLhrdwvaenfs7Na8yZ9/a77S87k/iRTTQwE93H0uwW0 y0HpYIlZ1+6ikk2ZhsKB9s4hdxr6MPtgw3PsE+yeCi9JaoVpg0tKYcv/YJ6UBTOqYS7lY+yVKUR cXxPllA1HNH6DUpTunSESSqOolF42KQHz9HCOJbXWiOa+DxGNsvVLPoZOGJtflQy2B6PvKuYau6 zuqsB/rofYdbCzLDpv5/Zs0kNDQkbHW9+5V7MBTlIX1n5JOROLFPAtw26X3Q4O2oG8bWql2KCQl TUC0/x5YqzFcfvSv+7BQCAHuP3oFckyQpiKUwE4NiD3dxYy2gi3Tbpg0TRdNyAvMTs5GaM/7Wf7 H3vFKeHg5xP1pnHGedD+21dFSg+0j+5L8I8l9mDn2G16JmCQlSCsWiQvQSyDnY+09KQu/IVItPN LtCe33tLUdlhrUZgdVEpAbgs3O2xfDtZA1lUZNzA== X-Received: by 2002:a17:90a:ec85:b0:366:132:fda7 with SMTP id 98e67ed59e1d1-380ba868183mr4288109a91.10.1782980869794; Thu, 02 Jul 2026 01:27:49 -0700 (PDT) Received: from csl-conti-dell7858.ntu.edu.sg ([155.69.195.57]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-380e165be46sm671663a91.7.2026.07.02.01.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jul 2026 01:27:49 -0700 (PDT) From: Maoyi Xie To: Maxim Levitsky Cc: Alex Dubov , Ulf Hansson , linux-mmc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] memstick: ms_block: reject a card that reports too many blocks Date: Thu, 2 Jul 2026 16:27:45 +0800 Message-Id: <20260702082745.1887848-1-maoyixie.tju@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit msb_ftl_initialize() computes the zone count from the card block count with no bound: msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE; ... for (i = 0; i < msb->zone_count; i++) msb->free_block_count[i] = MS_BLOCKS_IN_ZONE; msb->block_count is a card value. msb_read_boot_blocks() reads number_of_blocks from the card boot page and byte swaps it. free_block_count is a fixed int[MS_MAX_ZONES]. MS_MAX_ZONES is 16, so the valid indices are 0 to 15. The init loop above indexes it by zone_count. msb_mark_block_used() and msb_mark_block_unused() index it by pba / MS_BLOCKS_IN_ZONE, for pba up to block_count - 1. A card may report up to 65535 blocks. A block_count above 8192 (MS_MAX_ZONES * MS_BLOCKS_IN_ZONE) lets the pba index reach 16. That writes past free_block_count[] and corrupts struct msb_data. A larger count runs the init loop past the end too. A real Memory Stick has at most 16 zones. So it has at most 8192 blocks. msb_ftl_initialize() now rejects a card that reports more than MS_MAX_ZONES * MS_BLOCKS_IN_ZONE blocks. Fixes: 0ab30494bc4f ("memstick: add support for legacy memorysticks") Cc: stable@vger.kernel.org Signed-off-by: Maoyi Xie --- I do not have the hardware. I showed the overflow with a small harness. The harness replays the store loop into free_block_count[]. drivers/memstick/core/ms_block.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c index a01fe31355..ce33907bfc 100644 --- a/drivers/memstick/core/ms_block.c +++ b/drivers/memstick/core/ms_block.c @@ -1338,6 +1338,10 @@ static int msb_ftl_initialize(struct msb_data *msb) return 0; msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE; + if (msb->block_count > MS_MAX_ZONES * MS_BLOCKS_IN_ZONE) { + pr_err("Too many blocks: %d\n", msb->block_count); + return -EINVAL; + } msb->logical_block_count = msb->zone_count * 496 - 2; msb->used_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL); -- 2.34.1