MySQL security top wish list

Security seems to have no boundaries. I’ve been tightening our database security lately, and it seems like this could go on forever: from app to console to privileges to server, there are so many aspects to managing database security. Unfortunately, this is a field where MySQL is in particular weak, and with very little work done in the many years I’ve been working with MySQL.

My very own top-wanted security features for MySQL follows. Surely this is but a small subset, your mileage may vary.

Autherntication-only SSL

By default, MySQL client API is unencrypted and passwords are sent in cleartext. MySQL supports SSL, but it an “all or nothing” deal: if you want to use SSL, then everything goes by SSL: any query, SELECT, DDL and whatnot.

[UPDATE]: Thanks to Davi & Jan for correcting me on this: passwords are not sent via cleartext. I’m not sure by which constellation I saw cleartext passwords being sent — but obviously that was long time ago. Just verified via tcpdump, got “mysql_native_password” message and no cleartext password. Lesson learned!

Roles

Need I elaborate? This is a fundamental construct in a database grant system. The effort of maintaining multiple accounts with similar/identical privileges is overwhelming. (PS I haven’t used Securich to date)

Host aggregation

In MySQL the combination of user+host makes for a distinct account. Thus, ‘gromit’@’192.168.%’ is a completely different account than ‘gromit’@’10.10.%’. I get the idea: you can have more privileges to, say, gromit@localhost than for gromit@’192.%’. In practice, this only makes a headache. In all my years, I have never encountered nor designed a privilege set where two accounts of the same user had different set of privileges. Never ever ever. It is confusing and pointless: if an account has a different set of roles, just call it by another name!

Had we had roles, that would be less painful; but my opinion is that an account should be identified by user only, not by user+host. The ‘host’ part should just indicate the whitelist of machines from which the user is allowed to connect.

Host blacklist

Speaking of whitelist, it would be great to have a host blacklist. If I wanted to grant access to ‘gromit’@’192.168.%’ except for ‘192.168.10.%’ — well, I would have to whitelist all the possible subnets. I can’t exclude a set of hosts.

Catalogues

Another common construct not available in MySQL: a level above “schema” and below “server”. The need for catalogues is in particular obvious when you want to grant some user SELECT access to all your schemas. Ahem, excluding, of course, the mysql schema… If I could create a “user catalogue”, as opposed to “system catalogue”, then I would have been able to GRANT SELECT ON CATALOGUE user.* TO my_user@localhost, and this would apply to all databases in that catalogue.

Privileges auditing

I’ve spent the last week or so restricting privileges to all accounts. This is hard work, because you want to make sure you’re not revoking privileges which are required by the system (in which case I would either choose not to revoke, or create a new dedicated account with requested set of privileges). It would be so much fun if I could turn a flag on, like “SET GLOBAL audit_privileges := 1”, and have a ++counter for every time a privilege check is made per account.

I guess we could go on… On a brighter note, I’ve been using the audit plugin interface by writing a login audit plugin with very good results (= good auditing & important insights); the (simple) code will be released shortly as open source; I’ll write more on this at a later stage.

30 thoughts on “MySQL security top wish list

  1. I think I should have taken the bet:

    mysql> grant process,create,replication client on *.* to 'shushu'@'%' identified by 'abcd';

    mysql> select user,host,password from mysql.user where user='shushu';
    +--------+------+-------------------------------------------+
    | user | host | password |
    +--------+------+-------------------------------------------+
    | shushu | % | *A154C52565E9E7F94BFC08A1FE702624ED8EFFDA |
    +--------+------+-------------------------------------------+

    mysql> -- blocking the user:
    mysql> set password for 'shushu'@'%'='A154C52565E9E7F94BFC08A1FE702624ED8EFFDA*';

    mysql> -- let's be on the safe side:
    mysql> flush privileges;
    mysql> create user 'shushu'@'127.0.0.%' identified by '12345';

    Now, from another console (using mysqlsandbox):

    bash$ ./use -h 127.0.0.1 -P 5532 -ushushu -p12345
    mysql> select current_user();
    +------------------+
    | current_user() |
    +------------------+
    | shushu@127.0.0.% |
    +------------------+

    mysql> show grants for current_user();
    +---------------------------------------------------------------------------------------------------------------+
    | Grants for shushu@127.0.0.% |
    +---------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'shushu'@'127.0.0.%' IDENTIFIED BY PASSWORD '*00A51F3F48415C7D4E8908980D443C29C69B60C9' |
    +---------------------------------------------------------------------------------------------------------------+

    mysql> show slave status;
    ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation

  2. It doesn’t work only for global privileges (see below). I guess we owe each other a drink 😉

    mysql> create user gromit@’%’;
    ————–
    create user gromit@’%’
    ————–

    Query OK, 0 rows affected (0.00 sec)

    mysql> create database secret;
    ————–
    create database secret
    ————–

    Query OK, 1 row affected (0.00 sec)

    mysql> create user gromit@localhost;
    ————–
    create user gromit@localhost
    ————–

    Query OK, 0 rows affected (0.00 sec)

    mysql> exit;
    Bye

    d:\mysql\work\mysql-5.6\bld\mysql-test>mtcli -u gromit -h localhost
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.6.15-debug-log Source distribution

    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

    mysql> create table secret.t1(a int);
    ————–
    create table secret.t1(a int)
    ————–

    ERROR 1142 (42000): CREATE command denied to user ‘gromit’@’localhost’ for table ‘t1’

    mysql> exit;
    Bye

    d:\mysql\work\mysql-5.6\bld\mysql-test>mtcli -u root
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.6.15-debug-log Source distribution

    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

    mysql> grant all privileges on secret.* to gromit@’%’;
    ————–
    grant all privileges on secret.* to gromit@’%’
    ————–

    Query OK, 0 rows affected (0.00 sec)

    mysql> exit;
    Bye

    d:\mysql\work\mysql-5.6\bld\mysql-test>mtcli -u gromit -h localhost
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.6.15-debug-log Source distribution

    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

    mysql> create table secret.t1(a int);
    ————–
    create table secret.t1(a int)
    ————–

    Query OK, 0 rows affected (0.05 sec)

    mysql> show grants;
    ————–
    show grants
    ————–

    +——————————————–+
    | Grants for gromit@localhost |
    +——————————————–+
    | GRANT USAGE ON *.* TO ‘gromit’@’localhost’ |
    +——————————————–+
    1 row in set (0.00 sec)

    mysql> select version();
    ————–
    select version()
    ————–

    +——————+
    | version() |
    +——————+
    | 5.6.15-debug-log |
    +——————+
    1 row in set (0.00 sec)

    mysql> select user(), current_user();
    ————–
    select user(), current_user()
    ————–

    +——————+——————+
    | user() | current_user() |
    +——————+——————+
    | gromit@localhost | gromit@localhost |
    +——————+——————+
    1 row in set (0.00 sec)

    mysql>

  3. Shlomi, you probably use one of the most popular sites in the world that has different permissions for the same account: Wikipedia.

    There are two different systems there. The dominant single signon version has one account that is exclusive across all wikis. Permissions for the account are different on each wiki. You can be an ordinary user on one, administrator or various types of superuser on another. All from one login.

    The older system has completely different logins for each wiki even if the user name is the same. This allows a larger virtual namespace, so even though I could bar anyone else on any other wiki from using the same user name as me by switching to single signon, I choose not to.

    I agree with you partially: it would be good to have a list of login locations that a single account can use. That is probably the dominant reason for having more than one name/host pair at the moment. I also agree about roles, they would make life a lot easier.

    I don’t fully agree that it is a bad idea to change permissions based on location. That’s a convenience for the user, so they can use one user name/password and get the right permissions for their location. But it would be good to reduce this to only the cases where it is desired by making it easier to administer multiple locations with the same user name/password. I think but not particularly strongly that it is somewhat desirable to have the potential to use the same user name with different password for different locations/permissions, but think it’ll mostly be undesired by most places.

    Views are my own, for an official Oracle opinion consult a PR person. But pay good attention to Joro: he’s likely to be lead on any work involving permissions. 🙂

    James Day, MySQL Senior Principal Support Engineer, Oracle

  4. James,
    Thanks for this interesting use case.
    I guess I’m much more oriented to letting web apps manage control over database objects rather than mapping app users directly to database accounts.

    Most of your description relates to different privileges on different schemata. But on “so they can use one user name/password and get the right permissions for their location” I confess I still don’t get it. It’s not single sign on for different systems: this is a single sign on depending on whether I’m at the office, home, local datacenter or nearby subnet. And you get different privileges.

    I am happy to acknowledge I’m not familiar with all use cases. I can see some benefit in having more privileges connecting from local host as opposed to remote servers – for the DBA perhaps – and I don’t see that databases should be too much topology oriented.

    My views are my own, too 🙂

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.