tomoyo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * security/tomoyo/tomoyo.c
  3. *
  4. * LSM hooks for TOMOYO Linux.
  5. *
  6. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  7. *
  8. * Version: 2.2.0 2009/04/01
  9. *
  10. */
  11. #include <linux/security.h>
  12. #include "common.h"
  13. #include "tomoyo.h"
  14. #include "realpath.h"
  15. static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
  16. {
  17. new->security = NULL;
  18. return 0;
  19. }
  20. static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
  21. gfp_t gfp)
  22. {
  23. /*
  24. * Since "struct tomoyo_domain_info *" is a sharable pointer,
  25. * we don't need to duplicate.
  26. */
  27. new->security = old->security;
  28. return 0;
  29. }
  30. static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
  31. {
  32. /*
  33. * Since "struct tomoyo_domain_info *" is a sharable pointer,
  34. * we don't need to duplicate.
  35. */
  36. new->security = old->security;
  37. }
  38. static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
  39. {
  40. int rc;
  41. rc = cap_bprm_set_creds(bprm);
  42. if (rc)
  43. return rc;
  44. /*
  45. * Do only if this function is called for the first time of an execve
  46. * operation.
  47. */
  48. if (bprm->cred_prepared)
  49. return 0;
  50. /*
  51. * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
  52. * for the first time.
  53. */
  54. if (!tomoyo_policy_loaded)
  55. tomoyo_load_policy(bprm->filename);
  56. /*
  57. * Tell tomoyo_bprm_check_security() is called for the first time of an
  58. * execve operation.
  59. */
  60. bprm->cred->security = NULL;
  61. return 0;
  62. }
  63. static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
  64. {
  65. struct tomoyo_domain_info *domain = bprm->cred->security;
  66. /*
  67. * Execute permission is checked against pathname passed to do_execve()
  68. * using current domain.
  69. */
  70. if (!domain)
  71. return tomoyo_find_next_domain(bprm);
  72. /*
  73. * Read permission is checked against interpreters using next domain.
  74. * '1' is the result of open_to_namei_flags(O_RDONLY).
  75. */
  76. return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1);
  77. }
  78. static int tomoyo_path_truncate(struct path *path, loff_t length,
  79. unsigned int time_attrs)
  80. {
  81. return tomoyo_check_1path_perm(tomoyo_domain(),
  82. TOMOYO_TYPE_TRUNCATE_ACL,
  83. path);
  84. }
  85. static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
  86. {
  87. struct path path = { parent->mnt, dentry };
  88. return tomoyo_check_1path_perm(tomoyo_domain(),
  89. TOMOYO_TYPE_UNLINK_ACL,
  90. &path);
  91. }
  92. static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
  93. int mode)
  94. {
  95. struct path path = { parent->mnt, dentry };
  96. return tomoyo_check_1path_perm(tomoyo_domain(),
  97. TOMOYO_TYPE_MKDIR_ACL,
  98. &path);
  99. }
  100. static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
  101. {
  102. struct path path = { parent->mnt, dentry };
  103. return tomoyo_check_1path_perm(tomoyo_domain(),
  104. TOMOYO_TYPE_RMDIR_ACL,
  105. &path);
  106. }
  107. static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
  108. const char *old_name)
  109. {
  110. struct path path = { parent->mnt, dentry };
  111. return tomoyo_check_1path_perm(tomoyo_domain(),
  112. TOMOYO_TYPE_SYMLINK_ACL,
  113. &path);
  114. }
  115. static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
  116. int mode, unsigned int dev)
  117. {
  118. struct path path = { parent->mnt, dentry };
  119. int type = TOMOYO_TYPE_CREATE_ACL;
  120. switch (mode & S_IFMT) {
  121. case S_IFCHR:
  122. type = TOMOYO_TYPE_MKCHAR_ACL;
  123. break;
  124. case S_IFBLK:
  125. type = TOMOYO_TYPE_MKBLOCK_ACL;
  126. break;
  127. case S_IFIFO:
  128. type = TOMOYO_TYPE_MKFIFO_ACL;
  129. break;
  130. case S_IFSOCK:
  131. type = TOMOYO_TYPE_MKSOCK_ACL;
  132. break;
  133. }
  134. return tomoyo_check_1path_perm(tomoyo_domain(),
  135. type, &path);
  136. }
  137. static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
  138. struct dentry *new_dentry)
  139. {
  140. struct path path1 = { new_dir->mnt, old_dentry };
  141. struct path path2 = { new_dir->mnt, new_dentry };
  142. return tomoyo_check_2path_perm(tomoyo_domain(),
  143. TOMOYO_TYPE_LINK_ACL,
  144. &path1, &path2);
  145. }
  146. static int tomoyo_path_rename(struct path *old_parent,
  147. struct dentry *old_dentry,
  148. struct path *new_parent,
  149. struct dentry *new_dentry)
  150. {
  151. struct path path1 = { old_parent->mnt, old_dentry };
  152. struct path path2 = { new_parent->mnt, new_dentry };
  153. return tomoyo_check_2path_perm(tomoyo_domain(),
  154. TOMOYO_TYPE_RENAME_ACL,
  155. &path1, &path2);
  156. }
  157. static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
  158. unsigned long arg)
  159. {
  160. if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
  161. return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
  162. return 0;
  163. }
  164. static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
  165. {
  166. int flags = f->f_flags;
  167. if ((flags + 1) & O_ACCMODE)
  168. flags++;
  169. flags |= f->f_flags & (O_APPEND | O_TRUNC);
  170. /* Don't check read permission here if called from do_execve(). */
  171. if (current->in_execve)
  172. return 0;
  173. return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
  174. }
  175. /*
  176. * tomoyo_security_ops is a "struct security_operations" which is used for
  177. * registering TOMOYO.
  178. */
  179. static struct security_operations tomoyo_security_ops = {
  180. .name = "tomoyo",
  181. .cred_alloc_blank = tomoyo_cred_alloc_blank,
  182. .cred_prepare = tomoyo_cred_prepare,
  183. .cred_transfer = tomoyo_cred_transfer,
  184. .bprm_set_creds = tomoyo_bprm_set_creds,
  185. .bprm_check_security = tomoyo_bprm_check_security,
  186. .file_fcntl = tomoyo_file_fcntl,
  187. .dentry_open = tomoyo_dentry_open,
  188. .path_truncate = tomoyo_path_truncate,
  189. .path_unlink = tomoyo_path_unlink,
  190. .path_mkdir = tomoyo_path_mkdir,
  191. .path_rmdir = tomoyo_path_rmdir,
  192. .path_symlink = tomoyo_path_symlink,
  193. .path_mknod = tomoyo_path_mknod,
  194. .path_link = tomoyo_path_link,
  195. .path_rename = tomoyo_path_rename,
  196. };
  197. static int __init tomoyo_init(void)
  198. {
  199. struct cred *cred = (struct cred *) current_cred();
  200. if (!security_module_enable(&tomoyo_security_ops))
  201. return 0;
  202. /* register ourselves with the security framework */
  203. if (register_security(&tomoyo_security_ops))
  204. panic("Failure registering TOMOYO Linux");
  205. printk(KERN_INFO "TOMOYO Linux initialized\n");
  206. cred->security = &tomoyo_kernel_domain;
  207. tomoyo_realpath_init();
  208. return 0;
  209. }
  210. security_initcall(tomoyo_init);