tomoyo.c 7.7 KB

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