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

Database: Set 0640 permissions on SQLite database file #26339

Merged
merged 6 commits into from
Jul 23, 2020
Merged

Conversation

aknuds1
Copy link
Contributor

@aknuds1 aknuds1 commented Jul 15, 2020

What this PR does / why we need it:
#8283 points out that the we should restrict access to the SQLite database file, so only the owner can use it. This PR ensures the file has permissions 0640 (read/write for owner, read for group) when it gets created. If the file already exists and has broader permissions, a warning will be logged.

@wz2b @austinbutler does this look right to you?

Which issue(s) this PR fixes:
Fixes #8283.

Special notes for your reviewer:

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Copy link
Contributor

@papagian papagian left a comment

Choose a reason for hiding this comment

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

I have tested both with existing and not existing sqlite database file and with mysql database and it works as expected.

@aknuds1
Copy link
Contributor Author

aknuds1 commented Jul 15, 2020

I have tested both with existing and not existing sqlite database file and with mysql database and it works as expected.

Thanks @papagian - do you have any opinion on whether we should change the permissions of existing SQLite files or not? Maybe just log a warning to the administrator? I'm just worried in case there are valid scenarios where the permissions (or owner?) should be changed by the operator.

@papagian
Copy link
Contributor

Thanks @papagian - do you have any opinion on whether we should change the permissions of existing SQLite files or not? Maybe just log a warning to the administrator? I'm just worried in case there are valid scenarios where the permissions (or owner?) should be changed by the operator.

I didn't think about it. I'm not sure. Maybe the admin could overwrite the permissions by the configuration.

@wz2b
Copy link

wz2b commented Jul 15, 2020

Thanks @papagian - do you have any opinion on whether we should change the permissions of existing SQLite files or not? Maybe just log a warning to the administrator? I'm just worried in case there are valid scenarios where the permissions (or owner?) should be changed by the operator.

I didn't think about it. I'm not sure. Maybe the admin could overwrite the permissions by the configuration.

I think you should. Not only is it the user table but also the data_source table. In theory plugins will store passwords encrypted in secureJsonData but they don't all. Just an an example, the Zabbix datasource does store in secureJsonData now, but it didn't always, and if you upgrade the datasource it doesn't clear out the plaintext 'password' column that was there previously (even though it doesn't use it any more). What that means is if somebody didn't set up a zabbix service account with the right permissions, you can steal this password from grafana.db and do bad things to zabbix. We don't want somebody to be able to point to some exploit and have the reason be that grafana allowed the password to escape.

@kylebrandt
Copy link
Contributor

Thanks @papagian - do you have any opinion on whether we should change the permissions of existing SQLite files or not? Maybe just log a warning to the administrator? I'm just worried in case there are valid scenarios where the permissions (or owner?) should be changed by the operator.

I didn't think about it. I'm not sure. Maybe the admin could overwrite the permissions by the configuration.

One may want to give a group of users access to the sqlite db. So forcing group permissions to 0 could be problematic.

@wz2b
Copy link

wz2b commented Jul 15, 2020

One may want to give a group of users access to the sqlite db. So forcing group permissions to 0 could be problematic.

The Ubuntu package (maybe all debian) yields this:

-rw-r--r-- 1 grafana grafana 260056 July 15 13:29 /var/lib/grafana/grafana.db

so it creates a 'grafana' user and a 'grafana' group. So I'd be fine with the permissions being 640 (u+rw,g+r if I did that right). Normally there's nobody in the 'grafana' group other than the 'grafana' user. So, this way, if somebody did have some weird reason to add a second user to the 'grafana' group (like say for backing it up) that would still work.

Regarding changing an existing file's permissions if it exists: I think this is a good idea, but it's not something I would do on the go side. I would have grafana set 0640 if it creates the file; but as for changing a file that already exists, in thinking about this, it's really something I would put into the postinst script. If you want to be nice you'd even make it prompt the user whether or not they wanted you to do it.

@kylebrandt
Copy link
Contributor

I don't know enough about the various files the Grafana process can create, or the packaging ... but my fuzzy old knowledge says that maybe we should be setting the umask for the process so all things created are only accessible by grafana user/group - but I don't know if that might cause problems - and don't think it should be a blocker getting more secure specific DB perms in place.

@marefr
Copy link
Contributor

marefr commented Jul 16, 2020

Think logging a warning if permissions are to "broad" on sqlite file should be enough. Then handle the migration thru documentation because older Grafana instances can apply that change manually without upgrading, which is good from a security perspective.

@aknuds1
Copy link
Contributor Author

aknuds1 commented Jul 16, 2020

I'll change the permissions to 0640 give the group read access. That sound alright to you @papagian @kylebrandt @marefr?

@papagian @kylebrandt Do you agree with @marefr about just logging a warning if the file exists with broader permissions?

@aknuds1 aknuds1 changed the title SQLite: Set 0600 permissions on SQLite database file SQLite: Set 0640 permissions on SQLite database file Jul 16, 2020
@papagian
Copy link
Contributor

I'll change the permissions to 0640 give the group read access. That sound alright to you @papagian @kylebrandt @marefr?

Being created with the grafana group is not always the case. For instance if you are in development mode then the file is created with the current user/group. However, I think that restricting read access to the group can have implications so I would say yes.

@papagian @kylebrandt Do you agree with @marefr about just logging a warning if the file exists with broader permissions?

yes, I think that is much better.

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1
Copy link
Contributor Author

aknuds1 commented Jul 16, 2020

I made a change to only log a warning if the SQLite file permissions are broader than expected (0640).

@aknuds1
Copy link
Contributor Author

aknuds1 commented Jul 16, 2020

@marefr @kylebrandt @papagian Please have another look.

Copy link
Contributor

@kylebrandt kylebrandt left a comment

Choose a reason for hiding this comment

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

Two possible code considerations for checking an error and moving this to its own function, but leave those to your judgement.

I agree with the logging choice.

@papagian
Copy link
Contributor

I've tested it again and works as expected in my development environment.

@aknuds1 aknuds1 merged commit 1024480 into master Jul 23, 2020
@aknuds1 aknuds1 deleted the bugfix/fix-8283 branch July 23, 2020 13:47
xlson added a commit to xlson/grafana that referenced this pull request Jul 24, 2020
* master: (195 commits)
  update latest to 7.1.1 (grafana#26588)
  changelog 7.1.1 (grafana#26586)
  Drone: Reduce parallelism of certain steps to conserve memory (grafana#26582)
  CloudWatch: Fix a few API status codes (grafana#26578)
  Drone: Identify Drone runner for each pipeline (grafana#26573)
  DashboardImport: Fix variable interpolation when property contains multiple variable expressions (grafana#26574)
  Update TextArea.mdx (grafana#26225)
  CloudWatch: Fix passing of namespace for getting custom metrics (grafana#26515)
  Alerting: Change settings column type to mediumtext (grafana#26549)
  Dashboard: Restore panel edit permission check, fixes 26555' (grafana#26556)
  Docs: Add variable examples (grafana#26565)
  Graph: support setting field units (grafana#26529)
  cloudwatch: Consolidate client logic (grafana#25555)
  Docs: Add dependencies for debian buster to image rendering plugin (grafana#26452)
  Fix self cloosing bracket to display input (grafana#26552)
  SQLite: Set 0640 permissions on SQLite database file (grafana#26339)
  Docs: Remove TODO from the CloudWatch docs (grafana#26543)
  Cloudwatch: Add af-south-1 region (grafana#26513)
  LDAP: fix LDAP test with special chars in username (grafana#26539)
  HTTPServer: add possibility to use additional middlewares (grafana#26514)
  ...
@aknuds1 aknuds1 added this to the 7.2 milestone Aug 5, 2020
@marefr marefr changed the title SQLite: Set 0640 permissions on SQLite database file Database: Set 0640 permissions on SQLite database file Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Wrong permissions in grafana package for grafana.db
5 participants