msgutil.c 3.7 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_ns.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/sched.h>
  21. #include "util.h"
  22. DEFINE_SPINLOCK(mq_lock);
  23. /*
  24. * The next 2 defines are here bc this is the only file
  25. * compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE
  26. * and not CONFIG_IPC_NS.
  27. */
  28. struct ipc_namespace init_ipc_ns = {
  29. .count = REFCOUNT_INIT(1),
  30. .user_ns = &init_user_ns,
  31. .ns.inum = PROC_IPC_INIT_INO,
  32. #ifdef CONFIG_IPC_NS
  33. .ns.ops = &ipcns_operations,
  34. #endif
  35. };
  36. struct msg_msgseg {
  37. struct msg_msgseg *next;
  38. /* the next part of the message follows immediately */
  39. };
  40. #define DATALEN_MSG ((size_t)PAGE_SIZE-sizeof(struct msg_msg))
  41. #define DATALEN_SEG ((size_t)PAGE_SIZE-sizeof(struct msg_msgseg))
  42. static struct msg_msg *alloc_msg(size_t len)
  43. {
  44. struct msg_msg *msg;
  45. struct msg_msgseg **pseg;
  46. size_t alen;
  47. alen = min(len, DATALEN_MSG);
  48. msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL_ACCOUNT);
  49. if (msg == NULL)
  50. return NULL;
  51. msg->next = NULL;
  52. msg->security = NULL;
  53. len -= alen;
  54. pseg = &msg->next;
  55. while (len > 0) {
  56. struct msg_msgseg *seg;
  57. cond_resched();
  58. alen = min(len, DATALEN_SEG);
  59. seg = kmalloc(sizeof(*seg) + alen, GFP_KERNEL_ACCOUNT);
  60. if (seg == NULL)
  61. goto out_err;
  62. *pseg = seg;
  63. seg->next = NULL;
  64. pseg = &seg->next;
  65. len -= alen;
  66. }
  67. return msg;
  68. out_err:
  69. free_msg(msg);
  70. return NULL;
  71. }
  72. struct msg_msg *load_msg(const void __user *src, size_t len)
  73. {
  74. struct msg_msg *msg;
  75. struct msg_msgseg *seg;
  76. int err = -EFAULT;
  77. size_t alen;
  78. msg = alloc_msg(len);
  79. if (msg == NULL)
  80. return ERR_PTR(-ENOMEM);
  81. alen = min(len, DATALEN_MSG);
  82. if (copy_from_user(msg + 1, src, alen))
  83. goto out_err;
  84. for (seg = msg->next; seg != NULL; seg = seg->next) {
  85. len -= alen;
  86. src = (char __user *)src + alen;
  87. alen = min(len, DATALEN_SEG);
  88. if (copy_from_user(seg + 1, src, alen))
  89. goto out_err;
  90. }
  91. err = security_msg_msg_alloc(msg);
  92. if (err)
  93. goto out_err;
  94. return msg;
  95. out_err:
  96. free_msg(msg);
  97. return ERR_PTR(err);
  98. }
  99. #ifdef CONFIG_CHECKPOINT_RESTORE
  100. struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
  101. {
  102. struct msg_msgseg *dst_pseg, *src_pseg;
  103. size_t len = src->m_ts;
  104. size_t alen;
  105. if (src->m_ts > dst->m_ts)
  106. return ERR_PTR(-EINVAL);
  107. alen = min(len, DATALEN_MSG);
  108. memcpy(dst + 1, src + 1, alen);
  109. for (dst_pseg = dst->next, src_pseg = src->next;
  110. src_pseg != NULL;
  111. dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) {
  112. len -= alen;
  113. alen = min(len, DATALEN_SEG);
  114. memcpy(dst_pseg + 1, src_pseg + 1, alen);
  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, size_t len)
  127. {
  128. size_t alen;
  129. struct msg_msgseg *seg;
  130. alen = min(len, DATALEN_MSG);
  131. if (copy_to_user(dest, msg + 1, alen))
  132. return -1;
  133. for (seg = msg->next; seg != NULL; seg = seg->next) {
  134. len -= alen;
  135. dest = (char __user *)dest + alen;
  136. alen = min(len, DATALEN_SEG);
  137. if (copy_to_user(dest, seg + 1, alen))
  138. return -1;
  139. }
  140. return 0;
  141. }
  142. void free_msg(struct msg_msg *msg)
  143. {
  144. struct msg_msgseg *seg;
  145. security_msg_msg_free(msg);
  146. seg = msg->next;
  147. kfree(msg);
  148. while (seg != NULL) {
  149. struct msg_msgseg *tmp = seg->next;
  150. cond_resched();
  151. kfree(seg);
  152. seg = tmp;
  153. }
  154. }