path.c 6.4 KB

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