Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,9 @@ def export_system_report(self, task, source_object, task_data, cancellable):
p2 = SystemReportManager.archive_and_copy_to_desktop(
desktop_path, archive_name
)
# The tree under /tmp has served its purpose once the archive is
# written; do not leave the collected data lying around.
SystemReportManager.cleanup()
self.ui_gathering_logs_filename.set_label(archive_name)
if p2.returncode == 0:
task.return_boolean(True)
Expand Down
49 changes: 46 additions & 3 deletions src/util/SystemReportManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
import json
import os
import pwd
import shutil
import subprocess

Expand All @@ -22,6 +23,46 @@ def detect_pkexec_user():
pkexec_user = detect_pkexec_user()


def restrict_to_owner():
"""Hand the report tree to the requesting user and to nobody else.

generate_report() runs as root and collects data the user cannot normally
read: the full system journal, dmidecode output, network state and the
system logs. The tree lives under /tmp, so leaving it group/world readable
exposes that data to every local account until the next report is written.
Directories become 0700 and files 0600, both owned by the requesting user.
"""
try:
entry = pwd.getpwnam(pkexec_user)
except KeyError:
return

uid, gid = entry.pw_uid, entry.pw_gid

def apply(path, mode):
try:
os.chown(path, uid, gid)
os.chmod(path, mode)
except OSError:
pass

apply(ARCHIVE_DIR, 0o700)
for root, dirs, files in os.walk(ARCHIVE_DIR):
for name in dirs:
apply(os.path.join(root, name), 0o700)
for name in files:
apply(os.path.join(root, name), 0o600)


def cleanup():
"""Remove the report tree once it has been archived.

Without this the collected data stays in /tmp until the next report run or
a reboot, long after the user is done with it.
"""
shutil.rmtree(ARCHIVE_DIR, ignore_errors=True)


def run_and_save(command, command_name=None):
"""Usage: run_and_save(["journalctl", "-q", "-n", 1000]), it will be saved in /tmp/pardus_system_report/journalctl"""

Expand Down Expand Up @@ -124,9 +165,7 @@ def generate_report():
copy("/etc/apt/sources.list")
copy("/etc/apt/sources.list.d")

# set permission and owner
subprocess.run(["chown", pkexec_user, "-R", ARCHIVE_DIR])
subprocess.run(["chmod", "755", "-R", ARCHIVE_DIR])
restrict_to_owner()


def generate_user_report():
Expand All @@ -147,6 +186,10 @@ def generate_user_report():
)
run_and_save(["flatpak", "list"])

# This runs as the user and adds files after generate_report() has already
# tightened the tree, so apply the same modes to the new files.
restrict_to_owner()


def archive_and_copy_to_desktop(desktop_path, archive_name):
return subprocess.run(
Expand Down