diff -up openssh-6.3p1/openbsd-compat/port-linux.c.privsep-selinux openssh-6.3p1/openbsd-compat/port-linux.c --- openssh-6.3p1/openbsd-compat/port-linux.c.privsep-selinux 2013-10-10 14:58:20.634762245 +0200 +++ openssh-6.3p1/openbsd-compat/port-linux.c 2013-10-10 15:13:57.864306950 +0200 @@ -503,6 +503,25 @@ ssh_selinux_change_context(const char *n free(newctx); } +void +ssh_selinux_copy_context(void) +{ + security_context_t *ctx; + + if (!ssh_selinux_enabled()) + return; + + if (getexeccon((security_context_t *)&ctx) != 0) { + logit("%s: getcon failed with %s", __func__, strerror (errno)); + return; + } + if (ctx != NULL) { + if (setcon(ctx) != 0) + logit("%s: setcon failed with %s", __func__, strerror (errno)); + freecon(ctx); + } +} + #endif /* WITH_SELINUX */ #ifdef LINUX_OOM_ADJUST diff -up openssh-6.3p1/openbsd-compat/port-linux.h.privsep-selinux openssh-6.3p1/openbsd-compat/port-linux.h --- openssh-6.3p1/openbsd-compat/port-linux.h.privsep-selinux 2011-01-25 02:16:18.000000000 +0100 +++ openssh-6.3p1/openbsd-compat/port-linux.h 2013-10-10 14:58:20.634762245 +0200 @@ -24,6 +24,7 @@ int ssh_selinux_enabled(void); void ssh_selinux_setup_pty(char *, const char *); void ssh_selinux_setup_exec_context(char *); void ssh_selinux_change_context(const char *); +void ssh_selinux_copy_context(void); void ssh_selinux_setfscreatecon(const char *); #endif diff -up openssh-6.3p1/session.c.privsep-selinux openssh-6.3p1/session.c --- openssh-6.3p1/session.c.privsep-selinux 2013-10-10 14:58:20.617762326 +0200 +++ openssh-6.3p1/session.c 2013-10-10 15:13:16.520503590 +0200 @@ -1522,6 +1522,9 @@ do_setusercontext(struct passwd *pw) pw->pw_uid); chroot_path = percent_expand(tmp, "h", pw->pw_dir, "u", pw->pw_name, (char *)NULL); +#ifdef WITH_SELINUX + ssh_selinux_copy_context(); +#endif safely_chroot(chroot_path, pw->pw_uid); free(tmp); free(chroot_path); @@ -1544,6 +1547,12 @@ do_setusercontext(struct passwd *pw) /* Permanently switch to the desired uid. */ permanently_set_uid(pw); #endif + +#ifdef WITH_SELINUX + if (options.chroot_directory == NULL || + strcasecmp(options.chroot_directory, "none") == 0) + ssh_selinux_copy_context(); +#endif } else if (options.chroot_directory != NULL && strcasecmp(options.chroot_directory, "none") != 0) { fatal("server lacks privileges to chroot to ChrootDirectory"); @@ -1808,9 +1817,6 @@ do_child(Session *s, const char *command argv[i] = NULL; optind = optreset = 1; __progname = argv[0]; -#ifdef WITH_SELINUX - ssh_selinux_change_context("sftpd_t"); -#endif exit(sftp_server_main(i, argv, s->pw)); } diff -up openssh-6.3p1/sshd.c.privsep-selinux openssh-6.3p1/sshd.c --- openssh-6.3p1/sshd.c.privsep-selinux 2013-10-10 14:58:20.632762255 +0200 +++ openssh-6.3p1/sshd.c 2013-10-10 14:58:20.635762241 +0200 @@ -668,6 +668,10 @@ privsep_preauth_child(void) /* Demote the private keys to public keys. */ demote_sensitive_data(); +#ifdef WITH_SELINUX + ssh_selinux_change_context("sshd_net_t"); +#endif + /* Change our root directory */ if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1) fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR, @@ -811,6 +815,13 @@ privsep_postauth(Authctxt *authctxt) do_setusercontext(authctxt->pw); skip: +#ifdef WITH_SELINUX + /* switch SELinux content for root too */ + if (authctxt->pw->pw_uid == 0) { + ssh_selinux_copy_context(); + } +#endif + /* It is safe now to apply the key state */ monitor_apply_keystate(pmonitor);