Skip to content

Commit 7377c87

Browse files
committed
vmm: Consolidate VM name length checking
vm_create() is only called from one place. Rather than having similar checks everywhere, move them to vmmdev_create(). We can safely assume that the name is nul-terminated, the vmmctl ioctl handler and the legacy sysctl handler ensure this. So, don't bother with strnlen(). Finally, make sure that the name buffers are the same size on all platforms. VM_MAX_NAMELEN is supposed to be the maximum, not including the nul terminator. Reviewed by: corvink MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D53422
1 parent e758074 commit 7377c87

File tree

12 files changed

+47
-62
lines changed

12 files changed

+47
-62
lines changed

sys/amd64/include/vmm.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,7 @@ enum x2apic_state {
122122
#define VM_INTINFO_HWEXCEPTION (3 << 8)
123123
#define VM_INTINFO_SWINTR (4 << 8)
124124

125-
/*
126-
* The VM name has to fit into the pathname length constraints of devfs,
127-
* governed primarily by SPECNAMELEN. The length is the total number of
128-
* characters in the full path, relative to the mount point and not
129-
* including any leading '/' characters.
130-
* A prefix and a suffix are added to the name specified by the user.
131-
* The prefix is usually "vmm/" or "vmm.io/", but can be a few characters
132-
* longer for future use.
133-
* The suffix is a string that identifies a bootrom image or some similar
134-
* image that is attached to the VM. A separator character gets added to
135-
* the suffix automatically when generating the full path, so it must be
136-
* accounted for, reducing the effective length by 1.
137-
* The effective length of a VM name is 229 bytes for FreeBSD 13 and 37
138-
* bytes for FreeBSD 12. A minimum length is set for safety and supports
139-
* a SPECNAMELEN as small as 32 on old systems.
140-
*/
141-
#define VM_MAX_PREFIXLEN 10
142-
#define VM_MAX_SUFFIXLEN 15
143-
#define VM_MIN_NAMELEN 6
144-
#define VM_MAX_NAMELEN \
145-
(SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
146-
147125
#ifdef _KERNEL
148-
#include <sys/kassert.h>
149-
150-
CTASSERT(VM_MAX_NAMELEN >= VM_MIN_NAMELEN);
151-
152126
struct vm;
153127
struct vm_exception;
154128
struct vm_mem;

sys/amd64/include/vmm_dev.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <machine/vmm.h>
3535
#include <machine/vmm_snapshot.h>
3636

37+
#include <dev/vmm/vmm_param.h>
38+
3739
struct vm_memmap {
3840
vm_paddr_t gpa;
3941
int segid; /* memory segment */

sys/amd64/vmm/vmm.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,6 @@ vm_create(const char *name, struct vm **retvm)
523523
struct vm *vm;
524524
int error;
525525

526-
if (name == NULL || strnlen(name, VM_MAX_NAMELEN + 1) ==
527-
VM_MAX_NAMELEN + 1)
528-
return (EINVAL);
529-
530526
vm = malloc(sizeof(struct vm), M_VM, M_WAITOK | M_ZERO);
531527
error = vm_mem_init(&vm->mem, 0, VM_MAXUSER_ADDRESS_LA48);
532528
if (error != 0) {

sys/arm64/include/vmm.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,6 @@ enum vm_reg_name {
106106

107107
#define VM_GUEST_BASE_IPA 0x80000000UL /* Guest kernel start ipa */
108108

109-
/*
110-
* The VM name has to fit into the pathname length constraints of devfs,
111-
* governed primarily by SPECNAMELEN. The length is the total number of
112-
* characters in the full path, relative to the mount point and not
113-
* including any leading '/' characters.
114-
* A prefix and a suffix are added to the name specified by the user.
115-
* The prefix is usually "vmm/" or "vmm.io/", but can be a few characters
116-
* longer for future use.
117-
* The suffix is a string that identifies a bootrom image or some similar
118-
* image that is attached to the VM. A separator character gets added to
119-
* the suffix automatically when generating the full path, so it must be
120-
* accounted for, reducing the effective length by 1.
121-
* The effective length of a VM name is 229 bytes for FreeBSD 13 and 37
122-
* bytes for FreeBSD 12. A minimum length is set for safety and supports
123-
* a SPECNAMELEN as small as 32 on old systems.
124-
*/
125-
#define VM_MAX_PREFIXLEN 10
126-
#define VM_MAX_SUFFIXLEN 15
127-
#define VM_MAX_NAMELEN \
128-
(SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
129-
130109
#ifdef _KERNEL
131110
struct vm;
132111
struct vm_exception;

sys/arm64/include/vmm_dev.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include <machine/vmm.h>
3333

34+
#include <dev/vmm/vmm_param.h>
35+
3436
struct vm_memmap {
3537
vm_paddr_t gpa;
3638
int segid; /* memory segment */

sys/arm64/vmm/vmm.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct vm {
124124
volatile cpuset_t suspended_cpus; /* (i) suspended vcpus */
125125
volatile cpuset_t halted_cpus; /* (x) cpus in a hard halt */
126126
struct vm_mem mem; /* (i) guest memory */
127-
char name[VM_MAX_NAMELEN]; /* (o) virtual machine name */
127+
char name[VM_MAX_NAMELEN + 1]; /* (o) virtual machine name */
128128
struct vcpu **vcpu; /* (i) guest vcpus */
129129
struct vmm_mmio_region mmio_region[VM_MAX_MMIO_REGIONS];
130130
/* (o) guest MMIO regions */
@@ -437,9 +437,6 @@ vm_create(const char *name, struct vm **retvm)
437437
struct vm *vm;
438438
int error;
439439

440-
if (name == NULL || strlen(name) >= VM_MAX_NAMELEN)
441-
return (EINVAL);
442-
443440
vm = malloc(sizeof(struct vm), M_VMM, M_WAITOK | M_ZERO);
444441
error = vm_mem_init(&vm->mem, 0, 1ul << 39);
445442
if (error != 0) {

sys/dev/vmm/vmm_dev.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,9 @@ vmmdev_create(const char *name, struct ucred *cred)
984984
struct vm *vm;
985985
int error;
986986

987+
if (name == NULL || strlen(name) > VM_MAX_NAMELEN)
988+
return (EINVAL);
989+
987990
sx_xlock(&vmmdev_mtx);
988991
sc = vmmdev_lookup(name, cred);
989992
if (sc != NULL) {

sys/dev/vmm/vmm_dev.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
#include <sys/types.h>
1313
#include <sys/ioccom.h>
14+
1415
#include <machine/vmm_dev.h>
1516

17+
#include <dev/vmm/vmm_param.h>
18+
1619
#ifdef _KERNEL
1720
struct thread;
1821
struct vm;

sys/dev/vmm/vmm_param.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*-
2+
* SPDX-License-Identifier: BSD-2-Clause
3+
*
4+
* Copyright (c) 2011 NetApp, Inc.
5+
* All rights reserved.
6+
*/
7+
8+
#ifndef _DEV_VMM_PARAM_H_
9+
#define _DEV_VMM_PARAM_H_
10+
11+
/*
12+
* The VM name has to fit into the pathname length constraints of devfs,
13+
* governed primarily by SPECNAMELEN. The length is the total number of
14+
* characters in the full path, relative to the mount point and not
15+
* including any leading '/' characters.
16+
* A prefix and a suffix are added to the name specified by the user.
17+
* The prefix is usually "vmm/" or "vmm.io/", but can be a few characters
18+
* longer for future use.
19+
* The suffix is a string that identifies a bootrom image or some similar
20+
* image that is attached to the VM. A separator character gets added to
21+
* the suffix automatically when generating the full path, so it must be
22+
* accounted for, reducing the effective length by 1.
23+
* The effective length of a VM name is 229 bytes for FreeBSD 13 and 37
24+
* bytes for FreeBSD 12. A minimum length is set for safety and supports
25+
* a SPECNAMELEN as small as 32 on old systems.
26+
*/
27+
#define VM_MAX_PREFIXLEN 10
28+
#define VM_MAX_SUFFIXLEN 15
29+
#define VM_MIN_NAMELEN 6
30+
#define VM_MAX_NAMELEN \
31+
(SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
32+
33+
#endif /* !_DEV_VMM_PARAM_H_ */

sys/riscv/include/vmm.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ enum vm_reg_name {
103103
#define VM_INTINFO_HWEXCEPTION (3 << 8)
104104
#define VM_INTINFO_SWINTR (4 << 8)
105105

106-
#define VM_MAX_NAMELEN 32
107-
#define VM_MAX_SUFFIXLEN 15
108-
109106
#ifdef _KERNEL
110107

111108
struct vm;

0 commit comments

Comments
 (0)