net/handshake: fix file ref count in handshake_nl_accept_doit()
PATCH 1/1 net/handshake: fix file ref count in handshake_nl_accept_doit()
If req->hr_proto->hp_accept() fail, we call fput() twice:
Once in the error path, but also a second time because sock->file is at that point already associated with the file descriptor. Once the task exits, as it would probably do after receiving an error reading from netlink, the fd is closed, calling fput() a second time.
To fix, we move installing the file after the error path for the hp_accept() call. In the case of errors we simply put the unused fd. In case of success we can use fd_install() to link the sock->file to the reserved fd.
--- net/handshake/netlink.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-)
net/handshake/netlink.c +5 -25
@@ -87,29 +87,6 @@ struct nlmsghdr *handshake_genl_put(struct sk_buff *msg, }EXPORT_SYMBOL (handshake_genl_put); -/* -* dup() a kernel socket for use as a user space file descriptor -* in the current process. The kernel socket must have an -* instatiated struct file. -* -* Implicit argument: "current()" -*/ -static int handshake_dup (struct socket *sock) -{ -struct file *file; -int newfd; - - file =get_file (sock->file); - newfd =get_unused_fd_flags (O_CLOEXEC ); -if (newfd <0 ) { -fput (file); -return newfd; - } - -fd_install (newfd, file); -return newfd; -} -int handshake_nl_accept_doit (struct sk_buff *skb,struct genl_info *info) {struct net *net =sock_net (skb->sk);
@@ -133,17 +110,20 @@ int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info) goto out_status; sock = req->hr_sk->sk_socket; - fd =handshake_dup (sock); + fd =get_unused_fd_flags (O_CLOEXEC );if (fd <0 ) { err = fd; goto out_complete; } + err = req->hr_proto->hp_accept (req, info, fd);if (err) { -fput (sock->file); +put_unused_fd (fd); goto out_complete; } +fd_install (fd,get_file (sock->file)); +trace_handshake_cmd_accept (net, req, req->hr_sk, fd);return 0 ;
Signature · 1 line
2.41.0
Quoted 64 lines · Moritz Wanzenböck
If
req->hr_proto->hp_accept()fail, we callfput()twice:
Once in the error path, but also a second time becausesock->fileis at that point already associated with the file descriptor. Once the task exits, as it would probably do after receiving an error reading from netlink, the fd is closed, callingfput()a second time.To fix, we move installing the file after the error path for the
hp_accept()call. In the case of errors we simply put the unused fd. In case of success we can usefd_install()to link thesock->fileto the reserved fd.Fixes 7ea9c1ec66bc ("net/handshake: Fix handshake_dup() ref counting")Signed-off-by Moritz Wanzenböck <[email protected]>--- net/handshake/netlink.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-)net/handshake/netlink.c +2
-24@@ -87,29 +87,6 @@ struct nlmsghdr *handshake_genl_put(struct sk_buff *msg, }EXPORT_SYMBOL (handshake_genl_put); -/* -* dup() a kernel socket for use as a user space file descriptor -* in the current process. The kernel socket must have an -* instatiated struct file. -* -* Implicit argument: "current()" -*/ -static int handshake_dup (struct socket *sock) -{ -struct file *file; -int newfd; - - file =get_file (sock->file); - newfd =get_unused_fd_flags (O_CLOEXEC ); -if (newfd <0 ) { -fput (file); -return newfd; - } - -fd_install (newfd, file); -return newfd; -} -int handshake_nl_accept_doit (struct sk_buff *skb,struct genl_info *info) {struct net *net =sock_net (skb->sk);@@ -133,17 +110,20 @@ int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info) goto out_status; sock = req->hr_sk->sk_socket; - fd =handshake_dup (sock); + fd =get_unused_fd_flags (O_CLOEXEC );if (fd <0 ) { err = fd; goto out_complete; } + err = req->hr_proto->hp_accept (req, info, fd);if (err) {
net/handshake/netlink.c int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info) · line 129
Quoted 10 lines
net/handshake/netlink.c +3
-1-fput (sock->file); +put_unused_fd (fd); goto out_complete; } +fd_install (fd,get_file (sock->file)); +trace_handshake_cmd_accept (net, req, req->hr_sk, fd);return 0 ;
??
You sure this works?
With this patch 'fd' won't be available during the 'hp_accept' call, but I thought that we might want to read from the socket here. Hmm?
Cheers,
Hannes
Quoted 6 lines · Hannes Reinecke
??
You sure this works?
With this patch 'fd' won't be available during the 'hp_accept' call, but I thought that we might want to read from the socket here. Hmm?
Yes, this works. 'hp_accept' only constructs the netlink message for user space, so it only needs 'fd' for its integer value.
Regards,
Moritz
Quoted 38 lines · Moritz Wanzenböck
If
req->hr_proto->hp_accept()fail, we callfput()twice:
Once in the error path, but also a second time becausesock->fileis at that point already associated with the file descriptor. Once the task exits, as it would probably do after receiving an error reading from netlink, the fd is closed, callingfput()a second time.To fix, we move installing the file after the error path for the
hp_accept()call. In the case of errors we simply put the unused fd. In case of success we can usefd_install()to link thesock->fileto the reserved fd.Fixes 7ea9c1ec66bc ("net/handshake: Fix handshake_dup() ref counting")Signed-off-by Moritz Wanzenböck <[email protected]>--- net/handshake/netlink.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-)net/handshake/netlink.c +0
-13@@ -87,29 +87,6 @@ struct nlmsghdr *handshake_genl_put(struct sk_buff *msg, }EXPORT_SYMBOL (handshake_genl_put); -/* -* dup() a kernel socket for use as a user space file descriptor -* in the current process. The kernel socket must have an -* instatiated struct file. -* -* Implicit argument: "current()" -*/ -static int handshake_dup (struct socket *sock) -{ -struct file *file; -int newfd; - - file =get_file (sock->file);
net/handshake/netlink.c struct nlmsghdr *handshake_genl_put(struct sk_buff *msg, · line 112
Quoted 10 lines
net/handshake/netlink.c +0
-10- newfd =get_unused_fd_flags (O_CLOEXEC ); -if (newfd <0 ) { -fput (file); -return newfd; - } - -fd_install (newfd, file); -return newfd; -} -
The point of the handshake_dup() helper was to underscore the similarities between dup(2) and handshake ACCEPT. That similarity gets fainter over time.
Quoted 30 lines
net/handshake/netlink.c +5
-2int handshake_nl_accept_doit (struct sk_buff *skb,struct genl_info *info) {struct net *net =sock_net (skb->sk);@@ -133,17 +110,20 @@ int handshake_nl_accept_doit(struct sk_buff *skb, struct genl_info *info) goto out_status; sock = req->hr_sk->sk_socket; - fd =handshake_dup (sock); + fd =get_unused_fd_flags (O_CLOEXEC );if (fd <0 ) { err = fd; goto out_complete; } + err = req->hr_proto->hp_accept (req, info, fd);if (err) { -fput (sock->file); +put_unused_fd (fd); goto out_complete; } +fd_install (fd,get_file (sock->file)); +trace_handshake_cmd_accept (net, req, req->hr_sk, fd);return 0 ;Signature · 1 line
2.41.0
Signature · 1 line
Chuck Lever
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 6 lines · On Thu, 19 Oct 2023 14:58:47 +0200 you wrote:
If
req->hr_proto->hp_accept()fail, we callfput()twice:
Once in the error path, but also a second time becausesock->fileis at that point already associated with the file descriptor. Once the task exits, as it would probably do after receiving an error reading from netlink, the fd is closed, callingfput()a second time.
Quoted 6 lines
To fix, we move installing the file after the error path for the
hp_accept()call. In the case of errors we simply put the unused fd. In case of success we can usefd_install()to link thesock->fileto the reserved fd.[...]
Here is the summary with links:
- net/handshake: fix file ref count in handshake_nl_accept_doit()
https://git.kernel.org/netdev/net/c/7798b59409c3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html