compat.c 24 KB

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