summaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/setools/policyrep/xencontext.py
blob: fe78ec76dd868a752de7a4012630300eabcbb613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Derived from netcontext.py
#
# This file is part of SETools.
#
# SETools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1 of
# the License, or (at your option) any later version.
#
# SETools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with SETools.  If not, see
# <http://www.gnu.org/licenses/>.
#
from collections import namedtuple

from . import qpol
from . import symbol
from . import context

addr_range = namedtuple("memory_range", ["low", "high"])
port_range = namedtuple("port_range", ["low", "high"])


def iomemcon_factory(policy, name):
    """Factory function for creating iomemcon objects."""

    if not isinstance(name, qpol.qpol_iomemcon_t):
        raise NotImplementedError

    return Iomemcon(policy, name)


def ioportcon_factory(policy, name):
    """Factory function for creating ioportcon objects."""

    if not isinstance(name, qpol.qpol_ioportcon_t):
        raise NotImplementedError

    return Ioportcon(policy, name)


def pirqcon_factory(policy, name):
    """Factory function for creating pirqcon objects."""

    if not isinstance(name, qpol.qpol_pirqcon_t):
        raise NotImplementedError

    return Pirqcon(policy, name)


def pcidevicecon_factory(policy, name):
    """Factory function for creating pcidevicecon objects."""

    if not isinstance(name, qpol.qpol_pcidevicecon_t):
        raise NotImplementedError

    return Pcidevicecon(policy, name)


def devicetreecon_factory(policy, name):
    """Factory function for creating devicetreecon objects."""

    if not isinstance(name, qpol.qpol_devicetreecon_t):
        raise NotImplementedError

    return Devicetreecon(policy, name)


class XenContext(symbol.PolicySymbol):

    """Base class for in-policy xen labeling rules."""

    def __str__(self):
        raise NotImplementedError

    @property
    def context(self):
        """The context for this statement."""
        return context.context_factory(self.policy, self.qpol_symbol.context(self.policy))

    def statement(self):
        return str(self)


class Iomemcon(XenContext):

    """A iomemcon statement."""

    def __str__(self):
        low, high = self.addr

        if low == high:
            return "iomemcon {0} {1}".format(low, self.context)
        else:
            return "iomemcon {0}-{1} {2}".format(low, high, self.context)

    @property
    def addr(self):
        """
        The memory range for this iomemcon.

        Return: Tuple(low, high)
        low     The low memory of the range.
        high    The high memory of the range.
        """
        low = self.qpol_symbol.low_addr(self.policy)
        high = self.qpol_symbol.high_addr(self.policy)
        return addr_range(low, high)


class Ioportcon(XenContext):

    """A ioportcon statement."""

    def __str__(self):
        low, high = self.ports

        if low == high:
            return "ioportcon {0} {1}".format(low, self.context)
        else:
            return "ioportcon {0}-{1} {2}".format(low, high, self.context)

    @property
    def ports(self):
        """
        The port range for this ioportcon.

        Return: Tuple(low, high)
        low     The low port of the range.
        high    The high port of the range.
        """

        low = self.qpol_symbol.low_port(self.policy)
        high = self.qpol_symbol.high_port(self.policy)
        return port_range(low, high)


class Pcidevicecon(XenContext):

    """A pcidevicecon statement."""

    def __str__(self):
        return "pcidevicecon {0.device} {0.context}".format(self)

    @property
    def device(self):
        """
        The device for this pcidevicecon.

        Return: The PCI device ID.
        """
        return self.qpol_symbol.device(self.policy)


class Pirqcon(XenContext):

    """A pirqcon statement."""

    def __str__(self):
        return "pirqcon {0.irq} {0.context}".format(self)

    @property
    def irq(self):
        """
        The irq for this pirqcon.

        Return: The irq.
        """
        return self.qpol_symbol.irq(self.policy)


class Devicetreecon(XenContext):

    """A devicetreecon statement."""

    def __str__(self):
        return "devicetreecon {0.path} {0.context}".format(self)

    @property
    def path(self):
        """
        The path for this devicetreecon.

        Return: The device path name.
        """
        return self.qpol_symbol.path(self.policy)