realpath.c 13 KB

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