realpath.c 12 KB

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