History

packet: fix race condition in packet_set_ring

When packet_set_ring creates a ring buffer it will initialize a struct timer_list if the packet version is TPACKET_V3. This value can then be raced by a different thread calling setsockopt to set the version to TPACKET_V1 before packet_set_ring has finished. This leads to a use-after-free on a function pointer in the struct timer_list when the socket is closed as the previously initialized timer will not be deleted. The bug is fixed by taking lock_sock(sk) in packet_setsockopt when changing the packet version while also taking the lock at the start of packet_set_ring.

Philip Pettersson committed 9 years ago

Commit 84ac7261 parent 2219d5e1 file · +12 −6Browse files

Unified · Split

net/packet/af_packet.c+12−6
@@ -3648,19 +3648,25 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv · 3647 unmodified lines
36483648
36493649 if (optlen != sizeof(val))
36503650 return -EINVAL;
3651− if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3652− return -EBUSY;
36533651 if (copy_from_user(&val, optval, sizeof(val)))
36543652 return -EFAULT;
36553653 switch (val) {
36563654 case TPACKET_V1:
36573655 case TPACKET_V2:
36583656 case TPACKET_V3:
3659− po->tp_version = val;
3660− return 0;
3657+ break;
36613658 default:
36623659 return -EINVAL;
36633660 }
3661+ lock_sock(sk);
3662+ if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3663+ ret = -EBUSY;
3664+ } else {
3665+ po->tp_version = val;
3666+ ret = 0;
3667+ }
3668+ release_sock(sk);
3669+ return ret;
36643670 }
36653671 case PACKET_RESERVE:
36663672 {
@@ -4164,6 +4170,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, · 497 unmodified lines
41644170 /* Added to avoid minimal code churn */
41654171 struct tpacket_req *req = &req_u->req;
41664172
4173+ lock_sock(sk);
41674174 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
41684175 if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
41694176 net_warn_ratelimited("Tx-ring is not supported.\n");
@@ -4245,7 +4252,6 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, · 75 unmodified lines
42454252 goto out;
42464253 }
42474254
4248− lock_sock(sk);
42494255
42504256 /* Detach socket from network */
42514257 spin_lock(&po->bind_lock);
@@ -4294,11 +4300,11 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, · 42 unmodified lines
42944300 if (!tx_ring)
42954301 prb_shutdown_retire_blk_timer(po, rb_queue);
42964302 }
4297− release_sock(sk);
42984303
42994304 if (pg_vec)
43004305 free_pg_vec(pg_vec, order, req->tp_block_nr);
43014306 out:
4307+ release_sock(sk);
43024308 return err;
43034309 }
43044310