compat_mq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * ipc/compat_mq.c
  3. * 32 bit emulation for POSIX message queue system calls
  4. *
  5. * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author: Arnd Bergmann <arnd@arndb.de>
  7. */
  8. #include <linux/compat.h>
  9. #include <linux/fs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mqueue.h>
  12. #include <linux/syscalls.h>
  13. #include <asm/uaccess.h>
  14. struct compat_mq_attr {
  15. compat_long_t mq_flags; /* message queue flags */
  16. compat_long_t mq_maxmsg; /* maximum number of messages */
  17. compat_long_t mq_msgsize; /* maximum message size */
  18. compat_long_t mq_curmsgs; /* number of messages currently queued */
  19. compat_long_t __reserved[4]; /* ignored for input, zeroed for output */
  20. };
  21. static inline int get_compat_mq_attr(struct mq_attr *attr,
  22. const struct compat_mq_attr __user *uattr)
  23. {
  24. if (!access_ok(VERIFY_READ, uattr, sizeof *uattr))
  25. return -EFAULT;
  26. return __get_user(attr->mq_flags, &uattr->mq_flags)
  27. | __get_user(attr->mq_maxmsg, &uattr->mq_maxmsg)
  28. | __get_user(attr->mq_msgsize, &uattr->mq_msgsize)
  29. | __get_user(attr->mq_curmsgs, &uattr->mq_curmsgs);
  30. }
  31. static inline int put_compat_mq_attr(const struct mq_attr *attr,
  32. struct compat_mq_attr __user *uattr)
  33. {
  34. if (clear_user(uattr, sizeof *uattr))
  35. return -EFAULT;
  36. return __put_user(attr->mq_flags, &uattr->mq_flags)
  37. | __put_user(attr->mq_maxmsg, &uattr->mq_maxmsg)
  38. | __put_user(attr->mq_msgsize, &uattr->mq_msgsize)
  39. | __put_user(attr->mq_curmsgs, &uattr->mq_curmsgs);
  40. }
  41. asmlinkage long compat_sys_mq_open(const char __user *u_name,
  42. int oflag, compat_mode_t mode,
  43. struct compat_mq_attr __user *u_attr)
  44. {
  45. void __user *p = NULL;
  46. if (u_attr && oflag & O_CREAT) {
  47. struct mq_attr attr;
  48. memset(&attr, 0, sizeof(attr));
  49. p = compat_alloc_user_space(sizeof(attr));
  50. if (get_compat_mq_attr(&attr, u_attr) ||
  51. copy_to_user(p, &attr, sizeof(attr)))
  52. return -EFAULT;
  53. }
  54. return sys_mq_open(u_name, oflag, mode, p);
  55. }
  56. static int compat_prepare_timeout(struct timespec __user * *p,
  57. const struct compat_timespec __user *u)
  58. {
  59. struct timespec ts;
  60. if (!u) {
  61. *p = NULL;
  62. return 0;
  63. }
  64. *p = compat_alloc_user_space(sizeof(ts));
  65. if (get_compat_timespec(&ts, u) || copy_to_user(*p, &ts, sizeof(ts)))
  66. return -EFAULT;
  67. return 0;
  68. }
  69. asmlinkage long compat_sys_mq_timedsend(mqd_t mqdes,
  70. const char __user *u_msg_ptr,
  71. size_t msg_len, unsigned int msg_prio,
  72. const struct compat_timespec __user *u_abs_timeout)
  73. {
  74. struct timespec __user *u_ts;
  75. if (compat_prepare_timeout(&u_ts, u_abs_timeout))
  76. return -EFAULT;
  77. return sys_mq_timedsend(mqdes, u_msg_ptr, msg_len,
  78. msg_prio, u_ts);
  79. }
  80. asmlinkage ssize_t compat_sys_mq_timedreceive(mqd_t mqdes,
  81. char __user *u_msg_ptr,
  82. size_t msg_len, unsigned int __user *u_msg_prio,
  83. const struct compat_timespec __user *u_abs_timeout)
  84. {
  85. struct timespec __user *u_ts;
  86. if (compat_prepare_timeout(&u_ts, u_abs_timeout))
  87. return -EFAULT;
  88. return sys_mq_timedreceive(mqdes, u_msg_ptr, msg_len,
  89. u_msg_prio, u_ts);
  90. }
  91. asmlinkage long compat_sys_mq_notify(mqd_t mqdes,
  92. const struct compat_sigevent __user *u_notification)
  93. {
  94. struct sigevent __user *p = NULL;
  95. if (u_notification) {
  96. struct sigevent n;
  97. p = compat_alloc_user_space(sizeof(*p));
  98. if (get_compat_sigevent(&n, u_notification))
  99. return -EFAULT;
  100. if (n.sigev_notify == SIGEV_THREAD)
  101. n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
  102. if (copy_to_user(p, &n, sizeof(*p)))
  103. return -EFAULT;
  104. }
  105. return sys_mq_notify(mqdes, p);
  106. }
  107. asmlinkage long compat_sys_mq_getsetattr(mqd_t mqdes,
  108. const struct compat_mq_attr __user *u_mqstat,
  109. struct compat_mq_attr __user *u_omqstat)
  110. {
  111. struct mq_attr mqstat;
  112. struct mq_attr __user *p = compat_alloc_user_space(2 * sizeof(*p));
  113. long ret;
  114. memset(&mqstat, 0, sizeof(mqstat));
  115. if (u_mqstat) {
  116. if (get_compat_mq_attr(&mqstat, u_mqstat) ||
  117. copy_to_user(p, &mqstat, sizeof(mqstat)))
  118. return -EFAULT;
  119. }
  120. ret = sys_mq_getsetattr(mqdes,
  121. u_mqstat ? p : NULL,
  122. u_omqstat ? p + 1 : NULL);
  123. if (ret)
  124. return ret;
  125. if (u_omqstat) {
  126. if (copy_from_user(&mqstat, p + 1, sizeof(mqstat)) ||
  127. put_compat_mq_attr(&mqstat, u_omqstat))
  128. return -EFAULT;
  129. }
  130. return 0;
  131. }