copy_file_range and splice accept a negative offset

Open
#467 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c

Research direction

Start in src/syscall/io.c at io_copy_chunked, then trace the corresponding offset checks in the splice path. Use the issue’s negative-offset cases for copy_file_range and splice as a guide. Done means a negative offset passed by pointer returns the specified Linux errno before copying, while a null pointer still uses the descriptor position.

Written by the indexing model from the issue text.

Description

bug

copy_file_range and splice read *off_in and *off_out from guest memory into an int64_t that starts at -1, and the copy code treats any negative value as "use the descriptor's position" (io_copy_chunked in src/syscall/io.c, and the same >= 0 tests in the splice path). A guest that passes a pointer to a negative offset therefore gets a copy from or to the file position instead of an error.

Linux rejects these calls. Measured with a static aarch64 probe that writes 8 bytes to a regular file and calls each syscall with a pointer to -1 and a count of 4:

Call Linux (qemu) elfuse
copy_file_range, *off_in = -1 -1, EOVERFLOW 4
copy_file_range, *off_out = -1 -1, EOVERFLOW 4
splice file to pipe, *off_in = -1 -1, EINVAL 4
splice pipe to file, *off_out = -1 -1, EINVAL 4

On Linux, copy_file_range gives EOVERFLOW because generic_copy_file_checks adds the size_t count to the loff_t position, the negative position promotes to a huge unsigned value, and the sum wraps. splice gives EINVAL from the rw_verify_area check on a negative position.

Goal: a negative offset passed by pointer is refused with the Linux errno before anything is copied, and the "no pointer" case keeps using the descriptor position. That means the sentinel would have to be "pointer was NULL" rather than "value is negative".

Found while answering review on #463; not changed there because it is outside RLIMIT_FSIZE.

Dominant language
C
Stars
287
Forks
33
Avg merge
2d 17h
Merged PRs (30d)
18

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sysprog21/elfuse

All issues in sysprog21/elfuse

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.