Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reduce mode of SQLite storage file to 0o600 #1057

Merged
merged 12 commits into from
Nov 15, 2023

Conversation

tonyandrewmeyer
Copy link
Contributor

@tonyandrewmeyer tonyandrewmeyer commented Nov 7, 2023

sqlite3 creates new database files with 0o600 permissions. Although Charms should generally not store private data in the backend storage, this does happen sometimes, and it would be nicer to ensure that only the user (that is, root) can access the underlying storage file.

This chmod's the file (to -rw-------) after every open even if it already exists. It seems simpler to do this and get the additional access fix rather than only do it on initial creation.

Fixes #1051.

@tonyandrewmeyer tonyandrewmeyer added the bug Something isn't working label Nov 7, 2023
@benhoyt benhoyt changed the title fix: only permit the user to read/write the underlying sqlite storage file fix: reduce mode of SQLite storage file to 0o600 Nov 10, 2023
test/test_storage.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@benhoyt benhoyt requested review from jameinel and removed request for PietroPasotti November 10, 2023 10:14
ops/storage.py Outdated Show resolved Hide resolved
@jameinel
Copy link
Member

I wouldn't block this, but I would say that if we really want to care about the security of this file, we might as well take the step of creating it secure in the first place to avoid ever having a file handle that an attacker could exploit.

@tonyandrewmeyer
Copy link
Contributor Author

Moving back to draft until I verify the new approach with a real (non-unit, manual) test.

@tonyandrewmeyer tonyandrewmeyer marked this pull request as draft November 14, 2023 22:47
@tonyandrewmeyer
Copy link
Contributor Author

Manually verified the simple cases on micro-k8s (no existing state file, existing state file).

@tonyandrewmeyer tonyandrewmeyer marked this pull request as ready for review November 15, 2023 01:37
ops/storage.py Show resolved Hide resolved
ops/storage.py Outdated Show resolved Hide resolved
test/test_storage.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, and thanks for the tests. Per discussion, we'll fail hard in both cases. As Tony said on Mattermost:

It does seem like if we are going to add this (minimal) layer of security then we ought to ensure that it either happens or fails, so that people don't assume that it's happening when it's not.

@benhoyt benhoyt merged commit e4b0f9d into canonical:main Nov 15, 2023
20 checks passed
@tonyandrewmeyer tonyandrewmeyer deleted the stricter-sqlite-mode-1051 branch November 16, 2023 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.unit-state.db is world-readable
3 participants