Skip to contentSkip to conversation

nfsd: fix memory leak in __cld_pipe_inprogress_downcall()

PATCH In review Aleksandr Burakov 3 participants 3 messages Feb 16, 2024 – Feb 18, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfsd: fix memory leak in __cld_pipe_inprogress_downcall()

Dynamic memory, referenced by 'princhash.data' and 'name.data', is allocated by calling function 'memdup_user' and lost at __cld_pipe_inprogress_downcall() function return

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 11a60d159259 ("nfsd: add a "GetVersion" upcall for nfsdcld")
---
 fs/nfsd/nfs4recover.c | 2 ++
 1 file changed, 2 insertions(+)
fs/nfsd/nfs4recover.c +2 -0
index 2c060e0b1604..02663484782d 100644
@@ -850,6 +850,8 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
 			kfree(princhash.data);
 			return -EFAULT;
 		}
+		kfree(name.data);
+		kfree(princhash.data);
 		return nn->client_tracking_ops->msglen;
 	}
 	return -EFAULT;
Signature · 1 line
2.25.1
#1 lore raw

Hello Aleksandr,

Quoted 3 lines · Aleksandr Burakov

Dynamic memory, referenced by 'princhash.data' and 'name.data', is allocated by calling function 'memdup_user' and lost at __cld_pipe_inprogress_downcall() function return

It is not actually lost. If nfs4_client_to_reclaim() fails and thus returns NULL - this error case is already properly handled.

If nfs4_client_to_reclaim() succeeds then reference to the memory in question is passed to crp->cr_name.data and crp->cr_princhash.data correspondingly, and crp->cr_strhash entry is added to the list associated with nfsd_net. In this case the memory is supposed to be freed by nfs4_remove_reclaim_record(). See comment for nfs4_client_to_reclaim().

So I think the patch just introduces a double-free.

Quoted 24 lines

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 11a60d159259 ("nfsd: add a "GetVersion" upcall for nfsdcld")
---
 fs/nfsd/nfs4recover.c | 2 ++
 1 file changed, 2 insertions(+)
fs/nfsd/nfs4recover.c +2 -0
index 2c060e0b1604..02663484782d 100644
@@ -850,6 +850,8 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
 			kfree(princhash.data);
 			return -EFAULT;
 		}
+		kfree(name.data);
+		kfree(princhash.data);
 		return nn->client_tracking_ops->msglen;
 	}
 	return -EFAULT;
Signature · 1 line
2.25.1

--
Fedor

#2 lore raw
Quoted 10 lines · Fedor Pchelkin

Hello Aleksandr,

Quoted 3 lines · Aleksandr Burakov

Dynamic memory, referenced by 'princhash.data' and 'name.data', is allocated by calling function 'memdup_user' and lost at __cld_pipe_inprogress_downcall() function return

It is not actually lost. If nfs4_client_to_reclaim() fails and thus returns NULL - this error case is already properly handled.

Quoted 7 lines

If nfs4_client_to_reclaim() succeeds then reference to the memory in question is passed to crp->cr_name.data and crp->cr_princhash.data correspondingly, and crp->cr_strhash entry is added to the list associated with nfsd_net. In this case the memory is supposed to be freed by nfs4_remove_reclaim_record(). See comment for nfs4_client_to_reclaim().

So I think the patch just introduces a double-free.

Agreed - this patch is incorrect.

Thanks,
NeilBrown

Quoted 30 lines
Quoted 25 lines

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 11a60d159259 ("nfsd: add a "GetVersion" upcall for nfsdcld")
---
 fs/nfsd/nfs4recover.c | 2 ++
 1 file changed, 2 insertions(+)
fs/nfsd/nfs4recover.c +2 -0
index 2c060e0b1604..02663484782d 100644
@@ -850,6 +850,8 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
 			kfree(princhash.data);
 			return -EFAULT;
 		}
+		kfree(name.data);
+		kfree(princhash.data);
 		return nn->client_tracking_ops->msglen;
 	}
 	return -EFAULT;
Signature · 1 line
2.25.1

--
Fedor