Unintentional file creation caused at Tempfile with directory traversal
Ruby's Tempfile class did not sanitise the basename argument, so a value containing directory traversal sequences could cause temporary files to be created outside the intended temporary directory. An attacker who controls that argument could write files to other locations and infer whether a given path exists from the resulting errors.
- Triaged in 56 days
- Fixed in 3 months
- Paid in 3 months
- Disclosed after 3 months
The Tempfile argument of basename can use ../ without escaping.
Therefore, directory traversal may occur and unintended files may be generated.
create file patern
[vagrant@localhost ~]$ ls .
[vagrant@localhost ~]$ irb
irb(main):001:0> require 'tempfile'
=> true
irb(main):002:0> Tempfile.open(['../../home/vagrant/', '.red'])
=> #<Tempfile:/tmp/../../home/vagrant/20180103-4697-uwqiop.red>
irb(main):003:0> `ls`
=> "20180103-4697-uwqiop.red\n"
irb(main):004:0> Tempfile.new("/../../home/vagrant/green")
=> #<Tempfile:/tmp/../../home/vagrant/green20180103-4697-1wbl81o>
irb(main):005:0> `ls`
=> "20180103-4697-uwqiop.red\ngreen20180103-4697-1wbl81o\n"
irb(main):006:0> Tempfile.create("/../../home/vagrant/blue") {|f| p f.path}
"/tmp/../../home/vagrant/blue20180103-4697-1udvlji"
=> "/tmp/../../home/vagrant/blue20180103-4697-1udvlji"
# It can not be created because suffix specifies a directory that does not exist.
irb(main):007:0> Tempfile.open(['hoge', '/../../home/vagrant/bar'])
Traceback (most recent call last):
9: from /home/vagrant/.rbenv/versions/2.5.0/bin/irb:11:in `<main>'
8: from (irb):7
7: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:291:in `open'
6: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:291:in `new'
5: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:131:in `initialize'
4: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tmpdir.rb:126:in `create'
3: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:133:in `block in initialize'
2: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:133:in `open'
1: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:133:in `initialize'
Errno::ENOENT (No such file or directory @ rb_sysopen - /tmp/hoge20180103-4697-utss0s/../../home/vagrant/bar)
If the file exists
[vagrant@localhost ~]$ ls
test
[vagrant@localhost ~]$ irb
irb(main):001:0> require 'tempfile'
=> true
irb(main):002:0> Tempfile.new("/../../home/vagrant/test/xxx")
Traceback (most recent call last):
8: from /home/vagrant/.rbenv/versions/2.5.0/bin/irb:11:in `<main>'
7: from (irb):2
6: from (irb):2:in `new'
5: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:131:in `initialize'
4: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tmpdir.rb:126:in `create'
3: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:133:in `block in initialize'
2: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:133:in `open'
1: from /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tempfile.rb:133:in `initialize'
Errno::ENOTDIR (Not a directory @ rb_sysopen - /tmp/../../home/vagrant/test/xxx20180103-4783-1f4l2ox)
Impact
An unintended file may be generated in places other than the assumed directory. It is possible to confirm the existence of the file by using the occurrence or not a directory error.
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 basename passed to Tempfile was joined to the temporary directory without rejecting or normalising path traversal sequences, so '../' segments escaped the intended directory.
Impact
An unintended file may be created outside the assumed directory, and the presence or absence of a directory error could let an attacker confirm whether a path exists.
What the attacker needed
Attacker controls the Tempfile basename; code runs with write access to target path
How it was found
- Start an interactive Ruby session and load the tempfile library.
- Call Tempfile.open with a basename containing '../' segments pointing at another directory.
- List the current directory to see the file was created outside the temporary folder.
- Call Tempfile.new and Tempfile.create with similar traversal basenames and observe the resulting paths.
- Call Tempfile.new with a basename whose parent path is an existing file to trigger a not-a-directory 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 temporary file is a scratch file a program creates and later deletes. Ruby's Tempfile normally puts these in a safe temporary folder, but if the name you give it contains '../' (which means 'go up one folder'), the file can end up somewhere else on the disk. That lets someone who controls the name write files in unexpected places, or learn whether a folder exists by watching for errors.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.