msgutil.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_fs.h>
  19. #include <asm/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. .proc_inum = PROC_IPC_INIT_INO,
  31. };
  32. atomic_t nr_ipc_ns = ATOMIC_INIT(1);
  33. struct msg_msgseg {
  34. struct msg_msgseg* next;
  35. /* the next part of the message follows immediately */
  36. };
  37. #define DATALEN_MSG (PAGE_SIZE-sizeof(struct msg_msg))
  38. #define DATALEN_SEG (PAGE_SIZE-sizeof(struct msg_msgseg))
  39. struct msg_msg *load_msg(const void __user *src, int len)
  40. {
  41. struct msg_msg *msg;
  42. struct msg_msgseg **pseg;
  43. int err;
  44. int alen;
  45. alen = len;
  46. if (alen > DATALEN_MSG)
  47. alen = DATALEN_MSG;
  48. msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL);
  49. if (msg == NULL)
  50. return ERR_PTR(-ENOMEM);
  51. msg->next = NULL;
  52. msg->security = NULL;
  53. if (copy_from_user(msg + 1, src, alen)) {
  54. err = -EFAULT;
  55. goto out_err;
  56. }
  57. len -= alen;
  58. src = ((char __user *)src) + alen;
  59. pseg = &msg->next;
  60. while (len > 0) {
  61. struct msg_msgseg *seg;
  62. alen = len;
  63. if (alen > DATALEN_SEG)
  64. alen = DATALEN_SEG;
  65. seg = kmalloc(sizeof(*seg) + alen,
  66. GFP_KERNEL);
  67. if (seg == NULL) {
  68. err = -ENOMEM;
  69. goto out_err;
  70. }
  71. *pseg = seg;
  72. seg->next = NULL;
  73. if (copy_from_user(seg + 1, src, alen)) {
  74. err = -EFAULT;
  75. goto out_err;
  76. }
  77. pseg = &seg->next;
  78. len -= alen;
  79. src = ((char __user *)src) + alen;
  80. }
  81. err = security_msg_msg_alloc(msg);
  82. if (err)
  83. goto out_err;
  84. return msg;
  85. out_err:
  86. free_msg(msg);
  87. return ERR_PTR(err);
  88. }
  89. #ifdef CONFIG_CHECKPOINT_RESTORE
  90. struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
  91. {
  92. struct msg_msgseg *dst_pseg, *src_pseg;
  93. int len = src->m_ts;
  94. int alen;
  95. BUG_ON(dst == NULL);
  96. if (src->m_ts > dst->m_ts)
  97. return ERR_PTR(-EINVAL);
  98. alen = len;
  99. if (alen > DATALEN_MSG)
  100. alen = DATALEN_MSG;
  101. dst->next = NULL;
  102. dst->security = NULL;
  103. memcpy(dst + 1, src + 1, alen);
  104. len -= alen;
  105. dst_pseg = dst->next;
  106. src_pseg = src->next;
  107. while (len > 0) {
  108. alen = len;
  109. if (alen > DATALEN_SEG)
  110. alen = DATALEN_SEG;
  111. memcpy(dst_pseg + 1, src_pseg + 1, alen);
  112. dst_pseg = dst_pseg->next;
  113. len -= alen;
  114. src_pseg = src_pseg->next;
  115. }
  116. dst->m_type = src->m_type;
  117. dst->m_ts = src->m_ts;
  118. return dst;
  119. }
  120. #else
  121. struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
  122. {
  123. return ERR_PTR(-ENOSYS);
  124. }
  125. #endif
  126. int store_msg(void __user *dest, struct msg_msg *msg, int len)
  127. {
  128. int alen;
  129. struct msg_msgseg *seg;
  130. alen = len;
  131. if (alen > DATALEN_MSG)
  132. alen = DATALEN_MSG;
  133. if (copy_to_user(dest, msg + 1, alen))
  134. return -1;
  135. len -= alen;
  136. dest = ((char __user *)dest) + alen;
  137. seg = msg->next;
  138. while (len > 0) {
  139. alen = len;
  140. if (alen > DATALEN_SEG)
  141. alen = DATALEN_SEG;
  142. if (copy_to_user(dest, seg + 1, alen))
  143. return -1;
  144. len -= alen;
  145. dest = ((char __user *)dest) + alen;
  146. seg = seg->next;
  147. }
  148. return 0;
  149. }
  150. void free_msg(struct msg_msg *msg)
  151. {
  152. struct msg_msgseg *seg;
  153. security_msg_msg_free(msg);
  154. seg = msg->next;
  155. kfree(msg);
  156. while (seg != NULL) {
  157. struct msg_msgseg *tmp = seg->next;
  158. kfree(seg);
  159. seg = tmp;
  160. }
  161. }