2011-09-01 14:37:33 +02:00
|
|
|
/* -------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* contrib/sepgsql/uavc.c
|
|
|
|
*
|
|
|
|
* Implementation of userspace access vector cache; that enables to cache
|
|
|
|
* access control decisions recently used, and reduce number of kernel
|
|
|
|
* invocations to avoid unnecessary performance hit.
|
|
|
|
*
|
2024-01-04 02:49:05 +01:00
|
|
|
* Copyright (c) 2011-2024, PostgreSQL Global Development Group
|
2011-09-01 14:37:33 +02:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "catalog/pg_proc.h"
|
|
|
|
#include "commands/seclabel.h"
|
2020-02-27 04:55:41 +01:00
|
|
|
#include "common/hashfn.h"
|
2019-10-23 05:56:22 +02:00
|
|
|
#include "sepgsql.h"
|
2011-09-01 14:37:33 +02:00
|
|
|
#include "storage/ipc.h"
|
|
|
|
#include "utils/guc.h"
|
|
|
|
#include "utils/memutils.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* avc_cache
|
|
|
|
*
|
|
|
|
* It enables to cache access control decision (and behavior on execution of
|
|
|
|
* trusted procedure, db_procedure class only) for a particular pair of
|
|
|
|
* security labels and object class in userspace.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
uint32 hash; /* hash value of this cache entry */
|
|
|
|
char *scontext; /* security context of the subject */
|
|
|
|
char *tcontext; /* security context of the target */
|
|
|
|
uint16 tclass; /* object class of the target */
|
2011-09-01 14:37:33 +02:00
|
|
|
|
2012-06-10 21:20:04 +02:00
|
|
|
uint32 allowed; /* permissions to be allowed */
|
|
|
|
uint32 auditallow; /* permissions to be audited on allowed */
|
|
|
|
uint32 auditdeny; /* permissions to be audited on denied */
|
2011-09-01 14:37:33 +02:00
|
|
|
|
2012-06-10 21:20:04 +02:00
|
|
|
bool permissive; /* true, if permissive rule */
|
|
|
|
bool hot_cache; /* true, if recently referenced */
|
2011-09-01 14:37:33 +02:00
|
|
|
bool tcontext_is_valid;
|
2012-06-10 21:20:04 +02:00
|
|
|
/* true, if tcontext is valid */
|
|
|
|
char *ncontext; /* temporary scontext on execution of trusted
|
|
|
|
* procedure, or NULL elsewhere */
|
2017-06-21 20:39:04 +02:00
|
|
|
} avc_cache;
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Declaration of static variables
|
|
|
|
*/
|
|
|
|
#define AVC_NUM_SLOTS 512
|
|
|
|
#define AVC_NUM_RECLAIM 16
|
|
|
|
#define AVC_DEF_THRESHOLD 384
|
|
|
|
|
2012-06-10 21:20:04 +02:00
|
|
|
static MemoryContext avc_mem_cxt;
|
|
|
|
static List *avc_slots[AVC_NUM_SLOTS]; /* avc's hash buckets */
|
|
|
|
static int avc_num_caches; /* number of caches currently used */
|
|
|
|
static int avc_lru_hint; /* index of the buckets to be reclaimed next */
|
|
|
|
static int avc_threshold; /* threshold to launch cache-reclaiming */
|
|
|
|
static char *avc_unlabeled; /* system 'unlabeled' label */
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash function
|
|
|
|
*/
|
|
|
|
static uint32
|
|
|
|
sepgsql_avc_hash(const char *scontext, const char *tcontext, uint16 tclass)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
return hash_any((const unsigned char *) scontext, strlen(scontext))
|
|
|
|
^ hash_any((const unsigned char *) tcontext, strlen(tcontext))
|
2011-09-01 14:37:33 +02:00
|
|
|
^ tclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset all the avc caches
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sepgsql_avc_reset(void)
|
|
|
|
{
|
|
|
|
MemoryContextReset(avc_mem_cxt);
|
|
|
|
|
|
|
|
memset(avc_slots, 0, sizeof(List *) * AVC_NUM_SLOTS);
|
|
|
|
avc_num_caches = 0;
|
|
|
|
avc_lru_hint = 0;
|
|
|
|
avc_unlabeled = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reclaim caches recently unreferenced
|
2012-06-10 21:20:04 +02:00
|
|
|
*/
|
2011-09-01 14:37:33 +02:00
|
|
|
static void
|
|
|
|
sepgsql_avc_reclaim(void)
|
|
|
|
{
|
|
|
|
ListCell *cell;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
while (avc_num_caches >= avc_threshold - AVC_NUM_RECLAIM)
|
|
|
|
{
|
|
|
|
index = avc_lru_hint;
|
|
|
|
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
foreach(cell, avc_slots[index])
|
2011-09-01 14:37:33 +02:00
|
|
|
{
|
|
|
|
avc_cache *cache = lfirst(cell);
|
|
|
|
|
|
|
|
if (!cache->hot_cache)
|
|
|
|
{
|
|
|
|
avc_slots[index]
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
= foreach_delete_current(avc_slots[index], cell);
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
pfree(cache->scontext);
|
|
|
|
pfree(cache->tcontext);
|
|
|
|
if (cache->ncontext)
|
|
|
|
pfree(cache->ncontext);
|
|
|
|
pfree(cache);
|
|
|
|
|
|
|
|
avc_num_caches--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cache->hot_cache = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
avc_lru_hint = (avc_lru_hint + 1) % AVC_NUM_SLOTS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-27 14:37:25 +02:00
|
|
|
/* -------------------------------------------------------------------------
|
|
|
|
*
|
2011-09-01 14:37:33 +02:00
|
|
|
* sepgsql_avc_check_valid
|
|
|
|
*
|
2011-09-27 14:37:25 +02:00
|
|
|
* This function checks whether the cached entries are still valid. If
|
|
|
|
* the security policy has been reloaded (or any other events that requires
|
|
|
|
* resetting userspace caches has occurred) since the last reference to
|
|
|
|
* the access vector cache, we must flush the cache.
|
|
|
|
*
|
|
|
|
* Access control decisions must be atomic, but multiple system calls may
|
|
|
|
* be required to make a decision; thus, when referencing the access vector
|
|
|
|
* cache, we must loop until we complete without an intervening cache flush
|
2014-05-06 18:12:18 +02:00
|
|
|
* event. In practice, looping even once should be very rare. Callers should
|
2011-09-27 14:37:25 +02:00
|
|
|
* do something like this:
|
|
|
|
*
|
2012-06-10 21:20:04 +02:00
|
|
|
* sepgsql_avc_check_valid();
|
|
|
|
* do {
|
|
|
|
* :
|
|
|
|
* <reference to uavc>
|
|
|
|
* :
|
|
|
|
* } while (!sepgsql_avc_check_valid())
|
2011-09-27 14:37:25 +02:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
sepgsql_avc_check_valid(void)
|
|
|
|
{
|
|
|
|
if (selinux_status_updated() > 0)
|
|
|
|
{
|
|
|
|
sepgsql_avc_reset();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_avc_unlabeled
|
|
|
|
*
|
2012-06-10 21:20:04 +02:00
|
|
|
* Returns an alternative label to be applied when no label or an invalid
|
2011-09-27 14:37:25 +02:00
|
|
|
* label would otherwise be assigned.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
sepgsql_avc_unlabeled(void)
|
|
|
|
{
|
|
|
|
if (!avc_unlabeled)
|
|
|
|
{
|
2020-08-14 02:30:34 +02:00
|
|
|
char *unlabeled;
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
if (security_get_initial_context_raw("unlabeled", &unlabeled) < 0)
|
|
|
|
ereport(ERROR,
|
2012-06-10 21:20:04 +02:00
|
|
|
(errcode(ERRCODE_INTERNAL_ERROR),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
errmsg("SELinux: failed to get initial security label: %m")));
|
2011-09-01 14:37:33 +02:00
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
avc_unlabeled = MemoryContextStrdup(avc_mem_cxt, unlabeled);
|
|
|
|
}
|
2019-11-01 11:09:52 +01:00
|
|
|
PG_FINALLY();
|
2011-09-01 14:37:33 +02:00
|
|
|
{
|
|
|
|
freecon(unlabeled);
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
}
|
|
|
|
return avc_unlabeled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* sepgsql_avc_compute
|
2011-09-01 14:37:33 +02:00
|
|
|
*
|
|
|
|
* A fallback path, when cache mishit. It asks SELinux its access control
|
|
|
|
* decision for the supplied pair of security context and object class.
|
|
|
|
*/
|
|
|
|
static avc_cache *
|
|
|
|
sepgsql_avc_compute(const char *scontext, const char *tcontext, uint16 tclass)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
char *ucontext = NULL;
|
|
|
|
char *ncontext = NULL;
|
|
|
|
MemoryContext oldctx;
|
|
|
|
avc_cache *cache;
|
|
|
|
uint32 hash;
|
|
|
|
int index;
|
|
|
|
struct av_decision avd;
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
hash = sepgsql_avc_hash(scontext, tcontext, tclass);
|
|
|
|
index = hash % AVC_NUM_SLOTS;
|
|
|
|
|
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* Validation check of the supplied security context. Because it always
|
|
|
|
* invoke system-call, frequent check should be avoided. Unless security
|
|
|
|
* policy is reloaded, validation status shall be kept, so we also cache
|
|
|
|
* whether the supplied security context was valid, or not.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
2020-08-14 02:30:34 +02:00
|
|
|
if (security_check_context_raw(tcontext) != 0)
|
2011-09-01 14:37:33 +02:00
|
|
|
ucontext = sepgsql_avc_unlabeled();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ask SELinux its access control decision
|
|
|
|
*/
|
|
|
|
if (!ucontext)
|
|
|
|
sepgsql_compute_avd(scontext, tcontext, tclass, &avd);
|
|
|
|
else
|
|
|
|
sepgsql_compute_avd(scontext, ucontext, tclass, &avd);
|
|
|
|
|
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* It also caches a security label to be switched when a client labeled as
|
|
|
|
* 'scontext' executes a procedure labeled as 'tcontext', not only access
|
|
|
|
* control decision on the procedure. The security label to be switched
|
|
|
|
* shall be computed uniquely on a pair of 'scontext' and 'tcontext',
|
|
|
|
* thus, it is reasonable to cache the new label on avc, and enables to
|
|
|
|
* reduce unnecessary system calls. It shall be referenced at
|
|
|
|
* sepgsql_needs_fmgr_hook to check whether the supplied function is a
|
|
|
|
* trusted procedure, or not.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
if (tclass == SEPG_CLASS_DB_PROCEDURE)
|
|
|
|
{
|
|
|
|
if (!ucontext)
|
|
|
|
ncontext = sepgsql_compute_create(scontext, tcontext,
|
2013-03-28 20:38:35 +01:00
|
|
|
SEPG_CLASS_PROCESS, NULL);
|
2011-09-01 14:37:33 +02:00
|
|
|
else
|
|
|
|
ncontext = sepgsql_compute_create(scontext, ucontext,
|
2013-03-28 20:38:35 +01:00
|
|
|
SEPG_CLASS_PROCESS, NULL);
|
2011-09-01 14:37:33 +02:00
|
|
|
if (strcmp(scontext, ncontext) == 0)
|
|
|
|
{
|
|
|
|
pfree(ncontext);
|
|
|
|
ncontext = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up an avc_cache object
|
|
|
|
*/
|
|
|
|
oldctx = MemoryContextSwitchTo(avc_mem_cxt);
|
|
|
|
|
|
|
|
cache = palloc0(sizeof(avc_cache));
|
|
|
|
|
2012-06-10 21:20:04 +02:00
|
|
|
cache->hash = hash;
|
2011-09-01 14:37:33 +02:00
|
|
|
cache->scontext = pstrdup(scontext);
|
|
|
|
cache->tcontext = pstrdup(tcontext);
|
|
|
|
cache->tclass = tclass;
|
|
|
|
|
|
|
|
cache->allowed = avd.allowed;
|
|
|
|
cache->auditallow = avd.auditallow;
|
|
|
|
cache->auditdeny = avd.auditdeny;
|
|
|
|
cache->hot_cache = true;
|
|
|
|
if (avd.flags & SELINUX_AVD_FLAGS_PERMISSIVE)
|
|
|
|
cache->permissive = true;
|
|
|
|
if (!ucontext)
|
|
|
|
cache->tcontext_is_valid = true;
|
|
|
|
if (ncontext)
|
|
|
|
cache->ncontext = pstrdup(ncontext);
|
|
|
|
|
|
|
|
avc_num_caches++;
|
|
|
|
|
|
|
|
if (avc_num_caches > avc_threshold)
|
|
|
|
sepgsql_avc_reclaim();
|
|
|
|
|
|
|
|
avc_slots[index] = lcons(cache, avc_slots[index]);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldctx);
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_avc_lookup
|
|
|
|
*
|
2011-09-27 14:37:25 +02:00
|
|
|
* Look up a cache entry that matches the supplied security contexts and
|
|
|
|
* object class. If not found, create a new cache entry.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
static avc_cache *
|
|
|
|
sepgsql_avc_lookup(const char *scontext, const char *tcontext, uint16 tclass)
|
|
|
|
{
|
|
|
|
avc_cache *cache;
|
|
|
|
ListCell *cell;
|
|
|
|
uint32 hash;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
hash = sepgsql_avc_hash(scontext, tcontext, tclass);
|
|
|
|
index = hash % AVC_NUM_SLOTS;
|
|
|
|
|
2012-06-10 21:20:04 +02:00
|
|
|
foreach(cell, avc_slots[index])
|
2011-09-01 14:37:33 +02:00
|
|
|
{
|
|
|
|
cache = lfirst(cell);
|
|
|
|
|
|
|
|
if (cache->hash == hash &&
|
|
|
|
cache->tclass == tclass &&
|
|
|
|
strcmp(cache->tcontext, tcontext) == 0 &&
|
|
|
|
strcmp(cache->scontext, scontext) == 0)
|
|
|
|
{
|
|
|
|
cache->hot_cache = true;
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not found, so insert a new cache */
|
|
|
|
return sepgsql_avc_compute(scontext, tcontext, tclass);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_avc_check_perms(_label)
|
|
|
|
*
|
|
|
|
* It returns 'true', if the security policy suggested to allow the required
|
|
|
|
* permissions. Otherwise, it returns 'false' or raises an error according
|
2012-09-05 20:01:15 +02:00
|
|
|
* to the 'abort_on_violation' argument.
|
2011-09-01 14:37:33 +02:00
|
|
|
* The 'tobject' and 'tclass' identify the target object being referenced,
|
|
|
|
* and 'required' is a bitmask of permissions (SEPG_*__*) defined for each
|
|
|
|
* object classes.
|
|
|
|
* The 'audit_name' is the object name (optional). If SEPGSQL_AVC_NOAUDIT
|
|
|
|
* was supplied, it means to skip all the audit messages.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
sepgsql_avc_check_perms_label(const char *tcontext,
|
|
|
|
uint16 tclass, uint32 required,
|
2012-09-05 20:01:15 +02:00
|
|
|
const char *audit_name,
|
|
|
|
bool abort_on_violation)
|
2011-09-01 14:37:33 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
char *scontext = sepgsql_get_client_label();
|
2011-09-01 14:37:33 +02:00
|
|
|
avc_cache *cache;
|
|
|
|
uint32 denied;
|
|
|
|
uint32 audited;
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
sepgsql_avc_check_valid();
|
2012-06-10 21:20:04 +02:00
|
|
|
do
|
|
|
|
{
|
2011-09-01 14:37:33 +02:00
|
|
|
result = true;
|
|
|
|
|
|
|
|
/*
|
2011-09-27 14:37:25 +02:00
|
|
|
* If the target object is unlabeled, we perform the check using the
|
|
|
|
* label supplied by sepgsql_avc_unlabeled().
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
if (tcontext)
|
|
|
|
cache = sepgsql_avc_lookup(scontext, tcontext, tclass);
|
|
|
|
else
|
|
|
|
cache = sepgsql_avc_lookup(scontext,
|
|
|
|
sepgsql_avc_unlabeled(), tclass);
|
|
|
|
|
|
|
|
denied = required & ~cache->allowed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute permissions to be audited
|
|
|
|
*/
|
|
|
|
if (sepgsql_get_debug_audit())
|
|
|
|
audited = (denied ? (denied & ~0) : (required & ~0));
|
|
|
|
else
|
|
|
|
audited = denied ? (denied & cache->auditdeny)
|
2012-06-10 21:20:04 +02:00
|
|
|
: (required & cache->auditallow);
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
if (denied)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* In permissive mode or permissive domain, violated permissions
|
2011-09-27 14:37:25 +02:00
|
|
|
* shall be audited to the log files at once, and then implicitly
|
2012-06-10 21:20:04 +02:00
|
|
|
* allowed to avoid a flood of access denied logs, because the
|
|
|
|
* purpose of permissive mode/domain is to collect a violation log
|
|
|
|
* that will make it possible to fix up the security policy.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
if (!sepgsql_getenforce() || cache->permissive)
|
|
|
|
cache->allowed |= required;
|
|
|
|
else
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
} while (!sepgsql_avc_check_valid());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the case when we have something auditable actions here,
|
2012-06-10 21:20:04 +02:00
|
|
|
* sepgsql_audit_log shall be called with text representation of security
|
|
|
|
* labels for both of subject and object. It records this access
|
|
|
|
* violation, so DBA will be able to find out unexpected security problems
|
|
|
|
* later.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
if (audited != 0 &&
|
|
|
|
audit_name != SEPGSQL_AVC_NOAUDIT &&
|
|
|
|
sepgsql_get_mode() != SEPGSQL_MODE_INTERNAL)
|
|
|
|
{
|
2016-03-27 17:17:00 +02:00
|
|
|
sepgsql_audit_log(denied != 0,
|
2022-01-12 20:23:13 +01:00
|
|
|
(sepgsql_getenforce() && !cache->permissive),
|
2011-09-01 14:37:33 +02:00
|
|
|
cache->scontext,
|
|
|
|
cache->tcontext_is_valid ?
|
|
|
|
cache->tcontext : sepgsql_avc_unlabeled(),
|
|
|
|
cache->tclass,
|
|
|
|
audited,
|
|
|
|
audit_name);
|
|
|
|
}
|
|
|
|
|
2012-09-05 20:01:15 +02:00
|
|
|
if (abort_on_violation && !result)
|
2011-09-01 14:37:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("SELinux: security policy violation")));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sepgsql_avc_check_perms(const ObjectAddress *tobject,
|
|
|
|
uint16 tclass, uint32 required,
|
2012-09-05 20:01:15 +02:00
|
|
|
const char *audit_name,
|
|
|
|
bool abort_on_violation)
|
2011-09-01 14:37:33 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
char *tcontext = GetSecurityLabel(tobject, SEPGSQL_LABEL_TAG);
|
|
|
|
bool rc;
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
rc = sepgsql_avc_check_perms_label(tcontext,
|
|
|
|
tclass, required,
|
2012-09-05 20:01:15 +02:00
|
|
|
audit_name, abort_on_violation);
|
2011-09-01 14:37:33 +02:00
|
|
|
if (tcontext)
|
|
|
|
pfree(tcontext);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_avc_trusted_proc
|
|
|
|
*
|
2011-09-27 14:37:25 +02:00
|
|
|
* If the supplied function OID is configured as a trusted procedure, this
|
|
|
|
* function will return a security label to be used during the execution of
|
|
|
|
* that function. Otherwise, it returns NULL.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
sepgsql_avc_trusted_proc(Oid functionId)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
char *scontext = sepgsql_get_client_label();
|
|
|
|
char *tcontext;
|
|
|
|
ObjectAddress tobject;
|
|
|
|
avc_cache *cache;
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
tobject.classId = ProcedureRelationId;
|
|
|
|
tobject.objectId = functionId;
|
|
|
|
tobject.objectSubId = 0;
|
|
|
|
tcontext = GetSecurityLabel(&tobject, SEPGSQL_LABEL_TAG);
|
|
|
|
|
|
|
|
sepgsql_avc_check_valid();
|
2012-06-10 21:20:04 +02:00
|
|
|
do
|
|
|
|
{
|
2011-09-01 14:37:33 +02:00
|
|
|
if (tcontext)
|
|
|
|
cache = sepgsql_avc_lookup(scontext, tcontext,
|
|
|
|
SEPG_CLASS_DB_PROCEDURE);
|
|
|
|
else
|
|
|
|
cache = sepgsql_avc_lookup(scontext, sepgsql_avc_unlabeled(),
|
|
|
|
SEPG_CLASS_DB_PROCEDURE);
|
|
|
|
} while (!sepgsql_avc_check_valid());
|
|
|
|
|
|
|
|
return cache->ncontext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_avc_exit
|
|
|
|
*
|
2011-09-27 14:37:25 +02:00
|
|
|
* Clean up userspace AVC on process exit.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sepgsql_avc_exit(int code, Datum arg)
|
|
|
|
{
|
|
|
|
selinux_status_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_avc_init
|
|
|
|
*
|
2011-09-27 14:37:25 +02:00
|
|
|
* Initialize the userspace AVC. This should be called from _PG_init.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_avc_init(void)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
int rc;
|
2011-09-01 14:37:33 +02:00
|
|
|
|
|
|
|
/*
|
Add macros to make AllocSetContextCreate() calls simpler and safer.
I found that half a dozen (nearly 5%) of our AllocSetContextCreate calls
had typos in the context-sizing parameters. While none of these led to
especially significant problems, they did create minor inefficiencies,
and it's now clear that expecting people to copy-and-paste those calls
accurately is not a great idea. Let's reduce the risk of future errors
by introducing single macros that encapsulate the common use-cases.
Three such macros are enough to cover all but two special-purpose contexts;
those two calls can be left as-is, I think.
While this patch doesn't in itself improve matters for third-party
extensions, it doesn't break anything for them either, and they can
gradually adopt the simplified notation over time.
In passing, change TopMemoryContext to use the default allocation
parameters. Formerly it could only be extended 8K at a time. That was
probably reasonable when this code was written; but nowadays we create
many more contexts than we did then, so that it's not unusual to have a
couple hundred K in TopMemoryContext, even without considering various
dubious code that sticks other things there. There seems no good reason
not to let it use growing blocks like most other contexts.
Back-patch to 9.6, mostly because that's still close enough to HEAD that
it's easy to do so, and keeping the branches in sync can be expected to
avoid some future back-patching pain. The bugs fixed by these changes
don't seem to be significant enough to justify fixing them further back.
Discussion: <21072.1472321324@sss.pgh.pa.us>
2016-08-27 23:50:38 +02:00
|
|
|
* All the avc stuff shall be allocated in avc_mem_cxt
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
avc_mem_cxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"userspace access vector cache",
|
Add macros to make AllocSetContextCreate() calls simpler and safer.
I found that half a dozen (nearly 5%) of our AllocSetContextCreate calls
had typos in the context-sizing parameters. While none of these led to
especially significant problems, they did create minor inefficiencies,
and it's now clear that expecting people to copy-and-paste those calls
accurately is not a great idea. Let's reduce the risk of future errors
by introducing single macros that encapsulate the common use-cases.
Three such macros are enough to cover all but two special-purpose contexts;
those two calls can be left as-is, I think.
While this patch doesn't in itself improve matters for third-party
extensions, it doesn't break anything for them either, and they can
gradually adopt the simplified notation over time.
In passing, change TopMemoryContext to use the default allocation
parameters. Formerly it could only be extended 8K at a time. That was
probably reasonable when this code was written; but nowadays we create
many more contexts than we did then, so that it's not unusual to have a
couple hundred K in TopMemoryContext, even without considering various
dubious code that sticks other things there. There seems no good reason
not to let it use growing blocks like most other contexts.
Back-patch to 9.6, mostly because that's still close enough to HEAD that
it's easy to do so, and keeping the branches in sync can be expected to
avoid some future back-patching pain. The bugs fixed by these changes
don't seem to be significant enough to justify fixing them further back.
Discussion: <21072.1472321324@sss.pgh.pa.us>
2016-08-27 23:50:38 +02:00
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
2011-09-01 14:37:33 +02:00
|
|
|
memset(avc_slots, 0, sizeof(avc_slots));
|
|
|
|
avc_num_caches = 0;
|
|
|
|
avc_lru_hint = 0;
|
|
|
|
avc_threshold = AVC_DEF_THRESHOLD;
|
|
|
|
|
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* SELinux allows to mmap(2) its kernel status page in read-only mode to
|
|
|
|
* inform userspace applications its status updating (such as policy
|
|
|
|
* reloading) without system-call invocations. This feature is only
|
|
|
|
* supported in Linux-2.6.38 or later, however, libselinux provides a
|
|
|
|
* fallback mode to know its status using netlink sockets.
|
2011-09-01 14:37:33 +02:00
|
|
|
*/
|
|
|
|
rc = selinux_status_open(1);
|
|
|
|
if (rc < 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INTERNAL_ERROR),
|
|
|
|
errmsg("SELinux: could not open selinux status : %m")));
|
|
|
|
else if (rc > 0)
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("SELinux: kernel status page uses fallback mode")));
|
|
|
|
|
2011-09-27 14:37:25 +02:00
|
|
|
/* Arrange to close selinux status page on process exit. */
|
2011-09-01 14:37:33 +02:00
|
|
|
on_proc_exit(sepgsql_avc_exit, 0);
|
|
|
|
}
|