diff options
author | swapnili <swapnil.ingle@nutanix.com> | 2021-01-21 15:38:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-21 15:38:24 +0100 |
commit | e899af8f842c9ba2d48fb44b507e90187f2fab47 (patch) | |
tree | 3987cf106c923ad52b9605c6486b7868ac3c2cbe /lib/dma.h | |
parent | 4f3ad36fc3956d9c1ca926615301e40134b75d58 (diff) | |
download | libvfio-user-e899af8f842c9ba2d48fb44b507e90187f2fab47.zip libvfio-user-e899af8f842c9ba2d48fb44b507e90187f2fab47.tar.gz libvfio-user-e899af8f842c9ba2d48fb44b507e90187f2fab47.tar.bz2 |
Misc fixes for DMA_MAP region prot (#233)
* Misc fixes for DMA_MAP region prot
1. Validate prot passed in vfu_addr_to_sg()
2. Let user know region prot via vfu_unmap_dma_cb_t
Signed-off-by: Swapnil Ingle <swapnil.ingle@nutanix.com>
Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'lib/dma.h')
-rw-r--r-- | lib/dma.h | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -184,11 +184,17 @@ _dma_mark_dirty(const dma_controller_t *dma, const dma_memory_region_t *region, } } -static inline void +static inline int dma_init_sg(const dma_controller_t *dma, dma_sg_t *sg, dma_addr_t dma_addr, uint32_t len, int prot, int region_index) { const dma_memory_region_t *const region = &dma->regions[region_index]; + + if ((prot & PROT_WRITE) && !(region->prot & PROT_WRITE)) { + errno = EACCES; + return -1; + } + sg->dma_addr = region->dma_addr; sg->region = region_index; sg->offset = dma_addr - region->dma_addr; @@ -197,6 +203,8 @@ dma_init_sg(const dma_controller_t *dma, dma_sg_t *sg, dma_addr_t dma_addr, _dma_mark_dirty(dma, region, sg); } sg->mappable = region->virt_addr != NULL; + + return 0; } /* Takes a linear dma address span and returns a sg list suitable for DMA. @@ -206,7 +214,9 @@ dma_init_sg(const dma_controller_t *dma, dma_sg_t *sg, dma_addr_t dma_addr, * Returns: * - On success, number of scatter gather entries created. * - On failure: - * -1 if the dma address span is invalid + * -1 if + * - the DMA address span is invalid + * - protection violation (errno=EACCES) * (-x - 1) if @max_sg is too small, where x is the number of sg entries * necessary to complete this request. */ @@ -216,7 +226,7 @@ dma_addr_to_sg(const dma_controller_t *dma, dma_sg_t *sg, int max_sg, int prot) { static __thread int region_hint; - int cnt; + int cnt, ret; const dma_memory_region_t *const region = &dma->regions[region_hint]; const dma_addr_t region_end = region->dma_addr + region->size; @@ -225,7 +235,11 @@ dma_addr_to_sg(const dma_controller_t *dma, if (likely(max_sg > 0 && len > 0 && dma_addr >= region->dma_addr && dma_addr + len <= region_end && region_hint < dma->nregions)) { - dma_init_sg(dma, sg, dma_addr, len, prot, region_hint); + ret = dma_init_sg(dma, sg, dma_addr, len, prot, region_hint); + if (ret < 0) { + return ret; + } + return 1; } // Slow path: search through regions. |