1 /* $Id: port-linux.c,v 1.18 2013/06/01 22:07:32 dtucker Exp $ */
4 * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
5 * Copyright (c) 2006 Damien Miller <djm@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Linux-specific portability code - just SELinux support at present
26 #if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
34 #include "port-linux.h"
37 #include <selinux/selinux.h>
38 #include <selinux/flask.h>
39 #include <selinux/get_context_list.h>
41 #ifndef SSH_SELINUX_UNCONFINED_TYPE
42 # define SSH_SELINUX_UNCONFINED_TYPE ":unconfined_t:"
45 /* Wrapper around is_selinux_enabled() to log its return value once only */
47 ssh_selinux_enabled(void)
49 static int enabled = -1;
52 enabled = (is_selinux_enabled() == 1);
53 debug("SELinux support %s", enabled ? "enabled" : "disabled");
59 /* Return the default security context for the given username */
60 static security_context_t
61 ssh_selinux_getctxbyname(char *pwname)
63 security_context_t sc = NULL;
64 char *sename = NULL, *lvl = NULL;
67 #ifdef HAVE_GETSEUSERBYNAME
68 if (getseuserbyname(pwname, &sename, &lvl) != 0)
75 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
76 r = get_default_context_with_level(sename, lvl, NULL, &sc);
78 r = get_default_context(sename, NULL, &sc);
82 switch (security_getenforce()) {
84 fatal("%s: ssh_selinux_getctxbyname: "
85 "security_getenforce() failed", __func__);
87 error("%s: Failed to get default SELinux security "
88 "context for %s", __func__, pwname);
92 fatal("%s: Failed to get default SELinux security "
93 "context for %s (in enforcing mode)",
98 #ifdef HAVE_GETSEUSERBYNAME
106 /* Set the execution context to the default for the specified user */
108 ssh_selinux_setup_exec_context(char *pwname)
110 security_context_t user_ctx = NULL;
112 if (!ssh_selinux_enabled())
115 debug3("%s: setting execution context", __func__);
117 user_ctx = ssh_selinux_getctxbyname(pwname);
118 if (setexeccon(user_ctx) != 0) {
119 switch (security_getenforce()) {
121 fatal("%s: security_getenforce() failed", __func__);
123 error("%s: Failed to set SELinux execution "
124 "context for %s", __func__, pwname);
127 fatal("%s: Failed to set SELinux execution context "
128 "for %s (in enforcing mode)", __func__, pwname);
131 if (user_ctx != NULL)
134 debug3("%s: done", __func__);
137 /* Set the TTY context for the specified user */
139 ssh_selinux_setup_pty(char *pwname, const char *tty)
141 security_context_t new_tty_ctx = NULL;
142 security_context_t user_ctx = NULL;
143 security_context_t old_tty_ctx = NULL;
145 if (!ssh_selinux_enabled())
148 debug3("%s: setting TTY context on %s", __func__, tty);
150 user_ctx = ssh_selinux_getctxbyname(pwname);
152 /* XXX: should these calls fatal() upon failure in enforcing mode? */
154 if (getfilecon(tty, &old_tty_ctx) == -1) {
155 error("%s: getfilecon: %s", __func__, strerror(errno));
159 if (security_compute_relabel(user_ctx, old_tty_ctx,
160 SECCLASS_CHR_FILE, &new_tty_ctx) != 0) {
161 error("%s: security_compute_relabel: %s",
162 __func__, strerror(errno));
166 if (setfilecon(tty, new_tty_ctx) != 0)
167 error("%s: setfilecon: %s", __func__, strerror(errno));
169 if (new_tty_ctx != NULL)
170 freecon(new_tty_ctx);
171 if (old_tty_ctx != NULL)
172 freecon(old_tty_ctx);
173 if (user_ctx != NULL)
175 debug3("%s: done", __func__);
179 ssh_selinux_change_context(const char *newname)
182 char *oldctx, *newctx, *cx;
183 void (*switchlog) (const char *fmt,...) = logit;
185 if (!ssh_selinux_enabled())
188 if (getcon((security_context_t *)&oldctx) < 0) {
189 logit("%s: getcon failed with %s", __func__, strerror(errno));
192 if ((cx = index(oldctx, ':')) == NULL || (cx = index(cx + 1, ':')) ==
194 logit ("%s: unparseable context %s", __func__, oldctx);
199 * Check whether we are attempting to switch away from an unconfined
202 if (strncmp(cx, SSH_SELINUX_UNCONFINED_TYPE,
203 sizeof(SSH_SELINUX_UNCONFINED_TYPE) - 1) == 0)
206 newlen = strlen(oldctx) + strlen(newname) + 1;
207 newctx = xmalloc(newlen);
208 len = cx - oldctx + 1;
209 memcpy(newctx, oldctx, len);
210 strlcpy(newctx + len, newname, newlen - len);
211 if ((cx = index(cx + 1, ':')))
212 strlcat(newctx, cx, newlen);
213 debug3("%s: setting context from '%s' to '%s'", __func__,
215 if (setcon(newctx) < 0)
216 switchlog("%s: setcon %s from %s failed with %s", __func__,
217 newctx, oldctx, strerror(errno));
223 ssh_selinux_setfscreatecon(const char *path)
225 security_context_t context;
227 if (!ssh_selinux_enabled())
230 setfscreatecon(NULL);
233 if (matchpathcon(path, 0700, &context) == 0)
234 setfscreatecon(context);
237 #endif /* WITH_SELINUX */
239 #ifdef LINUX_OOM_ADJUST
241 * The magic "don't kill me" values, old and new, as documented in eg:
242 * http://lxr.linux.no/#linux+v2.6.32/Documentation/filesystems/proc.txt
243 * http://lxr.linux.no/#linux+v2.6.36/Documentation/filesystems/proc.txt
246 static int oom_adj_save = INT_MIN;
247 static char *oom_adj_path = NULL;
252 {"/proc/self/oom_score_adj", -1000}, /* kernels >= 2.6.36 */
253 {"/proc/self/oom_adj", -17}, /* kernels <= 2.6.35 */
258 * Tell the kernel's out-of-memory killer to avoid sshd.
259 * Returns the previous oom_adj value or zero.
262 oom_adjust_setup(void)
267 debug3("%s", __func__);
268 for (i = 0; oom_adjust[i].path != NULL; i++) {
269 oom_adj_path = oom_adjust[i].path;
270 value = oom_adjust[i].value;
271 if ((fp = fopen(oom_adj_path, "r+")) != NULL) {
272 if (fscanf(fp, "%d", &oom_adj_save) != 1)
273 verbose("error reading %s: %s", oom_adj_path,
277 if (fprintf(fp, "%d\n", value) <= 0)
278 verbose("error writing %s: %s",
279 oom_adj_path, strerror(errno));
281 debug("Set %s from %d to %d",
282 oom_adj_path, oom_adj_save, value);
291 /* Restore the saved OOM adjustment */
293 oom_adjust_restore(void)
297 debug3("%s", __func__);
298 if (oom_adj_save == INT_MIN || oom_adj_path == NULL ||
299 (fp = fopen(oom_adj_path, "w")) == NULL)
302 if (fprintf(fp, "%d\n", oom_adj_save) <= 0)
303 verbose("error writing %s: %s", oom_adj_path, strerror(errno));
305 debug("Set %s to %d", oom_adj_path, oom_adj_save);
310 #endif /* LINUX_OOM_ADJUST */
311 #endif /* WITH_SELINUX || LINUX_OOM_ADJUST */