path.c 5.8 KB

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