realpath.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include "realpath.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. /* Taken from d_namespace_path(). */
  88. struct path root;
  89. struct path ns_root = { };
  90. struct path tmp;
  91. read_lock(&current->fs->lock);
  92. root = current->fs->root;
  93. path_get(&root);
  94. read_unlock(&current->fs->lock);
  95. spin_lock(&vfsmount_lock);
  96. if (root.mnt && root.mnt->mnt_ns)
  97. ns_root.mnt = mntget(root.mnt->mnt_ns->root);
  98. if (ns_root.mnt)
  99. ns_root.dentry = dget(ns_root.mnt->mnt_root);
  100. spin_unlock(&vfsmount_lock);
  101. spin_lock(&dcache_lock);
  102. tmp = ns_root;
  103. sp = __d_path(path, &tmp, newname, newname_len);
  104. spin_unlock(&dcache_lock);
  105. path_put(&root);
  106. path_put(&ns_root);
  107. /* Prepend "/proc" prefix if using internal proc vfs mount. */
  108. if (!IS_ERR(sp) && (path->mnt->mnt_parent == path->mnt) &&
  109. (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) {
  110. sp -= 5;
  111. if (sp >= newname)
  112. memcpy(sp, "/proc", 5);
  113. else
  114. sp = ERR_PTR(-ENOMEM);
  115. }
  116. }
  117. if (IS_ERR(sp))
  118. error = PTR_ERR(sp);
  119. else
  120. error = tomoyo_encode(newname, sp - newname, sp);
  121. /* Append trailing '/' if dentry is a directory. */
  122. if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
  123. && *newname) {
  124. sp = newname + strlen(newname);
  125. if (*(sp - 1) != '/') {
  126. if (sp < newname + newname_len - 4) {
  127. *sp++ = '/';
  128. *sp = '\0';
  129. } else {
  130. error = -ENOMEM;
  131. }
  132. }
  133. }
  134. if (error)
  135. printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
  136. return error;
  137. }
  138. /**
  139. * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
  140. *
  141. * @path: Pointer to "struct path".
  142. *
  143. * Returns the realpath of the given @path on success, NULL otherwise.
  144. *
  145. * These functions use kzalloc(), so the caller must call kfree()
  146. * if these functions didn't return NULL.
  147. */
  148. char *tomoyo_realpath_from_path(struct path *path)
  149. {
  150. char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_KERNEL);
  151. BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
  152. <= TOMOYO_MAX_PATHNAME_LEN - 1);
  153. if (!buf)
  154. return NULL;
  155. if (tomoyo_realpath_from_path2(path, buf,
  156. TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
  157. return buf;
  158. kfree(buf);
  159. return NULL;
  160. }
  161. /**
  162. * tomoyo_realpath - Get realpath of a pathname.
  163. *
  164. * @pathname: The pathname to solve.
  165. *
  166. * Returns the realpath of @pathname on success, NULL otherwise.
  167. */
  168. char *tomoyo_realpath(const char *pathname)
  169. {
  170. struct path path;
  171. if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
  172. char *buf = tomoyo_realpath_from_path(&path);
  173. path_put(&path);
  174. return buf;
  175. }
  176. return NULL;
  177. }
  178. /**
  179. * tomoyo_realpath_nofollow - Get realpath of a pathname.
  180. *
  181. * @pathname: The pathname to solve.
  182. *
  183. * Returns the realpath of @pathname on success, NULL otherwise.
  184. */
  185. char *tomoyo_realpath_nofollow(const char *pathname)
  186. {
  187. struct path path;
  188. if (pathname && kern_path(pathname, 0, &path) == 0) {
  189. char *buf = tomoyo_realpath_from_path(&path);
  190. path_put(&path);
  191. return buf;
  192. }
  193. return NULL;
  194. }
  195. /* Memory allocated for non-string data. */
  196. static unsigned int tomoyo_allocated_memory_for_elements;
  197. /* Quota for holding non-string data. */
  198. static unsigned int tomoyo_quota_for_elements;
  199. /**
  200. * tomoyo_memory_ok - Check memory quota.
  201. *
  202. * @ptr: Pointer to allocated memory.
  203. *
  204. * Returns true on success, false otherwise.
  205. *
  206. * Caller holds tomoyo_policy_lock.
  207. * Memory pointed by @ptr will be zeroed on success.
  208. */
  209. bool tomoyo_memory_ok(void *ptr)
  210. {
  211. int allocated_len = ptr ? ksize(ptr) : 0;
  212. bool result = false;
  213. if (!ptr || (tomoyo_quota_for_elements &&
  214. tomoyo_allocated_memory_for_elements
  215. + allocated_len > tomoyo_quota_for_elements)) {
  216. printk(KERN_WARNING "ERROR: Out of memory "
  217. "for tomoyo_alloc_element().\n");
  218. if (!tomoyo_policy_loaded)
  219. panic("MAC Initialization failed.\n");
  220. } else {
  221. result = true;
  222. tomoyo_allocated_memory_for_elements += allocated_len;
  223. memset(ptr, 0, allocated_len);
  224. }
  225. return result;
  226. }
  227. /* Memory allocated for string data in bytes. */
  228. static unsigned int tomoyo_allocated_memory_for_savename;
  229. /* Quota for holding string data in bytes. */
  230. static unsigned int tomoyo_quota_for_savename;
  231. /*
  232. * TOMOYO uses this hash only when appending a string into the string
  233. * table. Frequency of appending strings is very low. So we don't need
  234. * large (e.g. 64k) hash size. 256 will be sufficient.
  235. */
  236. #define TOMOYO_HASH_BITS 8
  237. #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
  238. /*
  239. * tomoyo_name_entry is a structure which is used for linking
  240. * "struct tomoyo_path_info" into tomoyo_name_list .
  241. *
  242. * Since tomoyo_name_list manages a list of strings which are shared by
  243. * multiple processes (whereas "struct tomoyo_path_info" inside
  244. * "struct tomoyo_path_info_with_data" is not shared), a reference counter will
  245. * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
  246. * when TOMOYO starts supporting garbage collector.
  247. */
  248. struct tomoyo_name_entry {
  249. struct list_head list;
  250. struct tomoyo_path_info entry;
  251. };
  252. /*
  253. * tomoyo_name_list is used for holding string data used by TOMOYO.
  254. * Since same string data is likely used for multiple times (e.g.
  255. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  256. * "const struct tomoyo_path_info *".
  257. */
  258. static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  259. /**
  260. * tomoyo_save_name - Allocate permanent memory for string data.
  261. *
  262. * @name: The string to store into the permernent memory.
  263. *
  264. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  265. */
  266. const struct tomoyo_path_info *tomoyo_save_name(const char *name)
  267. {
  268. static DEFINE_MUTEX(lock);
  269. struct tomoyo_name_entry *ptr;
  270. unsigned int hash;
  271. int len;
  272. int allocated_len;
  273. struct list_head *head;
  274. if (!name)
  275. return NULL;
  276. len = strlen(name) + 1;
  277. hash = full_name_hash((const unsigned char *) name, len - 1);
  278. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  279. mutex_lock(&lock);
  280. list_for_each_entry(ptr, head, list) {
  281. if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
  282. goto out;
  283. }
  284. ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
  285. allocated_len = ptr ? ksize(ptr) : 0;
  286. if (!ptr || (tomoyo_quota_for_savename &&
  287. tomoyo_allocated_memory_for_savename + allocated_len
  288. > tomoyo_quota_for_savename)) {
  289. kfree(ptr);
  290. printk(KERN_WARNING "ERROR: Out of memory "
  291. "for tomoyo_save_name().\n");
  292. if (!tomoyo_policy_loaded)
  293. panic("MAC Initialization failed.\n");
  294. ptr = NULL;
  295. goto out;
  296. }
  297. tomoyo_allocated_memory_for_savename += allocated_len;
  298. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  299. memmove((char *) ptr->entry.name, name, len);
  300. tomoyo_fill_path_info(&ptr->entry);
  301. list_add_tail(&ptr->list, head);
  302. out:
  303. mutex_unlock(&lock);
  304. return ptr ? &ptr->entry : NULL;
  305. }
  306. /**
  307. * tomoyo_realpath_init - Initialize realpath related code.
  308. */
  309. void __init tomoyo_realpath_init(void)
  310. {
  311. int i;
  312. BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
  313. for (i = 0; i < TOMOYO_MAX_HASH; i++)
  314. INIT_LIST_HEAD(&tomoyo_name_list[i]);
  315. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  316. tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME);
  317. /*
  318. * tomoyo_read_lock() is not needed because this function is
  319. * called before the first "delete" request.
  320. */
  321. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  322. if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
  323. panic("Can't register tomoyo_kernel_domain");
  324. }
  325. /**
  326. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  327. *
  328. * @head: Pointer to "struct tomoyo_io_buffer".
  329. *
  330. * Returns memory usage.
  331. */
  332. int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  333. {
  334. if (!head->read_eof) {
  335. const unsigned int shared
  336. = tomoyo_allocated_memory_for_savename;
  337. const unsigned int private
  338. = tomoyo_allocated_memory_for_elements;
  339. char buffer[64];
  340. memset(buffer, 0, sizeof(buffer));
  341. if (tomoyo_quota_for_savename)
  342. snprintf(buffer, sizeof(buffer) - 1,
  343. " (Quota: %10u)",
  344. tomoyo_quota_for_savename);
  345. else
  346. buffer[0] = '\0';
  347. tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
  348. if (tomoyo_quota_for_elements)
  349. snprintf(buffer, sizeof(buffer) - 1,
  350. " (Quota: %10u)",
  351. tomoyo_quota_for_elements);
  352. else
  353. buffer[0] = '\0';
  354. tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
  355. tomoyo_io_printf(head, "Total: %10u\n", shared + private);
  356. head->read_eof = true;
  357. }
  358. return 0;
  359. }
  360. /**
  361. * tomoyo_write_memory_quota - Set memory quota.
  362. *
  363. * @head: Pointer to "struct tomoyo_io_buffer".
  364. *
  365. * Returns 0.
  366. */
  367. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  368. {
  369. char *data = head->write_buf;
  370. unsigned int size;
  371. if (sscanf(data, "Shared: %u", &size) == 1)
  372. tomoyo_quota_for_savename = size;
  373. else if (sscanf(data, "Private: %u", &size) == 1)
  374. tomoyo_quota_for_elements = size;
  375. return 0;
  376. }