securityfs_if.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_check_task_acl - Check permission for task operation.
  10. *
  11. * @r: Pointer to "struct tomoyo_request_info".
  12. * @ptr: Pointer to "struct tomoyo_acl_info".
  13. *
  14. * Returns true if granted, false otherwise.
  15. */
  16. static bool tomoyo_check_task_acl(struct tomoyo_request_info *r,
  17. const struct tomoyo_acl_info *ptr)
  18. {
  19. const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl),
  20. head);
  21. return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
  22. }
  23. /**
  24. * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
  25. *
  26. * @file: Pointer to "struct file".
  27. * @buf: Domainname to transit to.
  28. * @count: Size of @buf.
  29. * @ppos: Unused.
  30. *
  31. * Returns @count on success, negative value otherwise.
  32. *
  33. * If domain transition was permitted but the domain transition failed, this
  34. * function returns error rather than terminating current thread with SIGKILL.
  35. */
  36. static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
  37. size_t count, loff_t *ppos)
  38. {
  39. char *data;
  40. int error;
  41. if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
  42. return -ENOMEM;
  43. data = memdup_user_nul(buf, count);
  44. if (IS_ERR(data))
  45. return PTR_ERR(data);
  46. tomoyo_normalize_line(data);
  47. if (tomoyo_correct_domain(data)) {
  48. const int idx = tomoyo_read_lock();
  49. struct tomoyo_path_info name;
  50. struct tomoyo_request_info r;
  51. name.name = data;
  52. tomoyo_fill_path_info(&name);
  53. /* Check "task manual_domain_transition" permission. */
  54. tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  55. r.param_type = TOMOYO_TYPE_MANUAL_TASK_ACL;
  56. r.param.task.domainname = &name;
  57. tomoyo_check_acl(&r, tomoyo_check_task_acl);
  58. if (!r.granted)
  59. error = -EPERM;
  60. else {
  61. struct tomoyo_domain_info *new_domain =
  62. tomoyo_assign_domain(data, true);
  63. if (!new_domain) {
  64. error = -ENOENT;
  65. } else {
  66. struct cred *cred = prepare_creds();
  67. if (!cred) {
  68. error = -ENOMEM;
  69. } else {
  70. struct tomoyo_domain_info *old_domain =
  71. cred->security;
  72. cred->security = new_domain;
  73. atomic_inc(&new_domain->users);
  74. atomic_dec(&old_domain->users);
  75. commit_creds(cred);
  76. error = 0;
  77. }
  78. }
  79. }
  80. tomoyo_read_unlock(idx);
  81. } else
  82. error = -EINVAL;
  83. kfree(data);
  84. return error ? error : count;
  85. }
  86. /**
  87. * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
  88. *
  89. * @file: Pointer to "struct file".
  90. * @buf: Domainname which current thread belongs to.
  91. * @count: Size of @buf.
  92. * @ppos: Bytes read by now.
  93. *
  94. * Returns read size on success, negative value otherwise.
  95. */
  96. static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
  97. size_t count, loff_t *ppos)
  98. {
  99. const char *domain = tomoyo_domain()->domainname->name;
  100. loff_t len = strlen(domain);
  101. loff_t pos = *ppos;
  102. if (pos >= len || !count)
  103. return 0;
  104. len -= pos;
  105. if (count < len)
  106. len = count;
  107. if (copy_to_user(buf, domain + pos, len))
  108. return -EFAULT;
  109. *ppos += len;
  110. return len;
  111. }
  112. /* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
  113. static const struct file_operations tomoyo_self_operations = {
  114. .write = tomoyo_write_self,
  115. .read = tomoyo_read_self,
  116. };
  117. /**
  118. * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
  119. *
  120. * @inode: Pointer to "struct inode".
  121. * @file: Pointer to "struct file".
  122. *
  123. * Returns 0 on success, negative value otherwise.
  124. */
  125. static int tomoyo_open(struct inode *inode, struct file *file)
  126. {
  127. const int key = ((u8 *) file_inode(file)->i_private)
  128. - ((u8 *) NULL);
  129. return tomoyo_open_control(key, file);
  130. }
  131. /**
  132. * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
  133. *
  134. * @file: Pointer to "struct file".
  135. *
  136. */
  137. static int tomoyo_release(struct inode *inode, struct file *file)
  138. {
  139. tomoyo_close_control(file->private_data);
  140. return 0;
  141. }
  142. /**
  143. * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
  144. *
  145. * @file: Pointer to "struct file".
  146. * @wait: Pointer to "poll_table". Maybe NULL.
  147. *
  148. * Returns POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM if ready to read/write,
  149. * POLLOUT | POLLWRNORM otherwise.
  150. */
  151. static unsigned int tomoyo_poll(struct file *file, poll_table *wait)
  152. {
  153. return tomoyo_poll_control(file, wait);
  154. }
  155. /**
  156. * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
  157. *
  158. * @file: Pointer to "struct file".
  159. * @buf: Pointer to buffer.
  160. * @count: Size of @buf.
  161. * @ppos: Unused.
  162. *
  163. * Returns bytes read on success, negative value otherwise.
  164. */
  165. static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
  166. loff_t *ppos)
  167. {
  168. return tomoyo_read_control(file->private_data, buf, count);
  169. }
  170. /**
  171. * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
  172. *
  173. * @file: Pointer to "struct file".
  174. * @buf: Pointer to buffer.
  175. * @count: Size of @buf.
  176. * @ppos: Unused.
  177. *
  178. * Returns @count on success, negative value otherwise.
  179. */
  180. static ssize_t tomoyo_write(struct file *file, const char __user *buf,
  181. size_t count, loff_t *ppos)
  182. {
  183. return tomoyo_write_control(file->private_data, buf, count);
  184. }
  185. /*
  186. * tomoyo_operations is a "struct file_operations" which is used for handling
  187. * /sys/kernel/security/tomoyo/ interface.
  188. *
  189. * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
  190. * See tomoyo_io_buffer for internals.
  191. */
  192. static const struct file_operations tomoyo_operations = {
  193. .open = tomoyo_open,
  194. .release = tomoyo_release,
  195. .poll = tomoyo_poll,
  196. .read = tomoyo_read,
  197. .write = tomoyo_write,
  198. .llseek = noop_llseek,
  199. };
  200. /**
  201. * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
  202. *
  203. * @name: The name of the interface file.
  204. * @mode: The permission of the interface file.
  205. * @parent: The parent directory.
  206. * @key: Type of interface.
  207. *
  208. * Returns nothing.
  209. */
  210. static void __init tomoyo_create_entry(const char *name, const umode_t mode,
  211. struct dentry *parent, const u8 key)
  212. {
  213. securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
  214. &tomoyo_operations);
  215. }
  216. /**
  217. * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
  218. *
  219. * Returns 0.
  220. */
  221. static int __init tomoyo_initerface_init(void)
  222. {
  223. struct dentry *tomoyo_dir;
  224. /* Don't create securityfs entries unless registered. */
  225. if (current_cred()->security != &tomoyo_kernel_domain)
  226. return 0;
  227. tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
  228. tomoyo_create_entry("query", 0600, tomoyo_dir,
  229. TOMOYO_QUERY);
  230. tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
  231. TOMOYO_DOMAINPOLICY);
  232. tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
  233. TOMOYO_EXCEPTIONPOLICY);
  234. tomoyo_create_entry("audit", 0400, tomoyo_dir,
  235. TOMOYO_AUDIT);
  236. tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
  237. TOMOYO_PROCESS_STATUS);
  238. tomoyo_create_entry("stat", 0644, tomoyo_dir,
  239. TOMOYO_STAT);
  240. tomoyo_create_entry("profile", 0600, tomoyo_dir,
  241. TOMOYO_PROFILE);
  242. tomoyo_create_entry("manager", 0600, tomoyo_dir,
  243. TOMOYO_MANAGER);
  244. tomoyo_create_entry("version", 0400, tomoyo_dir,
  245. TOMOYO_VERSION);
  246. securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
  247. &tomoyo_self_operations);
  248. tomoyo_load_builtin_policy();
  249. return 0;
  250. }
  251. fs_initcall(tomoyo_initerface_init);