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
|
#!/usr/bin/env python3
# libpulp - User-space Livepatching Library
#
# Copyright (C) 2021 SUSE Software Solutions GmbH
#
# This file is part of libpulp.
#
# libpulp 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.
#
# libpulp 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 General Public License
# along with libpulp. If not, see <http://www.gnu.org/licenses/>.
# From standard library
import os
import pathlib
import re
import signal
import subprocess
import sys
import time
# Third-party libraries
import pexpect
import psutil
# Test case name as provided by automake tests
testname = os.path.splitext(sys.argv[0])
testname = os.path.basename(testname[0])
# Libpulp definitions
builddir = os.getcwd()
ulptool = builddir + '/../tools/ulp'
libpulp_path = builddir + '/../lib/.libs/libpulp.so'
# Check if certain library is livepatchable.
def is_library_livepatchable(library):
command = [ulptool, "livepatchable", library]
try:
tool = subprocess.run(command, timeout=10, stderr=subprocess.STDOUT)
except subprocess.TimeoutExpired:
print('ulp tool deadlock');
return False
if tool.returncode == 0:
return True
return False
# Wrapper around pexpect.spawn that automatically sets userspace livepatching
# requirements, such as LD_PRELOAD'ing libpulp.so, as well as extends its
# functionality with live patching operations.
#
# Using this testing framework requires few steps: importing this module,
# starting a live patchable process, optionally testing its default behavior,
# applying a live patch, and testing the patched behavior, for instance:
#
# # Import the testing framework:
# import testsuite
#
# # Start the target process:
# child = testsuite.spawn('testcase')
#
# # Check default behavior:
# child.send('ping')
# child.expect('pong')
#
# # Apply a live patch
# child.livepatch('metadata')
#
# # Check patched behavior:
# child.send('ping')
# child.expect('pang')
#
# The 'livepatch' and 'expect' methods are the main interfaces to the testing
# framework. Simply calling them is usually enough, because they check for
# error conditions on their own. However, if more control over the outcomes is
# needed, they raise exceptions which can be caught and dealt with. See the
# existing test cases for examples.
class spawn(pexpect.spawn):
# Spawn a live patchable process. Similar to pexpect.spawn, this class
# constructor starts and controls a child process. Unlike pexpect.spawn,
# LD_PRELOAD=libpulp.so is automatically provided, unless 'env' is passed as
# an argument. The arguments: 'timeout', 'env' and 'encoding' are forwarded
# to pexpect.spawn; whereas log is assigned to pexpect's logfile_read
# attribute. Finally, 'verbose' controls whether the live patching methods
# print messages to stdout. By default, all messages are printed, so that the
# test suite logs contain more information for debugging.
def __init__(self, testname, timeout=10, env=Ellipsis, log=sys.stdout,
encoding='utf-8', verbose=True, script=True):
# If testname is a relative path, i.e. not starting with slash, prepend
# dot-slash to enable command-line execution.
if testname[0] != '/':
testname = './' + testname
# if TEST_THROUGH_VALGRIND environment variable is defined, append valgrind
# call on testname. We actually call valgrind on the `sh` call, and it
# still catch memory issues while avoiding libpulp problems regarding
# the program actually being `valgrind` and not the test program.
try:
if os.environ['TESTS_THROUGH_VALGRIND'] == '1':
testname = 'valgrind --leak-check=full ' + testname
except KeyError:
pass
# If env has not been provided, default to LD_PRELOAD'ing libpulp.so.
if env == Ellipsis:
env = {'LD_PRELOAD': libpulp_path}
# Spawn the testcase with pexpect and enable logging.
super().__init__(testname, timeout=timeout, env=env, encoding=encoding,
echo=False)
self.logfile_read = log
self.verbose = verbose
# Print verbose messages when in verbose mode
def print(self, *args, end=None):
if self.verbose:
print(*args, flush=True, end=end)
# When pexpect.expect is unable to find the expected patterns, it prints
# comprehensive information about the scanning status, such as the whole
# child output, as well as less relevant information about the child process.
# Even though the output is useful, it pollutes the test suite logs
# unnecessarily. Thus, this class overrides expect() and always adds
# pexpect.EOF and pexpect.TIMEOUT to the list of expected patterns, so that
# it can print less verbose, more targeted information, when the original
# expected patterns and the actual output do not match. If it happens, the
# overridden method raises exceptions for EOF and Timeout, which have the
# added benefit of displaying local, shorter, and more friendly stack traces.
# The 'accept' and 'reject' parameters can be either single strings or lists
# of strings, such as pexpect.expect expects.
def expect(self, accept, reject=None):
# Converted string arguments into lists
if type(accept) == str:
accept = [accept]
if type(reject) == str:
reject = [reject]
if reject == None:
reject = []
# Valgrind always print the error summary message. Match if the errors are
# a positive value.
valgrind_errors = ['ERROR SUMMARY: [1-9]+ errors from',
'definitely lost: [1-9]+']
reject += valgrind_errors
valgrind_reject_index = len(reject) - len(valgrind_errors)
# Also add EOF and TIMEOUT to the expected patterns to avoid the verbose
# output of pexpect.expect when the expected patterns are not found
patterns = [pexpect.EOF, pexpect.TIMEOUT] + accept + reject
# Actually look for the expected patterns
index = super().expect(patterns)
# Handle EOF and TIMEOUT, then raise the corresponding exception
if index == 0 or index == 1:
self.print('error: expected output not found.')
self.print('expected:', accept, reject)
self.print('observed: %r' %(self.buffer[-self.str_last_chars:]))
if index == 0:
raise EOFError
if index == 1:
raise TimeoutError
# Account for the prepended items in the patterns list
index = index - 2
# If the matching pattern belongs to the accepted list, return its index
if index < len(accept):
self.print('Accept pattern found:', accept[index])
return index
# Otherwise, the matching pattern belongs to the rejected list
index = index - len(accept)
if index >= valgrind_reject_index:
self.print('Valgrind error detected.')
raise MemoryError
else:
self.print('Reject pattern found:', reject[index])
raise ValueError
# Verify sanity of arguments to ulp tools
def sanity(self, filename=Ellipsis, pid=Ellipsis):
# Check if the process with PID exists
if not pid == Ellipsis:
if not psutil.pid_exists(pid):
raise ValueError('Process ' + str(pid) + ' not found')
# Check if the live patch file exists
if not filename == Ellipsis and filename is not None:
file = pathlib.Path(filename)
if not file.is_file():
raise FileNotFoundError('File ' + filename + ' not found')
# Dump a compiled metadata file (.ulp) provided in metadata
def show_metadata(self, metadata):
# See if there is a .dsc file. On true, print it as well.
suffix = metadata.rfind(".ulp")
dsc = metadata[:suffix] + ".dsc"
if os.path.isfile(dsc):
self.print(dsc + " content:")
dscf = open(dsc, 'r')
content = dscf.read()
dscf.close()
self.print(content)
# Now print the .ulp file
command = [ulptool, "dump", metadata]
try:
self.print(metadata + ' content:')
tool = subprocess.run(command, timeout=10)
except subprocess.TimeoutExpired:
self.print('ulp dump timed out.')
raise
# Apply a live patch to the spawned process. The path to the live patch
# metadata must be passed through 'filename'. The remaining parameters, which
# are optional, are the same that the Trigger tool provides (see its --help
# output for more information).
def livepatch(self, filename=None, timeout=10, retries=1,
verbose=True, quiet=False, revert=False, revert_lib=None,
sanity=True, prefix=None, capture_tool_output=False,
disable_seccomp=False):
# Check sanity of command-line arguments
if sanity is True:
self.sanity(pid=self.pid)
self.sanity(filename=filename)
# Build command-line from arguments
command = [ulptool, "trigger", '-p', str(self.pid)]
if revert is True:
command.append("--revert")
if revert_lib is not None:
command.append("--revert-all")
command.append(revert_lib)
if filename is not None:
command.append(filename)
self.print('')
self.show_metadata(filename)
if verbose:
command.append('-v')
if quiet:
command.append('-q')
if retries > 1:
command.append('-r')
command.append(str(retries))
if prefix is not None:
command.append('-R')
command.append(str(prefix))
if disable_seccomp is True:
command.append('--disable-seccomp')
# Apply the live patch and check for common errors
try:
self.print('Applying/reverting live patch.')
if capture_tool_output == True:
tool = subprocess.run(command, timeout=timeout,
stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
else:
tool = subprocess.run(command, timeout=timeout)
except subprocess.TimeoutExpired:
self.print('Live patching timed out.')
raise
self.print("---- Messages in libpulp.so ----")
self.print(self.get_libpulp_messages())
# The trigger tool returns 0 on success, so use check_returncode(),
# which asserts that, and raises CalledProcessError otherwise.
if sanity is True:
tool.check_returncode()
if revert == True:
self.print('Live patch reverted successfully.')
else:
self.print('Live patch applied successfully.')
if tool.stdout is not None:
return tool.stdout.decode()
else:
return None
# Check if a live patch is already applied. The path to the live patch
# metadata must be passed through 'filename'. The remaining parameters, which
# are optional, are the same that the Checker tool provides (see its --help
# output for more information).
def is_patch_applied(self, filename, verbose=True, quiet=False):
# Check sanity of command-line arguments
self.sanity(pid=self.pid)
self.sanity(filename=filename)
# Build command-line from arguments
command = [ulptool, '-p', str(self.pid), "check", filename]
if verbose:
command.append('-v')
if quiet:
command.append('-q')
# Apply the live patch and check for common errors
try:
self.print('Checking live patch status.')
tool = subprocess.run(command, timeout=10, stderr=subprocess.STDOUT)
except subprocess.TimeoutExpired:
self.print('Live patch status check timed out.')
raise
# On success, the checker tool returns either 0 or 1:
# 1. If the given live patch has already been applied;
# 0. If it has not.
if tool.returncode == 0 or tool.returncode == 1:
self.print('Live patch status check ended successfully. ', end='')
if tool.returncode == 0:
self.print('Status is not applied.')
return False
if tool.returncode == 1:
self.print('Status is applied.')
return True
# Whereas on failure, the checker tool returns something different,
# usually -1, so raise CalledProcessError.
raise subprocess.CalledProcessError
def is_so_loaded(self, fname_so):
mapsf = open('/proc/' + str(self.pid) + '/maps', 'r')
maps = mapsf.read()
mapsf.close()
if maps.find(fname_so) == -1:
return False
return True
def get_patches(self, verbose=False):
# Build command-line from arguments
command = [ulptool, '-p', str(self.pid), "patches"]
if verbose:
command.append('-v')
try:
tool = subprocess.run(command, timeout=10, stdout=subprocess.PIPE)
except subprocess.TimeoutExpired:
self.print('Live patch status check timed out.')
raise
if tool.returncode != 0:
self.print('ulp tool returned an error code.')
raise
msg = str(tool.stdout.decode())
return msg
# Get libpulp messages, currently by calling ulp_messages and parsing
# its stdout.
def get_libpulp_messages(self):
self.sanity(pid=self.pid)
command = [ulptool, 'messages', '-p', str(self.pid)]
try:
self.print('Checking libpulp.so messages.')
tool = subprocess.run(command, timeout=10, stdout=subprocess.PIPE)
except:
raise
msgs = tool.stdout.decode()
return str(msgs)
def enable_livepatching(self):
self.sanity(pid=self.pid)
command = [ulptool, 'set_patchable', '-p', str(self.pid), 'enable']
try:
tool = subprocess.run(command, timeout=10, stdout=subprocess.PIPE)
except:
rai2e
return tool.returncode
def disable_livepatching(self):
self.sanity(pid=self.pid)
command = [ulptool, 'set_patchable', '-p', str(self.pid), 'disable']
try:
tool = subprocess.run(command, timeout=10, stdout=subprocess.PIPE)
except:
raise
return tool.returncode
def childless_livepatch(wildcard, timeout=10, retries=1,
verbose=False, quiet=False, revert_lib=None, capture_output=False):
# Build command-line from arguments
command = [ulptool, "trigger"]
if revert_lib is not None:
command.append("--revert-all")
command.append(revert_lib)
if wildcard is not None:
command.append(wildcard)
print('')
if verbose:
command.append('-v')
if quiet:
command.append('-q')
if retries > 1:
command.append('-r')
command.append(str(retries))
# Apply the live patch and check for common errors
try:
print('Applying/reverting live patch.')
if capture_output == True:
tool = subprocess.run(command, timeout=timeout, stdout=subprocess.PIPE)
else:
tool = subprocess.run(command, timeout=timeout)
except subprocess.TimeoutExpired:
print('Live patching timed out.')
raise
# The trigger tool returns 0 on success, so use check_returncode(),
# which asserts that, and raises CalledProcessError otherwise.
tool.check_returncode()
print('Live patch applied/reverted successfully.')
if tool.stdout is not None:
return tool.stdout.decode()
else:
return None
def childless_disable_livepatching(process_wildcard, userid, timeout=10):
# Build command-line from arguments
command = [ulptool, "set_patchable"]
if process_wildcard is not None:
command.append("-p")
command.append(process_wildcard)
if userid is not None:
command.append("-u")
command.append(str(userid))
command.append('disable')
# Apply the live patch and check for common errors
try:
print('Disabling livepatches.')
tool = subprocess.run(command, timeout=timeout)
except subprocess.TimeoutExpired:
print('Command run timed out')
raise
# The trigger tool returns 0 on success, so use check_returncode(),
# which asserts that, and raises CalledProcessError otherwise.
tool.check_returncode()
|