美文网首页
一个3.0内核下可用的LSM中使用securityfs的示例

一个3.0内核下可用的LSM中使用securityfs的示例

作者: sxs7 | 来源:发表于2020-10-26 22:40 被阅读0次
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/security.h>

static ssize_t test_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos)
{
    char *data;
    int error;
    if (!count || count >= 1024)
        return -ENOMEM;
    data = vmalloc(count + 1);
    if (!data)
        return -ENOMEM;
    memset(data, 0, count);
    if (copy_from_user(data, buf, count)) {
        error = -EFAULT;
        goto out;
    }
    
    pr_err("write : <%s>.\n", data);

/* handling kaddr */

out:
    vfree(data);
    return error ? error : count;
}

static ssize_t test_read(struct file *file, char __user *buf,size_t count, loff_t *ppos)
{
    char kaddr[] = "abc123";
    loff_t pos = *ppos;
    loff_t len = 7;/* strlen(kernel strings need to be copied) */;

    if (pos >= len || !count)
        return 0;
    len -= pos;
    if (count < len)
        len = count;

/* handling */

    if (copy_to_user(buf, kaddr, len))
        return -EFAULT;
    *ppos += len;
    return len;
}
static const struct file_operations test_measurements_file_ops = {
        .write = test_write,
        .read = test_read,
};

struct dentry * test_dir;
struct dentry * basc_output_file;

static int tst_bprm_check_security (struct linux_binprm *bprm)
{
    printk("fuck 222\n");

    if (test_dir == NULL) {
        pr_err("fuck +++++++++++++++++test_dir NULL.\n");
        test_dir = securityfs_create_dir("fuck", NULL);
    }
    
    if (basc_output_file == NULL) {
        pr_err("fuck +++++++ file NULL.\n");
        basc_output_file = securityfs_create_file("fuck.conf",S_IRUSR | S_IWUSR, test_dir, NULL, &test_measurements_file_ops);
    }       
    
    return 0;
}

static struct security_operations test_security_ops = {
        .bprm_check_security  =  tst_bprm_check_security,
};


static __init int tst_init(void)
{
     if (register_security(&test_security_ops))
                panic("fuck Test: kernel registration failed.\n");

    pr_err("fuck \n");

    return 0;
}


security_initcall(tst_init);

相关文章

网友评论

      本文标题:一个3.0内核下可用的LSM中使用securityfs的示例

      本文链接:https://www.haomeiwen.com/subject/xeozmktx.html