mount.c 6.8 KB

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