Null character at fnmatch
Ruby's fnmatch and fnmatch? can return true for a shorter string when the pattern contains a null byte, which may lead to unintended file operations.
- Disclosed after 11 months
I confirmed that it will behave unintentionally when null characters are entered in patterns with fnmatch,fnmatch?.
$ ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin16]
$ irb
irb(main):001:0> require 'pathname'
=> true
# should not be true
irb(main):002:0> File.fnmatch("x\0yz", 'x')
=> true
irb(main):003:0> File.fnmatch?("abc\0", 'abc')
=> true
irb(main):004:0> Pathname('x').fnmatch("x\0yz")
=> true
irb(main):005:0> Pathname('abc').fnmatch?("abc\0")
=> true
# It does not work as a delimiter
irb(main):006:0> File.fnmatch("x\0yz", 'yz')
=> false
irb(main):007:0> Pathname('yz').fnmatch("x\0yz")
=> false
# error
irb(main):008:0> File.fnmatch('x', "x\0")
Traceback (most recent call last):
3: from /usr/local/bin/irb:11:in `<main>'
2: from (irb):8
1: from (irb):8:in `fnmatch'
ArgumentError (path name contains null byte)
irb(main):009:0> Pathname("x\0").fnmatch('x')
Traceback (most recent call last):
4: from /usr/local/bin/irb:11:in `<main>'
3: from (irb):9
2: from (irb):9:in `Pathname'
1: from (irb):9:in `initialize'
ArgumentError (pathname contains null byte)
Impact
When using these methods, unintended file operation may be performed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the pattern string is treated as a C-style string that stops at the first null byte, so matching ignores everything after it.
Impact
Code that relies on these methods for file matching may perform unintended file operations when a pattern contains a null byte.
What the attacker needed
Pattern contains a null byte
How it was found
- Open an interactive Ruby session and load the pathname library.
- Call the file matching method with a pattern that contains a null byte followed by extra characters.
- Observe that it returns true for a shorter string that should not match.
- Call the same method with a null byte in the path argument instead and observe an error.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A null byte is a zero character. Ruby's file-name matching can return true for a shorter name when the pattern contains a null byte, so the wrong file could be chosen.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.