Compare commits
14 Commits
master
...
release4.0
Author | SHA1 | Date |
---|---|---|
Marek Marczykowski-Górecki | facdf4d3ea | 4 years ago |
Marek Marczykowski-Górecki | ffdc3e558a | 4 years ago |
Patrick Schleizer | 2716a39e48 | 4 years ago |
Patrick Schleizer | 3ed9157069 | 4 years ago |
Marek Marczykowski-Górecki | ea433821ac | 4 years ago |
Frédéric Pierret (fepitre) | 4068ac8dd1 | 4 years ago |
Marek Marczykowski-Górecki | c2beb1703a | 5 years ago |
xaki23 | ac13973ded | 5 years ago |
Marek Marczykowski-Górecki | 484f67a618 | 5 years ago |
xaki23 | 4a28ab6e3d | 5 years ago |
Marek Marczykowski-Górecki | ff7f65858e | 5 years ago |
M. Vefa Bicakci | 5a83961a88 | 5 years ago |
Marek Marczykowski-Górecki | 425f7f4f07 | 5 years ago |
Marek Marczykowski-Górecki | 6501b26a36 | 5 years ago |
@ -0,0 +1,3 @@
|
|||||||
|
usr/include/libqrexec-utils.h
|
||||||
|
usr/include/qrexec.h
|
||||||
|
usr/lib/libqrexec-utils.so
|
@ -0,0 +1 @@
|
|||||||
|
usr/lib/libqrexec-utils.so.2*
|
@ -0,0 +1 @@
|
|||||||
|
libqrexec-utils 2 libqrexec-utils2 (>= 4.0.16)
|
@ -0,0 +1 @@
|
|||||||
|
debian/tmp/usr/src/u2mfn-3.1.2/dkms.conf
|
@ -1,5 +1,13 @@
|
|||||||
install: install-scripts
|
ver := $(shell cat ../version)
|
||||||
|
srcdir := /usr/src/u2mfn-$(ver)
|
||||||
|
|
||||||
|
install: install-u2mfn install-scripts
|
||||||
|
|
||||||
install-scripts:
|
install-scripts:
|
||||||
install -d $(DESTDIR)/usr/sbin
|
install -d $(DESTDIR)/usr/sbin
|
||||||
install qubes-prepare-vm-kernel $(DESTDIR)/usr/sbin
|
install qubes-prepare-vm-kernel $(DESTDIR)/usr/sbin
|
||||||
|
|
||||||
|
install-u2mfn:
|
||||||
|
install -d $(DESTDIR)$(srcdir)
|
||||||
|
install -m 644 u2mfn/u2mfn.c u2mfn/Makefile $(DESTDIR)$(srcdir)
|
||||||
|
sed -e 's#@@VERSION@@#$(ver)#' u2mfn/dkms.conf.in > $(DESTDIR)$(srcdir)/dkms.conf
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
#
|
||||||
|
# The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
#
|
||||||
|
# Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or
|
||||||
|
# modify it under the terms of the GNU General Public License
|
||||||
|
# as published by the Free Software Foundation; either version 2
|
||||||
|
# of the License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program; if not, write to the Free Software
|
||||||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
obj-m += u2mfn.o
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o *.ko *~
|
||||||
|
rm -f .u2mfn.* *.mod.c Module.symvers modules.order
|
||||||
|
rm -fr .tmp_versions
|
||||||
|
rm -f Makefile.xen
|
@ -0,0 +1,12 @@
|
|||||||
|
PACKAGE_VERSION="@@VERSION@@"
|
||||||
|
|
||||||
|
# Items below here should not have to change with each driver version
|
||||||
|
PACKAGE_NAME="u2mfn"
|
||||||
|
|
||||||
|
BUILT_MODULE_NAME[0]="u2mfn"
|
||||||
|
DEST_MODULE_LOCATION[0]="/extra/"
|
||||||
|
AUTOINSTALL=yes
|
||||||
|
# BUILD_EXCLUSIVE_KERNEL doesn't support negation :(
|
||||||
|
if echo "$kernelver"|grep -q qubes; then
|
||||||
|
BUILD_EXCLUSIVE_KERNEL=not-on-qubes-kernel
|
||||||
|
fi
|
@ -0,0 +1,180 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/version.h>
|
||||||
|
#include <linux/proc_fs.h>
|
||||||
|
#include <linux/fs.h>
|
||||||
|
#include <linux/mm.h>
|
||||||
|
#include <linux/sched.h>
|
||||||
|
#ifndef FOREIGN_FRAME_BIT
|
||||||
|
#include <xen/page.h>
|
||||||
|
#endif
|
||||||
|
#include <linux/highmem.h>
|
||||||
|
|
||||||
|
/* copy of /usr/include/u2mfn-kernel.h, to reduce requirements */
|
||||||
|
#include <linux/ioctl.h>
|
||||||
|
#define U2MFN_MAGIC 0xf5
|
||||||
|
#define U2MFN_GET_MFN_FOR_PAGE _IOW(U2MFN_MAGIC, 1, int)
|
||||||
|
#define U2MFN_GET_LAST_MFN _IO(U2MFN_MAGIC, 2)
|
||||||
|
|
||||||
|
|
||||||
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
|
||||||
|
static inline unsigned long virt_to_phys(volatile void *address)
|
||||||
|
{
|
||||||
|
return __pa((unsigned long) address);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef virt_to_mfn
|
||||||
|
#define VIRT_TO_MFN virt_to_mfn
|
||||||
|
#else
|
||||||
|
extern unsigned long *phys_to_machine_mapping;
|
||||||
|
static inline unsigned long VIRT_TO_MFN(void *addr)
|
||||||
|
{
|
||||||
|
return phys_to_machine_mapping[virt_to_phys(addr) >> PAGE_SHIFT] & ~FOREIGN_FRAME_BIT;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0)
|
||||||
|
static int u2mfn_get_mfn(pte_t *pte, unsigned long addr, void *data) {
|
||||||
|
*((unsigned long *) data) = pfn_to_mfn(pte_pfn(*pte));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static int u2mfn_get_mfn(pte_t *pte, pgtable_t token, unsigned long addr, void *data) {
|
||||||
|
*((unsigned long *) data) = pfn_to_mfn(pte_pfn(*pte));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// User virtual address to mfn translator
|
||||||
|
/**
|
||||||
|
\param cmd ignored
|
||||||
|
\param data the user-specified address
|
||||||
|
\return mfn corresponding to "data" argument, or -1 on error
|
||||||
|
*/
|
||||||
|
static long u2mfn_ioctl(struct file *f, unsigned int cmd,
|
||||||
|
unsigned long data)
|
||||||
|
{
|
||||||
|
long ret;
|
||||||
|
unsigned long mfn;
|
||||||
|
|
||||||
|
if (_IOC_TYPE(cmd) != U2MFN_MAGIC) {
|
||||||
|
printk("Qubes u2mfn: wrong IOCTL magic");
|
||||||
|
return -ENOTTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
case U2MFN_GET_MFN_FOR_PAGE:
|
||||||
|
ret = apply_to_page_range(current->mm, data, PAGE_SIZE, u2mfn_get_mfn, &mfn);
|
||||||
|
|
||||||
|
if (ret < 0 || mfn == INVALID_P2M_ENTRY) {
|
||||||
|
printk("U2MFN_GET_MFN_FOR_PAGE: failed to get mfn, "
|
||||||
|
"addr=0x%lx ret=0x%lx\n", data, ret);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = mfn;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case U2MFN_GET_LAST_MFN:
|
||||||
|
if (f->private_data)
|
||||||
|
ret = VIRT_TO_MFN(f->private_data);
|
||||||
|
else
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printk("Qubes u2mfn: wrong ioctl passed!\n");
|
||||||
|
return -ENOTTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int u2mfn_mmap(struct file *f, struct vm_area_struct *vma)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
char *kbuf;
|
||||||
|
long length = vma->vm_end - vma->vm_start;
|
||||||
|
printk("u2mfn_mmap: entering, private=%p\n", f->private_data);
|
||||||
|
if (f->private_data)
|
||||||
|
return -EBUSY;
|
||||||
|
if (length != PAGE_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
kbuf = (char *) __get_free_page(GFP_KERNEL);
|
||||||
|
if (!kbuf)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
f->private_data = kbuf;
|
||||||
|
|
||||||
|
ret = remap_pfn_range(vma, vma->vm_start,
|
||||||
|
virt_to_phys(kbuf) >> PAGE_SHIFT,
|
||||||
|
length, vma->vm_page_prot);
|
||||||
|
|
||||||
|
printk("u2mfn_mmap: calling remap return %d\n", ret);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int u2mfn_release(struct inode *i, struct file *f)
|
||||||
|
{
|
||||||
|
printk("u2mfn_release, priv=%p\n", f->private_data);
|
||||||
|
if (f->private_data)
|
||||||
|
__free_page(f->private_data);
|
||||||
|
f->private_data = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct file_operations u2mfn_fops = {
|
||||||
|
.unlocked_ioctl = u2mfn_ioctl,
|
||||||
|
.mmap = u2mfn_mmap,
|
||||||
|
.release = u2mfn_release
|
||||||
|
};
|
||||||
|
|
||||||
|
/// u2mfn module registration
|
||||||
|
/**
|
||||||
|
tries to register "/proc/u2mfn" pseudofile
|
||||||
|
*/
|
||||||
|
static int u2mfn_init(void)
|
||||||
|
{
|
||||||
|
struct proc_dir_entry *u2mfn_node =
|
||||||
|
proc_create_data("u2mfn", 0666, NULL,
|
||||||
|
&u2mfn_fops, 0);
|
||||||
|
if (!u2mfn_node)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void u2mfn_exit(void)
|
||||||
|
{
|
||||||
|
remove_proc_entry("u2mfn", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
module_init(u2mfn_init);
|
||||||
|
module_exit(u2mfn_exit);
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
MODULE_VERSION("5.0.0");
|
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "libqrexec-utils.h"
|
||||||
|
|
||||||
|
#define BUFFER_LIMIT 50000000
|
||||||
|
static int total_mem;
|
||||||
|
static char *limited_malloc(int len)
|
||||||
|
{
|
||||||
|
char *ret;
|
||||||
|
total_mem += len;
|
||||||
|
if (total_mem > BUFFER_LIMIT) {
|
||||||
|
fprintf(stderr, "attempt to allocate >BUFFER_LIMIT\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
ret = malloc(len);
|
||||||
|
if (!ret) {
|
||||||
|
perror("malloc");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void limited_free(char *ptr, int len)
|
||||||
|
{
|
||||||
|
free(ptr);
|
||||||
|
total_mem -= len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void buffer_init(struct buffer *b)
|
||||||
|
{
|
||||||
|
b->buflen = 0;
|
||||||
|
b->data = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void buffer_free(struct buffer *b)
|
||||||
|
{
|
||||||
|
if (b->buflen)
|
||||||
|
limited_free(b->data, b->buflen);
|
||||||
|
buffer_init(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
The following two functions can be made much more efficient.
|
||||||
|
Yet the profiling output show they are not significant CPU hogs, so
|
||||||
|
we keep them so simple to make them obviously correct.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void buffer_append(struct buffer *b, const char *data, int len)
|
||||||
|
{
|
||||||
|
int newsize;
|
||||||
|
char *qdata;
|
||||||
|
if (len < 0 || len > BUFFER_LIMIT) {
|
||||||
|
fprintf(stderr, "buffer_append %d\n", len);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (len == 0)
|
||||||
|
return;
|
||||||
|
newsize = len + b->buflen;
|
||||||
|
qdata = limited_malloc(len + b->buflen);
|
||||||
|
memcpy(qdata, b->data, b->buflen);
|
||||||
|
memcpy(qdata + b->buflen, data, len);
|
||||||
|
buffer_free(b);
|
||||||
|
b->buflen = newsize;
|
||||||
|
b->data = qdata;
|
||||||
|
}
|
||||||
|
|
||||||
|
void buffer_remove(struct buffer *b, int len)
|
||||||
|
{
|
||||||
|
int newsize;
|
||||||
|
char *qdata = NULL;
|
||||||
|
if (len < 0 || len > b->buflen) {
|
||||||
|
fprintf(stderr, "buffer_remove %d/%d\n", len, b->buflen);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
newsize = b->buflen - len;
|
||||||
|
if (newsize > 0) {
|
||||||
|
qdata = limited_malloc(newsize);
|
||||||
|
memcpy(qdata, b->data + len, newsize);
|
||||||
|
}
|
||||||
|
buffer_free(b);
|
||||||
|
b->buflen = newsize;
|
||||||
|
b->data = qdata;
|
||||||
|
}
|
||||||
|
|
||||||
|
int buffer_len(struct buffer *b)
|
||||||
|
{
|
||||||
|
return b->buflen;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *buffer_data(struct buffer *b)
|
||||||
|
{
|
||||||
|
return b->data;
|
||||||
|
}
|
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "qrexec.h"
|
||||||
|
#include "libqrexec-utils.h"
|
||||||
|
|
||||||
|
static do_exec_t *exec_func = NULL;
|
||||||
|
void register_exec_func(do_exec_t *func) {
|
||||||
|
exec_func = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
void exec_qubes_rpc_if_requested(char *prog, char *const envp[]) {
|
||||||
|
/* avoid calling qubes-rpc-multiplexer through shell */
|
||||||
|
if (strncmp(prog, RPC_REQUEST_COMMAND, RPC_REQUEST_COMMAND_LEN) == 0) {
|
||||||
|
char *tok;
|
||||||
|
char *argv[16]; // right now 6 are used, but allow future extensions
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
tok=strtok(prog, " ");
|
||||||
|
do {
|
||||||
|
if (i >= sizeof(argv)/sizeof(argv[0])-1) {
|
||||||
|
fprintf(stderr, "To many arguments to %s\n", RPC_REQUEST_COMMAND);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
argv[i++] = tok;
|
||||||
|
} while ((tok=strtok(NULL, " ")));
|
||||||
|
argv[i] = NULL;
|
||||||
|
argv[0] = QUBES_RPC_MULTIPLEXER_PATH;
|
||||||
|
execve(QUBES_RPC_MULTIPLEXER_PATH, argv, envp);
|
||||||
|
perror("exec qubes-rpc-multiplexer");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fix_fds(int fdin, int fdout, int fderr)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 256; i++)
|
||||||
|
if (i != fdin && i != fdout && i != fderr)
|
||||||
|
close(i);
|
||||||
|
dup2(fdin, 0);
|
||||||
|
dup2(fdout, 1);
|
||||||
|
dup2(fderr, 2);
|
||||||
|
close(fdin);
|
||||||
|
close(fdout);
|
||||||
|
if (fderr != 2)
|
||||||
|
close(fderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fork_exec(const char *cmdline, int *pid, int *stdin_fd, int *stdout_fd,
|
||||||
|
int *stderr_fd)
|
||||||
|
{
|
||||||
|
int inpipe[2], outpipe[2], errpipe[2];
|
||||||
|
|
||||||
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, inpipe) ||
|
||||||
|
socketpair(AF_UNIX, SOCK_STREAM, 0, outpipe) ||
|
||||||
|
(stderr_fd && socketpair(AF_UNIX, SOCK_STREAM, 0, errpipe))) {
|
||||||
|
perror("socketpair");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
switch (*pid = fork()) {
|
||||||
|
case -1:
|
||||||
|
perror("fork");
|
||||||
|
exit(-1);
|
||||||
|
case 0:
|
||||||
|
if (stderr_fd) {
|
||||||
|
fix_fds(inpipe[0], outpipe[1], errpipe[1]);
|
||||||
|
} else
|
||||||
|
fix_fds(inpipe[0], outpipe[1], 2);
|
||||||
|
|
||||||
|
if (exec_func != NULL)
|
||||||
|
exec_func((char*)cmdline);
|
||||||
|
exit(-1);
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
close(inpipe[0]);
|
||||||
|
close(outpipe[1]);
|
||||||
|
*stdin_fd = inpipe[1];
|
||||||
|
*stdout_fd = outpipe[0];
|
||||||
|
if (stderr_fd) {
|
||||||
|
close(errpipe[1]);
|
||||||
|
*stderr_fd = errpipe[0];
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
* Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <libvchan.h>
|
||||||
|
|
||||||
|
struct buffer {
|
||||||
|
char *data;
|
||||||
|
int buflen;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* return codes for buffered writes */
|
||||||
|
#define WRITE_STDIN_OK 0 /* all written */
|
||||||
|
#define WRITE_STDIN_BUFFERED 1 /* something still in the buffer */
|
||||||
|
#define WRITE_STDIN_ERROR 2 /* write error, errno set */
|
||||||
|
|
||||||
|
typedef void (do_exec_t)(char *);
|
||||||
|
void register_exec_func(do_exec_t *func);
|
||||||
|
/*
|
||||||
|
* exec() qubes-rpc-multiplexer if *prog* starts with magic "QUBESRPC" keyword,
|
||||||
|
* do not return in that case; pass *envp* to execve() as en environment
|
||||||
|
* otherwise, return false without any action
|
||||||
|
*/
|
||||||
|
void exec_qubes_rpc_if_requested(char *prog, char *const envp[]);
|
||||||
|
|
||||||
|
void buffer_init(struct buffer *b);
|
||||||
|
void buffer_free(struct buffer *b);
|
||||||
|
void buffer_append(struct buffer *b, const char *data, int len);
|
||||||
|
void buffer_remove(struct buffer *b, int len);
|
||||||
|
int buffer_len(struct buffer *b);
|
||||||
|
void *buffer_data(struct buffer *b);
|
||||||
|
|
||||||
|
int flush_client_data(int fd, struct buffer *buffer);
|
||||||
|
int write_stdin(int fd, const char *data, int len, struct buffer *buffer);
|
||||||
|
int fork_and_flush_stdin(int fd, struct buffer *buffer);
|
||||||
|
|
||||||
|
void do_fork_exec(const char *cmdline, int *pid, int *stdin_fd, int *stdout_fd,
|
||||||
|
int *stderr_fd);
|
||||||
|
void wait_for_vchan_or_argfd(libvchan_t *vchan, int max, fd_set * rdset, fd_set * wrset);
|
||||||
|
int read_vchan_all(libvchan_t *vchan, void *data, size_t size);
|
||||||
|
int write_vchan_all(libvchan_t *vchan, const void *data, size_t size);
|
||||||
|
int read_all(int fd, void *buf, int size);
|
||||||
|
int write_all(int fd, const void *buf, int size);
|
||||||
|
void fix_fds(int fdin, int fdout, int fderr);
|
||||||
|
void set_nonblock(int fd);
|
||||||
|
void set_block(int fd);
|
||||||
|
|
||||||
|
int get_server_socket(const char *);
|
||||||
|
int do_accept(int s);
|
||||||
|
|
||||||
|
void set_nonblock(int fd);
|
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* See also http://wiki.qubes-os.org/trac/wiki/Qrexec */
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define QREXEC_PROTOCOL_VERSION 2
|
||||||
|
#define MAX_FDS 256
|
||||||
|
#define MAX_DATA_CHUNK 4096
|
||||||
|
|
||||||
|
#define RPC_REQUEST_COMMAND "QUBESRPC"
|
||||||
|
#define RPC_REQUEST_COMMAND_LEN (sizeof(RPC_REQUEST_COMMAND)-1)
|
||||||
|
#define NOGUI_CMD_PREFIX "nogui:"
|
||||||
|
#define NOGUI_CMD_PREFIX_LEN (sizeof(NOGUI_CMD_PREFIX)-1)
|
||||||
|
#define VCHAN_BASE_PORT 512
|
||||||
|
|
||||||
|
/* Messages sent over control vchan between daemon(dom0) and agent(vm).
|
||||||
|
* The same are used between client(dom0) and daemon(dom0).
|
||||||
|
*/
|
||||||
|
enum {
|
||||||
|
/* daemon->agent messages */
|
||||||
|
|
||||||
|
/* start process in VM and pass its stdin/out/err to dom0
|
||||||
|
* struct exec_params passed as data */
|
||||||
|
MSG_EXEC_CMDLINE = 0x200,
|
||||||
|
|
||||||
|
/* start process in VM discarding its stdin/out/err (connect to /dev/null)
|
||||||
|
* struct exec_params passed as data */
|
||||||
|
MSG_JUST_EXEC,
|
||||||
|
|
||||||
|
/* connect to existing process in VM to receive its stdin/out/err
|
||||||
|
* struct service_params passed as cmdline field in exec_params */
|
||||||
|
MSG_SERVICE_CONNECT,
|
||||||
|
|
||||||
|
/* refuse to start a service (denied by policy, invalid parameters etc)
|
||||||
|
* struct service_params passed as data to identify which service call was
|
||||||
|
* refused */
|
||||||
|
MSG_SERVICE_REFUSED,
|
||||||
|
|
||||||
|
/* agent->daemon messages */
|
||||||
|
/* call Qubes RPC service
|
||||||
|
* struct trigger_service_params passed as data */
|
||||||
|
MSG_TRIGGER_SERVICE = 0x210,
|
||||||
|
|
||||||
|
|
||||||
|
/* connection was terminated, struct exec_params passed as data (with empty
|
||||||
|
* cmdline field) informs about released vchan port */
|
||||||
|
MSG_CONNECTION_TERMINATED,
|
||||||
|
|
||||||
|
/* common messages */
|
||||||
|
/* initialize connection, struct peer_info passed as data
|
||||||
|
* should be sent as the first message (server first, then client) */
|
||||||
|
MSG_HELLO = 0x300,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* uniform for all peers, data type depends on message type */
|
||||||
|
struct msg_header {
|
||||||
|
uint32_t type; /* message type */
|
||||||
|
uint32_t len; /* data length */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* variable size */
|
||||||
|
struct exec_params {
|
||||||
|
uint32_t connect_domain; /* target domain name */
|
||||||
|
uint32_t connect_port; /* target vchan port for i/o exchange */
|
||||||
|
char cmdline[0]; /* command line to execute, null terminated, size = msg_header.len - sizeof(struct exec_params) */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct service_params {
|
||||||
|
char ident[32]; /* null terminated ASCII string */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct trigger_service_params {
|
||||||
|
char service_name[64]; /* null terminated ASCII string */
|
||||||
|
char target_domain[32]; /* null terminated ASCII string */
|
||||||
|
struct service_params request_id; /* service request id */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct peer_info {
|
||||||
|
uint32_t version; /* qrexec protocol version */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* data vchan client<->agent, separate for each VM process */
|
||||||
|
enum {
|
||||||
|
/* stdin dom0->VM */
|
||||||
|
MSG_DATA_STDIN = 0x190,
|
||||||
|
/* stdout VM->dom0 */
|
||||||
|
MSG_DATA_STDOUT,
|
||||||
|
/* stderr VM->dom0 */
|
||||||
|
MSG_DATA_STDERR,
|
||||||
|
/* VM process exit code VM->dom0 (uint32_t) */
|
||||||
|
MSG_DATA_EXIT_CODE,
|
||||||
|
};
|
||||||
|
|
||||||
|
// linux-specific stuff below
|
||||||
|
|
||||||
|
#define QREXEC_AGENT_TRIGGER_PATH "/var/run/qubes/qrexec-agent"
|
||||||
|
#define QREXEC_AGENT_FDPASS_PATH "/var/run/qubes/qrexec-agent-fdpass"
|
||||||
|
#define MEMINFO_WRITER_PIDFILE "/var/run/meminfo-writer.pid"
|
||||||
|
#define QUBES_RPC_MULTIPLEXER_PATH "/usr/lib/qubes/qubes-rpc-multiplexer"
|
||||||
|
#define QREXEC_DAEMON_SOCKET_DIR "/var/run/qubes"
|
||||||
|
|
@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <libvchan.h>
|
||||||
|
|
||||||
|
int wait_for_vchan_or_argfd_once(libvchan_t *ctrl, int max, fd_set * rdset, fd_set * wrset)
|
||||||
|
{
|
||||||
|
int vfd, ret;
|
||||||
|
struct timespec tv = { 1, 100000000 };
|
||||||
|
sigset_t empty_set;
|
||||||
|
|
||||||
|
sigemptyset(&empty_set);
|
||||||
|
|
||||||
|
vfd = libvchan_fd_for_select(ctrl);
|
||||||
|
FD_SET(vfd, rdset);
|
||||||
|
if (vfd > max)
|
||||||
|
max = vfd;
|
||||||
|
max++;
|
||||||
|
ret = pselect(max, rdset, wrset, NULL, &tv, &empty_set);
|
||||||
|
if (ret < 0) {
|
||||||
|
if (errno != EINTR) {
|
||||||
|
perror("select");
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
FD_ZERO(rdset);
|
||||||
|
FD_ZERO(wrset);
|
||||||
|
fprintf(stderr, "eintr\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (!libvchan_is_open(ctrl)) {
|
||||||
|
fprintf(stderr, "libvchan_is_eof\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
if (FD_ISSET(vfd, rdset))
|
||||||
|
// the following will never block; we need to do this to
|
||||||
|
// clear libvchan_fd pending state
|
||||||
|
libvchan_wait(ctrl);
|
||||||
|
if (libvchan_data_ready(ctrl))
|
||||||
|
return 1;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wait_for_vchan_or_argfd(libvchan_t *ctrl, int max, fd_set * rdset, fd_set * wrset)
|
||||||
|
{
|
||||||
|
fd_set r = *rdset, w = *wrset;
|
||||||
|
do {
|
||||||
|
*rdset = r;
|
||||||
|
*wrset = w;
|
||||||
|
}
|
||||||
|
while (wait_for_vchan_or_argfd_once(ctrl, max, rdset, wrset) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int write_vchan_all(libvchan_t *vchan, const void *data, size_t size) {
|
||||||
|
size_t pos;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
pos = 0;
|
||||||
|
while (pos < size) {
|
||||||
|
ret = libvchan_write(vchan, data+pos, size-pos);
|
||||||
|
if (ret < 0)
|
||||||
|
return 0;
|
||||||
|
pos += ret;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_vchan_all(libvchan_t *vchan, void *data, size_t size) {
|
||||||
|
size_t pos;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
pos = 0;
|
||||||
|
while (pos < size) {
|
||||||
|
ret = libvchan_read(vchan, data+pos, size-pos);
|
||||||
|
if (ret < 0)
|
||||||
|
return 0;
|
||||||
|
pos += ret;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
//#include "qrexec.h"
|
||||||
|
|
||||||
|
int get_server_socket(const char *socket_address)
|
||||||
|
{
|
||||||
|
struct sockaddr_un sockname;
|
||||||
|
int s;
|
||||||
|
|
||||||
|
unlink(socket_address);
|
||||||
|
|
||||||
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (s < 0) {
|
||||||
|
printf("socket() failed\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
memset(&sockname, 0, sizeof(sockname));
|
||||||
|
sockname.sun_family = AF_UNIX;
|
||||||
|
strncpy(sockname.sun_path, socket_address, sizeof sockname.sun_path);
|
||||||
|
sockname.sun_path[sizeof sockname.sun_path - 1] = 0;
|
||||||
|
|
||||||
|
if (bind(s, (struct sockaddr *) &sockname, sizeof(sockname)) == -1) {
|
||||||
|
printf("bind() failed\n");
|
||||||
|
close(s);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
// chmod(sockname.sun_path, 0666);
|
||||||
|
if (listen(s, 5) == -1) {
|
||||||
|
perror("listen() failed\n");
|
||||||
|
close(s);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
int do_accept(int s)
|
||||||
|
{
|
||||||
|
struct sockaddr_un peer;
|
||||||
|
unsigned int addrlen;
|
||||||
|
int fd;
|
||||||
|
addrlen = sizeof(peer);
|
||||||
|
fd = accept(s, (struct sockaddr *) &peer, &addrlen);
|
||||||
|
if (fd == -1) {
|
||||||
|
perror("unix accept");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return fd;
|
||||||
|
}
|
@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
* The Qubes OS Project, http://www.qubes-os.org
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <libvchan.h>
|
||||||
|
#include "qrexec.h"
|
||||||
|
#include "libqrexec-utils.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
There is buffered data in "buffer" for client and select()
|
||||||
|
reports that "fd" is writable. Write as much as possible to fd.
|
||||||
|
*/
|
||||||
|
int flush_client_data(int fd, struct buffer *buffer)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
int len;
|
||||||
|
for (;;) {
|
||||||
|
len = buffer_len(buffer);
|
||||||
|
if (!len) {
|
||||||
|
return WRITE_STDIN_OK;
|
||||||
|
}
|
||||||
|
if (len > MAX_DATA_CHUNK)
|
||||||
|
len = MAX_DATA_CHUNK;
|
||||||
|
ret = write(fd, buffer_data(buffer), len);
|
||||||
|
if (ret == -1) {
|
||||||
|
if (errno != EAGAIN) {
|
||||||
|
return WRITE_STDIN_ERROR;
|
||||||
|
} else
|
||||||
|
return WRITE_STDIN_BUFFERED;
|
||||||
|
}
|
||||||
|
// we previously called buffer_remove(buffer, len)
|
||||||
|
// it will be wrong if we change MAX_DATA_CHUNK to something large
|
||||||
|
// as pipes writes are atomic only to PIPE_MAX limit
|
||||||
|
buffer_remove(buffer, ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Write "len" bytes from "data" to "fd". If not all written, buffer the rest
|
||||||
|
* to "buffer".
|
||||||
|
*/
|
||||||
|
int write_stdin(int fd, const char *data, int len, struct buffer *buffer)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
int written = 0;
|
||||||
|
|
||||||
|
if (buffer_len(buffer)) {
|
||||||
|
buffer_append(buffer, data, len);
|
||||||
|
return WRITE_STDIN_BUFFERED;
|
||||||
|
}
|
||||||
|
while (written < len) {
|
||||||
|
ret = write(fd, data + written, len - written);
|
||||||
|
if (ret == 0) {
|
||||||
|
perror("write_stdin: write returns 0 ???");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (ret == -1) {
|
||||||
|
if (errno != EAGAIN)
|
||||||
|
return WRITE_STDIN_ERROR;
|
||||||
|
|
||||||
|
buffer_append(buffer, data + written,
|
||||||
|
len - written);
|
||||||
|
|
||||||
|
return WRITE_STDIN_BUFFERED;
|
||||||
|
}
|
||||||
|
written += ret;
|
||||||
|
}
|
||||||
|
return WRITE_STDIN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Data feed process has exited, so we need to clear all control structures for
|
||||||
|
* the client. However, if we have buffered data for the client (which is rare btw),
|
||||||
|
* fire&forget a separate process to flush them.
|
||||||
|
*/
|
||||||
|
int fork_and_flush_stdin(int fd, struct buffer *buffer)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
if (!buffer_len(buffer))
|
||||||
|
return 0;
|
||||||
|
switch (fork()) {
|
||||||
|
case -1:
|
||||||
|
perror("fork");
|
||||||
|
exit(1);
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (i = 0; i < MAX_FDS; i++)
|
||||||
|
if (i != fd && i != 2)
|
||||||
|
close(i);
|
||||||
|
set_block(fd);
|
||||||
|
write_all(fd, buffer_data(buffer), buffer_len(buffer));
|
||||||
|
_exit(0);
|
||||||
|
}
|
Loading…
Reference in new issue