compat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c.
  3. *
  4. * Copyright (C) 2000 VA Linux Co
  5. * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
  6. * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
  7. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  8. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  9. * Copyright (C) 2000 Hewlett-Packard Co.
  10. * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
  11. * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/gfp.h>
  15. #include <linux/fs.h>
  16. #include <linux/types.h>
  17. #include <linux/file.h>
  18. #include <linux/icmpv6.h>
  19. #include <linux/socket.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/filter.h>
  22. #include <linux/compat.h>
  23. #include <linux/security.h>
  24. #include <net/scm.h>
  25. #include <net/sock.h>
  26. #include <net/ip.h>
  27. #include <net/ipv6.h>
  28. #include <asm/uaccess.h>
  29. #include <net/compat.h>
  30. static inline int iov_from_user_compat_to_kern(struct iovec *kiov,
  31. struct compat_iovec __user *uiov32,
  32. int niov)
  33. {
  34. int tot_len = 0;
  35. while (niov > 0) {
  36. compat_uptr_t buf;
  37. compat_size_t len;
  38. if (get_user(len, &uiov32->iov_len) ||
  39. get_user(buf, &uiov32->iov_base))
  40. return -EFAULT;
  41. if (len > INT_MAX - tot_len)
  42. len = INT_MAX - tot_len;
  43. tot_len += len;
  44. kiov->iov_base = compat_ptr(buf);
  45. kiov->iov_len = (__kernel_size_t) len;
  46. uiov32++;
  47. kiov++;
  48. niov--;
  49. }
  50. return tot_len;
  51. }
  52. int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg)
  53. {
  54. compat_uptr_t tmp1, tmp2, tmp3;
  55. if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
  56. __get_user(tmp1, &umsg->msg_name) ||
  57. __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
  58. __get_user(tmp2, &umsg->msg_iov) ||
  59. __get_user(kmsg->msg_iovlen, &umsg->msg_iovlen) ||
  60. __get_user(tmp3, &umsg->msg_control) ||
  61. __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
  62. __get_user(kmsg->msg_flags, &umsg->msg_flags))
  63. return -EFAULT;
  64. kmsg->msg_name = compat_ptr(tmp1);
  65. kmsg->msg_iov = compat_ptr(tmp2);
  66. kmsg->msg_control = compat_ptr(tmp3);
  67. return 0;
  68. }
  69. /* I've named the args so it is easy to tell whose space the pointers are in. */
  70. int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov,
  71. struct sockaddr *kern_address, int mode)
  72. {
  73. int tot_len;
  74. if (kern_msg->msg_namelen) {
  75. if (mode == VERIFY_READ) {
  76. int err = move_addr_to_kernel(kern_msg->msg_name,
  77. kern_msg->msg_namelen,
  78. kern_address);
  79. if (err < 0)
  80. return err;
  81. }
  82. kern_msg->msg_name = kern_address;
  83. } else
  84. kern_msg->msg_name = NULL;
  85. tot_len = iov_from_user_compat_to_kern(kern_iov,
  86. (struct compat_iovec __user *)kern_msg->msg_iov,
  87. kern_msg->msg_iovlen);
  88. if (tot_len >= 0)
  89. kern_msg->msg_iov = kern_iov;
  90. return tot_len;
  91. }
  92. /* Bleech... */
  93. #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
  94. #define CMSG_COMPAT_DATA(cmsg) \
  95. ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
  96. #define CMSG_COMPAT_SPACE(len) \
  97. (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
  98. #define CMSG_COMPAT_LEN(len) \
  99. (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))
  100. #define CMSG_COMPAT_FIRSTHDR(msg) \
  101. (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
  102. (struct compat_cmsghdr __user *)((msg)->msg_control) : \
  103. (struct compat_cmsghdr __user *)NULL)
  104. #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
  105. ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
  106. (ucmlen) <= (unsigned long) \
  107. ((mhdr)->msg_controllen - \
  108. ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
  109. static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
  110. struct compat_cmsghdr __user *cmsg, int cmsg_len)
  111. {
  112. char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
  113. if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
  114. msg->msg_controllen)
  115. return NULL;
  116. return (struct compat_cmsghdr __user *)ptr;
  117. }
  118. /* There is a lot of hair here because the alignment rules (and
  119. * thus placement) of cmsg headers and length are different for
  120. * 32-bit apps. -DaveM
  121. */
  122. int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
  123. unsigned char *stackbuf, int stackbuf_size)
  124. {
  125. struct compat_cmsghdr __user *ucmsg;
  126. struct cmsghdr *kcmsg, *kcmsg_base;
  127. compat_size_t ucmlen;
  128. __kernel_size_t kcmlen, tmp;
  129. int err = -EFAULT;
  130. kcmlen = 0;
  131. kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
  132. ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
  133. while (ucmsg != NULL) {
  134. if (get_user(ucmlen, &ucmsg->cmsg_len))
  135. return -EFAULT;
  136. /* Catch bogons. */
  137. if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
  138. return -EINVAL;
  139. tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
  140. CMSG_ALIGN(sizeof(struct cmsghdr)));
  141. tmp = CMSG_ALIGN(tmp);
  142. kcmlen += tmp;
  143. ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
  144. }
  145. if (kcmlen == 0)
  146. return -EINVAL;
  147. /* The kcmlen holds the 64-bit version of the control length.
  148. * It may not be modified as we do not stick it into the kmsg
  149. * until we have successfully copied over all of the data
  150. * from the user.
  151. */
  152. if (kcmlen > stackbuf_size)
  153. kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
  154. if (kcmsg == NULL)
  155. return -ENOBUFS;
  156. /* Now copy them over neatly. */
  157. memset(kcmsg, 0, kcmlen);
  158. ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
  159. while (ucmsg != NULL) {
  160. if (__get_user(ucmlen, &ucmsg->cmsg_len))
  161. goto Efault;
  162. if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
  163. goto Einval;
  164. tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
  165. CMSG_ALIGN(sizeof(struct cmsghdr)));
  166. if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
  167. goto Einval;
  168. kcmsg->cmsg_len = tmp;
  169. tmp = CMSG_ALIGN(tmp);
  170. if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
  171. __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
  172. copy_from_user(CMSG_DATA(kcmsg),
  173. CMSG_COMPAT_DATA(ucmsg),
  174. (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg)))))
  175. goto Efault;
  176. /* Advance. */
  177. kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
  178. ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
  179. }
  180. /* Ok, looks like we made it. Hook it up and return success. */
  181. kmsg->msg_control = kcmsg_base;
  182. kmsg->msg_controllen = kcmlen;
  183. return 0;
  184. Einval:
  185. err = -EINVAL;
  186. Efault:
  187. if (kcmsg_base != (struct cmsghdr *)stackbuf)
  188. sock_kfree_s(sk, kcmsg_base, kcmlen);
  189. return err;
  190. }
  191. int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
  192. {
  193. struct compat_timeval ctv;
  194. struct compat_timespec cts[3];
  195. struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
  196. struct compat_cmsghdr cmhdr;
  197. int cmlen;
  198. if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
  199. kmsg->msg_flags |= MSG_CTRUNC;
  200. return 0; /* XXX: return error? check spec. */
  201. }
  202. if (level == SOL_SOCKET && type == SCM_TIMESTAMP) {
  203. struct timeval *tv = (struct timeval *)data;
  204. ctv.tv_sec = tv->tv_sec;
  205. ctv.tv_usec = tv->tv_usec;
  206. data = &ctv;
  207. len = sizeof(ctv);
  208. }
  209. if (level == SOL_SOCKET &&
  210. (type == SCM_TIMESTAMPNS || type == SCM_TIMESTAMPING)) {
  211. int count = type == SCM_TIMESTAMPNS ? 1 : 3;
  212. int i;
  213. struct timespec *ts = (struct timespec *)data;
  214. for (i = 0; i < count; i++) {
  215. cts[i].tv_sec = ts[i].tv_sec;
  216. cts[i].tv_nsec = ts[i].tv_nsec;
  217. }
  218. data = &cts;
  219. len = sizeof(cts[0]) * count;
  220. }
  221. cmlen = CMSG_COMPAT_LEN(len);
  222. if (kmsg->msg_controllen < cmlen) {
  223. kmsg->msg_flags |= MSG_CTRUNC;
  224. cmlen = kmsg->msg_controllen;
  225. }
  226. cmhdr.cmsg_level = level;
  227. cmhdr.cmsg_type = type;
  228. cmhdr.cmsg_len = cmlen;
  229. if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
  230. return -EFAULT;
  231. if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
  232. return -EFAULT;
  233. cmlen = CMSG_COMPAT_SPACE(len);
  234. if (kmsg->msg_controllen < cmlen)
  235. cmlen = kmsg->msg_controllen;
  236. kmsg->msg_control += cmlen;
  237. kmsg->msg_controllen -= cmlen;
  238. return 0;
  239. }
  240. void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
  241. {
  242. struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
  243. int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
  244. int fdnum = scm->fp->count;
  245. struct file **fp = scm->fp->fp;
  246. int __user *cmfptr;
  247. int err = 0, i;
  248. if (fdnum < fdmax)
  249. fdmax = fdnum;
  250. for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
  251. int new_fd;
  252. err = security_file_receive(fp[i]);
  253. if (err)
  254. break;
  255. err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
  256. ? O_CLOEXEC : 0);
  257. if (err < 0)
  258. break;
  259. new_fd = err;
  260. err = put_user(new_fd, cmfptr);
  261. if (err) {
  262. put_unused_fd(new_fd);
  263. break;
  264. }
  265. /* Bump the usage count and install the file. */
  266. get_file(fp[i]);
  267. fd_install(new_fd, fp[i]);
  268. }
  269. if (i > 0) {
  270. int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
  271. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  272. if (!err)
  273. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  274. if (!err)
  275. err = put_user(cmlen, &cm->cmsg_len);
  276. if (!err) {
  277. cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
  278. kmsg->msg_control += cmlen;
  279. kmsg->msg_controllen -= cmlen;
  280. }
  281. }
  282. if (i < fdnum)
  283. kmsg->msg_flags |= MSG_CTRUNC;
  284. /*
  285. * All of the files that fit in the message have had their
  286. * usage counts incremented, so we just free the list.
  287. */
  288. __scm_destroy(scm);
  289. }
  290. /*
  291. * A struct sock_filter is architecture independent.
  292. */
  293. struct compat_sock_fprog {
  294. u16 len;
  295. compat_uptr_t filter; /* struct sock_filter * */
  296. };
  297. static int do_set_attach_filter(struct socket *sock, int level, int optname,
  298. char __user *optval, unsigned int optlen)
  299. {
  300. struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
  301. struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
  302. compat_uptr_t ptr;
  303. u16 len;
  304. if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) ||
  305. !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) ||
  306. __get_user(len, &fprog32->len) ||
  307. __get_user(ptr, &fprog32->filter) ||
  308. __put_user(len, &kfprog->len) ||
  309. __put_user(compat_ptr(ptr), &kfprog->filter))
  310. return -EFAULT;
  311. return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
  312. sizeof(struct sock_fprog));
  313. }
  314. static int do_set_sock_timeout(struct socket *sock, int level,
  315. int optname, char __user *optval, unsigned int optlen)
  316. {
  317. struct compat_timeval __user *up = (struct compat_timeval __user *)optval;
  318. struct timeval ktime;
  319. mm_segment_t old_fs;
  320. int err;
  321. if (optlen < sizeof(*up))
  322. return -EINVAL;
  323. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  324. __get_user(ktime.tv_sec, &up->tv_sec) ||
  325. __get_user(ktime.tv_usec, &up->tv_usec))
  326. return -EFAULT;
  327. old_fs = get_fs();
  328. set_fs(KERNEL_DS);
  329. err = sock_setsockopt(sock, level, optname, (char *)&ktime, sizeof(ktime));
  330. set_fs(old_fs);
  331. return err;
  332. }
  333. static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
  334. char __user *optval, unsigned int optlen)
  335. {
  336. if (optname == SO_ATTACH_FILTER)
  337. return do_set_attach_filter(sock, level, optname,
  338. optval, optlen);
  339. if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
  340. return do_set_sock_timeout(sock, level, optname, optval, optlen);
  341. return sock_setsockopt(sock, level, optname, optval, optlen);
  342. }
  343. asmlinkage long compat_sys_setsockopt(int fd, int level, int optname,
  344. char __user *optval, unsigned int optlen)
  345. {
  346. int err;
  347. struct socket *sock = sockfd_lookup(fd, &err);
  348. if (sock) {
  349. err = security_socket_setsockopt(sock, level, optname);
  350. if (err) {
  351. sockfd_put(sock);
  352. return err;
  353. }
  354. if (level == SOL_SOCKET)
  355. err = compat_sock_setsockopt(sock, level,
  356. optname, optval, optlen);
  357. else if (sock->ops->compat_setsockopt)
  358. err = sock->ops->compat_setsockopt(sock, level,
  359. optname, optval, optlen);
  360. else
  361. err = sock->ops->setsockopt(sock, level,
  362. optname, optval, optlen);
  363. sockfd_put(sock);
  364. }
  365. return err;
  366. }
  367. static int do_get_sock_timeout(struct socket *sock, int level, int optname,
  368. char __user *optval, int __user *optlen)
  369. {
  370. struct compat_timeval __user *up;
  371. struct timeval ktime;
  372. mm_segment_t old_fs;
  373. int len, err;
  374. up = (struct compat_timeval __user *) optval;
  375. if (get_user(len, optlen))
  376. return -EFAULT;
  377. if (len < sizeof(*up))
  378. return -EINVAL;
  379. len = sizeof(ktime);
  380. old_fs = get_fs();
  381. set_fs(KERNEL_DS);
  382. err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
  383. set_fs(old_fs);
  384. if (!err) {
  385. if (put_user(sizeof(*up), optlen) ||
  386. !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  387. __put_user(ktime.tv_sec, &up->tv_sec) ||
  388. __put_user(ktime.tv_usec, &up->tv_usec))
  389. err = -EFAULT;
  390. }
  391. return err;
  392. }
  393. static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
  394. char __user *optval, int __user *optlen)
  395. {
  396. if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
  397. return do_get_sock_timeout(sock, level, optname, optval, optlen);
  398. return sock_getsockopt(sock, level, optname, optval, optlen);
  399. }
  400. int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  401. {
  402. struct compat_timeval __user *ctv =
  403. (struct compat_timeval __user *) userstamp;
  404. int err = -ENOENT;
  405. struct timeval tv;
  406. if (!sock_flag(sk, SOCK_TIMESTAMP))
  407. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  408. tv = ktime_to_timeval(sk->sk_stamp);
  409. if (tv.tv_sec == -1)
  410. return err;
  411. if (tv.tv_sec == 0) {
  412. sk->sk_stamp = ktime_get_real();
  413. tv = ktime_to_timeval(sk->sk_stamp);
  414. }
  415. err = 0;
  416. if (put_user(tv.tv_sec, &ctv->tv_sec) ||
  417. put_user(tv.tv_usec, &ctv->tv_usec))
  418. err = -EFAULT;
  419. return err;
  420. }
  421. EXPORT_SYMBOL(compat_sock_get_timestamp);
  422. int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
  423. {
  424. struct compat_timespec __user *ctv =
  425. (struct compat_timespec __user *) userstamp;
  426. int err = -ENOENT;
  427. struct timespec ts;
  428. if (!sock_flag(sk, SOCK_TIMESTAMP))
  429. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  430. ts = ktime_to_timespec(sk->sk_stamp);
  431. if (ts.tv_sec == -1)
  432. return err;
  433. if (ts.tv_sec == 0) {
  434. sk->sk_stamp = ktime_get_real();
  435. ts = ktime_to_timespec(sk->sk_stamp);
  436. }
  437. err = 0;
  438. if (put_user(ts.tv_sec, &ctv->tv_sec) ||
  439. put_user(ts.tv_nsec, &ctv->tv_nsec))
  440. err = -EFAULT;
  441. return err;
  442. }
  443. EXPORT_SYMBOL(compat_sock_get_timestampns);
  444. asmlinkage long compat_sys_getsockopt(int fd, int level, int optname,
  445. char __user *optval, int __user *optlen)
  446. {
  447. int err;
  448. struct socket *sock = sockfd_lookup(fd, &err);
  449. if (sock) {
  450. err = security_socket_getsockopt(sock, level, optname);
  451. if (err) {
  452. sockfd_put(sock);
  453. return err;
  454. }
  455. if (level == SOL_SOCKET)
  456. err = compat_sock_getsockopt(sock, level,
  457. optname, optval, optlen);
  458. else if (sock->ops->compat_getsockopt)
  459. err = sock->ops->compat_getsockopt(sock, level,
  460. optname, optval, optlen);
  461. else
  462. err = sock->ops->getsockopt(sock, level,
  463. optname, optval, optlen);
  464. sockfd_put(sock);
  465. }
  466. return err;
  467. }
  468. struct compat_group_req {
  469. __u32 gr_interface;
  470. struct __kernel_sockaddr_storage gr_group
  471. __attribute__ ((aligned(4)));
  472. } __packed;
  473. struct compat_group_source_req {
  474. __u32 gsr_interface;
  475. struct __kernel_sockaddr_storage gsr_group
  476. __attribute__ ((aligned(4)));
  477. struct __kernel_sockaddr_storage gsr_source
  478. __attribute__ ((aligned(4)));
  479. } __packed;
  480. struct compat_group_filter {
  481. __u32 gf_interface;
  482. struct __kernel_sockaddr_storage gf_group
  483. __attribute__ ((aligned(4)));
  484. __u32 gf_fmode;
  485. __u32 gf_numsrc;
  486. struct __kernel_sockaddr_storage gf_slist[1]
  487. __attribute__ ((aligned(4)));
  488. } __packed;
  489. #define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \
  490. sizeof(struct __kernel_sockaddr_storage))
  491. int compat_mc_setsockopt(struct sock *sock, int level, int optname,
  492. char __user *optval, unsigned int optlen,
  493. int (*setsockopt)(struct sock *, int, int, char __user *, unsigned int))
  494. {
  495. char __user *koptval = optval;
  496. int koptlen = optlen;
  497. switch (optname) {
  498. case MCAST_JOIN_GROUP:
  499. case MCAST_LEAVE_GROUP:
  500. {
  501. struct compat_group_req __user *gr32 = (void *)optval;
  502. struct group_req __user *kgr =
  503. compat_alloc_user_space(sizeof(struct group_req));
  504. u32 interface;
  505. if (!access_ok(VERIFY_READ, gr32, sizeof(*gr32)) ||
  506. !access_ok(VERIFY_WRITE, kgr, sizeof(struct group_req)) ||
  507. __get_user(interface, &gr32->gr_interface) ||
  508. __put_user(interface, &kgr->gr_interface) ||
  509. copy_in_user(&kgr->gr_group, &gr32->gr_group,
  510. sizeof(kgr->gr_group)))
  511. return -EFAULT;
  512. koptval = (char __user *)kgr;
  513. koptlen = sizeof(struct group_req);
  514. break;
  515. }
  516. case MCAST_JOIN_SOURCE_GROUP:
  517. case MCAST_LEAVE_SOURCE_GROUP:
  518. case MCAST_BLOCK_SOURCE:
  519. case MCAST_UNBLOCK_SOURCE:
  520. {
  521. struct compat_group_source_req __user *gsr32 = (void *)optval;
  522. struct group_source_req __user *kgsr = compat_alloc_user_space(
  523. sizeof(struct group_source_req));
  524. u32 interface;
  525. if (!access_ok(VERIFY_READ, gsr32, sizeof(*gsr32)) ||
  526. !access_ok(VERIFY_WRITE, kgsr,
  527. sizeof(struct group_source_req)) ||
  528. __get_user(interface, &gsr32->gsr_interface) ||
  529. __put_user(interface, &kgsr->gsr_interface) ||
  530. copy_in_user(&kgsr->gsr_group, &gsr32->gsr_group,
  531. sizeof(kgsr->gsr_group)) ||
  532. copy_in_user(&kgsr->gsr_source, &gsr32->gsr_source,
  533. sizeof(kgsr->gsr_source)))
  534. return -EFAULT;
  535. koptval = (char __user *)kgsr;
  536. koptlen = sizeof(struct group_source_req);
  537. break;
  538. }
  539. case MCAST_MSFILTER:
  540. {
  541. struct compat_group_filter __user *gf32 = (void *)optval;
  542. struct group_filter __user *kgf;
  543. u32 interface, fmode, numsrc;
  544. if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
  545. __get_user(interface, &gf32->gf_interface) ||
  546. __get_user(fmode, &gf32->gf_fmode) ||
  547. __get_user(numsrc, &gf32->gf_numsrc))
  548. return -EFAULT;
  549. koptlen = optlen + sizeof(struct group_filter) -
  550. sizeof(struct compat_group_filter);
  551. if (koptlen < GROUP_FILTER_SIZE(numsrc))
  552. return -EINVAL;
  553. kgf = compat_alloc_user_space(koptlen);
  554. if (!access_ok(VERIFY_WRITE, kgf, koptlen) ||
  555. __put_user(interface, &kgf->gf_interface) ||
  556. __put_user(fmode, &kgf->gf_fmode) ||
  557. __put_user(numsrc, &kgf->gf_numsrc) ||
  558. copy_in_user(&kgf->gf_group, &gf32->gf_group,
  559. sizeof(kgf->gf_group)) ||
  560. (numsrc && copy_in_user(kgf->gf_slist, gf32->gf_slist,
  561. numsrc * sizeof(kgf->gf_slist[0]))))
  562. return -EFAULT;
  563. koptval = (char __user *)kgf;
  564. break;
  565. }
  566. default:
  567. break;
  568. }
  569. return setsockopt(sock, level, optname, koptval, koptlen);
  570. }
  571. EXPORT_SYMBOL(compat_mc_setsockopt);
  572. int compat_mc_getsockopt(struct sock *sock, int level, int optname,
  573. char __user *optval, int __user *optlen,
  574. int (*getsockopt)(struct sock *, int, int, char __user *, int __user *))
  575. {
  576. struct compat_group_filter __user *gf32 = (void *)optval;
  577. struct group_filter __user *kgf;
  578. int __user *koptlen;
  579. u32 interface, fmode, numsrc;
  580. int klen, ulen, err;
  581. if (optname != MCAST_MSFILTER)
  582. return getsockopt(sock, level, optname, optval, optlen);
  583. koptlen = compat_alloc_user_space(sizeof(*koptlen));
  584. if (!access_ok(VERIFY_READ, optlen, sizeof(*optlen)) ||
  585. __get_user(ulen, optlen))
  586. return -EFAULT;
  587. /* adjust len for pad */
  588. klen = ulen + sizeof(*kgf) - sizeof(*gf32);
  589. if (klen < GROUP_FILTER_SIZE(0))
  590. return -EINVAL;
  591. if (!access_ok(VERIFY_WRITE, koptlen, sizeof(*koptlen)) ||
  592. __put_user(klen, koptlen))
  593. return -EFAULT;
  594. /* have to allow space for previous compat_alloc_user_space, too */
  595. kgf = compat_alloc_user_space(klen+sizeof(*optlen));
  596. if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
  597. __get_user(interface, &gf32->gf_interface) ||
  598. __get_user(fmode, &gf32->gf_fmode) ||
  599. __get_user(numsrc, &gf32->gf_numsrc) ||
  600. __put_user(interface, &kgf->gf_interface) ||
  601. __put_user(fmode, &kgf->gf_fmode) ||
  602. __put_user(numsrc, &kgf->gf_numsrc) ||
  603. copy_in_user(&kgf->gf_group, &gf32->gf_group, sizeof(kgf->gf_group)))
  604. return -EFAULT;
  605. err = getsockopt(sock, level, optname, (char __user *)kgf, koptlen);
  606. if (err)
  607. return err;
  608. if (!access_ok(VERIFY_READ, koptlen, sizeof(*koptlen)) ||
  609. __get_user(klen, koptlen))
  610. return -EFAULT;
  611. ulen = klen - (sizeof(*kgf)-sizeof(*gf32));
  612. if (!access_ok(VERIFY_WRITE, optlen, sizeof(*optlen)) ||
  613. __put_user(ulen, optlen))
  614. return -EFAULT;
  615. if (!access_ok(VERIFY_READ, kgf, klen) ||
  616. !access_ok(VERIFY_WRITE, gf32, ulen) ||
  617. __get_user(interface, &kgf->gf_interface) ||
  618. __get_user(fmode, &kgf->gf_fmode) ||
  619. __get_user(numsrc, &kgf->gf_numsrc) ||
  620. __put_user(interface, &gf32->gf_interface) ||
  621. __put_user(fmode, &gf32->gf_fmode) ||
  622. __put_user(numsrc, &gf32->gf_numsrc))
  623. return -EFAULT;
  624. if (numsrc) {
  625. int copylen;
  626. klen -= GROUP_FILTER_SIZE(0);
  627. copylen = numsrc * sizeof(gf32->gf_slist[0]);
  628. if (copylen > klen)
  629. copylen = klen;
  630. if (copy_in_user(gf32->gf_slist, kgf->gf_slist, copylen))
  631. return -EFAULT;
  632. }
  633. return err;
  634. }
  635. EXPORT_SYMBOL(compat_mc_getsockopt);
  636. /* Argument list sizes for compat_sys_socketcall */
  637. #define AL(x) ((x) * sizeof(u32))
  638. static unsigned char nas[21] = {
  639. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  640. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  641. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  642. AL(4), AL(5), AL(4)
  643. };
  644. #undef AL
  645. asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags)
  646. {
  647. return sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
  648. }
  649. asmlinkage long compat_sys_sendmmsg(int fd, struct compat_mmsghdr __user *mmsg,
  650. unsigned vlen, unsigned int flags)
  651. {
  652. return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  653. flags | MSG_CMSG_COMPAT);
  654. }
  655. asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg, unsigned int flags)
  656. {
  657. return sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
  658. }
  659. asmlinkage long compat_sys_recv(int fd, void __user *buf, size_t len, unsigned flags)
  660. {
  661. return sys_recv(fd, buf, len, flags | MSG_CMSG_COMPAT);
  662. }
  663. asmlinkage long compat_sys_recvfrom(int fd, void __user *buf, size_t len,
  664. unsigned flags, struct sockaddr __user *addr,
  665. int __user *addrlen)
  666. {
  667. return sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, addrlen);
  668. }
  669. asmlinkage long compat_sys_recvmmsg(int fd, struct compat_mmsghdr __user *mmsg,
  670. unsigned vlen, unsigned int flags,
  671. struct compat_timespec __user *timeout)
  672. {
  673. int datagrams;
  674. struct timespec ktspec;
  675. if (timeout == NULL)
  676. return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  677. flags | MSG_CMSG_COMPAT, NULL);
  678. if (get_compat_timespec(&ktspec, timeout))
  679. return -EFAULT;
  680. datagrams = __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  681. flags | MSG_CMSG_COMPAT, &ktspec);
  682. if (datagrams > 0 && put_compat_timespec(&ktspec, timeout))
  683. datagrams = -EFAULT;
  684. return datagrams;
  685. }
  686. asmlinkage long compat_sys_socketcall(int call, u32 __user *args)
  687. {
  688. int ret;
  689. u32 a[6];
  690. u32 a0, a1;
  691. if (call < SYS_SOCKET || call > SYS_SENDMMSG)
  692. return -EINVAL;
  693. if (copy_from_user(a, args, nas[call]))
  694. return -EFAULT;
  695. a0 = a[0];
  696. a1 = a[1];
  697. switch (call) {
  698. case SYS_SOCKET:
  699. ret = sys_socket(a0, a1, a[2]);
  700. break;
  701. case SYS_BIND:
  702. ret = sys_bind(a0, compat_ptr(a1), a[2]);
  703. break;
  704. case SYS_CONNECT:
  705. ret = sys_connect(a0, compat_ptr(a1), a[2]);
  706. break;
  707. case SYS_LISTEN:
  708. ret = sys_listen(a0, a1);
  709. break;
  710. case SYS_ACCEPT:
  711. ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
  712. break;
  713. case SYS_GETSOCKNAME:
  714. ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
  715. break;
  716. case SYS_GETPEERNAME:
  717. ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
  718. break;
  719. case SYS_SOCKETPAIR:
  720. ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
  721. break;
  722. case SYS_SEND:
  723. ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
  724. break;
  725. case SYS_SENDTO:
  726. ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
  727. break;
  728. case SYS_RECV:
  729. ret = compat_sys_recv(a0, compat_ptr(a1), a[2], a[3]);
  730. break;
  731. case SYS_RECVFROM:
  732. ret = compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
  733. compat_ptr(a[4]), compat_ptr(a[5]));
  734. break;
  735. case SYS_SHUTDOWN:
  736. ret = sys_shutdown(a0, a1);
  737. break;
  738. case SYS_SETSOCKOPT:
  739. ret = compat_sys_setsockopt(a0, a1, a[2],
  740. compat_ptr(a[3]), a[4]);
  741. break;
  742. case SYS_GETSOCKOPT:
  743. ret = compat_sys_getsockopt(a0, a1, a[2],
  744. compat_ptr(a[3]), compat_ptr(a[4]));
  745. break;
  746. case SYS_SENDMSG:
  747. ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
  748. break;
  749. case SYS_SENDMMSG:
  750. ret = compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
  751. break;
  752. case SYS_RECVMSG:
  753. ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
  754. break;
  755. case SYS_RECVMMSG:
  756. ret = compat_sys_recvmmsg(a0, compat_ptr(a1), a[2], a[3],
  757. compat_ptr(a[4]));
  758. break;
  759. case SYS_ACCEPT4:
  760. ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
  761. break;
  762. default:
  763. ret = -EINVAL;
  764. break;
  765. }
  766. return ret;
  767. }