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, loff_t length,
  86. unsigned int time_attrs)
  87. {
  88. return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path);
  89. }
  90. static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
  91. {
  92. struct path path = { parent->mnt, dentry };
  93. return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path);
  94. }
  95. static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
  96. int mode)
  97. {
  98. struct path path = { parent->mnt, dentry };
  99. return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
  100. mode & S_IALLUGO);
  101. }
  102. static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
  103. {
  104. struct path path = { parent->mnt, dentry };
  105. return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path);
  106. }
  107. static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
  108. const char *old_name)
  109. {
  110. struct path path = { parent->mnt, dentry };
  111. return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path);
  112. }
  113. static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
  114. int mode, unsigned int dev)
  115. {
  116. struct path path = { parent->mnt, dentry };
  117. int type = TOMOYO_TYPE_CREATE;
  118. const unsigned int perm = mode & S_IALLUGO;
  119. switch (mode & S_IFMT) {
  120. case S_IFCHR:
  121. type = TOMOYO_TYPE_MKCHAR;
  122. break;
  123. case S_IFBLK:
  124. type = TOMOYO_TYPE_MKBLOCK;
  125. break;
  126. default:
  127. goto no_dev;
  128. }
  129. return tomoyo_path_number3_perm(type, &path, perm, dev);
  130. no_dev:
  131. switch (mode & S_IFMT) {
  132. case S_IFIFO:
  133. type = TOMOYO_TYPE_MKFIFO;
  134. break;
  135. case S_IFSOCK:
  136. type = TOMOYO_TYPE_MKSOCK;
  137. break;
  138. }
  139. return tomoyo_path_number_perm(type, &path, perm);
  140. }
  141. static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
  142. struct dentry *new_dentry)
  143. {
  144. struct path path1 = { new_dir->mnt, old_dentry };
  145. struct path path2 = { new_dir->mnt, new_dentry };
  146. return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
  147. }
  148. static int tomoyo_path_rename(struct path *old_parent,
  149. struct dentry *old_dentry,
  150. struct path *new_parent,
  151. struct dentry *new_dentry)
  152. {
  153. struct path path1 = { old_parent->mnt, old_dentry };
  154. struct path path2 = { new_parent->mnt, new_dentry };
  155. return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
  156. }
  157. static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
  158. unsigned long arg)
  159. {
  160. if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
  161. return tomoyo_path_perm(TOMOYO_TYPE_REWRITE, &file->f_path);
  162. return 0;
  163. }
  164. static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
  165. {
  166. int flags = f->f_flags;
  167. /* Don't check read permission here if called from do_execve(). */
  168. if (current->in_execve)
  169. return 0;
  170. return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
  171. }
  172. static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
  173. unsigned long arg)
  174. {
  175. return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
  176. }
  177. static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
  178. mode_t mode)
  179. {
  180. struct path path = { mnt, dentry };
  181. return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
  182. mode & S_IALLUGO);
  183. }
  184. static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
  185. {
  186. int error = 0;
  187. if (uid != (uid_t) -1)
  188. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
  189. if (!error && gid != (gid_t) -1)
  190. error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
  191. return error;
  192. }
  193. static int tomoyo_path_chroot(struct path *path)
  194. {
  195. return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path);
  196. }
  197. static int tomoyo_sb_mount(char *dev_name, struct path *path,
  198. char *type, unsigned long flags, void *data)
  199. {
  200. return tomoyo_mount_permission(dev_name, path, type, flags, data);
  201. }
  202. static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
  203. {
  204. struct path path = { mnt, mnt->mnt_root };
  205. return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path);
  206. }
  207. static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
  208. {
  209. return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
  210. }
  211. /*
  212. * tomoyo_security_ops is a "struct security_operations" which is used for
  213. * registering TOMOYO.
  214. */
  215. static struct security_operations tomoyo_security_ops = {
  216. .name = "tomoyo",
  217. .cred_alloc_blank = tomoyo_cred_alloc_blank,
  218. .cred_prepare = tomoyo_cred_prepare,
  219. .cred_transfer = tomoyo_cred_transfer,
  220. .cred_free = tomoyo_cred_free,
  221. .bprm_set_creds = tomoyo_bprm_set_creds,
  222. .bprm_check_security = tomoyo_bprm_check_security,
  223. .file_fcntl = tomoyo_file_fcntl,
  224. .dentry_open = tomoyo_dentry_open,
  225. .path_truncate = tomoyo_path_truncate,
  226. .path_unlink = tomoyo_path_unlink,
  227. .path_mkdir = tomoyo_path_mkdir,
  228. .path_rmdir = tomoyo_path_rmdir,
  229. .path_symlink = tomoyo_path_symlink,
  230. .path_mknod = tomoyo_path_mknod,
  231. .path_link = tomoyo_path_link,
  232. .path_rename = tomoyo_path_rename,
  233. .file_ioctl = tomoyo_file_ioctl,
  234. .path_chmod = tomoyo_path_chmod,
  235. .path_chown = tomoyo_path_chown,
  236. .path_chroot = tomoyo_path_chroot,
  237. .sb_mount = tomoyo_sb_mount,
  238. .sb_umount = tomoyo_sb_umount,
  239. .sb_pivotroot = tomoyo_sb_pivotroot,
  240. };
  241. /* Lock for GC. */
  242. struct srcu_struct tomoyo_ss;
  243. static int __init tomoyo_init(void)
  244. {
  245. struct cred *cred = (struct cred *) current_cred();
  246. if (!security_module_enable(&tomoyo_security_ops))
  247. return 0;
  248. /* register ourselves with the security framework */
  249. if (register_security(&tomoyo_security_ops) ||
  250. init_srcu_struct(&tomoyo_ss))
  251. panic("Failure registering TOMOYO Linux");
  252. printk(KERN_INFO "TOMOYO Linux initialized\n");
  253. cred->security = &tomoyo_kernel_domain;
  254. tomoyo_mm_init();
  255. return 0;
  256. }
  257. security_initcall(tomoyo_init);