proc_avc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * linux/fs/proc/proc_avc.c
  3. *
  4. */
  5. #include <linux/types.h>
  6. #include <linux/errno.h>
  7. #include <linux/time.h>
  8. #include <linux/kernel.h>
  9. #include <linux/poll.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/fs.h>
  12. #include <linux/syslog.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/export.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/io.h>
  17. #ifdef CONFIG_SEC_DEBUG
  18. #include <mach/sec_debug.h>
  19. #endif
  20. #define LOG_MAGIC 0x4d474f4c /* "LOGM" */
  21. static unsigned *sec_avc_log_ptr;
  22. static char *sec_avc_log_buf;
  23. static unsigned sec_avc_log_size;
  24. int __init sec_avc_log_init(void)
  25. {
  26. unsigned size = SZ_256K;
  27. unsigned *sec_avc_log_mag;
  28. sec_avc_log_size = size + 8;
  29. sec_avc_log_mag = alloc_bootmem(sec_avc_log_size);
  30. pr_info("allocating %u bytes at %p (%lx physical) for avc log\n",
  31. sec_avc_log_size, sec_avc_log_mag, __pa(sec_avc_log_buf));
  32. sec_avc_log_ptr = sec_avc_log_mag + 4;
  33. sec_avc_log_buf = (char *)(sec_avc_log_mag + 8);
  34. sec_avc_log_size = size;
  35. if (*sec_avc_log_mag != LOG_MAGIC) {
  36. pr_info("%s: no old log found\n", __func__);
  37. *sec_avc_log_ptr = 0;
  38. *sec_avc_log_mag = LOG_MAGIC;
  39. }
  40. return 1;
  41. }
  42. #define BUF_SIZE 512
  43. void sec_avc_log(char *fmt, ...)
  44. {
  45. va_list args;
  46. char buf[BUF_SIZE];
  47. int len = 0;
  48. unsigned int idx;
  49. unsigned int size;
  50. /* In case of sec_avc_log_setup is failed */
  51. if (!sec_avc_log_size)
  52. return;
  53. va_start(args, fmt);
  54. vsnprintf(buf, sizeof(buf), fmt, args);
  55. va_end(args);
  56. idx = *sec_avc_log_ptr;
  57. size = strlen(buf);
  58. if (idx + size > sec_avc_log_size - 1) {
  59. len = scnprintf(&sec_avc_log_buf[0], size + 1, "%s\n", buf);
  60. *sec_avc_log_ptr = len;
  61. } else {
  62. len = scnprintf(&sec_avc_log_buf[idx], size + 1, "%s\n", buf);
  63. *sec_avc_log_ptr += len;
  64. }
  65. }
  66. static ssize_t sec_avc_log_write(struct file *file,
  67. const char __user *buf,
  68. size_t count, loff_t *ppos)
  69. {
  70. char *page = NULL;
  71. ssize_t ret;
  72. int new_value;
  73. if (!sec_avc_log_buf)
  74. return 0;
  75. ret = -ENOMEM;
  76. if (count >= PAGE_SIZE)
  77. return ret;
  78. ret = -ENOMEM;
  79. page = (char *)get_zeroed_page(GFP_KERNEL);
  80. if (!page)
  81. return ret;;
  82. ret = -EFAULT;
  83. if (copy_from_user(page, buf, count))
  84. goto out;
  85. ret = -EINVAL;
  86. if (sscanf(page, "%u", &new_value) != 1) {
  87. pr_info("%s\n", page);
  88. /* print avc_log to sec_avc_log_buf */
  89. sec_avc_log("%s", page);
  90. }
  91. ret = count;
  92. out:
  93. free_page((unsigned long)page);
  94. return ret;
  95. }
  96. static ssize_t sec_avc_log_read(struct file *file, char __user *buf,
  97. size_t len, loff_t *offset)
  98. {
  99. loff_t pos = *offset;
  100. ssize_t count;
  101. if (pos >= (*sec_avc_log_ptr & (sec_avc_log_size - 1)))
  102. return 0;
  103. count = min(len,
  104. (size_t)((*sec_avc_log_ptr & (sec_avc_log_size - 1)) - pos));
  105. if (copy_to_user(buf, sec_avc_log_buf + pos, count))
  106. return -EFAULT;
  107. *offset += count;
  108. return count;
  109. }
  110. static const struct file_operations avc_msg_file_ops = {
  111. .owner = THIS_MODULE,
  112. .read = sec_avc_log_read,
  113. .write = sec_avc_log_write,
  114. .llseek = generic_file_llseek,
  115. };
  116. static int __init sec_avc_log_late_init(void)
  117. {
  118. struct proc_dir_entry *entry;
  119. if (sec_avc_log_buf == NULL) {
  120. pr_err("%s: sec_avc_log_buf not initialized.\n", __func__);
  121. return 0;
  122. }
  123. entry = create_proc_entry("avc_msg", S_IFREG | S_IRUGO, NULL);
  124. if (!entry) {
  125. pr_err("%s: failed to create proc entry\n", __func__);
  126. return 0;
  127. }
  128. entry->proc_fops = &avc_msg_file_ops;
  129. entry->size = sec_avc_log_size;
  130. return 0;
  131. }
  132. late_initcall(sec_avc_log_late_init);