ntfs3.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH resend] fs: ntfs3: Add check for mft_ni in mi_read()
@ 2023-01-14  1:54 Jia-Ju Bai
  2023-01-14  2:13 ` Al Viro
  0 siblings, 1 reply; 2+ messages in thread
From: Jia-Ju Bai @ 2023-01-14  1:54 UTC (permalink / raw)
  To: almaz.alexandrovich, edward.lo
  Cc: ntfs3, linux-kernel, Jia-Ju Bai, TOTE Robot

In a previous commit 2681631c2973, the parameter ni of
attr_load_runs_vcn() can be NULL, and thus a NULL check is added.

However, in the same call stack, this variable is also dereferenced in
mi_read():

mi_read()
  ni_lock(mft_ni);
    attr_load_runs_vcn(mft_ni)
      if (ni) -> Add a check by previous commit (ni is mft_ni)
  ni_unlock(mft_ni);

Thus, to avoid possible null-pointer dereferences, mft_ni should be
also checked in mi_read().

These results are reported by a static tool designed by myself

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
---
 fs/ntfs3/record.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
index defce6a5c8e1..9c5f922543c2 100644
--- a/fs/ntfs3/record.c
+++ b/fs/ntfs3/record.c
@@ -144,13 +144,13 @@ int mi_read(struct mft_inode *mi, bool is_mft)
 	if (err != -ENOENT)
 		goto out;
 
-	if (rw_lock) {
+	if (rw_lock && mft_ni) {
 		ni_lock(mft_ni);
 		down_write(rw_lock);
 	}
 	err = attr_load_runs_vcn(mft_ni, ATTR_DATA, NULL, 0, &mft_ni->file.run,
 				 vbo >> sbi->cluster_bits);
-	if (rw_lock) {
+	if (rw_lock && mft_ni) {
 		up_write(rw_lock);
 		ni_unlock(mft_ni);
 	}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH resend] fs: ntfs3: Add check for mft_ni in mi_read()
  2023-01-14  1:54 [PATCH resend] fs: ntfs3: Add check for mft_ni in mi_read() Jia-Ju Bai
@ 2023-01-14  2:13 ` Al Viro
  0 siblings, 0 replies; 2+ messages in thread
From: Al Viro @ 2023-01-14  2:13 UTC (permalink / raw)
  To: Jia-Ju Bai
  Cc: almaz.alexandrovich, edward.lo, ntfs3, linux-kernel, TOTE Robot

On Sat, Jan 14, 2023 at 09:54:41AM +0800, Jia-Ju Bai wrote:
> In a previous commit 2681631c2973, the parameter ni of
> attr_load_runs_vcn() can be NULL, and thus a NULL check is added.
> 
> However, in the same call stack, this variable is also dereferenced in
> mi_read():
> 
> mi_read()
>   ni_lock(mft_ni);
>     attr_load_runs_vcn(mft_ni)
>       if (ni) -> Add a check by previous commit (ni is mft_ni)
>   ni_unlock(mft_ni);
> 
> Thus, to avoid possible null-pointer dereferences, mft_ni should be
> also checked in mi_read().
> 
> These results are reported by a static tool designed by myself

No, it should not.  ni_lock(mft_ni) is called only if rw_lock
is not NULL.  The only assignment of non-NULL to that variable is
here:

        if (is_mounted(sbi)) {
                if (!is_mft) {
                        rw_lock = &mft_ni->file.run_lock;
                        down_read(rw_lock);
                }
        }

Note that it would have already oopsed had mft_ni been NULL.

The logics might or might not be wrong there, but could we please
stop obfuscating it by checks piled higher and deeper just in case?

Incidentally, I hope the pattern that triggered here is not

	f() checks for its argument being NULL, one of the callers of f() passes it a pointer
	therefore that pointer might be NULL

for obvious reasons...

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-14  2:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-14  1:54 [PATCH resend] fs: ntfs3: Add check for mft_ni in mi_read() Jia-Ju Bai
2023-01-14  2:13 ` Al Viro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).