/usr/lib/python2.7/dist-packages/oslo_privsep/daemon.py is in python-oslo.privsep 1.27.0-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | # Copyright 2015 Rackspace Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
'''Privilege separation ("privsep") daemon.
To ease transition this supports 2 alternative methods of starting the
daemon, all resulting in a helper process running with elevated
privileges and open socket(s) to the original process:
1. Start via fork()
Assumes process currently has all required privileges and is about
to drop them (perhaps by setuid to an unprivileged user). If the
the initial environment is secure and `PrivContext.start(Method.FORK)`
is called early in `main()`, then this is the most secure and
simplest. In particular, if the initial process is already running
as non-root (but with sufficient capabilities, via eg suitable
systemd service files), then no part needs to involve uid=0 or
sudo.
2. Start via sudo/rootwrap
This starts the privsep helper on first use via sudo and rootwrap,
and communicates via a temporary Unix socket passed on the command
line. The communication channel is briefly exposed in the
filesystem, but is protected with file permissions and connecting
to it only grants access to the unprivileged process. Requires a
suitable entry in sudoers or rootwrap.conf filters.
The privsep daemon exits when the communication channel is closed,
(which usually occurs when the unprivileged process exits).
'''
import enum
import errno
import io
import logging as pylogging
import os
import platform
import socket
import subprocess
import sys
import tempfile
import threading
if platform.system() == 'Linux':
import fcntl
import grp
import pwd
import eventlet
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
from oslo_privsep._i18n import _
from oslo_privsep import capabilities
from oslo_privsep import comm
LOG = logging.getLogger(__name__)
@enum.unique
class StdioFd(enum.IntEnum):
# NOTE(gus): We can't use sys.std*.fileno() here. sys.std*
# objects may be random file-like objects that may not match the
# true system std* fds - and indeed may not even have a file
# descriptor at all (eg: test fixtures that monkey patch
# fixtures.StringStream onto sys.stdout). Below we always want
# the _real_ well-known 0,1,2 Unix fds during os.dup2
# manipulation.
STDIN = 0
STDOUT = 1
STDERR = 2
@enum.unique
class Message(enum.IntEnum):
"""Types of messages sent across the communication channel"""
PING = 1
PONG = 2
CALL = 3
RET = 4
ERR = 5
LOG = 6
class FailedToDropPrivileges(Exception):
pass
class ProtocolError(Exception):
pass
def set_cloexec(fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
if (flags & fcntl.FD_CLOEXEC) == 0:
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(fd, fcntl.F_SETFD, flags)
def setuid(user_id_or_name):
try:
new_uid = int(user_id_or_name)
except (TypeError, ValueError):
new_uid = pwd.getpwnam(user_id_or_name).pw_uid
if new_uid != 0:
try:
os.setuid(new_uid)
except OSError:
msg = _('Failed to set uid %s') % new_uid
LOG.critical(msg)
raise FailedToDropPrivileges(msg)
def setgid(group_id_or_name):
try:
new_gid = int(group_id_or_name)
except (TypeError, ValueError):
new_gid = grp.getgrnam(group_id_or_name).gr_gid
if new_gid != 0:
try:
os.setgid(new_gid)
except OSError:
msg = _('Failed to set gid %s') % new_gid
LOG.critical(msg)
raise FailedToDropPrivileges(msg)
class PrivsepLogHandler(pylogging.Handler):
def __init__(self, channel, processName=None):
super(PrivsepLogHandler, self).__init__()
self.channel = channel
self.processName = processName
def emit(self, record):
# Vaguely based on pylogging.handlers.SocketHandler.makePickle
if self.processName:
record.processName = self.processName
data = dict(record.__dict__)
if record.exc_info:
if not record.exc_text:
fmt = self.formatter or pylogging.Formatter()
data['exc_text'] = fmt.formatException(record.exc_info)
data['exc_info'] = None # drop traceback in favor of exc_text
# serialise msg now so we can drop (potentially unserialisable) args
data['msg'] = record.getMessage()
data['args'] = ()
self.channel.send((None, (Message.LOG, data)))
class _ClientChannel(comm.ClientChannel):
"""Our protocol, layered on the basic primitives in comm.ClientChannel"""
def __init__(self, sock):
super(_ClientChannel, self).__init__(sock)
self.exchange_ping()
def exchange_ping(self):
try:
# exchange "ready" messages
reply = self.send_recv((Message.PING.value,))
success = reply[0] == Message.PONG
except Exception as e:
LOG.exception('Error while sending initial PING to privsep: %s', e)
success = False
if not success:
msg = _('Privsep daemon failed to start')
LOG.critical(msg)
raise FailedToDropPrivileges(msg)
def remote_call(self, name, args, kwargs):
result = self.send_recv((Message.CALL.value, name, args, kwargs))
if result[0] == Message.RET:
# (RET, return value)
return result[1]
elif result[0] == Message.ERR:
# (ERR, exc_type, args)
#
# TODO(gus): see what can be done to preserve traceback
# (without leaking local values)
exc_type = importutils.import_class(result[1])
raise exc_type(*result[2])
else:
raise ProtocolError(_('Unexpected response: %r') % result)
def out_of_band(self, msg):
if msg[0] == Message.LOG:
# (LOG, LogRecord __dict__)
record = pylogging.makeLogRecord(msg[1])
if LOG.isEnabledFor(record.levelno):
LOG.logger.handle(record)
else:
LOG.warning('Ignoring unexpected OOB message from privileged '
'process: %r', msg)
def fdopen(fd, *args, **kwargs):
# NOTE(gus): We can't just use os.fdopen() here and allow the
# regular (optional) monkey_patching to do its thing. Turns out
# that regular file objects (as returned by os.fdopen) on python2
# are broken in lots of ways regarding blocking behaviour. We
# *need* the newer io.* objects on py2 (doesn't matter on py3,
# since the old file code has been replaced with io.*)
if eventlet.patcher.is_monkey_patched('socket'):
return eventlet.greenio.GreenPipe(fd, *args, **kwargs)
else:
return io.open(fd, *args, **kwargs)
def _fd_logger(level=logging.WARN):
"""Helper that returns a file object that is asynchronously logged"""
read_fd, write_fd = os.pipe()
read_end = fdopen(read_fd, 'r', 1)
write_end = fdopen(write_fd, 'w', 1)
def logger(f):
for line in f:
LOG.log(level, 'privsep log: %s', line.rstrip())
t = threading.Thread(
name='fd_logger',
target=logger, args=(read_end,)
)
t.daemon = True
t.start()
return write_end
def replace_logging(handler, log_root=None):
if log_root is None:
log_root = logging.getLogger(None).logger # root logger
for h in log_root.handlers:
log_root.removeHandler(h)
log_root.addHandler(handler)
class ForkingClientChannel(_ClientChannel):
def __init__(self, context):
"""Start privsep daemon using fork()
Assumes we already have required privileges.
"""
sock_a, sock_b = socket.socketpair()
for s in (sock_a, sock_b):
s.setblocking(True)
# Important that these sockets don't get leaked
set_cloexec(s)
# Try to prevent any buffered output from being written by both
# parent and child.
for f in (sys.stdout, sys.stderr):
f.flush()
if os.fork() == 0:
# child
channel = comm.ServerChannel(sock_b)
sock_a.close()
# Replace root logger early (to capture any errors during setup)
replace_logging(PrivsepLogHandler(channel,
processName=str(context)))
Daemon(channel, context=context).run()
LOG.debug('privsep daemon exiting')
os._exit(0)
# parent
sock_b.close()
super(ForkingClientChannel, self).__init__(sock_a)
class RootwrapClientChannel(_ClientChannel):
def __init__(self, context):
"""Start privsep daemon using exec()
Uses sudo/rootwrap to gain privileges.
"""
listen_sock = socket.socket(socket.AF_UNIX)
# Note we listen() on the unprivileged side, and connect to it
# from the privileged process. This means there is no exposed
# attack point on the privileged side.
# NB: Permissions on sockets are not checked on some (BSD) Unices
# so create socket in a private directory for safety. Privsep
# daemon will (initially) be running as root, so will still be
# able to connect to sock path.
tmpdir = tempfile.mkdtemp() # NB: created with 0700 perms
try:
sockpath = os.path.join(tmpdir, 'privsep.sock')
listen_sock.bind(sockpath)
listen_sock.listen(1)
cmd = context.helper_command(sockpath)
LOG.info('Running privsep helper: %s', cmd)
proc = subprocess.Popen(cmd, shell=False, stderr=_fd_logger())
if proc.wait() != 0:
msg = ('privsep helper command exited non-zero (%s)' %
proc.returncode)
LOG.critical(msg)
raise FailedToDropPrivileges(msg)
LOG.info('Spawned new privsep daemon via rootwrap')
sock, _addr = listen_sock.accept()
LOG.debug('Accepted privsep connection to %s', sockpath)
finally:
# Don't need listen_sock anymore, so clean up.
listen_sock.close()
try:
os.unlink(sockpath)
except OSError as e:
if e.errno != errno.ENOENT:
raise
os.rmdir(tmpdir)
super(RootwrapClientChannel, self).__init__(sock)
class Daemon(object):
"""NB: This doesn't fork() - do that yourself before calling run()"""
def __init__(self, channel, context):
self.channel = channel
self.context = context
self.user = context.conf.user
self.group = context.conf.group
self.caps = set(context.conf.capabilities)
def run(self):
"""Run request loop. Sets up environment, then calls loop()"""
os.chdir("/")
os.umask(0)
self._drop_privs()
self._close_stdio()
self.loop()
def _close_stdio(self):
with open(os.devnull, 'w+') as devnull:
os.dup2(devnull.fileno(), StdioFd.STDIN)
os.dup2(devnull.fileno(), StdioFd.STDOUT)
# stderr is left untouched
def _drop_privs(self):
try:
# Keep current capabilities across setuid away from root.
capabilities.set_keepcaps(True)
if self.group is not None:
try:
os.setgroups([])
except OSError:
msg = _('Failed to remove supplemental groups')
LOG.critical(msg)
raise FailedToDropPrivileges(msg)
if self.user is not None:
setuid(self.user)
if self.group is not None:
setgid(self.group)
finally:
capabilities.set_keepcaps(False)
LOG.info('privsep process running with uid/gid: %(uid)s/%(gid)s',
{'uid': os.getuid(), 'gid': os.getgid()})
capabilities.drop_all_caps_except(self.caps, self.caps, [])
def fmt_caps(capset):
if not capset:
return 'none'
fc = [capabilities.CAPS_BYVALUE.get(c, str(c))
for c in capset]
fc.sort()
return '|'.join(fc)
eff, prm, inh = capabilities.get_caps()
LOG.info(
'privsep process running with capabilities '
'(eff/prm/inh): %(eff)s/%(prm)s/%(inh)s',
{
'eff': fmt_caps(eff),
'prm': fmt_caps(prm),
'inh': fmt_caps(inh),
})
def _process_cmd(self, cmd, *args):
if cmd == Message.PING:
return (Message.PONG.value,)
elif cmd == Message.CALL:
name, f_args, f_kwargs = args
func = importutils.import_class(name)
if not self.context.is_entrypoint(func):
msg = _('Invalid privsep function: %s not exported') % name
raise NameError(msg)
ret = func(*f_args, **f_kwargs)
return (Message.RET.value, ret)
raise ProtocolError(_('Unknown privsep cmd: %s') % cmd)
def loop(self):
"""Main body of daemon request loop"""
LOG.info('privsep daemon running as pid %s', os.getpid())
# We *are* this context now - any calls through it should be
# executed locally.
self.context.set_client_mode(False)
for msgid, msg in self.channel:
LOG.debug('privsep: request[%(msgid)s]: %(req)s',
{'msgid': msgid, 'req': msg})
try:
reply = self._process_cmd(*msg)
except Exception as e:
LOG.debug(
'privsep: Exception during request[%(msgid)s]: %(err)s',
{'msgid': msgid, 'err': e}, exc_info=True)
cls = e.__class__
cls_name = '%s.%s' % (cls.__module__, cls.__name__)
reply = (Message.ERR.value, cls_name, e.args)
try:
LOG.debug('privsep: reply[%(msgid)s]: %(reply)s',
{'msgid': msgid, 'reply': reply})
self.channel.send((msgid, reply))
except IOError as e:
if e.errno == errno.EPIPE:
# Write stream closed, exit loop
break
raise
LOG.debug('Socket closed, shutting down privsep daemon')
def helper_main():
"""Start privileged process, serving requests over a Unix socket."""
cfg.CONF.register_cli_opts([
cfg.StrOpt('privsep_context', required=True),
cfg.StrOpt('privsep_sock_path', required=True),
])
logging.register_options(cfg.CONF)
cfg.CONF(args=sys.argv[1:], project='privsep')
logging.setup(cfg.CONF, 'privsep') # note replace_logging call below
context = importutils.import_class(cfg.CONF.privsep_context)
from oslo_privsep import priv_context # Avoid circular import
if not isinstance(context, priv_context.PrivContext):
LOG.fatal('--privsep_context must be the (python) name of a '
'PrivContext object')
sock = socket.socket(socket.AF_UNIX)
sock.connect(cfg.CONF.privsep_sock_path)
set_cloexec(sock)
channel = comm.ServerChannel(sock)
# Channel is set up, so fork off daemon "in the background" and exit
if os.fork() != 0:
# parent
return
# child
# Note we don't move into a new process group/session like a
# regular daemon might, since we _want_ to remain associated with
# the originating (unprivileged) process.
# Channel is set up now, so move to in-band logging
replace_logging(PrivsepLogHandler(channel))
LOG.info('privsep daemon starting')
try:
Daemon(channel, context).run()
except Exception as e:
LOG.exception(e)
sys.exit(str(e))
LOG.debug('privsep daemon exiting')
sys.exit(0)
if __name__ == '__main__':
helper_main()
|