compat.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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 <linux/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. struct compat_msghdr msg;
  38. ssize_t err;
  39. if (copy_from_user(&msg, umsg, sizeof(*umsg)))
  40. return -EFAULT;
  41. kmsg->msg_flags = msg.msg_flags;
  42. kmsg->msg_namelen = msg.msg_namelen;
  43. if (!msg.msg_name)
  44. kmsg->msg_namelen = 0;
  45. if (kmsg->msg_namelen < 0)
  46. return -EINVAL;
  47. if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
  48. kmsg->msg_namelen = sizeof(struct sockaddr_storage);
  49. kmsg->msg_control = compat_ptr(msg.msg_control);
  50. kmsg->msg_controllen = msg.msg_controllen;
  51. if (save_addr)
  52. *save_addr = compat_ptr(msg.msg_name);
  53. if (msg.msg_name && kmsg->msg_namelen) {
  54. if (!save_addr) {
  55. err = move_addr_to_kernel(compat_ptr(msg.msg_name),
  56. kmsg->msg_namelen,
  57. kmsg->msg_name);
  58. if (err < 0)
  59. return err;
  60. }
  61. } else {
  62. kmsg->msg_name = NULL;
  63. kmsg->msg_namelen = 0;
  64. }
  65. if (msg.msg_iovlen > UIO_MAXIOV)
  66. return -EMSGSIZE;
  67. kmsg->msg_iocb = NULL;
  68. return compat_import_iovec(save_addr ? READ : WRITE,
  69. compat_ptr(msg.msg_iov), msg.msg_iovlen,
  70. UIO_FASTIOV, iov, &kmsg->msg_iter);
  71. }
  72. /* Bleech... */
  73. #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
  74. #define CMSG_COMPAT_DATA(cmsg) \
  75. ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr)))
  76. #define CMSG_COMPAT_SPACE(len) \
  77. (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len))
  78. #define CMSG_COMPAT_LEN(len) \
  79. (sizeof(struct compat_cmsghdr) + (len))
  80. #define CMSG_COMPAT_FIRSTHDR(msg) \
  81. (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
  82. (struct compat_cmsghdr __user *)((msg)->msg_control) : \
  83. (struct compat_cmsghdr __user *)NULL)
  84. #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
  85. ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
  86. (ucmlen) <= (unsigned long) \
  87. ((mhdr)->msg_controllen - \
  88. ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
  89. static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
  90. struct compat_cmsghdr __user *cmsg, int cmsg_len)
  91. {
  92. char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
  93. if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
  94. msg->msg_controllen)
  95. return NULL;
  96. return (struct compat_cmsghdr __user *)ptr;
  97. }
  98. /* There is a lot of hair here because the alignment rules (and
  99. * thus placement) of cmsg headers and length are different for
  100. * 32-bit apps. -DaveM
  101. */
  102. int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
  103. unsigned char *stackbuf, int stackbuf_size)
  104. {
  105. struct compat_cmsghdr __user *ucmsg;
  106. struct cmsghdr *kcmsg, *kcmsg_base;
  107. compat_size_t ucmlen;
  108. __kernel_size_t kcmlen, tmp;
  109. int err = -EFAULT;
  110. BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
  111. CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
  112. kcmlen = 0;
  113. kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
  114. ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
  115. while (ucmsg != NULL) {
  116. if (get_user(ucmlen, &ucmsg->cmsg_len))
  117. return -EFAULT;
  118. /* Catch bogons. */
  119. if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
  120. return -EINVAL;
  121. tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
  122. tmp = CMSG_ALIGN(tmp);
  123. kcmlen += tmp;
  124. ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
  125. }
  126. if (kcmlen == 0)
  127. return -EINVAL;
  128. /* The kcmlen holds the 64-bit version of the control length.
  129. * It may not be modified as we do not stick it into the kmsg
  130. * until we have successfully copied over all of the data
  131. * from the user.
  132. */
  133. if (kcmlen > stackbuf_size)
  134. kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
  135. if (kcmsg == NULL)
  136. return -ENOBUFS;
  137. /* Now copy them over neatly. */
  138. memset(kcmsg, 0, kcmlen);
  139. ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
  140. while (ucmsg != NULL) {
  141. if (__get_user(ucmlen, &ucmsg->cmsg_len))
  142. goto Efault;
  143. if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
  144. goto Einval;
  145. tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
  146. if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
  147. goto Einval;
  148. kcmsg->cmsg_len = tmp;
  149. tmp = CMSG_ALIGN(tmp);
  150. if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
  151. __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
  152. copy_from_user(CMSG_DATA(kcmsg),
  153. CMSG_COMPAT_DATA(ucmsg),
  154. (ucmlen - sizeof(*ucmsg))))
  155. goto Efault;
  156. /* Advance. */
  157. kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
  158. ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
  159. }
  160. /*
  161. * check the length of messages copied in is the same as the
  162. * what we get from the first loop
  163. */
  164. if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
  165. goto Einval;
  166. /* Ok, looks like we made it. Hook it up and return success. */
  167. kmsg->msg_control = kcmsg_base;
  168. kmsg->msg_controllen = kcmlen;
  169. return 0;
  170. Einval:
  171. err = -EINVAL;
  172. Efault:
  173. if (kcmsg_base != (struct cmsghdr *)stackbuf)
  174. sock_kfree_s(sk, kcmsg_base, kcmlen);
  175. return err;
  176. }
  177. int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
  178. {
  179. struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
  180. struct compat_cmsghdr cmhdr;
  181. struct compat_timeval ctv;
  182. struct compat_timespec cts[3];
  183. int cmlen;
  184. if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
  185. kmsg->msg_flags |= MSG_CTRUNC;
  186. return 0; /* XXX: return error? check spec. */
  187. }
  188. if (!COMPAT_USE_64BIT_TIME) {
  189. if (level == SOL_SOCKET && type == SCM_TIMESTAMP) {
  190. struct timeval *tv = (struct timeval *)data;
  191. ctv.tv_sec = tv->tv_sec;
  192. ctv.tv_usec = tv->tv_usec;
  193. data = &ctv;
  194. len = sizeof(ctv);
  195. }
  196. if (level == SOL_SOCKET &&
  197. (type == SCM_TIMESTAMPNS || type == SCM_TIMESTAMPING)) {
  198. int count = type == SCM_TIMESTAMPNS ? 1 : 3;
  199. int i;
  200. struct timespec *ts = (struct timespec *)data;
  201. for (i = 0; i < count; i++) {
  202. cts[i].tv_sec = ts[i].tv_sec;
  203. cts[i].tv_nsec = ts[i].tv_nsec;
  204. }
  205. data = &cts;
  206. len = sizeof(cts[0]) * count;
  207. }
  208. }
  209. cmlen = CMSG_COMPAT_LEN(len);
  210. if (kmsg->msg_controllen < cmlen) {
  211. kmsg->msg_flags |= MSG_CTRUNC;
  212. cmlen = kmsg->msg_controllen;
  213. }
  214. cmhdr.cmsg_level = level;
  215. cmhdr.cmsg_type = type;
  216. cmhdr.cmsg_len = cmlen;
  217. if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
  218. return -EFAULT;
  219. if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
  220. return -EFAULT;
  221. cmlen = CMSG_COMPAT_SPACE(len);
  222. if (kmsg->msg_controllen < cmlen)
  223. cmlen = kmsg->msg_controllen;
  224. kmsg->msg_control += cmlen;
  225. kmsg->msg_controllen -= cmlen;
  226. return 0;
  227. }
  228. void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
  229. {
  230. struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
  231. int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
  232. int fdnum = scm->fp->count;
  233. struct file **fp = scm->fp->fp;
  234. int __user *cmfptr;
  235. int err = 0, i;
  236. if (fdnum < fdmax)
  237. fdmax = fdnum;
  238. for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
  239. int new_fd;
  240. err = security_file_receive(fp[i]);
  241. if (err)
  242. break;
  243. err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
  244. ? O_CLOEXEC : 0);
  245. if (err < 0)
  246. break;
  247. new_fd = err;
  248. err = put_user(new_fd, cmfptr);
  249. if (err) {
  250. put_unused_fd(new_fd);
  251. break;
  252. }
  253. /* Bump the usage count and install the file. */
  254. fd_install(new_fd, get_file(fp[i]));
  255. }
  256. if (i > 0) {
  257. int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
  258. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  259. if (!err)
  260. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  261. if (!err)
  262. err = put_user(cmlen, &cm->cmsg_len);
  263. if (!err) {
  264. cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
  265. kmsg->msg_control += cmlen;
  266. kmsg->msg_controllen -= cmlen;
  267. }
  268. }
  269. if (i < fdnum)
  270. kmsg->msg_flags |= MSG_CTRUNC;
  271. /*
  272. * All of the files that fit in the message have had their
  273. * usage counts incremented, so we just free the list.
  274. */
  275. __scm_destroy(scm);
  276. }
  277. /* allocate a 64-bit sock_fprog on the user stack for duration of syscall. */
  278. struct sock_fprog __user *get_compat_bpf_fprog(char __user *optval)
  279. {
  280. struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
  281. struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
  282. struct compat_sock_fprog f32;
  283. struct sock_fprog f;
  284. if (copy_from_user(&f32, fprog32, sizeof(*fprog32)))
  285. return NULL;
  286. memset(&f, 0, sizeof(f));
  287. f.len = f32.len;
  288. f.filter = compat_ptr(f32.filter);
  289. if (copy_to_user(kfprog, &f, sizeof(struct sock_fprog)))
  290. return NULL;
  291. return kfprog;
  292. }
  293. EXPORT_SYMBOL_GPL(get_compat_bpf_fprog);
  294. static int do_set_attach_filter(struct socket *sock, int level, int optname,
  295. char __user *optval, unsigned int optlen)
  296. {
  297. struct sock_fprog __user *kfprog;
  298. kfprog = get_compat_bpf_fprog(optval);
  299. if (!kfprog)
  300. return -EFAULT;
  301. return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
  302. sizeof(struct sock_fprog));
  303. }
  304. static int do_set_sock_timeout(struct socket *sock, int level,
  305. int optname, char __user *optval, unsigned int optlen)
  306. {
  307. struct compat_timeval __user *up = (struct compat_timeval __user *)optval;
  308. struct timeval ktime;
  309. mm_segment_t old_fs;
  310. int err;
  311. if (optlen < sizeof(*up))
  312. return -EINVAL;
  313. if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
  314. __get_user(ktime.tv_sec, &up->tv_sec) ||
  315. __get_user(ktime.tv_usec, &up->tv_usec))
  316. return -EFAULT;
  317. old_fs = get_fs();
  318. set_fs(KERNEL_DS);
  319. err = sock_setsockopt(sock, level, optname, (char *)&ktime, sizeof(ktime));
  320. set_fs(old_fs);
  321. return err;
  322. }
  323. static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
  324. char __user *optval, unsigned int optlen)
  325. {
  326. if (optname == SO_ATTACH_FILTER ||
  327. optname == SO_ATTACH_REUSEPORT_CBPF)
  328. return do_set_attach_filter(sock, level, optname,
  329. optval, optlen);
  330. if (!COMPAT_USE_64BIT_TIME &&
  331. (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
  332. return do_set_sock_timeout(sock, level, optname, optval, optlen);
  333. return sock_setsockopt(sock, level, optname, optval, optlen);
  334. }
  335. static int __compat_sys_setsockopt(int fd, int level, int optname,
  336. char __user *optval, unsigned int optlen)
  337. {
  338. int err;
  339. struct socket *sock = sockfd_lookup(fd, &err);
  340. if (sock) {
  341. err = security_socket_setsockopt(sock, level, optname);
  342. if (err) {
  343. sockfd_put(sock);
  344. return err;
  345. }
  346. if (level == SOL_SOCKET)
  347. err = compat_sock_setsockopt(sock, level,
  348. optname, optval, optlen);
  349. else if (sock->ops->compat_setsockopt)
  350. err = sock->ops->compat_setsockopt(sock, level,
  351. optname, optval, optlen);
  352. else
  353. err = sock->ops->setsockopt(sock, level,
  354. optname, optval, optlen);
  355. sockfd_put(sock);
  356. }
  357. return err;
  358. }
  359. COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  360. char __user *, optval, unsigned int, optlen)
  361. {
  362. return __compat_sys_setsockopt(fd, level, optname, optval, optlen);
  363. }
  364. static int do_get_sock_timeout(struct socket *sock, int level, int optname,
  365. char __user *optval, int __user *optlen)
  366. {
  367. struct compat_timeval __user *up;
  368. struct timeval ktime;
  369. mm_segment_t old_fs;
  370. int len, err;
  371. up = (struct compat_timeval __user *) optval;
  372. if (get_user(len, optlen))
  373. return -EFAULT;
  374. if (len < sizeof(*up))
  375. return -EINVAL;
  376. len = sizeof(ktime);
  377. old_fs = get_fs();
  378. set_fs(KERNEL_DS);
  379. err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
  380. set_fs(old_fs);
  381. if (!err) {
  382. if (put_user(sizeof(*up), optlen) ||
  383. !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
  384. __put_user(ktime.tv_sec, &up->tv_sec) ||
  385. __put_user(ktime.tv_usec, &up->tv_usec))
  386. err = -EFAULT;
  387. }
  388. return err;
  389. }
  390. static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
  391. char __user *optval, int __user *optlen)
  392. {
  393. if (!COMPAT_USE_64BIT_TIME &&
  394. (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
  395. return do_get_sock_timeout(sock, level, optname, optval, optlen);
  396. return sock_getsockopt(sock, level, optname, optval, optlen);
  397. }
  398. int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  399. {
  400. struct compat_timeval __user *ctv;
  401. int err;
  402. struct timeval tv;
  403. if (COMPAT_USE_64BIT_TIME)
  404. return sock_get_timestamp(sk, userstamp);
  405. ctv = (struct compat_timeval __user *) userstamp;
  406. err = -ENOENT;
  407. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  408. tv = ktime_to_timeval(sock_read_timestamp(sk));
  409. if (tv.tv_sec == -1)
  410. return err;
  411. if (tv.tv_sec == 0) {
  412. ktime_t kt = ktime_get_real();
  413. sock_write_timestamp(sk, kt);
  414. tv = ktime_to_timeval(kt);
  415. }
  416. err = 0;
  417. if (put_user(tv.tv_sec, &ctv->tv_sec) ||
  418. put_user(tv.tv_usec, &ctv->tv_usec))
  419. err = -EFAULT;
  420. return err;
  421. }
  422. EXPORT_SYMBOL(compat_sock_get_timestamp);
  423. int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
  424. {
  425. struct compat_timespec __user *ctv;
  426. int err;
  427. struct timespec ts;
  428. if (COMPAT_USE_64BIT_TIME)
  429. return sock_get_timestampns (sk, userstamp);
  430. ctv = (struct compat_timespec __user *) userstamp;
  431. err = -ENOENT;
  432. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  433. ts = ktime_to_timespec(sock_read_timestamp(sk));
  434. if (ts.tv_sec == -1)
  435. return err;
  436. if (ts.tv_sec == 0) {
  437. ktime_t kt = ktime_get_real();
  438. sock_write_timestamp(sk, kt);
  439. ts = ktime_to_timespec(kt);
  440. }
  441. err = 0;
  442. if (put_user(ts.tv_sec, &ctv->tv_sec) ||
  443. put_user(ts.tv_nsec, &ctv->tv_nsec))
  444. err = -EFAULT;
  445. return err;
  446. }
  447. EXPORT_SYMBOL(compat_sock_get_timestampns);
  448. static int __compat_sys_getsockopt(int fd, int level, int optname,
  449. char __user *optval,
  450. int __user *optlen)
  451. {
  452. int err;
  453. struct socket *sock = sockfd_lookup(fd, &err);
  454. if (sock) {
  455. err = security_socket_getsockopt(sock, level, optname);
  456. if (err) {
  457. sockfd_put(sock);
  458. return err;
  459. }
  460. if (level == SOL_SOCKET)
  461. err = compat_sock_getsockopt(sock, level,
  462. optname, optval, optlen);
  463. else if (sock->ops->compat_getsockopt)
  464. err = sock->ops->compat_getsockopt(sock, level,
  465. optname, optval, optlen);
  466. else
  467. err = sock->ops->getsockopt(sock, level,
  468. optname, optval, optlen);
  469. sockfd_put(sock);
  470. }
  471. return err;
  472. }
  473. COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  474. char __user *, optval, int __user *, optlen)
  475. {
  476. return __compat_sys_getsockopt(fd, level, optname, optval, optlen);
  477. }
  478. struct compat_group_req {
  479. __u32 gr_interface;
  480. struct __kernel_sockaddr_storage gr_group
  481. __aligned(4);
  482. } __packed;
  483. struct compat_group_source_req {
  484. __u32 gsr_interface;
  485. struct __kernel_sockaddr_storage gsr_group
  486. __aligned(4);
  487. struct __kernel_sockaddr_storage gsr_source
  488. __aligned(4);
  489. } __packed;
  490. struct compat_group_filter {
  491. __u32 gf_interface;
  492. struct __kernel_sockaddr_storage gf_group
  493. __aligned(4);
  494. __u32 gf_fmode;
  495. __u32 gf_numsrc;
  496. struct __kernel_sockaddr_storage gf_slist[1]
  497. __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. static inline long __compat_sys_sendmsg(int fd,
  656. struct compat_msghdr __user *msg,
  657. unsigned int flags)
  658. {
  659. return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
  660. flags | MSG_CMSG_COMPAT, false);
  661. }
  662. COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
  663. unsigned int, flags)
  664. {
  665. return __compat_sys_sendmsg(fd, msg, flags);
  666. }
  667. static inline long __compat_sys_sendmmsg(int fd,
  668. struct compat_mmsghdr __user *mmsg,
  669. unsigned int vlen, unsigned int flags)
  670. {
  671. return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  672. flags | MSG_CMSG_COMPAT, false);
  673. }
  674. COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
  675. unsigned int, vlen, unsigned int, flags)
  676. {
  677. return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
  678. }
  679. static inline long __compat_sys_recvmsg(int fd,
  680. struct compat_msghdr __user *msg,
  681. unsigned int flags)
  682. {
  683. return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
  684. flags | MSG_CMSG_COMPAT, false);
  685. }
  686. COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
  687. unsigned int, flags)
  688. {
  689. return __compat_sys_recvmsg(fd, msg, flags);
  690. }
  691. static inline long __compat_sys_recvfrom(int fd, void __user *buf,
  692. compat_size_t len, unsigned int flags,
  693. struct sockaddr __user *addr,
  694. int __user *addrlen)
  695. {
  696. return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
  697. addrlen);
  698. }
  699. COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
  700. {
  701. return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
  702. }
  703. COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
  704. unsigned int, flags, struct sockaddr __user *, addr,
  705. int __user *, addrlen)
  706. {
  707. return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
  708. }
  709. static int __compat_sys_recvmmsg(int fd, struct compat_mmsghdr __user *mmsg,
  710. unsigned int vlen, unsigned int flags,
  711. struct compat_timespec __user *timeout)
  712. {
  713. int datagrams;
  714. struct timespec ktspec;
  715. if (timeout == NULL)
  716. return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  717. flags | MSG_CMSG_COMPAT, NULL);
  718. if (compat_get_timespec(&ktspec, timeout))
  719. return -EFAULT;
  720. datagrams = __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
  721. flags | MSG_CMSG_COMPAT, &ktspec);
  722. if (datagrams > 0 && compat_put_timespec(&ktspec, timeout))
  723. datagrams = -EFAULT;
  724. return datagrams;
  725. }
  726. COMPAT_SYSCALL_DEFINE5(recvmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
  727. unsigned int, vlen, unsigned int, flags,
  728. struct compat_timespec __user *, timeout)
  729. {
  730. return __compat_sys_recvmmsg(fd, mmsg, vlen, flags, timeout);
  731. }
  732. COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
  733. {
  734. u32 a[AUDITSC_ARGS];
  735. unsigned int len;
  736. u32 a0, a1;
  737. int ret;
  738. if (call < SYS_SOCKET || call > SYS_SENDMMSG)
  739. return -EINVAL;
  740. len = nas[call];
  741. if (len > sizeof(a))
  742. return -EINVAL;
  743. if (copy_from_user(a, args, len))
  744. return -EFAULT;
  745. ret = audit_socketcall_compat(len / sizeof(a[0]), a);
  746. if (ret)
  747. return ret;
  748. a0 = a[0];
  749. a1 = a[1];
  750. switch (call) {
  751. case SYS_SOCKET:
  752. ret = __sys_socket(a0, a1, a[2]);
  753. break;
  754. case SYS_BIND:
  755. ret = __sys_bind(a0, compat_ptr(a1), a[2]);
  756. break;
  757. case SYS_CONNECT:
  758. ret = __sys_connect(a0, compat_ptr(a1), a[2]);
  759. break;
  760. case SYS_LISTEN:
  761. ret = __sys_listen(a0, a1);
  762. break;
  763. case SYS_ACCEPT:
  764. ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
  765. break;
  766. case SYS_GETSOCKNAME:
  767. ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
  768. break;
  769. case SYS_GETPEERNAME:
  770. ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
  771. break;
  772. case SYS_SOCKETPAIR:
  773. ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
  774. break;
  775. case SYS_SEND:
  776. ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
  777. break;
  778. case SYS_SENDTO:
  779. ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
  780. compat_ptr(a[4]), a[5]);
  781. break;
  782. case SYS_RECV:
  783. ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
  784. NULL, NULL);
  785. break;
  786. case SYS_RECVFROM:
  787. ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
  788. compat_ptr(a[4]),
  789. compat_ptr(a[5]));
  790. break;
  791. case SYS_SHUTDOWN:
  792. ret = __sys_shutdown(a0, a1);
  793. break;
  794. case SYS_SETSOCKOPT:
  795. ret = __compat_sys_setsockopt(a0, a1, a[2],
  796. compat_ptr(a[3]), a[4]);
  797. break;
  798. case SYS_GETSOCKOPT:
  799. ret = __compat_sys_getsockopt(a0, a1, a[2],
  800. compat_ptr(a[3]),
  801. compat_ptr(a[4]));
  802. break;
  803. case SYS_SENDMSG:
  804. ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
  805. break;
  806. case SYS_SENDMMSG:
  807. ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
  808. break;
  809. case SYS_RECVMSG:
  810. ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
  811. break;
  812. case SYS_RECVMMSG:
  813. ret = __compat_sys_recvmmsg(a0, compat_ptr(a1), a[2], a[3],
  814. compat_ptr(a[4]));
  815. break;
  816. case SYS_ACCEPT4:
  817. ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
  818. break;
  819. default:
  820. ret = -EINVAL;
  821. break;
  822. }
  823. return ret;
  824. }