path.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /**
  37. * d_namespace_path - lookup a name associated with a given path
  38. * @path: path to lookup (NOT NULL)
  39. * @buf: buffer to store path to (NOT NULL)
  40. * @buflen: length of @buf
  41. * @name: Returns - pointer for start of path name with in @buf (NOT NULL)
  42. * @flags: flags controlling path lookup
  43. *
  44. * Handle path name lookup.
  45. *
  46. * Returns: %0 else error code if path lookup fails
  47. * When no error the path name is returned in @name which points to
  48. * to a position in @buf
  49. */
  50. static int d_namespace_path(struct path *path, char *buf, int buflen,
  51. char **name, int flags)
  52. {
  53. char *res;
  54. int error = 0;
  55. int connected = 1;
  56. if (path->mnt->mnt_flags & MNT_INTERNAL) {
  57. /* it's not mounted anywhere */
  58. res = dentry_path(path->dentry, buf, buflen);
  59. *name = res;
  60. if (IS_ERR(res)) {
  61. *name = buf;
  62. return PTR_ERR(res);
  63. }
  64. if (path->dentry->d_sb->s_magic == PROC_SUPER_MAGIC &&
  65. strncmp(*name, "/sys/", 5) == 0) {
  66. /* TODO: convert over to using a per namespace
  67. * control instead of hard coded /proc
  68. */
  69. return prepend(name, *name - buf, "/proc", 5);
  70. }
  71. return 0;
  72. }
  73. /* resolve paths relative to chroot?*/
  74. if (flags & PATH_CHROOT_REL) {
  75. struct path root;
  76. get_fs_root(current->fs, &root);
  77. res = __d_path(path, &root, buf, buflen);
  78. path_put(&root);
  79. } else {
  80. res = d_absolute_path(path, buf, buflen);
  81. if (!our_mnt(path->mnt))
  82. connected = 0;
  83. }
  84. /* handle error conditions - and still allow a partial path to
  85. * be returned.
  86. */
  87. if (!res || IS_ERR(res)) {
  88. if (PTR_ERR(res) == -ENAMETOOLONG)
  89. return -ENAMETOOLONG;
  90. connected = 0;
  91. res = dentry_path_raw(path->dentry, buf, buflen);
  92. if (IS_ERR(res)) {
  93. error = PTR_ERR(res);
  94. *name = buf;
  95. goto out;
  96. };
  97. } else if (!our_mnt(path->mnt))
  98. connected = 0;
  99. *name = res;
  100. /* Handle two cases:
  101. * 1. A deleted dentry && profile is not allowing mediation of deleted
  102. * 2. On some filesystems, newly allocated dentries appear to the
  103. * security_path hooks as a deleted dentry except without an inode
  104. * allocated.
  105. */
  106. if (d_unlinked(path->dentry) && d_is_positive(path->dentry) &&
  107. !(flags & PATH_MEDIATE_DELETED)) {
  108. error = -ENOENT;
  109. goto out;
  110. }
  111. /* If the path is not connected to the expected root,
  112. * check if it is a sysctl and handle specially else remove any
  113. * leading / that __d_path may have returned.
  114. * Unless
  115. * specifically directed to connect the path,
  116. * OR
  117. * if in a chroot and doing chroot relative paths and the path
  118. * resolves to the namespace root (would be connected outside
  119. * of chroot) and specifically directed to connect paths to
  120. * namespace root.
  121. */
  122. if (!connected) {
  123. if (!(flags & PATH_CONNECT_PATH) &&
  124. !(((flags & CHROOT_NSCONNECT) == CHROOT_NSCONNECT) &&
  125. our_mnt(path->mnt))) {
  126. /* disconnected path, don't return pathname starting
  127. * with '/'
  128. */
  129. error = -EACCES;
  130. if (*res == '/')
  131. *name = res + 1;
  132. }
  133. }
  134. out:
  135. return error;
  136. }
  137. /**
  138. * get_name_to_buffer - get the pathname to a buffer ensure dir / is appended
  139. * @path: path to get name for (NOT NULL)
  140. * @flags: flags controlling path lookup
  141. * @buffer: buffer to put name in (NOT NULL)
  142. * @size: size of buffer
  143. * @name: Returns - contains position of path name in @buffer (NOT NULL)
  144. *
  145. * Returns: %0 else error on failure
  146. */
  147. static int get_name_to_buffer(struct path *path, int flags, char *buffer,
  148. int size, char **name, const char **info)
  149. {
  150. int adjust = (flags & PATH_IS_DIR) ? 1 : 0;
  151. int error = d_namespace_path(path, buffer, size - adjust, name, flags);
  152. if (!error && (flags & PATH_IS_DIR) && (*name)[1] != '\0')
  153. /*
  154. * Append "/" to the pathname. The root directory is a special
  155. * case; it already ends in slash.
  156. */
  157. strcpy(&buffer[size - 2], "/");
  158. if (info && error) {
  159. if (error == -ENOENT)
  160. *info = "Failed name lookup - deleted entry";
  161. else if (error == -EACCES)
  162. *info = "Failed name lookup - disconnected path";
  163. else if (error == -ENAMETOOLONG)
  164. *info = "Failed name lookup - name too long";
  165. else
  166. *info = "Failed name lookup";
  167. }
  168. return error;
  169. }
  170. /**
  171. * aa_path_name - compute the pathname of a file
  172. * @path: path the file (NOT NULL)
  173. * @flags: flags controlling path name generation
  174. * @buffer: buffer that aa_get_name() allocated (NOT NULL)
  175. * @name: Returns - the generated path name if !error (NOT NULL)
  176. * @info: Returns - information on why the path lookup failed (MAYBE 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_path_name(struct path *path, int flags, char **buffer, const char **name,
  190. const char **info)
  191. {
  192. char *buf, *str = NULL;
  193. int size = 256;
  194. int error;
  195. *name = NULL;
  196. *buffer = NULL;
  197. for (;;) {
  198. /* freed by caller */
  199. buf = kmalloc(size, GFP_KERNEL);
  200. if (!buf)
  201. return -ENOMEM;
  202. error = get_name_to_buffer(path, flags, buf, size, &str, info);
  203. if (error != -ENAMETOOLONG)
  204. break;
  205. kfree(buf);
  206. size <<= 1;
  207. if (size > aa_g_path_max)
  208. return -ENAMETOOLONG;
  209. *info = NULL;
  210. }
  211. *buffer = buf;
  212. *name = str;
  213. return error;
  214. }