Skip to content

Validate Guest Address Ranges for Overlapping Regions in map_region#1464

Open
Richard-Durkee wants to merge 1 commit into
hyperlight-dev:mainfrom
Richard-Durkee:fix/validate-map-region-overlap
Open

Validate Guest Address Ranges for Overlapping Regions in map_region#1464
Richard-Durkee wants to merge 1 commit into
hyperlight-dev:mainfrom
Richard-Durkee:fix/validate-map-region-overlap

Conversation

@Richard-Durkee
Copy link
Copy Markdown

@Richard-Durkee Richard-Durkee commented May 19, 2026

Add overlap validation to HyperlightVm::map_region to enforce the safety contract documented on VirtualMachine::map_memory, which requires non-overlapping regions.

Checks the new region against existing dynamically mapped regions (mmap_regions), the snapshot region (starting at BASE_ADDRESS), and the scratch region (at the top of the guest physical address space).

Adds Overlapping variant to MapRegionError with a descriptive message showing both the new and conflicting ranges.

Also adds early validation at the MultiUseSandbox::map_region level to reject invalid input before side effects (snapshot reset) occur.

Tested on KVM (c8i.2xlarge with nested virtualization enabled).

Closes #1289

@Richard-Durkee Richard-Durkee changed the title fix: validate guest address ranges for overlapping regions in map_region Validate Guest Address Ranges for Overlapping Regions in map_region May 19, 2026
@ludfjig ludfjig added the kind/bugfix For PRs that fix bugs label May 19, 2026
@Richard-Durkee Richard-Durkee force-pushed the fix/validate-map-region-overlap branch from 644165f to bb16213 Compare May 19, 2026 18:43
Copy link
Copy Markdown
Contributor

@ludfjig ludfjig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your contribution!

I think we can drop the validation in MultiUseSandbox::map_region entirely and rely on the check inside HyperlightVm::map_region, then reorder so the call happens before the snapshot reset:

unsafe { self.vm.map_region(rgn) }.map_err(HyperlightVmError::MapRegion)?;
self.snapshot = None;
self.mem_mgr.mapped_rgns += 1;

The same pattern can probably applies to map_file_cow too I think. What do you think about this?

@Richard-Durkee Richard-Durkee force-pushed the fix/validate-map-region-overlap branch 2 times, most recently from 3075ff3 to f2c4072 Compare May 19, 2026 21:38
@Richard-Durkee
Copy link
Copy Markdown
Author

thanks for your contribution!

I think we can drop the validation in MultiUseSandbox::map_region entirely and rely on the check inside HyperlightVm::map_region, then reorder so the call happens before the snapshot reset:

unsafe { self.vm.map_region(rgn) }.map_err(HyperlightVmError::MapRegion)?;
self.snapshot = None;
self.mem_mgr.mapped_rgns += 1;

The same pattern can probably applies to map_file_cow too I think. What do you think about this?

Thanks for the feedback!

This makes sense to me. I just pushed again and included these suggestions. Please let me know if there are any other changes you'd like.

@ludfjig
Copy link
Copy Markdown
Contributor

ludfjig commented May 20, 2026

thanks for your contribution!
I think we can drop the validation in MultiUseSandbox::map_region entirely and rely on the check inside HyperlightVm::map_region, then reorder so the call happens before the snapshot reset:

unsafe { self.vm.map_region(rgn) }.map_err(HyperlightVmError::MapRegion)?;
self.snapshot = None;
self.mem_mgr.mapped_rgns += 1;

The same pattern can probably applies to map_file_cow too I think. What do you think about this?

Thanks for the feedback!

This makes sense to me. I just pushed again and included these suggestions. Please let me know if there are any other changes you'd like.

Looks great! Just one minor thing I just realized the page-table "tail" of compacted snapshots are not mapped into the vm, so the could be less than mem_size(). I think switching to

#[cfg(not(unshared_snapshot_mem))]
let snap_end = snap_start + snapshot.guest_mapped_size();
#[cfg(unshared_snapshot_mem)]
let snap_end = snap_start + snapshot.mem_size();

should do it. Thanks!

@Richard-Durkee Richard-Durkee force-pushed the fix/validate-map-region-overlap branch from f2c4072 to cfd33a6 Compare May 20, 2026 22:57
Closes hyperlight-dev#1289

Signed-off-by: Richard Durkee <Richard-Durkee@users.noreply.github.com>
@Richard-Durkee Richard-Durkee force-pushed the fix/validate-map-region-overlap branch from cfd33a6 to fa24c67 Compare May 20, 2026 23:01
@Richard-Durkee
Copy link
Copy Markdown
Author

Ah, okay -- I just learned about compacted snapshots. Pushed the change. Thanks again for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bugfix For PRs that fix bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate guest address ranges for overlapping regions in map_region / map_file_cow

2 participants