blob: d84151e2c0d09f9fcdc429bac15a5e91633c8235 [file] [log] [blame]
/*
* linux/fs/proc/proc_avc.c
*
*/
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/syslog.h>
#include <linux/bootmem.h>
#include <linux/export.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/slab.h>
#ifdef CONFIG_PROC_AVC
#include <linux/proc_avc.h>
#endif
#define LOG_MAGIC 0x4d474f4c /* "LOGM" */
static unsigned int *sec_avc_log_ptr;
static char *sec_avc_log_buf;
static unsigned int sec_avc_log_size;
int __init sec_avc_log_init(void)
{
unsigned int size = SZ_256K;
unsigned int *sec_avc_log_mag;
sec_avc_log_size = size;
sec_avc_log_mag = kzalloc(sec_avc_log_size, GFP_NOWAIT);
pr_info("allocating %u bytes at %p (%llx physical) for avc log\n",
sec_avc_log_size, sec_avc_log_mag, __pa(sec_avc_log_buf));
sec_avc_log_ptr = sec_avc_log_mag + 4;
sec_avc_log_buf = (char *)(sec_avc_log_mag + 8);
if (*sec_avc_log_mag != LOG_MAGIC) {
pr_info("%s: no old log found\n", __func__);
*sec_avc_log_ptr = 0;
*sec_avc_log_mag = LOG_MAGIC;
}
return 1;
}
#define BUF_SIZE 512
void sec_avc_log(char *fmt, ...)
{
va_list args;
char buf[BUF_SIZE];
int len = 0;
unsigned int idx;
unsigned int size;
/* In case of sec_avc_log_setup is failed */
if (!sec_avc_log_size)
return;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
idx = *sec_avc_log_ptr;
size = strlen(buf);
if (idx + (size * 2) > sec_avc_log_size - 1) {
len = scnprintf(&sec_avc_log_buf[0], size + 1, "%s\n", buf);
*sec_avc_log_ptr = len;
} else {
len = scnprintf(&sec_avc_log_buf[idx], size + 1, "%s\n", buf);
*sec_avc_log_ptr += len;
}
}
static ssize_t sec_avc_log_write(struct file *file,
const char __user *buf,
size_t count, loff_t *ppos)
{
char *page = NULL;
ssize_t ret;
int new_value;
if (!sec_avc_log_buf)
return 0;
ret = -ENOMEM;
if (count >= PAGE_SIZE)
return ret;
ret = -ENOMEM;
page = (char *)get_zeroed_page(GFP_KERNEL);
if (!page)
return ret;
ret = -EFAULT;
if (copy_from_user(page, buf, count))
goto out;
ret = -EINVAL;
if (sscanf(page, "%u", &new_value) != 1) {
pr_info("%s\n", page);
/* print avc_log to sec_avc_log_buf */
sec_avc_log("%s", page);
}
ret = count;
out:
free_page((unsigned long)page);
return ret;
}
static ssize_t sec_avc_log_read(struct file *file, char __user *buf,
size_t len, loff_t *offset)
{
loff_t pos = *offset;
ssize_t count;
if (pos >= (*sec_avc_log_ptr & (sec_avc_log_size - 1)))
return 0;
count = min(len,
(size_t)((*sec_avc_log_ptr & (sec_avc_log_size - 1)) - pos));
if (copy_to_user(buf, sec_avc_log_buf + pos, count))
return -EFAULT;
*offset += count;
return count;
}
static const struct file_operations avc_msg_file_ops = {
.owner = THIS_MODULE,
.read = sec_avc_log_read,
.write = sec_avc_log_write,
.llseek = generic_file_llseek,
};
static int __init sec_avc_log_late_init(void)
{
struct proc_dir_entry *entry;
if (sec_avc_log_buf == NULL) {
pr_err("%s: sec_avc_log_buf not initialized.\n", __func__);
return 0;
}
entry = proc_create_data("avc_msg", S_IFREG | 0x444, NULL, &avc_msg_file_ops, NULL);
if (!entry) {
pr_err("%s: failed to create proc entry\n", __func__);
return 0;
}
proc_set_size(entry, sec_avc_log_size);
return 0;
}
late_initcall(sec_avc_log_late_init);
int __init sec_log_init(void)
{
#ifdef CONFIG_PROC_AVC
sec_avc_log_init();
#endif
return 0;
}
fs_initcall(sec_log_init);