path.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/mnt_namespace.h>
  16. #include <linux/mount.h>
  17. #include <linux/namei.h>
  18. #include <linux/nsproxy.h>
  19. #include <linux/path.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/fs_struct.h>
  23. #include "include/apparmor.h"
  24. #include "include/path.h"
  25. #include "include/policy.h"
  26. /* modified from dcache.c */
  27. static int prepend(char **buffer, int buflen, const char *str, int namelen)
  28. {
  29. buflen -= namelen;
  30. if (buflen < 0)
  31. return -ENAMETOOLONG;
  32. *buffer -= namelen;
  33. memcpy(*buffer, str, namelen);
  34. return 0;
  35. }
  36. #define CHROOT_NSCONNECT (PATH_CHROOT_REL | PATH_CHROOT_NSCONNECT)
  37. /**
  38. * d_namespace_path - lookup a name associated with a given path
  39. * @path: path to lookup (NOT NULL)
  40. * @buf: buffer to store path to (NOT NULL)
  41. * @buflen: length of @buf
  42. * @name: Returns - pointer for start of path name with in @buf (NOT NULL)
  43. * @flags: flags controlling path lookup
  44. *
  45. * Handle path name lookup.
  46. *
  47. * Returns: %0 else error code if path lookup fails
  48. * When no error the path name is returned in @name which points to
  49. * to a position in @buf
  50. */
  51. static int d_namespace_path(struct path *path, char *buf, int buflen,
  52. char **name, int flags)
  53. {
  54. struct path root, tmp;
  55. char *res;
  56. int deleted, connected;
  57. int error = 0;
  58. /* Get the root we want to resolve too */
  59. if (flags & PATH_CHROOT_REL) {
  60. /* resolve paths relative to chroot */
  61. read_lock(&current->fs->lock);
  62. root = current->fs->root;
  63. /* released below */
  64. path_get(&root);
  65. read_unlock(&current->fs->lock);
  66. } else {
  67. /* resolve paths relative to namespace */
  68. root.mnt = current->nsproxy->mnt_ns->root;
  69. root.dentry = root.mnt->mnt_root;
  70. /* released below */
  71. path_get(&root);
  72. }
  73. spin_lock(&dcache_lock);
  74. /* There is a race window between path lookup here and the
  75. * need to strip the " (deleted) string that __d_path applies
  76. * Detect the race and relookup the path
  77. *
  78. * The stripping of (deleted) is a hack that could be removed
  79. * with an updated __d_path
  80. */
  81. do {
  82. tmp = root;
  83. deleted = d_unlinked(path->dentry);
  84. res = __d_path(path, &tmp, buf, buflen);
  85. } while (deleted != d_unlinked(path->dentry));
  86. spin_unlock(&dcache_lock);
  87. *name = res;
  88. /* handle error conditions - and still allow a partial path to
  89. * be returned.
  90. */
  91. if (IS_ERR(res)) {
  92. error = PTR_ERR(res);
  93. *name = buf;
  94. goto out;
  95. }
  96. if (deleted) {
  97. /* On some filesystems, newly allocated dentries appear to the
  98. * security_path hooks as a deleted dentry except without an
  99. * inode allocated.
  100. *
  101. * Remove the appended deleted text and return as string for
  102. * normal mediation, or auditing. The (deleted) string is
  103. * guaranteed to be added in this case, so just strip it.
  104. */
  105. buf[buflen - 11] = 0; /* - (len(" (deleted)") +\0) */
  106. if (path->dentry->d_inode && !(flags & PATH_MEDIATE_DELETED)) {
  107. error = -ENOENT;
  108. goto out;
  109. }
  110. }
  111. /* Determine if the path is connected to the expected root */
  112. connected = tmp.dentry == root.dentry && tmp.mnt == root.mnt;
  113. /* If the path is not connected,
  114. * check if it is a sysctl and handle specially else remove any
  115. * leading / that __d_path may have returned.
  116. * Unless
  117. * specifically directed to connect the path,
  118. * OR
  119. * if in a chroot and doing chroot relative paths and the path
  120. * resolves to the namespace root (would be connected outside
  121. * of chroot) and specifically directed to connect paths to
  122. * namespace root.
  123. */
  124. if (!connected) {
  125. /* is the disconnect path a sysctl? */
  126. if (tmp.dentry->d_sb->s_magic == PROC_SUPER_MAGIC &&
  127. strncmp(*name, "/sys/", 5) == 0) {
  128. /* TODO: convert over to using a per namespace
  129. * control instead of hard coded /proc
  130. */
  131. error = prepend(name, *name - buf, "/proc", 5);
  132. } else if (!(flags & PATH_CONNECT_PATH) &&
  133. !(((flags & CHROOT_NSCONNECT) == CHROOT_NSCONNECT) &&
  134. (tmp.mnt == current->nsproxy->mnt_ns->root &&
  135. tmp.dentry == tmp.mnt->mnt_root))) {
  136. /* disconnected path, don't return pathname starting
  137. * with '/'
  138. */
  139. error = -ESTALE;
  140. if (*res == '/')
  141. *name = res + 1;
  142. }
  143. }
  144. out:
  145. path_put(&root);
  146. return error;
  147. }
  148. /**
  149. * get_name_to_buffer - get the pathname to a buffer ensure dir / is appended
  150. * @path: path to get name for (NOT NULL)
  151. * @flags: flags controlling path lookup
  152. * @buffer: buffer to put name in (NOT NULL)
  153. * @size: size of buffer
  154. * @name: Returns - contains position of path name in @buffer (NOT NULL)
  155. *
  156. * Returns: %0 else error on failure
  157. */
  158. static int get_name_to_buffer(struct path *path, int flags, char *buffer,
  159. int size, char **name)
  160. {
  161. int adjust = (flags & PATH_IS_DIR) ? 1 : 0;
  162. int error = d_namespace_path(path, buffer, size - adjust, name, flags);
  163. if (!error && (flags & PATH_IS_DIR) && (*name)[1] != '\0')
  164. /*
  165. * Append "/" to the pathname. The root directory is a special
  166. * case; it already ends in slash.
  167. */
  168. strcpy(&buffer[size - 2], "/");
  169. return error;
  170. }
  171. /**
  172. * aa_get_name - compute the pathname of a file
  173. * @path: path the file (NOT NULL)
  174. * @flags: flags controlling path name generation
  175. * @buffer: buffer that aa_get_name() allocated (NOT NULL)
  176. * @name: Returns - the generated path name if !error (NOT NULL)
  177. *
  178. * @name is a pointer to the beginning of the pathname (which usually differs
  179. * from the beginning of the buffer), or NULL. If there is an error @name
  180. * may contain a partial or invalid name that can be used for audit purposes,
  181. * but it can not be used for mediation.
  182. *
  183. * We need PATH_IS_DIR to indicate whether the file is a directory or not
  184. * because the file may not yet exist, and so we cannot check the inode's
  185. * file type.
  186. *
  187. * Returns: %0 else error code if could retrieve name
  188. */
  189. int aa_get_name(struct path *path, int flags, char **buffer, const char **name)
  190. {
  191. char *buf, *str = NULL;
  192. int size = 256;
  193. int error;
  194. *name = NULL;
  195. *buffer = NULL;
  196. for (;;) {
  197. /* freed by caller */
  198. buf = kmalloc(size, GFP_KERNEL);
  199. if (!buf)
  200. return -ENOMEM;
  201. error = get_name_to_buffer(path, flags, buf, size, &str);
  202. if (error != -ENAMETOOLONG)
  203. break;
  204. kfree(buf);
  205. size <<= 1;
  206. if (size > aa_g_path_max)
  207. return -ENAMETOOLONG;
  208. }
  209. *buffer = buf;
  210. *name = str;
  211. return error;
  212. }