Skip to content

feat: rewrite audit script in Python#960

Merged
RoyalOughtness merged 22 commits intosecureblue:livefrom
HastD:python-audit
May 5, 2025
Merged

feat: rewrite audit script in Python#960
RoyalOughtness merged 22 commits intosecureblue:livefrom
HastD:python-audit

Conversation

@HastD
Copy link
Copy Markdown
Collaborator

@HastD HastD commented Mar 22, 2025

The new Python script currently carries out the same checks as the old bash audit script (with a few minor enhancements, like checking for libhardened_malloc-light.so in a couple places), plus one additional check for MAC randomization. It is more modular and significantly faster: on my system with lots of flatpaks installed, the bash script typically takes about 20 seconds to run, while the Python script takes about 9 seconds.

Flatpak permission checks can be skipped by passing the --skip flatpak option. Note: the bash script seems to have only checked one version of each flatpak in cases where multiple versions are installed—I think this was an error, though in practice it typically just affects runtimes, which aren't visible in Flatseal anyway and are unlikely to fail the permission checks.

Implementation details: Each check is a generator (optionally asynchronous) that optionally accepts some state (an associative array that can be used to pass information between checks) and yields a sequence of reports (the lines that are shown under the "Audit" heading with a "success/warning/failure" status) and recommendations (printed at the end), which are handled by an audit runner. A decorator @audit is used to queue up generators to be run by the default audit runner. The file can also be imported as a Python module for use in other code—it doesn't run any audits automatically if imported.

@fiftydinar
Copy link
Copy Markdown
Collaborator

fiftydinar commented Mar 22, 2025

Maybe it's smart to put audit-secureblue to /usr/libexec/secureblue/audit-secureblue?

This way, path needs to be called fully for binary to execute & users will rely on ujust recipe more than just directly calling audit-secureblue?

Also, this way, any secureblue related scripts can be put into /usr/libexec/secureblue/, which would look neat.

@Rubiginosa
Copy link
Copy Markdown
Collaborator

I think the flatpak order could still be made deterministic—and that it might be worth the effort, especially running repeated tests or comparing across installations.

Not familiar with the specifics in python, but having the output thread wait on each of the checking threads in order should work.

@HastD
Copy link
Copy Markdown
Collaborator Author

HastD commented Mar 22, 2025

Thanks for the feedback, I put the script in /usr/libexec/secureblue/audit_secureblue.py (with ujust audit-secureblue simply calling this and forwarding any passed arguments) and made the flatpak permission checks print out in lexicographic order.

I see that Codacy is flagging the use of the subprocess module in the script; however, every use of it here is calling commands with trusted inputs and with shell=False, so there shouldn't be any security concern. Do I need to add some marker to the script to tell Codacy to ignore it, or can that check just be overridden some other way?

@HastD HastD marked this pull request as ready for review March 23, 2025 00:28
@HastD HastD requested a review from RoyalOughtness as a code owner March 23, 2025 00:28
@HastD

This comment was marked as resolved.

@HastD HastD changed the base branch from live to staging April 1, 2025 20:21
@RoyalOughtness RoyalOughtness force-pushed the staging branch 4 times, most recently from 235b1d0 to 787a6e8 Compare April 6, 2025 18:16
HastD added 15 commits May 3, 2025 10:14
The new Python script is a standalone executable in
/usr/bin/audit-secureblue. It currently carries out the same checks as
the old bash audit script, but is faster and more modular.

Each check is a generator that accepts some state (an associative array
that can be used to pass information between checks) and yields a
sequence of reports and recommendations, which are handled by the audit
runner.

Signed-off-by: Daniel Hast <[email protected]>
Also simplify implementation of async flatpak permissions checks.

Signed-off-by: Daniel Hast <[email protected]>
The security issue is only a concern with untrusted inputs, and we only
call subprocess with trusted inputs.

Signed-off-by: Daniel Hast <[email protected]>
Signed-off-by: Daniel Hast <[email protected]>
@HastD HastD changed the base branch from staging to live May 3, 2025 14:17
@RoyalOughtness RoyalOughtness linked an issue May 4, 2025 that may be closed by this pull request
@RoyalOughtness RoyalOughtness merged commit 38e4676 into secureblue:live May 5, 2025
13 of 14 checks passed
@HastD HastD deleted the python-audit branch May 5, 2025 22:56
RoyalOughtness pushed a commit to RoyalOughtness/secureblue-dev that referenced this pull request Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants