libbpf: fix invalid munmap call

When unmapping the AF_XDP memory regions used for the rings, an
invalid address was passed to the munmap() calls. Instead of passing
the beginning of the memory region, the descriptor region was passed
to munmap.

When the userspace application tried to tear down an AF_XDP socket,
the operation failed and the application would still have a reference
to socket it wished to get rid of.

Reported-by: William Tu <u9012063@gmail.com>
Fixes: 1cad078842 ("libbpf: add support for using AF_XDP sockets")
Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
Tested-by: William Tu <u9012063@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Björn Töpel 2019-04-30 14:45:35 +02:00 committed by Alexei Starovoitov
parent 6cea33701e
commit 0e6741f092
1 changed files with 40 additions and 37 deletions

View File

@ -248,8 +248,7 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size,
return 0; return 0;
out_mmap: out_mmap:
munmap(umem->fill, munmap(map, off.fr.desc + umem->config.fill_size * sizeof(__u64));
off.fr.desc + umem->config.fill_size * sizeof(__u64));
out_socket: out_socket:
close(umem->fd); close(umem->fd);
out_umem_alloc: out_umem_alloc:
@ -524,11 +523,11 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
struct xsk_ring_cons *rx, struct xsk_ring_prod *tx, struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
const struct xsk_socket_config *usr_config) const struct xsk_socket_config *usr_config)
{ {
void *rx_map = NULL, *tx_map = NULL;
struct sockaddr_xdp sxdp = {}; struct sockaddr_xdp sxdp = {};
struct xdp_mmap_offsets off; struct xdp_mmap_offsets off;
struct xsk_socket *xsk; struct xsk_socket *xsk;
socklen_t optlen; socklen_t optlen;
void *map;
int err; int err;
if (!umem || !xsk_ptr || !rx || !tx) if (!umem || !xsk_ptr || !rx || !tx)
@ -594,40 +593,40 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
} }
if (rx) { if (rx) {
map = xsk_mmap(NULL, off.rx.desc + rx_map = xsk_mmap(NULL, off.rx.desc +
xsk->config.rx_size * sizeof(struct xdp_desc), xsk->config.rx_size * sizeof(struct xdp_desc),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, MAP_SHARED | MAP_POPULATE,
xsk->fd, XDP_PGOFF_RX_RING); xsk->fd, XDP_PGOFF_RX_RING);
if (map == MAP_FAILED) { if (rx_map == MAP_FAILED) {
err = -errno; err = -errno;
goto out_socket; goto out_socket;
} }
rx->mask = xsk->config.rx_size - 1; rx->mask = xsk->config.rx_size - 1;
rx->size = xsk->config.rx_size; rx->size = xsk->config.rx_size;
rx->producer = map + off.rx.producer; rx->producer = rx_map + off.rx.producer;
rx->consumer = map + off.rx.consumer; rx->consumer = rx_map + off.rx.consumer;
rx->ring = map + off.rx.desc; rx->ring = rx_map + off.rx.desc;
} }
xsk->rx = rx; xsk->rx = rx;
if (tx) { if (tx) {
map = xsk_mmap(NULL, off.tx.desc + tx_map = xsk_mmap(NULL, off.tx.desc +
xsk->config.tx_size * sizeof(struct xdp_desc), xsk->config.tx_size * sizeof(struct xdp_desc),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, MAP_SHARED | MAP_POPULATE,
xsk->fd, XDP_PGOFF_TX_RING); xsk->fd, XDP_PGOFF_TX_RING);
if (map == MAP_FAILED) { if (tx_map == MAP_FAILED) {
err = -errno; err = -errno;
goto out_mmap_rx; goto out_mmap_rx;
} }
tx->mask = xsk->config.tx_size - 1; tx->mask = xsk->config.tx_size - 1;
tx->size = xsk->config.tx_size; tx->size = xsk->config.tx_size;
tx->producer = map + off.tx.producer; tx->producer = tx_map + off.tx.producer;
tx->consumer = map + off.tx.consumer; tx->consumer = tx_map + off.tx.consumer;
tx->ring = map + off.tx.desc; tx->ring = tx_map + off.tx.desc;
tx->cached_cons = xsk->config.tx_size; tx->cached_cons = xsk->config.tx_size;
} }
xsk->tx = tx; xsk->tx = tx;
@ -654,13 +653,11 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
out_mmap_tx: out_mmap_tx:
if (tx) if (tx)
munmap(xsk->tx, munmap(tx_map, off.tx.desc +
off.tx.desc +
xsk->config.tx_size * sizeof(struct xdp_desc)); xsk->config.tx_size * sizeof(struct xdp_desc));
out_mmap_rx: out_mmap_rx:
if (rx) if (rx)
munmap(xsk->rx, munmap(rx_map, off.rx.desc +
off.rx.desc +
xsk->config.rx_size * sizeof(struct xdp_desc)); xsk->config.rx_size * sizeof(struct xdp_desc));
out_socket: out_socket:
if (--umem->refcount) if (--umem->refcount)
@ -685,10 +682,12 @@ int xsk_umem__delete(struct xsk_umem *umem)
optlen = sizeof(off); optlen = sizeof(off);
err = getsockopt(umem->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen); err = getsockopt(umem->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
if (!err) { if (!err) {
munmap(umem->fill->ring, (void)munmap(umem->fill->ring - off.fr.desc,
off.fr.desc + umem->config.fill_size * sizeof(__u64)); off.fr.desc +
munmap(umem->comp->ring, umem->config.fill_size * sizeof(__u64));
off.cr.desc + umem->config.comp_size * sizeof(__u64)); (void)munmap(umem->comp->ring - off.cr.desc,
off.cr.desc +
umem->config.comp_size * sizeof(__u64));
} }
close(umem->fd); close(umem->fd);
@ -699,6 +698,7 @@ int xsk_umem__delete(struct xsk_umem *umem)
void xsk_socket__delete(struct xsk_socket *xsk) void xsk_socket__delete(struct xsk_socket *xsk)
{ {
size_t desc_sz = sizeof(struct xdp_desc);
struct xdp_mmap_offsets off; struct xdp_mmap_offsets off;
socklen_t optlen; socklen_t optlen;
int err; int err;
@ -711,14 +711,17 @@ void xsk_socket__delete(struct xsk_socket *xsk)
optlen = sizeof(off); optlen = sizeof(off);
err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen); err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
if (!err) { if (!err) {
if (xsk->rx) if (xsk->rx) {
munmap(xsk->rx->ring, (void)munmap(xsk->rx->ring - off.rx.desc,
off.rx.desc + off.rx.desc +
xsk->config.rx_size * sizeof(struct xdp_desc)); xsk->config.rx_size * desc_sz);
if (xsk->tx) }
munmap(xsk->tx->ring, if (xsk->tx) {
off.tx.desc + (void)munmap(xsk->tx->ring - off.tx.desc,
xsk->config.tx_size * sizeof(struct xdp_desc)); off.tx.desc +
xsk->config.tx_size * desc_sz);
}
} }
xsk->umem->refcount--; xsk->umem->refcount--;