mount.c 6.7 KB

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