realpath.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. struct path ns_root = {.mnt = NULL, .dentry = NULL};
  87. spin_lock(&dcache_lock);
  88. /* go to whatever namespace root we are under */
  89. sp = __d_path(path, &ns_root, newname, newname_len);
  90. spin_unlock(&dcache_lock);
  91. /* Prepend "/proc" prefix if using internal proc vfs mount. */
  92. if (!IS_ERR(sp) && (path->mnt->mnt_flags & MNT_INTERNAL) &&
  93. (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) {
  94. sp -= 5;
  95. if (sp >= newname)
  96. memcpy(sp, "/proc", 5);
  97. else
  98. sp = ERR_PTR(-ENOMEM);
  99. }
  100. }
  101. if (IS_ERR(sp))
  102. error = PTR_ERR(sp);
  103. else
  104. error = tomoyo_encode(newname, sp - newname, sp);
  105. /* Append trailing '/' if dentry is a directory. */
  106. if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
  107. && *newname) {
  108. sp = newname + strlen(newname);
  109. if (*(sp - 1) != '/') {
  110. if (sp < newname + newname_len - 4) {
  111. *sp++ = '/';
  112. *sp = '\0';
  113. } else {
  114. error = -ENOMEM;
  115. }
  116. }
  117. }
  118. if (error)
  119. printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
  120. return error;
  121. }
  122. /**
  123. * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
  124. *
  125. * @path: Pointer to "struct path".
  126. *
  127. * Returns the realpath of the given @path on success, NULL otherwise.
  128. *
  129. * These functions use kzalloc(), so the caller must call kfree()
  130. * if these functions didn't return NULL.
  131. */
  132. char *tomoyo_realpath_from_path(struct path *path)
  133. {
  134. char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_KERNEL);
  135. BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
  136. <= TOMOYO_MAX_PATHNAME_LEN - 1);
  137. if (!buf)
  138. return NULL;
  139. if (tomoyo_realpath_from_path2(path, buf,
  140. TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
  141. return buf;
  142. kfree(buf);
  143. return NULL;
  144. }
  145. /**
  146. * tomoyo_realpath - Get realpath of a pathname.
  147. *
  148. * @pathname: The pathname to solve.
  149. *
  150. * Returns the realpath of @pathname on success, NULL otherwise.
  151. */
  152. char *tomoyo_realpath(const char *pathname)
  153. {
  154. struct path path;
  155. if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
  156. char *buf = tomoyo_realpath_from_path(&path);
  157. path_put(&path);
  158. return buf;
  159. }
  160. return NULL;
  161. }
  162. /**
  163. * tomoyo_realpath_nofollow - Get realpath of a pathname.
  164. *
  165. * @pathname: The pathname to solve.
  166. *
  167. * Returns the realpath of @pathname on success, NULL otherwise.
  168. */
  169. char *tomoyo_realpath_nofollow(const char *pathname)
  170. {
  171. struct path path;
  172. if (pathname && kern_path(pathname, 0, &path) == 0) {
  173. char *buf = tomoyo_realpath_from_path(&path);
  174. path_put(&path);
  175. return buf;
  176. }
  177. return NULL;
  178. }
  179. /* Memory allocated for non-string data. */
  180. static atomic_t tomoyo_policy_memory_size;
  181. /* Quota for holding policy. */
  182. static unsigned int tomoyo_quota_for_policy;
  183. /**
  184. * tomoyo_memory_ok - Check memory quota.
  185. *
  186. * @ptr: Pointer to allocated memory.
  187. *
  188. * Returns true on success, false otherwise.
  189. *
  190. * Caller holds tomoyo_policy_lock.
  191. * Memory pointed by @ptr will be zeroed on success.
  192. */
  193. bool tomoyo_memory_ok(void *ptr)
  194. {
  195. int allocated_len = ptr ? ksize(ptr) : 0;
  196. atomic_add(allocated_len, &tomoyo_policy_memory_size);
  197. if (ptr && (!tomoyo_quota_for_policy ||
  198. atomic_read(&tomoyo_policy_memory_size)
  199. <= tomoyo_quota_for_policy)) {
  200. memset(ptr, 0, allocated_len);
  201. return true;
  202. }
  203. printk(KERN_WARNING "ERROR: Out of memory "
  204. "for tomoyo_alloc_element().\n");
  205. if (!tomoyo_policy_loaded)
  206. panic("MAC Initialization failed.\n");
  207. return false;
  208. }
  209. /**
  210. * tomoyo_memory_free - Free memory for elements.
  211. *
  212. * @ptr: Pointer to allocated memory.
  213. */
  214. void tomoyo_memory_free(void *ptr)
  215. {
  216. atomic_sub(ksize(ptr), &tomoyo_policy_memory_size);
  217. kfree(ptr);
  218. }
  219. /*
  220. * tomoyo_name_list is used for holding string data used by TOMOYO.
  221. * Since same string data is likely used for multiple times (e.g.
  222. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  223. * "const struct tomoyo_path_info *".
  224. */
  225. struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  226. /* Lock for protecting tomoyo_name_list . */
  227. DEFINE_MUTEX(tomoyo_name_list_lock);
  228. /**
  229. * tomoyo_get_name - Allocate permanent memory for string data.
  230. *
  231. * @name: The string to store into the permernent memory.
  232. *
  233. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  234. */
  235. const struct tomoyo_path_info *tomoyo_get_name(const char *name)
  236. {
  237. struct tomoyo_name_entry *ptr;
  238. unsigned int hash;
  239. int len;
  240. int allocated_len;
  241. struct list_head *head;
  242. if (!name)
  243. return NULL;
  244. len = strlen(name) + 1;
  245. hash = full_name_hash((const unsigned char *) name, len - 1);
  246. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  247. mutex_lock(&tomoyo_name_list_lock);
  248. list_for_each_entry(ptr, head, list) {
  249. if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
  250. continue;
  251. atomic_inc(&ptr->users);
  252. goto out;
  253. }
  254. ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
  255. allocated_len = ptr ? ksize(ptr) : 0;
  256. if (!ptr || (tomoyo_quota_for_policy &&
  257. atomic_read(&tomoyo_policy_memory_size) + allocated_len
  258. > tomoyo_quota_for_policy)) {
  259. kfree(ptr);
  260. printk(KERN_WARNING "ERROR: Out of memory "
  261. "for tomoyo_get_name().\n");
  262. if (!tomoyo_policy_loaded)
  263. panic("MAC Initialization failed.\n");
  264. ptr = NULL;
  265. goto out;
  266. }
  267. atomic_add(allocated_len, &tomoyo_policy_memory_size);
  268. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  269. memmove((char *) ptr->entry.name, name, len);
  270. atomic_set(&ptr->users, 1);
  271. tomoyo_fill_path_info(&ptr->entry);
  272. list_add_tail(&ptr->list, head);
  273. out:
  274. mutex_unlock(&tomoyo_name_list_lock);
  275. return ptr ? &ptr->entry : NULL;
  276. }
  277. /**
  278. * tomoyo_realpath_init - Initialize realpath related code.
  279. */
  280. void __init tomoyo_realpath_init(void)
  281. {
  282. int i;
  283. BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
  284. for (i = 0; i < TOMOYO_MAX_HASH; i++)
  285. INIT_LIST_HEAD(&tomoyo_name_list[i]);
  286. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  287. tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
  288. /*
  289. * tomoyo_read_lock() is not needed because this function is
  290. * called before the first "delete" request.
  291. */
  292. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  293. if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
  294. panic("Can't register tomoyo_kernel_domain");
  295. }
  296. /**
  297. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  298. *
  299. * @head: Pointer to "struct tomoyo_io_buffer".
  300. *
  301. * Returns memory usage.
  302. */
  303. int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  304. {
  305. if (!head->read_eof) {
  306. const unsigned int policy
  307. = atomic_read(&tomoyo_policy_memory_size);
  308. char buffer[64];
  309. memset(buffer, 0, sizeof(buffer));
  310. if (tomoyo_quota_for_policy)
  311. snprintf(buffer, sizeof(buffer) - 1,
  312. " (Quota: %10u)",
  313. tomoyo_quota_for_policy);
  314. else
  315. buffer[0] = '\0';
  316. tomoyo_io_printf(head, "Policy: %10u%s\n", policy, buffer);
  317. tomoyo_io_printf(head, "Total: %10u\n", policy);
  318. head->read_eof = true;
  319. }
  320. return 0;
  321. }
  322. /**
  323. * tomoyo_write_memory_quota - Set memory quota.
  324. *
  325. * @head: Pointer to "struct tomoyo_io_buffer".
  326. *
  327. * Returns 0.
  328. */
  329. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  330. {
  331. char *data = head->write_buf;
  332. unsigned int size;
  333. if (sscanf(data, "Policy: %u", &size) == 1)
  334. tomoyo_quota_for_policy = size;
  335. return 0;
  336. }