libnvmm: Fix mmap() failure with 'permission denied'
authorAaron LI <aly@aaronly.me>
Sun, 30 May 2021 23:25:14 +0000 (07:25 +0800)
committerAaron LI <aly@aaronly.me>
Tue, 15 Jun 2021 13:37:05 +0000 (21:37 +0800)
commitde56c667e852da83f763eddf9561d5930fb0b0b9
tree1c5b247fb1b45b79b8edd47c8e848d43072e4fc9
parentec8b21b882bc29f4666eb2af40132ab1bf64c708
libnvmm: Fix mmap() failure with 'permission denied'

The mmap() in nvmm_vcpu_create() was always failing with the EACCES
(permission denied) error code.  It was because mmap() was requesting
prot = PROT_READ|PROT_WRITE and flags = MAP_SHARED, but the fd was
opened with O_RDONLY (or O_WRONLY in nvmm_root_init()) and thus
disallowed such a mmap request.

Fix this issue by opening the nvmm fd with O_RDWR flag.  This also
requires to change the mode of '/dev/nvmm' from 0640 to 0660.
However, this makes root owner distinguishing in nvmm kernel module
useless.  So change to identify root owner by checking whether the
caller has root privilege.

In addition, refactor nvmm_root_init() to also check for root privilege
first and then call nvmm_init().
lib/libnvmm/libnvmm.c
sys/dev/virtual/nvmm/nvmm.c