msgutil.c 3.6 KB

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