msgutil.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 ((size_t)PAGE_SIZE-sizeof(struct msg_msg))
  38. #define DATALEN_SEG ((size_t)PAGE_SIZE-sizeof(struct msg_msgseg))
  39. struct msg_msg *load_msg(const void __user *src, size_t len)
  40. {
  41. struct msg_msg *msg;
  42. struct msg_msgseg **pseg;
  43. int err;
  44. size_t 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. int store_msg(void __user *dest, struct msg_msg *msg, size_t len)
  90. {
  91. size_t alen;
  92. struct msg_msgseg *seg;
  93. alen = len;
  94. if (alen > DATALEN_MSG)
  95. alen = DATALEN_MSG;
  96. if (copy_to_user(dest, msg + 1, alen))
  97. return -1;
  98. len -= alen;
  99. dest = ((char __user *)dest) + alen;
  100. seg = msg->next;
  101. while (len > 0) {
  102. alen = len;
  103. if (alen > DATALEN_SEG)
  104. alen = DATALEN_SEG;
  105. if (copy_to_user(dest, seg + 1, alen))
  106. return -1;
  107. len -= alen;
  108. dest = ((char __user *)dest) + alen;
  109. seg = seg->next;
  110. }
  111. return 0;
  112. }
  113. void free_msg(struct msg_msg *msg)
  114. {
  115. struct msg_msgseg *seg;
  116. security_msg_msg_free(msg);
  117. seg = msg->next;
  118. kfree(msg);
  119. while (seg != NULL) {
  120. struct msg_msgseg *tmp = seg->next;
  121. kfree(seg);
  122. seg = tmp;
  123. }
  124. }