Skip to contentSkip to conversation

fanotify: avoid possible NULL dereference

PATCH In review Su Hui 3 participants 3 messages Aug 9, 2023 – Jan 2, 2024

Subsystems (4)
lore
Share & tools
lore

Su Hui

author
#0 lore raw

PATCH 1/1 fanotify: avoid possible NULL dereference

smatch error:
fs/notify/fanotify/fanotify_user.c:462 copy_fid_info_to_user():
we previously assumed 'fh' could be null (see line 421)

Fixes afc894c784c8 ("fanotify: Store fanotify handles differently")
---
 fs/notify/fanotify/fanotify_user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/notify/fanotify/fanotify_user.c +2 -1
index f69c451018e3..5a5487ae2460 100644
@@ -459,12 +459,13 @@ static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
 	if (WARN_ON_ONCE(len < sizeof(handle)))
 		return -EFAULT;
 
-	handle.handle_type = fh->type;
 	handle.handle_bytes = fh_len;
 
 	/* Mangle handle_type for bad file_handle */
 	if (!fh_len)
 		handle.handle_type = FILEID_INVALID;
+	else
+		handle.handle_type = fh->type;
 
 	if (copy_to_user(buf, &handle, sizeof(handle)))
 		return -EFAULT;
Signature · 1 line
2.30.2
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Su Hui

smatch error:
fs/notify/fanotify/fanotify_user.c:462 copy_fid_info_to_user():
we previously assumed 'fh' could be null (see line 421)

Fixes afc894c784c8 ("fanotify: Store fanotify handles differently")
Quoted 26 lines
---
 fs/notify/fanotify/fanotify_user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/notify/fanotify/fanotify_user.c +2 -2
index f69c451018e3..5a5487ae2460 100644
@@ -459,12 +459,13 @@ static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
        if (WARN_ON_ONCE(len < sizeof(handle)))
                return -EFAULT;
 
-       handle.handle_type = fh->type;
        handle.handle_bytes = fh_len;
 
        /* Mangle handle_type for bad file_handle */
        if (!fh_len)
                handle.handle_type = FILEID_INVALID;
+       else
+               handle.handle_type = fh->type;
 
        if (copy_to_user(buf, &handle, sizeof(handle)))
                return -EFAULT;
--

2.30.2

maintainer
#2 lore raw
Quoted 10 lines · Amir Goldstein
Quoted 7 lines · Su Hui

smatch error:
fs/notify/fanotify/fanotify_user.c:462 copy_fid_info_to_user():
we previously assumed 'fh' could be null (see line 421)

Fixes afc894c784c8 ("fanotify: Store fanotify handles differently")

I'm sorry but this has somehow fallen through the cracks.

Quoted 10 lines

fs/notify/fanotify/fanotify_user.c static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh, · line 462

Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +0 -1
index f69c451018e3..5a5487ae2460 100644
@@ -459,12 +459,13 @@ static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
        if (WARN_ON_ONCE(len < sizeof(handle)))
                return -EFAULT;
 
-       handle.handle_type = fh->type;
        handle.handle_bytes = fh_len;

Well, if passed 'fh' is NULL, we have problems later in the function anyway. E.g. in fanotify_fh_buf() a few lines below. So I think this needs a bit more work that just this small fixup...

Honza

Quoted 12 lines
Quoted 12 lines
fs/notify/fanotify/fanotify_user.c +2 -1
        /* Mangle handle_type for bad file_handle */
        if (!fh_len)
                handle.handle_type = FILEID_INVALID;
+       else
+               handle.handle_type = fh->type;
 
        if (copy_to_user(buf, &handle, sizeof(handle)))
                return -EFAULT;
--

2.30.2

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR