mount.c 6.9 KB

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