securityfs_if.c 4.2 KB

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