diff mbox series

[v2,1/2] sctp: export sctp_endpoint_{hold,put}() and return incremented endpoint

Message ID 20211217134607.74983-1-lee.jones@linaro.org (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [v2,1/2] sctp: export sctp_endpoint_{hold,put}() and return incremented endpoint | expand

Checks

Context Check Description
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 66 this patch: 66
netdev/cc_maintainers fail 1 blamed authors not CCed: lucien.xin@gmail.com; 1 maintainers not CCed: lucien.xin@gmail.com
netdev/build_clang success Errors and warnings before: 20 this patch: 20
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 68 this patch: 68
netdev/checkpatch warning WARNING: function definition argument 'struct sctp_endpoint *' should also have an identifier name
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/tree_selection success Guessing tree name failed - patch did not apply

Commit Message

Lee Jones Dec. 17, 2021, 1:46 p.m. UTC
net/sctp/diag.c for instance is built into its own separate module
(sctp_diag.ko) and requires the use of sctp_endpoint_{hold,put}() in
order to prevent a recently found use-after-free issue.

In order to prevent data corruption of the pointer used to take a
reference on a specific endpoint, between the time of calling
sctp_endpoint_hold() and it returning, the API now returns a pointer
to the exact endpoint that was incremented.

For example, in sctp_sock_dump(), we could have the following hunk:

	sctp_endpoint_hold(tsp->asoc->ep);
	ep = tsp->asoc->ep;
	sk = ep->base.sk
	lock_sock(ep->base.sk);

It is possible for this task to be swapped out immediately following
the call into sctp_endpoint_hold() that would change the address of
tsp->asoc->ep to point to a completely different endpoint.  This means
a reference could be taken to the old endpoint and the new one would
be processed without a reference taken, moreover the new endpoint
could then be freed whilst still processing as a result, causing a
use-after-free.

If we return the exact pointer that was held, we ensure this task
processes only the endpoint we have taken a reference to.  The
resultant hunk now looks like this:

        ep = sctp_endpoint_hold(tsp->asoc->ep);
	sk = ep->base.sk
	lock_sock(sk);

Cc: Vlad Yasevich <vyasevich@gmail.com>
Cc: Neil Horman <nhorman@tuxdriver.com>
Cc: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: lksctp developers <linux-sctp@vger.kernel.org>
Cc: "H.P. Yarroll" <piggy@acm.org>
Cc: Karl Knutson <karl@athena.chicago.il.us>
Cc: Jon Grimm <jgrimm@us.ibm.com>
Cc: Xingang Guo <xingang.guo@intel.com>
Cc: Hui Huang <hui.huang@nokia.com>
Cc: Sridhar Samudrala <sri@us.ibm.com>
Cc: Daisy Chang <daisyc@us.ibm.com>
Cc: Ryan Layer <rmlayer@us.ibm.com>
Cc: Kevin Gao <kevin.gao@intel.com>
Cc: linux-sctp@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: stable@vger.kernel.org
Fixes: 8f840e47f190c ("sctp: add the sctp_diag.c file")
Signed-off-by: Lee Jones <lee.jones@linaro.org>
---
 include/net/sctp/structs.h | 2 +-
 net/sctp/endpointola.c     | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)

Comments

David Laight Dec. 17, 2021, 2:17 p.m. UTC | #1
From: Lee Jones
> Sent: 17 December 2021 13:46
> 
> net/sctp/diag.c for instance is built into its own separate module
> (sctp_diag.ko) and requires the use of sctp_endpoint_{hold,put}() in
> order to prevent a recently found use-after-free issue.
> 
> In order to prevent data corruption of the pointer used to take a
> reference on a specific endpoint, between the time of calling
> sctp_endpoint_hold() and it returning, the API now returns a pointer
> to the exact endpoint that was incremented.
> 
> For example, in sctp_sock_dump(), we could have the following hunk:
> 
> 	sctp_endpoint_hold(tsp->asoc->ep);
> 	ep = tsp->asoc->ep;
> 	sk = ep->base.sk
> 	lock_sock(ep->base.sk);
> 
> It is possible for this task to be swapped out immediately following
> the call into sctp_endpoint_hold() that would change the address of
> tsp->asoc->ep to point to a completely different endpoint.  This means
> a reference could be taken to the old endpoint and the new one would
> be processed without a reference taken, moreover the new endpoint
> could then be freed whilst still processing as a result, causing a
> use-after-free.
> 
> If we return the exact pointer that was held, we ensure this task
> processes only the endpoint we have taken a reference to.  The
> resultant hunk now looks like this:
> 
>         ep = sctp_endpoint_hold(tsp->asoc->ep);
> 	sk = ep->base.sk
> 	lock_sock(sk);

Isn't that just the same as doing things in the other order?
	ep = tsp->assoc->ep;
	sctp_endpoint_hold(ep);

But if tsp->assoc->ep is allowed to change, can't it also change to
something invalid?
So I've have thought you should be holding some kind of lock that
stops the data being changed before being 'allowed' to follow the pointers.
In which case the current code is just a missing optimisatoion.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Lee Jones Dec. 17, 2021, 2:35 p.m. UTC | #2
On Fri, 17 Dec 2021, David Laight wrote:

> From: Lee Jones
> > Sent: 17 December 2021 13:46
> > 
> > net/sctp/diag.c for instance is built into its own separate module
> > (sctp_diag.ko) and requires the use of sctp_endpoint_{hold,put}() in
> > order to prevent a recently found use-after-free issue.
> > 
> > In order to prevent data corruption of the pointer used to take a
> > reference on a specific endpoint, between the time of calling
> > sctp_endpoint_hold() and it returning, the API now returns a pointer
> > to the exact endpoint that was incremented.
> > 
> > For example, in sctp_sock_dump(), we could have the following hunk:
> > 
> > 	sctp_endpoint_hold(tsp->asoc->ep);
> > 	ep = tsp->asoc->ep;
> > 	sk = ep->base.sk
> > 	lock_sock(ep->base.sk);
> > 
> > It is possible for this task to be swapped out immediately following
> > the call into sctp_endpoint_hold() that would change the address of
> > tsp->asoc->ep to point to a completely different endpoint.  This means
> > a reference could be taken to the old endpoint and the new one would
> > be processed without a reference taken, moreover the new endpoint
> > could then be freed whilst still processing as a result, causing a
> > use-after-free.
> > 
> > If we return the exact pointer that was held, we ensure this task
> > processes only the endpoint we have taken a reference to.  The
> > resultant hunk now looks like this:
> > 
> > 	ep = sctp_endpoint_hold(tsp->asoc->ep);
> > 	sk = ep->base.sk
> > 	lock_sock(sk);
> 
> Isn't that just the same as doing things in the other order?
> 	ep = tsp->assoc->ep;
> 	sctp_endpoint_hold(ep);

Sleep for a few milliseconds between those lines and see what happens.

'ep' could still be freed between the assignment and the call.

> But if tsp->assoc->ep is allowed to change, can't it also change to
> something invalid?

Not sure I follow.

> So I've have thought you should be holding some kind of lock that
> stops the data being changed before being 'allowed' to follow the pointers.
> In which case the current code is just a missing optimisatoion.

Locking would be another potential solution.

The current code already tries to lock.

	lock_sock(sk);

The difficultly here is that we don't know whether 'sk' is still valid
at this point.  I've seen the current code panic here.  Xin Long
suggested something similar using the RCU infrastructure, but this
code can sleep, so it wasn't suitable.

If we were to use locking, we'd need to figure out a) what to apply
the lock to and b) where to apply the lock.
Jakub Kicinski Dec. 17, 2021, 3:06 p.m. UTC | #3
On Fri, 17 Dec 2021 13:46:06 +0000 Lee Jones wrote:
> For example, in sctp_sock_dump(), we could have the following hunk:
> 
> 	sctp_endpoint_hold(tsp->asoc->ep);
> 	ep = tsp->asoc->ep;
> 	sk = ep->base.sk
> 	lock_sock(ep->base.sk);
> 
> It is possible for this task to be swapped out immediately following
> the call into sctp_endpoint_hold() that would change the address of
> tsp->asoc->ep to point to a completely different endpoint.  This means
> a reference could be taken to the old endpoint and the new one would
> be processed without a reference taken, moreover the new endpoint
> could then be freed whilst still processing as a result, causing a
> use-after-free.
> 
> If we return the exact pointer that was held, we ensure this task
> processes only the endpoint we have taken a reference to.  The
> resultant hunk now looks like this:
> 
>       ep = sctp_endpoint_hold(tsp->asoc->ep);
> 	sk = ep->base.sk
> 	lock_sock(sk);

If you have to explain what the next patch will do to make sense 
of this one it really is better to merge the two patches.
Exporting something is not a functional change, nor does it make
the changes easier to review, in fact the opposite is true.

> Fixes: 8f840e47f190c ("sctp: add the sctp_diag.c file")

This patch in itself fixes exactly nothing.
David Laight Dec. 19, 2021, 2:04 p.m. UTC | #4
From: Lee Jones
> Sent: 17 December 2021 14:35
> 
> On Fri, 17 Dec 2021, David Laight wrote:
> 
> > From: Lee Jones
> > > Sent: 17 December 2021 13:46
> > >
> > > net/sctp/diag.c for instance is built into its own separate module
> > > (sctp_diag.ko) and requires the use of sctp_endpoint_{hold,put}() in
> > > order to prevent a recently found use-after-free issue.
> > >
> > > In order to prevent data corruption of the pointer used to take a
> > > reference on a specific endpoint, between the time of calling
> > > sctp_endpoint_hold() and it returning, the API now returns a pointer
> > > to the exact endpoint that was incremented.
> > >
> > > For example, in sctp_sock_dump(), we could have the following hunk:
> > >
> > > 	sctp_endpoint_hold(tsp->asoc->ep);
> > > 	ep = tsp->asoc->ep;
> > > 	sk = ep->base.sk
> > > 	lock_sock(ep->base.sk);
> > >
> > > It is possible for this task to be swapped out immediately following
> > > the call into sctp_endpoint_hold() that would change the address of
> > > tsp->asoc->ep to point to a completely different endpoint.  This means
> > > a reference could be taken to the old endpoint and the new one would
> > > be processed without a reference taken, moreover the new endpoint
> > > could then be freed whilst still processing as a result, causing a
> > > use-after-free.
> > >
> > > If we return the exact pointer that was held, we ensure this task
> > > processes only the endpoint we have taken a reference to.  The
> > > resultant hunk now looks like this:
> > >
> > > 	ep = sctp_endpoint_hold(tsp->asoc->ep);
> > > 	sk = ep->base.sk
> > > 	lock_sock(sk);
> >
> > Isn't that just the same as doing things in the other order?
> > 	ep = tsp->asoc->ep;
> > 	sctp_endpoint_hold(ep);
> 
> Sleep for a few milliseconds between those lines and see what happens.
> 
> 'ep' could still be freed between the assignment and the call.

It can also be freed half way through setting up the arguments to the call.
So any call:
		xxx(tsp->asoc->ep);
is only really valid if both tsp->asoc and asoc->ep are stable.
So it is exactly the same as doing:
		ep = tsp->asoc->ep;
		xxx(ep);
Returning the value of the argument doesn't help if any of the pointed-to
items can get freed.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
diff mbox series

Patch

diff --git a/include/net/sctp/structs.h b/include/net/sctp/structs.h
index 651bba654d77d..78d71ca56452b 100644
--- a/include/net/sctp/structs.h
+++ b/include/net/sctp/structs.h
@@ -1380,7 +1380,7 @@  static inline struct sctp_endpoint *sctp_ep(struct sctp_ep_common *base)
 struct sctp_endpoint *sctp_endpoint_new(struct sock *, gfp_t);
 void sctp_endpoint_free(struct sctp_endpoint *);
 void sctp_endpoint_put(struct sctp_endpoint *);
-void sctp_endpoint_hold(struct sctp_endpoint *);
+struct sctp_endpoint *sctp_endpoint_hold(struct sctp_endpoint *);
 void sctp_endpoint_add_asoc(struct sctp_endpoint *, struct sctp_association *);
 struct sctp_association *sctp_endpoint_lookup_assoc(
 	const struct sctp_endpoint *ep,
diff --git a/net/sctp/endpointola.c b/net/sctp/endpointola.c
index 48c9c2c7602f7..bdbf74fc7eb4c 100644
--- a/net/sctp/endpointola.c
+++ b/net/sctp/endpointola.c
@@ -222,10 +222,12 @@  static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
 }
 
 /* Hold a reference to an endpoint. */
-void sctp_endpoint_hold(struct sctp_endpoint *ep)
+struct sctp_endpoint *sctp_endpoint_hold(struct sctp_endpoint *ep)
 {
 	refcount_inc(&ep->base.refcnt);
+	return ep;
 }
+EXPORT_SYMBOL_GPL(sctp_endpoint_hold);
 
 /* Release a reference to an endpoint and clean up if there are
  * no more references.
@@ -235,6 +237,7 @@  void sctp_endpoint_put(struct sctp_endpoint *ep)
 	if (refcount_dec_and_test(&ep->base.refcnt))
 		sctp_endpoint_destroy(ep);
 }
+EXPORT_SYMBOL_GPL(sctp_endpoint_put);
 
 /* Is this the endpoint we are looking for?  */
 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,