mount.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /* Keywords for mount restrictions. */
  9. /* Allow to call 'mount --bind /source_dir /dest_dir' */
  10. #define TOMOYO_MOUNT_BIND_KEYWORD "--bind"
  11. /* Allow to call 'mount --move /old_dir /new_dir ' */
  12. #define TOMOYO_MOUNT_MOVE_KEYWORD "--move"
  13. /* Allow to call 'mount -o remount /dir ' */
  14. #define TOMOYO_MOUNT_REMOUNT_KEYWORD "--remount"
  15. /* Allow to call 'mount --make-unbindable /dir' */
  16. #define TOMOYO_MOUNT_MAKE_UNBINDABLE_KEYWORD "--make-unbindable"
  17. /* Allow to call 'mount --make-private /dir' */
  18. #define TOMOYO_MOUNT_MAKE_PRIVATE_KEYWORD "--make-private"
  19. /* Allow to call 'mount --make-slave /dir' */
  20. #define TOMOYO_MOUNT_MAKE_SLAVE_KEYWORD "--make-slave"
  21. /* Allow to call 'mount --make-shared /dir' */
  22. #define TOMOYO_MOUNT_MAKE_SHARED_KEYWORD "--make-shared"
  23. /**
  24. * tomoyo_audit_mount_log - Audit mount log.
  25. *
  26. * @r: Pointer to "struct tomoyo_request_info".
  27. *
  28. * Returns 0 on success, negative value otherwise.
  29. */
  30. static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
  31. {
  32. const char *dev = r->param.mount.dev->name;
  33. const char *dir = r->param.mount.dir->name;
  34. const char *type = r->param.mount.type->name;
  35. const unsigned long flags = r->param.mount.flags;
  36. if (r->granted)
  37. return 0;
  38. if (!strcmp(type, TOMOYO_MOUNT_REMOUNT_KEYWORD))
  39. tomoyo_warn_log(r, "mount -o remount %s 0x%lX", dir, flags);
  40. else if (!strcmp(type, TOMOYO_MOUNT_BIND_KEYWORD)
  41. || !strcmp(type, TOMOYO_MOUNT_MOVE_KEYWORD))
  42. tomoyo_warn_log(r, "mount %s %s %s 0x%lX", type, dev, dir,
  43. flags);
  44. else if (!strcmp(type, TOMOYO_MOUNT_MAKE_UNBINDABLE_KEYWORD) ||
  45. !strcmp(type, TOMOYO_MOUNT_MAKE_PRIVATE_KEYWORD) ||
  46. !strcmp(type, TOMOYO_MOUNT_MAKE_SLAVE_KEYWORD) ||
  47. !strcmp(type, TOMOYO_MOUNT_MAKE_SHARED_KEYWORD))
  48. tomoyo_warn_log(r, "mount %s %s 0x%lX", type, dir, flags);
  49. else
  50. tomoyo_warn_log(r, "mount -t %s %s %s 0x%lX", type, dev, dir,
  51. flags);
  52. return tomoyo_supervisor(r,
  53. TOMOYO_KEYWORD_ALLOW_MOUNT "%s %s %s 0x%lX\n",
  54. tomoyo_pattern(r->param.mount.dev),
  55. tomoyo_pattern(r->param.mount.dir), type,
  56. flags);
  57. }
  58. static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
  59. const struct tomoyo_acl_info *ptr)
  60. {
  61. const struct tomoyo_mount_acl *acl =
  62. container_of(ptr, typeof(*acl), head);
  63. return tomoyo_compare_number_union(r->param.mount.flags, &acl->flags) &&
  64. tomoyo_compare_name_union(r->param.mount.type, &acl->fs_type) &&
  65. tomoyo_compare_name_union(r->param.mount.dir, &acl->dir_name) &&
  66. (!r->param.mount.need_dev ||
  67. tomoyo_compare_name_union(r->param.mount.dev, &acl->dev_name));
  68. }
  69. /**
  70. * tomoyo_mount_acl - Check permission for mount() operation.
  71. *
  72. * @r: Pointer to "struct tomoyo_request_info".
  73. * @dev_name: Name of device file.
  74. * @dir: Pointer to "struct path".
  75. * @type: Name of filesystem type.
  76. * @flags: Mount options.
  77. *
  78. * Returns 0 on success, negative value otherwise.
  79. *
  80. * Caller holds tomoyo_read_lock().
  81. */
  82. static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
  83. struct path *dir, char *type, unsigned long flags)
  84. {
  85. struct path path;
  86. struct file_system_type *fstype = NULL;
  87. const char *requested_type = NULL;
  88. const char *requested_dir_name = NULL;
  89. const char *requested_dev_name = NULL;
  90. struct tomoyo_path_info rtype;
  91. struct tomoyo_path_info rdev;
  92. struct tomoyo_path_info rdir;
  93. int need_dev = 0;
  94. int error = -ENOMEM;
  95. /* Get fstype. */
  96. requested_type = tomoyo_encode(type);
  97. if (!requested_type)
  98. goto out;
  99. rtype.name = requested_type;
  100. tomoyo_fill_path_info(&rtype);
  101. /* Get mount point. */
  102. requested_dir_name = tomoyo_realpath_from_path(dir);
  103. if (!requested_dir_name) {
  104. error = -ENOMEM;
  105. goto out;
  106. }
  107. rdir.name = requested_dir_name;
  108. tomoyo_fill_path_info(&rdir);
  109. /* Compare fs name. */
  110. if (!strcmp(type, TOMOYO_MOUNT_REMOUNT_KEYWORD)) {
  111. /* dev_name is ignored. */
  112. } else if (!strcmp(type, TOMOYO_MOUNT_MAKE_UNBINDABLE_KEYWORD) ||
  113. !strcmp(type, TOMOYO_MOUNT_MAKE_PRIVATE_KEYWORD) ||
  114. !strcmp(type, TOMOYO_MOUNT_MAKE_SLAVE_KEYWORD) ||
  115. !strcmp(type, TOMOYO_MOUNT_MAKE_SHARED_KEYWORD)) {
  116. /* dev_name is ignored. */
  117. } else if (!strcmp(type, TOMOYO_MOUNT_BIND_KEYWORD) ||
  118. !strcmp(type, TOMOYO_MOUNT_MOVE_KEYWORD)) {
  119. need_dev = -1; /* dev_name is a directory */
  120. } else {
  121. fstype = get_fs_type(type);
  122. if (!fstype) {
  123. error = -ENODEV;
  124. goto out;
  125. }
  126. if (fstype->fs_flags & FS_REQUIRES_DEV)
  127. /* dev_name is a block device file. */
  128. need_dev = 1;
  129. }
  130. if (need_dev) {
  131. /* Get mount point or device file. */
  132. if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
  133. error = -ENOENT;
  134. goto out;
  135. }
  136. requested_dev_name = tomoyo_realpath_from_path(&path);
  137. path_put(&path);
  138. if (!requested_dev_name) {
  139. error = -ENOENT;
  140. goto out;
  141. }
  142. } else {
  143. /* Map dev_name to "<NULL>" if no dev_name given. */
  144. if (!dev_name)
  145. dev_name = "<NULL>";
  146. requested_dev_name = tomoyo_encode(dev_name);
  147. if (!requested_dev_name) {
  148. error = -ENOMEM;
  149. goto out;
  150. }
  151. }
  152. rdev.name = requested_dev_name;
  153. tomoyo_fill_path_info(&rdev);
  154. r->param_type = TOMOYO_TYPE_MOUNT_ACL;
  155. r->param.mount.need_dev = need_dev;
  156. r->param.mount.dev = &rdev;
  157. r->param.mount.dir = &rdir;
  158. r->param.mount.type = &rtype;
  159. r->param.mount.flags = flags;
  160. do {
  161. tomoyo_check_acl(r, tomoyo_check_mount_acl);
  162. error = tomoyo_audit_mount_log(r);
  163. } while (error == TOMOYO_RETRY_REQUEST);
  164. out:
  165. kfree(requested_dev_name);
  166. kfree(requested_dir_name);
  167. if (fstype)
  168. put_filesystem(fstype);
  169. kfree(requested_type);
  170. return error;
  171. }
  172. /**
  173. * tomoyo_mount_permission - Check permission for mount() operation.
  174. *
  175. * @dev_name: Name of device file.
  176. * @path: Pointer to "struct path".
  177. * @type: Name of filesystem type. May be NULL.
  178. * @flags: Mount options.
  179. * @data_page: Optional data. May be NULL.
  180. *
  181. * Returns 0 on success, negative value otherwise.
  182. */
  183. int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
  184. unsigned long flags, 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_MOUNT_REMOUNT_KEYWORD;
  196. flags &= ~MS_REMOUNT;
  197. } else if (flags & MS_BIND) {
  198. type = TOMOYO_MOUNT_BIND_KEYWORD;
  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_MOUNT_MAKE_SHARED_KEYWORD;
  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_MOUNT_MAKE_PRIVATE_KEYWORD;
  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_MOUNT_MAKE_SLAVE_KEYWORD;
  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_MOUNT_MAKE_UNBINDABLE_KEYWORD;
  219. flags &= ~MS_UNBINDABLE;
  220. } else if (flags & MS_MOVE) {
  221. type = TOMOYO_MOUNT_MOVE_KEYWORD;
  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. }
  231. static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
  232. const struct tomoyo_acl_info *b)
  233. {
  234. const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
  235. const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
  236. return tomoyo_same_acl_head(&p1->head, &p2->head) &&
  237. tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
  238. tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
  239. tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
  240. tomoyo_same_number_union(&p1->flags, &p2->flags);
  241. }
  242. /**
  243. * tomoyo_write_mount - Write "struct tomoyo_mount_acl" list.
  244. *
  245. * @data: String to parse.
  246. * @domain: Pointer to "struct tomoyo_domain_info".
  247. * @is_delete: True if it is a delete request.
  248. *
  249. * Returns 0 on success, negative value otherwise.
  250. *
  251. * Caller holds tomoyo_read_lock().
  252. */
  253. int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain,
  254. const bool is_delete)
  255. {
  256. struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
  257. int error = is_delete ? -ENOENT : -ENOMEM;
  258. char *w[4];
  259. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[3][0])
  260. return -EINVAL;
  261. if (!tomoyo_parse_name_union(w[0], &e.dev_name) ||
  262. !tomoyo_parse_name_union(w[1], &e.dir_name) ||
  263. !tomoyo_parse_name_union(w[2], &e.fs_type) ||
  264. !tomoyo_parse_number_union(w[3], &e.flags))
  265. goto out;
  266. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  267. tomoyo_same_mount_acl, NULL);
  268. out:
  269. tomoyo_put_name_union(&e.dev_name);
  270. tomoyo_put_name_union(&e.dir_name);
  271. tomoyo_put_name_union(&e.fs_type);
  272. tomoyo_put_number_union(&e.flags);
  273. return error;
  274. }