#include <linux/mman.h>
#include <linux/err.h>
#include <linux/types.h>
-#include <kprobe/swap_kprobes.h>
-#include <kprobe/swap_kprobes_deps.h>
#include <us_manager/sspt/sspt_proc.h>
#include <us_manager/sspt/sspt_ip.h>
#include <us_manager/callbacks.h>
+#include <us_manager/usm_hook.h>
#include <writer/kernel_operations.h>
#include <master/swap_initializer.h>
#include "loader_defs.h"
-struct mmap_priv {
- struct dentry *dentry;
-};
-
-static inline bool check_prot(unsigned long prot)
-{
- return !!((prot & PROT_READ) && (prot & PROT_EXEC));
-}
-
-static int mmap_entry_handler(struct kretprobe_instance *ri,
- struct pt_regs *regs)
-{
- struct file *file = (struct file *)swap_get_karg(regs, 0);
- unsigned long prot = swap_get_karg(regs, 3);
- struct mmap_priv *priv = (struct mmap_priv *)ri->data;
- struct task_struct *task = current->group_leader;
- struct dentry *dentry, *loader_dentry;
- struct pd_t *pd;
- struct hd_t *hd;
- struct sspt_proc *proc;
-
- priv->dentry = NULL;
- if (!check_prot(prot))
- return 0;
-
- if (!file)
- return 0;
-
- dentry = file->f_path.dentry;
- if (dentry == NULL)
- return 0;
-
- loader_dentry = ld_get_loader_dentry();
- if (dentry == loader_dentry) {
- priv->dentry = loader_dentry;
- return 0;
- }
-
- proc = sspt_proc_get_by_task(task);
- if (!proc)
- return 0;
-
- pd = lpd_get(proc);
- if (pd == NULL) {
- printk(LOADER_PREFIX "%d: No process data! Current %d %s\n",
- __LINE__, current->tgid, current->comm);
- return 0;
- }
-
- hd = lpd_get_hd(pd, dentry);
- if (hd != NULL)
- priv->dentry = lpd_get_dentry(hd);
-
- return 0;
-}
-
-static int mmap_ret_handler(struct kretprobe_instance *ri,
- struct pt_regs *regs)
+static void mmap_handler(struct sspt_proc *proc, struct vm_area_struct *vma)
{
- struct mmap_priv *priv = (struct mmap_priv *)ri->data;
- struct task_struct *task = current->group_leader;
struct pd_t *pd;
- struct hd_t *hd;
- struct sspt_proc *proc;
- struct dentry *loader_dentry;
- unsigned long vaddr;
-
- if (!task->mm)
- return 0;
-
- if (priv->dentry == NULL)
- return 0;
-
- vaddr = (unsigned long)regs_return_value(regs);
- if (IS_ERR_VALUE(vaddr))
- return 0;
-
- proc = sspt_proc_get_by_task(task);
- if (!proc)
- return 0;
+ unsigned long vaddr = vma->vm_start;
+ struct dentry *dentry = vma->vm_file->f_path.dentry;
pd = lpd_get(proc);
if (pd == NULL) {
printk(LOADER_PREFIX "%d: No process data! Current %d %s\n",
__LINE__, current->tgid, current->comm);
- return 0;
+ return;
}
- loader_dentry = ld_get_loader_dentry();
- if (priv->dentry == loader_dentry)
+ if (dentry == ld_get_loader_dentry()) {
lpd_set_loader_base(pd, vaddr);
+ } else {
+ struct hd_t *hd;
-
- hd = lpd_get_hd(pd, priv->dentry);
- if (hd != NULL)
- lpd_set_handlers_base(hd, vaddr);
-
- return 0;
+ hd = lpd_get_hd(pd, dentry);
+ if (hd)
+ lpd_set_handlers_base(hd, vaddr);
+ }
}
-static struct kretprobe mmap_rp = {
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
- .kp.symbol_name = "do_mmap",
-#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)) */
- .kp.symbol_name = "do_mmap_pgoff",
-#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)) */
- .data_size = sizeof(struct mmap_priv),
- .entry_handler = mmap_entry_handler,
- .handler = mmap_ret_handler
+
+static struct usm_hook usm_hook = {
+ .owner = THIS_MODULE,
+ .mmap = mmap_handler,
};
static bool mmap_rp_inst = false;
int res;
mutex_lock(&mmap_rp_mtx);
- res = swap_register_kretprobe(&mmap_rp);
+ res = usm_hook_reg(&usm_hook);
if (res != 0)
- printk(KERN_ERR LOADER_PREFIX "Registering do_mmap_pgoff probe failed\n");
+ pr_err(LOADER_PREFIX "Cannot register usm_hook\n");
else
mmap_rp_inst = true;
mutex_unlock(&mmap_rp_mtx);
{
mutex_lock(&mmap_rp_mtx);
if (mmap_rp_inst) {
- swap_unregister_kretprobe(&mmap_rp);
+ usm_hook_unreg(&usm_hook);
mmap_rp_inst = false;
}
mutex_unlock(&mmap_rp_mtx);
{
mutex_lock(&mmap_rp_mtx);
if (mmap_rp_inst) {
- swap_unregister_kretprobe(&mmap_rp);
+ usm_hook_unreg(&usm_hook);
mmap_rp_inst = false;
}
mutex_unlock(&mmap_rp_mtx);