path.c 5.8 KB

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