Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1236535

[PATCH] mtd: mtdram: check offs and len in mtdram->erase

From Sudip Mukherjee <sudipm.mukherjee@gmail.com>
Newsgroups linux.kernel
Subject [PATCH] mtd: mtdram: check offs and len in mtdram->erase
Date 2015-09-30 18:50 +0200
Message-ID <qesJR-2eW-29@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


We should prevent user to erasing mtd device with an unaligned offset
or length.

Signed-off-by: Sudip Mukherjee <sudip@vectorindia.org>
---

I am not sure if I should add the Signed-off-by of
Dongsheng Yang <yangds.fnst@cn.fujitsu.com> . He is the original author
and he should get the credit for that.

 drivers/mtd/devices/mtdram.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/drivers/mtd/devices/mtdram.c b/drivers/mtd/devices/mtdram.c
index 8e28508..21b6a05 100644
--- a/drivers/mtd/devices/mtdram.c
+++ b/drivers/mtd/devices/mtdram.c
@@ -32,8 +32,35 @@ MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
 // We could store these in the mtd structure, but we only support 1 device..
 static struct mtd_info *mtd_info;
 
+static int check_offs_len(struct mtd_info *mtd, loff_t ofs, uint64_t len)
+{
+	int ret = 0;
+	uint64_t temp_len, rem;
+
+	/* Start address must align on block boundary */
+	temp_len = ofs;
+	rem = do_div(temp_len, mtd->erasesize);
+	if (rem) {
+		pr_debug("%s: unaligned address\n", __func__);
+		ret = -EINVAL;
+	}
+
+	/* Length must align on block boundary */
+	temp_len = len;
+	rem = do_div(temp_len, mtd->erasesize);
+
+	if (rem) {
+		pr_debug("%s: length not block aligned\n", __func__);
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
 static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
 {
+	if (check_offs_len(mtd, instr->addr, instr->len))
+		return -EINVAL;
 	memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
 	instr->state = MTD_ERASE_DONE;
 	mtd_erase_callback(instr);
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH] mtd: mtdram: check offs and len in mtdram->erase Sudip Mukherjee <sudipm.mukherjee@gmail.com> - 2015-09-30 18:50 +0200
  Re: [PATCH] mtd: mtdram: check offs and len in mtdram->erase Dongsheng Yang <yangds.fnst@cn.fujitsu.com> - 2015-10-02 11:50 +0200
    Re: [PATCH] mtd: mtdram: check offs and len in mtdram->erase Sudip Mukherjee <sudipm.mukherjee@gmail.com> - 2015-10-02 12:10 +0200
      Re: [PATCH] mtd: mtdram: check offs and len in mtdram->erase Brian Norris <computersforpeace@gmail.com> - 2015-10-02 19:40 +0200
        Re: [PATCH] mtd: mtdram: check offs and len in mtdram->erase Dongsheng Yang <yangds.fnst@cn.fujitsu.com> - 2015-10-03 05:40 +0200
          Re: [PATCH] mtd: mtdram: check offs and len in mtdram->erase Sudip Mukherjee <sudipm.mukherjee@gmail.com> - 2015-10-03 07:50 +0200

csiph-web