1 /* $Id: port-linux.c,v 1.17 2012/03/08 23:25:18 djm 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
108 /* Set the execution context to the default for the specified user */
110 ssh_selinux_setup_exec_context(char *pwname)
112 security_context_t user_ctx = NULL;
114 if (!ssh_selinux_enabled())
117 debug3("%s: setting execution context", __func__);
119 user_ctx = ssh_selinux_getctxbyname(pwname);
120 if (setexeccon(user_ctx) != 0) {
121 switch (security_getenforce()) {
123 fatal("%s: security_getenforce() failed", __func__);
125 error("%s: Failed to set SELinux execution "
126 "context for %s", __func__, pwname);
129 fatal("%s: Failed to set SELinux execution context "
130 "for %s (in enforcing mode)", __func__, pwname);
133 if (user_ctx != NULL)
136 debug3("%s: done", __func__);
139 /* Set the TTY context for the specified user */
141 ssh_selinux_setup_pty(char *pwname, const char *tty)
143 security_context_t new_tty_ctx = NULL;
144 security_context_t user_ctx = NULL;
145 security_context_t old_tty_ctx = NULL;
147 if (!ssh_selinux_enabled())
150 debug3("%s: setting TTY context on %s", __func__, tty);
152 user_ctx = ssh_selinux_getctxbyname(pwname);
154 /* XXX: should these calls fatal() upon failure in enforcing mode? */
156 if (getfilecon(tty, &old_tty_ctx) == -1) {
157 error("%s: getfilecon: %s", __func__, strerror(errno));
161 if (security_compute_relabel(user_ctx, old_tty_ctx,
162 SECCLASS_CHR_FILE, &new_tty_ctx) != 0) {
163 error("%s: security_compute_relabel: %s",
164 __func__, strerror(errno));
168 if (setfilecon(tty, new_tty_ctx) != 0)
169 error("%s: setfilecon: %s", __func__, strerror(errno));
171 if (new_tty_ctx != NULL)
172 freecon(new_tty_ctx);
173 if (old_tty_ctx != NULL)
174 freecon(old_tty_ctx);
175 if (user_ctx != NULL)
177 debug3("%s: done", __func__);
181 ssh_selinux_change_context(const char *newname)
184 char *oldctx, *newctx, *cx;
185 void (*switchlog) (const char *fmt,...) = logit;
187 if (!ssh_selinux_enabled())
190 if (getcon((security_context_t *)&oldctx) < 0) {
191 logit("%s: getcon failed with %s", __func__, strerror(errno));
194 if ((cx = index(oldctx, ':')) == NULL || (cx = index(cx + 1, ':')) ==
196 logit ("%s: unparseable context %s", __func__, oldctx);
201 * Check whether we are attempting to switch away from an unconfined
204 if (strncmp(cx, SSH_SELINUX_UNCONFINED_TYPE,
205 sizeof(SSH_SELINUX_UNCONFINED_TYPE) - 1) == 0)
208 newlen = strlen(oldctx) + strlen(newname) + 1;
209 newctx = xmalloc(newlen);
210 len = cx - oldctx + 1;
211 memcpy(newctx, oldctx, len);
212 strlcpy(newctx + len, newname, newlen - len);
213 if ((cx = index(cx + 1, ':')))
214 strlcat(newctx, cx, newlen);
215 debug3("%s: setting context from '%s' to '%s'", __func__,
217 if (setcon(newctx) < 0)
218 switchlog("%s: setcon %s from %s failed with %s", __func__,
219 newctx, oldctx, strerror(errno));
225 ssh_selinux_setfscreatecon(const char *path)
227 security_context_t context;
229 if (!ssh_selinux_enabled())
232 setfscreatecon(NULL);
235 if (matchpathcon(path, 0700, &context) == 0)
236 setfscreatecon(context);
239 #endif /* WITH_SELINUX */
241 #ifdef LINUX_OOM_ADJUST
243 * The magic "don't kill me" values, old and new, as documented in eg:
244 * http://lxr.linux.no/#linux+v2.6.32/Documentation/filesystems/proc.txt
245 * http://lxr.linux.no/#linux+v2.6.36/Documentation/filesystems/proc.txt
248 static int oom_adj_save = INT_MIN;
249 static char *oom_adj_path = NULL;
254 {"/proc/self/oom_score_adj", -1000}, /* kernels >= 2.6.36 */
255 {"/proc/self/oom_adj", -17}, /* kernels <= 2.6.35 */
260 * Tell the kernel's out-of-memory killer to avoid sshd.
261 * Returns the previous oom_adj value or zero.
264 oom_adjust_setup(void)
269 debug3("%s", __func__);
270 for (i = 0; oom_adjust[i].path != NULL; i++) {
271 oom_adj_path = oom_adjust[i].path;
272 value = oom_adjust[i].value;
273 if ((fp = fopen(oom_adj_path, "r+")) != NULL) {
274 if (fscanf(fp, "%d", &oom_adj_save) != 1)
275 verbose("error reading %s: %s", oom_adj_path,
279 if (fprintf(fp, "%d\n", value) <= 0)
280 verbose("error writing %s: %s",
281 oom_adj_path, strerror(errno));
283 verbose("Set %s from %d to %d",
284 oom_adj_path, oom_adj_save, value);
293 /* Restore the saved OOM adjustment */
295 oom_adjust_restore(void)
299 debug3("%s", __func__);
300 if (oom_adj_save == INT_MIN || oom_adj_path == NULL ||
301 (fp = fopen(oom_adj_path, "w")) == NULL)
304 if (fprintf(fp, "%d\n", oom_adj_save) <= 0)
305 verbose("error writing %s: %s", oom_adj_path, strerror(errno));
307 verbose("Set %s to %d", oom_adj_path, oom_adj_save);
312 #endif /* LINUX_OOM_ADJUST */
313 #endif /* WITH_SELINUX || LINUX_OOM_ADJUST */