Skip to content

Commit

Permalink
Do not allow "root" user to access DBus APIs if it does not have CAP_…
Browse files Browse the repository at this point in the history
…SYSADMIN

Signed-off-by: Christopher M. Cantalupo <[email protected]>
  • Loading branch information
cmcantalupo committed Oct 2, 2023
1 parent c180c9f commit bcab682
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions service/geopmdpy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,10 @@ def _get_user(self, call_info):
"""
unique_name = call_info['sender']
uid = self._dbus_proxy.GetConnectionUnixUser(unique_name)
return pwd.getpwuid(uid).pw_name
name = pwd.getpwuid(uid).pw_name
if name == 'root':
self._check_cap_sys_admin(call_info)
return name

def _get_pid(self, call_info):
"""Use DBus proxy object to derive the Linux PID of the client
Expand All @@ -1009,7 +1012,7 @@ def _get_pid(self, call_info):
unique_name = call_info['sender']
return self._dbus_proxy.GetConnectionUnixProcessID(unique_name)

def _check_cap_sys_admin(self, call_info, api_name):
def _check_cap_sys_admin(self, call_info, api_name=None):
has_admin = False
cap = 0
cap_sys_admin = 0x00200000
Expand All @@ -1020,4 +1023,8 @@ def _check_cap_sys_admin(self, call_info, api_name):
if line.startswith('CapEff:'):
cap = int(line.split(':')[1], 16)
if cap & cap_sys_admin == 0:
raise RuntimeError(f'Calling "io.github.geopm.{api_name}" failed, try with sudo or as "root" user (requires CAP_SYS_ADMIN)')
if api_name is not None:
msg = f'Calling "io.github.geopm.{api_name}" failed, try with sudo or as "root" user (requires CAP_SYS_ADMIN)'
else:
msg = f'User name is "root", but this user does not have CAP_SYS_ADMIN'
raise RuntimeError(msg)

0 comments on commit bcab682

Please sign in to comment.