mtdchar: prevent unbounded allocation in MEMWRITE ioctl
authorMichał Kępień <kernel@kempniu.pl>
Tue, 30 Nov 2021 11:31:49 +0000 (12:31 +0100)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Thu, 9 Dec 2021 16:52:29 +0000 (17:52 +0100)
commit6420ac0af95dbcb2fd8452e2d551ab50e1bbad83
tree8c60fc35d095bb16a0daae2820a7813936363aea
parentdd8a2e884a462c09a562f04927cb227e3cdaa498
mtdchar: prevent unbounded allocation in MEMWRITE ioctl

In the mtdchar_write_ioctl() function, memdup_user() is called with its
'len' parameter set to verbatim values provided by user space via a
struct mtd_write_req.  Both the 'len' and 'ooblen' fields of that
structure are 64-bit unsigned integers, which means the MEMWRITE ioctl
can trigger unbounded kernel memory allocation requests.

Fix by iterating over the buffers provided by user space in a loop,
processing at most mtd->erasesize bytes in each iteration.  Adopt some
checks from mtd_check_oob_ops() to retain backward user space
compatibility.

Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
Signed-off-by: Michał Kępień <kernel@kempniu.pl>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20211130113149.21848-1-kernel@kempniu.pl
drivers/mtd/mtdchar.c