msgutil.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * linux/ipc/msgutil.c
  3. * Copyright (C) 1999, 2004 Manfred Spraul
  4. *
  5. * This file is released under GNU General Public Licence version 2 or
  6. * (at your option) any later version.
  7. *
  8. * See the file COPYING for more details.
  9. */
  10. #include <linux/spinlock.h>
  11. #include <linux/init.h>
  12. #include <linux/security.h>
  13. #include <linux/slab.h>
  14. #include <linux/ipc.h>
  15. #include <linux/msg.h>
  16. #include <linux/ipc_namespace.h>
  17. #include <linux/utsname.h>
  18. #include <linux/proc_ns.h>
  19. #include <linux/uaccess.h>
  20. #include "util.h"
  21. DEFINE_SPINLOCK(mq_lock);
  22. /*
  23. * The next 2 defines are here bc this is the only file
  24. * compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE
  25. * and not CONFIG_IPC_NS.
  26. */
  27. struct ipc_namespace init_ipc_ns = {
  28. .count = ATOMIC_INIT(1),
  29. .user_ns = &init_user_ns,
  30. .ns.inum = PROC_IPC_INIT_INO,
  31. #ifdef CONFIG_IPC_NS
  32. .ns.ops = &ipcns_operations,
  33. #endif
  34. };
  35. struct msg_msgseg {
  36. struct msg_msgseg *next;
  37. /* the next part of the message follows immediately */
  38. };
  39. #define DATALEN_MSG ((size_t)PAGE_SIZE-sizeof(struct msg_msg))
  40. #define DATALEN_SEG ((size_t)PAGE_SIZE-sizeof(struct msg_msgseg))
  41. static struct msg_msg *alloc_msg(size_t len)
  42. {
  43. struct msg_msg *msg;
  44. struct msg_msgseg **pseg;
  45. size_t alen;
  46. alen = min(len, DATALEN_MSG);
  47. msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL_ACCOUNT);
  48. if (msg == NULL)
  49. return NULL;
  50. msg->next = NULL;
  51. msg->security = NULL;
  52. len -= alen;
  53. pseg = &msg->next;
  54. while (len > 0) {
  55. struct msg_msgseg *seg;
  56. alen = min(len, DATALEN_SEG);
  57. seg = kmalloc(sizeof(*seg) + alen, GFP_KERNEL_ACCOUNT);
  58. if (seg == NULL)
  59. goto out_err;
  60. *pseg = seg;
  61. seg->next = NULL;
  62. pseg = &seg->next;
  63. len -= alen;
  64. }
  65. return msg;
  66. out_err:
  67. free_msg(msg);
  68. return NULL;
  69. }
  70. struct msg_msg *load_msg(const void __user *src, size_t len)
  71. {
  72. struct msg_msg *msg;
  73. struct msg_msgseg *seg;
  74. int err = -EFAULT;
  75. size_t alen;
  76. msg = alloc_msg(len);
  77. if (msg == NULL)
  78. return ERR_PTR(-ENOMEM);
  79. alen = min(len, DATALEN_MSG);
  80. if (copy_from_user(msg + 1, src, alen))
  81. goto out_err;
  82. for (seg = msg->next; seg != NULL; seg = seg->next) {
  83. len -= alen;
  84. src = (char __user *)src + alen;
  85. alen = min(len, DATALEN_SEG);
  86. if (copy_from_user(seg + 1, src, alen))
  87. goto out_err;
  88. }
  89. err = security_msg_msg_alloc(msg);
  90. if (err)
  91. goto out_err;
  92. return msg;
  93. out_err:
  94. free_msg(msg);
  95. return ERR_PTR(err);
  96. }
  97. #ifdef CONFIG_CHECKPOINT_RESTORE
  98. struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
  99. {
  100. struct msg_msgseg *dst_pseg, *src_pseg;
  101. size_t len = src->m_ts;
  102. size_t alen;
  103. if (src->m_ts > dst->m_ts)
  104. return ERR_PTR(-EINVAL);
  105. alen = min(len, DATALEN_MSG);
  106. memcpy(dst + 1, src + 1, alen);
  107. for (dst_pseg = dst->next, src_pseg = src->next;
  108. src_pseg != NULL;
  109. dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) {
  110. len -= alen;
  111. alen = min(len, DATALEN_SEG);
  112. memcpy(dst_pseg + 1, src_pseg + 1, alen);
  113. }
  114. dst->m_type = src->m_type;
  115. dst->m_ts = src->m_ts;
  116. return dst;
  117. }
  118. #else
  119. struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
  120. {
  121. return ERR_PTR(-ENOSYS);
  122. }
  123. #endif
  124. int store_msg(void __user *dest, struct msg_msg *msg, size_t len)
  125. {
  126. size_t alen;
  127. struct msg_msgseg *seg;
  128. alen = min(len, DATALEN_MSG);
  129. if (copy_to_user(dest, msg + 1, alen))
  130. return -1;
  131. for (seg = msg->next; seg != NULL; seg = seg->next) {
  132. len -= alen;
  133. dest = (char __user *)dest + alen;
  134. alen = min(len, DATALEN_SEG);
  135. if (copy_to_user(dest, seg + 1, alen))
  136. return -1;
  137. }
  138. return 0;
  139. }
  140. void free_msg(struct msg_msg *msg)
  141. {
  142. struct msg_msgseg *seg;
  143. security_msg_msg_free(msg);
  144. seg = msg->next;
  145. kfree(msg);
  146. while (seg != NULL) {
  147. struct msg_msgseg *tmp = seg->next;
  148. kfree(seg);
  149. seg = tmp;
  150. }
  151. }