@@ -16,6 +16,9 @@ extern int krsi_fs_initialized;
struct krsi_bprm_ctx {
struct linux_binprm *bprm;
+ char *arg_pages;
+ unsigned long num_arg_pages;
+ unsigned long max_arg_offset;
};
/*
@@ -4,6 +4,8 @@
#include <linux/filter.h>
#include <linux/bpf.h>
#include <linux/binfmts.h>
+#include <linux/highmem.h>
+#include <linux/mm.h>
#include "krsi_init.h"
@@ -17,6 +19,53 @@ struct krsi_hook krsi_hooks_list[] = {
#undef KRSI_HOOK_INIT
};
+static int pin_arg_pages(struct krsi_bprm_ctx *ctx)
+{
+ int ret = 0;
+ char *kaddr;
+ struct page *page;
+ unsigned long i, pos, num_arg_pages;
+ struct linux_binprm *bprm = ctx->bprm;
+ char *buf;
+
+ /*
+ * The bprm->vma_pages does not have the correct count
+ * for execution that is done by a kernel thread using the UMH.
+ * vm_pages is updated in acct_arg_size and bails
+ * out if current->mm is NULL (which is the case for a kernel thread).
+ * It's safer to use vma_pages(struct linux_binprm*) to get the
+ * actual number
+ */
+ num_arg_pages = vma_pages(bprm->vma);
+ if (!num_arg_pages)
+ return -ENOMEM;
+
+ buf = kmalloc_array(num_arg_pages, PAGE_SIZE, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ for (i = 0; i < num_arg_pages; i++) {
+ pos = ALIGN_DOWN(bprm->p, PAGE_SIZE) + i * PAGE_SIZE;
+ ret = get_user_pages_remote(current, bprm->mm, pos, 1,
+ FOLL_FORCE, &page, NULL, NULL);
+ if (ret <= 0) {
+ kfree(buf);
+ return -ENOMEM;
+ }
+
+ kaddr = kmap(page);
+ memcpy(buf + i * PAGE_SIZE, kaddr, PAGE_SIZE);
+ kunmap(page);
+ put_page(page);
+ }
+
+ ctx->arg_pages = buf;
+ ctx->num_arg_pages = num_arg_pages;
+ ctx->max_arg_offset = num_arg_pages * PAGE_SIZE;
+
+ return 0;
+}
+
static int krsi_process_execution(struct linux_binprm *bprm)
{
int ret;
@@ -26,7 +75,14 @@ static int krsi_process_execution(struct linux_binprm *bprm)
.bprm = bprm,
};
+ ret = pin_arg_pages(&ctx.bprm_ctx);
+ if (ret < 0)
+ goto out_arg_pages;
+
ret = krsi_run_progs(PROCESS_EXECUTION, &ctx);
+ kfree(ctx.bprm_ctx.arg_pages);
+
+out_arg_pages:
return ret;
}