realpath.c 13 KB

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