Page MenuHomeFreeBSD

vmm: Add credential to cdev object

Authored by on Jul 12 2021, 7:58 PM.



Add a credential to the cdev object in sysctl_vmm_create, then check that we have the correct credentials in sysctl_vmm_destroy. This prevents a process in one jail from opening or destroying the /dev/vmm file corresponding to a vm in another jail.

Diff Detail

R10 FreeBSD src repository
Automatic diff as part of commit; lint not applicable.
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

Could you add a couple of short atf-sh tests for this using bhyvectl --create and --destroy? I suspect they should live in tests/sys/vmm.


I think it's probably ok to borrow the cdev's credential reference like this but it'd be a bit nicer to maintain a separate reference (to the same credential) in the VM softc as well. The overhead of doing that is negligible, and it might avoid some tricky bugs that would arise if something tries to access the VM's credential after the cdev is destroyed.

For example, this function sets sc->cdev = NULL before triggering asynchronous destruction of the VM (which may take a long time) by calling destroy_dev_sched_cb(). If someone naively modifies vmmdev_destroy() such that it tries to dereference sc->cdev->si_cred (perhaps through several layers of indirection), we'll get a null pointer dereference. Keeping a separate credential reference avoids coupling between VMM and devfs object lifecycles, so I'd tend to prefer that approach even though it requires a bit more code.


I think EPERM is a more suitable error number here.

Add a credential to the softc struct as well. Also add some basic tests.


These tests should first check whether the vmm kernel module is loaded. Something like

if ! kldstat -qn vmm; then
    atf_skip "vmm is not loaded"

Check that vmm module is loaded before running tests.


I think that we should move this check into vmmdev_lookup() and have it return NULL if the check fails. Otherwise every caller of vmmdev_lookup() will have to be careful to check the credential.

Move the credential check to vmmdev_lookup. Note that this means the error code will be EINVAL in sysctl_vmm_destroy, rather than EPERM.

This revision is now accepted and ready to land.Jul 21 2021, 2:30 PM
jhb added a subscriber: jhb.
jhb added inline comments.

This is ok, but another way you could have handled this would be:

   if (strcmp(...) == 0)
if (sc == NULL)
  return (NULL);

if (!cr_cansee(curthread->td_ucred, sc->ucred))
  return (NULL);

return (sc);

Layering the checks like that would make the new code a bit smaller (you are just adding a new check, not changing the existing loop). It also avoids deep nesting if we wanted to add additional checks in the future. edited the summary of this revision. (Show Details)

Simplify the loop in vmmdev_lookup.

This revision now requires review to proceed.Jul 30 2021, 6:21 PM

Made a mistake in the last diff - accidentally omitted the test files.

This revision was not accepted when it landed; it landed in state Needs Review.Aug 18 2021, 5:44 PM
This revision was automatically updated to reflect the committed changes.