tomoyo.c 7.5 KB

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