Skip to content

Commit addc29e

Browse files
[Spyre] change in vfio device access permissions
Changed the vfio device access permission such that only root and group users will have access to the device. Signed-off-by: Sahithi Ravindranath <[email protected]>
1 parent 2a57a3a commit addc29e

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

servicereportpkg/repair/plugins/spyre_repair.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
import os
10+
import grp
1011
import stat
1112
import re
1213
import shutil
@@ -117,14 +118,25 @@ def fix_vfio_perm_check(self, plugin_obj, vfio_device_permission_check):
117118
"""Fix VFIO device permission"""
118119

119120
vfio_dir = "/dev/vfio/"
121+
group_name = 'sentient'
122+
try:
123+
gid = grp.getgrnam(group_name).gr_gid
124+
except Exception as e:
125+
self.log.error("Failed to get groupid of group: %s", group_name)
126+
vfio_device_permission_check.set_note(Notes.FAIL_TO_FIX)
127+
return
128+
120129
for name in os.listdir(vfio_dir):
121130
full_path = vfio_dir + name
122131
try:
123132
mode = os.stat(full_path).st_mode
124133
if stat.S_ISCHR(mode):
125-
os.chmod(full_path, 0o666)
134+
os.chmod(full_path, 0o660)
135+
if os.stat(full_path).st_gid != gid:
136+
os.chown(full_path, -1, gid)
137+
126138
except Exception as e:
127-
self.log.error("Failed to %s file permission to 0o666", full_path)
139+
self.log.error("Failed to set %s file permission to 0o660", full_path)
128140

129141
re_check = plugin_obj.check_vfio_access_permission()
130142
if re_check.get_status():
@@ -297,10 +309,10 @@ def repair(self, plugin_obj, checks):
297309
vfio_kernel_mod_check.set_note(Notes.FAIL_TO_FIX)
298310

299311
vfio_device_permission_check = check_dir["VFIO device permission"]
300-
if vfio_device_permission_check.get_status() is False:
301-
self.fix_vfio_perm_check(plugin_obj, vfio_device_permission_check)
302-
elif vfio_device_permission_check.get_status() is None:
312+
if user_group_conf_check.get_status() is not True or vfio_device_permission_check.get_status() is None:
303313
vfio_device_permission_check.set_note(Notes.NOT_FIXABLE)
314+
elif vfio_device_permission_check.get_status() is False:
315+
self.fix_vfio_perm_check(plugin_obj, vfio_device_permission_check)
304316

305317
sos_package_check = check_dir["sos package"]
306318
if sos_package_check.get_status() is False:

servicereportpkg/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,36 @@ def is_read_write_to_all_users(file_path):
404404
log.debug("File %s not found.", file_path)
405405
return False
406406

407+
def is_read_write_to_owner_group_users(file_path):
408+
"""
409+
Check if a file has read and write permissions for owner,
410+
group only
411+
412+
Args:
413+
file_path (str): The full path to the file.
414+
415+
Returns:
416+
bool: True if users (owner, group) have both read and write
417+
permissions, False otherwise. Also returns False if the file does
418+
not exist.
419+
"""
420+
421+
log = get_default_logger()
422+
423+
try:
424+
mode = os.stat(file_path).st_mode
425+
return (
426+
not(bool(mode & stat.S_IROTH) and # Read permission for others
427+
bool(mode & stat.S_IWOTH)) and # Write permission for others
428+
bool(mode & stat.S_IRUSR) and # Read permission for owner
429+
bool(mode & stat.S_IWUSR) and # Write permission for owner
430+
bool(mode & stat.S_IRGRP) and # Read permission for group
431+
bool(mode & stat.S_IWGRP) # Write permission for group
432+
)
433+
except FileNotFoundError:
434+
log.debug("File %s not found.", file_path)
435+
return False
436+
407437

408438
def append_to_file(file_path, s):
409439
"""Append the given stirng to the file"""

servicereportpkg/validate/plugins/spyre.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""Plugin to check spyre configuration"""
77

88
import os
9+
import grp
910
import re
1011
import stat
1112
import pyudev
@@ -19,6 +20,7 @@
1920
from servicereportpkg.utils import is_package_installed
2021
from servicereportpkg.check import ConfigurationFileCheck
2122
from servicereportpkg.utils import is_read_write_to_all_users
23+
from servicereportpkg.utils import is_read_write_to_owner_group_users
2224

2325

2426
class Spyre(Plugin, Scheme):
@@ -105,7 +107,7 @@ def check_driver_config(self):
105107
def check_udev_rule(self):
106108
"""VFIO udev rules configuration"""
107109

108-
vfio_udev = "SUBSYSTEM==\"vfio\", MODE=\"0666\""
110+
vfio_udev = "SUBSYSTEM==\"vfio\", GROUP=\"sentient\", MODE=\"0660\""
109111
config_file = "/etc/udev/rules.d/95-vfio-3.rules"
110112

111113
conf_check = ConfigurationFileCheck(self.check_udev_rule.__doc__,
@@ -248,14 +250,26 @@ def check_vfio_access_permission(self):
248250

249251
if not os.path.isdir(vfio_dir):
250252
self.log.error("No %s directory", vfio_dir)
253+
perm_check.set_status(False)
254+
return perm_check
255+
256+
group_name = 'sentient'
257+
try:
258+
gid = grp.getgrnam(group_name).gr_gid
259+
except Exception as e:
260+
self.log.error("Failed to get groupid of group: %s", group_name)
261+
perm_check.set_status(False)
251262
return perm_check
252263

253264
for name in os.listdir(vfio_dir):
254265
full_path = vfio_dir + name
255266
try:
267+
ret = True
268+
if os.stat(full_path).st_gid != gid:
269+
ret = False
256270
mode = os.stat(full_path).st_mode
257271
if stat.S_ISCHR(mode):
258-
ret = is_read_write_to_all_users(full_path)
272+
ret = ret & is_read_write_to_owner_group_users(full_path)
259273
if not ret and status:
260274
status = ret
261275
perm_check.add_file(full_path, ret)

0 commit comments

Comments
 (0)