tomoyo.c 7.6 KB

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