realpath.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 tomoyo_alloc(), so the caller must call tomoyo_free()
  146. * if these functions didn't return NULL.
  147. */
  148. char *tomoyo_realpath_from_path(struct path *path)
  149. {
  150. char *buf = tomoyo_alloc(sizeof(struct tomoyo_page_buffer));
  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. tomoyo_free(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_alloc_element - Allocate permanent memory for structures.
  201. *
  202. * @size: Size in bytes.
  203. *
  204. * Returns pointer to allocated memory on success, NULL otherwise.
  205. *
  206. * Memory has to be zeroed.
  207. * The RAM is chunked, so NEVER try to kfree() the returned pointer.
  208. */
  209. void *tomoyo_alloc_element(const unsigned int size)
  210. {
  211. static char *buf;
  212. static DEFINE_MUTEX(lock);
  213. static unsigned int buf_used_len = PATH_MAX;
  214. char *ptr = NULL;
  215. /*Assumes sizeof(void *) >= sizeof(long) is true. */
  216. const unsigned int word_aligned_size
  217. = roundup(size, max(sizeof(void *), sizeof(long)));
  218. if (word_aligned_size > PATH_MAX)
  219. return NULL;
  220. mutex_lock(&lock);
  221. if (buf_used_len + word_aligned_size > PATH_MAX) {
  222. if (!tomoyo_quota_for_elements ||
  223. tomoyo_allocated_memory_for_elements
  224. + PATH_MAX <= tomoyo_quota_for_elements)
  225. ptr = kzalloc(PATH_MAX, GFP_KERNEL);
  226. if (!ptr) {
  227. printk(KERN_WARNING "ERROR: Out of memory "
  228. "for tomoyo_alloc_element().\n");
  229. if (!tomoyo_policy_loaded)
  230. panic("MAC Initialization failed.\n");
  231. } else {
  232. buf = ptr;
  233. tomoyo_allocated_memory_for_elements += PATH_MAX;
  234. buf_used_len = word_aligned_size;
  235. ptr = buf;
  236. }
  237. } else if (word_aligned_size) {
  238. int i;
  239. ptr = buf + buf_used_len;
  240. buf_used_len += word_aligned_size;
  241. for (i = 0; i < word_aligned_size; i++) {
  242. if (!ptr[i])
  243. continue;
  244. printk(KERN_ERR "WARNING: Reserved memory was tainted! "
  245. "The system might go wrong.\n");
  246. ptr[i] = '\0';
  247. }
  248. }
  249. mutex_unlock(&lock);
  250. return ptr;
  251. }
  252. /* Memory allocated for string data in bytes. */
  253. static unsigned int tomoyo_allocated_memory_for_savename;
  254. /* Quota for holding string data in bytes. */
  255. static unsigned int tomoyo_quota_for_savename;
  256. /*
  257. * TOMOYO uses this hash only when appending a string into the string
  258. * table. Frequency of appending strings is very low. So we don't need
  259. * large (e.g. 64k) hash size. 256 will be sufficient.
  260. */
  261. #define TOMOYO_HASH_BITS 8
  262. #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
  263. /*
  264. * tomoyo_name_entry is a structure which is used for linking
  265. * "struct tomoyo_path_info" into tomoyo_name_list .
  266. *
  267. * Since tomoyo_name_list manages a list of strings which are shared by
  268. * multiple processes (whereas "struct tomoyo_path_info" inside
  269. * "struct tomoyo_path_info_with_data" is not shared), a reference counter will
  270. * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
  271. * when TOMOYO starts supporting garbage collector.
  272. */
  273. struct tomoyo_name_entry {
  274. struct list_head list;
  275. struct tomoyo_path_info entry;
  276. };
  277. /* Structure for available memory region. */
  278. struct tomoyo_free_memory_block_list {
  279. struct list_head list;
  280. char *ptr; /* Pointer to a free area. */
  281. int len; /* Length of the area. */
  282. };
  283. /*
  284. * tomoyo_name_list is used for holding string data used by TOMOYO.
  285. * Since same string data is likely used for multiple times (e.g.
  286. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  287. * "const struct tomoyo_path_info *".
  288. */
  289. static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  290. /**
  291. * tomoyo_save_name - Allocate permanent memory for string data.
  292. *
  293. * @name: The string to store into the permernent memory.
  294. *
  295. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  296. *
  297. * The RAM is shared, so NEVER try to modify or kfree() the returned name.
  298. */
  299. const struct tomoyo_path_info *tomoyo_save_name(const char *name)
  300. {
  301. static LIST_HEAD(fmb_list);
  302. static DEFINE_MUTEX(lock);
  303. struct tomoyo_name_entry *ptr;
  304. unsigned int hash;
  305. /* fmb contains available size in bytes.
  306. fmb is removed from the fmb_list when fmb->len becomes 0. */
  307. struct tomoyo_free_memory_block_list *fmb;
  308. int len;
  309. char *cp;
  310. struct list_head *head;
  311. if (!name)
  312. return NULL;
  313. len = strlen(name) + 1;
  314. if (len > TOMOYO_MAX_PATHNAME_LEN) {
  315. printk(KERN_WARNING "ERROR: Name too long "
  316. "for tomoyo_save_name().\n");
  317. return NULL;
  318. }
  319. hash = full_name_hash((const unsigned char *) name, len - 1);
  320. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  321. mutex_lock(&lock);
  322. list_for_each_entry(ptr, head, list) {
  323. if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
  324. goto out;
  325. }
  326. list_for_each_entry(fmb, &fmb_list, list) {
  327. if (len <= fmb->len)
  328. goto ready;
  329. }
  330. if (!tomoyo_quota_for_savename ||
  331. tomoyo_allocated_memory_for_savename + PATH_MAX
  332. <= tomoyo_quota_for_savename)
  333. cp = kzalloc(PATH_MAX, GFP_KERNEL);
  334. else
  335. cp = NULL;
  336. fmb = kzalloc(sizeof(*fmb), GFP_KERNEL);
  337. if (!cp || !fmb) {
  338. kfree(cp);
  339. kfree(fmb);
  340. printk(KERN_WARNING "ERROR: Out of memory "
  341. "for tomoyo_save_name().\n");
  342. if (!tomoyo_policy_loaded)
  343. panic("MAC Initialization failed.\n");
  344. ptr = NULL;
  345. goto out;
  346. }
  347. tomoyo_allocated_memory_for_savename += PATH_MAX;
  348. list_add(&fmb->list, &fmb_list);
  349. fmb->ptr = cp;
  350. fmb->len = PATH_MAX;
  351. ready:
  352. ptr = tomoyo_alloc_element(sizeof(*ptr));
  353. if (!ptr)
  354. goto out;
  355. ptr->entry.name = fmb->ptr;
  356. memmove(fmb->ptr, name, len);
  357. tomoyo_fill_path_info(&ptr->entry);
  358. fmb->ptr += len;
  359. fmb->len -= len;
  360. list_add_tail(&ptr->list, head);
  361. if (fmb->len == 0) {
  362. list_del(&fmb->list);
  363. kfree(fmb);
  364. }
  365. out:
  366. mutex_unlock(&lock);
  367. return ptr ? &ptr->entry : NULL;
  368. }
  369. /**
  370. * tomoyo_realpath_init - Initialize realpath related code.
  371. */
  372. void __init tomoyo_realpath_init(void)
  373. {
  374. int i;
  375. BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX);
  376. for (i = 0; i < TOMOYO_MAX_HASH; i++)
  377. INIT_LIST_HEAD(&tomoyo_name_list[i]);
  378. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  379. tomoyo_kernel_domain.domainname = tomoyo_save_name(TOMOYO_ROOT_NAME);
  380. /*
  381. * tomoyo_read_lock() is not needed because this function is
  382. * called before the first "delete" request.
  383. */
  384. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  385. if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
  386. panic("Can't register tomoyo_kernel_domain");
  387. }
  388. /* Memory allocated for temporary purpose. */
  389. static atomic_t tomoyo_dynamic_memory_size;
  390. /**
  391. * tomoyo_alloc - Allocate memory for temporary purpose.
  392. *
  393. * @size: Size in bytes.
  394. *
  395. * Returns pointer to allocated memory on success, NULL otherwise.
  396. */
  397. void *tomoyo_alloc(const size_t size)
  398. {
  399. void *p = kzalloc(size, GFP_KERNEL);
  400. if (p)
  401. atomic_add(ksize(p), &tomoyo_dynamic_memory_size);
  402. return p;
  403. }
  404. /**
  405. * tomoyo_free - Release memory allocated by tomoyo_alloc().
  406. *
  407. * @p: Pointer returned by tomoyo_alloc(). May be NULL.
  408. *
  409. * Returns nothing.
  410. */
  411. void tomoyo_free(const void *p)
  412. {
  413. if (p) {
  414. atomic_sub(ksize(p), &tomoyo_dynamic_memory_size);
  415. kfree(p);
  416. }
  417. }
  418. /**
  419. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  420. *
  421. * @head: Pointer to "struct tomoyo_io_buffer".
  422. *
  423. * Returns memory usage.
  424. */
  425. int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  426. {
  427. if (!head->read_eof) {
  428. const unsigned int shared
  429. = tomoyo_allocated_memory_for_savename;
  430. const unsigned int private
  431. = tomoyo_allocated_memory_for_elements;
  432. const unsigned int dynamic
  433. = atomic_read(&tomoyo_dynamic_memory_size);
  434. char buffer[64];
  435. memset(buffer, 0, sizeof(buffer));
  436. if (tomoyo_quota_for_savename)
  437. snprintf(buffer, sizeof(buffer) - 1,
  438. " (Quota: %10u)",
  439. tomoyo_quota_for_savename);
  440. else
  441. buffer[0] = '\0';
  442. tomoyo_io_printf(head, "Shared: %10u%s\n", shared, buffer);
  443. if (tomoyo_quota_for_elements)
  444. snprintf(buffer, sizeof(buffer) - 1,
  445. " (Quota: %10u)",
  446. tomoyo_quota_for_elements);
  447. else
  448. buffer[0] = '\0';
  449. tomoyo_io_printf(head, "Private: %10u%s\n", private, buffer);
  450. tomoyo_io_printf(head, "Dynamic: %10u\n", dynamic);
  451. tomoyo_io_printf(head, "Total: %10u\n",
  452. shared + private + dynamic);
  453. head->read_eof = true;
  454. }
  455. return 0;
  456. }
  457. /**
  458. * tomoyo_write_memory_quota - Set memory quota.
  459. *
  460. * @head: Pointer to "struct tomoyo_io_buffer".
  461. *
  462. * Returns 0.
  463. */
  464. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  465. {
  466. char *data = head->write_buf;
  467. unsigned int size;
  468. if (sscanf(data, "Shared: %u", &size) == 1)
  469. tomoyo_quota_for_savename = size;
  470. else if (sscanf(data, "Private: %u", &size) == 1)
  471. tomoyo_quota_for_elements = size;
  472. return 0;
  473. }