Skip to content

Commit b68d3ab

Browse files
davem330gregkh
authored andcommitted
netlink: Always copy on mmap TX.
[ Upstream commit 4682a03 ] Checking the file f_count and the nlk->mapped count is not completely sufficient to prevent the mmap'd area contents from changing from under us during netlink mmap sendmsg() operations. Be careful to sample the header's length field only once, because this could change from under us as well. Fixes: 5fd9612 ("netlink: implement memory mapped sendmsg()") Signed-off-by: David S. Miller <davem@davemloft.net> Acked-by: Daniel Borkmann <dborkman@redhat.com> Acked-by: Thomas Graf <tgraf@suug.ch> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 15c1160 commit b68d3ab

1 file changed

Lines changed: 16 additions & 36 deletions

File tree

‎net/netlink/af_netlink.c‎

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,14 @@ static int netlink_mmap(struct file *file, struct socket *sock,
526526
return err;
527527
}
528528

529-
static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
529+
static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr, unsigned int nm_len)
530530
{
531531
#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
532532
struct page *p_start, *p_end;
533533

534534
/* First page is flushed through netlink_{get,set}_status */
535535
p_start = pgvec_to_page(hdr + PAGE_SIZE);
536-
p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1);
536+
p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + nm_len - 1);
537537
while (p_start <= p_end) {
538538
flush_dcache_page(p_start);
539539
p_start++;
@@ -715,60 +715,40 @@ static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
715715
struct nl_mmap_hdr *hdr;
716716
struct sk_buff *skb;
717717
unsigned int maxlen;
718-
bool excl = true;
719718
int err = 0, len = 0;
720719

721-
/* Netlink messages are validated by the receiver before processing.
722-
* In order to avoid userspace changing the contents of the message
723-
* after validation, the socket and the ring may only be used by a
724-
* single process, otherwise we fall back to copying.
725-
*/
726-
if (atomic_long_read(&sk->sk_socket->file->f_count) > 1 ||
727-
atomic_read(&nlk->mapped) > 1)
728-
excl = false;
729-
730720
mutex_lock(&nlk->pg_vec_lock);
731721

732722
ring = &nlk->tx_ring;
733723
maxlen = ring->frame_size - NL_MMAP_HDRLEN;
734724

735725
do {
726+
unsigned int nm_len;
727+
736728
hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
737729
if (hdr == NULL) {
738730
if (!(msg->msg_flags & MSG_DONTWAIT) &&
739731
atomic_read(&nlk->tx_ring.pending))
740732
schedule();
741733
continue;
742734
}
743-
if (hdr->nm_len > maxlen) {
735+
736+
nm_len = ACCESS_ONCE(hdr->nm_len);
737+
if (nm_len > maxlen) {
744738
err = -EINVAL;
745739
goto out;
746740
}
747741

748-
netlink_frame_flush_dcache(hdr);
742+
netlink_frame_flush_dcache(hdr, nm_len);
749743

750-
if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
751-
skb = alloc_skb_head(GFP_KERNEL);
752-
if (skb == NULL) {
753-
err = -ENOBUFS;
754-
goto out;
755-
}
756-
sock_hold(sk);
757-
netlink_ring_setup_skb(skb, sk, ring, hdr);
758-
NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
759-
__skb_put(skb, hdr->nm_len);
760-
netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
761-
atomic_inc(&ring->pending);
762-
} else {
763-
skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
764-
if (skb == NULL) {
765-
err = -ENOBUFS;
766-
goto out;
767-
}
768-
__skb_put(skb, hdr->nm_len);
769-
memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
770-
netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
744+
skb = alloc_skb(nm_len, GFP_KERNEL);
745+
if (skb == NULL) {
746+
err = -ENOBUFS;
747+
goto out;
771748
}
749+
__skb_put(skb, nm_len);
750+
memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, nm_len);
751+
netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
772752

773753
netlink_increment_head(ring);
774754

@@ -814,7 +794,7 @@ static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
814794
hdr->nm_pid = NETLINK_CB(skb).creds.pid;
815795
hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
816796
hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
817-
netlink_frame_flush_dcache(hdr);
797+
netlink_frame_flush_dcache(hdr, hdr->nm_len);
818798
netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
819799

820800
NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;

0 commit comments

Comments
 (0)