bpftool: fix batch file handling issues - #8684
Open
kernel-patches-daemon-bpf-rc[bot] wants to merge 3 commits into
Open
bpftool: fix batch file handling issues#8684kernel-patches-daemon-bpf-rc[bot] wants to merge 3 commits into
kernel-patches-daemon-bpf-rc[bot] wants to merge 3 commits into
Conversation
Author
|
Upstream branch: d114bb9 |
kernel-patches-daemon-bpf-rc
Bot
force-pushed
the
bpf-next_base
branch
from
August 11, 2026 22:08
818432c to
d922dc0
Compare
Author
|
Upstream branch: 07cb86a |
map_dump() closes the map fd in its error path, and do_dump() then closes the same fd again after a successful dump. Closing an already closed fd leaves errno set to EBADF, which poisons later errno checks such as the batch file read check in do_batch(). Let do_dump() own the fd and remove the close from map_dump(). The same double-close pattern exists in do_show_subset(): both show_map_close_json() and show_map_close_plain() already close the fd, so drop the extra close() there as well. Also propagate the error when bpf_map_get_info_by_fd() fails on a subsequent map in do_dump(): set err = -1 before breaking out of the loop, so a later failure is not silently hidden after an earlier iteration succeeded. Fixes: 99f9863 ("bpftool: Match maps by name") Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
do_batch() checks errno after the read loop to detect read failures, but fgets() does not clear errno on success, so a stale errno left by a previously executed command (e.g. map dump's EBADF from a double close) makes bpftool report a batch file read failure and exit with an error even though every command succeeded. Use ferror() instead, and track the too-long-line case explicitly. Since do_batch() no longer inspects errno to detect read failures, drop the USE_LIBCAP errno reset in main() that existed only to keep errno clean for the batch mode. Fixes: 71bb428 ("tools: bpf: add bpftool") Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
do_batch() strips trailing comments by truncating the line at '#' before checking whether fgets() filled the buffer. If a batch line longer than the buffer contains a '#' within the first sizeof(buf) - 1 bytes, the truncation makes strlen(buf) smaller and the line-length check is bypassed. The unread remainder of the line then stays in the file stream and is parsed and executed as a separate command on the next loop iteration. Continuation lines handled below are affected the same way: an overlong continuation line containing '#' bypasses the "command is too long" check, and its unread remainder is executed as a separate command. Detect the truncated read before stripping the comment, using memchr() to look for a newline (instead of strlen(), which is also fooled by an embedded NUL byte) and feof() to tell an overlong line apart from a final line without a trailing newline. A line that fills the buffer exactly (the byte after the read is a newline) is not treated as truncated, so valid maximal-length lines are no longer rejected. Use the result for the line-length checks, so overlong lines are rejected regardless of comments or NUL bytes. Fixes: 71bb428 ("tools: bpf: add bpftool") Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
kernel-patches-daemon-bpf-rc
Bot
force-pushed
the
series/1143455=>bpf-next
branch
from
August 11, 2026 22:11
d4966c1 to
61f80ca
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull request for series with
subject: bpftool: fix batch file handling issues
version: 5
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=1143455