b4df5eb
diff --git a/configure.ac b/configure.ac
b4df5eb
index aeef42a..d01e67e 100644
b4df5eb
--- a/configure.ac
b4df5eb
+++ b/configure.ac
b4df5eb
@@ -4998,6 +4998,37 @@ if test -n "$conf_lastlog_location"; then
b4df5eb
 		[Define if you want to specify the path to your lastlog file])
b4df5eb
 fi
b4df5eb
 
b4df5eb
+AC_ARG_WITH(libcap-ng,
b4df5eb
+	[  --with-libcap-ng=[auto/yes/no]  Add Libcap-ng support [default=auto]],,
b4df5eb
+	with_libcap_ng=auto)
b4df5eb
+
b4df5eb
+dnl libcap-ng detection
b4df5eb
+if test x$with_libcap_ng = xno ; then
b4df5eb
+	have_libcap_ng=no;
b4df5eb
+else
b4df5eb
+	# Start by checking for header file
b4df5eb
+	AC_CHECK_HEADER(cap-ng.h, capng_headers=yes, capng_headers=no)
b4df5eb
+
b4df5eb
+	# See if we have libcap-ng library
b4df5eb
+	AC_CHECK_LIB(cap-ng, capng_clear, CAPNG_LDADD=-lcap-ng,)
b4df5eb
+
b4df5eb
+	# Check results are usable
b4df5eb
+	if test x$with_libcap_ng = xyes -a x$CAPNG_LDADD = x ; then
b4df5eb
+	AC_MSG_ERROR(libcap-ng support was requested and the library was not found)
b4df5eb
+	fi
b4df5eb
+	if test x$CAPNG_LDADD != x -a $capng_headers = no ; then
b4df5eb
+	AC_MSG_ERROR(libcap-ng libraries found but headers are missing)
b4df5eb
+	fi
b4df5eb
+fi
b4df5eb
+AC_MSG_CHECKING(whether to use libcap-ng)
b4df5eb
+if test x$CAPNG_LDADD != x ; then
b4df5eb
+	AC_DEFINE(HAVE_LIBCAP_NG,1,[libcap-ng support])
b4df5eb
+	SSHDLIBS="$SSHDLIBS -lcap-ng"
b4df5eb
+	AC_MSG_RESULT(yes)
b4df5eb
+else
b4df5eb
+	AC_MSG_RESULT(no)
b4df5eb
+fi
b4df5eb
+
b4df5eb
 dnl utmp detection
b4df5eb
 AC_MSG_CHECKING([if your system defines UTMP_FILE])
b4df5eb
 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
b4df5eb
diff --git a/session.c b/session.c
b4df5eb
index 6cfcba4..80d2806 100644
b4df5eb
--- a/session.c
b4df5eb
+++ b/session.c
b4df5eb
@@ -96,6 +96,10 @@
b4df5eb
 #include "monitor_wrap.h"
b4df5eb
 #include "sftp.h"
b4df5eb
 
b4df5eb
+#ifdef HAVE_LIBCAP_NG
b4df5eb
+#include <cap-ng.h>
b4df5eb
+#endif
b4df5eb
+
b4df5eb
 #if defined(KRB5) && defined(USE_AFS)
b4df5eb
 #include <kafs.h>
b4df5eb
 #endif
b4df5eb
@@ -1586,6 +1590,7 @@ void
b4df5eb
 do_setusercontext(struct passwd *pw)
b4df5eb
 {
b4df5eb
 	char *chroot_path, *tmp;
b4df5eb
+	int dropped_suid = -1;
b4df5eb
 
b4df5eb
 	platform_setusercontext(pw);
b4df5eb
 
ecc9f8d
@@ -1619,10 +1624,25 @@ do_setusercontext(struct passwd *pw)
b4df5eb
 			    pw->pw_uid);
b4df5eb
 			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
b4df5eb
 			    "u", pw->pw_name, (char *)NULL);
b4df5eb
+#ifdef HAVE_LIBCAP_NG
b4df5eb
+			/* drop suid soon, retain SYS_CHROOT capability */
b4df5eb
+			capng_clear(CAPNG_SELECT_BOTH);
b4df5eb
+			capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SYS_CHROOT);
ecc9f8d
+			if (pw->pw_uid != 0 &&
ecc9f8d
+			    (dropped_suid = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_INIT_SUPP_GRP)) != 0)
b4df5eb
+				logit("capng_change_id() = %d (failure): Try to drop UID later", dropped_suid);
b4df5eb
+#endif
b4df5eb
 #ifdef WITH_SELINUX
b4df5eb
 			sshd_selinux_copy_context();
b4df5eb
 #endif
b4df5eb
 			safely_chroot(chroot_path, pw->pw_uid);
b4df5eb
+#ifdef HAVE_LIBCAP_NG
b4df5eb
+			/* Drop chroot capability. Already used */
b4df5eb
+			if (dropped_suid == 0) {
b4df5eb
+				capng_clear(CAPNG_SELECT_BOTH);
b4df5eb
+				capng_apply(CAPNG_SELECT_BOTH);
b4df5eb
+			}
b4df5eb
+#endif
b4df5eb
 			free(tmp);
b4df5eb
 			free(chroot_path);
b4df5eb
 			/* Make sure we don't attempt to chroot again */
b4df5eb
@@ -1654,8 +1673,9 @@ do_setusercontext(struct passwd *pw)
b4df5eb
 		if (!in_chroot && set_id(pw->pw_name) != 0)
b4df5eb
 			fatal("set_id(%s) Failed", pw->pw_name);
b4df5eb
 # endif /* USE_LIBIAF */
b4df5eb
-		/* Permanently switch to the desired uid. */
b4df5eb
-		permanently_set_uid(pw);
b4df5eb
+		/* Permanently switch to the desired uid if not yet done. */
b4df5eb
+		if (dropped_suid != 0)
b4df5eb
+			permanently_set_uid(pw);
b4df5eb
 #endif
b4df5eb
 
b4df5eb
 #ifdef WITH_SELINUX