Skip to content

Commit 6352feb

Browse files
authored
Merge pull request #138 from tweksteen/add_netlink_xperm
Add support for nlmsg extended permission
2 parents 1b3ce1b + fc2d3da commit 6352feb

15 files changed

Lines changed: 248 additions & 205 deletions

File tree

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [push, pull_request]
44

55
env:
66
# This should be the minimum version required to run setools:
7-
SELINUX_USERSPACE_VERSION: 3.2
7+
SELINUX_USERSPACE_VERSION: main
88

99
# GitHub doesn't support building env
1010
# vars from others in this block.

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/sepol.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ cdef extern from "<sepol/policydb/avtab.h>":
157157
#
158158
cdef int AVTAB_XPERMS_IOCTLFUNCTION
159159
cdef int AVTAB_XPERMS_IOCTLDRIVER
160+
cdef int AVTAB_XPERMS_NLMSG
160161

161162
cdef struct avtab_extended_perms:
162163
uint8_t specified
@@ -437,6 +438,7 @@ cdef extern from "<sepol/policydb/policydb.h>":
437438
#
438439
cdef int AVRULE_XPERMS_IOCTLFUNCTION
439440
cdef int AVRULE_XPERMS_IOCTLDRIVER
441+
cdef int AVRULE_XPERMS_NLMSG
440442
cdef int EXTENDED_PERMS_LEN
441443

442444
cdef struct av_extended_perms:

setools/policyrep/terule.pxi

Lines changed: 18 additions & 7 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
@@ -292,9 +300,10 @@ cdef class AVRuleXperm(BaseTERule):
292300
#
293301
for curr in range(len):
294302
if sepol.xperm_test(curr, xperms.perms):
295-
if xperms.specified & sepol.AVTAB_XPERMS_IOCTLFUNCTION:
303+
if (xperms.specified == sepol.AVTAB_XPERMS_IOCTLFUNCTION \
304+
or xperms.specified == sepol.AVTAB_XPERMS_NLMSG):
296305
perms.add(xperms.driver << 8 | curr)
297-
elif xperms.specified & sepol.AVTAB_XPERMS_IOCTLDRIVER:
306+
elif xperms.specified == sepol.AVTAB_XPERMS_IOCTLDRIVER:
298307
base_value = curr << 8
299308
perms.update(range(base_value, base_value + 0x100))
300309
else:
@@ -309,6 +318,8 @@ cdef class AVRuleXperm(BaseTERule):
309318
if datum.xperms.specified == sepol.AVTAB_XPERMS_IOCTLFUNCTION \
310319
or datum.xperms.specified == sepol.AVTAB_XPERMS_IOCTLDRIVER:
311320
xperm_type = intern("ioctl")
321+
elif datum.xperms.specified == sepol.AVTAB_XPERMS_NLMSG:
322+
xperm_type = intern("nlmsg")
312323
else:
313324
raise LowLevelPolicyError(f"Unknown extended permission: {datum.xperms.specified}")
314325

@@ -322,7 +333,7 @@ cdef class AVRuleXperm(BaseTERule):
322333
r.source = type_or_attr_factory(policy, policy.type_value_to_datum(key.source_type - 1))
323334
r.target = type_or_attr_factory(policy, policy.type_value_to_datum(key.target_type - 1))
324335
r.tclass = ObjClass.factory(policy, policy.class_value_to_datum(key.target_class - 1))
325-
r.perms = IoctlSet(perms)
336+
r.perms = XpermSet(perms)
326337
r.extended = True
327338
r.xperm_type = xperm_type
328339
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/rules.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ common infoflow
1717
low_r
1818
med_r
1919
hi_r
20-
ioctl
20+
ioctl
21+
nlmsg
2122
}
2223

2324
class infoflow
@@ -120,7 +121,7 @@ if (a_bool) {
120121
type_transition type31b system:infoflow4 type30 "the_filename";
121122

122123
allowxperm type30 type31a:infoflow ioctl 0x00ff;
123-
auditallowxperm type31a type31b:infoflow ioctl { 0x001-0x0003 };
124+
auditallowxperm type31a type31b:infoflow nlmsg { 0x001-0x0003 };
124125

125126
allow system self:infoflow hi_w;
126127
range_transition type30 system:infoflow7 s0:c1 - s2:c0.c4;

0 commit comments

Comments
 (0)