Bug #7879
closedFile.readable? fails when ruby runs as root
Description
I have the following script to find sysfs (linux) files which are readable but throw an exception when read:
Dir.glob("/sys/**/*").each do |file|
next if File.directory?(file)
if File.world_writable?(file)
puts "#{file} is world-writable"
end
if File.readable?(file)
begin
File.open(file) { |f|
result = f.readline
}
rescue EOFError
nil
rescue => e
puts e.message
end
end
end
If I run as a normal user, it works just fine, but when I run as root, even for files which are write only (chmod 200), File.readable?(f) will return true which makes my script fail. I have also tried File.readable_real? but it has the same outcome.
Updated by ko1 (Koichi Sasada) almost 12 years ago
- Category set to core
- Assignee set to nobu (Nobuyoshi Nakada)
- Target version set to 2.1.0
Updated by kosaki (Motohiro KOSAKI) almost 12 years ago
- Status changed from Open to Rejected
When run as root, access(2) ignore permission and always return success. That's UNIX filesystem semantics.
In the other words, this is completely your script bug.
You need specific concern when opening /proc, /sys and similar pseudo filesystem's file.
Updated by mame (Yusuke Endoh) almost 12 years ago
kosaki (Motohiro KOSAKI) wrote:
When run as root, access(2) ignore permission and always return success. That's UNIX filesystem semantics.
In the other words, this is completely your script bug.
Wow...
You need specific concern when opening /proc, /sys and similar pseudo filesystem's file.
Out of curiosity, what should we concern concretely? Should we assume File.readable? as a hint?
--
Yusuke Endoh mame@tsg.ne.jp
Updated by kosaki (Motohiro KOSAKI) almost 12 years ago
You need specific concern when opening /proc, /sys and similar pseudo filesystem's file.
Out of curiosity, what should we concern concretely? Should we assume File.readable? as a hint?
Pseudo filesystem hae several exceptional rule. this is one of them.
So, yes, when you can touch pseudo filesystems, I don't recommend to
use File.readable?. This is OS limitation and we can not fix them.