48 lines
2.0 KiB
Python
48 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Exact dev/test root script payload. Never installed as a daemon/provider."""
|
|
import os
|
|
from pathlib import Path
|
|
import stat
|
|
import sys
|
|
import time
|
|
import uuid
|
|
|
|
assert os.geteuid() == 0 and len(sys.argv) == 3
|
|
operation, identifier = sys.argv[1:]
|
|
assert operation in ('effect', 'read') and str(uuid.UUID(identifier)) == identifier
|
|
directory = Path('/var/lib/rmm-helper')/('crash-'+identifier)
|
|
for parent in directory.parents:
|
|
metadata = parent.lstat()
|
|
assert stat.S_ISDIR(metadata.st_mode) and metadata.st_uid == 0 and metadata.st_mode & 0o022 == 0
|
|
os.umask(0o077)
|
|
if operation == 'effect' and not directory.exists(): directory.mkdir(mode=0o700)
|
|
metadata = directory.lstat()
|
|
assert stat.S_ISDIR(metadata.st_mode) and metadata.st_uid == 0 and stat.S_IMODE(metadata.st_mode) == 0o700
|
|
counter = directory/'count'
|
|
def read_counter():
|
|
fd = os.open(counter, os.O_RDONLY | os.O_NOFOLLOW)
|
|
with os.fdopen(fd, 'r') as stream:
|
|
metadata = os.fstat(stream.fileno())
|
|
assert stat.S_ISREG(metadata.st_mode) and metadata.st_uid == 0 and metadata.st_nlink == 1
|
|
assert stat.S_IMODE(metadata.st_mode) == 0o600 and metadata.st_size <= 16
|
|
value = stream.read()
|
|
assert value.endswith('\n') and value[:-1].isdigit()
|
|
return int(value)
|
|
if operation == 'effect':
|
|
lock = directory/'lock';lock.mkdir(mode=0o700)
|
|
try:
|
|
value = read_counter() if counter.exists() else 0
|
|
assert 0 <= value < 100
|
|
fd = os.open(directory/'count.next',os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'w') as stream:
|
|
stream.write(str(value+1)+'\n');stream.flush();os.fsync(stream.fileno())
|
|
os.replace(directory/'count.next',counter)
|
|
fd = os.open(directory,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW)
|
|
try: os.fsync(fd)
|
|
finally: os.close(fd)
|
|
finally: lock.rmdir()
|
|
# Independent helper remains alive after only the agent process dies.
|
|
time.sleep(90)
|
|
assert read_counter() == 1
|
|
print('RMM_CRASH_COUNT=1')
|