Skip to content

Commit 10aa3aa

Browse files
author
Dominic Cleal
committed
(#15049) Return only one selinuxfs path as string from mounts
The block that parses /proc/self/mountinfo to find a selinuxfs filesystem would return results as an array. On Ruby 1.8, interpolating this into a string for File.exists? when one result was returned worked, while on Ruby 1.9 it interpolated as ["/sys/fs/selinux"]/enforce so later failed. This changes the block to return the single result string rather than an array. This also fixes #11531 where multiple selinuxfs filesystems could be mounted, as it returns only the first mountpoint. The /proc file was changed from /proc/self/mountinfo to /proc/self/mounts for compatibility with Linux 2.6.25 and older.
1 parent f63dc76 commit 10aa3aa

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

lib/facter/selinux.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
# This supports the fact that the selinux mount point is not always in the
1616
# same location -- the selinux mount point is operating system specific.
1717
def selinux_mount_point
18-
if FileTest.exists?('/proc/self/mountinfo')
19-
File.open('/proc/self/mountinfo') do |f|
18+
path = "/selinux"
19+
if FileTest.exists?('/proc/self/mounts')
20+
File.open('/proc/self/mounts') do |f|
2021
f.grep(/selinuxfs/) do |line|
21-
line.split[4]
22+
path = line.split[1]
23+
break
2224
end
2325
end
24-
else
25-
"/selinux"
2626
end
27+
path
2728
end
2829

2930
Facter.add("selinux") do

spec/unit/selinux_spec.rb

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,58 @@
99
Facter.clear
1010
end
1111

12-
it "should return true if SELinux enabled" do
13-
Facter.fact(:kernel).stubs(:value).returns("Linux")
12+
describe "should detect if SELinux is enabled" do
13+
it "and return true with default /selinux" do
14+
Facter.fact(:kernel).stubs(:value).returns("Linux")
1415

15-
FileTest.stubs(:exists?).returns false
16-
File.stubs(:read).with("/proc/self/attr/current").returns("notkernel")
16+
FileTest.stubs(:exists?).returns false
17+
File.stubs(:read).with("/proc/self/attr/current").returns("notkernel")
1718

18-
FileTest.expects(:exists?).with("/selinux/enforce").returns true
19-
FileTest.expects(:exists?).with("/proc/self/attr/current").returns true
20-
File.expects(:read).with("/proc/self/attr/current").returns("kernel")
19+
FileTest.expects(:exists?).with("/selinux/enforce").returns true
20+
FileTest.expects(:exists?).with("/proc/self/attr/current").returns true
21+
File.expects(:read).with("/proc/self/attr/current").returns("kernel")
22+
23+
Facter.fact(:selinux).value.should == "true"
24+
end
25+
26+
it "and return true with selinuxfs path from /proc" do
27+
Facter.fact(:kernel).stubs(:value).returns("Linux")
28+
29+
mounts = mock()
30+
lines = [ "selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0" ]
31+
mounts.expects(:grep).multiple_yields(*lines)
32+
33+
FileTest.expects(:exists?).with("/proc/self/mounts").returns true
34+
File.expects(:open).with("/proc/self/mounts").yields(mounts)
35+
36+
FileTest.expects(:exists?).with("/sys/fs/selinux/enforce").returns true
37+
38+
FileTest.expects(:exists?).with("/proc/self/attr/current").returns true
39+
File.expects(:read).with("/proc/self/attr/current").returns("kernel")
40+
41+
Facter.fact(:selinux).value.should == "true"
42+
end
43+
44+
it "and return true with multiple selinuxfs mounts from /proc" do
45+
Facter.fact(:kernel).stubs(:value).returns("Linux")
46+
47+
mounts = mock()
48+
lines = [
49+
"selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0",
50+
"selinuxfs /var/tmp/imgcreate-R2wmE6/install_root/sys/fs/selinux selinuxfs rw,relatime 0 0",
51+
]
52+
mounts.expects(:grep).multiple_yields(*lines)
53+
54+
FileTest.expects(:exists?).with("/proc/self/mounts").returns true
55+
File.expects(:open).with("/proc/self/mounts").yields(mounts)
56+
57+
FileTest.expects(:exists?).with("/sys/fs/selinux/enforce").returns true
58+
59+
FileTest.expects(:exists?).with("/proc/self/attr/current").returns true
60+
File.expects(:read).with("/proc/self/attr/current").returns("kernel")
2161

22-
Facter.fact(:selinux).value.should == "true"
62+
Facter.fact(:selinux).value.should == "true"
63+
end
2364
end
2465

2566
it "should return true if SELinux policy enabled" do
@@ -36,7 +77,7 @@
3677

3778
it "should return an SELinux policy version" do
3879
Facter.fact(:selinux).stubs(:value).returns("true")
39-
FileTest.stubs(:exists?).with("/proc/self/mountinfo").returns false
80+
FileTest.stubs(:exists?).with("/proc/self/mounts").returns false
4081

4182
File.stubs(:read).with("/selinux/policyvers").returns("")
4283

0 commit comments

Comments
 (0)