Skip to content

Commit

Permalink
q-dev: deny list drop ins and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 15, 2024
1 parent 63489c1 commit 97084d6
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions qubes/ext/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
import importlib
import os

import qubes.api
import qubes.api.internal
Expand Down Expand Up @@ -177,11 +178,35 @@ def on_device_attach(

# load device deny list
deny = {}
AdminExtension._load_deny_list(deny, DEVICE_DENY_LIST)

# load drop ins
drop_in_path = DEVICE_DENY_LIST + '.d'
if os.path.isdir(drop_in_path):
for deny_list_name in os.listdir(drop_in_path):
deny_list_path = os.path.join(drop_in_path, deny_list_name)

if os.path.isfile(deny_list_path):
AdminExtension._load_deny_list(deny, deny_list_path)

# check if any presented interface is on deny list
for interface in deny.get(dest.name, set()):
pattern = DeviceInterface(interface)
for devint in device.interfaces:
if pattern.matches(devint):
raise qubes.exc.PermissionDenied()

@staticmethod
def _load_deny_list(deny: dict, path: str) -> None:
try:
with open(DEVICE_DENY_LIST, 'r', encoding="utf-8") as file:
with open(path, 'r', encoding="utf-8") as file:
for line in file:
line = line.strip()

# skip comments
if line.startswith('#'):
continue

if line:
name, *values = line.split()

Expand All @@ -191,10 +216,3 @@ def on_device_attach(
deny[name] = deny.get(name, set()).union(set(values))
except IOError:
pass

# check if any presented interface is on deny list
for interface in deny.get(dest.name, set()):
pattern = DeviceInterface(interface)
for devint in device.interfaces:
if pattern.matches(devint):
raise qubes.exc.PermissionDenied()

0 comments on commit 97084d6

Please sign in to comment.