]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-5.15/nfsd-clean-up-potential-nfsd_file-refcount-leaks-in-.patch
Fixes for 5.15
[thirdparty/kernel/stable-queue.git] / queue-5.15 / nfsd-clean-up-potential-nfsd_file-refcount-leaks-in-.patch
1 From 73493b28b964a1fa16d3eb4f47d4b0d174eddc7f Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Tue, 17 Jan 2023 14:38:31 -0500
4 Subject: nfsd: clean up potential nfsd_file refcount leaks in COPY codepath
5
6 From: Jeff Layton <jlayton@kernel.org>
7
8 [ Upstream commit 6ba434cb1a8d403ea9aad1b667c3ea3ad8b3191f ]
9
10 There are two different flavors of the nfsd4_copy struct. One is
11 embedded in the compound and is used directly in synchronous copies. The
12 other is dynamically allocated, refcounted and tracked in the client
13 struture. For the embedded one, the cleanup just involves releasing any
14 nfsd_files held on its behalf. For the async one, the cleanup is a bit
15 more involved, and we need to dequeue it from lists, unhash it, etc.
16
17 There is at least one potential refcount leak in this code now. If the
18 kthread_create call fails, then both the src and dst nfsd_files in the
19 original nfsd4_copy object are leaked.
20
21 The cleanup in this codepath is also sort of weird. In the async copy
22 case, we'll have up to four nfsd_file references (src and dst for both
23 flavors of copy structure). They are both put at the end of
24 nfsd4_do_async_copy, even though the ones held on behalf of the embedded
25 one outlive that structure.
26
27 Change it so that we always clean up the nfsd_file refs held by the
28 embedded copy structure before nfsd4_copy returns. Rework
29 cleanup_async_copy to handle both inter and intra copies. Eliminate
30 nfsd4_cleanup_intra_ssc since it now becomes a no-op.
31
32 Signed-off-by: Jeff Layton <jlayton@kernel.org>
33 Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
34 ---
35 fs/nfsd/nfs4proc.c | 23 ++++++++++-------------
36 1 file changed, 10 insertions(+), 13 deletions(-)
37
38 diff --git a/fs/nfsd/nfs4proc.c b/fs/nfsd/nfs4proc.c
39 index 6fb5f10602233..ada46ef5a093d 100644
40 --- a/fs/nfsd/nfs4proc.c
41 +++ b/fs/nfsd/nfs4proc.c
42 @@ -1512,7 +1512,6 @@ nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
43 long timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
44
45 nfs42_ssc_close(filp);
46 - nfsd_file_put(dst);
47 fput(filp);
48
49 spin_lock(&nn->nfsd_ssc_lock);
50 @@ -1562,13 +1561,6 @@ nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
51 &copy->nf_dst);
52 }
53
54 -static void
55 -nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
56 -{
57 - nfsd_file_put(src);
58 - nfsd_file_put(dst);
59 -}
60 -
61 static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
62 {
63 struct nfsd4_cb_offload *cbo =
64 @@ -1683,12 +1675,18 @@ static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
65 dst->ss_nsui = src->ss_nsui;
66 }
67
68 +static void release_copy_files(struct nfsd4_copy *copy)
69 +{
70 + if (copy->nf_src)
71 + nfsd_file_put(copy->nf_src);
72 + if (copy->nf_dst)
73 + nfsd_file_put(copy->nf_dst);
74 +}
75 +
76 static void cleanup_async_copy(struct nfsd4_copy *copy)
77 {
78 nfs4_free_copy_state(copy);
79 - nfsd_file_put(copy->nf_dst);
80 - if (!nfsd4_ssc_is_inter(copy))
81 - nfsd_file_put(copy->nf_src);
82 + release_copy_files(copy);
83 spin_lock(&copy->cp_clp->async_lock);
84 list_del(&copy->copies);
85 spin_unlock(&copy->cp_clp->async_lock);
86 @@ -1748,7 +1746,6 @@ static int nfsd4_do_async_copy(void *data)
87 } else {
88 nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file,
89 copy->nf_dst->nf_file, false);
90 - nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
91 }
92
93 do_callback:
94 @@ -1811,9 +1808,9 @@ nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
95 } else {
96 status = nfsd4_do_copy(copy, copy->nf_src->nf_file,
97 copy->nf_dst->nf_file, true);
98 - nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
99 }
100 out:
101 + release_copy_files(copy);
102 return status;
103 out_err:
104 if (async_copy)
105 --
106 2.43.0
107