Skip to content

Fix FileStat stat parsing on non-GNU systems#20830

Merged
cdelafuente-r7 merged 1 commit intorapid7:masterfrom
SilentSobs:fix-filestat-non-gnu
Feb 27, 2026
Merged

Fix FileStat stat parsing on non-GNU systems#20830
cdelafuente-r7 merged 1 commit intorapid7:masterfrom
SilentSobs:fix-filestat-non-gnu

Conversation

@SilentSobs
Copy link
Copy Markdown
Contributor

This PR fixes a portability issue in Msf::Post::File.stat where the implementation assumed GNU stat --format output.

On non-GNU systems (such as BSD variants), the existing code raised a runtime exception because the stat output did not match the expected format. This change preserves existing GNU/Linux behavior and adds a minimal, behavior-based fallback to a BSD-compatible stat -f format when the initial output does not match expectations.

No OS detection is introduced, and no external interfaces are changed.

Fixes #20030

@SilentSobs SilentSobs changed the title Fix FileStat stat parsing on non-GNU systems #20030 Fix FileStat stat parsing on non-GNU systems Dec 29, 2025
@bcoles bcoles added the bug label Dec 30, 2025
@cdelafuente-r7 cdelafuente-r7 moved this from Todo to In Progress in Metasploit Kanban Jan 14, 2026
@cdelafuente-r7 cdelafuente-r7 self-assigned this Jan 14, 2026
Copy link
Copy Markdown
Contributor

@cdelafuente-r7 cdelafuente-r7 left a comment

Choose a reason for hiding this comment

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

Thanks @SilentSobs for fixing this. I just left a minor suggestion before it lands. I tested against FreeBSD v14.3 and the new stat format works.

Comment thread lib/msf/core/post/file.rb Outdated
).to_s.chomp
end

raise 'format argument of stat command not behaving as expected' unless data =~ /(\d+,){12}\w+/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is another problem that was highlighted in the original issue. This line is likely the cause of this.

Even if the format is good, the call can still return an error, for example, if the file doesn't exist. The current error message is confusing since it refers to the format arguments. I would suggest to change this error message and give more feedback to the user:

Suggested change
raise 'format argument of stat command not behaving as expected' unless data =~ /(\d+,){12}\w+/
raise "stat command returned an unexpected result: #{data}" unless data =~ /(\d+,){12}\w+/

@cdelafuente-r7 cdelafuente-r7 moved this from In Progress to Waiting on Contributor in Metasploit Kanban Jan 15, 2026
@cdelafuente-r7
Copy link
Copy Markdown
Contributor

Hello! I'm wondering if you have any updates on this. I would be happy to help if you have questions or blockers.

@SilentSobs
Copy link
Copy Markdown
Contributor Author

Sorry I making a update .. give me hour

@SilentSobs
Copy link
Copy Markdown
Contributor Author

SilentSobs commented Feb 24, 2026

I'm looking to contribute more to Metasploit. Are there any open issues or areas where a bigger contribution would be helpful? I'm comfortable with Ruby, post modules, and security research

@cdelafuente-r7
Copy link
Copy Markdown
Contributor

Thanks for updating this @SilentSobs ! Everything looks good to me now. I tested against FreeBSD v14.3 and verified the stat format doesn't break anymore. It returns the expected format. I'll go ahead and land it.

That's a great news! I will be happy to help you for any contribution to Metasploit. We can have this discussion on the public Metasploit Slack. Look for @n00tmeg - R7, it's me.

@github-project-automation github-project-automation Bot moved this from Waiting on Contributor to In Progress in Metasploit Kanban Feb 26, 2026
@cdelafuente-r7 cdelafuente-r7 merged commit e60f77a into rapid7:master Feb 27, 2026
47 of 48 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Metasploit Kanban Feb 27, 2026
@cdelafuente-r7
Copy link
Copy Markdown
Contributor

Release Notes

This fixes a portability issue in Msf::Post::File.stat where the code incorrectly assumed a GNU stat output format.

@cdelafuente-r7 cdelafuente-r7 added the rn-fix release notes fix label Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug rn-fix release notes fix

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Msf::Post::File: format argument of stat command not behaving as expected

3 participants