mount.c 6.7 KB

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