path.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, const char *disconnected)
  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 {
  61. if (**name != '/')
  62. /* CONNECT_PATH with missing root */
  63. error = prepend(name, *name - buf, "/", 1);
  64. if (!error && disconnected)
  65. error = prepend(name, *name - buf, disconnected,
  66. strlen(disconnected));
  67. }
  68. return error;
  69. }
  70. /**
  71. * d_namespace_path - lookup a name associated with a given path
  72. * @path: path to lookup (NOT NULL)
  73. * @buf: buffer to store path to (NOT NULL)
  74. * @name: Returns - pointer for start of path name with in @buf (NOT NULL)
  75. * @flags: flags controlling path lookup
  76. * @disconnected: string to prefix to disconnected paths
  77. *
  78. * Handle path name lookup.
  79. *
  80. * Returns: %0 else error code if path lookup fails
  81. * When no error the path name is returned in @name which points to
  82. * to a position in @buf
  83. */
  84. static int d_namespace_path(const struct path *path, char *buf, char **name,
  85. int flags, const char *disconnected)
  86. {
  87. char *res;
  88. int error = 0;
  89. int connected = 1;
  90. int isdir = (flags & PATH_IS_DIR) ? 1 : 0;
  91. int buflen = aa_g_path_max - isdir;
  92. if (path->mnt->mnt_flags & MNT_INTERNAL) {
  93. /* it's not mounted anywhere */
  94. res = dentry_path(path->dentry, buf, buflen);
  95. *name = res;
  96. if (IS_ERR(res)) {
  97. *name = buf;
  98. return PTR_ERR(res);
  99. }
  100. if (path->dentry->d_sb->s_magic == PROC_SUPER_MAGIC &&
  101. strncmp(*name, "/sys/", 5) == 0) {
  102. /* TODO: convert over to using a per namespace
  103. * control instead of hard coded /proc
  104. */
  105. error = prepend(name, *name - buf, "/proc", 5);
  106. goto out;
  107. } else
  108. error = disconnect(path, buf, name, flags,
  109. disconnected);
  110. goto out;
  111. }
  112. /* resolve paths relative to chroot?*/
  113. if (flags & PATH_CHROOT_REL) {
  114. struct path root;
  115. get_fs_root(current->fs, &root);
  116. res = __d_path(path, &root, buf, buflen);
  117. path_put(&root);
  118. } else {
  119. res = d_absolute_path(path, buf, buflen);
  120. if (!our_mnt(path->mnt))
  121. connected = 0;
  122. }
  123. /* handle error conditions - and still allow a partial path to
  124. * be returned.
  125. */
  126. if (!res || IS_ERR(res)) {
  127. if (PTR_ERR(res) == -ENAMETOOLONG) {
  128. error = -ENAMETOOLONG;
  129. *name = buf;
  130. goto out;
  131. }
  132. connected = 0;
  133. res = dentry_path_raw(path->dentry, buf, buflen);
  134. if (IS_ERR(res)) {
  135. error = PTR_ERR(res);
  136. *name = buf;
  137. goto out;
  138. };
  139. } else if (!our_mnt(path->mnt))
  140. connected = 0;
  141. *name = res;
  142. if (!connected)
  143. error = disconnect(path, buf, name, flags, disconnected);
  144. /* Handle two cases:
  145. * 1. A deleted dentry && profile is not allowing mediation of deleted
  146. * 2. On some filesystems, newly allocated dentries appear to the
  147. * security_path hooks as a deleted dentry except without an inode
  148. * allocated.
  149. */
  150. if (d_unlinked(path->dentry) && d_is_positive(path->dentry) &&
  151. !(flags & (PATH_MEDIATE_DELETED | PATH_DELEGATE_DELETED))) {
  152. error = -ENOENT;
  153. goto out;
  154. }
  155. out:
  156. /*
  157. * Append "/" to the pathname. The root directory is a special
  158. * case; it already ends in slash.
  159. */
  160. if (!error && isdir && ((*name)[1] != '\0' || (*name)[0] != '/'))
  161. strcpy(&buf[aa_g_path_max - 2], "/");
  162. return error;
  163. }
  164. /**
  165. * aa_path_name - get the pathname to a buffer ensure dir / is appended
  166. * @path: path the file (NOT NULL)
  167. * @flags: flags controlling path name generation
  168. * @buffer: buffer to put name in (NOT NULL)
  169. * @name: Returns - the generated path name if !error (NOT NULL)
  170. * @info: Returns - information on why the path lookup failed (MAYBE NULL)
  171. * @disconnected: string to prepend to disconnected paths
  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_path_name(const struct path *path, int flags, char *buffer,
  185. const char **name, const char **info, const char *disconnected)
  186. {
  187. char *str = NULL;
  188. int error = d_namespace_path(path, buffer, &str, flags, disconnected);
  189. if (info && error) {
  190. if (error == -ENOENT)
  191. *info = "Failed name lookup - deleted entry";
  192. else if (error == -EACCES)
  193. *info = "Failed name lookup - disconnected path";
  194. else if (error == -ENAMETOOLONG)
  195. *info = "Failed name lookup - name too long";
  196. else
  197. *info = "Failed name lookup";
  198. }
  199. *name = str;
  200. return error;
  201. }