realpath.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * security/tomoyo/realpath.c
  3. *
  4. * Get the canonicalized absolute pathnames. The basis for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  7. *
  8. * Version: 2.2.0 2009/04/01
  9. *
  10. */
  11. #include <linux/types.h>
  12. #include <linux/mount.h>
  13. #include <linux/mnt_namespace.h>
  14. #include <linux/fs_struct.h>
  15. #include <linux/hash.h>
  16. #include <linux/magic.h>
  17. #include "common.h"
  18. /**
  19. * tomoyo_encode: Convert binary string to ascii string.
  20. *
  21. * @buffer: Buffer for ASCII string.
  22. * @buflen: Size of @buffer.
  23. * @str: Binary string.
  24. *
  25. * Returns 0 on success, -ENOMEM otherwise.
  26. */
  27. int tomoyo_encode(char *buffer, int buflen, const char *str)
  28. {
  29. while (1) {
  30. const unsigned char c = *(unsigned char *) str++;
  31. if (tomoyo_is_valid(c)) {
  32. if (--buflen <= 0)
  33. break;
  34. *buffer++ = (char) c;
  35. if (c != '\\')
  36. continue;
  37. if (--buflen <= 0)
  38. break;
  39. *buffer++ = (char) c;
  40. continue;
  41. }
  42. if (!c) {
  43. if (--buflen <= 0)
  44. break;
  45. *buffer = '\0';
  46. return 0;
  47. }
  48. buflen -= 4;
  49. if (buflen <= 0)
  50. break;
  51. *buffer++ = '\\';
  52. *buffer++ = (c >> 6) + '0';
  53. *buffer++ = ((c >> 3) & 7) + '0';
  54. *buffer++ = (c & 7) + '0';
  55. }
  56. return -ENOMEM;
  57. }
  58. /**
  59. * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root.
  60. *
  61. * @path: Pointer to "struct path".
  62. * @newname: Pointer to buffer to return value in.
  63. * @newname_len: Size of @newname.
  64. *
  65. * Returns 0 on success, negative value otherwise.
  66. *
  67. * If dentry is a directory, trailing '/' is appended.
  68. * Characters out of 0x20 < c < 0x7F range are converted to
  69. * \ooo style octal string.
  70. * Character \ is converted to \\ string.
  71. */
  72. int tomoyo_realpath_from_path2(struct path *path, char *newname,
  73. int newname_len)
  74. {
  75. int error = -ENOMEM;
  76. struct dentry *dentry = path->dentry;
  77. char *sp;
  78. if (!dentry || !path->mnt || !newname || newname_len <= 2048)
  79. return -EINVAL;
  80. if (dentry->d_op && dentry->d_op->d_dname) {
  81. /* For "socket:[\$]" and "pipe:[\$]". */
  82. static const int offset = 1536;
  83. sp = dentry->d_op->d_dname(dentry, newname + offset,
  84. newname_len - offset);
  85. } else {
  86. /* Taken from d_namespace_path(). */
  87. struct path root;
  88. struct path ns_root = { };
  89. struct path tmp;
  90. read_lock(&current->fs->lock);
  91. root = current->fs->root;
  92. path_get(&root);
  93. read_unlock(&current->fs->lock);
  94. spin_lock(&vfsmount_lock);
  95. if (root.mnt && root.mnt->mnt_ns)
  96. ns_root.mnt = mntget(root.mnt->mnt_ns->root);
  97. if (ns_root.mnt)
  98. ns_root.dentry = dget(ns_root.mnt->mnt_root);
  99. spin_unlock(&vfsmount_lock);
  100. spin_lock(&dcache_lock);
  101. tmp = ns_root;
  102. sp = __d_path(path, &tmp, newname, newname_len);
  103. spin_unlock(&dcache_lock);
  104. path_put(&root);
  105. path_put(&ns_root);
  106. /* Prepend "/proc" prefix if using internal proc vfs mount. */
  107. if (!IS_ERR(sp) && (path->mnt->mnt_parent == path->mnt) &&
  108. (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) {
  109. sp -= 5;
  110. if (sp >= newname)
  111. memcpy(sp, "/proc", 5);
  112. else
  113. sp = ERR_PTR(-ENOMEM);
  114. }
  115. }
  116. if (IS_ERR(sp))
  117. error = PTR_ERR(sp);
  118. else
  119. error = tomoyo_encode(newname, sp - newname, sp);
  120. /* Append trailing '/' if dentry is a directory. */
  121. if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
  122. && *newname) {
  123. sp = newname + strlen(newname);
  124. if (*(sp - 1) != '/') {
  125. if (sp < newname + newname_len - 4) {
  126. *sp++ = '/';
  127. *sp = '\0';
  128. } else {
  129. error = -ENOMEM;
  130. }
  131. }
  132. }
  133. if (error)
  134. printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
  135. return error;
  136. }
  137. /**
  138. * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
  139. *
  140. * @path: Pointer to "struct path".
  141. *
  142. * Returns the realpath of the given @path on success, NULL otherwise.
  143. *
  144. * These functions use kzalloc(), so the caller must call kfree()
  145. * if these functions didn't return NULL.
  146. */
  147. char *tomoyo_realpath_from_path(struct path *path)
  148. {
  149. char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_KERNEL);
  150. BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
  151. <= TOMOYO_MAX_PATHNAME_LEN - 1);
  152. if (!buf)
  153. return NULL;
  154. if (tomoyo_realpath_from_path2(path, buf,
  155. TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
  156. return buf;
  157. kfree(buf);
  158. return NULL;
  159. }
  160. /**
  161. * tomoyo_realpath - Get realpath of a pathname.
  162. *
  163. * @pathname: The pathname to solve.
  164. *
  165. * Returns the realpath of @pathname on success, NULL otherwise.
  166. */
  167. char *tomoyo_realpath(const char *pathname)
  168. {
  169. struct path path;
  170. if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
  171. char *buf = tomoyo_realpath_from_path(&path);
  172. path_put(&path);
  173. return buf;
  174. }
  175. return NULL;
  176. }
  177. /**
  178. * tomoyo_realpath_nofollow - Get realpath of a pathname.
  179. *
  180. * @pathname: The pathname to solve.
  181. *
  182. * Returns the realpath of @pathname on success, NULL otherwise.
  183. */
  184. char *tomoyo_realpath_nofollow(const char *pathname)
  185. {
  186. struct path path;
  187. if (pathname && kern_path(pathname, 0, &path) == 0) {
  188. char *buf = tomoyo_realpath_from_path(&path);
  189. path_put(&path);
  190. return buf;
  191. }
  192. return NULL;
  193. }
  194. /* Memory allocated for non-string data. */
  195. static atomic_t tomoyo_policy_memory_size;
  196. /* Quota for holding policy. */
  197. static unsigned int tomoyo_quota_for_policy;
  198. /**
  199. * tomoyo_memory_ok - Check memory quota.
  200. *
  201. * @ptr: Pointer to allocated memory.
  202. *
  203. * Returns true on success, false otherwise.
  204. *
  205. * Caller holds tomoyo_policy_lock.
  206. * Memory pointed by @ptr will be zeroed on success.
  207. */
  208. bool tomoyo_memory_ok(void *ptr)
  209. {
  210. int allocated_len = ptr ? ksize(ptr) : 0;
  211. atomic_add(allocated_len, &tomoyo_policy_memory_size);
  212. if (ptr && (!tomoyo_quota_for_policy ||
  213. atomic_read(&tomoyo_policy_memory_size)
  214. <= tomoyo_quota_for_policy)) {
  215. memset(ptr, 0, allocated_len);
  216. return true;
  217. }
  218. printk(KERN_WARNING "ERROR: Out of memory "
  219. "for tomoyo_alloc_element().\n");
  220. if (!tomoyo_policy_loaded)
  221. panic("MAC Initialization failed.\n");
  222. return false;
  223. }
  224. /**
  225. * tomoyo_memory_free - Free memory for elements.
  226. *
  227. * @ptr: Pointer to allocated memory.
  228. */
  229. void tomoyo_memory_free(void *ptr)
  230. {
  231. atomic_sub(ksize(ptr), &tomoyo_policy_memory_size);
  232. kfree(ptr);
  233. }
  234. /*
  235. * tomoyo_name_list is used for holding string data used by TOMOYO.
  236. * Since same string data is likely used for multiple times (e.g.
  237. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  238. * "const struct tomoyo_path_info *".
  239. */
  240. struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  241. /* Lock for protecting tomoyo_name_list . */
  242. DEFINE_MUTEX(tomoyo_name_list_lock);
  243. /**
  244. * tomoyo_get_name - Allocate permanent memory for string data.
  245. *
  246. * @name: The string to store into the permernent memory.
  247. *
  248. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  249. */
  250. const struct tomoyo_path_info *tomoyo_get_name(const char *name)
  251. {
  252. struct tomoyo_name_entry *ptr;
  253. unsigned int hash;
  254. int len;
  255. int allocated_len;
  256. struct list_head *head;
  257. if (!name)
  258. return NULL;
  259. len = strlen(name) + 1;
  260. hash = full_name_hash((const unsigned char *) name, len - 1);
  261. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  262. mutex_lock(&tomoyo_name_list_lock);
  263. list_for_each_entry(ptr, head, list) {
  264. if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
  265. continue;
  266. atomic_inc(&ptr->users);
  267. goto out;
  268. }
  269. ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
  270. allocated_len = ptr ? ksize(ptr) : 0;
  271. if (!ptr || (tomoyo_quota_for_policy &&
  272. atomic_read(&tomoyo_policy_memory_size) + allocated_len
  273. > tomoyo_quota_for_policy)) {
  274. kfree(ptr);
  275. printk(KERN_WARNING "ERROR: Out of memory "
  276. "for tomoyo_get_name().\n");
  277. if (!tomoyo_policy_loaded)
  278. panic("MAC Initialization failed.\n");
  279. ptr = NULL;
  280. goto out;
  281. }
  282. atomic_add(allocated_len, &tomoyo_policy_memory_size);
  283. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  284. memmove((char *) ptr->entry.name, name, len);
  285. atomic_set(&ptr->users, 1);
  286. tomoyo_fill_path_info(&ptr->entry);
  287. list_add_tail(&ptr->list, head);
  288. out:
  289. mutex_unlock(&tomoyo_name_list_lock);
  290. return ptr ? &ptr->entry : NULL;
  291. }
  292. /**
  293. * tomoyo_realpath_init - Initialize realpath related code.
  294. */
  295. void __init tomoyo_realpath_init(void)
  296. {
  297. int i;
  298. BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
  299. for (i = 0; i < TOMOYO_MAX_HASH; i++)
  300. INIT_LIST_HEAD(&tomoyo_name_list[i]);
  301. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  302. tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
  303. /*
  304. * tomoyo_read_lock() is not needed because this function is
  305. * called before the first "delete" request.
  306. */
  307. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  308. if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
  309. panic("Can't register tomoyo_kernel_domain");
  310. }
  311. /**
  312. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  313. *
  314. * @head: Pointer to "struct tomoyo_io_buffer".
  315. *
  316. * Returns memory usage.
  317. */
  318. int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  319. {
  320. if (!head->read_eof) {
  321. const unsigned int policy
  322. = atomic_read(&tomoyo_policy_memory_size);
  323. char buffer[64];
  324. memset(buffer, 0, sizeof(buffer));
  325. if (tomoyo_quota_for_policy)
  326. snprintf(buffer, sizeof(buffer) - 1,
  327. " (Quota: %10u)",
  328. tomoyo_quota_for_policy);
  329. else
  330. buffer[0] = '\0';
  331. tomoyo_io_printf(head, "Policy: %10u%s\n", policy, buffer);
  332. tomoyo_io_printf(head, "Total: %10u\n", policy);
  333. head->read_eof = true;
  334. }
  335. return 0;
  336. }
  337. /**
  338. * tomoyo_write_memory_quota - Set memory quota.
  339. *
  340. * @head: Pointer to "struct tomoyo_io_buffer".
  341. *
  342. * Returns 0.
  343. */
  344. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  345. {
  346. char *data = head->write_buf;
  347. unsigned int size;
  348. if (sscanf(data, "Policy: %u", &size) == 1)
  349. tomoyo_quota_for_policy = size;
  350. return 0;
  351. }