mount.c 6.6 KB

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