realpath.c 9.5 KB

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