l2tp: pppol2tp_connect() fixes
Subsystems (3)
cover l2tp: pppol2tp_connect() fixes
This series fixes a few remaining issues with pppol2tp_connect().
It doesn't try to prevent invalid configurations that have no effect on kernel's reliability. That would be work for a future patch set.
Patch 2 is the most important as it avoids an invalid pointer dereference crashing the kernel. It depends on patch 1 for correctly identifying L2TP session types.
Patches 3 and 4 avoid creating stale tunnels and sessions.
Guillaume Nault (4):
l2tp: fix pseudo-wire type for sessions created by pppol2tp_connect()
l2tp: only accept PPP sessions in pppol2tp_connect()
l2tp: prevent pppol2tp_connect() from creating kernel sockets
l2tp: clean up stale tunnel or session in pppol2tp_connect's error
path
net/l2tp/l2tp_ppp.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
Signature · 1 line
2.17.1
PATCH 1/4 l2tp: fix pseudo-wire type for sessions created by pppol2tp_connect()
Define cfg.pw_type so that the new session is created with its .pwtype field properly set (L2TP_PWTYPE_PPP).
Not setting the pseudo-wire type had several annoying effects:
* Invalid value returned in the L2TP_ATTR_PW_TYPE attribute when dumping sessions with the netlink API.
* Impossibility to delete the session using the netlink API (because l2tp_nl_cmd_session_delete() gets the deletion callback function from an array indexed by the session's pseudo-wire type).
Also, there are several cases where we should check a session's pseudo-wire type. For example, pppol2tp_connect() should refuse to connect a session that is not PPPoL2TP, but that requires the session's .pwtype field to be properly set.
--- net/l2tp/l2tp_ppp.c | 1 + 1 file changed, 1 insertion(+)
net/l2tp/l2tp_ppp.c +1 -0
@@ -751,6 +751,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,/* Default MTU must allow space for UDP/L2TP/PPP headers */ cfg.mtu =1500 -PPPOL2TP_HEADER_OVERHEAD ; cfg.mru = cfg.mtu; + cfg.pw_type =L2TP_PWTYPE_PPP ; session = l2tp_session_create(sizeof(struct pppol2tp_session), tunnel, session_id,
Signature · 1 line
2.17.1
PATCH 2/4 l2tp: only accept PPP sessions in pppol2tp_connect()
In reply to Guillaume Naultl2tp_session_priv() returns a struct pppol2tp_session pointer only for PPPoL2TP sessions. In particular, if the session is an L2TP_PWTYPE_ETH pseudo-wire, l2tp_session_priv() returns a pointer to an l2tp_eth_sess structure, which is much smaller than struct pppol2tp_session. This leads to invalid memory dereference when trying to lock ps->sk_lock.
--- net/l2tp/l2tp_ppp.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/l2tp/l2tp_ppp.c +6 -0
@@ -734,6 +734,12 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, session =l2tp_session_get (sock_net (sk), tunnel, session_id);if (session) { drop_refcnt = true; + +if (session->pwtype !=L2TP_PWTYPE_PPP ) { + error = -EPROTOTYPE ; + goto end; + } + ps =l2tp_session_priv (session);/* Using a pre-existing session is fine as long as it hasn't
Signature · 1 line
2.17.1
PATCH 3/4 l2tp: prevent pppol2tp_connect() from creating kernel sockets
In reply to Guillaume NaultIf 'fd' is negative, l2tp_tunnel_create() creates a tunnel socket using the configuration passed in 'tcfg'. Currently, pppol2tp_connect() sets the relevant fields to zero, tricking l2tp_tunnel_create() into setting up an unusable kernel socket.
We can't set 'tcfg' with the required fields because there's no way to get them from the current connect() parameters. So let's restrict kernel sockets creation to the netlink API, which is the original use case.
--- net/l2tp/l2tp_ppp.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/l2tp/l2tp_ppp.c +9 -0
@@ -701,6 +701,15 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, .encap = L2TP_ENCAPTYPE_UDP, .debug = 0, }; + +/* Prevent l2tp_tunnel_register() from trying to set up +* a kernel socket. +*/ +if (fd <0 ) { + error = -EBADF ; + goto end; + } + error =l2tp_tunnel_create (sock_net (sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);if (error <0 ) goto end;
Signature · 1 line
2.17.1
PATCH 4/4 l2tp: clean up stale tunnel or session in pppol2tp_connect's error path
In reply to Guillaume Naultpppol2tp_connect() may create a tunnel or a session. Remove them in case of error.
--- net/l2tp/l2tp_ppp.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
net/l2tp/l2tp_ppp.c +10 -0
@@ -612,6 +612,8 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,u32 session_id, peer_session_id;bool drop_refcnt = false;bool drop_tunnel = false; +bool new_session = false; +bool new_tunnel = false;int ver =2 ;int fd;
@@ -722,6 +724,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, goto end; } drop_tunnel = true; + new_tunnel = true; } }else {/* Error if we can't find the tunnel */
@@ -788,6 +791,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, goto end; } drop_refcnt = true; + new_session = true; }/* Special case: if source & dest session_id == 0x0000, this
@@ -834,6 +838,12 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, session->name); end: +if (error) { +if (new_session) +l2tp_session_delete (session); +if (new_tunnel) +l2tp_tunnel_delete (tunnel); + }if (drop_refcnt)l2tp_session_dec_refcount (session);if (drop_tunnel)
Signature · 1 line
2.17.1
David Miller
maintainerFrom: Guillaume Nault <[email protected]>
Date: Wed, 13 Jun 2018 15:09:16 +0200
Quoted 10 lines
This series fixes a few remaining issues with
pppol2tp_connect().It doesn't try to prevent invalid configurations that have no effect on kernel's reliability. That would be work for a future patch set.
Patch 2 is the most important as it avoids an invalid pointer dereference crashing the kernel. It depends on patch 1 for correctly identifying L2TP session types.
Patches 3 and 4 avoid creating stale tunnels and sessions.
Series applied, thank you.