tomoyo.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_prepare(struct cred *new, const struct cred *old,
  16. gfp_t gfp)
  17. {
  18. /*
  19. * Since "struct tomoyo_domain_info *" is a sharable pointer,
  20. * we don't need to duplicate.
  21. */
  22. new->security = old->security;
  23. return 0;
  24. }
  25. static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
  26. {
  27. int rc;
  28. rc = cap_bprm_set_creds(bprm);
  29. if (rc)
  30. return rc;
  31. /*
  32. * Do only if this function is called for the first time of an execve
  33. * operation.
  34. */
  35. if (bprm->cred_prepared)
  36. return 0;
  37. /*
  38. * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
  39. * for the first time.
  40. */
  41. if (!tomoyo_policy_loaded)
  42. tomoyo_load_policy(bprm->filename);
  43. /*
  44. * Tell tomoyo_bprm_check_security() is called for the first time of an
  45. * execve operation.
  46. */
  47. bprm->cred->security = NULL;
  48. return 0;
  49. }
  50. static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
  51. {
  52. struct tomoyo_domain_info *domain = bprm->cred->security;
  53. /*
  54. * Execute permission is checked against pathname passed to do_execve()
  55. * using current domain.
  56. */
  57. if (!domain)
  58. return tomoyo_find_next_domain(bprm);
  59. /*
  60. * Read permission is checked against interpreters using next domain.
  61. * '1' is the result of open_to_namei_flags(O_RDONLY).
  62. */
  63. return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1);
  64. }
  65. #ifdef CONFIG_SYSCTL
  66. static int tomoyo_prepend(char **buffer, int *buflen, const char *str)
  67. {
  68. int namelen = strlen(str);
  69. if (*buflen < namelen)
  70. return -ENOMEM;
  71. *buflen -= namelen;
  72. *buffer -= namelen;
  73. memcpy(*buffer, str, namelen);
  74. return 0;
  75. }
  76. /**
  77. * tomoyo_sysctl_path - return the realpath of a ctl_table.
  78. * @table: pointer to "struct ctl_table".
  79. *
  80. * Returns realpath(3) of the @table on success.
  81. * Returns NULL on failure.
  82. *
  83. * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
  84. * if this function didn't return NULL.
  85. */
  86. static char *tomoyo_sysctl_path(struct ctl_table *table)
  87. {
  88. int buflen = TOMOYO_MAX_PATHNAME_LEN;
  89. char *buf = tomoyo_alloc(buflen);
  90. char *end = buf + buflen;
  91. int error = -ENOMEM;
  92. if (!buf)
  93. return NULL;
  94. *--end = '\0';
  95. buflen--;
  96. while (table) {
  97. char num[32];
  98. const char *sp = table->procname;
  99. if (!sp) {
  100. memset(num, 0, sizeof(num));
  101. snprintf(num, sizeof(num) - 1, "=%d=", table->ctl_name);
  102. sp = num;
  103. }
  104. if (tomoyo_prepend(&end, &buflen, sp) ||
  105. tomoyo_prepend(&end, &buflen, "/"))
  106. goto out;
  107. table = table->parent;
  108. }
  109. if (tomoyo_prepend(&end, &buflen, "/proc/sys"))
  110. goto out;
  111. error = tomoyo_encode(buf, end - buf, end);
  112. out:
  113. if (!error)
  114. return buf;
  115. tomoyo_free(buf);
  116. return NULL;
  117. }
  118. static int tomoyo_sysctl(struct ctl_table *table, int op)
  119. {
  120. int error;
  121. char *name;
  122. op &= MAY_READ | MAY_WRITE;
  123. if (!op)
  124. return 0;
  125. name = tomoyo_sysctl_path(table);
  126. if (!name)
  127. return -ENOMEM;
  128. error = tomoyo_check_file_perm(tomoyo_domain(), name, op);
  129. tomoyo_free(name);
  130. return error;
  131. }
  132. #endif
  133. static int tomoyo_path_truncate(struct path *path, loff_t length,
  134. unsigned int time_attrs)
  135. {
  136. return tomoyo_check_1path_perm(tomoyo_domain(),
  137. TOMOYO_TYPE_TRUNCATE_ACL,
  138. path);
  139. }
  140. static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
  141. {
  142. struct path path = { parent->mnt, dentry };
  143. return tomoyo_check_1path_perm(tomoyo_domain(),
  144. TOMOYO_TYPE_UNLINK_ACL,
  145. &path);
  146. }
  147. static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
  148. int mode)
  149. {
  150. struct path path = { parent->mnt, dentry };
  151. return tomoyo_check_1path_perm(tomoyo_domain(),
  152. TOMOYO_TYPE_MKDIR_ACL,
  153. &path);
  154. }
  155. static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
  156. {
  157. struct path path = { parent->mnt, dentry };
  158. return tomoyo_check_1path_perm(tomoyo_domain(),
  159. TOMOYO_TYPE_RMDIR_ACL,
  160. &path);
  161. }
  162. static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
  163. const char *old_name)
  164. {
  165. struct path path = { parent->mnt, dentry };
  166. return tomoyo_check_1path_perm(tomoyo_domain(),
  167. TOMOYO_TYPE_SYMLINK_ACL,
  168. &path);
  169. }
  170. static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
  171. int mode, unsigned int dev)
  172. {
  173. struct path path = { parent->mnt, dentry };
  174. int type = TOMOYO_TYPE_CREATE_ACL;
  175. switch (mode & S_IFMT) {
  176. case S_IFCHR:
  177. type = TOMOYO_TYPE_MKCHAR_ACL;
  178. break;
  179. case S_IFBLK:
  180. type = TOMOYO_TYPE_MKBLOCK_ACL;
  181. break;
  182. case S_IFIFO:
  183. type = TOMOYO_TYPE_MKFIFO_ACL;
  184. break;
  185. case S_IFSOCK:
  186. type = TOMOYO_TYPE_MKSOCK_ACL;
  187. break;
  188. }
  189. return tomoyo_check_1path_perm(tomoyo_domain(),
  190. type, &path);
  191. }
  192. static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
  193. struct dentry *new_dentry)
  194. {
  195. struct path path1 = { new_dir->mnt, old_dentry };
  196. struct path path2 = { new_dir->mnt, new_dentry };
  197. return tomoyo_check_2path_perm(tomoyo_domain(),
  198. TOMOYO_TYPE_LINK_ACL,
  199. &path1, &path2);
  200. }
  201. static int tomoyo_path_rename(struct path *old_parent,
  202. struct dentry *old_dentry,
  203. struct path *new_parent,
  204. struct dentry *new_dentry)
  205. {
  206. struct path path1 = { old_parent->mnt, old_dentry };
  207. struct path path2 = { new_parent->mnt, new_dentry };
  208. return tomoyo_check_2path_perm(tomoyo_domain(),
  209. TOMOYO_TYPE_RENAME_ACL,
  210. &path1, &path2);
  211. }
  212. static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
  213. unsigned long arg)
  214. {
  215. if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
  216. return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
  217. return 0;
  218. }
  219. static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
  220. {
  221. int flags = f->f_flags;
  222. if ((flags + 1) & O_ACCMODE)
  223. flags++;
  224. flags |= f->f_flags & (O_APPEND | O_TRUNC);
  225. /* Don't check read permission here if called from do_execve(). */
  226. if (current->in_execve)
  227. return 0;
  228. return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
  229. }
  230. /*
  231. * tomoyo_security_ops is a "struct security_operations" which is used for
  232. * registering TOMOYO.
  233. */
  234. static struct security_operations tomoyo_security_ops = {
  235. .name = "tomoyo",
  236. .cred_prepare = tomoyo_cred_prepare,
  237. .bprm_set_creds = tomoyo_bprm_set_creds,
  238. .bprm_check_security = tomoyo_bprm_check_security,
  239. #ifdef CONFIG_SYSCTL
  240. .sysctl = tomoyo_sysctl,
  241. #endif
  242. .file_fcntl = tomoyo_file_fcntl,
  243. .dentry_open = tomoyo_dentry_open,
  244. .path_truncate = tomoyo_path_truncate,
  245. .path_unlink = tomoyo_path_unlink,
  246. .path_mkdir = tomoyo_path_mkdir,
  247. .path_rmdir = tomoyo_path_rmdir,
  248. .path_symlink = tomoyo_path_symlink,
  249. .path_mknod = tomoyo_path_mknod,
  250. .path_link = tomoyo_path_link,
  251. .path_rename = tomoyo_path_rename,
  252. };
  253. static int __init tomoyo_init(void)
  254. {
  255. struct cred *cred = (struct cred *) current_cred();
  256. if (!security_module_enable(&tomoyo_security_ops))
  257. return 0;
  258. /* register ourselves with the security framework */
  259. if (register_security(&tomoyo_security_ops))
  260. panic("Failure registering TOMOYO Linux");
  261. printk(KERN_INFO "TOMOYO Linux initialized\n");
  262. cred->security = &tomoyo_kernel_domain;
  263. tomoyo_realpath_init();
  264. return 0;
  265. }
  266. security_initcall(tomoyo_init);