Skip to contentSkip to conversation

Patches to enable chroot for all users

PATCH In review [email protected] 2 participants 5 messages Mar 20, 2013

Subsystems (6)
Networking VFS MM core Scheduler +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Patches to enable chroot for all users

From: Tal Tchwella <[email protected]>

I want to suggest adding chroot capability to all users.
The following patches enable that capability, while considering the security mechanism needed to disable escape routes for chroots by non-root users, and keeping chroot backward-compatible for root users.
The first patch establishes the concept of multi-user chroot, while disabling checks for root user in the chroot process.
The second patch checks whether a fd that is sent to a chroot application by a non-root user refers to a directory.
If that is the case, that fd is skipped, to disable an escape route.
The third patch disables the option for applications that have open fds to directories to be chrooted by a non-root user.

These patches were applied and test on linux-kernel 3.2.0-31-generic.

Any comments, feedback and suggestions are appreciated!

Thanks,
Tal

Tal Tchwella (3):
enabled chroot for all users
socket checks for uds fds transfer
open fds check when starting chroot

 fs/exec.c             |    3 +++
 fs/open.c             |   32 ++++++++++++++++++++++++++++++--
 include/linux/sched.h |   12 ++++++++++++
 init/main.c           |    1 +
 kernel/fork.c         |    2 ++
 net/core/scm.c        |    9 +++++++++
 6 files changed, 57 insertions(+), 2 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/3 enabled chroot for all users

From: Tal Tchwella <[email protected]>

This patch enables chroot for all users, not just root users, by disabling the appropriate checks in fs/open.c.
It also disables nested chroots by non-root users,
so they will not be able to break out of a chroot using classic techniques.

---
 fs/exec.c             |    3 +++
 fs/open.c             |    8 ++++++--
 include/linux/sched.h |   12 ++++++++++++
 init/main.c           |    1 +
 kernel/fork.c         |    2 ++
 5 files changed, 24 insertions(+), 2 deletions(-)
fs/exec.c +3 -0
index 7d27def..e2303ef 100644
@@ -1299,6 +1299,9 @@ int prepare_binprm(struct linux_binprm *bprm)
 	    !current->no_new_privs) {
 		/* Set-uid? */
 		if (mode & S_ISUID) {
+			/* chroot by a regular, non root, user? */
+			if (current->user_chroot == CHROOT_USER_MODE)
+				return -EACCES;
 			bprm->per_clear |= PER_CLEAR_ON_SETID;
 			bprm->cred->euid = inode->i_uid;
 		}
fs/open.c +6 -2
index a47a0a7..82832d8 100644
@@ -435,9 +435,13 @@ SYSCALL_DEFINE1(chroot, const char __user *, filename)
 	if (error)
 		goto dput_and_out;
 
-	error = -EPERM;
+	/* Are we in a nested chroot by a regular, non root, user? */
+	if (current->user_chroot != CHROOT_USER_MODE)
+		current->user_chroot = CHROOT_ROOT_MODE;
+
 	if (!capable(CAP_SYS_CHROOT))
-		goto dput_and_out;
+		current->user_chroot = CHROOT_USER_MODE;
+
 	error = security_path_chroot(&path);
 	if (error)
 		goto dput_and_out;
include/linux/sched.h +12 -0
index 9e960d4..315e6fe 100644
@@ -30,6 +30,16 @@
 #define CLONE_NEWNET		0x40000000	/* New network namespace */
 #define CLONE_IO		0x80000000	/* Clone io context */
 
+ /*
+  * chroot syscall Modes: INIT, USER, ROOT
+  * Init mode is to be used only by main.
+  * User mode is initialized when a non-root user tries to enter chroot mode.
+  * ROOT mode is the normal mode of operation for chroot.
+  */
+#define CHROOT_INIT		0
+#define CHROOT_USER_MODE	1
+#define CHROOT_ROOT_MODE	2
+
 /*
  * Scheduling policies
  */
@@ -1232,6 +1242,8 @@ struct task_struct {
 	unsigned int flags;	/* per process flags, defined below */
 	unsigned int ptrace;
 
+	unsigned int user_chroot;
+
 #ifdef CONFIG_SMP
 	struct llist_node wake_entry;
 	int on_cpu;
init/main.c +1 -0
index 0ec0731..044c655 100644
@@ -781,6 +781,7 @@ static noinline int init_post(void)
 
 
 	current->signal->flags |= SIGNAL_UNKILLABLE;
+	current->user_chroot = CHROOT_INIT;
 
 	if (ramdisk_execute_command) {
 		run_init_process(ramdisk_execute_command);
kernel/fork.c +2 -0
index 0269b2a..7f6d662 100644
@@ -276,6 +276,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
 	if (err)
 		goto out;
 
+	tsk->user_chroot = orig->user_chroot;
+
 	tsk->stack = ti;
 
 	setup_thread_stack(tsk, orig);
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/3 socket checks for uds fds transfer

In reply to [email protected]

From: Tal Tchwella <[email protected]>

This patch checks whether another user is trying to send a chrooted application by a non-root user a fd to a directory, which will allow it to escape. By preventing this kind of fd transfer to chrooted applications by non-root users, certain security risks are mitigated.

---
 net/core/scm.c |    9 +++++++++
 1 file changed, 9 insertions(+)
net/core/scm.c +9 -0
index ff52ad0..e505528 100644
@@ -282,6 +282,15 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
 	for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
 	     i++, cmfptr++)
 	{
+		/*
+		 * Restricts passing of fds via unix domain sockets to non-root
+		 * chrooted applications to files and does not allow directories
+		 * to be passed.
+		 */
+		if (current->user_chroot == CHROOT_USER_MODE) {
+			if (S_ISDIR(fp[i]->f_dentry->d_inode->i_mode))
+				continue;
+		}
 		int new_fd;
 		err = security_file_receive(fp[i]);
 		if (err)
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH 3/3 open fds check when starting chroot

In reply to [email protected]

From: Tal Tchwella <[email protected]>

This patch checks for open fds to directories when a non-root user tries to chroot, and does not allow that user to chroot if the application has an open fd to a directory because the appilcation has an escape path with that fd.

---
 fs/open.c |   24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
fs/open.c +24 -0
index 82832d8..6dc6443 100644
@@ -426,6 +426,30 @@ SYSCALL_DEFINE1(chroot, const char __user *, filename)
 {
 	struct path path;
 	int error;
+	struct files_struct *current_files;
+	struct fdtable *files_table;
+	int i = 0;
+
+	error = -EPERM;
+	/*
+	 * Checks to see if there are open file descriptors to directories
+	 * when a user that does not have the chroot capability
+	 * tries to chroot. Since chroot is availble to all users,
+	 * want to eliminate ways to break out. The second part
+	 * of the if statement, is true by default,
+	 * since during the initilization of the kernel, it
+	 * goes into chroot mode.
+	 */
+	if (!capable(CAP_SYS_CHROOT) && current->user_chroot != CHROOT_INIT) {
+		current_files = current->files;
+		files_table = files_fdtable(current_files);
+		while (files_table->fd[i] != NULL) {
+			if (S_ISDIR(files_table->fd[i]->
+				f_dentry->d_inode->i_mode))
+					 goto out;
+			i++;
+		}
+	}
 
 	error = user_path_dir(filename, &path);
 	if (error)
Signature · 1 line
1.7.9.5
#4 lore raw
In reply to [email protected]
Quoted 3 lines · On 3/20/2013 6:09 AM, [email protected] wrote:

From: Tal Tchwella <[email protected]>

I want to suggest adding chroot capability to all users.

It is much too easy to create an environment for tricking privileged programs using chroot to allow unprivileged processes to use chroot. /etc/shadow is your trivial attack surface.

Quoted 28 lines

The following patches enable that capability, while considering the security mechanism needed to disable escape routes for chroots by non-root users, and keeping chroot backward-compatible for root users.
The first patch establishes the concept of multi-user chroot, while disabling checks for root user in the chroot process.
The second patch checks whether a fd that is sent to a chroot application by a non-root user refers to a directory.
If that is the case, that fd is skipped, to disable an escape route.
The third patch disables the option for applications that have open fds to directories to be chrooted by a non-root user.

These patches were applied and test on linux-kernel 3.2.0-31-generic.

Any comments, feedback and suggestions are appreciated!

Thanks,
Tal

Tal Tchwella (3):
enabled chroot for all users
socket checks for uds fds transfer
open fds check when starting chroot

 fs/exec.c             |    3 +++
 fs/open.c             |   32 ++++++++++++++++++++++++++++++--
 include/linux/sched.h |   12 ++++++++++++
 init/main.c           |    1 +
 kernel/fork.c         |    2 ++
 net/core/scm.c        |    9 +++++++++
 6 files changed, 57 insertions(+), 2 deletions(-)