securityfs_if.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * security/tomoyo/common.c
  3. *
  4. * Securityfs interface for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include <linux/security.h>
  9. #include "common.h"
  10. /**
  11. * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
  12. *
  13. * @inode: Pointer to "struct inode".
  14. * @file: Pointer to "struct file".
  15. *
  16. * Returns 0 on success, negative value otherwise.
  17. */
  18. static int tomoyo_open(struct inode *inode, struct file *file)
  19. {
  20. const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
  21. - ((u8 *) NULL);
  22. return tomoyo_open_control(key, file);
  23. }
  24. /**
  25. * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
  26. *
  27. * @inode: Pointer to "struct inode".
  28. * @file: Pointer to "struct file".
  29. *
  30. * Returns 0 on success, negative value otherwise.
  31. */
  32. static int tomoyo_release(struct inode *inode, struct file *file)
  33. {
  34. return tomoyo_close_control(file);
  35. }
  36. /**
  37. * tomoyo_poll - poll() for /proc/ccs/ interface.
  38. *
  39. * @file: Pointer to "struct file".
  40. * @wait: Pointer to "poll_table".
  41. *
  42. * Returns 0 on success, negative value otherwise.
  43. */
  44. static unsigned int tomoyo_poll(struct file *file, poll_table *wait)
  45. {
  46. return tomoyo_poll_control(file, wait);
  47. }
  48. /**
  49. * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
  50. *
  51. * @file: Pointer to "struct file".
  52. * @buf: Pointer to buffer.
  53. * @count: Size of @buf.
  54. * @ppos: Unused.
  55. *
  56. * Returns bytes read on success, negative value otherwise.
  57. */
  58. static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
  59. loff_t *ppos)
  60. {
  61. return tomoyo_read_control(file, buf, count);
  62. }
  63. /**
  64. * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
  65. *
  66. * @file: Pointer to "struct file".
  67. * @buf: Pointer to buffer.
  68. * @count: Size of @buf.
  69. * @ppos: Unused.
  70. *
  71. * Returns @count on success, negative value otherwise.
  72. */
  73. static ssize_t tomoyo_write(struct file *file, const char __user *buf,
  74. size_t count, loff_t *ppos)
  75. {
  76. return tomoyo_write_control(file, buf, count);
  77. }
  78. /*
  79. * tomoyo_operations is a "struct file_operations" which is used for handling
  80. * /sys/kernel/security/tomoyo/ interface.
  81. *
  82. * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
  83. * See tomoyo_io_buffer for internals.
  84. */
  85. static const struct file_operations tomoyo_operations = {
  86. .open = tomoyo_open,
  87. .release = tomoyo_release,
  88. .poll = tomoyo_poll,
  89. .read = tomoyo_read,
  90. .write = tomoyo_write,
  91. .llseek = noop_llseek,
  92. };
  93. /**
  94. * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
  95. *
  96. * @name: The name of the interface file.
  97. * @mode: The permission of the interface file.
  98. * @parent: The parent directory.
  99. * @key: Type of interface.
  100. *
  101. * Returns nothing.
  102. */
  103. static void __init tomoyo_create_entry(const char *name, const mode_t mode,
  104. struct dentry *parent, const u8 key)
  105. {
  106. securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
  107. &tomoyo_operations);
  108. }
  109. /**
  110. * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
  111. *
  112. * Returns 0.
  113. */
  114. static int __init tomoyo_initerface_init(void)
  115. {
  116. struct dentry *tomoyo_dir;
  117. /* Don't create securityfs entries unless registered. */
  118. if (current_cred()->security != &tomoyo_kernel_domain)
  119. return 0;
  120. tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
  121. tomoyo_create_entry("query", 0600, tomoyo_dir,
  122. TOMOYO_QUERY);
  123. tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
  124. TOMOYO_DOMAINPOLICY);
  125. tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
  126. TOMOYO_EXCEPTIONPOLICY);
  127. tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
  128. TOMOYO_SELFDOMAIN);
  129. tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
  130. TOMOYO_DOMAIN_STATUS);
  131. tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
  132. TOMOYO_PROCESS_STATUS);
  133. tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
  134. TOMOYO_MEMINFO);
  135. tomoyo_create_entry("profile", 0600, tomoyo_dir,
  136. TOMOYO_PROFILE);
  137. tomoyo_create_entry("manager", 0600, tomoyo_dir,
  138. TOMOYO_MANAGER);
  139. tomoyo_create_entry("version", 0400, tomoyo_dir,
  140. TOMOYO_VERSION);
  141. return 0;
  142. }
  143. fs_initcall(tomoyo_initerface_init);