From: Zhihao Cheng <chengzhihao1@huawei.com>
To: ZhaoLong Wang <wangzhaolong1@huawei.com>, <richard@nod.at>,
<miquel.raynal@bootlin.com>, <vigneshr@ti.com>
Cc: <linux-mtd@lists.infradead.org>, <linux-kernel@vger.kernel.org>,
<yi.zhang@huawei.com>, <yangerkun@huawei.com>
Subject: Re: [PATCH RFC] ubi: gluebi: Fix NULL pointer dereference caused by ftl notifier
Date: Thu, 12 Oct 2023 19:25:39 +0800 [thread overview]
Message-ID: <f1b27408-6eae-4195-4c97-c286c4ad04db@huawei.com> (raw)
In-Reply-To: <2d04fa9e-e594-705c-339b-3090cb7d6fbd@huawei.com>
在 2023/10/12 17:31, ZhaoLong Wang 写道:
>
>>>
>>>> 3. P1 P2
>>>> gluebi_create -> mtd_device_register -> add_mtd_device:
>>>> device_register // dev/mtd1 is visible
>>>>
>>>> fd = open(/dev/mtd1, O_WRONLY)
>>>> gluebi_get_device
>>>> gluebi->desc = ubi_open_volume
>>>>
>>>> ftl_add_mtd
>>>> mtd_read
>>>> gluebi_read
>>>> gluebi->desc is not ERR_PTR/NULL
>>>>
>>>> close(fd)
>>>> gluebi_put_device
>>>> ubi_close_volume
>>>> kfree(desc)
>>>> ubi_read(gluebi->desc) // UAF (×)
>>>>
>>>
>>> Yes, it's also a problem. Perhaps it should be set to NULL after
>>> destroying gluebi->desc.
>>
>> The key point is that 'gluebi->desc' check & usage is not atomic in
>> gluebi_read. So following patch still can't handle situation 3.
>>
>
> Setting the desc to NULL works because
> mutex_lock "mtd_table_mutex" is held on all paths where
> ftl_add_mtd() is called.
>
Oh, you're right. Just one nit below:
> @@ -154,9 +159,26 @@ static int gluebi_read(struct mtd_info *mtd, loff_t
> from, size_t len,
> size_t *retlen, unsigned char *buf)
> {
> int err = 0, lnum, offs, bytes_left;
> - struct gluebi_device *gluebi;
> + struct gluebi_device *gluebi = container_of(mtd, struct
gluebi_device,
> + mtd);
> + int isnt_get = unlikely(gluebi->desc == NULL) ? 1 : 0;
This 'unlikey' can be removed.
Rename 'isnt_get' as 'has_desc' ?
> + /**
> + * In normal case, the UBI volume desc has been initialized by
> + * ->_get_device(). However, in the ftl notifier process, the
> + * ->_get_device() is not executed in advance and the MTD device
> + * is directly scanned which cause null pointe dereference.
> + * Therefore, try to get the MTD device here.
> + */
> + if (unlikely(isnt_get)) {
> + err = __get_mtd_device(mtd);
> + if (err) {
> + err_msg("cannot get MTD device %d, UBI device %d, volume
> %d, error %d",
> + mtd->index, gluebi->ubi_num, gluebi->vol_id, err);
> + return err;
> + }
> + }
next prev parent reply other threads:[~2023-10-12 11:26 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-10 14:29 ZhaoLong Wang
2023-10-11 2:32 ` ZhaoLong Wang
2023-10-12 2:38 ` Zhihao Cheng
2023-10-12 8:04 ` ZhaoLong Wang
2023-10-12 8:18 ` Zhihao Cheng
2023-10-12 9:31 ` ZhaoLong Wang
2023-10-12 11:25 ` Zhihao Cheng [this message]
2023-10-12 2:17 ` Zhihao Cheng
2023-10-12 11:31 ` ZhaoLong Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f1b27408-6eae-4195-4c97-c286c4ad04db@huawei.com \
--to=chengzhihao1@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=vigneshr@ti.com \
--cc=wangzhaolong1@huawei.com \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®