Skip to content

Commit ab4cce4

Browse files
committed
Rename IoctlSet to XpermSet
The same class can be used for both ioctl and nlmsg extended permissions. Rename the current class and mark IoctlSet as deprecated. Signed-off-by: Thiébaud Weksteen <tweek@google.com>
1 parent e0e687f commit ab4cce4

9 files changed

Lines changed: 102 additions & 91 deletions

File tree

setools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
IoctlSet, Iomemcon, IomemconRange, Ioportcon, IoportconRange, Level, LevelDecl, MLSRule, \
2727
Netifcon, Nodecon, ObjClass, Pcidevicecon, Pirqcon, PolicyCapability, Portcon, PortconRange, \
2828
Range, Role, RoleAllow, RoleTransition, Sensitivity, TERule, TruthTableRow, Type, \
29-
TypeAttribute, User, Validatetrans
29+
TypeAttribute, User, Validatetrans, XpermSet
3030

3131
# Exceptions
3232
from . import exception

setools/diff/terules.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class ModifiedAVRuleXperm(DifferenceResult):
4141
"""Difference details for a modified access vector rule."""
4242

4343
rule: policyrep.AVRuleXperm
44-
added_perms: policyrep.IoctlSet
45-
removed_perms: policyrep.IoctlSet
46-
matched_perms: policyrep.IoctlSet
44+
added_perms: policyrep.XpermSet
45+
removed_perms: policyrep.XpermSet
46+
matched_perms: policyrep.XpermSet
4747

4848

4949
@dataclass(frozen=True, order=True)
@@ -365,9 +365,9 @@ def diff(self) -> None:
365365
if added_perms or removed_perms:
366366
modified.append(
367367
ModifiedAVRuleXperm(left_rule.origin,
368-
policyrep.IoctlSet(added_perms),
369-
policyrep.IoctlSet(removed_perms),
370-
policyrep.IoctlSet(p[0] for p in matched_perms)))
368+
policyrep.XpermSet(added_perms),
369+
policyrep.XpermSet(removed_perms),
370+
policyrep.XpermSet(p[0] for p in matched_perms)))
371371

372372
setattr(self, f"added_{ruletype}s", set(a.origin for a in added))
373373
setattr(self, f"removed_{ruletype}s", set(r.origin for r in removed))

setools/policyrep.pyi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PolicyRule(PolicyObject):
4343
target: "PolicySymbol" = ...
4444
tclass: "ObjClass" = ...
4545
xperm_type: str = ...
46-
perms: frozenset[str] | "IoctlSet" = ...
46+
perms: frozenset[str] | "XpermSet" = ...
4747
default: PolicyObject = ...
4848
filename: str = ...
4949
def enabled(self, **kwargs) -> bool: ...
@@ -101,7 +101,7 @@ class AVRule(BaseTERule):
101101

102102
class AVRuleXperm(BaseTERule):
103103
default: NoReturn = ...
104-
perms: "IoctlSet" = ...
104+
perms: "XpermSet" = ...
105105
xperm_type: str = ...
106106
def expand(self, *args, **kwargs) -> Iterable["AVRuleXperm"]: ...
107107

@@ -247,9 +247,11 @@ class IbpkeyconRange:
247247
class InitialSID(Ocontext):
248248
name: str = ...
249249

250-
class IoctlSet(frozenset[int]):
250+
class XpermSet(frozenset[int]):
251251
def ranges(self) -> int: ...
252252

253+
class IoctlSet(XpermSet): ...
254+
253255
class Iomemcon(Ocontext):
254256
addr: "IomemconRange" = ...
255257

setools/policyrep/terule.pxi

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ cdef class AVRule(BaseTERule):
213213
return self.rule_string
214214

215215

216-
cdef class IoctlSet(frozenset):
216+
cdef class XpermSet(frozenset):
217217

218218
"""
219219
A set with overridden string functions which compresses
220-
the output into ioctl ranges instead of individual elements.
220+
the output into ioctl/nlmsg ranges instead of individual elements.
221221
"""
222222

223223
def __format__(self, spec):
@@ -249,7 +249,7 @@ cdef class IoctlSet(frozenset):
249249
elif spec == ",":
250250
return ", ".join(shortlist)
251251
else:
252-
return super(IoctlSet, self).__format__(spec)
252+
return super().__format__(spec)
253253

254254
def __str__(self):
255255
return f"{self}"
@@ -267,12 +267,20 @@ cdef class IoctlSet(frozenset):
267267
sorted(self), key=lambda k, c=itertools.count(): k - next(c)))
268268

269269

270+
cdef class IoctlSet(XpermSet):
271+
272+
def __init__(self, *args, **kwargs):
273+
log = logging.getLogger(__name__)
274+
log.warning("IoctlSet is deprecated, use XpermSet instead.")
275+
super().__init__(*args, **kwargs)
276+
277+
270278
cdef class AVRuleXperm(BaseTERule):
271279

272280
"""An extended permission access vector type enforcement rule."""
273281

274282
cdef:
275-
readonly IoctlSet perms
283+
readonly XpermSet perms
276284
readonly str xperm_type
277285

278286
@staticmethod
@@ -322,7 +330,7 @@ cdef class AVRuleXperm(BaseTERule):
322330
r.source = type_or_attr_factory(policy, policy.type_value_to_datum(key.source_type - 1))
323331
r.target = type_or_attr_factory(policy, policy.type_value_to_datum(key.target_type - 1))
324332
r.tclass = ObjClass.factory(policy, policy.class_value_to_datum(key.target_class - 1))
325-
r.perms = IoctlSet(perms)
333+
r.perms = XpermSet(perms)
326334
r.extended = True
327335
r.xperm_type = xperm_type
328336
r._conditional = conditional

setools/terulequery.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ class TERuleQuery(mixins.MatchObjClass, mixins.MatchPermission, query.PolicyQuer
8080
boolean = CriteriaSetDescriptor[policyrep.Boolean]("boolean_regex", "lookup_boolean")
8181
boolean_regex: bool = False
8282
boolean_equal: bool = False
83-
_xperms: policyrep.IoctlSet | None = None
83+
_xperms: policyrep.XpermSet | None = None
8484
xperms_equal: bool = False
8585

8686
@property
87-
def xperms(self) -> policyrep.IoctlSet | None:
87+
def xperms(self) -> policyrep.XpermSet | None:
8888
return self._xperms
8989

9090
@xperms.setter
@@ -104,7 +104,7 @@ def xperms(self, value: Iterable[tuple[int, int]] | None) -> None:
104104

105105
pending_xperms.update(i for i in range(low, high + 1))
106106

107-
self._xperms = policyrep.IoctlSet(pending_xperms)
107+
self._xperms = policyrep.XpermSet(pending_xperms)
108108
else:
109109
self._xperms = None
110110

tests/library/policyrep/test_rules.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RuleTestCase:
2525
type_: type # the rule's policyrep class
2626
tclass: str | None = None
2727
xperm: str | None = None
28-
perms: set[str] | setools.IoctlSet | None = None
28+
perms: set[str] | setools.XpermSet | None = None
2929
default: str | None = None
3030
filename: str | None = None
3131
conditional: str | None = None
@@ -57,10 +57,10 @@ class RuleTestCase:
5757
default="system", type_=setools.TERule, conditional="a_bool",
5858
statement="type_change type31c type31b:infoflow2 system; [ a_bool ]:False"),
5959
RuleTestCase(setools.TERuletype.allowxperm, "type30", "type31a", tclass="infoflow",
60-
xperm="ioctl", perms=setools.IoctlSet((0x00ff,)), type_=setools.AVRuleXperm,
60+
xperm="ioctl", perms=setools.XpermSet((0x00ff,)), type_=setools.AVRuleXperm,
6161
statement="allowxperm type30 type31a:infoflow ioctl 0x00ff;"),
6262
RuleTestCase(setools.TERuletype.auditallowxperm, "type31a", "type31b", tclass="infoflow",
63-
xperm="ioctl", perms=setools.IoctlSet((1, 2, 3)), type_=setools.AVRuleXperm,
63+
xperm="ioctl", perms=setools.XpermSet((1, 2, 3)), type_=setools.AVRuleXperm,
6464
statement="auditallowxperm type31a type31b:infoflow ioctl 0x0001-0x0003;")]
6565

6666

@@ -213,5 +213,5 @@ def test_regression(self, compiled_policy: setools.SELinuxPolicy):
213213
# expect 2 rules:
214214
# allowxperm init_type_t init_type_t : unix_dgram_socket ioctl { 0x8910 };
215215
# allowxperm init_type_t init_type_t : unix_dgram_socket ioctl { 0x0-0xff };
216-
assert setools.IoctlSet(range(0x100)) == rules[0].perms, f"{rules[0].perms}"
217-
assert setools.IoctlSet([0x8910]) == rules[1].perms, f"{rules[1].perms}"
216+
assert setools.XpermSet(range(0x100)) == rules[0].perms, f"{rules[0].perms}"
217+
assert setools.XpermSet([0x8910]) == rules[1].perms, f"{rules[1].perms}"

0 commit comments

Comments
 (0)