securityfs_if.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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->f_path.dentry->d_inode->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. * @inode: Pointer to "struct inode".
  140. * @file: Pointer to "struct file".
  141. *
  142. * Returns 0 on success, negative value otherwise.
  143. */
  144. static int tomoyo_release(struct inode *inode, struct file *file)
  145. {
  146. return tomoyo_close_control(file->private_data);
  147. }
  148. /**
  149. * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
  150. *
  151. * @file: Pointer to "struct file".
  152. * @wait: Pointer to "poll_table". Maybe NULL.
  153. *
  154. * Returns POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM if ready to read/write,
  155. * POLLOUT | POLLWRNORM otherwise.
  156. */
  157. static unsigned int tomoyo_poll(struct file *file, poll_table *wait)
  158. {
  159. return tomoyo_poll_control(file, wait);
  160. }
  161. /**
  162. * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
  163. *
  164. * @file: Pointer to "struct file".
  165. * @buf: Pointer to buffer.
  166. * @count: Size of @buf.
  167. * @ppos: Unused.
  168. *
  169. * Returns bytes read on success, negative value otherwise.
  170. */
  171. static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
  172. loff_t *ppos)
  173. {
  174. return tomoyo_read_control(file->private_data, buf, count);
  175. }
  176. /**
  177. * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
  178. *
  179. * @file: Pointer to "struct file".
  180. * @buf: Pointer to buffer.
  181. * @count: Size of @buf.
  182. * @ppos: Unused.
  183. *
  184. * Returns @count on success, negative value otherwise.
  185. */
  186. static ssize_t tomoyo_write(struct file *file, const char __user *buf,
  187. size_t count, loff_t *ppos)
  188. {
  189. return tomoyo_write_control(file->private_data, buf, count);
  190. }
  191. /*
  192. * tomoyo_operations is a "struct file_operations" which is used for handling
  193. * /sys/kernel/security/tomoyo/ interface.
  194. *
  195. * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
  196. * See tomoyo_io_buffer for internals.
  197. */
  198. static const struct file_operations tomoyo_operations = {
  199. .open = tomoyo_open,
  200. .release = tomoyo_release,
  201. .poll = tomoyo_poll,
  202. .read = tomoyo_read,
  203. .write = tomoyo_write,
  204. .llseek = noop_llseek,
  205. };
  206. /**
  207. * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
  208. *
  209. * @name: The name of the interface file.
  210. * @mode: The permission of the interface file.
  211. * @parent: The parent directory.
  212. * @key: Type of interface.
  213. *
  214. * Returns nothing.
  215. */
  216. static void __init tomoyo_create_entry(const char *name, const umode_t mode,
  217. struct dentry *parent, const u8 key)
  218. {
  219. securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
  220. &tomoyo_operations);
  221. }
  222. /**
  223. * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
  224. *
  225. * Returns 0.
  226. */
  227. static int __init tomoyo_initerface_init(void)
  228. {
  229. struct dentry *tomoyo_dir;
  230. /* Don't create securityfs entries unless registered. */
  231. if (current_cred()->security != &tomoyo_kernel_domain)
  232. return 0;
  233. tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
  234. tomoyo_create_entry("query", 0600, tomoyo_dir,
  235. TOMOYO_QUERY);
  236. tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
  237. TOMOYO_DOMAINPOLICY);
  238. tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
  239. TOMOYO_EXCEPTIONPOLICY);
  240. tomoyo_create_entry("audit", 0400, tomoyo_dir,
  241. TOMOYO_AUDIT);
  242. tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
  243. TOMOYO_PROCESS_STATUS);
  244. tomoyo_create_entry("stat", 0644, tomoyo_dir,
  245. TOMOYO_STAT);
  246. tomoyo_create_entry("profile", 0600, tomoyo_dir,
  247. TOMOYO_PROFILE);
  248. tomoyo_create_entry("manager", 0600, tomoyo_dir,
  249. TOMOYO_MANAGER);
  250. tomoyo_create_entry("version", 0400, tomoyo_dir,
  251. TOMOYO_VERSION);
  252. securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
  253. &tomoyo_self_operations);
  254. tomoyo_load_builtin_policy();
  255. return 0;
  256. }
  257. fs_initcall(tomoyo_initerface_init);