| 1 |
======= |
| 2 |
Usage |
| 3 |
======= |
| 4 |
|
| 5 |
oslo.privsep lets you define in your code specific functions that will run |
| 6 |
in predefined privilege contexts. This lets you run functions with more (or |
| 7 |
less) privileges than the rest of the code. Privsep functions live in a |
| 8 |
specific ``privsep`` submodule (for example, ``nova.privsep`` for nova). |
| 9 |
|
| 10 |
Defining a context |
| 11 |
================== |
| 12 |
|
| 13 |
Contexts are defined in the ``privsep/__init__.py`` file. For example, this |
| 14 |
defines a sys_admin_pctxt with ``CAP_CHOWN``, ``CAP_DAC_OVERRIDE``, |
| 15 |
``CAP_DAC_READ_SEARCH``, ``CAP_FOWNER``, ``CAP_NET_ADMIN``, and |
| 16 |
``CAP_SYS_ADMIN`` rights (equivalent to ``sudo`` rights):: |
| 17 |
|
| 18 |
from oslo_privsep import capabilities |
| 19 |
from oslo_privsep import priv_context |
| 20 |
|
| 21 |
sys_admin_pctxt = priv_context.PrivContext( |
| 22 |
'nova', |
| 23 |
cfg_section='nova_sys_admin', |
| 24 |
pypath=__name__ + '.sys_admin_pctxt', |
| 25 |
capabilities=[capabilities.CAP_CHOWN, |
| 26 |
capabilities.CAP_DAC_OVERRIDE, |
| 27 |
capabilities.CAP_DAC_READ_SEARCH, |
| 28 |
capabilities.CAP_FOWNER, |
| 29 |
capabilities.CAP_NET_ADMIN, |
| 30 |
capabilities.CAP_SYS_ADMIN], |
| 31 |
) |
| 32 |
|
| 33 |
Defining a context with timeout |
| 34 |
------------------------------- |
| 35 |
|
| 36 |
It is possible to initialize PrivContext with timeout:: |
| 37 |
|
| 38 |
from oslo_privsep import capabilities |
| 39 |
from oslo_privsep import priv_context |
| 40 |
|
| 41 |
dhcp_release_cmd = priv_context.PrivContext( |
| 42 |
__name__, |
| 43 |
cfg_section='privsep_dhcp_release', |
| 44 |
pypath=__name__ + '.dhcp_release_cmd', |
| 45 |
capabilities=[caps.CAP_SYS_ADMIN, |
| 46 |
caps.CAP_NET_ADMIN], |
| 47 |
timeout=5 |
| 48 |
) |
| 49 |
|
| 50 |
``PrivsepTimeout`` is raised if timeout is reached. |
| 51 |
|
| 52 |
.. warning:: |
| 53 |
|
| 54 |
The daemon (the root process) task won't stop when timeout |
| 55 |
is reached. That means we'll have less available threads if the related |
| 56 |
thread never finishes. |
| 57 |
|
| 58 |
Defining a privileged function |
| 59 |
============================== |
| 60 |
|
| 61 |
Functions are defined in files under the ``privsep/`` subdirectory, for |
| 62 |
example in a ``privsep/motd.py`` file for functions touching the MOTD file. |
| 63 |
They make use of a decorator pointing to the context we defined above:: |
| 64 |
|
| 65 |
import nova.privsep |
| 66 |
|
| 67 |
@nova.privsep.sys_admin_pctxt.entrypoint |
| 68 |
def update_motd(message): |
| 69 |
with open('/etc/motd', 'w') as f: |
| 70 |
f.write(message) |
| 71 |
|
| 72 |
Privileged functions must be as simple, specialized and narrow as possible, |
| 73 |
so as to prevent further escalation. In this example, ``update_motd(message)`` |
| 74 |
is narrow: it only allows the service to overwrite the MOTD file. If a more |
| 75 |
generic ``update_file(filename, content)`` was created, it could be used to |
| 76 |
overwrite any file in the filesystem, allowing easy escalation to root |
| 77 |
rights. That would defeat the whole purpose of oslo.privsep. |
| 78 |
|
| 79 |
Defining a privileged function with timeout |
| 80 |
------------------------------------------- |
| 81 |
|
| 82 |
It is possible to use ``entrypoint_with_timeout`` decorator:: |
| 83 |
|
| 84 |
from oslo_privsep import daemon |
| 85 |
|
| 86 |
from neutron import privileged |
| 87 |
|
| 88 |
@privileged.default.entrypoint_with_timeout(timeout=5) |
| 89 |
def get_link_devices(namespace, **kwargs): |
| 90 |
try: |
| 91 |
with get_iproute(namespace) as ip: |
| 92 |
return make_serializable(ip.get_links(**kwargs)) |
| 93 |
except OSError as e: |
| 94 |
if e.errno == errno.ENOENT: |
| 95 |
raise NetworkNamespaceNotFound(netns_name=namespace) |
| 96 |
raise |
| 97 |
except daemon.FailedToDropPrivileges: |
| 98 |
raise |
| 99 |
except daemon.PrivsepTimeout: |
| 100 |
raise |
| 101 |
|
| 102 |
``PrivsepTimeout`` is raised if timeout is reached. |
| 103 |
|
| 104 |
.. warning:: |
| 105 |
|
| 106 |
The daemon (the root process) task won't stop when timeout |
| 107 |
is reached. That means we'll have less available threads if the related |
| 108 |
thread never finishes. |
| 109 |
|
| 110 |
Using a privileged function |
| 111 |
=========================== |
| 112 |
|
| 113 |
To use the privileged function in the regular code, you can just call it:: |
| 114 |
|
| 115 |
import nova.privsep.motd |
| 116 |
... |
| 117 |
|
| 118 |
nova.privsep.motd.update_motd('This node is currently idle') |
| 119 |
|
| 120 |
It is better to import the complete path (``import nova.privsep.motd``) rather |
| 121 |
than the motd name (``from nova.privsep import motd``) so that it is easier to |
| 122 |
spot that the function runs in a different privileged context. |
| 123 |
|
| 124 |
For more details, you can read the following blog post: |
| 125 |
|
| 126 |
* `How to make a privileged call with oslo privsep`_ |
| 127 |
|
| 128 |
.. _How to make a privileged call with oslo privsep: https://www.madebymikal.com/how-to-make-a-privileged-call-with-oslo-privsep/ |
| 129 |
|
| 130 |
|
| 131 |
Converting from rootwrap to privsep |
| 132 |
=================================== |
| 133 |
|
| 134 |
oslo.rootwrap is a precursor of oslo.privsep to allow code to run commands |
| 135 |
under sudo if they match a predefined filter. For example, you could define |
| 136 |
a filter that would allow you to run chmod as root using the following |
| 137 |
filter:: |
| 138 |
|
| 139 |
chmod: CommandFilter, chmod, root |
| 140 |
|
| 141 |
Beyond the bad performance of calling full commands in order to accomplish |
| 142 |
simple tasks, rootwrap also led to bad security: it was difficult to filter |
| 143 |
commands in a way that would not easily allow privilege escalation. |
| 144 |
|
| 145 |
Replacing rootwrap filters with privsep functions is easy. The chmod filter |
| 146 |
above can be replaced with a function that calls ``os.chmod()``. However a |
| 147 |
straight 1:1 filter:function replacement generally results in functions that |
| 148 |
are still too broad for good security. It is better to replace each chmod |
| 149 |
rootwrap *call* with a narrow privsep function that will limit it to specific |
| 150 |
files. |
| 151 |
|
| 152 |
Sometimes it is necessary to refactor the calling code: the rootwrap design |
| 153 |
discouraged the creation of new filters and therefore often resulted in the |
| 154 |
creation of overly-broad calling functions. |
| 155 |
|
| 156 |
As an example, this `patch series`_ is work-in-progress to transition Nova |
| 157 |
from rootwrap to privsep. |
| 158 |
|
| 159 |
For more details, you can read the following blog post: |
| 160 |
|
| 161 |
* `Adding oslo privsep to a new project, a worked example`_ |
| 162 |
|
| 163 |
.. _patch series: https://review.openstack.org/#/q/project:openstack/nova+branch:master+topic:my-own-personal-alternative-universe |
| 164 |
|
| 165 |
.. _Adding oslo privsep to a new project, a worked example: https://www.madebymikal.com/adding-oslo-privsep-to-a-new-project-a-worked-example/ |