compat_mq.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <linux/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. COMPAT_SYSCALL_DEFINE4(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. COMPAT_SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes,
  57. const char __user *, u_msg_ptr,
  58. compat_size_t, msg_len, unsigned int, msg_prio,
  59. const struct compat_timespec __user *, u_abs_timeout)
  60. {
  61. struct timespec __user *u_ts;
  62. if (compat_convert_timespec(&u_ts, u_abs_timeout))
  63. return -EFAULT;
  64. return sys_mq_timedsend(mqdes, u_msg_ptr, msg_len,
  65. msg_prio, u_ts);
  66. }
  67. COMPAT_SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes,
  68. char __user *, u_msg_ptr,
  69. compat_size_t, msg_len, unsigned int __user *, u_msg_prio,
  70. const struct compat_timespec __user *, u_abs_timeout)
  71. {
  72. struct timespec __user *u_ts;
  73. if (compat_convert_timespec(&u_ts, u_abs_timeout))
  74. return -EFAULT;
  75. return sys_mq_timedreceive(mqdes, u_msg_ptr, msg_len,
  76. u_msg_prio, u_ts);
  77. }
  78. COMPAT_SYSCALL_DEFINE2(mq_notify, mqd_t, mqdes,
  79. const struct compat_sigevent __user *, u_notification)
  80. {
  81. struct sigevent __user *p = NULL;
  82. if (u_notification) {
  83. struct sigevent n;
  84. p = compat_alloc_user_space(sizeof(*p));
  85. if (get_compat_sigevent(&n, u_notification))
  86. return -EFAULT;
  87. if (n.sigev_notify == SIGEV_THREAD)
  88. n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
  89. if (copy_to_user(p, &n, sizeof(*p)))
  90. return -EFAULT;
  91. }
  92. return sys_mq_notify(mqdes, p);
  93. }
  94. COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
  95. const struct compat_mq_attr __user *, u_mqstat,
  96. struct compat_mq_attr __user *, u_omqstat)
  97. {
  98. struct mq_attr mqstat;
  99. struct mq_attr __user *p = compat_alloc_user_space(2 * sizeof(*p));
  100. long ret;
  101. memset(&mqstat, 0, sizeof(mqstat));
  102. if (u_mqstat) {
  103. if (get_compat_mq_attr(&mqstat, u_mqstat) ||
  104. copy_to_user(p, &mqstat, sizeof(mqstat)))
  105. return -EFAULT;
  106. }
  107. ret = sys_mq_getsetattr(mqdes,
  108. u_mqstat ? p : NULL,
  109. u_omqstat ? p + 1 : NULL);
  110. if (ret)
  111. return ret;
  112. if (u_omqstat) {
  113. if (copy_from_user(&mqstat, p + 1, sizeof(mqstat)) ||
  114. put_compat_mq_attr(&mqstat, u_omqstat))
  115. return -EFAULT;
  116. }
  117. return 0;
  118. }