realpath.c 12 KB

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