1 /* $Id: port-linux.c,v 1.12 2011/01/25 01:16: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 /* Wrapper around is_selinux_enabled() to log its return value once only */
43 ssh_selinux_enabled(void)
45 static int enabled
= -1;
48 enabled
= (is_selinux_enabled() == 1);
49 debug("SELinux support %s", enabled
? "enabled" : "disabled");
55 /* Return the default security context for the given username */
56 static security_context_t
57 ssh_selinux_getctxbyname(char *pwname
)
59 security_context_t sc
;
60 char *sename
= NULL
, *lvl
= NULL
;
63 #ifdef HAVE_GETSEUSERBYNAME
64 if (getseuserbyname(pwname
, &sename
, &lvl
) != 0)
71 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
72 r
= get_default_context_with_level(sename
, lvl
, NULL
, &sc
);
74 r
= get_default_context(sename
, NULL
, &sc
);
78 switch (security_getenforce()) {
80 fatal("%s: ssh_selinux_getctxbyname: "
81 "security_getenforce() failed", __func__
);
83 error("%s: Failed to get default SELinux security "
84 "context for %s", __func__
, pwname
);
87 fatal("%s: Failed to get default SELinux security "
88 "context for %s (in enforcing mode)",
93 #ifdef HAVE_GETSEUSERBYNAME
103 /* Set the execution context to the default for the specified user */
105 ssh_selinux_setup_exec_context(char *pwname
)
107 security_context_t user_ctx
= NULL
;
109 if (!ssh_selinux_enabled())
112 debug3("%s: setting execution context", __func__
);
114 user_ctx
= ssh_selinux_getctxbyname(pwname
);
115 if (setexeccon(user_ctx
) != 0) {
116 switch (security_getenforce()) {
118 fatal("%s: security_getenforce() failed", __func__
);
120 error("%s: Failed to set SELinux execution "
121 "context for %s", __func__
, pwname
);
124 fatal("%s: Failed to set SELinux execution context "
125 "for %s (in enforcing mode)", __func__
, pwname
);
128 if (user_ctx
!= NULL
)
131 debug3("%s: done", __func__
);
134 /* Set the TTY context for the specified user */
136 ssh_selinux_setup_pty(char *pwname
, const char *tty
)
138 security_context_t new_tty_ctx
= NULL
;
139 security_context_t user_ctx
= NULL
;
140 security_context_t old_tty_ctx
= NULL
;
142 if (!ssh_selinux_enabled())
145 debug3("%s: setting TTY context on %s", __func__
, tty
);
147 user_ctx
= ssh_selinux_getctxbyname(pwname
);
149 /* XXX: should these calls fatal() upon failure in enforcing mode? */
151 if (getfilecon(tty
, &old_tty_ctx
) == -1) {
152 error("%s: getfilecon: %s", __func__
, strerror(errno
));
156 if (security_compute_relabel(user_ctx
, old_tty_ctx
,
157 SECCLASS_CHR_FILE
, &new_tty_ctx
) != 0) {
158 error("%s: security_compute_relabel: %s",
159 __func__
, strerror(errno
));
163 if (setfilecon(tty
, new_tty_ctx
) != 0)
164 error("%s: setfilecon: %s", __func__
, strerror(errno
));
166 if (new_tty_ctx
!= NULL
)
167 freecon(new_tty_ctx
);
168 if (old_tty_ctx
!= NULL
)
169 freecon(old_tty_ctx
);
170 if (user_ctx
!= NULL
)
172 debug3("%s: done", __func__
);
176 ssh_selinux_change_context(const char *newname
)
179 char *oldctx
, *newctx
, *cx
;
181 if (!ssh_selinux_enabled())
184 if (getcon((security_context_t
*)&oldctx
) < 0) {
185 logit("%s: getcon failed with %s", __func__
, strerror (errno
));
188 if ((cx
= index(oldctx
, ':')) == NULL
|| (cx
= index(cx
+ 1, ':')) ==
190 logit ("%s: unparseable context %s", __func__
, oldctx
);
194 newlen
= strlen(oldctx
) + strlen(newname
) + 1;
195 newctx
= xmalloc(newlen
);
196 len
= cx
- oldctx
+ 1;
197 memcpy(newctx
, oldctx
, len
);
198 strlcpy(newctx
+ len
, newname
, newlen
- len
);
199 if ((cx
= index(cx
+ 1, ':')))
200 strlcat(newctx
, cx
, newlen
);
201 debug3("%s: setting context from '%s' to '%s'", __func__
, oldctx
,
203 if (setcon(newctx
) < 0)
204 logit("%s: setcon failed with %s", __func__
, strerror (errno
));
210 ssh_selinux_setfscreatecon(const char *path
)
212 security_context_t context
;
215 setfscreatecon(NULL
);
218 matchpathcon(path
, 0700, &context
);
219 setfscreatecon(context
);
222 #endif /* WITH_SELINUX */
224 #ifdef LINUX_OOM_ADJUST
226 * The magic "don't kill me" values, old and new, as documented in eg:
227 * http://lxr.linux.no/#linux+v2.6.32/Documentation/filesystems/proc.txt
228 * http://lxr.linux.no/#linux+v2.6.36/Documentation/filesystems/proc.txt
231 static int oom_adj_save
= INT_MIN
;
232 static char *oom_adj_path
= NULL
;
237 {"/proc/self/oom_score_adj", -1000}, /* kernels >= 2.6.36 */
238 {"/proc/self/oom_adj", -17}, /* kernels <= 2.6.35 */
243 * Tell the kernel's out-of-memory killer to avoid sshd.
244 * Returns the previous oom_adj value or zero.
247 oom_adjust_setup(void)
252 debug3("%s", __func__
);
253 for (i
= 0; oom_adjust
[i
].path
!= NULL
; i
++) {
254 oom_adj_path
= oom_adjust
[i
].path
;
255 value
= oom_adjust
[i
].value
;
256 if ((fp
= fopen(oom_adj_path
, "r+")) != NULL
) {
257 if (fscanf(fp
, "%d", &oom_adj_save
) != 1)
258 verbose("error reading %s: %s", oom_adj_path
,
262 if (fprintf(fp
, "%d\n", value
) <= 0)
263 verbose("error writing %s: %s",
264 oom_adj_path
, strerror(errno
));
266 verbose("Set %s from %d to %d",
267 oom_adj_path
, oom_adj_save
, value
);
276 /* Restore the saved OOM adjustment */
278 oom_adjust_restore(void)
282 debug3("%s", __func__
);
283 if (oom_adj_save
== INT_MIN
|| oom_adj_path
== NULL
||
284 (fp
= fopen(oom_adj_path
, "w")) == NULL
)
287 if (fprintf(fp
, "%d\n", oom_adj_save
) <= 0)
288 verbose("error writing %s: %s", oom_adj_path
, strerror(errno
));
290 verbose("Set %s to %d", oom_adj_path
, oom_adj_save
);
295 #endif /* LINUX_OOM_ADJUST */
296 #endif /* WITH_SELINUX || LINUX_OOM_ADJUST */