Forum: Ruby-core [ruby-trunk - Bug #7879][Open] File.readable? fails when ruby runs as root

Posted by Felipe Balbi (felipebalbi)
on 2013-02-18 18:45
(Received via mailing list)
Issue #7879 has been reported by balbi (Feliple Balbi).

----------------------------------------
Bug #7879: File.readable? fails when ruby runs as root
https://bugs.ruby-lang.org/issues/7879

Author: balbi (Feliple Balbi)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]


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.
Posted by ko1 (Koichi Sasada) (Guest)
on 2013-02-18 23:27
(Received via mailing list)
Issue #7879 has been updated by ko1 (Koichi Sasada).

Category set to core
Assignee set to nobu (Nobuyoshi Nakada)
Target version set to 2.1.0


----------------------------------------
Bug #7879: File.readable? fails when ruby runs as root
https://bugs.ruby-lang.org/issues/7879#change-36571

Author: balbi (Feliple Balbi)
Status: Open
Priority: Normal
Assignee: nobu (Nobuyoshi Nakada)
Category: core
Target version: 2.1.0
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]


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.
Posted by kosaki (Motohiro KOSAKI) (Guest)
on 2013-02-19 03:39
(Received via mailing list)
Issue #7879 has been updated by kosaki (Motohiro KOSAKI).

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.
----------------------------------------
Bug #7879: File.readable? fails when ruby runs as root
https://bugs.ruby-lang.org/issues/7879#change-36583

Author: balbi (Feliple Balbi)
Status: Rejected
Priority: Normal
Assignee: nobu (Nobuyoshi Nakada)
Category: core
Target version: 2.1.0
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]


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.
Posted by mame (Yusuke Endoh) (Guest)
on 2013-02-20 07:28
(Received via mailing list)
Issue #7879 has been updated by mame (Yusuke Endoh).


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>

----------------------------------------
Bug #7879: File.readable? fails when ruby runs as root
https://bugs.ruby-lang.org/issues/7879#change-36653

Author: balbi (Feliple Balbi)
Status: Rejected
Priority: Normal
Assignee: nobu (Nobuyoshi Nakada)
Category: core
Target version: 2.1.0
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]


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.
Posted by KOSAKI Motohiro (Guest)
on 2013-02-20 07:33
(Received via mailing list)
>> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.