msgutil.c 3.4 KB

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