tomoyo.c 7.3 KB

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