realpath.c 13 KB

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