From f515769b9e5ddd0aceb6fdac3c1290e3beef7280 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Tue, 3 Mar 2020 17:54:51 +0100 Subject: [PATCH 01/18] Add an include directive to `vunet_ioctl.c` Include `linux/sockios.h` to make SIOCGSTAMP visible. --- vunet/vunet_ioctl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/vunet/vunet_ioctl.c b/vunet/vunet_ioctl.c index 64fc12b..30f8cff 100644 --- a/vunet/vunet_ioctl.c +++ b/vunet/vunet_ioctl.c @@ -21,6 +21,7 @@ #include #include #include +#include long vunet_ioctl_parms(unsigned long request) { switch (request) { From 3e1d965368966b21a072d1bb0be5cccd08e83e9e Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Tue, 3 Mar 2020 17:57:19 +0100 Subject: [PATCH 02/18] Add `fixes.txt` file --- fixes.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 fixes.txt diff --git a/fixes.txt b/fixes.txt new file mode 100644 index 0000000..9fa2518 --- /dev/null +++ b/fixes.txt @@ -0,0 +1,4 @@ +in vuos: + in vunet/vunet_ioctl.c:34:10: error: 'SIOCGSTAMP' undeclared + starting from linux kernel v5.2-rc1 'SIOCGSTAMP' has been renamed to 'SIOCGSTAMP_OLD' + FIX: to access it again 'linux/sockios.h' needs to be included From 3331de6aa4fcf41e25a4fae70cafd9df753f52d4 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Wed, 22 Jul 2020 16:34:33 +0200 Subject: [PATCH 03/18] Add first stub for file locking APIs virtualization Add a demo folder Add some utility scripts --- .gitignore | 2 + demos/fcntlflock/Makefile | 15 +++ demos/fcntlflock/dllock.c | 120 +++++++++++++++++ demos/fcntlflock/fcntl_lock.c | 229 +++++++++++++++++++++++++++++++++ demos/fcntlflock/fcntl_other.c | 36 ++++++ demos/fcntlflock/flock_lock.c | 175 +++++++++++++++++++++++++ lxt-run.sh | 2 + projbuild.sh | 20 +++ umvu/src/vu_wrap_file.c | 67 ++++++++++ vu_syscalls.conf | 1 + 10 files changed, 667 insertions(+) create mode 100644 .gitignore create mode 100644 demos/fcntlflock/Makefile create mode 100644 demos/fcntlflock/dllock.c create mode 100644 demos/fcntlflock/fcntl_lock.c create mode 100644 demos/fcntlflock/fcntl_other.c create mode 100644 demos/fcntlflock/flock_lock.c create mode 100755 lxt-run.sh create mode 100755 projbuild.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93d7b28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +demos/build/ diff --git a/demos/fcntlflock/Makefile b/demos/fcntlflock/Makefile new file mode 100644 index 0000000..d0528b7 --- /dev/null +++ b/demos/fcntlflock/Makefile @@ -0,0 +1,15 @@ +BUILDDIR=build + +all: clean builddir fcntl_lock.c flock_lock.c fcntl_other.c dllock + gcc -g -o $(BUILDDIR)/fcntl_lock fcntl_lock.c + gcc -g -o $(BUILDDIR)/fcntl_other fcntl_other.c + gcc -g -o $(BUILDDIR)/flock_lock flock_lock.c + +dllock: dllock.c + gcc -g -shared -fPIC -o build/dllock.so dllock.c -ldl + +builddir: + mkdir $(BUILDDIR) + +clean: + rm -rf $(BUILDDIR) diff --git a/demos/fcntlflock/dllock.c b/demos/fcntlflock/dllock.c new file mode 100644 index 0000000..5eb99a3 --- /dev/null +++ b/demos/fcntlflock/dllock.c @@ -0,0 +1,120 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +// define the types of the original flock functions to be used with dlsym +typedef int (*real_flock)(int, int); +typedef int (*real_fcntl)(int, int, ...); + +// override the flock SC +int flock(int fd, int operation) { + // check whether the call has to be BLOCKING + int noblock = operation & LOCK_NB; + short fcntl_ltype; + + // tell fcntl whether to wait or not based on the LOCK_NB flag in operation + int fcntl_cmd = noblock ? F_OFD_SETLK : F_OFD_SETLKW; + + switch (operation & ~LOCK_NB) { + case LOCK_SH: // apply SHARED lock + fcntl_ltype = F_RDLCK; + break; + + case LOCK_EX: // apply EXCLUSIVE lock + fcntl_ltype = F_WRLCK; + break; + + case LOCK_UN: // UNlock + fcntl_ltype = F_UNLCK; + break; + + default: // operation is not valid + errno = EINVAL; + return -1; + } + + struct flock lockinfo = { fcntl_ltype, SEEK_SET, 0, 0, 0 }; + int res = fcntl(fd, fcntl_cmd, &lockinfo); + int errno_backup = errno; + int new_errno; + + if (res < 0) { + // the error generated when a call results in entering a waiting state + // is different between flock and fcntl + if (errno == EACCES || errno == EAGAIN) { + errno = EWOULDBLOCK; + } + + /* + * TODO: flock can apply locks regardless of the open mode of the target file, + * whereas fcntl locks must be applied to files which open mode match the + * desired lock type (READ for READ locks, etc.). + * In such cases fcntl raises an EBADF error, while flock successfully applies + * the lock so the behaviour of this method should change accordingly (assuming + * it is possible). + */ + + return -1; + } + + //return ((real_flock)dlsym(RTLD_NEXT, "flock"))(fd, operation); + return 0; +} + +int fcntl(int fd, int cmd, ...) { + va_list ap; + struct flock *lockinfo; + int res; + int errno_backup; + struct f_owner_ex *ownp_arg; + int int_arg; + uint64_t *uint_argp; + + // get the actual fcntl function address + real_fcntl r_fcntl = dlsym(RTLD_NEXT, "fcntl"); + va_start(ap, cmd); + + switch (cmd) { + case F_SETLK: + case F_SETLKW: + case F_GETLK: + case F_OFD_SETLK: + case F_OFD_SETLKW: + case F_OFD_GETLK: + // retrieve the flock struct pointer from the variadic parameter + lockinfo = va_arg(ap, struct flock *); + res = r_fcntl(fd, cmd, lockinfo); + break; + + case F_GETOWN_EX: + case F_SETOWN_EX: + ownp_arg = va_arg(ap, struct f_owner_ex *); + res = r_fcntl(fd, cmd, ownp_arg); + break; + + case F_GET_RW_HINT: + case F_SET_RW_HINT: + case F_GET_FILE_RW_HINT: + case F_SET_FILE_RW_HINT: + uint_argp = va_arg(ap, uint64_t *); + res = r_fcntl(fd, cmd, uint_argp); + break; + + default: // all fcntl calls that require an int as third argument + int_arg = va_arg(ap, int); + res = r_fcntl(fd, cmd, int_arg); + break; + } + + errno_backup = errno; + va_end(ap); + errno = errno_backup; + return res; +} + diff --git a/demos/fcntlflock/fcntl_lock.c b/demos/fcntlflock/fcntl_lock.c new file mode 100644 index 0000000..0a52072 --- /dev/null +++ b/demos/fcntlflock/fcntl_lock.c @@ -0,0 +1,229 @@ +#include +#include +#include +#include +#include +#include + +#define LI_MAX_SIZE 10 + +struct lock_info { + int fd; + int fd_o_mode; + char* path; + struct flock* lock_params; +}; + +struct lock_info opened_locks[LI_MAX_SIZE]; + +int get_first_free_lockinfo_index() { + int i; + + for (i = 0; i < LI_MAX_SIZE + && opened_locks[i].fd != 0; i++) ; + + return i < LI_MAX_SIZE ? i : -1; +} + +int push_to_locks_array(struct lock_info linfo) { + int i = get_first_free_lockinfo_index(); + if (i == -1) { + printf("No space left\n"); + return -1; + } + + opened_locks[i] = linfo; + return i; +} + +int open_file(char* path, int mode) { + int res = open(path, O_CREAT | mode, S_IRUSR | S_IWUSR); + + if (res < 0) { + printf("Error while opening %s\n", path); + return -1; + } + + struct lock_info linfo = (struct lock_info) { .fd=res, .fd_o_mode=mode, .path=path, .lock_params=NULL }; + int index = push_to_locks_array(linfo); + if (index != -1) { + printf("Successfully opened file %s, fd: %d\n", path, res); + return index; + } + + return -2; +} + +int duplicate(int atIndex) { + int newfd = dup(opened_locks[atIndex].fd); + + if (newfd < 0) { + printf("\ndup failed\n\n"); + return -1; + } + + struct lock_info linfo = opened_locks[atIndex]; + linfo.fd = newfd; + + int index = push_to_locks_array(linfo); + if (index != -1) { + printf("Successfully dup'ed fd %d, the new one is %d\n", opened_locks[atIndex].fd, newfd); + return index; + } + + return -2; +} + +void init() { + for (int i = 0; i < LI_MAX_SIZE; i++) { + opened_locks[i] = (struct lock_info) { .fd=0, .fd_o_mode=0, .path=NULL, .lock_params=NULL }; + } +} + +void deinit() { + for (int i = 0; i < LI_MAX_SIZE + && opened_locks[i].path != NULL + && opened_locks[i].lock_params != NULL; i++) { + unlink(opened_locks[i].path); + free(opened_locks[i].lock_params); + opened_locks[i].lock_params = NULL; + } +} + +const char* getTypeStrFromInt(int type) { + switch (type) { + case F_RDLCK: + return "F_RDLCK"; + case F_WRLCK: + return "F_WRLCK"; + case F_UNLCK: + return "F_UNLCK"; + default: + return "unknown"; + } +} + +void apply_lock(int index) { + // malloc'ing this because it will be stored after the method returns + struct flock* lockinfo = malloc(sizeof(struct flock)); + int cmd; + + switch (index) { + case 0: + *lockinfo = (struct flock) { F_RDLCK, SEEK_SET, 0, 0 }; + cmd = F_SETLK; + break; + case 1: + *lockinfo = (struct flock) { F_WRLCK, SEEK_SET, 0, 0 }; + cmd = F_SETLK; + break; + case 2: + *lockinfo = (struct flock) { F_RDLCK, SEEK_SET, 5, 7 }; + cmd = F_SETLK; + break; + case 3: + opened_locks[index] = opened_locks[2]; + + // avoid double-freeing this pointer if the fcntl call fails + opened_locks[index].lock_params = NULL; + + *lockinfo = (struct flock) { F_WRLCK, SEEK_SET, 2, 9 }; + cmd = F_SETLK; + break; + case 4: + *lockinfo = (struct flock) { F_UNLCK, SEEK_SET, 0, 0 }; + cmd = F_SETLK; + break; + default: + break; + } + + // apply the lock + int res = fcntl(opened_locks[index].fd, cmd, lockinfo); + if (res < 0) { + printf("\n\t-------------------------------\n"); + printf("\t Cannot apply lock on index %d\n", index); + printf("\t-------------------------------\n\n"); + + // pointer not stored anywhere so it must be free'd here + free(lockinfo); + return; + } + + // save the lock in the global array + struct lock_info* flinfo = &(opened_locks[index]); + flinfo->lock_params = lockinfo; + + printf("\n\t-------------------------------\n"); + printf("\t Lock applied to file %s, fd: %d\n", flinfo->path, flinfo->fd); + printf("\t l_type: %s\n", getTypeStrFromInt(lockinfo->l_type)); + printf("\t l_whence: %s\n", lockinfo->l_whence == SEEK_SET ? "SEEK_SET" : "SEEK_END or SEEK_CUR"); + printf("\t l_start: %d\n", lockinfo->l_start); + printf("\t l_len: %d\n", lockinfo->l_len); + printf("\t-------------------------------\n\n"); +} + +void populate_file(int fd) { + char* text = "this is the file content\n"; + + size_t writtenb = write(fd, text, 100); + printf("%d bytes written to fd %d\n", writtenb, fd); +} + +void flush_stdin() { + char c; + + // consume all characters in stdin + // not doing so will cause stdin unread characters to be read + // from the next fgets call, that will immediately return + while ((c = getchar()) != '\n' && c != EOF) ; +} + +int main(int argc, char **argv) { + init(); + + open_file("read.lock", O_RDONLY); + open_file("write.lock", O_WRONLY); + open_file("partial_read.lock", O_RDWR); + + // write something into the file that will be partially locked + populate_file(opened_locks[2].fd); + + fgets(NULL, 0, stdin); + flush_stdin(); // flush the stream to delete all unmatched characters + apply_lock(0); + + fgets(NULL, 0, stdin); + flush_stdin(); + apply_lock(1); + + fgets(NULL, 0, stdin); + flush_stdin(); + apply_lock(2); + + fgets(NULL, 0, stdin); + flush_stdin(); + apply_lock(3); + + fgets(NULL, 0, stdin); + flush_stdin(); + // get another fd for the same file + int index = open_file("partial_read.lock", O_RDWR); + // closig this fd will cause the release of all the opened locks + // even if it wasn't used to acquire any lock + close(opened_locks[index].fd); + printf("fd %d closed\n", opened_locks[index].fd); + + fgets(NULL, 0, stdin); + flush_stdin(); + // get a dup'ed fd for the element at the specified index + int dupindex = duplicate(0); + // closing this fd will cause the release of all the locks onto the + // file the original fd referred to + close(opened_locks[dupindex].fd); + printf("fd %d closed\n", opened_locks[dupindex].fd); + + fgets(NULL, 0, stdin); + flush_stdin(); + deinit(); +} diff --git a/demos/fcntlflock/fcntl_other.c b/demos/fcntlflock/fcntl_other.c new file mode 100644 index 0000000..5c0014a --- /dev/null +++ b/demos/fcntlflock/fcntl_other.c @@ -0,0 +1,36 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +int main() { + int fd = open("/proc/locks", O_RDONLY); + if (fd < 0) { + printf("Error on open(2)\n"); + exit(fd); + } + + int dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 7); + printf("F_DUPFD result: %d\n", dupfd); + + int fl = fcntl(dupfd, F_GETFL, 34); + printf("F_GETFL result: %d\n", fl); + + struct f_owner_ex setown = { F_OWNER_PID, getpid() }; + struct f_owner_ex getown; + + fcntl(dupfd, F_SETOWN_EX, &setown); + fcntl(dupfd, F_GETOWN_EX, &getown); + + printf("F_GETOWN_EX result: type %d (%d), pid %d (%d)\n", + getown.type, + setown.type, + getown.pid, + setown.pid); + + return 0; +} diff --git a/demos/fcntlflock/flock_lock.c b/demos/fcntlflock/flock_lock.c new file mode 100644 index 0000000..35bc9bc --- /dev/null +++ b/demos/fcntlflock/flock_lock.c @@ -0,0 +1,175 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define LI_MAX_SIZE 10 + +struct lock_info { + int fd; + int fd_o_mode; + char* path; + int operation; +}; + +struct lock_info opened_locks[LI_MAX_SIZE]; + +int get_first_free_lockinfo_index() { + int i; + + for (i = 0; i < LI_MAX_SIZE + && opened_locks[i].fd != 0; i++) ; + + return i < LI_MAX_SIZE ? i : -1; +} + +void open_file(char* path, int mode) { + int res = open(path, O_CREAT | mode, S_IRUSR | S_IWUSR); + + if (res < 0) { + printf("Error while opening %s\n", path); + return; + } + + int i = get_first_free_lockinfo_index(); + if (i == -1) { + printf("No space left\n"); + return; + } + + opened_locks[i] = (struct lock_info) { .fd=res, .fd_o_mode=mode, .path=path, .operation=-1 }; + + printf("Successfully opened file %s, fd: %d\n", path, res); +} + +void deinit() { + for (int i = 0; i < LI_MAX_SIZE + && opened_locks[i].path != NULL; i++) { + unlink(opened_locks[i].path); + } +} + +void init() { + for (int i = 0; i < LI_MAX_SIZE; i++) { + opened_locks[i] = (struct lock_info) { .fd=0, .fd_o_mode=0, .path=NULL, .operation=-1 }; + } +} + +char* getOpStrFromInt(int cmd) { + int noblock = cmd & LOCK_NB; + char *nbstr = noblock ? " | LOCK_NB" : ""; + char *opstr; + + switch (cmd & ~LOCK_NB) { + case LOCK_SH: + opstr = "LOCK_SH"; + break; + case LOCK_EX: + opstr = "LOCK_EX"; + break; + case LOCK_UN: + opstr = "LOCK_UN"; + break; + default: + return "unknown"; + } + + char *res = malloc(sizeof(char)); + sprintf(res, "%s%s", opstr, nbstr); + + return res; +} + +void apply_lock(int index) { + int cmd; + + switch (index) { + case 0: + cmd = LOCK_SH; + break; + case 1: + // exclusive lock, non-blocking + cmd = LOCK_EX | LOCK_NB; + break; + case 2: + cmd = LOCK_SH; + break; + case 3: + opened_locks[index] = opened_locks[2]; + cmd = LOCK_SH; + break; + case 4: + cmd = LOCK_EX | LOCK_NB; + default: + break; + } + + // apply the lock + int fd = opened_locks[index].fd; + int res = flock(fd, cmd); + if (res < 0) { + int errnocpy = errno; + printf("\n\t-------------------------------\n"); + printf("\t Cannot apply lock on index %d\n\t (fd %d, error: %d)\n", + index, fd, errnocpy); + printf("\t-------------------------------\n\n"); + return; + } + + // save the lock in the global array + struct lock_info* flinfo = &(opened_locks[index]); + flinfo->operation = cmd; + + char *optstr = getOpStrFromInt(cmd); + printf("\n\t-------------------------------\n"); + printf("\t Lock applied to file %s, fd: %d\n", flinfo->path, flinfo->fd); + printf("\t operation: %s\n", optstr); + printf("\t-------------------------------\n\n"); + + free(optstr); +} + +void flush_stdin() { + char c; + + while ((c = getchar()) != '\n' && c != EOF) ; +} + +int main(int argc, char **argv) { + init(); + + open_file("/etc/passwd", O_RDONLY); + open_file("/etc/shadow", O_WRONLY); + open_file("/etc/hosts", O_RDWR); + + fgets(NULL, 0, stdin); + flush_stdin(); // flush the stream to delete all unmatched characters + apply_lock(0); + + fgets(NULL, 0, stdin); + flush_stdin(); + apply_lock(1); + + fgets(NULL, 0, stdin); + flush_stdin(); + apply_lock(2); + + fgets(NULL, 0, stdin); + flush_stdin(); + apply_lock(3); + + fgets(NULL, 0, stdin); + flush_stdin(); + open_file("write.lock", O_RDONLY); + apply_lock(4); + + fgets(NULL, 0, stdin); + flush_stdin(); + + deinit(); +} + diff --git a/lxt-run.sh b/lxt-run.sh new file mode 100755 index 0000000..8dd0f04 --- /dev/null +++ b/lxt-run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +lxterminal --title="UMVU SHELL" --working-directory=$PWD/test_demos/fcntlflock --geometry=140x30 --command="umvu bash" diff --git a/projbuild.sh b/projbuild.sh new file mode 100755 index 0000000..f2c1d9c --- /dev/null +++ b/projbuild.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +ROOTDIR=$PWD +DEFAULT_BUILDDIR=build + +if [ -z ${BUILDDIR:+x} ]; + then + echo Using default build directory; + BUILDDIR=$ROOTDIR/$DEFAULT_BUILDDIR + else + BUILDDIR=$ROOTDIR/$1 +fi + +rm -rf $BUILDDIR +mkdir -p $BUILDDIR +cd $BUILDDIR + +cmake $ROOTDIR +make +make install diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index 9990a89..c13dd53 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -558,6 +558,18 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { } } return; + case F_GETLK: + case F_SETLK: + case F_SETLKW: + case F_OFD_GETLK: + case F_OFD_SETLK: + case F_OFD_SETLKW: + /* + * perform the SC on the VUFS virtualized file + * using the real SC + * */ + printkdebug(F, "wi_fcntl for file locking: %d", cmd); + return; } } else { switch (cmd) { /* common mgmt real fd*/ @@ -568,6 +580,19 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { case F_SETFL: sd->action = DOIT_CB_AFTER; return; + case F_GETLK: + case F_SETLK: + case F_SETLKW: + case F_OFD_GETLK: + case F_OFD_SETLK: + case F_OFD_SETLKW: + /* + * File locking commands + * if the file is not virtualized, execute the real SC + * then check for the return value in the wrapoutf function + * */ + sd->action = DOIT_CB_AFTER; + return; } } if (nested) { @@ -632,10 +657,52 @@ void wo_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { vu_fd_set_flflags(fd, nested, flags); } break; + case F_GETLK: + case F_SETLK: + case F_SETLKW: + case F_OFD_GETLK: + case F_OFD_SETLK: + case F_OFD_SETLKW: + printkdebug(F, "wo_fcntl for file locking: %d", cmd); + printkdebug(F, "fcntl orig_ret_value: %d", ret_value); + if (ret_value < 0) { + // check errno to know the type of the error + // |_ how to get errno value?? + printkdebug(F, "real fcntl call failed"); + } + return; } sd->ret_value = ret_value; } +void wi_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { + int fd = sd->syscall_args[0]; + int op = sd->syscall_args[1]; + printkdebug(F, "wi_flock on fd %d: %d", fd, op); + if (ht) { + // handle lock on virtualized file + } else { + // non-virtualized file, try to execute the real SC and check + // the result in the wrapout function + sd->action = DOIT_CB_AFTER; + } +} + +void wo_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { + // do not check for the ht variable: assume this function + // runs only if the file isn't virtualized + int fd = sd->syscall_args[0]; + int op = sd->syscall_args[1]; + int ret_value = sd->orig_ret_value; + printkdebug(F, "wo_flock on fd %d: %d", fd, op); + printkdebug(F, "flock orig_ret_value: %d", ret_value); + + if (ret_value < 0) { + // check errno as in wo_fcntl + printkdebug(F, "real flock call failed"); + } +} + /* umask */ /* umask always succeeds. just copy the value */ void wi_umask(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { diff --git a/vu_syscalls.conf b/vu_syscalls.conf index 3f1f942..1f780c8 100644 --- a/vu_syscalls.conf +++ b/vu_syscalls.conf @@ -71,6 +71,7 @@ capset/2: sc, capset, NULL, NULL clock_gettime/2, gettimeofday/2, time/1: sc, clock_gettime, NULL, NULL clock_settime/2, settimeofday/2: sc, clock_settime, NULL, NULL clock_getres/2: sc, clock_getres, NULL, NULL +flock/3: std, flock, NULL, flock BUILTIN execve/13, execveat/315: path, execve, NULL, execve From 93e446e2bdb5d78a148f7a261a81a173d1abbe5a Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Thu, 23 Jul 2020 17:26:25 +0200 Subject: [PATCH 04/18] Add/fix some examples, add comments, minor fixes Fix an error with the fcntl/flock wrapper functions that did not set the SC's actual return value Add some comments in VUOS Add some examples Fix some code/examples --- CMakeLists.txt | 2 +- demos/fcntlflock/fcntl_lock.c | 14 +++++++++----- demos/fcntlflock/flock_lock.c | 12 +++++++----- demos/flockdemo/Makefile | 2 ++ demos/flockdemo/executable | Bin 0 -> 19464 bytes demos/flockdemo/flockdemo.c | 18 ++++++++++++++++++ demos/openerr/Makefile | 2 ++ demos/openerr/executable | Bin 0 -> 17864 bytes demos/openerr/openerr.c | 10 ++++++++++ demos/wrongfcntl/Makefile | 3 +++ demos/wrongfcntl/executable | Bin 0 -> 19744 bytes demos/wrongfcntl/wrfcntl.c | 27 +++++++++++++++++++++++++++ lxt-run.sh | 2 +- umvu/src/umvu_tracer.c | 9 +++++++++ umvu/src/vu_wrap_file.c | 5 ++++- 15 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 demos/flockdemo/Makefile create mode 100755 demos/flockdemo/executable create mode 100644 demos/flockdemo/flockdemo.c create mode 100644 demos/openerr/Makefile create mode 100755 demos/openerr/executable create mode 100644 demos/openerr/openerr.c create mode 100644 demos/wrongfcntl/Makefile create mode 100755 demos/wrongfcntl/executable create mode 100644 demos/wrongfcntl/wrfcntl.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ece387..cf61f9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project("vuos") include(GNUInstallDirs) set(CMAKE_C_FLAGS - "-ggdb -Wall -Wextra -pedantic -std=gnu11 -Wno-unused-parameter") + "-ggdb -Wall -Wextra -pedantic -std=gnu11 -Wno-unused-parameter") add_definitions(-D_GNU_SOURCE) execute_process(COMMAND echo ${add_definitions} RESULT_VARIABLE rv) diff --git a/demos/fcntlflock/fcntl_lock.c b/demos/fcntlflock/fcntl_lock.c index 0a52072..4dabe34 100644 --- a/demos/fcntlflock/fcntl_lock.c +++ b/demos/fcntlflock/fcntl_lock.c @@ -122,7 +122,7 @@ void apply_lock(int index) { cmd = F_SETLK; break; case 3: - opened_locks[index] = opened_locks[2]; + opened_locks[index] = opened_locks[0]; // avoid double-freeing this pointer if the fcntl call fails opened_locks[index].lock_params = NULL; @@ -182,9 +182,13 @@ void flush_stdin() { int main(int argc, char **argv) { init(); - open_file("read.lock", O_RDONLY); - open_file("write.lock", O_WRONLY); - open_file("partial_read.lock", O_RDWR); + char *FILE1 = "read.lock"; + char *FILE2 = "write.lock"; + char *FILE3 = "partial_read.lock"; + + open_file(FILE1, O_RDONLY); + open_file(FILE2, O_WRONLY); + open_file(FILE3, O_RDWR); // write something into the file that will be partially locked populate_file(opened_locks[2].fd); @@ -208,7 +212,7 @@ int main(int argc, char **argv) { fgets(NULL, 0, stdin); flush_stdin(); // get another fd for the same file - int index = open_file("partial_read.lock", O_RDWR); + int index = open_file(FILE2, O_RDWR); // closig this fd will cause the release of all the opened locks // even if it wasn't used to acquire any lock close(opened_locks[index].fd); diff --git a/demos/fcntlflock/flock_lock.c b/demos/fcntlflock/flock_lock.c index 35bc9bc..27f0748 100644 --- a/demos/fcntlflock/flock_lock.c +++ b/demos/fcntlflock/flock_lock.c @@ -30,17 +30,18 @@ int get_first_free_lockinfo_index() { void open_file(char* path, int mode) { int res = open(path, O_CREAT | mode, S_IRUSR | S_IWUSR); - if (res < 0) { - printf("Error while opening %s\n", path); - return; - } - int i = get_first_free_lockinfo_index(); if (i == -1) { printf("No space left\n"); return; } + if (res < 0) { + opened_locks[i].fd = -1; + printf("Error while opening %s\n", path); + return; + } + opened_locks[i] = (struct lock_info) { .fd=res, .fd_o_mode=mode, .path=path, .operation=-1 }; printf("Successfully opened file %s, fd: %d\n", path, res); @@ -116,6 +117,7 @@ void apply_lock(int index) { printf("\n\t-------------------------------\n"); printf("\t Cannot apply lock on index %d\n\t (fd %d, error: %d)\n", index, fd, errnocpy); + printf("\t flock return value: %d\n", res); printf("\t-------------------------------\n\n"); return; } diff --git a/demos/flockdemo/Makefile b/demos/flockdemo/Makefile new file mode 100644 index 0000000..10f652d --- /dev/null +++ b/demos/flockdemo/Makefile @@ -0,0 +1,2 @@ +all: flockdemo.c + gcc -g flockdemo.c -o executable diff --git a/demos/flockdemo/executable b/demos/flockdemo/executable new file mode 100755 index 0000000000000000000000000000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F{caK2?oYu~x1ZM^O!@bJ_ILOH^9s+`PXv^wqT7_n#Eqw)|EPY( zyZ;``J_ai4#=q>srvr{7Q<@J}7tR~&z7B@w>~s%N;dv?loffx0%S`_fm<}9_=lS}r zUW&Fj-W+LBX8gaxKx2>byx!u!gL7TApT{TT_&U=3o?IBu>#}PhC{eaLq;h6F)3;!$ z#*46W?Q^IiO1ZTC{|QSqp69cD3drr`09-H2KLJBJZhtuJSi%bL_E+g}oN&H_gLLk8 zGJd)55029UhWScRFeg<&cU-scUCZE~QNX|Aj;Y{o{HA5_2kE&3iq{2jiK1^A{181q zLK$)aT%y>g@a}$|A8_!W +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + int fd = open("/proc/self", O_RDONLY); + int res = syscall(73, -23, LOCK_SH); + + printf("fd: %d, return value: %d\n", fd, res); + + return 0; +} diff --git a/demos/openerr/Makefile b/demos/openerr/Makefile new file mode 100644 index 0000000..a67b545 --- /dev/null +++ b/demos/openerr/Makefile @@ -0,0 +1,2 @@ +all: openerr.c + gcc -g openerr.c -o executable diff --git a/demos/openerr/executable b/demos/openerr/executable new file mode 100755 index 0000000000000000000000000000000000000000..e6cbdef9d7abb6d25b4dc9b0c56f41174ab580a9 GIT binary patch literal 17864 zcmeHPU2GiH6~43U^{(T1?ZkvS39Tm~X+yzlJ0Za#ak6WB<1uo62o98=WV-f_y$k!3 z?#>zqS~XxmED#!`s;a71qVxq+9su{m#ri zo7wTYqAwMy=1M!?J>Na&+AYZ zjN{c}y=a8oU~zVN)CQE=DdnrhkmUV9vU^sRyYVWi$H*-tB)g^3Hv@JFqmca|JF7^_ z%rkx_D_~^4TAwOwbfG5g7@c+$oB1x=ZeGTZQIq{XAd&4zB)f~!?xM6~oRjr2ay%&} z^m|46bK?-oC^3>Qr33GpNy;~$V=??EZWHpJ#7l)SGd6{oI9T^zZ zH%B%{whDb@Y_Fa)3dU5rXc~pFy@Q#2&KOIKXUK9Yo6kv0-BzuuNR6VX+CK$nhhod) zptL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?ZOV~kba30~RcXj>O96fn)sp6h%C!Y5)x}U(L z>r7WepfxDl^Zbxebt)sz_cx|{k#XAdJd$w(D@t_R4}rgcH7VE6^G!y!x7JM1wHCb& zpZk!fbpzY;yp-`-sY~m8>O0%ha*q-%<7kS@_B?-Oe2|nJ$sNBVD5Etu+w(k^k>(wg zxc#ihcn`{G4bL*qhZ$8eMsl~`W`(#98Hy>}^SqhyWvR&ZyZir;v>%Xq{C5dPjuYAA zX}>6bPlBVE@ZVABQD`NJWaCWKR%a?haN4WVu8K}&5P_>LS}mRl9^Sd=kG#*`tNfEx6^IUGW~Ujd~y4Ee%B-IxgOS+;b2i*LXJ48Ugg<-spkj# z2_nP!sxdO7(xKb0TbJhlZjStXTN=6-cy8VGOW5B}4<;nKzMaW!-?M~$$7b7V)M<>o z+kU6CclR?X?P+{F6WcqHuRW(;bc-#`$bq{KBDv)A$@gWtzwo)YE$ri|?KTEn&beOJ TrR7OI`(N*{HHKXVE>Zj&w2rvF literal 0 HcmV?d00001 diff --git a/demos/openerr/openerr.c b/demos/openerr/openerr.c new file mode 100644 index 0000000..677c877 --- /dev/null +++ b/demos/openerr/openerr.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main(int argc, char **argv) { + const char path[] = "non-existent-file"; + int fd = open(path, O_RDONLY); + return fd; +} + diff --git a/demos/wrongfcntl/Makefile b/demos/wrongfcntl/Makefile new file mode 100644 index 0000000..bb57bfd --- /dev/null +++ b/demos/wrongfcntl/Makefile @@ -0,0 +1,3 @@ +all: wrfcntl.c + gcc -g wrfcntl.c -o executable + diff --git a/demos/wrongfcntl/executable b/demos/wrongfcntl/executable new file mode 100755 index 0000000000000000000000000000000000000000..da61781e5237aaa6f23671f0d81dde01888a93c6 GIT binary patch literal 19744 zcmeHPeQ;FQb-%l-)$T*mN+1~|AZ5Wop|*Af1|dEaRzeFakHCb01GVvEwfk1uvf5R5 z-wMgCYZBa~ii!i1#_=D{gf?x*ZQ3c#bUJP)6CkE|l4d-((=?=xG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5H^x=3mNx9wF)W{8#PCHKf7%AG|2MYz|M(!RdzuegrlEpdj zG`@`eM*nFGdg5>BNHbBRU6HiA|wYMLNzp$18W-N_@-qF zrJS6QkTI+l=5P{Dn6pf+6>3><9WBI1(g`%5Gs!bLN4wh)f-6F}OlV6F%or>V!6cp? z!ZoE>d1zqK;9%YuV+sCm27~JeSEcLrm&9ntfImI>t*{F3SD89@4y0=@_ky8&yKK+< zTBbp}GV1(yli^LM*zI}$%XBR(D(bZF1O6`V^>X{XPiD&YUS-eso!RXHRPZY~*MHtm zGxe(h(>+0|vOV1}pz<(2v^D4UdEd?Sc~Y_!XZ+|^DBa6ud)}WjU8-!k|E$OKB;<5& zoO#~IGYu+xXa5x}9!G^@%J#h9XL?yFa{JElKc(z@mEH}d$aLIhk6p^V_&p7bVnY3s zh4(9pl8wDkU+sl%u-ff|%8uy?yE5ty_u`Kn_NSEsQywq7VV)`7Z?m`0zw1otj;pnh7dJMV%)Cr~ThL>{lu~rn{6oCQkcH4tqXVG3D!r z?Ky7De+v~l6Y%kt_r3i8*Esc=XZl^(Hrnj@JaDTWAPVbQ++CjWA3;ECkK5<-9RL47 z(=<6Kygu2E=aJ42q{8-m?z<5tDPYJ^T3pIr|y~s?7v9QJ5X921`bwqEMWiDt(HAKKe87GD|RV+=Q!)> z;EVFg+=XrNY;UIy6{$GA-O|6I7Vc`WoAKfK +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + int fd = 999; + + if (argc > 1) { + fd = open(*argv, O_RDWR); + } + + struct flock lockinfo = { 9472, SEEK_SET, 0, 0 }; + int res = fcntl(fd, F_SETLK, &lockinfo); + int error = errno; + + printf("result: %d", res); + if (res < 0) { + printf(", error: %d", error); + } + + printf("\n"); + + return res; +} diff --git a/lxt-run.sh b/lxt-run.sh index 8dd0f04..cb18414 100755 --- a/lxt-run.sh +++ b/lxt-run.sh @@ -1,2 +1,2 @@ #!/bin/bash -lxterminal --title="UMVU SHELL" --working-directory=$PWD/test_demos/fcntlflock --geometry=140x30 --command="umvu bash" +lxterminal --title="UMVU SHELL" --working-directory=$PWD/demos --geometry=140x30 --command="umvu bash" diff --git a/umvu/src/umvu_tracer.c b/umvu/src/umvu_tracer.c index f2f5198..0ca542a 100644 --- a/umvu/src/umvu_tracer.c +++ b/umvu/src/umvu_tracer.c @@ -411,6 +411,12 @@ static int umvu_trace_seccomp(pid_t tracee_tid) P_CONT(sig_tid, 0L); } else P_CONT(sig_tid, 0L); + /* + * PTRACE_O_SYSGOOD option was set by the tracer, so when + * the syscall-exit-stop event occurs, WSTOPSIG(wstatus) will + * be equal to (SIGTRAP | 0x80) to be able to tell it apart + * from the syscall-enter-stop event o normal traps + * */ } else if (WSTOPSIG(wstatus) == (SIGTRAP | 0x80)) { if (syscall_desc.waiting_pid != 0) r_kill(syscall_desc.waiting_pid, SIGKILL); @@ -423,6 +429,9 @@ static int umvu_trace_seccomp(pid_t tracee_tid) umvu_poke_syscall(®s, &syscall_desc, POKE_ARGS); P_SETREGS(sig_tid, ®s); } else { + /* If the SC's return value has been modified by the wrap + * function, update the corresponding register + * */ if (umvu_poke_syscall(®s, &syscall_desc, POKE_RETVALUE)) P_SETREGS(sig_tid, ®s); syscall_desc.inout = NULL; diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index c13dd53..816eaa2 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -665,12 +665,13 @@ void wo_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { case F_OFD_SETLKW: printkdebug(F, "wo_fcntl for file locking: %d", cmd); printkdebug(F, "fcntl orig_ret_value: %d", ret_value); + printkdebug(F, "orig_rax value: %d", sd->syscall_number); if (ret_value < 0) { // check errno to know the type of the error // |_ how to get errno value?? printkdebug(F, "real fcntl call failed"); } - return; + break; } sd->ret_value = ret_value; } @@ -701,6 +702,8 @@ void wo_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { // check errno as in wo_fcntl printkdebug(F, "real flock call failed"); } + + sd->ret_value = ret_value; } /* umask */ From f7cd49a86da391faf9918aa519c947469a13ad3c Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Fri, 31 Jul 2020 10:52:25 +0200 Subject: [PATCH 05/18] First implementation stub for the fcntl sc virtualization Add the possibility for a module to export custom "sc"s. Add custom sc export conformance to all modules. --- .gitignore | 1 + demos/fcntlflock/fcntl_lock.c | 8 ++- include/vumodule.h | 2 + test_modules/mountreal.c | 4 +- test_modules/netlinkdump.c | 4 +- test_modules/unreal.c | 4 +- test_modules/unrealcap.c | 4 +- test_modules/unrealinfofs.c | 4 +- test_modules/unrealsock.c | 4 +- test_modules/unrealuidgid.c | 4 +- umvu/include/service.h | 15 +++++- umvu/src/vu_modutils.c | 26 +++++++++- umvu/src/vu_wrap_file.c | 66 +++++++++++++++++++++--- vubinfmt/vubinfmt.c | 4 +- vudev/vudev.c | 4 +- vufs/vufs.c | 96 +++++++++++++++++++---------------- vufs/vufsops.c | 61 +++++++++++++++++++++- vufuse/vufuse.c | 4 +- vumisc/vumisc.c | 4 +- vunet/vunet.c | 4 +- 20 files changed, 258 insertions(+), 65 deletions(-) diff --git a/.gitignore b/.gitignore index 93d7b28..b2b2e78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ demos/build/ +*.swp diff --git a/demos/fcntlflock/fcntl_lock.c b/demos/fcntlflock/fcntl_lock.c index 4dabe34..b748619 100644 --- a/demos/fcntlflock/fcntl_lock.c +++ b/demos/fcntlflock/fcntl_lock.c @@ -182,16 +182,22 @@ void flush_stdin() { int main(int argc, char **argv) { init(); + /* char *FILE1 = "read.lock"; char *FILE2 = "write.lock"; char *FILE3 = "partial_read.lock"; + */ + + char *FILE1 = "/etc/passwd"; + char *FILE2 = "/etc/shadow"; + char *FILE3 = "/etc/passwd"; open_file(FILE1, O_RDONLY); open_file(FILE2, O_WRONLY); open_file(FILE3, O_RDWR); // write something into the file that will be partially locked - populate_file(opened_locks[2].fd); + //populate_file(opened_locks[2].fd); fgets(NULL, 0, stdin); flush_stdin(); // flush the stream to delete all unmatched characters diff --git a/include/vumodule.h b/include/vumodule.h index f547f4d..023f8d9 100644 --- a/include/vumodule.h +++ b/include/vumodule.h @@ -35,6 +35,8 @@ struct vuht_entry_t; struct vu_module_t { char *name; char *description; + int mod_nr_vsyscalls; + char **vsyscalls; }; typedef unsigned long int syscall_arg_t; diff --git a/test_modules/mountreal.c b/test_modules/mountreal.c index 82ea7dc..4d5281b 100644 --- a/test_modules/mountreal.c +++ b/test_modules/mountreal.c @@ -35,7 +35,9 @@ VU_PROTOTYPES(mountreal) struct vu_module_t vu_module = { .name = "mountreal", - .description = "Mount mapping to FS (server side)" + .description = "Mount mapping to FS (server side)", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; struct mountreal_entry { diff --git a/test_modules/netlinkdump.c b/test_modules/netlinkdump.c index 81e65d3..81c8403 100644 --- a/test_modules/netlinkdump.c +++ b/test_modules/netlinkdump.c @@ -35,7 +35,9 @@ VU_PROTOTYPES(netlinkdump) struct vu_module_t vu_module = { .name = "netlinkdump", - .description = "dump netlink messages" + .description = "dump netlink messages", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; static struct vuht_entry_t *ht; diff --git a/test_modules/unreal.c b/test_modules/unreal.c index 920ded6..a4532c0 100644 --- a/test_modules/unreal.c +++ b/test_modules/unreal.c @@ -58,7 +58,9 @@ VU_PROTOTYPES(unreal) struct vu_module_t vu_module = { .name = "unreal", - .description = "Mapping to FS (server side)" + .description = "Mapping to FS (server side)", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; int vu_unreal_getdents64(unsigned int fd, struct dirent64 *dirp, unsigned int count, void *private) { diff --git a/test_modules/unrealcap.c b/test_modules/unrealcap.c index b1517ee..199d8b3 100644 --- a/test_modules/unrealcap.c +++ b/test_modules/unrealcap.c @@ -41,7 +41,9 @@ VU_PROTOTYPES(unrealcap) struct vu_module_t vu_module = { .name = "unrealcap", - .description = "virtualize capabilities" + .description = "virtualize capabilities", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; struct vu_cap_t { diff --git a/test_modules/unrealinfofs.c b/test_modules/unrealinfofs.c index 91422e1..dde50ad 100644 --- a/test_modules/unrealinfofs.c +++ b/test_modules/unrealinfofs.c @@ -61,7 +61,9 @@ VU_PROTOTYPES(unrealinfofs) struct vu_module_t vu_module = { .name = "unrealinfofs", - .description = "example of informational file system" + .description = "example of informational file system", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; struct info { diff --git a/test_modules/unrealsock.c b/test_modules/unrealsock.c index e4da75a..f780274 100644 --- a/test_modules/unrealsock.c +++ b/test_modules/unrealsock.c @@ -35,7 +35,9 @@ VU_PROTOTYPES(unrealsock) struct vu_module_t vu_module = { .name = "unrealsock", - .description = "tcp-ip stack server side" + .description = "tcp-ip stack server side", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; static struct vuht_entry_t *ht[3]; diff --git a/test_modules/unrealuidgid.c b/test_modules/unrealuidgid.c index f184ed2..cd8187d 100644 --- a/test_modules/unrealuidgid.c +++ b/test_modules/unrealuidgid.c @@ -34,7 +34,9 @@ VU_PROTOTYPES(unrealuidgid) struct vu_module_t vu_module = { .name = "unrealuidgid", - .description = "virtualize uid gid" + .description = "virtualize uid gid", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; struct vu_uid_gid_t { diff --git a/umvu/include/service.h b/umvu/include/service.h index 659ba8e..e827103 100644 --- a/umvu/include/service.h +++ b/umvu/include/service.h @@ -3,6 +3,7 @@ #include #include +#include /* each module define a service. services are registered in the hashtable (he key is the module name */ @@ -23,7 +24,7 @@ struct vu_service_t { struct vuht_entry_t *service_ht; // private data of the module (modules can use this pointer as they please. void *private; - // table of vu_syscalls implementation. + // table of vu_syscalls implementation + visrtual syscalls added by the module syscall_t module_syscall[]; }; @@ -60,5 +61,17 @@ __attribute__((always_inline)) return service->module_syscall[vu_syscall_number]; } +__attribute__((always_inline)) + static inline syscall_t service_vsyscall(struct vuht_entry_t *ht, int vu_syscall_number) { + struct vu_service_t *service = vuht_get_service(ht); + if (service->mod->vsyscalls != NULL && vu_syscall_number < service->mod->mod_nr_vsyscalls) + return service->module_syscall[VU_NR_MODULE_SYSCALLS + vu_syscall_number]; + + /* + * if the module doesn't declare any new sc, + * the element at VU_NR_MODULE_SYSCALLS is always sys_enosys */ + return service->module_syscall[VU_NR_MODULE_SYSCALLS]; + } + #endif diff --git a/umvu/src/vu_modutils.c b/umvu/src/vu_modutils.c index cef2563..13b62a7 100644 --- a/umvu/src/vu_modutils.c +++ b/umvu/src/vu_modutils.c @@ -121,8 +121,9 @@ struct vu_service_t *module_load(const char *modname) #pragma GCC diagnostic ignored "-Wpedantic" if ((module = dlsym(handle, "vu_module"))) { #pragma GCC diagnostic pop + int VSYSCALL_NR = module->mod_nr_vsyscalls > 0 ? module->mod_nr_vsyscalls : 1; struct vu_service_t *service = malloc(sizeof(struct vu_service_t) + - VU_NR_SYSCALLS * sizeof(syscall_t)); + (VU_NR_SYSCALLS + VSYSCALL_NR) * sizeof(syscall_t)); int prefixlen = strlen(module->name) + 4; int fnamelen = prefixlen + VU_SYSCALL_MAX_NAMELEN + 1; char fname[fnamelen]; @@ -146,6 +147,29 @@ struct vu_service_t *module_load(const char *modname) printkdebug(m, "%s syscall %s -> %s", module->name, vu_syscall_names[i], fname); } } + + /* + * note that even if the module doesn't export any new sc, one is added + * anyway at the end of the array + * */ + if (module->mod_nr_vsyscalls == 0) { + service->module_syscall[i] = sys_enosys; + return service; + } + + // load new syscalls added by the module + for (; i < VU_NR_MODULE_SYSCALLS + VSYSCALL_NR; i++) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" + service->module_syscall[i] = dlsym(handle, module->vsyscalls[i - VU_NR_MODULE_SYSCALLS]); +#pragma GCC diagnostic pop + if (service->module_syscall[i] == NULL) { + service->module_syscall[i] = sys_enosys; + } else { + printkdebug(m, "%s %s module vsyscall added", module->name, module->vsyscalls[i - VU_NR_MODULE_SYSCALLS]); + } + } + return service; } else { errno = EINVAL; diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index 816eaa2..3d466e1 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -569,6 +569,50 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { * using the real SC * */ printkdebug(F, "wi_fcntl for file locking: %d", cmd); + /* + * let VUFS manage this since it can create virtual representations + * of the fs and apply locks on them + * */ + + // define the struct here to allow access to its fields + struct vufs_t { + pthread_mutex_t mutex; + + char *source; + char *target; + int rdirfd; + int vdirfd; + int ddirfd; + int flags; + + char *except[]; + }; + + struct vufs_t *vufs = vu_get_ht_private_data(); + char *path = sd->extra->path + 1; + + printkdebug(F, "virtual hierarchy root fd: %d", vufs->vdirfd); + printkdebug(F, "the path of the file is /%s", sd->extra->path); + + // call the VUFS-added sc to copy the file in the virtual hierarchy + size_t MAX_SIZE = ((1ULL<<((sizeof(size_t)*8)-1))-1); + int res = service_vsyscall(ht, 0)(vufs, path, MAX_SIZE); + if (res < 0) { + // copyfile didn't work, try to execute the sc on the original file anyway + printkdebug(F, "Could not create virtual copy of %s", sd->extra->path); + } else { + int flags = vu_fd_get_fdflags(fd, sd->extra->nested); + int vfd = openat(vufs->vdirfd, path, flags | O_CREAT, 0600); + //int vfd = openat(vufs->vdirfd, path+1, flags); + + if (vfd < 0) { + // cannot open file, go on anyway + printkdebug(F, "Could not open virtual copy of file %s", sd->extra->path); + } else { + sd->syscall_args[0] = vfd; + } + } + return; } } else { @@ -591,7 +635,10 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { * if the file is not virtualized, execute the real SC * then check for the return value in the wrapoutf function * */ - sd->action = DOIT_CB_AFTER; + /* + * Actually without VUFS we can't do much, so we let the + * SC run unmodified and fail if it has to + * */ return; } } @@ -663,6 +710,11 @@ void wo_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { case F_OFD_GETLK: case F_OFD_SETLK: case F_OFD_SETLKW: + /* + * Actually this shouldn't be needed since we call the VUFS + * fcntl implementations directly in the wrapin function + * (and the action should be SKIPIT [?]) + * */ printkdebug(F, "wo_fcntl for file locking: %d", cmd); printkdebug(F, "fcntl orig_ret_value: %d", ret_value); printkdebug(F, "orig_rax value: %d", sd->syscall_number); @@ -676,17 +728,19 @@ void wo_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { sd->ret_value = ret_value; } +/* + * The following two wrapper functions should behave (almost) as the + * fcntl ones since their goal is very similar + * */ void wi_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { int fd = sd->syscall_args[0]; int op = sd->syscall_args[1]; printkdebug(F, "wi_flock on fd %d: %d", fd, op); if (ht) { // handle lock on virtualized file - } else { - // non-virtualized file, try to execute the real SC and check - // the result in the wrapout function - sd->action = DOIT_CB_AFTER; - } + } + + // always virtualize } void wo_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { diff --git a/vubinfmt/vubinfmt.c b/vubinfmt/vubinfmt.c index 122c5ea..a53577d 100644 --- a/vubinfmt/vubinfmt.c +++ b/vubinfmt/vubinfmt.c @@ -33,7 +33,9 @@ VU_PROTOTYPES(vubinfmt) struct vu_module_t vu_module = { .name = "vubinfmt", - .description = "vu binfmt_misc support" + .description = "vu binfmt_misc support", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; struct vubinfmt_entry_t { diff --git a/vudev/vudev.c b/vudev/vudev.c index dd03439..98db7a9 100644 --- a/vudev/vudev.c +++ b/vudev/vudev.c @@ -37,7 +37,9 @@ VU_PROTOTYPES(vudev) struct vu_module_t vu_module = { .name = "vudev", - .description = "vu virtual devices" + .description = "vu virtual devices", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; #define VUDEVFLAGS_DEVID 1 diff --git a/vufs/vufs.c b/vufs/vufs.c index 9362153..1c1f3be 100644 --- a/vufs/vufs.c +++ b/vufs/vufs.c @@ -48,15 +48,15 @@ static int vufs_confirm(uint8_t type, void *arg, int arglen, struct vuht_entry_t } static int set_mount_options(const char *input, struct vufs_t *vufs) { - int tagc = stropt(input, NULL, NULL, 0); + int tagc = stropt(input, NULL, NULL, 0); int retval = 0; - if(tagc > 1) { - char buf[strlen(input)+1]; - char *tags[tagc]; - char *args[tagc]; + if(tagc > 1) { + char buf[strlen(input)+1]; + char *tags[tagc]; + char *args[tagc]; int excl_choice = 0; - stropt(input, tags, args, buf); - for (int i=0; tags[i] != NULL; i++) { + stropt(input, tags, args, buf); + for (int i=0; tags[i] != NULL; i++) { uint64_t strcasetag = strcase(tags[i]); if (vufs == NULL) { switch(strcasetag) { @@ -77,8 +77,8 @@ static int set_mount_options(const char *input, struct vufs_t *vufs) { } if (++excl_choice > 1) { printk(KERN_ERR "vufs: move, merge, cow and mincow are mutually exclusive\n", tags[i]); - return -1; - } + return -1; + } break; default: printk(KERN_ERR "vufs: %s unknown tag\n", tags[i]); @@ -161,11 +161,11 @@ int vu_vufs_mount(const char *source, const char *target, pthread_mutex_init(&(new_vufs->mutex), NULL); pthread_mutex_lock(&(new_vufs->mutex)); - vuht_pathadd(CHECKPATH, source, target, filesystemtype, mountflags, data, s, 0, vufs_confirm, new_vufs); + vuht_pathadd(CHECKPATH, source, target, filesystemtype, mountflags, data, s, 0, vufs_confirm, new_vufs); pthread_mutex_unlock(&(new_vufs->mutex)); - errno = 0; - return 0; + errno = 0; + return 0; rdirerr: close(new_vufs->vdirfd); vdirerr: @@ -175,21 +175,21 @@ int vu_vufs_mount(const char *source, const char *target, } int vu_vufs_umount2(const char *target, int flags) { - struct vuht_entry_t *ht = vu_mod_getht(); - int ret_value; - if ((ret_value = vuht_del(ht, flags)) < 0) { - errno = -ret_value; - return -1; - } - return 0; + struct vuht_entry_t *ht = vu_mod_getht(); + int ret_value; + if ((ret_value = vuht_del(ht, flags)) < 0) { + errno = -ret_value; + return -1; + } + return 0; } void vu_vufs_cleanup(uint8_t type, void *arg, int arglen,struct vuht_entry_t *ht) { - if (type == CHECKPATH) { - struct vufs_t *vufs = vuht_get_private_data(ht); - if (vufs == NULL) { - errno = EINVAL; - } else { + if (type == CHECKPATH) { + struct vufs_t *vufs = vuht_get_private_data(ht); + if (vufs == NULL) { + errno = EINVAL; + } else { if (vufs->ddirfd >= 0) close(vufs->ddirfd); if (vufs->rdirfd >= 0) @@ -200,38 +200,48 @@ void vu_vufs_cleanup(uint8_t type, void *arg, int arglen,struct vuht_entry_t *ht free(vufs->target); free(vufs); } - } + } } void *vu_vufs_init(void) { - struct vu_service_t *s = vu_mod_getservice(); + struct vu_service_t *s = vu_mod_getservice(); + /* the following assignments set the actual glibc function + * as the handler for every SC this module does not virtualize + * this tells the hypervisor to use the original implementation + * of the SC instead of the one provided by the module + * */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wincompatible-pointer-types" - vu_syscall_handler(s, read) = read; - vu_syscall_handler(s, write) = write; - vu_syscall_handler(s, lseek) = lseek; + vu_syscall_handler(s, read) = read; + vu_syscall_handler(s, write) = write; + vu_syscall_handler(s, lseek) = lseek; vu_syscall_handler(s, pread64) = pread; - vu_syscall_handler(s, pwrite64) = pwrite; - vu_syscall_handler(s, fcntl) = fcntl; + vu_syscall_handler(s, pwrite64) = pwrite; + vu_syscall_handler(s, fcntl) = fcntl; + #pragma GCC diagnostic pop - return NULL; + return NULL; } int vu_vufs_fini(void *private) { - return 0; + return 0; } - struct vu_module_t vu_module = { - .name = "vufs", - .description = "vu filesystem patchworking" - }; +char *vsyscalls[] = { [0] = "vufs_copyfile" }; + +struct vu_module_t vu_module = { + .name = "vufs", + .description = "vu filesystem patchworking", + .mod_nr_vsyscalls = 1, + .vsyscalls = vsyscalls +}; __attribute__((constructor)) - static void init(void) { - debug_set_name(V, "VUFS"); - } + static void init(void) { + debug_set_name(V, "VUFS"); + } __attribute__((destructor)) - static void fini(void) { - debug_set_name(V, ""); - } + static void fini(void) { + debug_set_name(V, ""); + } diff --git a/vufs/vufsops.c b/vufs/vufsops.c index 558d605..74c4ff2 100644 --- a/vufs/vufsops.c +++ b/vufs/vufsops.c @@ -35,6 +35,7 @@ #include #include #include +#include #define MAXSIZE ((1ULL<<((sizeof(size_t)*8)-1))-1) #define CHUNKSIZE 4096 @@ -94,7 +95,7 @@ static void vufs_copyfile_create_path_cb(void *arg, int dirfd, const char *path) } } -static int vufs_copyfile(struct vufs_t *vufs, const char *path, size_t truncate) { +int vufs_copyfile(struct vufs_t *vufs, const char *path, size_t truncate) { int fdin = openat(vufs->rdirfd, path, O_RDONLY, 0); if (fdin >= 0) { struct vu_stat instat; @@ -732,3 +733,61 @@ int vu_vufs_close(int fd, void *fdprivate) { pthread_mutex_unlock(&(vufs->mutex)); return retval; } + +// RECORD LOCKING SYSCALLS +/* +int vu_vufs_fcntl(int fd, int cmd, ...) { + va_list ap; + va_start(ap, cmd); + + switch (cmd) { + case F_SETLK: + case F_SETLKW: + case F_GETLK: + case F_OFD_SETLK: + case F_OFD_SETLKW: + case F_OFD_GETLK: + struct vufs_t *vufs = vu_get_ht_private_data(); + struct vuht_entry *ht = vu_mod_getht(); + char dest_path[MAXSIZE]; + struct flock *lockinfo; +*/ + /* + * get the original path from the fd table + * then make a copy in the virtual hierarchy + * */ +/* vu_fd_get_path(fd, 0, dest_path, PATH_MAX); + int retval = vufs_copyfile(vufs, dest_path, MAXSIZE); + + if (retval < 0) { + printkdebug(V, "Could not copy file %s", dest_path); + errno = EBADF; + retval = -1; + } else { + int flags = vu_fd_get_fdflags(fd, 0); + // could open with RDWR instead of retrieving the open flags ? + int vfd = openat(vufs->vdirfd, dest_path, flags); + + if (vfd < 0) { + printkdebug(V, "Could not open virtual copy of %s", dest_path); + errno = EBADF; + retval = -1; + } else { + lockinfo = (struct flock*) va_arg(ap, struct flock*); + retval = r_fcntl(vfd, cmd, lockinfo); + printkdebug(V, "fcntl returned %d", retval); + } + } + break; + default: + // manage other fcntl cases + break; + } + + va_end(ap); + return retval; +} + +int vu_vufs_flock(int fd, int operation) { +} +*/ diff --git a/vufuse/vufuse.c b/vufuse/vufuse.c index 4f11a53..36e43cb 100644 --- a/vufuse/vufuse.c +++ b/vufuse/vufuse.c @@ -35,7 +35,9 @@ VU_PROTOTYPES(vufuse) struct vu_module_t vu_module = { .name = "vufuse", - .description = "vu virtual file systems (user level FUSE)" + .description = "vu virtual file systems (user level FUSE)", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; /* values for INUSE and thread synchro */ diff --git a/vumisc/vumisc.c b/vumisc/vumisc.c index 166cea3..16d9921 100644 --- a/vumisc/vumisc.c +++ b/vumisc/vumisc.c @@ -68,7 +68,9 @@ VU_PROTOTYPES(vumisc) struct vu_module_t vu_module = { .name = "vumisc", - .description = "system call virtualization using info file system" + .description = "system call virtualization using info file system", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; struct vumisc_t { diff --git a/vunet/vunet.c b/vunet/vunet.c index 96a7098..d5be8a2 100644 --- a/vunet/vunet.c +++ b/vunet/vunet.c @@ -31,7 +31,9 @@ VU_PROTOTYPES(vunet) struct vu_module_t vu_module = { .name = "vunet", - .description = "vu virtual networking" + .description = "vu virtual networking", + .mod_nr_vsyscalls = 0, + .vsyscalls = NULL }; struct vunet_default_t { From 68f9a413e6f3e41a65c18ca9c7b95f6d642fa29a Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Fri, 31 Jul 2020 11:05:11 +0200 Subject: [PATCH 06/18] Remove modules virtual sc export --- test_modules/mountreal.c | 4 +--- test_modules/netlinkdump.c | 4 +--- test_modules/unreal.c | 4 +--- test_modules/unrealcap.c | 4 +--- test_modules/unrealinfofs.c | 4 +--- test_modules/unrealsock.c | 4 +--- test_modules/unrealuidgid.c | 4 +--- umvu/src/vu_modutils.c | 22 ---------------------- vubinfmt/vubinfmt.c | 4 +--- vudev/vudev.c | 4 +--- vufs/vufs.c | 4 +--- vufuse/vufuse.c | 4 +--- vumisc/vumisc.c | 4 +--- 13 files changed, 12 insertions(+), 58 deletions(-) diff --git a/test_modules/mountreal.c b/test_modules/mountreal.c index 4d5281b..82ea7dc 100644 --- a/test_modules/mountreal.c +++ b/test_modules/mountreal.c @@ -35,9 +35,7 @@ VU_PROTOTYPES(mountreal) struct vu_module_t vu_module = { .name = "mountreal", - .description = "Mount mapping to FS (server side)", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "Mount mapping to FS (server side)" }; struct mountreal_entry { diff --git a/test_modules/netlinkdump.c b/test_modules/netlinkdump.c index 81c8403..81e65d3 100644 --- a/test_modules/netlinkdump.c +++ b/test_modules/netlinkdump.c @@ -35,9 +35,7 @@ VU_PROTOTYPES(netlinkdump) struct vu_module_t vu_module = { .name = "netlinkdump", - .description = "dump netlink messages", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "dump netlink messages" }; static struct vuht_entry_t *ht; diff --git a/test_modules/unreal.c b/test_modules/unreal.c index a4532c0..920ded6 100644 --- a/test_modules/unreal.c +++ b/test_modules/unreal.c @@ -58,9 +58,7 @@ VU_PROTOTYPES(unreal) struct vu_module_t vu_module = { .name = "unreal", - .description = "Mapping to FS (server side)", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "Mapping to FS (server side)" }; int vu_unreal_getdents64(unsigned int fd, struct dirent64 *dirp, unsigned int count, void *private) { diff --git a/test_modules/unrealcap.c b/test_modules/unrealcap.c index 199d8b3..b1517ee 100644 --- a/test_modules/unrealcap.c +++ b/test_modules/unrealcap.c @@ -41,9 +41,7 @@ VU_PROTOTYPES(unrealcap) struct vu_module_t vu_module = { .name = "unrealcap", - .description = "virtualize capabilities", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "virtualize capabilities" }; struct vu_cap_t { diff --git a/test_modules/unrealinfofs.c b/test_modules/unrealinfofs.c index dde50ad..91422e1 100644 --- a/test_modules/unrealinfofs.c +++ b/test_modules/unrealinfofs.c @@ -61,9 +61,7 @@ VU_PROTOTYPES(unrealinfofs) struct vu_module_t vu_module = { .name = "unrealinfofs", - .description = "example of informational file system", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "example of informational file system" }; struct info { diff --git a/test_modules/unrealsock.c b/test_modules/unrealsock.c index f780274..e4da75a 100644 --- a/test_modules/unrealsock.c +++ b/test_modules/unrealsock.c @@ -35,9 +35,7 @@ VU_PROTOTYPES(unrealsock) struct vu_module_t vu_module = { .name = "unrealsock", - .description = "tcp-ip stack server side", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "tcp-ip stack server side" }; static struct vuht_entry_t *ht[3]; diff --git a/test_modules/unrealuidgid.c b/test_modules/unrealuidgid.c index cd8187d..f184ed2 100644 --- a/test_modules/unrealuidgid.c +++ b/test_modules/unrealuidgid.c @@ -34,9 +34,7 @@ VU_PROTOTYPES(unrealuidgid) struct vu_module_t vu_module = { .name = "unrealuidgid", - .description = "virtualize uid gid", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "virtualize uid gid" }; struct vu_uid_gid_t { diff --git a/umvu/src/vu_modutils.c b/umvu/src/vu_modutils.c index 13b62a7..0809043 100644 --- a/umvu/src/vu_modutils.c +++ b/umvu/src/vu_modutils.c @@ -148,28 +148,6 @@ struct vu_service_t *module_load(const char *modname) } } - /* - * note that even if the module doesn't export any new sc, one is added - * anyway at the end of the array - * */ - if (module->mod_nr_vsyscalls == 0) { - service->module_syscall[i] = sys_enosys; - return service; - } - - // load new syscalls added by the module - for (; i < VU_NR_MODULE_SYSCALLS + VSYSCALL_NR; i++) { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" - service->module_syscall[i] = dlsym(handle, module->vsyscalls[i - VU_NR_MODULE_SYSCALLS]); -#pragma GCC diagnostic pop - if (service->module_syscall[i] == NULL) { - service->module_syscall[i] = sys_enosys; - } else { - printkdebug(m, "%s %s module vsyscall added", module->name, module->vsyscalls[i - VU_NR_MODULE_SYSCALLS]); - } - } - return service; } else { errno = EINVAL; diff --git a/vubinfmt/vubinfmt.c b/vubinfmt/vubinfmt.c index a53577d..122c5ea 100644 --- a/vubinfmt/vubinfmt.c +++ b/vubinfmt/vubinfmt.c @@ -33,9 +33,7 @@ VU_PROTOTYPES(vubinfmt) struct vu_module_t vu_module = { .name = "vubinfmt", - .description = "vu binfmt_misc support", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "vu binfmt_misc support" }; struct vubinfmt_entry_t { diff --git a/vudev/vudev.c b/vudev/vudev.c index 98db7a9..dd03439 100644 --- a/vudev/vudev.c +++ b/vudev/vudev.c @@ -37,9 +37,7 @@ VU_PROTOTYPES(vudev) struct vu_module_t vu_module = { .name = "vudev", - .description = "vu virtual devices", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "vu virtual devices" }; #define VUDEVFLAGS_DEVID 1 diff --git a/vufs/vufs.c b/vufs/vufs.c index 1c1f3be..e799807 100644 --- a/vufs/vufs.c +++ b/vufs/vufs.c @@ -231,9 +231,7 @@ char *vsyscalls[] = { [0] = "vufs_copyfile" }; struct vu_module_t vu_module = { .name = "vufs", - .description = "vu filesystem patchworking", - .mod_nr_vsyscalls = 1, - .vsyscalls = vsyscalls + .description = "vu filesystem patchworking" }; __attribute__((constructor)) diff --git a/vufuse/vufuse.c b/vufuse/vufuse.c index 36e43cb..4f11a53 100644 --- a/vufuse/vufuse.c +++ b/vufuse/vufuse.c @@ -35,9 +35,7 @@ VU_PROTOTYPES(vufuse) struct vu_module_t vu_module = { .name = "vufuse", - .description = "vu virtual file systems (user level FUSE)", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "vu virtual file systems (user level FUSE)" }; /* values for INUSE and thread synchro */ diff --git a/vumisc/vumisc.c b/vumisc/vumisc.c index 16d9921..166cea3 100644 --- a/vumisc/vumisc.c +++ b/vumisc/vumisc.c @@ -68,9 +68,7 @@ VU_PROTOTYPES(vumisc) struct vu_module_t vu_module = { .name = "vumisc", - .description = "system call virtualization using info file system", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL + .description = "system call virtualization using info file system" }; struct vumisc_t { From c36084f6c5bb06bdd0dbab4cda420e7652c5cdd7 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Fri, 31 Jul 2020 11:54:27 +0200 Subject: [PATCH 07/18] Add first stub of VUFS fcntl implementation --- vufs/vufs.c | 3 ++- vufs/vufsops.c | 34 +++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/vufs/vufs.c b/vufs/vufs.c index e799807..30b9793 100644 --- a/vufs/vufs.c +++ b/vufs/vufs.c @@ -217,7 +217,8 @@ void *vu_vufs_init(void) { vu_syscall_handler(s, lseek) = lseek; vu_syscall_handler(s, pread64) = pread; vu_syscall_handler(s, pwrite64) = pwrite; - vu_syscall_handler(s, fcntl) = fcntl; + + /* removed fcntl line since it is virtualized */ #pragma GCC diagnostic pop return NULL; diff --git a/vufs/vufsops.c b/vufs/vufsops.c index 74c4ff2..49e2c1e 100644 --- a/vufs/vufsops.c +++ b/vufs/vufsops.c @@ -17,6 +17,7 @@ * along with this program. If not, see . * */ +#define _GNU_SOURCE #include #include @@ -95,7 +96,7 @@ static void vufs_copyfile_create_path_cb(void *arg, int dirfd, const char *path) } } -int vufs_copyfile(struct vufs_t *vufs, const char *path, size_t truncate) { +static int vufs_copyfile(struct vufs_t *vufs, const char *path, size_t truncate) { int fdin = openat(vufs->rdirfd, path, O_RDONLY, 0); if (fdin >= 0) { struct vu_stat instat; @@ -735,7 +736,6 @@ int vu_vufs_close(int fd, void *fdprivate) { } // RECORD LOCKING SYSCALLS -/* int vu_vufs_fcntl(int fd, int cmd, ...) { va_list ap; va_start(ap, cmd); @@ -751,20 +751,21 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { struct vuht_entry *ht = vu_mod_getht(); char dest_path[MAXSIZE]; struct flock *lockinfo; -*/ + /* * get the original path from the fd table * then make a copy in the virtual hierarchy * */ -/* vu_fd_get_path(fd, 0, dest_path, PATH_MAX); + vu_fd_get_path(fd, 0, dest_path, PATH_MAX); int retval = vufs_copyfile(vufs, dest_path, MAXSIZE); if (retval < 0) { - printkdebug(V, "Could not copy file %s", dest_path); + printkdebug(V, "Could not create virtual copy of file %s", dest_path); errno = EBADF; retval = -1; } else { - int flags = vu_fd_get_fdflags(fd, 0); + int flags = O_RDWR; + //int flags = vu_fd_get_fdflags(fd, 0); // could open with RDWR instead of retrieving the open flags ? int vfd = openat(vufs->vdirfd, dest_path, flags); @@ -774,13 +775,28 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { retval = -1; } else { lockinfo = (struct flock*) va_arg(ap, struct flock*); - retval = r_fcntl(vfd, cmd, lockinfo); + retval = fcntl(vfd, cmd, lockinfo); printkdebug(V, "fcntl returned %d", retval); } } break; + + case F_GETOWN_EX: + case F_SETOWN_EX: + struct f_owner_ex* ownp_arg = va_arg(ap, struct f_owner_ex*); + retval = fcntl(fd, cmd, ownp_arg); + + case F_GET_RW_HINT: + case F_SET_RW_HINT: + case F_GET_FILE_RW_HINT: + case F_SET_FILE_RW_HINT: + uint argp = va_arg(ap, uint64_t *); + retval = fcntl(fd, cmd, argp); + break; + default: - // manage other fcntl cases + int arg = va_arg(ap, int); + retval = fcntl(fd, cmd, arg); break; } @@ -790,4 +806,4 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { int vu_vufs_flock(int fd, int operation) { } -*/ + From ab6c70e27fc34134c88f862373610ff1d6244419 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Fri, 31 Jul 2020 19:02:38 +0200 Subject: [PATCH 08/18] Fix some problems, remove old code --- umvu/include/service.h | 12 ----------- umvu/src/vu_wrap_file.c | 44 +++++++---------------------------------- vufs/vufsops.c | 42 +++++++++++++++++++++------------------ 3 files changed, 30 insertions(+), 68 deletions(-) diff --git a/umvu/include/service.h b/umvu/include/service.h index e827103..f35b68a 100644 --- a/umvu/include/service.h +++ b/umvu/include/service.h @@ -61,17 +61,5 @@ __attribute__((always_inline)) return service->module_syscall[vu_syscall_number]; } -__attribute__((always_inline)) - static inline syscall_t service_vsyscall(struct vuht_entry_t *ht, int vu_syscall_number) { - struct vu_service_t *service = vuht_get_service(ht); - if (service->mod->vsyscalls != NULL && vu_syscall_number < service->mod->mod_nr_vsyscalls) - return service->module_syscall[VU_NR_MODULE_SYSCALLS + vu_syscall_number]; - - /* - * if the module doesn't declare any new sc, - * the element at VU_NR_MODULE_SYSCALLS is always sys_enosys */ - return service->module_syscall[VU_NR_MODULE_SYSCALLS]; - } - #endif diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index 3d466e1..dd1d7f2 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -569,48 +569,18 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { * using the real SC * */ printkdebug(F, "wi_fcntl for file locking: %d", cmd); + printkdebug(F, "the path of the file is %s", sd->extra->path); + /* * let VUFS manage this since it can create virtual representations * of the fs and apply locks on them * */ - - // define the struct here to allow access to its fields - struct vufs_t { - pthread_mutex_t mutex; - - char *source; - char *target; - int rdirfd; - int vdirfd; - int ddirfd; - int flags; - - char *except[]; - }; - - struct vufs_t *vufs = vu_get_ht_private_data(); - char *path = sd->extra->path + 1; - - printkdebug(F, "virtual hierarchy root fd: %d", vufs->vdirfd); - printkdebug(F, "the path of the file is /%s", sd->extra->path); - - // call the VUFS-added sc to copy the file in the virtual hierarchy - size_t MAX_SIZE = ((1ULL<<((sizeof(size_t)*8)-1))-1); - int res = service_vsyscall(ht, 0)(vufs, path, MAX_SIZE); - if (res < 0) { - // copyfile didn't work, try to execute the sc on the original file anyway - printkdebug(F, "Could not create virtual copy of %s", sd->extra->path); + struct flock *lockinfo = (struct flock*) sd->syscall_args[2]; + ret_value = service_syscall(ht, __VU_fcntl)(sfd, cmd, lockinfo, sd->extra->path); + if (ret_value < 0) { + sd->ret_value = -errno; } else { - int flags = vu_fd_get_fdflags(fd, sd->extra->nested); - int vfd = openat(vufs->vdirfd, path, flags | O_CREAT, 0600); - //int vfd = openat(vufs->vdirfd, path+1, flags); - - if (vfd < 0) { - // cannot open file, go on anyway - printkdebug(F, "Could not open virtual copy of file %s", sd->extra->path); - } else { - sd->syscall_args[0] = vfd; - } + sd->ret_value = ret_value; } return; diff --git a/vufs/vufsops.c b/vufs/vufsops.c index 49e2c1e..63c3113 100644 --- a/vufs/vufsops.c +++ b/vufs/vufsops.c @@ -17,7 +17,6 @@ * along with this program. If not, see . * */ -#define _GNU_SOURCE #include #include @@ -737,6 +736,8 @@ int vu_vufs_close(int fd, void *fdprivate) { // RECORD LOCKING SYSCALLS int vu_vufs_fcntl(int fd, int cmd, ...) { + int retval; + va_list ap; va_start(ap, cmd); @@ -746,18 +747,20 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { case F_GETLK: case F_OFD_SETLK: case F_OFD_SETLKW: - case F_OFD_GETLK: + case F_OFD_GETLK: ; struct vufs_t *vufs = vu_get_ht_private_data(); - struct vuht_entry *ht = vu_mod_getht(); - char dest_path[MAXSIZE]; - struct flock *lockinfo; + + /* retrieve variadic parameters */ + struct flock *lockinfo = va_arg(ap, struct flock*); + char *dest_path = va_arg(ap, char *); /* * get the original path from the fd table * then make a copy in the virtual hierarchy * */ - vu_fd_get_path(fd, 0, dest_path, PATH_MAX); - int retval = vufs_copyfile(vufs, dest_path, MAXSIZE); + //vu_fd_get_path(fd, 0, dest_path, PATH_MAX); + dest_path++; + retval = vufs_copyfile(vufs, dest_path, MAXSIZE); if (retval < 0) { printkdebug(V, "Could not create virtual copy of file %s", dest_path); @@ -765,8 +768,11 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { retval = -1; } else { int flags = O_RDWR; - //int flags = vu_fd_get_fdflags(fd, 0); - // could open with RDWR instead of retrieving the open flags ? + + // if this is used, vu_fd_table.h must be included + // int flags = vu_fd_get_fdflags(fd, 0); + + // TODO: remember to close this fd when the original one is int vfd = openat(vufs->vdirfd, dest_path, flags); if (vfd < 0) { @@ -774,7 +780,6 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { errno = EBADF; retval = -1; } else { - lockinfo = (struct flock*) va_arg(ap, struct flock*); retval = fcntl(vfd, cmd, lockinfo); printkdebug(V, "fcntl returned %d", retval); } @@ -782,21 +787,19 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { break; case F_GETOWN_EX: - case F_SETOWN_EX: - struct f_owner_ex* ownp_arg = va_arg(ap, struct f_owner_ex*); - retval = fcntl(fd, cmd, ownp_arg); + case F_SETOWN_EX: ; + retval = fcntl(fd, cmd, va_arg(ap, struct f_owner_ex*)); + break; case F_GET_RW_HINT: case F_SET_RW_HINT: case F_GET_FILE_RW_HINT: - case F_SET_FILE_RW_HINT: - uint argp = va_arg(ap, uint64_t *); - retval = fcntl(fd, cmd, argp); + case F_SET_FILE_RW_HINT: ; + retval = fcntl(fd, cmd, va_arg(ap, uint64_t *)); break; - default: - int arg = va_arg(ap, int); - retval = fcntl(fd, cmd, arg); + default: ; + retval = fcntl(fd, cmd, va_arg(ap, int)); break; } @@ -805,5 +808,6 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { } int vu_vufs_flock(int fd, int operation) { + return -1; } From 6b402edfaf8c08baa630b2dd6c2da3fd4178615b Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Mon, 3 Aug 2020 11:38:15 +0200 Subject: [PATCH 09/18] Lock info struct now is correctly read and written The struct flock argument now is correctly read from the tracee memory using umvu's peek/poke functions. --- umvu/src/vu_wrap_file.c | 38 +++++++++++++++++++++++++++++++++----- vufs/vufsops.c | 12 ++++-------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index dd1d7f2..bdf17ee 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -567,23 +567,51 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { /* * perform the SC on the VUFS virtualized file * using the real SC + * let VUFS manage this since it can create virtual representations + * of the fs and apply locks on them * */ printkdebug(F, "wi_fcntl for file locking: %d", cmd); printkdebug(F, "the path of the file is %s", sd->extra->path); - /* - * let VUFS manage this since it can create virtual representations - * of the fs and apply locks on them + uintptr_t flockaddr = sd->syscall_args[2]; + void *lockinfo = malloc(sizeof(struct flock)); + + /* + * the third parameter is a pointer to a struct flock + * so process memory cannot be accessed directly * */ - struct flock *lockinfo = (struct flock*) sd->syscall_args[2]; + if (umvu_peek_data(flockaddr, lockinfo, sizeof(struct flock)) < 0) { + printkdebug(F, "cannot peek flock info"); + free(lockinfo); + break; + } else { + printkdebug(F, "successfully retrieved lockinfo"); + } + + //struct flock *lockinfo = (struct flock*) sd->syscall_args[2]; ret_value = service_syscall(ht, __VU_fcntl)(sfd, cmd, lockinfo, sd->extra->path); if (ret_value < 0) { sd->ret_value = -errno; } else { + /* + * F_GETLK and F_OFD_GETLK could set the pid field in the struct flock* + * parameter of the process that is blocking the specified file, if any + * */ + if (cmd == F_GETLK || cmd == F_OFD_GETLK) { + /* as before, the tracee memory cannot be accessed directly */ + if (umvu_poke_data(flockaddr, lockinfo, sizeof(struct flock) < 0)) { + printkdebug(F, "cannot poke flock info"); + } else { + printkdebug(F, "successfully written data to tracee memory"); + } + } + sd->ret_value = ret_value; } - return; + free(lockinfo); + sd->action = SKIPIT; + break; } } else { switch (cmd) { /* common mgmt real fd*/ diff --git a/vufs/vufsops.c b/vufs/vufsops.c index 63c3113..4e428f0 100644 --- a/vufs/vufsops.c +++ b/vufs/vufsops.c @@ -754,11 +754,7 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { struct flock *lockinfo = va_arg(ap, struct flock*); char *dest_path = va_arg(ap, char *); - /* - * get the original path from the fd table - * then make a copy in the virtual hierarchy - * */ - //vu_fd_get_path(fd, 0, dest_path, PATH_MAX); + /* make a copy of the file in the virtual hierarchy */ dest_path++; retval = vufs_copyfile(vufs, dest_path, MAXSIZE); @@ -787,18 +783,18 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { break; case F_GETOWN_EX: - case F_SETOWN_EX: ; + case F_SETOWN_EX: retval = fcntl(fd, cmd, va_arg(ap, struct f_owner_ex*)); break; case F_GET_RW_HINT: case F_SET_RW_HINT: case F_GET_FILE_RW_HINT: - case F_SET_FILE_RW_HINT: ; + case F_SET_FILE_RW_HINT: retval = fcntl(fd, cmd, va_arg(ap, uint64_t *)); break; - default: ; + default: retval = fcntl(fd, cmd, va_arg(ap, int)); break; } From 33202b1ab5e5d50271ed19c34c440ca5fe3a374a Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Mon, 3 Aug 2020 16:49:11 +0200 Subject: [PATCH 10/18] Add flock virtualization Remove flock out function since it's no longer needed. --- umvu/src/vu_wrap_file.c | 66 +++++++++++------------------------------ vu_syscalls.conf | 2 +- vufs/vufsops.c | 34 +++++++++++++++++++-- 3 files changed, 50 insertions(+), 52 deletions(-) diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index bdf17ee..572211e 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -629,12 +629,7 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { case F_OFD_SETLK: case F_OFD_SETLKW: /* - * File locking commands - * if the file is not virtualized, execute the real SC - * then check for the return value in the wrapoutf function - * */ - /* - * Actually without VUFS we can't do much, so we let the + * without VUFS we can't do much, so we let the * SC run unmodified and fail if it has to * */ return; @@ -702,60 +697,33 @@ void wo_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { vu_fd_set_flflags(fd, nested, flags); } break; - case F_GETLK: - case F_SETLK: - case F_SETLKW: - case F_OFD_GETLK: - case F_OFD_SETLK: - case F_OFD_SETLKW: - /* - * Actually this shouldn't be needed since we call the VUFS - * fcntl implementations directly in the wrapin function - * (and the action should be SKIPIT [?]) - * */ - printkdebug(F, "wo_fcntl for file locking: %d", cmd); - printkdebug(F, "fcntl orig_ret_value: %d", ret_value); - printkdebug(F, "orig_rax value: %d", sd->syscall_number); - if (ret_value < 0) { - // check errno to know the type of the error - // |_ how to get errno value?? - printkdebug(F, "real fcntl call failed"); - } - break; } + sd->ret_value = ret_value; } -/* - * The following two wrapper functions should behave (almost) as the - * fcntl ones since their goal is very similar - * */ void wi_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { int fd = sd->syscall_args[0]; int op = sd->syscall_args[1]; - printkdebug(F, "wi_flock on fd %d: %d", fd, op); - if (ht) { - // handle lock on virtualized file - } + int ret_value; - // always virtualize -} + if (ht) { + printkdebug(F, "wi_flock for file locking: %d", op); + printkdebug(F, "the path of the file is %s", sd->extra->path); -void wo_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { - // do not check for the ht variable: assume this function - // runs only if the file isn't virtualized - int fd = sd->syscall_args[0]; - int op = sd->syscall_args[1]; - int ret_value = sd->orig_ret_value; - printkdebug(F, "wo_flock on fd %d: %d", fd, op); - printkdebug(F, "flock orig_ret_value: %d", ret_value); + ret_value = service_syscall(ht, __VU_flock)(fd, op, sd->extra->path); + if (ret_value < 0) { + sd->ret_value = -errno; + } else { + sd->ret_value = ret_value; + } - if (ret_value < 0) { - // check errno as in wo_fcntl - printkdebug(F, "real flock call failed"); - } + sd->ret_value = ret_value; + sd->action = SKIPIT; + return; + } - sd->ret_value = ret_value; + sd->action = DOIT; } /* umask */ diff --git a/vu_syscalls.conf b/vu_syscalls.conf index 1f780c8..3b53595 100644 --- a/vu_syscalls.conf +++ b/vu_syscalls.conf @@ -71,7 +71,7 @@ capset/2: sc, capset, NULL, NULL clock_gettime/2, gettimeofday/2, time/1: sc, clock_gettime, NULL, NULL clock_settime/2, settimeofday/2: sc, clock_settime, NULL, NULL clock_getres/2: sc, clock_getres, NULL, NULL -flock/3: std, flock, NULL, flock +flock/3: std, flock, NULL, NULL BUILTIN execve/13, execveat/315: path, execve, NULL, execve diff --git a/vufs/vufsops.c b/vufs/vufsops.c index 4e428f0..c0c6e65 100644 --- a/vufs/vufsops.c +++ b/vufs/vufsops.c @@ -36,6 +36,7 @@ #include #include #include +#include #define MAXSIZE ((1ULL<<((sizeof(size_t)*8)-1))-1) #define CHUNKSIZE 4096 @@ -803,7 +804,36 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { return retval; } -int vu_vufs_flock(int fd, int operation) { - return -1; +int vu_vufs_flock(int fd, int operation, char *dest_path) { + struct vufs_t *vufs = vu_get_ht_private_data(); + int retval; + + dest_path++; + retval = vufs_copyfile(vufs, dest_path, MAXSIZE); + + if (retval < 0) { + printkdebug(V, "Could not create virtual copy of file %s", dest_path); + errno = EBADF; + retval = -1; + } else { + int flags = O_RDWR; + + // if this is used, vu_fd_table.h must be included + // int flags = vu_fd_get_fdflags(fd, 0); + + // TODO: remember to close this fd when the original one is + int vfd = openat(vufs->vdirfd, dest_path, flags); + + if (vfd < 0) { + printkdebug(V, "Could not open virtual copy of %s", dest_path); + errno = EBADF; + retval = -1; + } else { + retval = flock(vfd, operation); + printkdebug(V, "fcntl returned %d", retval); + } + } + + return retval; } From 07888176026564e7d87896441ac3da6c672cfc63 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Tue, 4 Aug 2020 16:18:51 +0200 Subject: [PATCH 11/18] Remove extra files --- .gitignore | 3 +- demos/fcntlflock/Makefile | 15 --- demos/fcntlflock/dllock.c | 120 ----------------- demos/fcntlflock/fcntl_lock.c | 239 --------------------------------- demos/fcntlflock/fcntl_other.c | 36 ----- demos/fcntlflock/flock_lock.c | 177 ------------------------ demos/flockdemo/Makefile | 2 - demos/flockdemo/executable | Bin 19464 -> 0 bytes demos/flockdemo/flockdemo.c | 18 --- demos/openerr/Makefile | 2 - demos/openerr/executable | Bin 17864 -> 0 bytes demos/openerr/openerr.c | 10 -- demos/wrongfcntl/Makefile | 3 - demos/wrongfcntl/executable | Bin 19744 -> 0 bytes demos/wrongfcntl/wrfcntl.c | 27 ---- fixes.txt | 4 - lxt-run.sh | 2 - projbuild.sh | 20 --- 18 files changed, 2 insertions(+), 676 deletions(-) delete mode 100644 demos/fcntlflock/Makefile delete mode 100644 demos/fcntlflock/dllock.c delete mode 100644 demos/fcntlflock/fcntl_lock.c delete mode 100644 demos/fcntlflock/fcntl_other.c delete mode 100644 demos/fcntlflock/flock_lock.c delete mode 100644 demos/flockdemo/Makefile delete mode 100755 demos/flockdemo/executable delete mode 100644 demos/flockdemo/flockdemo.c delete mode 100644 demos/openerr/Makefile delete mode 100755 demos/openerr/executable delete mode 100644 demos/openerr/openerr.c delete mode 100644 demos/wrongfcntl/Makefile delete mode 100755 demos/wrongfcntl/executable delete mode 100644 demos/wrongfcntl/wrfcntl.c delete mode 100644 fixes.txt delete mode 100755 lxt-run.sh delete mode 100755 projbuild.sh diff --git a/.gitignore b/.gitignore index b2b2e78..29da3a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.gitignore build/ -demos/build/ +extra/ *.swp diff --git a/demos/fcntlflock/Makefile b/demos/fcntlflock/Makefile deleted file mode 100644 index d0528b7..0000000 --- a/demos/fcntlflock/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -BUILDDIR=build - -all: clean builddir fcntl_lock.c flock_lock.c fcntl_other.c dllock - gcc -g -o $(BUILDDIR)/fcntl_lock fcntl_lock.c - gcc -g -o $(BUILDDIR)/fcntl_other fcntl_other.c - gcc -g -o $(BUILDDIR)/flock_lock flock_lock.c - -dllock: dllock.c - gcc -g -shared -fPIC -o build/dllock.so dllock.c -ldl - -builddir: - mkdir $(BUILDDIR) - -clean: - rm -rf $(BUILDDIR) diff --git a/demos/fcntlflock/dllock.c b/demos/fcntlflock/dllock.c deleted file mode 100644 index 5eb99a3..0000000 --- a/demos/fcntlflock/dllock.c +++ /dev/null @@ -1,120 +0,0 @@ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include -#include - -// define the types of the original flock functions to be used with dlsym -typedef int (*real_flock)(int, int); -typedef int (*real_fcntl)(int, int, ...); - -// override the flock SC -int flock(int fd, int operation) { - // check whether the call has to be BLOCKING - int noblock = operation & LOCK_NB; - short fcntl_ltype; - - // tell fcntl whether to wait or not based on the LOCK_NB flag in operation - int fcntl_cmd = noblock ? F_OFD_SETLK : F_OFD_SETLKW; - - switch (operation & ~LOCK_NB) { - case LOCK_SH: // apply SHARED lock - fcntl_ltype = F_RDLCK; - break; - - case LOCK_EX: // apply EXCLUSIVE lock - fcntl_ltype = F_WRLCK; - break; - - case LOCK_UN: // UNlock - fcntl_ltype = F_UNLCK; - break; - - default: // operation is not valid - errno = EINVAL; - return -1; - } - - struct flock lockinfo = { fcntl_ltype, SEEK_SET, 0, 0, 0 }; - int res = fcntl(fd, fcntl_cmd, &lockinfo); - int errno_backup = errno; - int new_errno; - - if (res < 0) { - // the error generated when a call results in entering a waiting state - // is different between flock and fcntl - if (errno == EACCES || errno == EAGAIN) { - errno = EWOULDBLOCK; - } - - /* - * TODO: flock can apply locks regardless of the open mode of the target file, - * whereas fcntl locks must be applied to files which open mode match the - * desired lock type (READ for READ locks, etc.). - * In such cases fcntl raises an EBADF error, while flock successfully applies - * the lock so the behaviour of this method should change accordingly (assuming - * it is possible). - */ - - return -1; - } - - //return ((real_flock)dlsym(RTLD_NEXT, "flock"))(fd, operation); - return 0; -} - -int fcntl(int fd, int cmd, ...) { - va_list ap; - struct flock *lockinfo; - int res; - int errno_backup; - struct f_owner_ex *ownp_arg; - int int_arg; - uint64_t *uint_argp; - - // get the actual fcntl function address - real_fcntl r_fcntl = dlsym(RTLD_NEXT, "fcntl"); - va_start(ap, cmd); - - switch (cmd) { - case F_SETLK: - case F_SETLKW: - case F_GETLK: - case F_OFD_SETLK: - case F_OFD_SETLKW: - case F_OFD_GETLK: - // retrieve the flock struct pointer from the variadic parameter - lockinfo = va_arg(ap, struct flock *); - res = r_fcntl(fd, cmd, lockinfo); - break; - - case F_GETOWN_EX: - case F_SETOWN_EX: - ownp_arg = va_arg(ap, struct f_owner_ex *); - res = r_fcntl(fd, cmd, ownp_arg); - break; - - case F_GET_RW_HINT: - case F_SET_RW_HINT: - case F_GET_FILE_RW_HINT: - case F_SET_FILE_RW_HINT: - uint_argp = va_arg(ap, uint64_t *); - res = r_fcntl(fd, cmd, uint_argp); - break; - - default: // all fcntl calls that require an int as third argument - int_arg = va_arg(ap, int); - res = r_fcntl(fd, cmd, int_arg); - break; - } - - errno_backup = errno; - va_end(ap); - errno = errno_backup; - return res; -} - diff --git a/demos/fcntlflock/fcntl_lock.c b/demos/fcntlflock/fcntl_lock.c deleted file mode 100644 index b748619..0000000 --- a/demos/fcntlflock/fcntl_lock.c +++ /dev/null @@ -1,239 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define LI_MAX_SIZE 10 - -struct lock_info { - int fd; - int fd_o_mode; - char* path; - struct flock* lock_params; -}; - -struct lock_info opened_locks[LI_MAX_SIZE]; - -int get_first_free_lockinfo_index() { - int i; - - for (i = 0; i < LI_MAX_SIZE - && opened_locks[i].fd != 0; i++) ; - - return i < LI_MAX_SIZE ? i : -1; -} - -int push_to_locks_array(struct lock_info linfo) { - int i = get_first_free_lockinfo_index(); - if (i == -1) { - printf("No space left\n"); - return -1; - } - - opened_locks[i] = linfo; - return i; -} - -int open_file(char* path, int mode) { - int res = open(path, O_CREAT | mode, S_IRUSR | S_IWUSR); - - if (res < 0) { - printf("Error while opening %s\n", path); - return -1; - } - - struct lock_info linfo = (struct lock_info) { .fd=res, .fd_o_mode=mode, .path=path, .lock_params=NULL }; - int index = push_to_locks_array(linfo); - if (index != -1) { - printf("Successfully opened file %s, fd: %d\n", path, res); - return index; - } - - return -2; -} - -int duplicate(int atIndex) { - int newfd = dup(opened_locks[atIndex].fd); - - if (newfd < 0) { - printf("\ndup failed\n\n"); - return -1; - } - - struct lock_info linfo = opened_locks[atIndex]; - linfo.fd = newfd; - - int index = push_to_locks_array(linfo); - if (index != -1) { - printf("Successfully dup'ed fd %d, the new one is %d\n", opened_locks[atIndex].fd, newfd); - return index; - } - - return -2; -} - -void init() { - for (int i = 0; i < LI_MAX_SIZE; i++) { - opened_locks[i] = (struct lock_info) { .fd=0, .fd_o_mode=0, .path=NULL, .lock_params=NULL }; - } -} - -void deinit() { - for (int i = 0; i < LI_MAX_SIZE - && opened_locks[i].path != NULL - && opened_locks[i].lock_params != NULL; i++) { - unlink(opened_locks[i].path); - free(opened_locks[i].lock_params); - opened_locks[i].lock_params = NULL; - } -} - -const char* getTypeStrFromInt(int type) { - switch (type) { - case F_RDLCK: - return "F_RDLCK"; - case F_WRLCK: - return "F_WRLCK"; - case F_UNLCK: - return "F_UNLCK"; - default: - return "unknown"; - } -} - -void apply_lock(int index) { - // malloc'ing this because it will be stored after the method returns - struct flock* lockinfo = malloc(sizeof(struct flock)); - int cmd; - - switch (index) { - case 0: - *lockinfo = (struct flock) { F_RDLCK, SEEK_SET, 0, 0 }; - cmd = F_SETLK; - break; - case 1: - *lockinfo = (struct flock) { F_WRLCK, SEEK_SET, 0, 0 }; - cmd = F_SETLK; - break; - case 2: - *lockinfo = (struct flock) { F_RDLCK, SEEK_SET, 5, 7 }; - cmd = F_SETLK; - break; - case 3: - opened_locks[index] = opened_locks[0]; - - // avoid double-freeing this pointer if the fcntl call fails - opened_locks[index].lock_params = NULL; - - *lockinfo = (struct flock) { F_WRLCK, SEEK_SET, 2, 9 }; - cmd = F_SETLK; - break; - case 4: - *lockinfo = (struct flock) { F_UNLCK, SEEK_SET, 0, 0 }; - cmd = F_SETLK; - break; - default: - break; - } - - // apply the lock - int res = fcntl(opened_locks[index].fd, cmd, lockinfo); - if (res < 0) { - printf("\n\t-------------------------------\n"); - printf("\t Cannot apply lock on index %d\n", index); - printf("\t-------------------------------\n\n"); - - // pointer not stored anywhere so it must be free'd here - free(lockinfo); - return; - } - - // save the lock in the global array - struct lock_info* flinfo = &(opened_locks[index]); - flinfo->lock_params = lockinfo; - - printf("\n\t-------------------------------\n"); - printf("\t Lock applied to file %s, fd: %d\n", flinfo->path, flinfo->fd); - printf("\t l_type: %s\n", getTypeStrFromInt(lockinfo->l_type)); - printf("\t l_whence: %s\n", lockinfo->l_whence == SEEK_SET ? "SEEK_SET" : "SEEK_END or SEEK_CUR"); - printf("\t l_start: %d\n", lockinfo->l_start); - printf("\t l_len: %d\n", lockinfo->l_len); - printf("\t-------------------------------\n\n"); -} - -void populate_file(int fd) { - char* text = "this is the file content\n"; - - size_t writtenb = write(fd, text, 100); - printf("%d bytes written to fd %d\n", writtenb, fd); -} - -void flush_stdin() { - char c; - - // consume all characters in stdin - // not doing so will cause stdin unread characters to be read - // from the next fgets call, that will immediately return - while ((c = getchar()) != '\n' && c != EOF) ; -} - -int main(int argc, char **argv) { - init(); - - /* - char *FILE1 = "read.lock"; - char *FILE2 = "write.lock"; - char *FILE3 = "partial_read.lock"; - */ - - char *FILE1 = "/etc/passwd"; - char *FILE2 = "/etc/shadow"; - char *FILE3 = "/etc/passwd"; - - open_file(FILE1, O_RDONLY); - open_file(FILE2, O_WRONLY); - open_file(FILE3, O_RDWR); - - // write something into the file that will be partially locked - //populate_file(opened_locks[2].fd); - - fgets(NULL, 0, stdin); - flush_stdin(); // flush the stream to delete all unmatched characters - apply_lock(0); - - fgets(NULL, 0, stdin); - flush_stdin(); - apply_lock(1); - - fgets(NULL, 0, stdin); - flush_stdin(); - apply_lock(2); - - fgets(NULL, 0, stdin); - flush_stdin(); - apply_lock(3); - - fgets(NULL, 0, stdin); - flush_stdin(); - // get another fd for the same file - int index = open_file(FILE2, O_RDWR); - // closig this fd will cause the release of all the opened locks - // even if it wasn't used to acquire any lock - close(opened_locks[index].fd); - printf("fd %d closed\n", opened_locks[index].fd); - - fgets(NULL, 0, stdin); - flush_stdin(); - // get a dup'ed fd for the element at the specified index - int dupindex = duplicate(0); - // closing this fd will cause the release of all the locks onto the - // file the original fd referred to - close(opened_locks[dupindex].fd); - printf("fd %d closed\n", opened_locks[dupindex].fd); - - fgets(NULL, 0, stdin); - flush_stdin(); - deinit(); -} diff --git a/demos/fcntlflock/fcntl_other.c b/demos/fcntlflock/fcntl_other.c deleted file mode 100644 index 5c0014a..0000000 --- a/demos/fcntlflock/fcntl_other.c +++ /dev/null @@ -1,36 +0,0 @@ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include - -int main() { - int fd = open("/proc/locks", O_RDONLY); - if (fd < 0) { - printf("Error on open(2)\n"); - exit(fd); - } - - int dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 7); - printf("F_DUPFD result: %d\n", dupfd); - - int fl = fcntl(dupfd, F_GETFL, 34); - printf("F_GETFL result: %d\n", fl); - - struct f_owner_ex setown = { F_OWNER_PID, getpid() }; - struct f_owner_ex getown; - - fcntl(dupfd, F_SETOWN_EX, &setown); - fcntl(dupfd, F_GETOWN_EX, &getown); - - printf("F_GETOWN_EX result: type %d (%d), pid %d (%d)\n", - getown.type, - setown.type, - getown.pid, - setown.pid); - - return 0; -} diff --git a/demos/fcntlflock/flock_lock.c b/demos/fcntlflock/flock_lock.c deleted file mode 100644 index 27f0748..0000000 --- a/demos/fcntlflock/flock_lock.c +++ /dev/null @@ -1,177 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#define LI_MAX_SIZE 10 - -struct lock_info { - int fd; - int fd_o_mode; - char* path; - int operation; -}; - -struct lock_info opened_locks[LI_MAX_SIZE]; - -int get_first_free_lockinfo_index() { - int i; - - for (i = 0; i < LI_MAX_SIZE - && opened_locks[i].fd != 0; i++) ; - - return i < LI_MAX_SIZE ? i : -1; -} - -void open_file(char* path, int mode) { - int res = open(path, O_CREAT | mode, S_IRUSR | S_IWUSR); - - int i = get_first_free_lockinfo_index(); - if (i == -1) { - printf("No space left\n"); - return; - } - - if (res < 0) { - opened_locks[i].fd = -1; - printf("Error while opening %s\n", path); - return; - } - - opened_locks[i] = (struct lock_info) { .fd=res, .fd_o_mode=mode, .path=path, .operation=-1 }; - - printf("Successfully opened file %s, fd: %d\n", path, res); -} - -void deinit() { - for (int i = 0; i < LI_MAX_SIZE - && opened_locks[i].path != NULL; i++) { - unlink(opened_locks[i].path); - } -} - -void init() { - for (int i = 0; i < LI_MAX_SIZE; i++) { - opened_locks[i] = (struct lock_info) { .fd=0, .fd_o_mode=0, .path=NULL, .operation=-1 }; - } -} - -char* getOpStrFromInt(int cmd) { - int noblock = cmd & LOCK_NB; - char *nbstr = noblock ? " | LOCK_NB" : ""; - char *opstr; - - switch (cmd & ~LOCK_NB) { - case LOCK_SH: - opstr = "LOCK_SH"; - break; - case LOCK_EX: - opstr = "LOCK_EX"; - break; - case LOCK_UN: - opstr = "LOCK_UN"; - break; - default: - return "unknown"; - } - - char *res = malloc(sizeof(char)); - sprintf(res, "%s%s", opstr, nbstr); - - return res; -} - -void apply_lock(int index) { - int cmd; - - switch (index) { - case 0: - cmd = LOCK_SH; - break; - case 1: - // exclusive lock, non-blocking - cmd = LOCK_EX | LOCK_NB; - break; - case 2: - cmd = LOCK_SH; - break; - case 3: - opened_locks[index] = opened_locks[2]; - cmd = LOCK_SH; - break; - case 4: - cmd = LOCK_EX | LOCK_NB; - default: - break; - } - - // apply the lock - int fd = opened_locks[index].fd; - int res = flock(fd, cmd); - if (res < 0) { - int errnocpy = errno; - printf("\n\t-------------------------------\n"); - printf("\t Cannot apply lock on index %d\n\t (fd %d, error: %d)\n", - index, fd, errnocpy); - printf("\t flock return value: %d\n", res); - printf("\t-------------------------------\n\n"); - return; - } - - // save the lock in the global array - struct lock_info* flinfo = &(opened_locks[index]); - flinfo->operation = cmd; - - char *optstr = getOpStrFromInt(cmd); - printf("\n\t-------------------------------\n"); - printf("\t Lock applied to file %s, fd: %d\n", flinfo->path, flinfo->fd); - printf("\t operation: %s\n", optstr); - printf("\t-------------------------------\n\n"); - - free(optstr); -} - -void flush_stdin() { - char c; - - while ((c = getchar()) != '\n' && c != EOF) ; -} - -int main(int argc, char **argv) { - init(); - - open_file("/etc/passwd", O_RDONLY); - open_file("/etc/shadow", O_WRONLY); - open_file("/etc/hosts", O_RDWR); - - fgets(NULL, 0, stdin); - flush_stdin(); // flush the stream to delete all unmatched characters - apply_lock(0); - - fgets(NULL, 0, stdin); - flush_stdin(); - apply_lock(1); - - fgets(NULL, 0, stdin); - flush_stdin(); - apply_lock(2); - - fgets(NULL, 0, stdin); - flush_stdin(); - apply_lock(3); - - fgets(NULL, 0, stdin); - flush_stdin(); - open_file("write.lock", O_RDONLY); - apply_lock(4); - - fgets(NULL, 0, stdin); - flush_stdin(); - - deinit(); -} - diff --git a/demos/flockdemo/Makefile b/demos/flockdemo/Makefile deleted file mode 100644 index 10f652d..0000000 --- a/demos/flockdemo/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -all: flockdemo.c - gcc -g flockdemo.c -o executable diff --git a/demos/flockdemo/executable b/demos/flockdemo/executable deleted file mode 100755 index 3ef1fad9018d5fa52e803b952d68cba8953e6fb4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 19464 zcmeHPdu&_Rc|VsFC0BM|&qtA0RMG8^0>gOhd%x%JJLi1&T=E>=d-Rdfi7}6+2`*l7K#(-u>1e{r5FJ(w zNLUPtR-Eqj$r0QEPFFmUL zOjUP9D4Ura*cr+udb62aX|{KEaG-Z!XRw$L_RD@#eDXbXx~@9< z;4=Dqm(i!858!YYid`@M8R!FUXUJ=CY^tLd$Zwp|IUuGBnVg*z`DrUBOfxyGqY>les5MXCcU! zdd(#eb~w`WJxH3ZVLChh|C9UCK{d{e3^X#($Uq|ljSMt0(8xd|1OMML5S{yhKYGFU zy^s*mk6pGs<*U)TEB;q2X(;ddK1}5;H*oIQ5C%swtwzMn>t&QJ|4cGXk#Am=^52t8 zQ`ei9rTlTo$x8&Fvi*nS^RGV^o&SDx?#8WS$0siOJ_Bs@V*3tB_P+w3^6D{Q-s#vd zLYDoXh7i5zqd3t$x9!zv?%l+rxqA6}$A)th_fBZNQ0A=tOjG|F>zC7M*|7Zi`;nzXJM&UUX?8 zjnivB4YGEMaZGQ12*wk@AhCh@Cw~x~FJ1pCH4qJZ-Vov|#4mjNPPsfk`t!f0w0-p^ z5HF8|b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DZ6};^pqS0!aPQnzk#0eb6KrP<+EN`Xd2&%aTpCtx5~*@{=6^#K4tkk z=*i!mpYJGto&Y73|97Rn61KwpJM`3kcbuO>AEQW<*~;wR=I(g%66 zSX8m)^o(|7yQ5=sHRq9fSa&OR?I!Np2qIWKJB3LSs9kVqnmNodt-`bj=JK``j0}zU z+Od?9uzm?nmNM9mpGk-yN$FTIErN-&ImB})rc=@~gUNIT6YFZhgsos@V+2s#bk-I@ z+3_IQU@8yMwq|jraR;Zoyh=eUtwvfpfgmh7P-ni%XY-|BJ?1o~Nk@jUNj8mM*#&!2;=-VUZ3&+}8Jo7quOH+}^Ack!Dh*U$4bd z#`FA{DSvly``M4_R$_cek7M@QiN|@@R(pyo@gR8~|6pm@Q;^{E1@ZxzF{caK2?oYu~x1ZM^O!@bJ_ILOH^9s+`PXv^wqT7_n#Eqw)|EPY( zyZ;``J_ai4#=q>srvr{7Q<@J}7tR~&z7B@w>~s%N;dv?loffx0%S`_fm<}9_=lS}r zUW&Fj-W+LBX8gaxKx2>byx!u!gL7TApT{TT_&U=3o?IBu>#}PhC{eaLq;h6F)3;!$ z#*46W?Q^IiO1ZTC{|QSqp69cD3drr`09-H2KLJBJZhtuJSi%bL_E+g}oN&H_gLLk8 zGJd)55029UhWScRFeg<&cU-scUCZE~QNX|Aj;Y{o{HA5_2kE&3iq{2jiK1^A{181q zLK$)aT%y>g@a}$|A8_!W -#include -#include -#include -#include -#include -#include - -int main(int argc, char **argv) { - int fd = open("/proc/self", O_RDONLY); - int res = syscall(73, -23, LOCK_SH); - - printf("fd: %d, return value: %d\n", fd, res); - - return 0; -} diff --git a/demos/openerr/Makefile b/demos/openerr/Makefile deleted file mode 100644 index a67b545..0000000 --- a/demos/openerr/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -all: openerr.c - gcc -g openerr.c -o executable diff --git a/demos/openerr/executable b/demos/openerr/executable deleted file mode 100755 index e6cbdef9d7abb6d25b4dc9b0c56f41174ab580a9..0000000000000000000000000000000000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{tfVI z9eor03Ov<#KEf&w5KojE!FPlxUY&T2B-TiDNn* z#)V^IvXICcda{(w9)pQP(xGYAsLDq)TBYlI4#xJz_4vLa9s9&;Rg*#=x_w`4@5o?n ziM1gO6rRd@T$3KOp@Tvn9^F3>8`byk*>g*LOdpF4jK;~UwT*3}Sh5=ZpEs@<$%L6; zlgm5bqS2bIZ#rq&+uNX9yVp%m7V~;4kxSA(xRL$PN~Uvqsc4`(ms>(HfjnL;N;lSI zhXUiS3LEP zDp!h(m(tivnNErbNvT9JB_hdVIry{DEZ8N}MxmI_=V}BUWd$RXAOo2@nlVMh>Uad% z$W$JpY0ThAg9%D`EBFy3C5KNc2`4PsruI0pEjW)F64~?ws?VF`8I7Zp7(sAF1cRAo zi{Om$(iBV*xhaf%DW-FidCn)s#|y?ZOV~kba30~RcXj>O96fn)sp6h%C!Y5)x}U(L z>r7WepfxDl^Zbxebt)sz_cx|{k#XAdJd$w(D@t_R4}rgcH7VE6^G!y!x7JM1wHCb& zpZk!fbpzY;yp-`-sY~m8>O0%ha*q-%<7kS@_B?-Oe2|nJ$sNBVD5Etu+w(k^k>(wg zxc#ihcn`{G4bL*qhZ$8eMsl~`W`(#98Hy>}^SqhyWvR&ZyZir;v>%Xq{C5dPjuYAA zX}>6bPlBVE@ZVABQD`NJWaCWKR%a?haN4WVu8K}&5P_>LS}mRl9^Sd=kG#*`tNfEx6^IUGW~Ujd~y4Ee%B-IxgOS+;b2i*LXJ48Ugg<-spkj# z2_nP!sxdO7(xKb0TbJhlZjStXTN=6-cy8VGOW5B}4<;nKzMaW!-?M~$$7b7V)M<>o z+kU6CclR?X?P+{F6WcqHuRW(;bc-#`$bq{KBDv)A$@gWtzwo)YE$ri|?KTEn&beOJ TrR7OI`(N*{HHKXVE>Zj&w2rvF diff --git a/demos/openerr/openerr.c b/demos/openerr/openerr.c deleted file mode 100644 index 677c877..0000000 --- a/demos/openerr/openerr.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include - -int main(int argc, char **argv) { - const char path[] = "non-existent-file"; - int fd = open(path, O_RDONLY); - return fd; -} - diff --git a/demos/wrongfcntl/Makefile b/demos/wrongfcntl/Makefile deleted file mode 100644 index bb57bfd..0000000 --- a/demos/wrongfcntl/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -all: wrfcntl.c - gcc -g wrfcntl.c -o executable - diff --git a/demos/wrongfcntl/executable b/demos/wrongfcntl/executable deleted file mode 100755 index da61781e5237aaa6f23671f0d81dde01888a93c6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 19744 zcmeHPeQ;FQb-%l-)$T*mN+1~|AZ5Wop|*Af1|dEaRzeFakHCb01GVvEwfk1uvf5R5 z-wMgCYZBa~ii!i1#_=D{gf?x*ZQ3c#bUJP)6CkE|l4d-((=?=xGfKvu2-h9slsbw( z*#6Fa_lUPot90^Dn?LpryXT(Y`MCFj=)hjL%O$vY#76~j=b9`eq68=U zR0Sj=x0hgUFlG5%dqFZr3Ciy|dV&;CnA+{AD>Lm3my9gihVU6pPs96}%SB#fOh%Qv> zAXrjCxO@RT0X$eifA<3V%?ses1@I?<2k~*1`v6qx=hMK0PG#^N_^hdb3(QA?!rv)I zi)Lato)@F}bkT{rUiY);SQ5^*z~%Zl8nkp-FgtG46iKi{*&Fhl?7vgDa zdGoU5zfC-CeQ#cr{5OfGE#}P&lFx%rodYk+rvL1peC4s&p0snz4<#39)S%4ADMjOJF&^)<(Hx#`~(3y^Tc;t zzqc|r6}=pr65pT*#wK5S0YuDPyHhGnMn8zX6unM7=nX}u6`diPBKf-5*(2Adq91&N zjuq^{kPc>N_JdA|$>{B==^h|!(gBe?Q4u{Mmv9s+&KR&RLhU%aBJPc!3KJ=I? zHoe-oN(@ZCH!%5b-{jjprIsJW&b;V~ZGWx!Cat|kj`uv;bG+xV9vy86T8VOvmaR&F z)l!W>H3HQLR3lK0Ks5r@2vj3bjX*U5)d*B0@JmI&g{MGyqfpG4JA&(y;{G5W-sSSL zE?g@-JGA4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8-m?z<5tDPYJ^T3pIr|y~s?7v9QJ5X921`bwqEMWiDt(HAKKe87GD|RV+=Q!)> z;EVFg+=XrNY;UIy6{$GA-O|6I7Vc`WoAKfK -#include -#include -#include -#include -#include - -int main(int argc, char **argv) { - int fd = 999; - - if (argc > 1) { - fd = open(*argv, O_RDWR); - } - - struct flock lockinfo = { 9472, SEEK_SET, 0, 0 }; - int res = fcntl(fd, F_SETLK, &lockinfo); - int error = errno; - - printf("result: %d", res); - if (res < 0) { - printf(", error: %d", error); - } - - printf("\n"); - - return res; -} diff --git a/fixes.txt b/fixes.txt deleted file mode 100644 index 9fa2518..0000000 --- a/fixes.txt +++ /dev/null @@ -1,4 +0,0 @@ -in vuos: - in vunet/vunet_ioctl.c:34:10: error: 'SIOCGSTAMP' undeclared - starting from linux kernel v5.2-rc1 'SIOCGSTAMP' has been renamed to 'SIOCGSTAMP_OLD' - FIX: to access it again 'linux/sockios.h' needs to be included diff --git a/lxt-run.sh b/lxt-run.sh deleted file mode 100755 index cb18414..0000000 --- a/lxt-run.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -lxterminal --title="UMVU SHELL" --working-directory=$PWD/demos --geometry=140x30 --command="umvu bash" diff --git a/projbuild.sh b/projbuild.sh deleted file mode 100755 index f2c1d9c..0000000 --- a/projbuild.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -ROOTDIR=$PWD -DEFAULT_BUILDDIR=build - -if [ -z ${BUILDDIR:+x} ]; - then - echo Using default build directory; - BUILDDIR=$ROOTDIR/$DEFAULT_BUILDDIR - else - BUILDDIR=$ROOTDIR/$1 -fi - -rm -rf $BUILDDIR -mkdir -p $BUILDDIR -cd $BUILDDIR - -cmake $ROOTDIR -make -make install From c6626a045c8ea8ae22d3b93c20b3f36540a9309f Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Tue, 4 Aug 2020 16:22:29 +0200 Subject: [PATCH 12/18] Remove .gitignore --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 29da3a7..0000000 --- a/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -.gitignore -build/ -extra/ -*.swp From 95ce92cae07ec06d626ecfd198d7a32b786d8054 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Wed, 5 Aug 2020 17:36:52 +0200 Subject: [PATCH 13/18] Close fds opened from fcntl, minor fixes File descriptor opened from vu_vufs_fcntl are now correctly closed. Needs testing. vu_vufs_fcntl now uses the fsa to perform its operations. Minor fixes. --- vufs/vufs.c | 6 ++- vufs/vufs.h | 4 ++ vufs/vufsa.c | 4 +- vufs/vufsops.c | 109 ++++++++++++++++++++++++++----------------------- 4 files changed, 70 insertions(+), 53 deletions(-) diff --git a/vufs/vufs.c b/vufs/vufs.c index 30b9793..e7e6ec2 100644 --- a/vufs/vufs.c +++ b/vufs/vufs.c @@ -217,10 +217,12 @@ void *vu_vufs_init(void) { vu_syscall_handler(s, lseek) = lseek; vu_syscall_handler(s, pread64) = pread; vu_syscall_handler(s, pwrite64) = pwrite; + /* removed fcntl line since it is virtualized, is it needed? */ +#pragma GCC diagnostic pop - /* removed fcntl line since it is virtualized */ + for (int i = 0; i < MAX_OPEN_FILES; i++) + open_fds[i] = -1; -#pragma GCC diagnostic pop return NULL; } diff --git a/vufs/vufs.h b/vufs/vufs.h index b3718f4..88d48d7 100644 --- a/vufs/vufs.h +++ b/vufs/vufs.h @@ -5,6 +5,8 @@ VU_PROTOTYPES(vufs) +#define MAX_OPEN_FILES 1024 + #define O_UNLINK (O_PATH | O_EXCL) #define VUFS_TYPEMASK 0x7 @@ -32,4 +34,6 @@ struct vufs_fdprivate { char path[]; }; +int open_fds[MAX_OPEN_FILES]; + #endif diff --git a/vufs/vufsa.c b/vufs/vufsa.c index 7a57439..b01efdb 100644 --- a/vufs/vufsa.c +++ b/vufs/vufsa.c @@ -244,7 +244,9 @@ static vufsa_status vufsa_mincow(vufsa_status status, else return VUFSA_DOREAL; case VUFSA_DOREAL: - if (rv < 0 && errno == EACCES) + if (rv < 0 && (errno == EACCES || + errno == EAGAIN || + errno == EWOULDBLOCK )) return VUFSA_DOCOPY; else return VUFSA_FINAL; diff --git a/vufs/vufsops.c b/vufs/vufsops.c index c0c6e65..21c95af 100644 --- a/vufs/vufsops.c +++ b/vufs/vufsops.c @@ -672,25 +672,25 @@ int vu_vufs_chmod(const char *path, mode_t mode, int fd, void *fdprivate) { int vu_vufs_open(const char *pathname, int flags, mode_t mode, void **private) { struct vufs_t *vufs = vu_get_ht_private_data(); vufsa_status status = VUFSA_START; - int retval; + int retval; const char *filepath; mode_t oldmode = vu_mod_getmode(); - pathname += 1; + pathname += 1; if (flags == O_UNLINK) flags = O_PATH; //PATH+EXCL has the special meaning of UNLINK - vufsa_next vufsa_next = vufsa_select(vufs, flags); - while ((status = vufsa_next(status, vufs, pathname, retval)) != VUFSA_EXIT) { - switch (status) { - case VUFSA_DOREAL: + vufsa_next vufsa_next = vufsa_select(vufs, flags); + while ((status = vufsa_next(status, vufs, pathname, retval)) != VUFSA_EXIT) { + switch (status) { + case VUFSA_DOREAL: filepath = *pathname ? pathname : vufs->target; retval = openat(vufs->rdirfd, filepath, flags, mode); - break; - case VUFSA_DOVIRT: + break; + case VUFSA_DOVIRT: filepath = *pathname ? pathname : vufs->source; if (oldmode == 0) vufs_create_path(vufs->vdirfd, filepath, vufs_copyfile_create_path_cb, vufs); retval = openat(vufs->vdirfd, filepath, flags, mode); - break; - case VUFSA_DOCOPY: + break; + case VUFSA_DOCOPY: retval = vufs_copyfile(vufs, pathname, flags & O_TRUNC ? 0 : MAXSIZE); break; case VUFSA_FINAL: @@ -724,6 +724,11 @@ int vu_vufs_close(int fd, void *fdprivate) { struct vufs_t *vufs = vu_get_ht_private_data(); int retval; pthread_mutex_lock(&(vufs->mutex)); + if (open_fds[fd] != -1) { + // TODO: check correctness + close(open_fds[fd]); + open_fds[fd] = -1; + } retval = close(fd); if (retval == 0 && fdprivate != NULL) { struct vufs_fdprivate *vufs_fdprivate = fdprivate; @@ -737,6 +742,7 @@ int vu_vufs_close(int fd, void *fdprivate) { // RECORD LOCKING SYSCALLS int vu_vufs_fcntl(int fd, int cmd, ...) { + struct vufs_t *vufs = vu_get_ht_private_data(); int retval; va_list ap; @@ -749,55 +755,58 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { case F_OFD_SETLK: case F_OFD_SETLKW: case F_OFD_GETLK: ; - struct vufs_t *vufs = vu_get_ht_private_data(); - - /* retrieve variadic parameters */ - struct flock *lockinfo = va_arg(ap, struct flock*); - char *dest_path = va_arg(ap, char *); - - /* make a copy of the file in the virtual hierarchy */ - dest_path++; - retval = vufs_copyfile(vufs, dest_path, MAXSIZE); - - if (retval < 0) { - printkdebug(V, "Could not create virtual copy of file %s", dest_path); - errno = EBADF; - retval = -1; - } else { - int flags = O_RDWR; - - // if this is used, vu_fd_table.h must be included - // int flags = vu_fd_get_fdflags(fd, 0); - - // TODO: remember to close this fd when the original one is - int vfd = openat(vufs->vdirfd, dest_path, flags); - - if (vfd < 0) { - printkdebug(V, "Could not open virtual copy of %s", dest_path); - errno = EBADF; - retval = -1; - } else { - retval = fcntl(vfd, cmd, lockinfo); - printkdebug(V, "fcntl returned %d", retval); - } - } - break; + /* retrieve variadic parameters */ + struct flock *lockinfo = va_arg(ap, struct flock*); + char *dest_path = va_arg(ap, char *); + vufsa_status status = VUFSA_START; + // if this is used, vu_fd_table.h must be included + // int flags = vu_fd_get_fdflags(fd, 0); + int flags = O_RDWR; + int vfd = fd; + dest_path += 1; + + vufsa_next vufsa_next = vufsa_select(vufs, flags); + while ((status = vufsa_next(status, vufs, dest_path, retval)) != VUFSA_EXIT) { + switch (status) { + case VUFSA_DOREAL: + retval = fcntl(fd, cmd, lockinfo); + break; + case VUFSA_DOVIRT: + if (open_fds[fd] != -1) { + vfd = open_fds[fd]; + } else { + // TODO: check that this fd is effectively closed + vfd = openat(vufs->vdirfd, dest_path, flags); + open_fds[fd] = vfd; + } + retval = fcntl(vfd, cmd, lockinfo); + break; + case VUFSA_DOCOPY: + vufs_copyfile(vufs, dest_path, MAXSIZE); + break; + case VUFSA_ERR: + retval = -1; + case VUFSA_FINAL: + break; + } + } + break; case F_GETOWN_EX: case F_SETOWN_EX: - retval = fcntl(fd, cmd, va_arg(ap, struct f_owner_ex*)); - break; + retval = fcntl(fd, cmd, va_arg(ap, struct f_owner_ex*)); + break; case F_GET_RW_HINT: case F_SET_RW_HINT: case F_GET_FILE_RW_HINT: case F_SET_FILE_RW_HINT: - retval = fcntl(fd, cmd, va_arg(ap, uint64_t *)); - break; + retval = fcntl(fd, cmd, va_arg(ap, uint64_t *)); + break; default: - retval = fcntl(fd, cmd, va_arg(ap, int)); - break; + retval = fcntl(fd, cmd, va_arg(ap, int)); + break; } va_end(ap); @@ -833,7 +842,7 @@ int vu_vufs_flock(int fd, int operation, char *dest_path) { printkdebug(V, "fcntl returned %d", retval); } } - + return retval; } From 47971fbb3801dfb81825b521fb48e041f2085cf5 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Thu, 6 Aug 2020 09:30:47 +0200 Subject: [PATCH 14/18] Add implementation of flock --- vufs/vufsops.c | 53 +++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/vufs/vufsops.c b/vufs/vufsops.c index 21c95af..47c41c4 100644 --- a/vufs/vufsops.c +++ b/vufs/vufsops.c @@ -816,33 +816,38 @@ int vu_vufs_fcntl(int fd, int cmd, ...) { int vu_vufs_flock(int fd, int operation, char *dest_path) { struct vufs_t *vufs = vu_get_ht_private_data(); int retval; + vufsa_status status = VUFSA_START; + // if this is used, vu_fd_table.h must be included + // int flags = vu_fd_get_fdflags(fd, 0); + int flags = O_RDWR; + int vfd = fd; + dest_path += 1; - dest_path++; - retval = vufs_copyfile(vufs, dest_path, MAXSIZE); - - if (retval < 0) { - printkdebug(V, "Could not create virtual copy of file %s", dest_path); - errno = EBADF; - retval = -1; - } else { - int flags = O_RDWR; - - // if this is used, vu_fd_table.h must be included - // int flags = vu_fd_get_fdflags(fd, 0); - - // TODO: remember to close this fd when the original one is - int vfd = openat(vufs->vdirfd, dest_path, flags); - - if (vfd < 0) { - printkdebug(V, "Could not open virtual copy of %s", dest_path); - errno = EBADF; - retval = -1; - } else { - retval = flock(vfd, operation); - printkdebug(V, "fcntl returned %d", retval); + vufsa_next vufsa_next = vufsa_select(vufs, flags); + while ((status = vufsa_next(status, vufs, dest_path, retval)) != VUFSA_EXIT) { + switch (status) { + case VUFSA_DOREAL: + retval = flock(fd, operation); + break; + case VUFSA_DOVIRT: + if (open_fds[fd] != -1) { + vfd = open_fds[fd]; + } else { + // TODO: check that this fd is effectively closed + vfd = openat(vufs->vdirfd, dest_path, flags); + open_fds[fd] = vfd; + } + retval = flock(vfd, operation); + break; + case VUFSA_DOCOPY: + retval = vufs_copyfile(vufs, dest_path, MAXSIZE); + break; + case VUFSA_ERR: + retval = -1; + case VUFSA_FINAL: + break; } } return retval; } - From 0ffdc533c24dba01e1f0d13f27171774b3e1bbee Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Thu, 6 Aug 2020 09:36:39 +0200 Subject: [PATCH 15/18] Remove print statements from fcntl/flock wrap functions --- umvu/src/vu_wrap_file.c | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index 572211e..96d66a9 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -570,9 +570,6 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { * let VUFS manage this since it can create virtual representations * of the fs and apply locks on them * */ - printkdebug(F, "wi_fcntl for file locking: %d", cmd); - printkdebug(F, "the path of the file is %s", sd->extra->path); - uintptr_t flockaddr = sd->syscall_args[2]; void *lockinfo = malloc(sizeof(struct flock)); @@ -581,14 +578,10 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { * so process memory cannot be accessed directly * */ if (umvu_peek_data(flockaddr, lockinfo, sizeof(struct flock)) < 0) { - printkdebug(F, "cannot peek flock info"); free(lockinfo); break; - } else { - printkdebug(F, "successfully retrieved lockinfo"); } - //struct flock *lockinfo = (struct flock*) sd->syscall_args[2]; ret_value = service_syscall(ht, __VU_fcntl)(sfd, cmd, lockinfo, sd->extra->path); if (ret_value < 0) { sd->ret_value = -errno; @@ -600,9 +593,6 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { if (cmd == F_GETLK || cmd == F_OFD_GETLK) { /* as before, the tracee memory cannot be accessed directly */ if (umvu_poke_data(flockaddr, lockinfo, sizeof(struct flock) < 0)) { - printkdebug(F, "cannot poke flock info"); - } else { - printkdebug(F, "successfully written data to tracee memory"); } } @@ -708,9 +698,6 @@ void wi_flock(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { int ret_value; if (ht) { - printkdebug(F, "wi_flock for file locking: %d", op); - printkdebug(F, "the path of the file is %s", sd->extra->path); - ret_value = service_syscall(ht, __VU_flock)(fd, op, sd->extra->path); if (ret_value < 0) { sd->ret_value = -errno; From 52872da9f1462780f3db123fe5a2a81dee644e51 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Thu, 6 Aug 2020 15:29:31 +0200 Subject: [PATCH 16/18] Remove old fcntl virtualization leftovers --- include/vumodule.h | 2 -- umvu/src/vu_modutils.c | 3 +-- vufs/vufs.c | 2 -- vunet/vunet.c | 2 -- 4 files changed, 1 insertion(+), 8 deletions(-) diff --git a/include/vumodule.h b/include/vumodule.h index 023f8d9..f547f4d 100644 --- a/include/vumodule.h +++ b/include/vumodule.h @@ -35,8 +35,6 @@ struct vuht_entry_t; struct vu_module_t { char *name; char *description; - int mod_nr_vsyscalls; - char **vsyscalls; }; typedef unsigned long int syscall_arg_t; diff --git a/umvu/src/vu_modutils.c b/umvu/src/vu_modutils.c index 0809043..7dcc3fb 100644 --- a/umvu/src/vu_modutils.c +++ b/umvu/src/vu_modutils.c @@ -121,9 +121,8 @@ struct vu_service_t *module_load(const char *modname) #pragma GCC diagnostic ignored "-Wpedantic" if ((module = dlsym(handle, "vu_module"))) { #pragma GCC diagnostic pop - int VSYSCALL_NR = module->mod_nr_vsyscalls > 0 ? module->mod_nr_vsyscalls : 1; struct vu_service_t *service = malloc(sizeof(struct vu_service_t) + - (VU_NR_SYSCALLS + VSYSCALL_NR) * sizeof(syscall_t)); + VU_NR_SYSCALLS * sizeof(syscall_t)); int prefixlen = strlen(module->name) + 4; int fnamelen = prefixlen + VU_SYSCALL_MAX_NAMELEN + 1; char fname[fnamelen]; diff --git a/vufs/vufs.c b/vufs/vufs.c index e7e6ec2..2aa4875 100644 --- a/vufs/vufs.c +++ b/vufs/vufs.c @@ -230,8 +230,6 @@ int vu_vufs_fini(void *private) { return 0; } -char *vsyscalls[] = { [0] = "vufs_copyfile" }; - struct vu_module_t vu_module = { .name = "vufs", .description = "vu filesystem patchworking" diff --git a/vunet/vunet.c b/vunet/vunet.c index d5be8a2..40251d8 100644 --- a/vunet/vunet.c +++ b/vunet/vunet.c @@ -32,8 +32,6 @@ VU_PROTOTYPES(vunet) struct vu_module_t vu_module = { .name = "vunet", .description = "vu virtual networking", - .mod_nr_vsyscalls = 0, - .vsyscalls = NULL }; struct vunet_default_t { From 2816804c80a7d804d3b73c50686dec3fc43492ce Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Thu, 6 Aug 2020 15:35:29 +0200 Subject: [PATCH 17/18] Minor fixes --- umvu/include/service.h | 3 +-- umvu/src/vu_wrap_file.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/umvu/include/service.h b/umvu/include/service.h index f35b68a..46f4684 100644 --- a/umvu/include/service.h +++ b/umvu/include/service.h @@ -3,7 +3,6 @@ #include #include -#include /* each module define a service. services are registered in the hashtable (he key is the module name */ @@ -24,7 +23,7 @@ struct vu_service_t { struct vuht_entry_t *service_ht; // private data of the module (modules can use this pointer as they please. void *private; - // table of vu_syscalls implementation + visrtual syscalls added by the module + // table of vu_syscalls implementation syscall_t module_syscall[]; }; diff --git a/umvu/src/vu_wrap_file.c b/umvu/src/vu_wrap_file.c index 96d66a9..7e8b867 100644 --- a/umvu/src/vu_wrap_file.c +++ b/umvu/src/vu_wrap_file.c @@ -563,7 +563,7 @@ void wi_fcntl(struct vuht_entry_t *ht, struct syscall_descriptor_t *sd) { case F_SETLKW: case F_OFD_GETLK: case F_OFD_SETLK: - case F_OFD_SETLKW: + case F_OFD_SETLKW: ; /* * perform the SC on the VUFS virtualized file * using the real SC From bdeae1b559554d680ba407715859ebc363119224 Mon Sep 17 00:00:00 2001 From: Stefano Andriolo Date: Thu, 6 Aug 2020 15:44:28 +0200 Subject: [PATCH 18/18] Minor fix --- vunet/vunet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vunet/vunet.c b/vunet/vunet.c index 40251d8..96a7098 100644 --- a/vunet/vunet.c +++ b/vunet/vunet.c @@ -31,7 +31,7 @@ VU_PROTOTYPES(vunet) struct vu_module_t vu_module = { .name = "vunet", - .description = "vu virtual networking", + .description = "vu virtual networking" }; struct vunet_default_t {