|
Added
Link Here
|
| 1 |
/* $Id$ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2006 Damien Miller <djm@openbsd.org> |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and distribute this software for any |
| 7 |
* purpose with or without fee is hereby granted, provided that the above |
| 8 |
* copyright notice and this permission notice appear in all copies. |
| 9 |
* |
| 10 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* Linux-specified portability code - just SELinux support at present |
| 21 |
* |
| 22 |
* SELinux support based on patch from Daniel Walsh <dwalsh AT redhat.com> |
| 23 |
* http://bugzilla.mindrot.org/show_bug.cgi?id=880 |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "includes.h" |
| 27 |
|
| 28 |
#ifdef WITH_SELINUX |
| 29 |
#include "log.h" |
| 30 |
#include "port-linux.h" |
| 31 |
|
| 32 |
#include <selinux/selinux.h> |
| 33 |
#include <selinux/flask.h> |
| 34 |
#include <selinux/get_context_list.h> |
| 35 |
|
| 36 |
/* Wrapper around is_selinux_enabled() to log its return value once only */ |
| 37 |
static int |
| 38 |
ssh_selinux_enabled(void) |
| 39 |
{ |
| 40 |
static int enabled = -1; |
| 41 |
|
| 42 |
if (enabled == -1) { |
| 43 |
enabled = is_selinux_enabled(); |
| 44 |
debug("SELinux support %s", enabled ? "enabled" : "disabled"); |
| 45 |
} |
| 46 |
|
| 47 |
return (enabled); |
| 48 |
} |
| 49 |
|
| 50 |
/* Return the default security context for the given username */ |
| 51 |
static security_context_t |
| 52 |
ssh_selinux_getctxbyname(char *pwname) |
| 53 |
{ |
| 54 |
security_context_t sc; |
| 55 |
char *sename = NULL, *lvl = NULL; |
| 56 |
|
| 57 |
#ifdef HAVE_GETSEUSERBYNAME |
| 58 |
if (getseuserbyname(pwname, &sename, &lvl) != 0) |
| 59 |
return NULL; |
| 60 |
#else |
| 61 |
sename = pwname; |
| 62 |
lvl = NULL; |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL |
| 66 |
if (get_default_context_with_level(sename, lvl, NULL, &sc) != 0) { |
| 67 |
#else |
| 68 |
if (get_default_context(sename, NULL, &sc) != 0) { |
| 69 |
#endif |
| 70 |
switch (security_getenforce()) { |
| 71 |
case -1: |
| 72 |
fatal("%s: ssh_selinux_getctxbyname: " |
| 73 |
"security_getenforce() failed", __func__); |
| 74 |
case 0: |
| 75 |
error("%s: Failed to get default SELinux security " |
| 76 |
"context for %s", __func__, pwname); |
| 77 |
default: |
| 78 |
fatal("%s: Failed to get default SELinux security " |
| 79 |
"context for %s (in enforcing mode)", |
| 80 |
__func__, pwname); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#ifdef HAVE_GETSEUSERBYNAME |
| 85 |
if (sename != NULL) |
| 86 |
xfree(sename); |
| 87 |
if (lvl != NULL) |
| 88 |
xfree(lvl); |
| 89 |
#endif |
| 90 |
|
| 91 |
return (sc); |
| 92 |
} |
| 93 |
|
| 94 |
/* Set the execution context to the default for the specified user */ |
| 95 |
void |
| 96 |
ssh_selinux_setup_exec_context(char *pwname) |
| 97 |
{ |
| 98 |
security_context_t user_ctx = NULL; |
| 99 |
|
| 100 |
if (!ssh_selinux_enabled()) |
| 101 |
return; |
| 102 |
|
| 103 |
debug3("%s: setting execution context", __func__); |
| 104 |
|
| 105 |
user_ctx = ssh_selinux_getctxbyname(pwname); |
| 106 |
if (setexeccon(user_ctx) != 0) { |
| 107 |
switch (security_getenforce()) { |
| 108 |
case -1: |
| 109 |
fatal("%s: security_getenforce() failed", __func__); |
| 110 |
case 0: |
| 111 |
error("%s: Failed to set SELinux execution " |
| 112 |
"context for %s", __func__, pwname); |
| 113 |
default: |
| 114 |
fatal("%s: Failed to set SELinux execution context " |
| 115 |
"for %s (in enforcing mode)", __func__, pwname); |
| 116 |
} |
| 117 |
} |
| 118 |
if (user_ctx != NULL) |
| 119 |
freecon(user_ctx); |
| 120 |
|
| 121 |
debug3("%s: done", __func__); |
| 122 |
} |
| 123 |
|
| 124 |
/* Set the TTY context for the specified user */ |
| 125 |
void |
| 126 |
ssh_selinux_setup_pty(char *pwname, const char *tty) |
| 127 |
{ |
| 128 |
security_context_t new_tty_ctx = NULL; |
| 129 |
security_context_t user_ctx = NULL; |
| 130 |
security_context_t old_tty_ctx = NULL; |
| 131 |
|
| 132 |
if (!ssh_selinux_enabled()) |
| 133 |
return; |
| 134 |
|
| 135 |
debug3("%s: setting TTY context on %s", __func__, tty); |
| 136 |
|
| 137 |
user_ctx = ssh_selinux_getctxbyname(pwname); |
| 138 |
|
| 139 |
/* XXX: should these calls fatal() upon failure in enforcing mode? */ |
| 140 |
|
| 141 |
if (getfilecon(tty, &old_tty_ctx) == -1) { |
| 142 |
error("%s: getfilecon: %s", __func__, strerror(errno)); |
| 143 |
goto out; |
| 144 |
} |
| 145 |
|
| 146 |
if (security_compute_relabel(user_ctx, old_tty_ctx, |
| 147 |
SECCLASS_CHR_FILE, &new_tty_ctx) != 0) { |
| 148 |
error("%s: security_compute_relabel: %s", |
| 149 |
__func__, strerror(errno)); |
| 150 |
goto out; |
| 151 |
} |
| 152 |
|
| 153 |
if (setfilecon(tty, new_tty_ctx) != 0) |
| 154 |
error("%s: setfilecon: %s", __func__, strerror(errno)); |
| 155 |
out: |
| 156 |
if (new_tty_ctx != NULL) |
| 157 |
freecon(new_tty_ctx); |
| 158 |
if (old_tty_ctx != NULL) |
| 159 |
freecon(old_tty_ctx); |
| 160 |
if (user_ctx != NULL) |
| 161 |
freecon(user_ctx); |
| 162 |
debug3("%s: done", __func__); |
| 163 |
} |
| 164 |
#endif /* WITH_SELINUX */ |