sepgsql.sgml 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<!-- doc/src/sgml/sepgsql.sgml -->

<sect1 id="sepgsql">
 <title>sepgsql</title>

 <indexterm zone="sepgsql">
  <primary>sepgsql</primary>
 </indexterm>

 <para>
11 12
  <filename>sepgsql</> is a loadable module which supports label-based
  mandatory access control (MAC) based on <productname>SELinux</> security
13
  policy.
14 15
 </para>

16 17 18 19 20 21 22 23
 <warning>
   <para>
     This implementation has signification limitations, and does not enforce
     mandatory access control for all actions.  See
     <xref linkend="sepgsql-limitations">.
   </para>
 </warning>

24 25 26 27
 <sect2 id="sepgsql-overview">
  <title>Overview</title>

  <para>
28 29 30 31 32 33 34 35 36
   This module integrates with <productname>SELinux</> to provide an
   additional layer of security checking above and beyond what is normaly
   provided by <productname>PostgreSQL</productname>.  From the perspective of
   <productname>SELinux</>, this module allows
   <productname>PostgreSQL</productname> to function as a user-space object
   manager.  Each table or function access initiated by a DML query will be
   checked against the system security policy.  This check is an additional to
   the usual permissions checking performed by
   <productname>PostgreSQL</productname>.
37
  </para>
38

39
  <para>
40 41 42 43 44 45 46 47 48 49 50
   <productname>SELinux</productname> access control decisions are made using
   security labels, which are represented by strings such as
   <literal>system_u:object_r:sepgsql_table_t:s0</>.  Each access control
   decision involves two labels: the label of the subject attempting to
   perform the action, and the label of the object on which the operation is
   to be performed.  Since these labels can be applied to any sort of object,
   access control decisions for objects stored within the database can be
   (and, with this module, are) subjected to the same general criteria used
   for objects of any other type (e.g. files).  This design is intended to
   allow a centralized security policy to protect information assets
   independent of the particulars of how those assets are stored.
51
  </para>
52

53
  <para>
54 55
   The <xref linkend="sql-security-label"> statement allows assignment of
   a security label to a database object.
56
  </para>
57

58 59 60
 </sect2>
 <sect2 id="sepgsql-installation">
  <title>Installation</title>
61

62
  <para>
63 64 65 66 67 68 69
    This module can only be used on <productname>Linux</productname> 2.6.28
    or higher with <productname>SELinux</productname> enabled.  It is not
    available on any other platform, and must be explicitly enabled using
    <literal>--with-selinux</>.  You will also need <productname>libselinux</>
    2.0.93 or higher and <productname>selinux-policy</> 3.9.13 or higher
    (some distributions may backport the necessary rules into older policy
    versions).
70
  </para>
71

72
  <para>
73 74
   The <command>sestatus</> command allows you to check the status of
   <productname>SELinux</productname>.
75 76 77 78 79 80 81 82 83
<screen>
$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted
</screen>
84 85
   If <productname>SELinux</> is disabled or not installed, you must set
   that product up first before installing this module.
86
  </para>
87

88
  <para>
89 90 91 92 93 94
   To use this module, you must add include <literal>sepgsql</>
   in <xref linkend="guc-shared-preload-libraries">.  The module will not
   function if loaded in any other manner.  Once the module is loaded, you
   should execute <filename>sepgsql.sql</filename> in each database.
   This will install functions needed for security label management, and
   assign initial security labels.
95
  </para>
96

97
  <para>
98 99
   The following instructions that assume your installation is under the
   <filename>/usr/local/pgsql</> directory. Adjust the paths shown below as
100
   appropriate for your installaton.
101
  </para>
102

103
<screen>
104
$ initdb
105 106
$ vi $PGDATA/postgresql.conf
$ for DBNAME in template0 template1 postgres; do
107
  postgres --single -F -O -c exit_on_error=true $DBNAME \
108 109 110
      < /usr/local/pgsql/share/contrib/sepgsql.sql > /dev/null
  done
</screen>
111

112
  <para>
113 114
   If the installation process completes without error, you can now start the
   server normally.
115 116 117 118 119 120
  </para>
 </sect2>

 <sect2 id="sepgsql-regression">
  <title>Regression Tests</title>
  <para>
121 122 123
   Due to the nature of <productname>SELinux</productname>, running the
   regression tests for this module requires several additional configuration
   steps.
124
  </para>
125

126
  <para>
127
   First, install the policy package for the regression test.
128
   The <filename>sepgsql-regtest.pp</> is a special purpose policy package
129 130 131 132 133 134
   which provides a set of rules to be allowed during the regression tests.
   You need to install this policy package using the <command>semodule</>
   command, which links supplied policy packages and loads them
   into the kernel space.  If this packages is correctly installed,
   <literal><command>semodule</> -l</> should list sepgsql-regtest as an
   available policy package.
135
  </para>
136

137 138 139 140 141 142 143 144
<screen>
$ su
# semodule -u /usr/local/pgsql/share/contrib/sepgsql-regtest.pp
# semodule -l
    :
sepgsql-regtest 1.03
    :
</screen>
145

146
  <para>
147
   Second, turn on <literal>sepgsql_regression_test_mode</>.
148
   We don't enable all the rules in the <filename>sepgsql-regtest.pp</>
149
   by default, for your system's safety.
150 151 152 153
   The <literal>sepgsql_regression_test_mode</literal> parameter is associated
   with rules to launch regression test.
   It can be turned on using <command>setsebool</> command.
  </para>
154

155 156 157 158 159 160
<screen>
$ su
# setsebool sepgsql_regression_test_mode on
# getsebool sepgsql_regression_test_mode
sepgsql_regression_test_mode --> on
</screen>
161

162 163 164
  <para>
   Last, kick the regression test from the <literal>unconfined_t</> domain.
  </para>
165

166 167
  <para>
   The <command>id</> command tells us the current working domain.
168
   Confirm your shell is now performing with the <literal>unconfined_t</>
169 170 171 172 173 174
   domain as follows.
  </para>
<screen>
$ id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
</screen>
175

176
  <para>
177 178
   See <xref linkend="sepgsql-resources"> for details on adjusting your
   working domain, if necessary.
179
  </para>
180

181
  <para>
182 183 184 185 186
   If <command>pg_regress</> fails to launch the <command>psql</> command,
   you may need to ensure that the <command>psql</> command is labeled
   as <literal>bin_t</>.  If it is not, the <command>restorecon</> command can
   often be used to fix up security labels within the
   <productname>PostgreSQL</productname> installation directory.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  </para>

<screen>
$ restorecon -R /usr/local/pgsql/
</screen>
 </sect2>

 <sect2 id="sepgsql-parameters">
  <title>GUC Parameters</title>

  <variablelist>
   <varlistentry id="guc-sepgsql-permissive" xreflabel="sepgsql.permissive">
    <term><varname>sepgsql.permissive</> (<type>boolean</type>)</term>
    <indexterm>
     <primary><varname>sepgsql.permissive</> configuration parameter</primary>
    </indexterm>
    <listitem>
     <para>
205 206 207
      This parameter enables <productname>SE-PostgreSQL</> to function
      in permissive mode, regardless of the system setting.
      The default is off.
208 209 210
      This parameter can only be set in the <filename>postgresql.conf</>
      file or on the server command line.
     </para>
211

212
     <para>
213 214 215
      When this parameter is on, <productname>SE-PostgreSQL</> functions
      in permissive mode, even if the platform system is working in enforcing
      mode.  This parameter is primarily useful for testing purposes.
216 217
     </para>
    </listitem>
218

219 220 221 222 223 224 225 226
   </varlistentry>
   <varlistentry id="guc-sepgsql-debug-audit" xreflabel="sepgsql.debug_audit">
    <term><varname>sepgsql.debug_audit</> (<type>boolean</>)</>
    <indexterm>
     <primary><varname>sepgsql.debug_audit</> configuration parameter</>
    </indexterm>
    <listitem>
     <para>
227
      This parameter enables the printing of audit messages independent from
228 229 230
      the policy setting.
      The default is off (according to the security policy setting).
     </para>
231

232 233
     <para>
      The security policy of <productname>SELinux</> also has rules to
234 235 236
      control whether or not particular accesses are logged.
      By default, access violations are logged, but allowed
      accesses are not.
237
     </para>
238

239
     <para>
240 241
      This parameter forces all possible logging to be turned on, regardless
      of the system policy.
242 243 244 245 246 247 248 249 250
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </sect2>

 <sect2 id="sepgsql-features">
  <title>Features</title>
  <sect3>
251
   <title>Controlled Object Classes</title>
252 253 254
   <para>
    The security model of <productname>SELinux</> describes all the access
    control rules as a relationship between a subject entity (typically,
255 256 257 258
    it is a client of database) and an object entity, each of which is
    identified by a security label.  If access to an unlabelled object is
    attempted, the object is treated as if it were assigned the label
    <literal>unlabeled_t</>.
259
   </para>
260

261
   <para>
262 263 264
    Currently, <productname>sepgsql</productname> allows security labels to be
    assigned to schemas, tables, columns, sequences, views, and functions.
    When <productname>sepgsql</productname> is in use, security labels are
265
    automatically assigned to supported database objects at creation time.
266 267 268
    This label is called as a default security label, being decided according
    to the system security policy, which takes as input the creator's label
    and the label assigned to the new object's parent object.
269
   </para>
270

271
   <para>
272
    A new database object basically inherits the security label of the parent
273 274
    object, except when the security policy has special rules known as
    type-transition rules, in which case a different label may be applied.
275 276 277
    For schemas, the parent object is the current database; for columns, it
    is the corresponding table; for tables, sequences, views, and functions,
    it is the containing schema.
278 279
   </para>
  </sect3>
280

281 282
  <sect3>
   <title>DML Permissions</title>
283

284 285
   <para>
    For tables, <literal>db_table:select</>, <literal>db_table:insert</>,
286
    <literal>db_table:update</> or <literal>db_table:delete</> is
287
    checked for all the referenced target tables depending on the sort of
288
    statement;
289
    in addition, <literal>db_table:select</> is also checked for
290
    all the tables that contain the columns referenced in the
291 292 293
    <literal>WHERE</> or <literal>RETURNING</> clause, as a data source
    of <literal>UPDATE</>, and so on.
   </para>
294

295 296 297 298
   <para>
<synopsis>
UPDATE t1 SET x = 2, y = md5sum(y) WHERE z = 100;
</synopsis>
299

300
    In this case, we must have <literal>db_table:select</> in addition to
301
    <literal>db_table:update</>, because <literal>t1.a</> is referenced
302 303
    within the <literal>WHERE</> clause.  Column-level permissions will also be
    checked for each referenced column.
304
   </para>
305

306
   <para>
307 308 309 310
    The client must be allowed to access all referenced tables and
    columns, even if they originated from views which were then expanded,
    so that we apply consistent access control rules independent of the manner
    in which the table contents are referenced.
311
   </para>
312

313
   <para>
314
    For columns, <literal>db_column:select</> is checked on
315
    not only the columns being read using <literal>SELECT</>, but being
316
    referenced in other DML statements.
317
   </para>
318

319 320 321 322 323
   <para>
    Of course, it also checks <literal>db_column:update</> or
    <literal>db_column:insert</> on the column being modified by
    <literal>UPDATE</> or <literal>INSERT</>.
   </para>
324

325 326 327 328 329 330 331 332 333
   <para>
<synopsis>
UPDATE t1 SET x = 2, y = md5sum(y) WHERE z = 100;
</synopsis>
    In this case, it checks <literal>db_column:update</> on
    the <literal>t1.x</> being updated, <literal>db_column:{select update}</>
    on the <literal>t1.y</> being updated and referenced,
    and <literal>db_column:select</> on the <literal>t1.z</> being only
    referenced in the <literal>WHERE</> clause.
334 335
    <literal>db_table:{select update}</> will also be checked
    at the table level.
336
   </para>
337

338
   <para>
339 340 341 342
    For sequences, <literal>db_sequence:get_value</> is checked when we
    reference a sequence object using <literal>SELECT</>; however, note that we
    do not currently check permissions on execution of corresponding functions
    such as <literal>lastval()</>.
343
   </para>
344

345 346 347 348 349
   <para>
    For views, <literal>db_view:expand</> shall be checked, then any other
    corresponding permissions shall be also checked on the objects being
    expanded from the view, individually.
   </para>
350

351
   <para>
352
    For functions, <literal>db_procedure:{execute}</> is defined, but not
353 354
    checked in this version.
   </para>
355

356 357 358
   <para>
    The default database privilege system allows database superusers to
    modify system catalogs using DML commands, and reference or modify
359 360
    toast tables.  These operations are prohibited when
    <productname>sepgsql</> is enabled.
361 362
   </para>
  </sect3>
363

364 365 366 367 368 369 370 371
  <sect3>
   <title>DDL Permissions</title>
   <para>
    On <xref linkend="sql-security-label"> command, <literal>setattr</> and
    <literal>relabelfrom</> shall be checked on the object being relabeled
    with an old security label, then <literal>relabelto</> on the supplied
    new security label.
   </para>
372

373
   <para>
374
    In the case where multiple label providers are installed and the user tries
375 376
    to set a security label, but is not managed by <productname>SELinux</>,
    only <literal>setattr</> should be checked here.
377
    This is currently not checked due to implementation restrictions.
378 379
   </para>
  </sect3>
380

381 382 383
  <sect3>
   <title>Trusted Procedure</title>
   <para>
384 385 386 387 388 389 390 391
    Trusted procedures are similar to security definer functions or set-uid
    commands. <productname>SELinux</> provides a feature to allow trusted
    code to run using a security label different from that of the client,
    generally for the purpose of providing highly controlled access to
    sensitive data (e.g. rows might be omitted, or the precision of stored
    values might be reduced).  Whether or not a function acts as a trusted
    procedure is controlled by its security label and the operating system
    security policy.  For example:
392
   </para>
393

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
<screen>
postgres=# CREATE TABLE customer (
               cid     int primary key,
               cname   text,
               credit  text
           );
CREATE TABLE
postgres=# SECURITY LABEL ON COLUMN customer.credit
               IS 'system_u:object_r:sepgsql_secret_table_t:s0';
SECURITY LABEL
postgres=# CREATE FUNCTION show_credit(int) RETURNS text
             AS 'SELECT regexp_replace(credit, ''-[0-9]+$'', ''-xxxx'', ''g'')
                        FROM customer WHERE cid = $1'
           LANGUAGE sql;
CREATE FUNCTION
postgres=# SECURITY LABEL ON FUNCTION show_credit(int)
               IS 'system_u:object_r:sepgsql_trusted_proc_exec_t:s0';
SECURITY LABEL
</screen>
413

414
   <para>
415
    The above operations should be performed by an administrative user.
416
   </para>
417

418 419 420 421 422 423 424 425 426 427
<screen>
postgres=# SELECT * FROM customer;
ERROR:  SELinux: security policy violation
postgres=# SELECT cid, cname, show_credit(cid) FROM customer;
 cid | cname  |     show_credit
-----+--------+---------------------
   1 | taro   | 1111-2222-3333-xxxx
   2 | hanako | 5555-6666-7777-xxxx
(2 rows)
</screen>
428

429 430 431
   <para>
    In this case, a regular user cannot reference <literal>customer.credit</>
    directly, but a trusted procedure <literal>show_credit</> enables us
432 433
    to print the credit card number of customers with some of the digits masked
    out.
434 435
   </para>
  </sect3>
436

437 438 439
  <sect3>
   <title>Miscellaneous</title>
   <para>
440 441
    We reject the <xref linkend="sql-load"> command across the board, because
    any module loaded could easily circumvent security policy enforcement.
442
   </para>
443

444 445
  </sect3>
 </sect2>
446

447 448
 <sect2 id="sepgsql-limitations">
  <title>Limitations</title>
449

450 451 452 453 454
  <variablelist>
   <varlistentry>
    <term>Userspace access vector cache</term>
    <listitem>
     <para>
455 456
      <productname>sepgsql</> does not yet support an access vector cache.
      This would likely improve performance.
457 458 459
     </para>
    </listitem>
   </varlistentry>
460

461
   <varlistentry>
462
    <term>Data Definition Language (DDL) Permissions</term>
463 464
    <listitem>
     <para>
465
      Due to implementation restrictions, DDL permissions are not checked.
466 467 468
     </para>
    </listitem>
   </varlistentry>
469 470 471 472 473 474 475 476 477 478

   <varlistentry>
    <term>Data Control Language (DCL) Permissions</term>
    <listitem>
     <para>
      Due to implementation restrictions, DCL permissions are not checked.
     </para>
    </listitem>
   </varlistentry>

479 480 481 482
   <varlistentry>
    <term>Row-level access control</term>
    <listitem>
     <para>
483 484
      <productname>PostgreSQL</> does not support row-level access; therefore,
      <productname>sepgsql</productname> does not support it either.
485 486 487
     </para>
    </listitem>
   </varlistentry>
488

489 490 491 492
   <varlistentry>
    <term>Covert channels</term>
    <listitem>
     <para>
493
      <productname>sepgsql</> never tries to hide existence of
494 495 496 497 498 499
      a certain object, even if the user is not allowed to the reference.
      For example, we can infer the existence of an invisible object as
      a result of primary key conflicts, foreign key violations, and so on,
      even if we cannot reference contents of these objects.  The existence
      of a top secret table cannot be hidden; we only hope to conceal its
      contents.
500 501 502 503 504
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </sect2>
505

506 507 508 509 510 511 512
 <sect2 id="sepgsql-resources">
  <title>External Resources</title>
  <variablelist>
   <varlistentry>
    <term><ulink url="http://wiki.postgresql.org/wiki/SEPostgreSQL">SE-PostgreSQL Introduction</ulink></term>
    <listitem>
     <para>
513
      This wiki page provides a brief-overview, security design, architecture,
514
      administration and upcoming features.
515 516 517 518 519 520 521
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><ulink url="http://docs.fedoraproject.org/selinux-user-guide/">Fedora SELinux User Guide</ulink></term>
    <listitem>
     <para>
522
      This document provides a wide spectrum of knowledge to administer
523
      <productname>SELinux</> on your systems.
524
      It focuses primarily on Fedora, but is not limited to Fedora.
525 526 527 528 529 530 531
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><ulink url="http://docs.fedoraproject.org/selinux-faq">Fedora SELinux FAQ</ulink></term>
    <listitem>
     <para>
532
      This document answers frequently asked questions about
533
      <productname>SELinux</productname>.
534
      It focuses primarily on Fedora, but is not limited to Fedora.
535 536 537 538 539
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </sect2>
540

541 542 543
 <sect2 id="sepgsql-author">
  <title>Author</title>
  <para>
544
   KaiGai Kohei <email>kaigai@ak.jp.nec.com</email>
545 546 547
  </para>
 </sect2>
</sect1>