tpm2_util: modify tpm2_safe_read_from_stdin to read the entire line#3553
Merged
JuergenReppSIT merged 1 commit intoFeb 18, 2026
Merged
Conversation
Fixes tpm2-software#3551. The tpm2_safe_read_from_stdin function is used exclusively in tools/fapi/tss2_template.c to read file paths from stdin. The current implementation however uses the "%s" template in sscanf, which cannot properly handle file paths containing whitespaces. This commit modifies the function's behavior to read the entire line. Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
JuergenReppSIT
added a commit
to JuergenReppSIT/tpm2-tools
that referenced
this pull request
Mar 6, 2026
The problem was already fixed by PR tpm2-software#3553. Fixes: tpm2-software#3567 This reverts commit 91f5524. Signed-off-by: Juergen Repp <juergen_repp@web.de>
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.
Summary
This PR fixes an issue where
tpm2_safe_read_from_stdinfails to correctly read file paths containing whitespaces.Fixes #3551
Supersedes #3548
Problem
The tpm2_safe_read_from_stdin function is used exclusively in
tools/fapi/tss2_template.cto read file paths from stdin. Currently, the function usessscanf(buf, "%s", read_data), which stops reading at the first whitespace character (excluding leading whitespace). This means whitespaces are interpreted as delimiters, leading to unexpected behavior when users provide file paths containing spacesExample
/home/user/workspace/sample file.bin/home/user/workspace/sample/home/user/workspace/sample file.binChanges
This PR modifies
tpm2_safe_read_from_stdinto read the entire line.fgets(data, length, stdin)to get the entire line. This ensures null termination and prevents buffer overflows.strcspnto find the first newline character (\ror\n) and replace it with\0.As a by-product of this change, the implementation has been significantly cleaned up:
mallocandfreecalls are now completely removed, eliminating unnecessary memory management.mallocwas missing has been resolved.This is a breaking change because the function now captures spaces instead of treating them as delimiters. However, given its specific usage for file paths, this is the intended and correct behavior.
Test
Note on CodeQL Alert
I noticed that CodeQL flagged "Uncontrolled data used in path expression" vulnerabilities regarding user input being passed to file operations. It is important to note that this does not introduce a new vulnerability; the original implementation also processed user-provided paths from stdin for file access, but its previous complexity (redundant
mallocandsscanf) likely obscured the data flow from the static analyzer.By simplifying the code to use
fgets, the existing data flow became transparent to CodeQL. Since this is a CLI utility where the user is expected to provide file paths, this behavior is intended and consistent with the original logic.