Skip to content

Commit

Permalink
net: Fix use after free by removing length arg from sk_data_ready cal…
Browse files Browse the repository at this point in the history
…lbacks.

Several spots in the kernel perform a sequence like:

	skb_queue_tail(&sk->s_receive_queue, skb);
	sk->sk_data_ready(sk, skb->len);

But at the moment we place the SKB onto the socket receive queue it
can be consumed and freed up.  So this skb->len access is potentially
to freed up memory.

Furthermore, the skb->len can be modified by the consumer so it is
possible that the value isn't accurate.

And finally, no actual implementation of this callback actually uses
the length argument.  And since nobody actually cared about it's
value, lots of call sites pass arbitrary values in such as '0' and
even '1'.

So just remove the length argument from the callback, that way there
is no confusion whatsoever and all of these use-after-free cases get
fixed as a side effect.

Based upon a patch by Eric Dumazet and his suggestion to audit this
issue tree-wide.

Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
davem330 committed Apr 11, 2014
1 parent ad20d5f commit 676d236
Show file tree
Hide file tree
Showing 58 changed files with 112 additions and 121 deletions.
2 changes: 1 addition & 1 deletion drivers/scsi/iscsi_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static inline int iscsi_sw_sk_state_check(struct sock *sk)
return 0;
}

static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
static void iscsi_sw_tcp_data_ready(struct sock *sk)
{
struct iscsi_conn *conn;
struct iscsi_tcp_conn *tcp_conn;
Expand Down
2 changes: 1 addition & 1 deletion drivers/scsi/iscsi_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct iscsi_sw_tcp_conn {

struct iscsi_sw_tcp_send out;
/* old values for socket callbacks */
void (*old_data_ready)(struct sock *, int);
void (*old_data_ready)(struct sock *);
void (*old_state_change)(struct sock *);
void (*old_write_space)(struct sock *);

Expand Down
4 changes: 2 additions & 2 deletions drivers/staging/lustre/lnet/klnds/socklnd/socklnd_lib-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ extern void ksocknal_write_callback (ksock_conn_t *conn);
* socket call back in Linux
*/
static void
ksocknal_data_ready (struct sock *sk, int n)
ksocknal_data_ready (struct sock *sk)
{
ksock_conn_t *conn;

Expand All @@ -666,7 +666,7 @@ ksocknal_data_ready (struct sock *sk, int n)
conn = sk->sk_user_data;
if (conn == NULL) { /* raced with ksocknal_terminate_conn */
LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
sk->sk_data_ready (sk, n);
sk->sk_data_ready (sk);
} else
ksocknal_read_callback(conn);

Expand Down
2 changes: 1 addition & 1 deletion drivers/target/iscsi/iscsi_target_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ struct iscsi_conn {
struct completion rx_half_close_comp;
/* socket used by this connection */
struct socket *sock;
void (*orig_data_ready)(struct sock *, int);
void (*orig_data_ready)(struct sock *);
void (*orig_state_change)(struct sock *);
#define LOGIN_FLAGS_READ_ACTIVE 1
#define LOGIN_FLAGS_CLOSED 2
Expand Down
2 changes: 1 addition & 1 deletion drivers/target/iscsi/iscsi_target_nego.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_log
return 0;
}

static void iscsi_target_sk_data_ready(struct sock *sk, int count)
static void iscsi_target_sk_data_ready(struct sock *sk)
{
struct iscsi_conn *conn = sk->sk_user_data;
bool rc;
Expand Down
2 changes: 1 addition & 1 deletion fs/dlm/lowcomms.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
}

/* Data available on socket or listen socket received a connect */
static void lowcomms_data_ready(struct sock *sk, int count_unused)
static void lowcomms_data_ready(struct sock *sk)
{
struct connection *con = sock2con(sk);
if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Expand Down
4 changes: 2 additions & 2 deletions fs/ncpfs/ncp_fs_sb.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct ncp_server {

spinlock_t requests_lock; /* Lock accesses to tx.requests, tx.creq and rcv.creq when STREAM mode */

void (*data_ready)(struct sock* sk, int len);
void (*data_ready)(struct sock* sk);
void (*error_report)(struct sock* sk);
void (*write_space)(struct sock* sk); /* STREAM mode only */
struct {
Expand Down Expand Up @@ -153,7 +153,7 @@ extern void ncp_tcp_tx_proc(struct work_struct *work);
extern void ncpdgram_rcv_proc(struct work_struct *work);
extern void ncpdgram_timeout_proc(struct work_struct *work);
extern void ncpdgram_timeout_call(unsigned long server);
extern void ncp_tcp_data_ready(struct sock* sk, int len);
extern void ncp_tcp_data_ready(struct sock* sk);
extern void ncp_tcp_write_space(struct sock* sk);
extern void ncp_tcp_error_report(struct sock* sk);

Expand Down
4 changes: 2 additions & 2 deletions fs/ncpfs/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ static void ncp_req_put(struct ncp_request_reply *req)
kfree(req);
}

void ncp_tcp_data_ready(struct sock *sk, int len)
void ncp_tcp_data_ready(struct sock *sk)
{
struct ncp_server *server = sk->sk_user_data;

server->data_ready(sk, len);
server->data_ready(sk);
schedule_work(&server->rcv.tq);
}

Expand Down
15 changes: 7 additions & 8 deletions fs/ocfs2/cluster/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static int o2net_sys_err_translations[O2NET_ERR_MAX] =
static void o2net_sc_connect_completed(struct work_struct *work);
static void o2net_rx_until_empty(struct work_struct *work);
static void o2net_shutdown_sc(struct work_struct *work);
static void o2net_listen_data_ready(struct sock *sk, int bytes);
static void o2net_listen_data_ready(struct sock *sk);
static void o2net_sc_send_keep_req(struct work_struct *work);
static void o2net_idle_timer(unsigned long data);
static void o2net_sc_postpone_idle(struct o2net_sock_container *sc);
Expand Down Expand Up @@ -597,9 +597,9 @@ static void o2net_set_nn_state(struct o2net_node *nn,
}

/* see o2net_register_callbacks() */
static void o2net_data_ready(struct sock *sk, int bytes)
static void o2net_data_ready(struct sock *sk)
{
void (*ready)(struct sock *sk, int bytes);
void (*ready)(struct sock *sk);

read_lock(&sk->sk_callback_lock);
if (sk->sk_user_data) {
Expand All @@ -613,7 +613,7 @@ static void o2net_data_ready(struct sock *sk, int bytes)
}
read_unlock(&sk->sk_callback_lock);

ready(sk, bytes);
ready(sk);
}

/* see o2net_register_callbacks() */
Expand Down Expand Up @@ -1953,9 +1953,9 @@ static void o2net_accept_many(struct work_struct *work)
cond_resched();
}

static void o2net_listen_data_ready(struct sock *sk, int bytes)
static void o2net_listen_data_ready(struct sock *sk)
{
void (*ready)(struct sock *sk, int bytes);
void (*ready)(struct sock *sk);

read_lock(&sk->sk_callback_lock);
ready = sk->sk_user_data;
Expand All @@ -1978,7 +1978,6 @@ static void o2net_listen_data_ready(struct sock *sk, int bytes)
*/

if (sk->sk_state == TCP_LISTEN) {
mlog(ML_TCP, "bytes: %d\n", bytes);
queue_work(o2net_wq, &o2net_listen_work);
} else {
ready = NULL;
Expand All @@ -1987,7 +1986,7 @@ static void o2net_listen_data_ready(struct sock *sk, int bytes)
out:
read_unlock(&sk->sk_callback_lock);
if (ready != NULL)
ready(sk, bytes);
ready(sk);
}

static int o2net_open_listening_sock(__be32 addr, __be16 port)
Expand Down
2 changes: 1 addition & 1 deletion fs/ocfs2/cluster/tcp_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ struct o2net_sock_container {

/* original handlers for the sockets */
void (*sc_state_change)(struct sock *sk);
void (*sc_data_ready)(struct sock *sk, int bytes);
void (*sc_data_ready)(struct sock *sk);

u32 sc_msg_key;
u16 sc_msg_type;
Expand Down
2 changes: 1 addition & 1 deletion include/linux/sunrpc/svcsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct svc_sock {

/* We keep the old state_change and data_ready CB's here */
void (*sk_ostate)(struct sock *);
void (*sk_odata)(struct sock *, int bytes);
void (*sk_odata)(struct sock *);
void (*sk_owspace)(struct sock *);

/* private TCP part */
Expand Down
2 changes: 1 addition & 1 deletion include/net/sctp/sctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void sctp_addr_wq_mgmt(struct net *, struct sctp_sockaddr_entry *, int);
int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb);
int sctp_inet_listen(struct socket *sock, int backlog);
void sctp_write_space(struct sock *sk);
void sctp_data_ready(struct sock *sk, int len);
void sctp_data_ready(struct sock *sk);
unsigned int sctp_poll(struct file *file, struct socket *sock,
poll_table *wait);
void sctp_sock_rfree(struct sk_buff *skb);
Expand Down
2 changes: 1 addition & 1 deletion include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ struct sock {
u32 sk_classid;
struct cg_proto *sk_cgrp;
void (*sk_state_change)(struct sock *sk);
void (*sk_data_ready)(struct sock *sk, int bytes);
void (*sk_data_ready)(struct sock *sk);
void (*sk_write_space)(struct sock *sk);
void (*sk_error_report)(struct sock *sk);
int (*sk_backlog_rcv)(struct sock *sk,
Expand Down
2 changes: 1 addition & 1 deletion net/atm/clip.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static int to_atmarpd(enum atmarp_ctrl_type type, int itf, __be32 ip)

sk = sk_atm(atmarpd);
skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);
return 0;
}

Expand Down
10 changes: 5 additions & 5 deletions net/atm/lec.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static void lec_handle_bridge(struct sk_buff *skb, struct net_device *dev)
atm_force_charge(priv->lecd, skb2->truesize);
sk = sk_atm(priv->lecd);
skb_queue_tail(&sk->sk_receive_queue, skb2);
sk->sk_data_ready(sk, skb2->len);
sk->sk_data_ready(sk);
}
}
#endif /* defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE) */
Expand Down Expand Up @@ -447,7 +447,7 @@ static int lec_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
atm_force_charge(priv->lecd, skb2->truesize);
sk = sk_atm(priv->lecd);
skb_queue_tail(&sk->sk_receive_queue, skb2);
sk->sk_data_ready(sk, skb2->len);
sk->sk_data_ready(sk);
}
}
#endif /* defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE) */
Expand Down Expand Up @@ -530,13 +530,13 @@ send_to_lecd(struct lec_priv *priv, atmlec_msg_type type,
atm_force_charge(priv->lecd, skb->truesize);
sk = sk_atm(priv->lecd);
skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);

if (data != NULL) {
pr_debug("about to send %d bytes of data\n", data->len);
atm_force_charge(priv->lecd, data->truesize);
skb_queue_tail(&sk->sk_receive_queue, data);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);
}

return 0;
Expand Down Expand Up @@ -616,7 +616,7 @@ static void lec_push(struct atm_vcc *vcc, struct sk_buff *skb)

pr_debug("%s: To daemon\n", dev->name);
skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);
} else { /* Data frame, queue to protocol handlers */
struct lec_arp_table *entry;
unsigned char *src, *dst;
Expand Down
6 changes: 3 additions & 3 deletions net/atm/mpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
dprintk("(%s) control packet arrived\n", dev->name);
/* Pass control packets to daemon */
skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);
return;
}

Expand Down Expand Up @@ -992,7 +992,7 @@ int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)

sk = sk_atm(mpc->mpoad_vcc);
skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);

return 0;
}
Expand Down Expand Up @@ -1273,7 +1273,7 @@ static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)

sk = sk_atm(vcc);
skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);
dprintk("exiting\n");
}

Expand Down
2 changes: 1 addition & 1 deletion net/atm/raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void atm_push_raw(struct atm_vcc *vcc, struct sk_buff *skb)
struct sock *sk = sk_atm(vcc);

skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);
}
}

Expand Down
2 changes: 1 addition & 1 deletion net/atm/signaling.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void sigd_put_skb(struct sk_buff *skb)
#endif
atm_force_charge(sigd, skb->truesize);
skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb);
sk_atm(sigd)->sk_data_ready(sk_atm(sigd), skb->len);
sk_atm(sigd)->sk_data_ready(sk_atm(sigd));
}

static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg)
Expand Down
2 changes: 1 addition & 1 deletion net/ax25/ax25_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,

if (sk) {
if (!sock_flag(sk, SOCK_DEAD))
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);
sock_put(sk);
} else {
free:
Expand Down
6 changes: 3 additions & 3 deletions net/bluetooth/l2cap_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)

if (parent) {
bt_accept_unlink(sk);
parent->sk_data_ready(parent, 0);
parent->sk_data_ready(parent);
} else {
sk->sk_state_change(sk);
}
Expand Down Expand Up @@ -1327,7 +1327,7 @@ static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
sk->sk_state_change(sk);

if (parent)
parent->sk_data_ready(parent, 0);
parent->sk_data_ready(parent);

release_sock(sk);
}
Expand All @@ -1340,7 +1340,7 @@ static void l2cap_sock_defer_cb(struct l2cap_chan *chan)

parent = bt_sk(sk)->parent;
if (parent)
parent->sk_data_ready(parent, 0);
parent->sk_data_ready(parent);

release_sock(sk);
}
Expand Down
4 changes: 2 additions & 2 deletions net/bluetooth/rfcomm/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ static void rfcomm_l2state_change(struct sock *sk)
rfcomm_schedule();
}

static void rfcomm_l2data_ready(struct sock *sk, int bytes)
static void rfcomm_l2data_ready(struct sock *sk)
{
BT_DBG("%p bytes %d", sk, bytes);
BT_DBG("%p", sk);
rfcomm_schedule();
}

Expand Down
4 changes: 2 additions & 2 deletions net/bluetooth/rfcomm/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)

atomic_add(skb->len, &sk->sk_rmem_alloc);
skb_queue_tail(&sk->sk_receive_queue, skb);
sk->sk_data_ready(sk, skb->len);
sk->sk_data_ready(sk);

if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
rfcomm_dlc_throttle(d);
Expand Down Expand Up @@ -84,7 +84,7 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
sock_set_flag(sk, SOCK_ZAPPED);
bt_accept_unlink(sk);
}
parent->sk_data_ready(parent, 0);
parent->sk_data_ready(parent);
} else {
if (d->state == BT_CONNECTED)
rfcomm_session_getaddr(d->session,
Expand Down
2 changes: 1 addition & 1 deletion net/bluetooth/sco.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ static void sco_conn_ready(struct sco_conn *conn)
sk->sk_state = BT_CONNECTED;

/* Wake up parent */
parent->sk_data_ready(parent, 1);
parent->sk_data_ready(parent);

bh_unlock_sock(parent);

Expand Down
4 changes: 1 addition & 3 deletions net/caif/caif_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ static void caif_flow_ctrl(struct sock *sk, int mode)
static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
{
int err;
int skb_len;
unsigned long flags;
struct sk_buff_head *list = &sk->sk_receive_queue;
struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Expand Down Expand Up @@ -153,14 +152,13 @@ static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
* may be freed by other threads of control pulling packets
* from the queue.
*/
skb_len = skb->len;
spin_lock_irqsave(&list->lock, flags);
if (!sock_flag(sk, SOCK_DEAD))
__skb_queue_tail(list, skb);
spin_unlock_irqrestore(&list->lock, flags);

if (!sock_flag(sk, SOCK_DEAD))
sk->sk_data_ready(sk, skb_len);
sk->sk_data_ready(sk);
else
kfree_skb(skb);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion net/ceph/messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ static void con_sock_state_closed(struct ceph_connection *con)
*/

/* data available on socket, or listen socket received a connect */
static void ceph_sock_data_ready(struct sock *sk, int count_unused)
static void ceph_sock_data_ready(struct sock *sk)
{
struct ceph_connection *con = sk->sk_user_data;
if (atomic_read(&con->msgr->stopping)) {
Expand Down
Loading

0 comments on commit 676d236

Please sign in to comment.