mount.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * security/tomoyo/mount.c
  3. *
  4. * Copyright (C) 2005-2011 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. Maybe NULL.
  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,
  70. const char *dev_name,
  71. const struct path *dir, const char *type,
  72. unsigned long flags)
  73. {
  74. struct tomoyo_obj_info obj = { };
  75. struct path path;
  76. struct file_system_type *fstype = NULL;
  77. const char *requested_type = NULL;
  78. const char *requested_dir_name = NULL;
  79. const char *requested_dev_name = NULL;
  80. struct tomoyo_path_info rtype;
  81. struct tomoyo_path_info rdev;
  82. struct tomoyo_path_info rdir;
  83. int need_dev = 0;
  84. int error = -ENOMEM;
  85. r->obj = &obj;
  86. /* Get fstype. */
  87. requested_type = tomoyo_encode(type);
  88. if (!requested_type)
  89. goto out;
  90. rtype.name = requested_type;
  91. tomoyo_fill_path_info(&rtype);
  92. /* Get mount point. */
  93. obj.path2 = *dir;
  94. requested_dir_name = tomoyo_realpath_from_path(dir);
  95. if (!requested_dir_name) {
  96. error = -ENOMEM;
  97. goto out;
  98. }
  99. rdir.name = requested_dir_name;
  100. tomoyo_fill_path_info(&rdir);
  101. /* Compare fs name. */
  102. if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
  103. /* dev_name is ignored. */
  104. } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
  105. type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
  106. type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
  107. type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
  108. /* dev_name is ignored. */
  109. } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
  110. type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
  111. need_dev = -1; /* dev_name is a directory */
  112. } else {
  113. fstype = get_fs_type(type);
  114. if (!fstype) {
  115. error = -ENODEV;
  116. goto out;
  117. }
  118. if (fstype->fs_flags & FS_REQUIRES_DEV)
  119. /* dev_name is a block device file. */
  120. need_dev = 1;
  121. }
  122. if (need_dev) {
  123. /* Get mount point or device file. */
  124. if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
  125. error = -ENOENT;
  126. goto out;
  127. }
  128. obj.path1 = path;
  129. requested_dev_name = tomoyo_realpath_from_path(&path);
  130. if (!requested_dev_name) {
  131. error = -ENOENT;
  132. goto out;
  133. }
  134. } else {
  135. /* Map dev_name to "<NULL>" if no dev_name given. */
  136. if (!dev_name)
  137. dev_name = "<NULL>";
  138. requested_dev_name = tomoyo_encode(dev_name);
  139. if (!requested_dev_name) {
  140. error = -ENOMEM;
  141. goto out;
  142. }
  143. }
  144. rdev.name = requested_dev_name;
  145. tomoyo_fill_path_info(&rdev);
  146. r->param_type = TOMOYO_TYPE_MOUNT_ACL;
  147. r->param.mount.need_dev = need_dev;
  148. r->param.mount.dev = &rdev;
  149. r->param.mount.dir = &rdir;
  150. r->param.mount.type = &rtype;
  151. r->param.mount.flags = flags;
  152. do {
  153. tomoyo_check_acl(r, tomoyo_check_mount_acl);
  154. error = tomoyo_audit_mount_log(r);
  155. } while (error == TOMOYO_RETRY_REQUEST);
  156. out:
  157. kfree(requested_dev_name);
  158. kfree(requested_dir_name);
  159. if (fstype)
  160. put_filesystem(fstype);
  161. kfree(requested_type);
  162. /* Drop refcount obtained by kern_path(). */
  163. if (obj.path1.dentry)
  164. path_put(&obj.path1);
  165. return error;
  166. }
  167. /**
  168. * tomoyo_mount_permission - Check permission for mount() operation.
  169. *
  170. * @dev_name: Name of device file. Maybe NULL.
  171. * @path: Pointer to "struct path".
  172. * @type: Name of filesystem type. Maybe NULL.
  173. * @flags: Mount options.
  174. * @data_page: Optional data. Maybe NULL.
  175. *
  176. * Returns 0 on success, negative value otherwise.
  177. */
  178. int tomoyo_mount_permission(const char *dev_name, const struct path *path,
  179. const char *type, unsigned long flags,
  180. void *data_page)
  181. {
  182. struct tomoyo_request_info r;
  183. int error;
  184. int idx;
  185. if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
  186. == TOMOYO_CONFIG_DISABLED)
  187. return 0;
  188. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  189. flags &= ~MS_MGC_MSK;
  190. if (flags & MS_REMOUNT) {
  191. type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
  192. flags &= ~MS_REMOUNT;
  193. } else if (flags & MS_BIND) {
  194. type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
  195. flags &= ~MS_BIND;
  196. } else if (flags & MS_SHARED) {
  197. if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  198. return -EINVAL;
  199. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
  200. flags &= ~MS_SHARED;
  201. } else if (flags & MS_PRIVATE) {
  202. if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
  203. return -EINVAL;
  204. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
  205. flags &= ~MS_PRIVATE;
  206. } else if (flags & MS_SLAVE) {
  207. if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE))
  208. return -EINVAL;
  209. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
  210. flags &= ~MS_SLAVE;
  211. } else if (flags & MS_UNBINDABLE) {
  212. if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
  213. return -EINVAL;
  214. type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
  215. flags &= ~MS_UNBINDABLE;
  216. } else if (flags & MS_MOVE) {
  217. type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
  218. flags &= ~MS_MOVE;
  219. }
  220. if (!type)
  221. type = "<NULL>";
  222. idx = tomoyo_read_lock();
  223. error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
  224. tomoyo_read_unlock(idx);
  225. return error;
  226. }