Definition
A copy-only helper is a helper function §2.19 that gives a guest program data by copying bytes into the guest’s own memory, never by handing it a pointer into host memory. A helper function is a host function that a guest, code running inside a virtual machine §2.3 (VM), may call. The guest names the data it wants, such as an offset in a received packet, plus a destination buffer and a length; the helper checks both ranges, then copies. The name describes a design pattern, not a standard term.
The idea
A guest needs 4 bytes of a received packet that the host holds in its own memory. The host can answer in two ways. It can return the address of the packet. The guest then holds a pointer into host memory, and every later access through that pointer must be checked against the packet’s bounds and against its lifetime, since the host may free the packet while the guest still holds the address. Or the host can copy the 4 bytes into a buffer in the guest’s memory and return a status. The guest then holds values, not an address, and its later accesses go to its own memory, which the virtual machine checks anyway.
Copying concentrates the checks at one point, the entry to the helper, and keeps host addresses inside the host. The price is the time to copy, which grows with the length; buffer space in the guest; and a copy that does not follow later changes to the original.
A library that photocopies the requested pages instead of lending the book is a fair picture. The reader never holds the book, and the librarian checks that the requested pages exist and that the reader’s folder has room for them. The picture stops at content. A photocopy of a page that contains a secret still discloses the secret. Copying limits what the guest can reach; it does not decide what the guest may learn.
How it works
A copy request carries a source object, a source offset , a destination offset inside the guest’s memory and a length , all in bytes. The helper works in this order.
Find the source. The guest names an object the host has given it, such as the current packet, never a raw host address. The host knows the object’s authorized length , the number of bytes the guest may learn.
Check the source range. Bytes to must lie inside the bytes: test , then . This order is an overflow-safe range check §4.17: the subtraction runs only after the first test has shown that is not larger than , so it cannot go below zero, and no sum is formed that could wrap around.
Check the destination range. Bytes to must lie inside the guest memory that the guest may write, of size : test , then .
Cap the length. Test against a fixed maximum, because the copy’s time grows with and the guest’s run time must stay bounded.
Copy exactly bytes. If the source can also lie in guest memory, so that the two ranges may overlap, use an operation defined for overlap, such as C’s
memmove;memcpyon overlapping ranges is undefined behavior §4.12.Report the outcome: the number of bytes copied, or an error code.
The two range checks are independent. A valid source does not stop a write past the end of the destination, and a valid destination does not stop a read past the end of the source. The source check prevents an out-of-bounds read §4.5, which would disclose host memory to the guest. The destination check prevents an out-of-bounds write §4.4, which would corrupt host memory next to the guest’s buffer.
Three more choices belong to the helper’s contract. First, what the destination holds after a failure: several Linux helpers of this kind, such as the one that loads packet bytes into an eBPF §2.1 (extended Berkeley Packet Filter) program’s buffer, fill the destination with zeros, so the program never reads leftover bytes. Second, which bytes the source exposes. A structure copied whole includes its padding §4.27, unused bytes that the compiler inserts so that each field starts at an address its type requires, and padding keeps whatever that memory held before. Copy named fields, or a defined encoding of them, instead. Third, consistency: if another thread can change the source during the copy, the result can mix old and new values. Copy under a lock, or from a coherent snapshot §9.23, a copy taken at one instant that no writer changes.
Worked example
The host holds a 20-byte packet, so , and the guest has a 16-byte buffer, so . Packet bytes 8 to 11 hold 192, 168, 1 and 2.
Request , , . Source: , and . Destination: , and . Both pass. Buffer positions 4, 5, 6 and 7 receive 192, 168, 1 and 2. The guest has the values, not the packet’s address.
Request , , . Source: is false. Refused. The copy would have read packet bytes 18 to 21, and bytes 20 and 21 are host memory beyond the packet.
Request , , . Destination: is false. Refused. The copy would have written positions 14 to 17, and positions 16 and 17 are host memory beyond the buffer.
The form of the test matters when numbers can wrap around. Let the helper use unsigned 32-bit arithmetic, which counts modulo : any result that reaches loses . A guest sends and . The naive test computes , which wraps to , and passes: the helper would read at offset 4,294,967,295, far outside the packet. The safe test asks whether , which is false.
Now the padding leak. A C structure holds a 1-byte field kind followed by a 4-byte integer field value. Common C compilers place a 4-byte integer at an address divisible by 4, so kind sits at offset 0, padding fills offsets 1 to 3, and value sits at offsets 4 to 7: 8 bytes, of which carry data. With kind equal to 2 and value equal to 500, stored least significant byte first as 244, 1, 0, 0, copying all 8 bytes gives the guest 2, then three leftover bytes, perhaps part of a key, then 244, 1, 0, 0. Copying the named fields gives exactly 2, 244, 1, 0, 0.
Stated precisely
Let be the authorized length of the source object, the size of the guest memory the destination may occupy, and the source and destination offsets, the length, and the helper’s length cap, all non-negative integers counting bytes. The copy is admitted only if with each subtraction evaluated only after the comparison just before it has shown that the result cannot go below zero. After a successful copy, for every with , where is the source object and the guest memory, and no other guest byte has changed.
Three named assumptions stand behind this. Nothing writes the source during the copy; otherwise the copy may mix values from two moments. The destination stays allocated and reserved for the guest for the whole copy. And every one of the source bytes is a byte the guest is allowed to learn: the range checks prove only that the guest reads from the authorized object, not that the object contains nothing secret.
Limits and common mistakes
A copy is a snapshot. It shows the source at one instant and goes stale when the source changes, so a guest that decides later decides on old data.
Copy-only does not remove every pointer. The guest still passes the helper the address of its destination, and that address must be checked and must stay valid for the call; the helper borrows it only until it returns.
Copying costs time in proportion to and needs buffer space in the guest. Without a length cap, a guest can make one call as expensive as it likes.
The common mistake is to check only one end: to validate the destination because the guest controls it, and to trust the source offset because it refers to host data. Both offsets and the length come from the guest. A related mistake is to check only : an access that starts inside the source can still end outside it.
Copying works in both directions. A helper that copies guest bytes into host memory needs the same two range checks, and the host must treat the copied bytes as untrusted input.
Questions answered
Are both source length and destination range validated?
Both must be validated, independently, before the first byte is copied; checking only one leaves the other end open. A copy-only helper is a host function that gives a guest program, code running in a virtual machine, data by copying bytes into the guest’s memory. The source check confirms that the requested bytes starting at source offset lie inside the source’s authorized length ; it prevents an out-of-bounds read that would disclose host memory. The destination check confirms that the bytes starting at destination offset lie inside the guest memory of size that the guest may write; it prevents an out-of-bounds write that would corrupt host memory.
Each check uses the overflow-safe form, and then , and likewise and then , because the naive form can wrap around for a huge offset and pass. The length also needs a fixed cap, so that one call’s time stays bounded. With , and , source offset 18 fails because , and destination offset 14 fails because . Linux eBPF (extended Berkeley Packet Filter) splits the work in a typical way: for its packet-loading helper, the verifier checks the destination buffer and the length before the program runs, and the helper checks the source offset at run time, because the packet’s length is known only then.
Related entries: Overflow-safe range check (§4.17), Guest context (§2.23), Runtime bounds check (§2.27), Helper contract (§2.21), Struct padding and packing (§4.27), Coherent snapshot (§9.23), Out-of-bounds read (§4.5). Further reading: [2, 78].