path.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor function for pathnames
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/magic.h>
  15. #include <linux/mount.h>
  16. #include <linux/namei.h>
  17. #include <linux/nsproxy.h>
  18. #include <linux/path.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/fs_struct.h>
  22. #include "include/apparmor.h"
  23. #include "include/path.h"
  24. #include "include/policy.h"
  25. /* modified from dcache.c */
  26. static int prepend(char **buffer, int buflen, const char *str, int namelen)
  27. {
  28. buflen -= namelen;
  29. if (buflen < 0)
  30. return -ENAMETOOLONG;
  31. *buffer -= namelen;
  32. memcpy(*buffer, str, namelen);
  33. return 0;
  34. }
  35. #define CHROOT_NSCONNECT (PATH_CHROOT_REL | PATH_CHROOT_NSCONNECT)
  36. /* If the path is not connected to the expected root,
  37. * check if it is a sysctl and handle specially else remove any
  38. * leading / that __d_path may have returned.
  39. * Unless
  40. * specifically directed to connect the path,
  41. * OR
  42. * if in a chroot and doing chroot relative paths and the path
  43. * resolves to the namespace root (would be connected outside
  44. * of chroot) and specifically directed to connect paths to
  45. * namespace root.
  46. */
  47. static int disconnect(const struct path *path, char *buf, char **name,
  48. int flags)
  49. {
  50. int error = 0;
  51. if (!(flags & PATH_CONNECT_PATH) &&
  52. !(((flags & CHROOT_NSCONNECT) == CHROOT_NSCONNECT) &&
  53. our_mnt(path->mnt))) {
  54. /* disconnected path, don't return pathname starting
  55. * with '/'
  56. */
  57. error = -EACCES;
  58. if (**name == '/')
  59. *name = *name + 1;
  60. } else if (**name != '/')
  61. /* CONNECT_PATH with missing root */
  62. error = prepend(name, *name - buf, "/", 1);
  63. return error;
  64. }
  65. /**
  66. * d_namespace_path - lookup a name associated with a given path
  67. * @path: path to lookup (NOT NULL)
  68. * @buf: buffer to store path to (NOT NULL)
  69. * @buflen: length of @buf
  70. * @name: Returns - pointer for start of path name with in @buf (NOT NULL)
  71. * @flags: flags controlling path lookup
  72. *
  73. * Handle path name lookup.
  74. *
  75. * Returns: %0 else error code if path lookup fails
  76. * When no error the path name is returned in @name which points to
  77. * to a position in @buf
  78. */
  79. static int d_namespace_path(const struct path *path, char *buf, int buflen,
  80. char **name, int flags)
  81. {
  82. char *res;
  83. int error = 0;
  84. int connected = 1;
  85. if (path->mnt->mnt_flags & MNT_INTERNAL) {
  86. /* it's not mounted anywhere */
  87. res = dentry_path(path->dentry, buf, buflen);
  88. *name = res;
  89. if (IS_ERR(res)) {
  90. *name = buf;
  91. return PTR_ERR(res);
  92. }
  93. if (path->dentry->d_sb->s_magic == PROC_SUPER_MAGIC &&
  94. strncmp(*name, "/sys/", 5) == 0) {
  95. /* TODO: convert over to using a per namespace
  96. * control instead of hard coded /proc
  97. */
  98. return prepend(name, *name - buf, "/proc", 5);
  99. } else
  100. return disconnect(path, buf, name, flags);
  101. return 0;
  102. }
  103. /* resolve paths relative to chroot?*/
  104. if (flags & PATH_CHROOT_REL) {
  105. struct path root;
  106. get_fs_root(current->fs, &root);
  107. res = __d_path(path, &root, buf, buflen);
  108. path_put(&root);
  109. } else {
  110. res = d_absolute_path(path, buf, buflen);
  111. if (!our_mnt(path->mnt))
  112. connected = 0;
  113. }
  114. /* handle error conditions - and still allow a partial path to
  115. * be returned.
  116. */
  117. if (!res || IS_ERR(res)) {
  118. if (PTR_ERR(res) == -ENAMETOOLONG)
  119. return -ENAMETOOLONG;
  120. connected = 0;
  121. res = dentry_path_raw(path->dentry, buf, buflen);
  122. if (IS_ERR(res)) {
  123. error = PTR_ERR(res);
  124. *name = buf;
  125. goto out;
  126. };
  127. } else if (!our_mnt(path->mnt))
  128. connected = 0;
  129. *name = res;
  130. /* Handle two cases:
  131. * 1. A deleted dentry && profile is not allowing mediation of deleted
  132. * 2. On some filesystems, newly allocated dentries appear to the
  133. * security_path hooks as a deleted dentry except without an inode
  134. * allocated.
  135. */
  136. if (d_unlinked(path->dentry) && d_is_positive(path->dentry) &&
  137. !(flags & PATH_MEDIATE_DELETED)) {
  138. error = -ENOENT;
  139. goto out;
  140. }
  141. if (!connected)
  142. error = disconnect(path, buf, name, flags);
  143. out:
  144. return error;
  145. }
  146. /**
  147. * get_name_to_buffer - get the pathname to a buffer ensure dir / is appended
  148. * @path: path to get name for (NOT NULL)
  149. * @flags: flags controlling path lookup
  150. * @buffer: buffer to put name in (NOT NULL)
  151. * @size: size of buffer
  152. * @name: Returns - contains position of path name in @buffer (NOT NULL)
  153. *
  154. * Returns: %0 else error on failure
  155. */
  156. static int get_name_to_buffer(const struct path *path, int flags, char *buffer,
  157. int size, char **name, const char **info)
  158. {
  159. int adjust = (flags & PATH_IS_DIR) ? 1 : 0;
  160. int error = d_namespace_path(path, buffer, size - adjust, name, flags);
  161. if (!error && (flags & PATH_IS_DIR) && (*name)[1] != '\0')
  162. /*
  163. * Append "/" to the pathname. The root directory is a special
  164. * case; it already ends in slash.
  165. */
  166. strcpy(&buffer[size - 2], "/");
  167. if (info && error) {
  168. if (error == -ENOENT)
  169. *info = "Failed name lookup - deleted entry";
  170. else if (error == -EACCES)
  171. *info = "Failed name lookup - disconnected path";
  172. else if (error == -ENAMETOOLONG)
  173. *info = "Failed name lookup - name too long";
  174. else
  175. *info = "Failed name lookup";
  176. }
  177. return error;
  178. }
  179. /**
  180. * aa_path_name - compute the pathname of a file
  181. * @path: path the file (NOT NULL)
  182. * @flags: flags controlling path name generation
  183. * @buffer: buffer that aa_get_name() allocated (NOT NULL)
  184. * @name: Returns - the generated path name if !error (NOT NULL)
  185. * @info: Returns - information on why the path lookup failed (MAYBE NULL)
  186. *
  187. * @name is a pointer to the beginning of the pathname (which usually differs
  188. * from the beginning of the buffer), or NULL. If there is an error @name
  189. * may contain a partial or invalid name that can be used for audit purposes,
  190. * but it can not be used for mediation.
  191. *
  192. * We need PATH_IS_DIR to indicate whether the file is a directory or not
  193. * because the file may not yet exist, and so we cannot check the inode's
  194. * file type.
  195. *
  196. * Returns: %0 else error code if could retrieve name
  197. */
  198. int aa_path_name(const struct path *path, int flags, char **buffer,
  199. const char **name, const char **info)
  200. {
  201. char *buf, *str = NULL;
  202. int size = 256;
  203. int error;
  204. *name = NULL;
  205. *buffer = NULL;
  206. for (;;) {
  207. /* freed by caller */
  208. buf = kmalloc(size, GFP_KERNEL);
  209. if (!buf)
  210. return -ENOMEM;
  211. error = get_name_to_buffer(path, flags, buf, size, &str, info);
  212. if (error != -ENAMETOOLONG)
  213. break;
  214. kfree(buf);
  215. size <<= 1;
  216. if (size > aa_g_path_max)
  217. return -ENAMETOOLONG;
  218. *info = NULL;
  219. }
  220. *buffer = buf;
  221. *name = str;
  222. return error;
  223. }