Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YxOvF-0000iq-VJ for pgsql-hackers@arkaria.postgresql.org; Wed, 27 May 2015 00:10:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YxOvE-0001rX-Kj for pgsql-hackers@arkaria.postgresql.org; Wed, 27 May 2015 00:10:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YxOvC-0001qd-O3 for pgsql-hackers@postgreSQL.org; Wed, 27 May 2015 00:10:10 +0000 Received: from tamriel.snowman.net ([2001:4830:167b::11]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1YxOv8-0001lF-0O for pgsql-hackers@postgreSQL.org; Wed, 27 May 2015 00:10:09 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id C3CDA5F7A0; Tue, 26 May 2015 20:10:04 -0400 (EDT) Date: Tue, 26 May 2015 20:10:04 -0400 From: Stephen Frost To: Noah Misch Cc: Tom Lane , pgsql-hackers@postgreSQL.org Subject: Re: [COMMITTERS] pgsql: Add pg_audit, an auditing extension Message-ID: <20150527001004.GD26667@tamriel.snowman.net> References: <21368.1431618712@sss.pgh.pa.us> <20150514161032.GA30322@tamriel.snowman.net> <4844.1431621244@sss.pgh.pa.us> <20150514163551.GB30322@tamriel.snowman.net> <5043.1431621669@sss.pgh.pa.us> <20150514164330.GD30322@tamriel.snowman.net> <5654.1431623278@sss.pgh.pa.us> <20150514171101.GG30322@tamriel.snowman.net> <6454.1431625129@sss.pgh.pa.us> <20150521053500.GB3934913@tornado.leadboat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="LmUak3kE33t3Bnfm" Content-Disposition: inline In-Reply-To: <20150521053500.GB3934913@tornado.leadboat.com> X-Editor: Vim http://www.vim.org/ X-Info: http://www.snowman.net X-Operating-System: Linux/3.13.0-46-generic (x86_64) X-Uptime: 19:00:17 up 68 days, 4:48, 18 users, load average: 0.18, 0.09, 0.06 User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --LmUak3kE33t3Bnfm Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Noah, * Noah Misch (noah@leadboat.com) wrote: > On Thu, May 14, 2015 at 01:38:49PM -0400, Tom Lane wrote: > > * The comments in the code betray utter ignorance of how logging actual= ly > > works, in particular this: > >=20 > > * Administrators can choose which log level the audit log is to be log= ged > > * at. The default level is LOG, which goes into the server log but do= es > > * not go to the client. Set to NOTICE in the regression tests. > >=20 > > All the user has to do is change client_min_messages and he'll see all = the > > reports, which means if you think that letting the user see the audit > > reports is a security problem then you have a hole a mile wide. >=20 > That indicates the patch's general readiness: While I agree that the comment was poorly worded and agreed with Tom that it should be changed, I do not agree that it's indicative of the patch's readiness, nor do I feel it's an issue that clients can see the auditing information, provided that it's clearly documented. I'm planning to commit a number of documentation updates (along with other updates) to pg_audit to address that. Using ereport() for query information isn't anything new- we do that in auto_explain, and we also report the query in the CONTEXT lines of error messages. We should (and I plan to) document that in the appropriate places and note that there are risks associated with it (eg: with security definer functions). > > + /* These are DDL, unless they are ROLE */ > > + case LOGSTMT_DDL: > > + className =3D CLASS_DDL; > > + class =3D LOG_DDL; > > + > > + /* Identify role statements */ > > + switch (stackItem->auditEvent.commandTag) > > + { > > + /* We know these are all role statements */ > > + case T_GrantStmt: > > + case T_GrantRoleStmt: > > + case T_CreateRoleStmt: > > + case T_DropRoleStmt: > > + case T_AlterRoleStmt: > > + case T_AlterRoleSetStmt: > > + className =3D CLASS_ROLE; > > + class =3D LOG_ROLE; > > + break; >=20 > Not T_AlterDefaultPrivilegesStmt? Agreed, that would be more sensible under ROLE than DDL. > > +static void > > +pg_audit_ProcessUtility_hook(Node *parsetree, > > + const char *queryString, > > + ProcessUtilityContext context, > > + ParamListInfo params, > > + DestReceiver *dest, > > + char *completionTag) > > +{ > > + AuditEventStackItem *stackItem =3D NULL; > > + int64 stackId =3D 0; > > + > > + /* > > + * Don't audit substatements. All the substatements we care about sh= ould > > + * be covered by the event triggers. > > + */ > > + if (context <=3D PROCESS_UTILITY_QUERY && !IsAbortedTransactionBlockS= tate()) >=20 > They aren't covered. A GRANT inside CREATE SCHEMA escapes auditing: Actually, they are covered. David and I discussed this extensively, prior to your review, and concluded that this approach works because the items not under the EventTrigger charter are shared catalogs, updates to which wouldn't ever make sense to happen under a CREATE SCHEMA. I do hope that we are able to improve on EventTriggers by supporting them for shared catalogs one day, but that is a discussion for another thread. The issue which you discovered here is that GRANTs were categorized under CLASS_ROLE, but we have a check at the top of the DDL event trigger which does an early-exit if DDL isn't selected for inclusion. That clearly needs to be changed, so that the GRANT under CREATE SCHEMA is caught and logged properly. We put a great deal of thought into any and all places where we filter data to do our best to prevent this from happening, taking steps beyond what a simple module would do to capture information and make sure that something is logged, even when we don't have all of the information available. That isn't to say there aren't bugs- certainly issues have been found through the various reviews and comments provided by multiple individuals now, and I don't pretend that there are no others, but I don't believe that this module is particularly more bug-ridden than other contrib modules or even parts of core. > I'm wary of the ease of forgetting to run CREATE EXTENSION. One gets much > auditing from GUCs alone; for example, we audit "CREATE TABLE t ()" with = or > without the extension, but only with the extension do we audit the inner > CREATE TABLE of "CREATE SCHEMA s CREATE TABLE t ()". A user that creates= a > database without creating the extension might look at the audit messages = and > mistakenly think the database is all set. I agree with this concern, but I don't believe there's a lot that we can do about it except to document it. I had asked, during the discussion of deparse and related pieces, that it be possible for us to directly call the necessary functions to get at the information we needed without having to go through the event trigger system. That didn't happen and I accepted that we would need to use CREATE EXTENSION. Users are certainly able to monitor their databases or include the extension in template1 to make sure it is pulled in for any new databases. > > + /* Return objects affected by the (non drop) DDL statement */ > > + query =3D "SELECT UPPER(object_type), object_identity\n" > > + " FROM pg_event_trigger_ddl_commands()"; >=20 > This SPI query neglects to schema-qualify its function calls. Agreed, will fix. > > + DefineCustomStringVariable( > > + "pg_audit.log", > > + > > + "Specifies which classes of statements will be logged by session aud= it " > > + "logging. Multiple classes can be provided using a comma-separated " > > + "list and classes can be subtracted by prefacing the class with a " > > + "- sign.", > > + > > + NULL, >=20 > The short_desc is several lines long, while long_desc is NULL. Good point, will address. > > --- /dev/null > > +++ b/contrib/pg_audit/sql/pg_audit.sql >=20 > I do applaud the breadth of test coverage. Thanks! We'll be adding more based on your feedback and that of Fujii and others. > > +-- Set pg_audit parameters for the current (super)user. > > +ALTER ROLE :current_user SET pg_audit.log =3D 'Role'; > > +ALTER ROLE :current_user SET pg_audit.log_level =3D 'notice'; >=20 > Do not ALTER the initial login role in a regression test. In the install= check > case, the role belongs to the test operator; it is not ours to modify. Alright, we could address that, but I imagine the right answer is to take the approach discussed below, instead of modifying the roles. > > +CREATE USER user1; > > +ALTER ROLE user1 SET pg_audit.log =3D 'ddl, ROLE'; > > +ALTER ROLE user1 SET pg_audit.log_level =3D 'notice'; > > + > > +-- > > +-- Create, select, drop (select will not be audited) > > +\connect - user1 >=20 > Adding new CREATE USER statements, as opposed to CREATE ROLE, to regressi= on > tests is almost always wrong. During "make check" on Windows, such users > cannot connect. "REGRESS_OPTS =3D --create-role=3Duser1" fixes that prob= lem but > does not help installcheck to pass under MD5 authentication (which it did= as > of commit c82725e). Skip past these challenges by using SET statements > instead of ALTER ROLE + \connect. Unfortunately, it's not possible to avoid the CREATE USER statements, if we wish to exercise the GUCs set via ALTER ROLE. GUCs set that way are only set on brand new connections as that specific role, and given that these are all SUSET GUCs, non-superusers wouldn't be able to change them. That said, the GUC system isn't really what we're trying to test here, and, I agree, we can simply SET/RESET from the initial superuser role throughout the regression tests and use SET ROLE instead. It's not exactly the same as testing with a real user who has GUCs set on their role, but it should work for the testing requirements of this module. > > + > > + Settings can be specified globally (in > > + postgresql.conf or using > > + ALTER SYSTEM ... SET), at the database level (using > > + ALTER DATABASE ... SET), or at the role level= (using > > + ALTER ROLE ... SET). Note that settings are = not > > + inherited through normal role inheritance and SET ROLE<= /> will > > + not alter a user's pg_audit settings. This is a lim= itation > > + of the roles system and not inherent to pg_audit. > > + >=20 > This paragraph applies equally to every SUSET GUC in PostgreSQL. Agreed, we should make sure this is adequately documented in the main docs and reference that from here instead. > > + > > + The pg_audit extension must be loaded in > > + . Otherwise, an = error > > + will be raised at load time and no audit logging will occur. > > + >=20 > The test suite is a counterexample to that. This was a relatively late change to appease the buildfarm. It was not, previously, the case, so the docs do need updating here. > > + > > + Object audit logging logs statements that affect a particular re= lation. > > + Only SELECT, INSERT, UPDATE = and > > + DELETE commands are supported. TRUNCATE= is not > > + included because there is no specific privilege for it. > > + >=20 > PostgreSQL has a TRUNCATE privilege: >=20 > GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRI= GGER } > [, ...] | ALL [ PRIVILEGES ] } > ON { [ TABLE ] table_name [, ...] > | ALL TABLES IN SCHEMA schema_name [, ...] } > TO role_specification [, ...] [ WITH GRANT OPTION ] Excellent point, we should be able to fix that. As I expect many of us remember, it wasn't always the case. > > + > > + Object-level audit logging is implemented via the roles system= =2E The > > + setting defines the role t= hat > > + will be used for audit logging. A relation (TABLE, > > + VIEW, etc.) will be audit logged when the audit ro= le has > > + permissions for the command executed or inherits the permissio= ns from > > + another role. This allows you to effectively have multiple au= dit roles > > + even though there is a single master role in any context. > > + >=20 > This is clever, but it means that any user can add to the list of audited > objects by granting individual object permissions, or the role itself, to= the > audit role. Crowding the audit log is one thing, but object owners can a= lso > use REVOKE to stop auditing an object. Perhaps the answer is to put all > important objects under trusted owners, but that's a heavy restriction. = Do > other RDBMS auditing features behave this way? Indeed, that's correct. It is certainly a limitation and one which I hope to remove eventually, but until then, we need to document it. I don't believe it's a reason to not have this capability. Object owners can also DROP objects or columns, or add columns which may not be included. There has been very little discussion about segregating permissions (or auditing) from object ownership, but we will hopefully, eventually, get there. However, even with such a heavy restriction, I have confidence that there will be significant users; certainly I have worked in a number of environments where this would have been a very welcome capability, even with its limitations. Please understand that these limitations were understood at the time of the design of the feature, they are not surprises, nor is there a misunderstanding regarding them. Further, I would be mildly surprised if our first pass at supporting auditing in-core doesn't have that same limitation of being subject to the object owner. It's a fall-back we've used for a long time and changing that will be quite difficult. > Stephen, please revert this feature. Most pg_audit bugs will create secu= rity > vulnerabilities, so incremental development in the PostgreSQL tree is the > wrong approach. The feature needs substantial hacking, then an additional > committer's thorough review. (The above sampling of defects and weak are= as > was not a thorough review.) I certainly understand the concern about this code potentially creating more security vulnerabilities than other areas, but I don't agree that we should avoid including the capability due to that fear. The prior discussion about incremental development was about adding the necessary pieces to core to eventually provide better auditing than what this provides; this module is very clearly intended to be a complete feature, with limitations which users need to be aware of prior to deploying it. I have a reasonably high degree of confidence that users who are interested in this capability will properly research the limitations and be able to decide for themselves if they are able to deploy it in their environments. The feature which is "auditing", as a whole, needs a great deal more than even simply removing the limitations associated with the design of this module. This was never intended to be the one true auditing solution, nor was it ever portrayed as such, but rather it's a good start with specific limitations that users need to be aware of. This is a step along the route to proper in-core auditing support, much the same as the various changes which have been made for logical replication and BDR, and as dblink was a step on the road towards FDWs. Just as we did not have the ultimate solution land in core in one release cycle for any of those, I don't expect this initial capability to be without limitations. I do not take this position lightly and it has been top-of-mind for me over these past days. I've further discussed it with community members, users of PG (those who are not our clients, who I've discussed it extensively with already), and read some of the discussion about how this capability may be used in non-auditing situations. Perhaps it should have been given a different name, one which would not come across as trying to address such a large, difficult, and contentious problem, but that is a two-edged sword, as users who this module would work for may not realize the capability that this module provides in that case. I certainly welcome review from others and if there is not another committer-level formal review before we get close on 9.5 (say, end of August), then I'll revert it. There is certainly no concern that doing so would be difficult to do, as it is entirely self-contained. Thanks! Stephen --LmUak3kE33t3Bnfm Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJVZQtcAAoJEO1sijiDR2RVS3cQAIsGYGRTl8Zi4qfcPzfn5621 oYHc82Vhai5uw4k/yiN1L5xP/MZwMyH1haH+D937t/UBLNOO+xsoVIhmLr9Ak0hx mRml3jKs58ESsB53WIssgvh1Ttgi7t7GdFTwSLXbMoUmve+AKN4MnLELHOw4n48P n7xCiYNxAPXeK/5A3DNwDQTdHdplFYpd7/5WPmmnugOp9YV27LJtIgct2r7LfbOZ HbTgOIm8Y4c/reBIbd75f3N5SnGMiKqHsc7G9Pgq2RYpuDL/tlbqHnnOOLvmb7i0 zTmllwkUVYArNgVkxOH7aPXhYuHgmQJUx+yH/WOS1/bPI5IUsUn3fUmuirDEAhuV rHJvhka3Sq+AIPoCe5NPkfXkw2WBFyrTtRiyWPJC5r0BlRezfYFyOczU1+jyx4Qn fJaR3ddAB0/s58fQnAn3UPgzrWAlam8en47zCZQdhbOwNMnJhRIXAhQphO51f/7W kQDQyinarSVvhcj4Oo9S2j+caAY9XiqsFCojA8Zjvj/GIyPZ34Iu8tC0sk/I1uFl 5fm9vm3cxwYS/bc9wY50LE2kue9ZXIWl//1dJ88Dtf7pX5CUKoepwZjSfqbaBB7k SuWdn31KOJiNK/LIgpU3bStNKCZopOX+xqxDtAbCiJDkRysdT1/fO3Ea53gidHrU sp7za68ds9ZK2cX/Ezjz =IJIk -----END PGP SIGNATURE----- --LmUak3kE33t3Bnfm--