compat.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. /* allocate a 64-bit sock_fprog on the user stack for duration of syscall. */
  277. struct sock_fprog __user *get_compat_bpf_fprog(char __user *optval)
  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 NULL;
  290. return kfprog;
  291. }
  292. EXPORT_SYMBOL_GPL(get_compat_bpf_fprog);
  293. static int do_set_attach_filter(struct socket *sock, int level, int optname,
  294. char __user *optval, unsigned int optlen)
  295. {
  296. struct sock_fprog __user *kfprog;
  297. kfprog = get_compat_bpf_fprog(optval);
  298. if (!kfprog)
  299. return -EFAULT;
  300. return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
  301. sizeof(struct sock_fprog));
  302. }
  303. static int do_set_sock_timeout(struct socket *sock, int level,
  304. int optname, char __user *optval, unsigned int optlen)
  305. {
  306. struct compat_timeval __user *up = (struct compat_timeval __user *)optval;
  307. struct timeval ktime;
  308. mm_segment_t old_fs;
  309. int err;
  310. if (optlen < sizeof(*up))
  311. return -EINVAL;
  312. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  313. __get_user(ktime.tv_sec, &up->tv_sec) ||
  314. __get_user(ktime.tv_usec, &up->tv_usec))
  315. return -EFAULT;
  316. old_fs = get_fs();
  317. set_fs(KERNEL_DS);
  318. err = sock_setsockopt(sock, level, optname, (char *)&ktime, sizeof(ktime));
  319. set_fs(old_fs);
  320. return err;
  321. }
  322. static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
  323. char __user *optval, unsigned int optlen)
  324. {
  325. if (optname == SO_ATTACH_FILTER ||
  326. optname == SO_ATTACH_REUSEPORT_CBPF)
  327. return do_set_attach_filter(sock, level, optname,
  328. optval, optlen);
  329. if (!COMPAT_USE_64BIT_TIME &&
  330. (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
  331. return do_set_sock_timeout(sock, level, optname, optval, optlen);
  332. return sock_setsockopt(sock, level, optname, optval, optlen);
  333. }
  334. COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  335. char __user *, optval, unsigned int, optlen)
  336. {
  337. int err;
  338. struct socket *sock = sockfd_lookup(fd, &err);
  339. if (sock) {
  340. err = security_socket_setsockopt(sock, level, optname);
  341. if (err) {
  342. sockfd_put(sock);
  343. return err;
  344. }
  345. if (level == SOL_SOCKET)
  346. err = compat_sock_setsockopt(sock, level,
  347. optname, optval, optlen);
  348. else if (sock->ops->compat_setsockopt)
  349. err = sock->ops->compat_setsockopt(sock, level,
  350. optname, optval, optlen);
  351. else
  352. err = sock->ops->setsockopt(sock, level,
  353. optname, optval, optlen);
  354. sockfd_put(sock);
  355. }
  356. return err;
  357. }
  358. static int do_get_sock_timeout(struct socket *sock, int level, int optname,
  359. char __user *optval, int __user *optlen)
  360. {
  361. struct compat_timeval __user *up;
  362. struct timeval ktime;
  363. mm_segment_t old_fs;
  364. int len, err;
  365. up = (struct compat_timeval __user *) optval;
  366. if (get_user(len, optlen))
  367. return -EFAULT;
  368. if (len < sizeof(*up))
  369. return -EINVAL;
  370. len = sizeof(ktime);
  371. old_fs = get_fs();
  372. set_fs(KERNEL_DS);
  373. err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
  374. set_fs(old_fs);
  375. if (!err) {
  376. if (put_user(sizeof(*up), optlen) ||
  377. !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  378. __put_user(ktime.tv_sec, &up->tv_sec) ||
  379. __put_user(ktime.tv_usec, &up->tv_usec))
  380. err = -EFAULT;
  381. }
  382. return err;
  383. }
  384. static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
  385. char __user *optval, int __user *optlen)
  386. {
  387. if (!COMPAT_USE_64BIT_TIME &&
  388. (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
  389. return do_get_sock_timeout(sock, level, optname, optval, optlen);
  390. return sock_getsockopt(sock, level, optname, optval, optlen);
  391. }
  392. int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  393. {
  394. struct compat_timeval __user *ctv;
  395. int err;
  396. struct timeval tv;
  397. if (COMPAT_USE_64BIT_TIME)
  398. return sock_get_timestamp(sk, userstamp);
  399. ctv = (struct compat_timeval __user *) userstamp;
  400. err = -ENOENT;
  401. if (!sock_flag(sk, SOCK_TIMESTAMP))
  402. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  403. tv = ktime_to_timeval(sk->sk_stamp);
  404. if (tv.tv_sec == -1)
  405. return err;
  406. if (tv.tv_sec == 0) {
  407. sk->sk_stamp = ktime_get_real();
  408. tv = ktime_to_timeval(sk->sk_stamp);
  409. }
  410. err = 0;
  411. if (put_user(tv.tv_sec, &ctv->tv_sec) ||
  412. put_user(tv.tv_usec, &ctv->tv_usec))
  413. err = -EFAULT;
  414. return err;
  415. }
  416. EXPORT_SYMBOL(compat_sock_get_timestamp);
  417. int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
  418. {
  419. struct compat_timespec __user *ctv;
  420. int err;
  421. struct timespec ts;
  422. if (COMPAT_USE_64BIT_TIME)
  423. return sock_get_timestampns (sk, userstamp);
  424. ctv = (struct compat_timespec __user *) userstamp;
  425. err = -ENOENT;
  426. if (!sock_flag(sk, SOCK_TIMESTAMP))
  427. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  428. ts = ktime_to_timespec(sk->sk_stamp);
  429. if (ts.tv_sec == -1)
  430. return err;
  431. if (ts.tv_sec == 0) {
  432. sk->sk_stamp = ktime_get_real();
  433. ts = ktime_to_timespec(sk->sk_stamp);
  434. }
  435. err = 0;
  436. if (put_user(ts.tv_sec, &ctv->tv_sec) ||
  437. put_user(ts.tv_nsec, &ctv->tv_nsec))
  438. err = -EFAULT;
  439. return err;
  440. }
  441. EXPORT_SYMBOL(compat_sock_get_timestampns);
  442. COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  443. char __user *, optval, int __user *, optlen)
  444. {
  445. int err;
  446. struct socket *sock = sockfd_lookup(fd, &err);
  447. if (sock) {
  448. err = security_socket_getsockopt(sock, level, optname);
  449. if (err) {
  450. sockfd_put(sock);
  451. return err;
  452. }
  453. if (level == SOL_SOCKET)
  454. err = compat_sock_getsockopt(sock, level,
  455. optname, optval, optlen);
  456. else if (sock->ops->compat_getsockopt)
  457. err = sock->ops->compat_getsockopt(sock, level,
  458. optname, optval, optlen);
  459. else
  460. err = sock->ops->getsockopt(sock, level,
  461. optname, optval, optlen);
  462. sockfd_put(sock);
  463. }
  464. return err;
  465. }
  466. struct compat_group_req {
  467. __u32 gr_interface;
  468. struct __kernel_sockaddr_storage gr_group
  469. __aligned(4);
  470. } __packed;
  471. struct compat_group_source_req {
  472. __u32 gsr_interface;
  473. struct __kernel_sockaddr_storage gsr_group
  474. __aligned(4);
  475. struct __kernel_sockaddr_storage gsr_source
  476. __aligned(4);
  477. } __packed;
  478. struct compat_group_filter {
  479. __u32 gf_interface;
  480. struct __kernel_sockaddr_storage gf_group
  481. __aligned(4);
  482. __u32 gf_fmode;
  483. __u32 gf_numsrc;
  484. struct __kernel_sockaddr_storage gf_slist[1]
  485. __aligned(4);
  486. } __packed;
  487. #define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \
  488. sizeof(struct __kernel_sockaddr_storage))
  489. int compat_mc_setsockopt(struct sock *sock, int level, int optname,
  490. char __user *optval, unsigned int optlen,
  491. int (*setsockopt)(struct sock *, int, int, char __user *, unsigned int))
  492. {
  493. char __user *koptval = optval;
  494. int koptlen = optlen;
  495. switch (optname) {
  496. case MCAST_JOIN_GROUP:
  497. case MCAST_LEAVE_GROUP:
  498. {
  499. struct compat_group_req __user *gr32 = (void *)optval;
  500. struct group_req __user *kgr =
  501. compat_alloc_user_space(sizeof(struct group_req));
  502. u32 interface;
  503. if (!access_ok(VERIFY_READ, gr32, sizeof(*gr32)) ||
  504. !access_ok(VERIFY_WRITE, kgr, sizeof(struct group_req)) ||
  505. __get_user(interface, &gr32->gr_interface) ||
  506. __put_user(interface, &kgr->gr_interface) ||
  507. copy_in_user(&kgr->gr_group, &gr32->gr_group,
  508. sizeof(kgr->gr_group)))
  509. return -EFAULT;
  510. koptval = (char __user *)kgr;
  511. koptlen = sizeof(struct group_req);
  512. break;
  513. }
  514. case MCAST_JOIN_SOURCE_GROUP:
  515. case MCAST_LEAVE_SOURCE_GROUP:
  516. case MCAST_BLOCK_SOURCE:
  517. case MCAST_UNBLOCK_SOURCE:
  518. {
  519. struct compat_group_source_req __user *gsr32 = (void *)optval;
  520. struct group_source_req __user *kgsr = compat_alloc_user_space(
  521. sizeof(struct group_source_req));
  522. u32 interface;
  523. if (!access_ok(VERIFY_READ, gsr32, sizeof(*gsr32)) ||
  524. !access_ok(VERIFY_WRITE, kgsr,
  525. sizeof(struct group_source_req)) ||
  526. __get_user(interface, &gsr32->gsr_interface) ||
  527. __put_user(interface, &kgsr->gsr_interface) ||
  528. copy_in_user(&kgsr->gsr_group, &gsr32->gsr_group,
  529. sizeof(kgsr->gsr_group)) ||
  530. copy_in_user(&kgsr->gsr_source, &gsr32->gsr_source,
  531. sizeof(kgsr->gsr_source)))
  532. return -EFAULT;
  533. koptval = (char __user *)kgsr;
  534. koptlen = sizeof(struct group_source_req);
  535. break;
  536. }
  537. case MCAST_MSFILTER:
  538. {
  539. struct compat_group_filter __user *gf32 = (void *)optval;
  540. struct group_filter __user *kgf;
  541. u32 interface, fmode, numsrc;
  542. if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
  543. __get_user(interface, &gf32->gf_interface) ||
  544. __get_user(fmode, &gf32->gf_fmode) ||
  545. __get_user(numsrc, &gf32->gf_numsrc))
  546. return -EFAULT;
  547. koptlen = optlen + sizeof(struct group_filter) -
  548. sizeof(struct compat_group_filter);
  549. if (koptlen < GROUP_FILTER_SIZE(numsrc))
  550. return -EINVAL;
  551. kgf = compat_alloc_user_space(koptlen);
  552. if (!access_ok(VERIFY_WRITE, kgf, koptlen) ||
  553. __put_user(interface, &kgf->gf_interface) ||
  554. __put_user(fmode, &kgf->gf_fmode) ||
  555. __put_user(numsrc, &kgf->gf_numsrc) ||
  556. copy_in_user(&kgf->gf_group, &gf32->gf_group,
  557. sizeof(kgf->gf_group)) ||
  558. (numsrc && copy_in_user(kgf->gf_slist, gf32->gf_slist,
  559. numsrc * sizeof(kgf->gf_slist[0]))))
  560. return -EFAULT;
  561. koptval = (char __user *)kgf;
  562. break;
  563. }
  564. default:
  565. break;
  566. }
  567. return setsockopt(sock, level, optname, koptval, koptlen);
  568. }
  569. EXPORT_SYMBOL(compat_mc_setsockopt);
  570. int compat_mc_getsockopt(struct sock *sock, int level, int optname,
  571. char __user *optval, int __user *optlen,
  572. int (*getsockopt)(struct sock *, int, int, char __user *, int __user *))
  573. {
  574. struct compat_group_filter __user *gf32 = (void *)optval;
  575. struct group_filter __user *kgf;
  576. int __user *koptlen;
  577. u32 interface, fmode, numsrc;
  578. int klen, ulen, err;
  579. if (optname != MCAST_MSFILTER)
  580. return getsockopt(sock, level, optname, optval, optlen);
  581. koptlen = compat_alloc_user_space(sizeof(*koptlen));
  582. if (!access_ok(VERIFY_READ, optlen, sizeof(*optlen)) ||
  583. __get_user(ulen, optlen))
  584. return -EFAULT;
  585. /* adjust len for pad */
  586. klen = ulen + sizeof(*kgf) - sizeof(*gf32);
  587. if (klen < GROUP_FILTER_SIZE(0))
  588. return -EINVAL;
  589. if (!access_ok(VERIFY_WRITE, koptlen, sizeof(*koptlen)) ||
  590. __put_user(klen, koptlen))
  591. return -EFAULT;
  592. /* have to allow space for previous compat_alloc_user_space, too */
  593. kgf = compat_alloc_user_space(klen+sizeof(*optlen));
  594. if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
  595. __get_user(interface, &gf32->gf_interface) ||
  596. __get_user(fmode, &gf32->gf_fmode) ||
  597. __get_user(numsrc, &gf32->gf_numsrc) ||
  598. __put_user(interface, &kgf->gf_interface) ||
  599. __put_user(fmode, &kgf->gf_fmode) ||
  600. __put_user(numsrc, &kgf->gf_numsrc) ||
  601. copy_in_user(&kgf->gf_group, &gf32->gf_group, sizeof(kgf->gf_group)))
  602. return -EFAULT;
  603. err = getsockopt(sock, level, optname, (char __user *)kgf, koptlen);
  604. if (err)
  605. return err;
  606. if (!access_ok(VERIFY_READ, koptlen, sizeof(*koptlen)) ||
  607. __get_user(klen, koptlen))
  608. return -EFAULT;
  609. ulen = klen - (sizeof(*kgf)-sizeof(*gf32));
  610. if (!access_ok(VERIFY_WRITE, optlen, sizeof(*optlen)) ||
  611. __put_user(ulen, optlen))
  612. return -EFAULT;
  613. if (!access_ok(VERIFY_READ, kgf, klen) ||
  614. !access_ok(VERIFY_WRITE, gf32, ulen) ||
  615. __get_user(interface, &kgf->gf_interface) ||
  616. __get_user(fmode, &kgf->gf_fmode) ||
  617. __get_user(numsrc, &kgf->gf_numsrc) ||
  618. __put_user(interface, &gf32->gf_interface) ||
  619. __put_user(fmode, &gf32->gf_fmode) ||
  620. __put_user(numsrc, &gf32->gf_numsrc))
  621. return -EFAULT;
  622. if (numsrc) {
  623. int copylen;
  624. klen -= GROUP_FILTER_SIZE(0);
  625. copylen = numsrc * sizeof(gf32->gf_slist[0]);
  626. if (copylen > klen)
  627. copylen = klen;
  628. if (copy_in_user(gf32->gf_slist, kgf->gf_slist, copylen))
  629. return -EFAULT;
  630. }
  631. return err;
  632. }
  633. EXPORT_SYMBOL(compat_mc_getsockopt);
  634. /* Argument list sizes for compat_sys_socketcall */
  635. #define AL(x) ((x) * sizeof(u32))
  636. static unsigned char nas[21] = {
  637. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  638. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  639. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  640. AL(4), AL(5), AL(4)
  641. };
  642. #undef AL
  643. COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags)
  644. {
  645. return __sys_sendmsg(fd, (struct user_msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
  646. }
  647. COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
  648. unsigned int, vlen, unsigned int, flags)
  649. {
  650. return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  651. flags | MSG_CMSG_COMPAT);
  652. }
  653. COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags)
  654. {
  655. return __sys_recvmsg(fd, (struct user_msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
  656. }
  657. COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
  658. {
  659. return sys_recv(fd, buf, len, flags | MSG_CMSG_COMPAT);
  660. }
  661. COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
  662. unsigned int, flags, struct sockaddr __user *, addr,
  663. int __user *, addrlen)
  664. {
  665. return sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, addrlen);
  666. }
  667. COMPAT_SYSCALL_DEFINE5(recvmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
  668. unsigned int, vlen, unsigned int, flags,
  669. struct compat_timespec __user *, timeout)
  670. {
  671. int datagrams;
  672. struct timespec ktspec;
  673. if (timeout == NULL)
  674. return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  675. flags | MSG_CMSG_COMPAT, NULL);
  676. if (compat_get_timespec(&ktspec, timeout))
  677. return -EFAULT;
  678. datagrams = __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  679. flags | MSG_CMSG_COMPAT, &ktspec);
  680. if (datagrams > 0 && compat_put_timespec(&ktspec, timeout))
  681. datagrams = -EFAULT;
  682. return datagrams;
  683. }
  684. COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
  685. {
  686. u32 a[AUDITSC_ARGS];
  687. unsigned int len;
  688. u32 a0, a1;
  689. int ret;
  690. if (call < SYS_SOCKET || call > SYS_SENDMMSG)
  691. return -EINVAL;
  692. len = nas[call];
  693. if (len > sizeof(a))
  694. return -EINVAL;
  695. if (copy_from_user(a, args, len))
  696. return -EFAULT;
  697. ret = audit_socketcall_compat(len / sizeof(a[0]), a);
  698. if (ret)
  699. return ret;
  700. a0 = a[0];
  701. a1 = a[1];
  702. switch (call) {
  703. case SYS_SOCKET:
  704. ret = sys_socket(a0, a1, a[2]);
  705. break;
  706. case SYS_BIND:
  707. ret = sys_bind(a0, compat_ptr(a1), a[2]);
  708. break;
  709. case SYS_CONNECT:
  710. ret = sys_connect(a0, compat_ptr(a1), a[2]);
  711. break;
  712. case SYS_LISTEN:
  713. ret = sys_listen(a0, a1);
  714. break;
  715. case SYS_ACCEPT:
  716. ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
  717. break;
  718. case SYS_GETSOCKNAME:
  719. ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
  720. break;
  721. case SYS_GETPEERNAME:
  722. ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
  723. break;
  724. case SYS_SOCKETPAIR:
  725. ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
  726. break;
  727. case SYS_SEND:
  728. ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
  729. break;
  730. case SYS_SENDTO:
  731. ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
  732. break;
  733. case SYS_RECV:
  734. ret = compat_sys_recv(a0, compat_ptr(a1), a[2], a[3]);
  735. break;
  736. case SYS_RECVFROM:
  737. ret = compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
  738. compat_ptr(a[4]), compat_ptr(a[5]));
  739. break;
  740. case SYS_SHUTDOWN:
  741. ret = sys_shutdown(a0, a1);
  742. break;
  743. case SYS_SETSOCKOPT:
  744. ret = compat_sys_setsockopt(a0, a1, a[2],
  745. compat_ptr(a[3]), a[4]);
  746. break;
  747. case SYS_GETSOCKOPT:
  748. ret = compat_sys_getsockopt(a0, a1, a[2],
  749. compat_ptr(a[3]), compat_ptr(a[4]));
  750. break;
  751. case SYS_SENDMSG:
  752. ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
  753. break;
  754. case SYS_SENDMMSG:
  755. ret = compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
  756. break;
  757. case SYS_RECVMSG:
  758. ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
  759. break;
  760. case SYS_RECVMMSG:
  761. ret = compat_sys_recvmmsg(a0, compat_ptr(a1), a[2], a[3],
  762. compat_ptr(a[4]));
  763. break;
  764. case SYS_ACCEPT4:
  765. ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
  766. break;
  767. default:
  768. ret = -EINVAL;
  769. break;
  770. }
  771. return ret;
  772. }