compat.c 25 KB

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