mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-15 00:34:10 +08:00
d00fa9adc5
The tunnel socket tunnel->sock (struct sock) is accessed when
preparing a new ppp session on a tunnel at pppol2tp_session_init. If
the socket is closed by a thread while another is creating a new
session, the threads race. In pppol2tp_connect, the tunnel object may
be created if the pppol2tp socket is associated with the special
session_id 0 and the tunnel socket is looked up using the provided
fd. When handling this, pppol2tp_connect cannot sock_hold the tunnel
socket to prevent it being destroyed during pppol2tp_connect since
this may itself may race with the socket being destroyed. Doing
sockfd_lookup in pppol2tp_connect isn't sufficient to prevent
tunnel->sock going away either because a given tunnel socket fd may be
reused between calls to pppol2tp_connect. Instead, have
l2tp_tunnel_create sock_hold the tunnel socket before it does
sockfd_put. This ensures that the tunnel's socket is always extant
while the tunnel object exists. Hold a ref on the socket until the
tunnel is destroyed and ensure that all tunnel destroy paths go
through a common function (l2tp_tunnel_delete) since this will do the
final sock_put to release the tunnel socket.
Since the tunnel's socket is now guaranteed to exist if the tunnel
exists, we no longer need to use sockfd_lookup via l2tp_sock_to_tunnel
to derive the tunnel from the socket since this is always
sk_user_data.
Also, sessions no longer sock_hold the tunnel socket since sessions
already hold a tunnel ref and the tunnel sock will not be freed until
the tunnel is freed. Removing these sock_holds in
l2tp_session_register avoids a possible sock leak in the
pppol2tp_connect error path if l2tp_session_register succeeds but
attaching a ppp channel fails. The pppol2tp_connect error path could
have been fixed instead and have the sock ref dropped when the session
is freed, but doing a sock_put of the tunnel socket when the session
is freed would require a new session_free callback. It is simpler to
just remove the sock_hold of the tunnel socket in
l2tp_session_register, now that the tunnel socket lifetime is
guaranteed.
Finally, some init code in l2tp_tunnel_create is reordered to ensure
that the new tunnel object's refcount is set and the tunnel socket ref
is taken before the tunnel socket destructor callbacks are set.
kasan: CONFIG_KASAN_INLINE enabled
kasan: GPF could be caused by NULL-ptr deref or user memory access
general protection fault: 0000 [#1] SMP KASAN
Modules linked in:
CPU: 0 PID: 4360 Comm: syzbot_19c09769 Not tainted 4.16.0-rc2+ #34
Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
RIP: 0010:pppol2tp_session_init+0x1d6/0x500
RSP: 0018:ffff88001377fb40 EFLAGS: 00010212
RAX: dffffc0000000000 RBX: ffff88001636a940 RCX: ffffffff84836c1d
RDX: 0000000000000045 RSI: 0000000055976744 RDI: 0000000000000228
RBP: ffff88001377fb60 R08: ffffffff84836bc8 R09: 0000000000000002
R10: ffff88001377fab8 R11: 0000000000000001 R12: 0000000000000000
R13: ffff88001636aac8 R14: ffff8800160f81c0 R15: 1ffff100026eff76
FS: 00007ffb3ea66700(0000) GS:ffff88001a400000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000020e77000 CR3: 0000000016261000 CR4: 00000000000006f0
Call Trace:
pppol2tp_connect+0xd18/0x13c0
? pppol2tp_session_create+0x170/0x170
? __might_fault+0x115/0x1d0
? lock_downgrade+0x860/0x860
? __might_fault+0xe5/0x1d0
? security_socket_connect+0x8e/0xc0
SYSC_connect+0x1b6/0x310
? SYSC_bind+0x280/0x280
? __do_page_fault+0x5d1/0xca0
? up_read+0x1f/0x40
? __do_page_fault+0x3c8/0xca0
SyS_connect+0x29/0x30
? SyS_accept+0x40/0x40
do_syscall_64+0x1e0/0x730
? trace_hardirqs_off_thunk+0x1a/0x1c
entry_SYSCALL_64_after_hwframe+0x42/0xb7
RIP: 0033:0x7ffb3e376259
RSP: 002b:00007ffeda4f6508 EFLAGS: 00000202 ORIG_RAX: 000000000000002a
RAX: ffffffffffffffda RBX: 0000000020e77012 RCX: 00007ffb3e376259
RDX: 000000000000002e RSI: 0000000020e77000 RDI: 0000000000000004
RBP: 00007ffeda4f6540 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000202 R12: 0000000000400b60
R13: 00007ffeda4f6660 R14: 0000000000000000 R15: 0000000000000000
Code: 80 3d b0 ff 06 02 00 0f 84 07 02 00 00 e8 13 d6 db fc 49 8d bc 24 28 02 00 00 48 b8 00 00 00 00 00 fc ff df 48 89 f
a 48 c1 ea 03 <80> 3c 02 00 0f 85 ed 02 00 00 4d 8b a4 24 28 02 00 00 e8 13 16
Fixes: 80d84ef3ff
("l2tp: prevent l2tp_tunnel_delete racing with userspace close")
Signed-off-by: James Chapman <jchapman@katalix.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
312 lines
9.3 KiB
C
312 lines
9.3 KiB
C
/*
|
|
* L2TP internal definitions.
|
|
*
|
|
* Copyright (c) 2008,2009 Katalix Systems Ltd
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
#include <linux/refcount.h>
|
|
|
|
#ifndef _L2TP_CORE_H_
|
|
#define _L2TP_CORE_H_
|
|
|
|
/* Just some random numbers */
|
|
#define L2TP_TUNNEL_MAGIC 0x42114DDA
|
|
#define L2TP_SESSION_MAGIC 0x0C04EB7D
|
|
|
|
/* Per tunnel, session hash table size */
|
|
#define L2TP_HASH_BITS 4
|
|
#define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS)
|
|
|
|
/* System-wide, session hash table size */
|
|
#define L2TP_HASH_BITS_2 8
|
|
#define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2)
|
|
|
|
struct sk_buff;
|
|
|
|
struct l2tp_stats {
|
|
atomic_long_t tx_packets;
|
|
atomic_long_t tx_bytes;
|
|
atomic_long_t tx_errors;
|
|
atomic_long_t rx_packets;
|
|
atomic_long_t rx_bytes;
|
|
atomic_long_t rx_seq_discards;
|
|
atomic_long_t rx_oos_packets;
|
|
atomic_long_t rx_errors;
|
|
atomic_long_t rx_cookie_discards;
|
|
};
|
|
|
|
struct l2tp_tunnel;
|
|
|
|
/* Describes a session. Contains information to determine incoming
|
|
* packets and transmit outgoing ones.
|
|
*/
|
|
struct l2tp_session_cfg {
|
|
enum l2tp_pwtype pw_type;
|
|
unsigned int data_seq:2; /* data sequencing level
|
|
* 0 => none, 1 => IP only,
|
|
* 2 => all
|
|
*/
|
|
unsigned int recv_seq:1; /* expect receive packets with
|
|
* sequence numbers? */
|
|
unsigned int send_seq:1; /* send packets with sequence
|
|
* numbers? */
|
|
unsigned int lns_mode:1; /* behave as LNS? LAC enables
|
|
* sequence numbers under
|
|
* control of LNS. */
|
|
int debug; /* bitmask of debug message
|
|
* categories */
|
|
u16 vlan_id; /* VLAN pseudowire only */
|
|
u16 l2specific_type; /* Layer 2 specific type */
|
|
u8 cookie[8]; /* optional cookie */
|
|
int cookie_len; /* 0, 4 or 8 bytes */
|
|
u8 peer_cookie[8]; /* peer's cookie */
|
|
int peer_cookie_len; /* 0, 4 or 8 bytes */
|
|
int reorder_timeout; /* configured reorder timeout
|
|
* (in jiffies) */
|
|
int mtu;
|
|
int mru;
|
|
char *ifname;
|
|
};
|
|
|
|
struct l2tp_session {
|
|
int magic; /* should be
|
|
* L2TP_SESSION_MAGIC */
|
|
long dead;
|
|
|
|
struct l2tp_tunnel *tunnel; /* back pointer to tunnel
|
|
* context */
|
|
u32 session_id;
|
|
u32 peer_session_id;
|
|
u8 cookie[8];
|
|
int cookie_len;
|
|
u8 peer_cookie[8];
|
|
int peer_cookie_len;
|
|
u16 l2specific_type;
|
|
u16 hdr_len;
|
|
u32 nr; /* session NR state (receive) */
|
|
u32 ns; /* session NR state (send) */
|
|
struct sk_buff_head reorder_q; /* receive reorder queue */
|
|
u32 nr_max; /* max NR. Depends on tunnel */
|
|
u32 nr_window_size; /* NR window size */
|
|
u32 nr_oos; /* NR of last OOS packet */
|
|
int nr_oos_count; /* For OOS recovery */
|
|
int nr_oos_count_max;
|
|
struct hlist_node hlist; /* Hash list node */
|
|
refcount_t ref_count;
|
|
|
|
char name[32]; /* for logging */
|
|
char ifname[IFNAMSIZ];
|
|
unsigned int data_seq:2; /* data sequencing level
|
|
* 0 => none, 1 => IP only,
|
|
* 2 => all
|
|
*/
|
|
unsigned int recv_seq:1; /* expect receive packets with
|
|
* sequence numbers? */
|
|
unsigned int send_seq:1; /* send packets with sequence
|
|
* numbers? */
|
|
unsigned int lns_mode:1; /* behave as LNS? LAC enables
|
|
* sequence numbers under
|
|
* control of LNS. */
|
|
int debug; /* bitmask of debug message
|
|
* categories */
|
|
int reorder_timeout; /* configured reorder timeout
|
|
* (in jiffies) */
|
|
int reorder_skip; /* set if skip to next nr */
|
|
int mtu;
|
|
int mru;
|
|
enum l2tp_pwtype pwtype;
|
|
struct l2tp_stats stats;
|
|
struct hlist_node global_hlist; /* Global hash list node */
|
|
|
|
int (*build_header)(struct l2tp_session *session, void *buf);
|
|
void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
|
|
void (*session_close)(struct l2tp_session *session);
|
|
#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
|
|
void (*show)(struct seq_file *m, void *priv);
|
|
#endif
|
|
uint8_t priv[0]; /* private data */
|
|
};
|
|
|
|
/* Describes the tunnel. It contains info to track all the associated
|
|
* sessions so incoming packets can be sorted out
|
|
*/
|
|
struct l2tp_tunnel_cfg {
|
|
int debug; /* bitmask of debug message
|
|
* categories */
|
|
enum l2tp_encap_type encap;
|
|
|
|
/* Used only for kernel-created sockets */
|
|
struct in_addr local_ip;
|
|
struct in_addr peer_ip;
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
struct in6_addr *local_ip6;
|
|
struct in6_addr *peer_ip6;
|
|
#endif
|
|
u16 local_udp_port;
|
|
u16 peer_udp_port;
|
|
unsigned int use_udp_checksums:1,
|
|
udp6_zero_tx_checksums:1,
|
|
udp6_zero_rx_checksums:1;
|
|
};
|
|
|
|
struct l2tp_tunnel {
|
|
int magic; /* Should be L2TP_TUNNEL_MAGIC */
|
|
|
|
unsigned long dead;
|
|
|
|
struct rcu_head rcu;
|
|
rwlock_t hlist_lock; /* protect session_hlist */
|
|
bool acpt_newsess; /* Indicates whether this
|
|
* tunnel accepts new sessions.
|
|
* Protected by hlist_lock.
|
|
*/
|
|
struct hlist_head session_hlist[L2TP_HASH_SIZE];
|
|
/* hashed list of sessions,
|
|
* hashed by id */
|
|
u32 tunnel_id;
|
|
u32 peer_tunnel_id;
|
|
int version; /* 2=>L2TPv2, 3=>L2TPv3 */
|
|
|
|
char name[20]; /* for logging */
|
|
int debug; /* bitmask of debug message
|
|
* categories */
|
|
enum l2tp_encap_type encap;
|
|
struct l2tp_stats stats;
|
|
|
|
struct list_head list; /* Keep a list of all tunnels */
|
|
struct net *l2tp_net; /* the net we belong to */
|
|
|
|
refcount_t ref_count;
|
|
#ifdef CONFIG_DEBUG_FS
|
|
void (*show)(struct seq_file *m, void *arg);
|
|
#endif
|
|
int (*recv_payload_hook)(struct sk_buff *skb);
|
|
void (*old_sk_destruct)(struct sock *);
|
|
struct sock *sock; /* Parent socket */
|
|
int fd; /* Parent fd, if tunnel socket
|
|
* was created by userspace */
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
bool v4mapped;
|
|
#endif
|
|
|
|
struct work_struct del_work;
|
|
|
|
uint8_t priv[0]; /* private data */
|
|
};
|
|
|
|
struct l2tp_nl_cmd_ops {
|
|
int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
|
|
u32 session_id, u32 peer_session_id,
|
|
struct l2tp_session_cfg *cfg);
|
|
int (*session_delete)(struct l2tp_session *session);
|
|
};
|
|
|
|
static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
|
|
{
|
|
return &tunnel->priv[0];
|
|
}
|
|
|
|
static inline void *l2tp_session_priv(struct l2tp_session *session)
|
|
{
|
|
return &session->priv[0];
|
|
}
|
|
|
|
struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
|
|
void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
|
|
|
|
struct l2tp_session *l2tp_session_get(const struct net *net,
|
|
struct l2tp_tunnel *tunnel,
|
|
u32 session_id);
|
|
struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
|
|
struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
|
|
const char *ifname);
|
|
struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id);
|
|
struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth);
|
|
|
|
int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
|
|
u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
|
|
struct l2tp_tunnel **tunnelp);
|
|
void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
|
|
void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
|
|
struct l2tp_session *l2tp_session_create(int priv_size,
|
|
struct l2tp_tunnel *tunnel,
|
|
u32 session_id, u32 peer_session_id,
|
|
struct l2tp_session_cfg *cfg);
|
|
int l2tp_session_register(struct l2tp_session *session,
|
|
struct l2tp_tunnel *tunnel);
|
|
|
|
void __l2tp_session_unhash(struct l2tp_session *session);
|
|
int l2tp_session_delete(struct l2tp_session *session);
|
|
void l2tp_session_free(struct l2tp_session *session);
|
|
void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
|
|
unsigned char *ptr, unsigned char *optr, u16 hdrflags,
|
|
int length, int (*payload_hook)(struct sk_buff *skb));
|
|
int l2tp_session_queue_purge(struct l2tp_session *session);
|
|
int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
|
|
void l2tp_session_set_header_len(struct l2tp_session *session, int version);
|
|
|
|
int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
|
|
int hdr_len);
|
|
|
|
int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
|
|
const struct l2tp_nl_cmd_ops *ops);
|
|
void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
|
|
int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
|
|
|
|
static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
|
|
{
|
|
refcount_inc(&tunnel->ref_count);
|
|
}
|
|
|
|
static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
|
|
{
|
|
if (refcount_dec_and_test(&tunnel->ref_count))
|
|
l2tp_tunnel_free(tunnel);
|
|
}
|
|
|
|
/* Session reference counts. Incremented when code obtains a reference
|
|
* to a session.
|
|
*/
|
|
static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
|
|
{
|
|
refcount_inc(&session->ref_count);
|
|
}
|
|
|
|
static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
|
|
{
|
|
if (refcount_dec_and_test(&session->ref_count))
|
|
l2tp_session_free(session);
|
|
}
|
|
|
|
static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
|
|
{
|
|
switch (session->l2specific_type) {
|
|
case L2TP_L2SPECTYPE_DEFAULT:
|
|
return 4;
|
|
case L2TP_L2SPECTYPE_NONE:
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
#define l2tp_printk(ptr, type, func, fmt, ...) \
|
|
do { \
|
|
if (((ptr)->debug) & (type)) \
|
|
func(fmt, ##__VA_ARGS__); \
|
|
} while (0)
|
|
|
|
#define l2tp_warn(ptr, type, fmt, ...) \
|
|
l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
|
|
#define l2tp_info(ptr, type, fmt, ...) \
|
|
l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
|
|
#define l2tp_dbg(ptr, type, fmt, ...) \
|
|
l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
|
|
|
|
#define MODULE_ALIAS_L2TP_PWTYPE(type) \
|
|
MODULE_ALIAS("net-l2tp-type-" __stringify(type))
|
|
|
|
#endif /* _L2TP_CORE_H_ */
|