mount.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
  133. error = -ENOENT;
  134. goto out;
  135. }
  136. requested_dev_name = tomoyo_realpath_from_path(&path);
  137. if (!requested_dev_name) {
  138. error = -ENOENT;
  139. goto out;
  140. }
  141. } else {
  142. /* Map dev_name to "<NULL>" if no dev_name given. */
  143. if (!dev_name)
  144. dev_name = "<NULL>";
  145. requested_dev_name = tomoyo_encode(dev_name);
  146. if (!requested_dev_name) {
  147. error = -ENOMEM;
  148. goto out;
  149. }
  150. }
  151. rdev.name = requested_dev_name;
  152. tomoyo_fill_path_info(&rdev);
  153. r->param_type = TOMOYO_TYPE_MOUNT_ACL;
  154. r->param.mount.need_dev = need_dev;
  155. r->param.mount.dev = &rdev;
  156. r->param.mount.dir = &rdir;
  157. r->param.mount.type = &rtype;
  158. r->param.mount.flags = flags;
  159. do {
  160. tomoyo_check_acl(r, tomoyo_check_mount_acl);
  161. error = tomoyo_audit_mount_log(r);
  162. } while (error == TOMOYO_RETRY_REQUEST);
  163. out:
  164. kfree(requested_dev_name);
  165. kfree(requested_dir_name);
  166. if (fstype)
  167. put_filesystem(fstype);
  168. kfree(requested_type);
  169. return error;
  170. }
  171. /**
  172. * tomoyo_mount_permission - Check permission for mount() operation.
  173. *
  174. * @dev_name: Name of device file.
  175. * @path: Pointer to "struct path".
  176. * @type: Name of filesystem type. May be NULL.
  177. * @flags: Mount options.
  178. * @data_page: Optional data. May be NULL.
  179. *
  180. * Returns 0 on success, negative value otherwise.
  181. */
  182. int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
  183. unsigned long flags, void *data_page)
  184. {
  185. struct tomoyo_request_info r;
  186. int error;
  187. int idx;
  188. if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
  189. == TOMOYO_CONFIG_DISABLED)
  190. return 0;
  191. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  192. flags &= ~MS_MGC_MSK;
  193. if (flags & MS_REMOUNT) {
  194. type = TOMOYO_MOUNT_REMOUNT_KEYWORD;
  195. flags &= ~MS_REMOUNT;
  196. }
  197. if (flags & MS_MOVE) {
  198. type = TOMOYO_MOUNT_MOVE_KEYWORD;
  199. flags &= ~MS_MOVE;
  200. }
  201. if (flags & MS_BIND) {
  202. type = TOMOYO_MOUNT_BIND_KEYWORD;
  203. flags &= ~MS_BIND;
  204. }
  205. if (flags & MS_UNBINDABLE) {
  206. type = TOMOYO_MOUNT_MAKE_UNBINDABLE_KEYWORD;
  207. flags &= ~MS_UNBINDABLE;
  208. }
  209. if (flags & MS_PRIVATE) {
  210. type = TOMOYO_MOUNT_MAKE_PRIVATE_KEYWORD;
  211. flags &= ~MS_PRIVATE;
  212. }
  213. if (flags & MS_SLAVE) {
  214. type = TOMOYO_MOUNT_MAKE_SLAVE_KEYWORD;
  215. flags &= ~MS_SLAVE;
  216. }
  217. if (flags & MS_SHARED) {
  218. type = TOMOYO_MOUNT_MAKE_SHARED_KEYWORD;
  219. flags &= ~MS_SHARED;
  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. }
  228. static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
  229. const struct tomoyo_acl_info *b)
  230. {
  231. const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
  232. const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
  233. return tomoyo_same_acl_head(&p1->head, &p2->head) &&
  234. tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
  235. tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
  236. tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
  237. tomoyo_same_number_union(&p1->flags, &p2->flags);
  238. }
  239. /**
  240. * tomoyo_write_mount - Write "struct tomoyo_mount_acl" list.
  241. *
  242. * @data: String to parse.
  243. * @domain: Pointer to "struct tomoyo_domain_info".
  244. * @is_delete: True if it is a delete request.
  245. *
  246. * Returns 0 on success, negative value otherwise.
  247. *
  248. * Caller holds tomoyo_read_lock().
  249. */
  250. int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain,
  251. const bool is_delete)
  252. {
  253. struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
  254. int error = is_delete ? -ENOENT : -ENOMEM;
  255. char *w[4];
  256. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[3][0])
  257. return -EINVAL;
  258. if (!tomoyo_parse_name_union(w[0], &e.dev_name) ||
  259. !tomoyo_parse_name_union(w[1], &e.dir_name) ||
  260. !tomoyo_parse_name_union(w[2], &e.fs_type) ||
  261. !tomoyo_parse_number_union(w[3], &e.flags))
  262. goto out;
  263. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  264. tomoyo_same_mount_acl, NULL);
  265. out:
  266. tomoyo_put_name_union(&e.dev_name);
  267. tomoyo_put_name_union(&e.dir_name);
  268. tomoyo_put_name_union(&e.fs_type);
  269. tomoyo_put_number_union(&e.flags);
  270. return error;
  271. }