Skip to content

mkimg: Fix parsing of filenames containing colons#2041

Open
kitkat1424 wants to merge 2 commits intofreebsd:mainfrom
kitkat1424:fix/mkimg-colon-parsing
Open

mkimg: Fix parsing of filenames containing colons#2041
kitkat1424 wants to merge 2 commits intofreebsd:mainfrom
kitkat1424:fix/mkimg-colon-parsing

Conversation

@kitkat1424
Copy link
Contributor

When using PART_KIND_FILE (-p type:=filename), mkimg uses a colon to separate an optional offset (e.g., file:offset). strsep() was being used to split the string at the first colon. This caused failures when the filename itself contained a colon (e.g., "th:is").

This patch use stat() to check if the entire string exists as a file. If so, use it directly without splitting.
If the full string is not a valid file, fall back to splitting at the right-most colon using strrchr().

PR: 257960
Signed-off by: Aaditya Singh aadityavksingh@gmail.com

@kitkat1424 kitkat1424 marked this pull request as ready for review February 22, 2026 06:09
@kitkat1424 kitkat1424 marked this pull request as draft February 22, 2026 06:23
@kitkat1424 kitkat1424 force-pushed the fix/mkimg-colon-parsing branch from 7448a97 to 8800453 Compare February 22, 2026 06:39
@kitkat1424 kitkat1424 marked this pull request as ready for review February 22, 2026 06:45
When using PART_KIND_FILE (-p type:=filename), mkimg uses a colon
to separate an optional offset (e.g., file:offset).
strsep() was being used to split the string at the first colon.
This caused failures when the filename itself contained a colon
(e.g., "th:is").

This patch use stat() to check if the entire string exists as a
file. If so, use it directly without splitting.
If the full string is not a valid file, fall back to splitting
at the right-most colon using strrchr().

PR:  		\<257960\>
Signed-off-by:  Aaditya Singh <aadityavksingh@gmail.com>
@kitkat1424 kitkat1424 force-pushed the fix/mkimg-colon-parsing branch from 8800453 to 1fe1f5c Compare March 8, 2026 11:41
Copy link
Member

@jlduran jlduran left a comment

Choose a reason for hiding this comment

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

Thank you. I think this is a good approach. I believe it is only missing the path when it is a directory, and should error.

Also, per style(9), the braces around single-line statements are optional, and in the case of the current style of the file, should not be used.

Consider this a pre-review, while we wait for the CODEOWNER.

case PART_KIND_FILE:
size = part->contents;
if (stat(part->contents, &sb) == 0 &&
!S_ISDIR(sb.st_mode)) {
Copy link
Member

Choose a reason for hiding this comment

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

What happens when it is a directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For an existing directory, the program would have fallen through to the next switch statement. The image_copyin function returns an EISDIR error (specifically, when it eventually attempts a read(fd, ...) on the directory's file descriptor).

In my 2nd commit, I have added a change so that it returns with this error right away to avoid some unnecessary operations.

Incorrect directories, just like incorrect filenames were already handled (they will return an error on open()).

@github-actions
Copy link

github-actions bot commented Mar 21, 2026

Thank you for taking the time to contribute to FreeBSD!

All issues resolved.

@kitkat1424 kitkat1424 force-pushed the fix/mkimg-colon-parsing branch from 9113936 to f7cd337 Compare March 21, 2026 19:23
@kitkat1424
Copy link
Contributor Author

Thanks for taking the time to look at this @jlduran

I have made some changes to address your feedback regarding
the missed case of valid directory as argument and the style
change.
I am not sure if this counts as a logical change and should be left
as a second commit, or if it is a fix-up and I should squash both.

Use errc() to fail and exit immediately when an existing directory
is input instead of a file in PART_KIND_FILE mode.

Signed-off-by: Aaditya Singh <aadityavksingh@gmail.com>
@kitkat1424 kitkat1424 force-pushed the fix/mkimg-colon-parsing branch from f7cd337 to 74de4d4 Compare March 21, 2026 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants