realpath.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 "common.h"
  16. #include "realpath.h"
  17. /**
  18. * tomoyo_encode: Convert binary string to ascii string.
  19. *
  20. * @buffer: Buffer for ASCII string.
  21. * @buflen: Size of @buffer.
  22. * @str: Binary string.
  23. *
  24. * Returns 0 on success, -ENOMEM otherwise.
  25. */
  26. int tomoyo_encode(char *buffer, int buflen, const char *str)
  27. {
  28. while (1) {
  29. const unsigned char c = *(unsigned char *) str++;
  30. if (tomoyo_is_valid(c)) {
  31. if (--buflen <= 0)
  32. break;
  33. *buffer++ = (char) c;
  34. if (c != '\\')
  35. continue;
  36. if (--buflen <= 0)
  37. break;
  38. *buffer++ = (char) c;
  39. continue;
  40. }
  41. if (!c) {
  42. if (--buflen <= 0)
  43. break;
  44. *buffer = '\0';
  45. return 0;
  46. }
  47. buflen -= 4;
  48. if (buflen <= 0)
  49. break;
  50. *buffer++ = '\\';
  51. *buffer++ = (c >> 6) + '0';
  52. *buffer++ = ((c >> 3) & 7) + '0';
  53. *buffer++ = (c & 7) + '0';
  54. }
  55. return -ENOMEM;
  56. }
  57. /**
  58. * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root.
  59. *
  60. * @path: Pointer to "struct path".
  61. * @newname: Pointer to buffer to return value in.
  62. * @newname_len: Size of @newname.
  63. *
  64. * Returns 0 on success, negative value otherwise.
  65. *
  66. * If dentry is a directory, trailing '/' is appended.
  67. * Characters out of 0x20 < c < 0x7F range are converted to
  68. * \ooo style octal string.
  69. * Character \ is converted to \\ string.
  70. */
  71. int tomoyo_realpath_from_path2(struct path *path, char *newname,
  72. int newname_len)
  73. {
  74. int error = -ENOMEM;
  75. struct dentry *dentry = path->dentry;
  76. char *sp;
  77. if (!dentry || !path->mnt || !newname || newname_len <= 2048)
  78. return -EINVAL;
  79. if (dentry->d_op && dentry->d_op->d_dname) {
  80. /* For "socket:[\$]" and "pipe:[\$]". */
  81. static const int offset = 1536;
  82. sp = dentry->d_op->d_dname(dentry, newname + offset,
  83. newname_len - offset);
  84. } else {
  85. /* Taken from d_namespace_path(). */
  86. struct path root;
  87. struct path ns_root = { };
  88. struct path tmp;
  89. read_lock(&current->fs->lock);
  90. root = current->fs->root;
  91. path_get(&root);
  92. read_unlock(&current->fs->lock);
  93. spin_lock(&vfsmount_lock);
  94. if (root.mnt && root.mnt->mnt_ns)
  95. ns_root.mnt = mntget(root.mnt->mnt_ns->root);
  96. if (ns_root.mnt)
  97. ns_root.dentry = dget(ns_root.mnt->mnt_root);
  98. spin_unlock(&vfsmount_lock);
  99. spin_lock(&dcache_lock);
  100. tmp = ns_root;
  101. sp = __d_path(path, &tmp, newname, newname_len);
  102. spin_unlock(&dcache_lock);
  103. path_put(&root);
  104. path_put(&ns_root);
  105. }
  106. if (IS_ERR(sp))
  107. error = PTR_ERR(sp);
  108. else
  109. error = tomoyo_encode(newname, sp - newname, sp);
  110. /* Append trailing '/' if dentry is a directory. */
  111. if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)
  112. && *newname) {
  113. sp = newname + strlen(newname);
  114. if (*(sp - 1) != '/') {
  115. if (sp < newname + newname_len - 4) {
  116. *sp++ = '/';
  117. *sp = '\0';
  118. } else {
  119. error = -ENOMEM;
  120. }
  121. }
  122. }
  123. if (error)
  124. printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n");
  125. return error;
  126. }
  127. /**
  128. * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
  129. *
  130. * @path: Pointer to "struct path".
  131. *
  132. * Returns the realpath of the given @path on success, NULL otherwise.
  133. *
  134. * These functions use tomoyo_alloc(), so the caller must call tomoyo_free()
  135. * if these functions didn't return NULL.
  136. */
  137. char *tomoyo_realpath_from_path(struct path *path)
  138. {
  139. char *buf = tomoyo_alloc(sizeof(struct tomoyo_page_buffer));
  140. BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer)
  141. <= TOMOYO_MAX_PATHNAME_LEN - 1);
  142. if (!buf)
  143. return NULL;
  144. if (tomoyo_realpath_from_path2(path, buf,
  145. TOMOYO_MAX_PATHNAME_LEN - 1) == 0)
  146. return buf;
  147. tomoyo_free(buf);
  148. return NULL;
  149. }
  150. /**
  151. * tomoyo_realpath - Get realpath of a pathname.
  152. *
  153. * @pathname: The pathname to solve.
  154. *
  155. * Returns the realpath of @pathname on success, NULL otherwise.
  156. */
  157. char *tomoyo_realpath(const char *pathname)
  158. {
  159. struct path path;
  160. if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
  161. char *buf = tomoyo_realpath_from_path(&path);
  162. path_put(&path);
  163. return buf;
  164. }
  165. return NULL;
  166. }
  167. /**
  168. * tomoyo_realpath_nofollow - Get realpath of a pathname.
  169. *
  170. * @pathname: The pathname to solve.
  171. *
  172. * Returns the realpath of @pathname on success, NULL otherwise.
  173. */
  174. char *tomoyo_realpath_nofollow(const char *pathname)
  175. {
  176. struct path path;
  177. if (pathname && kern_path(pathname, 0, &path) == 0) {
  178. char *buf = tomoyo_realpath_from_path(&path);
  179. path_put(&path);
  180. return buf;
  181. }
  182. return NULL;
  183. }
  184. /* Memory allocated for non-string data. */
  185. static unsigned int tomoyo_allocated_memory_for_elements;
  186. /* Quota for holding non-string data. */
  187. static unsigned int tomoyo_quota_for_elements;
  188. /**
  189. * tomoyo_alloc_element - Allocate permanent memory for structures.
  190. *
  191. * @size: Size in bytes.
  192. *
  193. * Returns pointer to allocated memory on success, NULL otherwise.
  194. *
  195. * Memory has to be zeroed.
  196. * The RAM is chunked, so NEVER try to kfree() the returned pointer.
  197. */
  198. void *tomoyo_alloc_element(const unsigned int size)
  199. {
  200. static char *buf;
  201. static DEFINE_MUTEX(lock);
  202. static unsigned int buf_used_len = PATH_MAX;
  203. char *ptr = NULL;
  204. /*Assumes sizeof(void *) >= sizeof(long) is true. */
  205. const unsigned int word_aligned_size
  206. = roundup(size, max(sizeof(void *), sizeof(long)));
  207. if (word_aligned_size > PATH_MAX)
  208. return NULL;
  209. mutex_lock(&lock);
  210. if (buf_used_len + word_aligned_size > PATH_MAX) {
  211. if (!tomoyo_quota_for_elements ||
  212. tomoyo_allocated_memory_for_elements
  213. + PATH_MAX <= tomoyo_quota_for_elements)
  214. ptr = kzalloc(PATH_MAX, GFP_KERNEL);
  215. if (!ptr) {
  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. buf = ptr;
  222. tomoyo_allocated_memory_for_elements += PATH_MAX;
  223. buf_used_len = word_aligned_size;
  224. ptr = buf;
  225. }
  226. } else if (word_aligned_size) {
  227. int i;
  228. ptr = buf + buf_used_len;
  229. buf_used_len += word_aligned_size;
  230. for (i = 0; i < word_aligned_size; i++) {
  231. if (!ptr[i])
  232. continue;
  233. printk(KERN_ERR "WARNING: Reserved memory was tainted! "
  234. "The system might go wrong.\n");
  235. ptr[i] = '\0';
  236. }
  237. }
  238. mutex_unlock(&lock);
  239. return ptr;
  240. }
  241. /* Memory allocated for string data in bytes. */
  242. static unsigned int tomoyo_allocated_memory_for_savename;
  243. /* Quota for holding string data in bytes. */
  244. static unsigned int tomoyo_quota_for_savename;
  245. /*
  246. * TOMOYO uses this hash only when appending a string into the string
  247. * table. Frequency of appending strings is very low. So we don't need
  248. * large (e.g. 64k) hash size. 256 will be sufficient.
  249. */
  250. #define TOMOYO_MAX_HASH 256
  251. /*
  252. * tomoyo_name_entry is a structure which is used for linking
  253. * "struct tomoyo_path_info" into tomoyo_name_list .
  254. *
  255. * Since tomoyo_name_list manages a list of strings which are shared by
  256. * multiple processes (whereas "struct tomoyo_path_info" inside
  257. * "struct tomoyo_path_info_with_data" is not shared), a reference counter will
  258. * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
  259. * when TOMOYO starts supporting garbage collector.
  260. */
  261. struct tomoyo_name_entry {
  262. struct list_head list;
  263. struct tomoyo_path_info entry;
  264. };
  265. /* Structure for available memory region. */
  266. struct tomoyo_free_memory_block_list {
  267. struct list_head list;
  268. char *ptr; /* Pointer to a free area. */
  269. int len; /* Length of the area. */
  270. };
  271. /*
  272. * tomoyo_name_list is used for holding string data used by TOMOYO.
  273. * Since same string data is likely used for multiple times (e.g.
  274. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  275. * "const struct tomoyo_path_info *".
  276. */
  277. static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  278. /**
  279. * tomoyo_save_name - Allocate permanent memory for string data.
  280. *
  281. * @name: The string to store into the permernent memory.
  282. *
  283. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  284. *
  285. * The RAM is shared, so NEVER try to modify or kfree() the returned name.
  286. */
  287. const struct tomoyo_path_info *tomoyo_save_name(const char *name)
  288. {
  289. static LIST_HEAD(fmb_list);
  290. static DEFINE_MUTEX(lock);
  291. struct tomoyo_name_entry *ptr;
  292. unsigned int hash;
  293. /* fmb contains available size in bytes.
  294. fmb is removed from the fmb_list when fmb->len becomes 0. */
  295. struct tomoyo_free_memory_block_list *fmb;
  296. int len;
  297. char *cp;
  298. if (!name)
  299. return NULL;
  300. len = strlen(name) + 1;
  301. if (len > TOMOYO_MAX_PATHNAME_LEN) {
  302. printk(KERN_WARNING "ERROR: Name too long "
  303. "for tomoyo_save_name().\n");
  304. return NULL;
  305. }
  306. hash = full_name_hash((const unsigned char *) name, len - 1);
  307. mutex_lock(&lock);
  308. list_for_each_entry(ptr, &tomoyo_name_list[hash % TOMOYO_MAX_HASH],
  309. list) {
  310. if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
  311. goto out;
  312. }
  313. list_for_each_entry(fmb, &fmb_list, list) {
  314. if (len <= fmb->len)
  315. goto ready;
  316. }
  317. if (!tomoyo_quota_for_savename ||
  318. tomoyo_allocated_memory_for_savename + PATH_MAX
  319. <= tomoyo_quota_for_savename)
  320. cp = kzalloc(PATH_MAX, GFP_KERNEL);
  321. else
  322. cp = NULL;
  323. fmb = kzalloc(sizeof(*fmb), GFP_KERNEL);
  324. if (!cp || !fmb) {
  325. kfree(cp);
  326. kfree(fmb);
  327. printk(KERN_WARNING "ERROR: Out of memory "
  328. "for tomoyo_save_name().\n");
  329. if (!tomoyo_policy_loaded)
  330. panic("MAC Initialization failed.\n");
  331. ptr = NULL;
  332. goto out;
  333. }
  334. tomoyo_allocated_memory_for_savename += PATH_MAX;
  335. list_add(&fmb->list, &fmb_list);
  336. fmb->ptr = cp;
  337. fmb->len = PATH_MAX;
  338. ready:
  339. ptr = tomoyo_alloc_element(sizeof(*ptr));
  340. if (!ptr)
  341. goto out;
  342. ptr->entry.name = fmb->ptr;
  343. memmove(fmb->ptr, name, len);
  344. tomoyo_fill_path_info(&ptr->entry);
  345. fmb->ptr += len;
  346. fmb->len -= len;
  347. list_add_tail(&ptr->list, &tomoyo_name_list[hash % TOMOYO_MAX_HASH]);
  348. if (fmb->len == 0) {
  349. list_del(&fmb->list);
  350. kfree(fmb);
  351. }
  352. out:
  353. mutex_unlock(&lock);
  354. return ptr ? &ptr->entry : NULL;
  355. }
  356. /**
  357. * tomoyo_realpath_init - Initialize realpath related code.
  358. */
  359. void __init tomoyo_realpath_init(void)
  360. {
  361. int i;
  362. BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
  363. for (i = 0; i < TOMOYO_MAX_HASH; i++)
  364. INIT_LIST_HEAD(&tomoyo_name_list[i]);
  365. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  366. tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME);
  367. list_add_tail(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  368. down_read(&tomoyo_domain_list_lock);
  369. if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
  370. panic("Can't register tomoyo_kernel_domain");
  371. up_read(&tomoyo_domain_list_lock);
  372. }
  373. /* Memory allocated for temporary purpose. */
  374. static atomic_t tomoyo_dynamic_memory_size;
  375. /**
  376. * tomoyo_alloc - Allocate memory for temporary purpose.
  377. *
  378. * @size: Size in bytes.
  379. *
  380. * Returns pointer to allocated memory on success, NULL otherwise.
  381. */
  382. void *tomoyo_alloc(const size_t size)
  383. {
  384. void *p = kzalloc(size, GFP_KERNEL);
  385. if (p)
  386. atomic_add(ksize(p), &tomoyo_dynamic_memory_size);
  387. return p;
  388. }
  389. /**
  390. * tomoyo_free - Release memory allocated by tomoyo_alloc().
  391. *
  392. * @p: Pointer returned by tomoyo_alloc(). May be NULL.
  393. *
  394. * Returns nothing.
  395. */
  396. void tomoyo_free(const void *p)
  397. {
  398. if (p) {
  399. atomic_sub(ksize(p), &tomoyo_dynamic_memory_size);
  400. kfree(p);
  401. }
  402. }
  403. /**
  404. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  405. *
  406. * @head: Pointer to "struct tomoyo_io_buffer".
  407. *
  408. * Returns memory usage.
  409. */
  410. int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  411. {
  412. if (!head->read_eof) {
  413. const unsigned int shared
  414. = tomoyo_allocated_memory_for_savename;
  415. const unsigned int private
  416. = tomoyo_allocated_memory_for_elements;
  417. const unsigned int dynamic
  418. = atomic_read(&tomoyo_dynamic_memory_size);
  419. char buffer[64];
  420. memset(buffer, 0, sizeof(buffer));
  421. if (tomoyo_quota_for_savename)
  422. snprintf(buffer, sizeof(buffer) - 1,
  423. " (Quota: %10u)",
  424. tomoyo_quota_for_savename);
  425. else
  426. buffer[0] = '\0';
  427. tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
  428. if (tomoyo_quota_for_elements)
  429. snprintf(buffer, sizeof(buffer) - 1,
  430. " (Quota: %10u)",
  431. tomoyo_quota_for_elements);
  432. else
  433. buffer[0] = '\0';
  434. tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
  435. tomoyo_io_printf(head, "Dynamic: %10u\n", dynamic);
  436. tomoyo_io_printf(head, "Total: %10u\n",
  437. shared + private + dynamic);
  438. head->read_eof = true;
  439. }
  440. return 0;
  441. }
  442. /**
  443. * tomoyo_write_memory_quota - Set memory quota.
  444. *
  445. * @head: Pointer to "struct tomoyo_io_buffer".
  446. *
  447. * Returns 0.
  448. */
  449. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  450. {
  451. char *data = head->write_buf;
  452. unsigned int size;
  453. if (sscanf(data, "Shared: %u", &size) == 1)
  454. tomoyo_quota_for_savename = size;
  455. else if (sscanf(data, "Private: %u", &size) == 1)
  456. tomoyo_quota_for_elements = size;
  457. return 0;
  458. }