Re: [PATCH RFC] ubifs: Fix use-after-free in ubifs_tnc_end_commit

From: Zhihao Cheng
Date: Tue Oct 29 2024 - 05:06:45 EST


在 2024/10/29 16:10, Ryder Wang 写道:

Hi, Ryder
I just have some interesting finding.

The code of the function write_index() has a race condition bug. c->enext is used in write_index(), but it is NOT protected by c->tnc_mutex. So there must be SOME race condition problem. I believe some znode may be corrupted or misread if 2 kernel tasks are operating the same znode concurrently.

Thanks for reporting that :-). I noticed it a period time ago too, and I found 'c->znext', 'c->cnext' and 'znode->cnext' won't be accessed (in write mode) by other tasks, because there is only one function do_commit() modifying them and do_commit() can be executed by at most one task in any time.

The code comment of struct ubifs_info below indicates that c->enext must be protected by c->tnc_mutex.
--------------------------------------------------------------------------------------------------
* @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
* @calc_idx_sz
--------------------------------------------------------------------------------------------------

That's why I don't think this patch can fully fix this issue because the race condition is still there.

.