The branch stable/15 has been updated by markj:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=0e33b730ed1a25d3ba50c8e237217b6f5a89fbed

commit 0e33b730ed1a25d3ba50c8e237217b6f5a89fbed
Author:     Mark Johnston <[email protected]>
AuthorDate: 2025-10-21 17:34:11 +0000
Commit:     Mark Johnston <[email protected]>
CommitDate: 2025-11-04 12:46:51 +0000

    vmm: Improve register get/set handling a bit
    
    On non-amd64 platforms, check for negative register indices.  This isn't
    required today since we match against individual register indices, but
    we might as well check it.  On amd64, add a comment explaining why we
    permit negative register indices.
    
    Use mallocarray() for allocating register arrays in the ioctl layer.
    
    No functional change intended.
    
    Reviewed by:    corvink
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D53143
    
    (cherry picked from commit 14133abfe9c218b97e888edf04d2ec4a86e7ab4b)
---
 sys/amd64/vmm/vmm.c   |  3 ++-
 sys/arm64/vmm/vmm.c   |  5 ++---
 sys/dev/vmm/vmm_dev.c | 16 ++++++++--------
 sys/riscv/vmm/vmm.c   |  5 ++---
 4 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/sys/amd64/vmm/vmm.c b/sys/amd64/vmm/vmm.c
index 2ac076551165..7254ebed6097 100644
--- a/sys/amd64/vmm/vmm.c
+++ b/sys/amd64/vmm/vmm.c
@@ -870,7 +870,7 @@ vm_assign_pptdev(struct vm *vm, int bus, int slot, int func)
 int
 vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval)
 {
-
+       /* Negative values represent VM control structure fields. */
        if (reg >= VM_REG_LAST)
                return (EINVAL);
 
@@ -882,6 +882,7 @@ vm_set_register(struct vcpu *vcpu, int reg, uint64_t val)
 {
        int error;
 
+       /* Negative values represent VM control structure fields. */
        if (reg >= VM_REG_LAST)
                return (EINVAL);
 
diff --git a/sys/arm64/vmm/vmm.c b/sys/arm64/vmm/vmm.c
index aeda689f3b1a..4f266de0cfb1 100644
--- a/sys/arm64/vmm/vmm.c
+++ b/sys/arm64/vmm/vmm.c
@@ -1279,8 +1279,7 @@ vcpu_get_state(struct vcpu *vcpu, int *hostcpu)
 int
 vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval)
 {
-
-       if (reg >= VM_REG_LAST)
+       if (reg < 0 || reg >= VM_REG_LAST)
                return (EINVAL);
 
        return (vmmops_getreg(vcpu->cookie, reg, retval));
@@ -1291,7 +1290,7 @@ vm_set_register(struct vcpu *vcpu, int reg, uint64_t val)
 {
        int error;
 
-       if (reg >= VM_REG_LAST)
+       if (reg < 0 || reg >= VM_REG_LAST)
                return (EINVAL);
        error = vmmops_setreg(vcpu->cookie, reg, val);
        if (error || reg != VM_REG_GUEST_PC)
diff --git a/sys/dev/vmm/vmm_dev.c b/sys/dev/vmm/vmm_dev.c
index 460a508a60dc..51c946f24c91 100644
--- a/sys/dev/vmm/vmm_dev.c
+++ b/sys/dev/vmm/vmm_dev.c
@@ -640,10 +640,10 @@ vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, 
int fflag,
                        error = EINVAL;
                        break;
                }
-               regvals = malloc(sizeof(regvals[0]) * vmregset->count, M_VMMDEV,
-                   M_WAITOK);
-               regnums = malloc(sizeof(regnums[0]) * vmregset->count, M_VMMDEV,
-                   M_WAITOK);
+               regvals = mallocarray(vmregset->count, sizeof(regvals[0]),
+                   M_VMMDEV, M_WAITOK);
+               regnums = mallocarray(vmregset->count, sizeof(regnums[0]),
+                   M_VMMDEV, M_WAITOK);
                error = copyin(vmregset->regnums, regnums, sizeof(regnums[0]) *
                    vmregset->count);
                if (error == 0)
@@ -666,10 +666,10 @@ vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, 
int fflag,
                        error = EINVAL;
                        break;
                }
-               regvals = malloc(sizeof(regvals[0]) * vmregset->count, M_VMMDEV,
-                   M_WAITOK);
-               regnums = malloc(sizeof(regnums[0]) * vmregset->count, M_VMMDEV,
-                   M_WAITOK);
+               regvals = mallocarray(vmregset->count, sizeof(regvals[0]),
+                   M_VMMDEV, M_WAITOK);
+               regnums = mallocarray(vmregset->count, sizeof(regnums[0]),
+                   M_VMMDEV, M_WAITOK);
                error = copyin(vmregset->regnums, regnums, sizeof(regnums[0]) *
                    vmregset->count);
                if (error == 0)
diff --git a/sys/riscv/vmm/vmm.c b/sys/riscv/vmm/vmm.c
index 790dcc576507..fda444d6461b 100644
--- a/sys/riscv/vmm/vmm.c
+++ b/sys/riscv/vmm/vmm.c
@@ -954,8 +954,7 @@ vcpu_get_state(struct vcpu *vcpu, int *hostcpu)
 int
 vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval)
 {
-
-       if (reg >= VM_REG_LAST)
+       if (reg < 0 || reg >= VM_REG_LAST)
                return (EINVAL);
 
        return (vmmops_getreg(vcpu->cookie, reg, retval));
@@ -966,7 +965,7 @@ vm_set_register(struct vcpu *vcpu, int reg, uint64_t val)
 {
        int error;
 
-       if (reg >= VM_REG_LAST)
+       if (reg < 0 || reg >= VM_REG_LAST)
                return (EINVAL);
        error = vmmops_setreg(vcpu->cookie, reg, val);
        if (error || reg != VM_REG_GUEST_SEPC)

Reply via email to