compat.c 25 KB

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