mount.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * security/tomoyo/mount.c
  3. *
  4. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  5. */
  6. #include <linux/slab.h>
  7. #include "common.h"
  8. /* String table for special mount operations. */
  9. static const char * const tomoyo_mounts[TOMOYO_MAX_SPECIAL_MOUNT] = {
  10. [TOMOYO_MOUNT_BIND] = "--bind",
  11. [TOMOYO_MOUNT_MOVE] = "--move",
  12. [TOMOYO_MOUNT_REMOUNT] = "--remount",
  13. [TOMOYO_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable",
  14. [TOMOYO_MOUNT_MAKE_PRIVATE] = "--make-private",
  15. [TOMOYO_MOUNT_MAKE_SLAVE] = "--make-slave",
  16. [TOMOYO_MOUNT_MAKE_SHARED] = "--make-shared",
  17. };
  18. /**
  19. * tomoyo_audit_mount_log - Audit mount log.
  20. *
  21. * @r: Pointer to "struct tomoyo_request_info".
  22. *
  23. * Returns 0 on success, negative value otherwise.
  24. */
  25. static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
  26. {
  27. return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
  28. r->param.mount.dev->name,
  29. r->param.mount.dir->name,
  30. r->param.mount.type->name,
  31. r->param.mount.flags);
  32. }
  33. /**
  34. * tomoyo_check_mount_acl - Check permission for path path path number operation.
  35. *
  36. * @r: Pointer to "struct tomoyo_request_info".
  37. * @ptr: Pointer to "struct tomoyo_acl_info".
  38. *
  39. * Returns true if granted, false otherwise.
  40. */
  41. static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
  42. const struct tomoyo_acl_info *ptr)
  43. {
  44. const struct tomoyo_mount_acl *acl =
  45. container_of(ptr, typeof(*acl), head);
  46. return tomoyo_compare_number_union(r->param.mount.flags,
  47. &acl->flags) &&
  48. tomoyo_compare_name_union(r->param.mount.type,
  49. &acl->fs_type) &&
  50. tomoyo_compare_name_union(r->param.mount.dir,
  51. &acl->dir_name) &&
  52. (!r->param.mount.need_dev ||
  53. tomoyo_compare_name_union(r->param.mount.dev,
  54. &acl->dev_name));
  55. }
  56. /**
  57. * tomoyo_mount_acl - Check permission for mount() operation.
  58. *
  59. * @r: Pointer to "struct tomoyo_request_info".
  60. * @dev_name: Name of device file.
  61. * @dir: Pointer to "struct path".
  62. * @type: Name of filesystem type.
  63. * @flags: Mount options.
  64. *
  65. * Returns 0 on success, negative value otherwise.
  66. *
  67. * Caller holds tomoyo_read_lock().
  68. */
  69. static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
  70. struct path *dir, const char *type,
  71. unsigned long flags)
  72. {
  73. struct path path;
  74. struct file_system_type *fstype = NULL;
  75. const char *requested_type = NULL;
  76. const char *requested_dir_name = NULL;
  77. const char *requested_dev_name = NULL;
  78. struct tomoyo_path_info rtype;
  79. struct tomoyo_path_info rdev;
  80. struct tomoyo_path_info rdir;
  81. int need_dev = 0;
  82. int error = -ENOMEM;
  83. /* Get fstype. */
  84. requested_type = tomoyo_encode(type);
  85. if (!requested_type)
  86. goto out;
  87. rtype.name = requested_type;
  88. tomoyo_fill_path_info(&rtype);
  89. /* Get mount point. */
  90. requested_dir_name = tomoyo_realpath_from_path(dir);
  91. if (!requested_dir_name) {
  92. error = -ENOMEM;
  93. goto out;
  94. }
  95. rdir.name = requested_dir_name;
  96. tomoyo_fill_path_info(&rdir);
  97. /* Compare fs name. */
  98. if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
  99. /* dev_name is ignored. */
  100. } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
  101. type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
  102. type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
  103. type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
  104. /* dev_name is ignored. */
  105. } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
  106. type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
  107. need_dev = -1; /* dev_name is a directory */
  108. } else {
  109. fstype = get_fs_type(type);
  110. if (!fstype) {
  111. error = -ENODEV;
  112. goto out;
  113. }
  114. if (fstype->fs_flags & FS_REQUIRES_DEV)
  115. /* dev_name is a block device file. */
  116. need_dev = 1;
  117. }
  118. if (need_dev) {
  119. /* Get mount point or device file. */
  120. if (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
  121. error = -ENOENT;
  122. goto out;
  123. }
  124. requested_dev_name = tomoyo_realpath_from_path(&path);
  125. path_put(&path);
  126. if (!requested_dev_name) {
  127. error = -ENOENT;
  128. goto out;
  129. }
  130. } else {
  131. /* Map dev_name to "<NULL>" if no dev_name given. */
  132. if (!dev_name)
  133. dev_name = "<NULL>";
  134. requested_dev_name = tomoyo_encode(dev_name);
  135. if (!requested_dev_name) {
  136. error = -ENOMEM;
  137. goto out;
  138. }
  139. }
  140. rdev.name = requested_dev_name;
  141. tomoyo_fill_path_info(&rdev);
  142. r->param_type = TOMOYO_TYPE_MOUNT_ACL;
  143. r->param.mount.need_dev = need_dev;
  144. r->param.mount.dev = &rdev;
  145. r->param.mount.dir = &rdir;
  146. r->param.mount.type = &rtype;
  147. r->param.mount.flags = flags;
  148. do {
  149. tomoyo_check_acl(r, tomoyo_check_mount_acl);
  150. error = tomoyo_audit_mount_log(r);
  151. } while (error == TOMOYO_RETRY_REQUEST);
  152. out:
  153. kfree(requested_dev_name);
  154. kfree(requested_dir_name);
  155. if (fstype)
  156. put_filesystem(fstype);
  157. kfree(requested_type);
  158. return error;
  159. }
  160. /**
  161. * tomoyo_mount_permission - Check permission for mount() operation.
  162. *
  163. * @dev_name: Name of device file.
  164. * @path: Pointer to "struct path".
  165. * @type: Name of filesystem type. May be NULL.
  166. * @flags: Mount options.
  167. * @data_page: Optional data. May be NULL.
  168. *
  169. * Returns 0 on success, negative value otherwise.
  170. */
  171. int tomoyo_mount_permission(char *dev_name, struct path *path,
  172. const char *type, unsigned long flags,
  173. void *data_page)
  174. {
  175. struct tomoyo_request_info r;
  176. int error;
  177. int idx;
  178. if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
  179. == TOMOYO_CONFIG_DISABLED)
  180. return 0;
  181. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  182. flags &= ~MS_MGC_MSK;
  183. if (flags & MS_REMOUNT) {
  184. type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
  185. flags &= ~MS_REMOUNT;
  186. }
  187. if (flags & MS_MOVE) {
  188. type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
  189. flags &= ~MS_MOVE;
  190. }
  191. if (flags & MS_BIND) {
  192. type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
  193. flags &= ~MS_BIND;
  194. }
  195. if (flags & MS_UNBINDABLE) {
  196. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
  197. flags &= ~MS_UNBINDABLE;
  198. }
  199. if (flags & MS_PRIVATE) {
  200. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
  201. flags &= ~MS_PRIVATE;
  202. }
  203. if (flags & MS_SLAVE) {
  204. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
  205. flags &= ~MS_SLAVE;
  206. }
  207. if (flags & MS_SHARED) {
  208. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
  209. flags &= ~MS_SHARED;
  210. }
  211. if (!type)
  212. type = "<NULL>";
  213. idx = tomoyo_read_lock();
  214. error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
  215. tomoyo_read_unlock(idx);
  216. return error;
  217. }