securityfs_if.c 7.4 KB

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