tomoyo.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * security/tomoyo/tomoyo.c
  3. *
  4. * LSM hooks for TOMOYO Linux.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include <linux/security.h>
  9. #include "common.h"
  10. static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
  11. {
  12. new->security = NULL;
  13. return 0;
  14. }
  15. static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
  16. gfp_t gfp)
  17. {
  18. struct tomoyo_domain_info *domain = old->security;
  19. new->security = domain;
  20. if (domain)
  21. atomic_inc(&domain->users);
  22. return 0;
  23. }
  24. static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
  25. {
  26. tomoyo_cred_prepare(new, old, 0);
  27. }
  28. static void tomoyo_cred_free(struct cred *cred)
  29. {
  30. struct tomoyo_domain_info *domain = cred->security;
  31. if (domain)
  32. atomic_dec(&domain->users);
  33. }
  34. static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
  35. {
  36. int rc;
  37. rc = cap_bprm_set_creds(bprm);
  38. if (rc)
  39. return rc;
  40. /*
  41. * Do only if this function is called for the first time of an execve
  42. * operation.
  43. */
  44. if (bprm->cred_prepared)
  45. return 0;
  46. /*
  47. * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
  48. * for the first time.
  49. */
  50. if (!tomoyo_policy_loaded)
  51. tomoyo_load_policy(bprm->filename);
  52. /*
  53. * Release reference to "struct tomoyo_domain_info" stored inside
  54. * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
  55. * stored inside "bprm->cred->security" will be acquired later inside
  56. * tomoyo_find_next_domain().
  57. */
  58. atomic_dec(&((struct tomoyo_domain_info *)
  59. bprm->cred->security)->users);
  60. /*
  61. * Tell tomoyo_bprm_check_security() is called for the first time of an
  62. * execve operation.
  63. */
  64. bprm->cred->security = NULL;
  65. return 0;
  66. }
  67. static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
  68. {
  69. struct tomoyo_domain_info *domain = bprm->cred->security;
  70. /*
  71. * Execute permission is checked against pathname passed to do_execve()
  72. * using current domain.
  73. */
  74. if (!domain) {
  75. const int idx = tomoyo_read_lock();
  76. const int err = tomoyo_find_next_domain(bprm);
  77. tomoyo_read_unlock(idx);
  78. return err;
  79. }
  80. /*
  81. * Read permission is checked against interpreters using next domain.
  82. */
  83. return tomoyo_check_open_permission(domain, &bprm->file->f_path, O_RDONLY);
  84. }
  85. static int tomoyo_path_truncate(struct path *path)
  86. {
  87. return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path);
  88. }
  89. static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
  90. {
  91. struct path path = { parent->mnt, dentry };
  92. return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path);
  93. }
  94. static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
  95. int mode)
  96. {
  97. struct path path = { parent->mnt, dentry };
  98. return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
  99. mode & S_IALLUGO);
  100. }
  101. static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
  102. {
  103. struct path path = { parent->mnt, dentry };
  104. return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path);
  105. }
  106. static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
  107. const char *old_name)
  108. {
  109. struct path path = { parent->mnt, dentry };
  110. return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path);
  111. }
  112. static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
  113. int mode, unsigned int dev)
  114. {
  115. struct path path = { parent->mnt, dentry };
  116. int type = TOMOYO_TYPE_CREATE;
  117. const unsigned int perm = mode & S_IALLUGO;
  118. switch (mode & S_IFMT) {
  119. case S_IFCHR:
  120. type = TOMOYO_TYPE_MKCHAR;
  121. break;
  122. case S_IFBLK:
  123. type = TOMOYO_TYPE_MKBLOCK;
  124. break;
  125. default:
  126. goto no_dev;
  127. }
  128. return tomoyo_mkdev_perm(type, &path, perm, dev);
  129. no_dev:
  130. switch (mode & S_IFMT) {
  131. case S_IFIFO:
  132. type = TOMOYO_TYPE_MKFIFO;
  133. break;
  134. case S_IFSOCK:
  135. type = TOMOYO_TYPE_MKSOCK;
  136. break;
  137. }
  138. return tomoyo_path_number_perm(type, &path, perm);
  139. }
  140. static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
  141. struct dentry *new_dentry)
  142. {
  143. struct path path1 = { new_dir->mnt, old_dentry };
  144. struct path path2 = { new_dir->mnt, new_dentry };
  145. return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
  146. }
  147. static int tomoyo_path_rename(struct path *old_parent,
  148. struct dentry *old_dentry,
  149. struct path *new_parent,
  150. struct dentry *new_dentry)
  151. {
  152. struct path path1 = { old_parent->mnt, old_dentry };
  153. struct path path2 = { new_parent->mnt, new_dentry };
  154. return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
  155. }
  156. static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
  157. unsigned long arg)
  158. {
  159. if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
  160. return tomoyo_path_perm(TOMOYO_TYPE_REWRITE, &file->f_path);
  161. return 0;
  162. }
  163. static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
  164. {
  165. int flags = f->f_flags;
  166. /* Don't check read permission here if called from do_execve(). */
  167. if (current->in_execve)
  168. return 0;
  169. return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
  170. }
  171. static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
  172. unsigned long arg)
  173. {
  174. return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
  175. }
  176. static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
  177. mode_t mode)
  178. {
  179. struct path path = { mnt, dentry };
  180. return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
  181. mode & S_IALLUGO);
  182. }
  183. static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
  184. {
  185. int error = 0;
  186. if (uid != (uid_t) -1)
  187. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
  188. if (!error && gid != (gid_t) -1)
  189. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
  190. return error;
  191. }
  192. static int tomoyo_path_chroot(struct path *path)
  193. {
  194. return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path);
  195. }
  196. static int tomoyo_sb_mount(char *dev_name, struct path *path,
  197. char *type, unsigned long flags, void *data)
  198. {
  199. return tomoyo_mount_permission(dev_name, path, type, flags, data);
  200. }
  201. static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
  202. {
  203. struct path path = { mnt, mnt->mnt_root };
  204. return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path);
  205. }
  206. static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
  207. {
  208. return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
  209. }
  210. /*
  211. * tomoyo_security_ops is a "struct security_operations" which is used for
  212. * registering TOMOYO.
  213. */
  214. static struct security_operations tomoyo_security_ops = {
  215. .name = "tomoyo",
  216. .cred_alloc_blank = tomoyo_cred_alloc_blank,
  217. .cred_prepare = tomoyo_cred_prepare,
  218. .cred_transfer = tomoyo_cred_transfer,
  219. .cred_free = tomoyo_cred_free,
  220. .bprm_set_creds = tomoyo_bprm_set_creds,
  221. .bprm_check_security = tomoyo_bprm_check_security,
  222. .file_fcntl = tomoyo_file_fcntl,
  223. .dentry_open = tomoyo_dentry_open,
  224. .path_truncate = tomoyo_path_truncate,
  225. .path_unlink = tomoyo_path_unlink,
  226. .path_mkdir = tomoyo_path_mkdir,
  227. .path_rmdir = tomoyo_path_rmdir,
  228. .path_symlink = tomoyo_path_symlink,
  229. .path_mknod = tomoyo_path_mknod,
  230. .path_link = tomoyo_path_link,
  231. .path_rename = tomoyo_path_rename,
  232. .file_ioctl = tomoyo_file_ioctl,
  233. .path_chmod = tomoyo_path_chmod,
  234. .path_chown = tomoyo_path_chown,
  235. .path_chroot = tomoyo_path_chroot,
  236. .sb_mount = tomoyo_sb_mount,
  237. .sb_umount = tomoyo_sb_umount,
  238. .sb_pivotroot = tomoyo_sb_pivotroot,
  239. };
  240. /* Lock for GC. */
  241. struct srcu_struct tomoyo_ss;
  242. static int __init tomoyo_init(void)
  243. {
  244. struct cred *cred = (struct cred *) current_cred();
  245. if (!security_module_enable(&tomoyo_security_ops))
  246. return 0;
  247. /* register ourselves with the security framework */
  248. if (register_security(&tomoyo_security_ops) ||
  249. init_srcu_struct(&tomoyo_ss))
  250. panic("Failure registering TOMOYO Linux");
  251. printk(KERN_INFO "TOMOYO Linux initialized\n");
  252. cred->security = &tomoyo_kernel_domain;
  253. tomoyo_mm_init();
  254. return 0;
  255. }
  256. security_initcall(tomoyo_init);