path.c 5.9 KB

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