1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c. 4 * 5 * Copyright (C) 2000 VA Linux Co 6 * Copyright (C) 2000 Don Dugger <n0ano@valinux.com> 7 * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com> 8 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) 9 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu) 10 * Copyright (C) 2000 Hewlett-Packard Co. 11 * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com> 12 * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs 13 */ 14 15 #include <linux/kernel.h> 16 #include <linux/gfp.h> 17 #include <linux/fs.h> 18 #include <linux/types.h> 19 #include <linux/file.h> 20 #include <linux/icmpv6.h> 21 #include <linux/socket.h> 22 #include <linux/syscalls.h> 23 #include <linux/filter.h> 24 #include <linux/compat.h> 25 #include <linux/security.h> 26 #include <linux/audit.h> 27 #include <linux/export.h> 28 29 #include <net/scm.h> 30 #include <net/sock.h> 31 #include <net/ip.h> 32 #include <net/ipv6.h> 33 #include <linux/uaccess.h> 34 #include <net/compat.h> 35 36 int __get_compat_msghdr(struct msghdr *kmsg, 37 struct compat_msghdr __user *umsg, 38 struct sockaddr __user **save_addr, 39 compat_uptr_t *ptr, compat_size_t *len) 40 { 41 struct compat_msghdr msg; 42 ssize_t err; 43 44 if (copy_from_user(&msg, umsg, sizeof(*umsg))) 45 return -EFAULT; 46 47 kmsg->msg_flags = msg.msg_flags; 48 kmsg->msg_namelen = msg.msg_namelen; 49 50 if (!msg.msg_name) 51 kmsg->msg_namelen = 0; 52 53 if (kmsg->msg_namelen < 0) 54 return -EINVAL; 55 56 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) 57 kmsg->msg_namelen = sizeof(struct sockaddr_storage); 58 59 kmsg->msg_control_is_user = true; 60 kmsg->msg_control_user = compat_ptr(msg.msg_control); 61 kmsg->msg_controllen = msg.msg_controllen; 62 63 if (save_addr) 64 *save_addr = compat_ptr(msg.msg_name); 65 66 if (msg.msg_name && kmsg->msg_namelen) { 67 if (!save_addr) { 68 err = move_addr_to_kernel(compat_ptr(msg.msg_name), 69 kmsg->msg_namelen, 70 kmsg->msg_name); 71 if (err < 0) 72 return err; 73 } 74 } else { 75 kmsg->msg_name = NULL; 76 kmsg->msg_namelen = 0; 77 } 78 79 if (msg.msg_iovlen > UIO_MAXIOV) 80 return -EMSGSIZE; 81 82 kmsg->msg_iocb = NULL; 83 kmsg->msg_ubuf = NULL; 84 *ptr = msg.msg_iov; 85 *len = msg.msg_iovlen; 86 return 0; 87 } 88 89 int get_compat_msghdr(struct msghdr *kmsg, 90 struct compat_msghdr __user *umsg, 91 struct sockaddr __user **save_addr, 92 struct iovec **iov) 93 { 94 compat_uptr_t ptr; 95 compat_size_t len; 96 ssize_t err; 97 98 err = __get_compat_msghdr(kmsg, umsg, save_addr, &ptr, &len); 99 if (err) 100 return err; 101 102 err = import_iovec(save_addr ? READ : WRITE, compat_ptr(ptr), len, 103 UIO_FASTIOV, iov, &kmsg->msg_iter); 104 return err < 0 ? err : 0; 105 } 106 107 /* Bleech... */ 108 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32)) 109 110 #define CMSG_COMPAT_DATA(cmsg) \ 111 ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr))) 112 #define CMSG_COMPAT_SPACE(len) \ 113 (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len)) 114 #define CMSG_COMPAT_LEN(len) \ 115 (sizeof(struct compat_cmsghdr) + (len)) 116 117 #define CMSG_COMPAT_FIRSTHDR(msg) \ 118 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \ 119 (struct compat_cmsghdr __user *)((msg)->msg_control) : \ 120 (struct compat_cmsghdr __user *)NULL) 121 122 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \ 123 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \ 124 (ucmlen) <= (unsigned long) \ 125 ((mhdr)->msg_controllen - \ 126 ((char __user *)(ucmsg) - (char __user *)(mhdr)->msg_control_user))) 127 128 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg, 129 struct compat_cmsghdr __user *cmsg, int cmsg_len) 130 { 131 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len); 132 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) > 133 msg->msg_controllen) 134 return NULL; 135 return (struct compat_cmsghdr __user *)ptr; 136 } 137 138 /* There is a lot of hair here because the alignment rules (and 139 * thus placement) of cmsg headers and length are different for 140 * 32-bit apps. -DaveM 141 */ 142 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk, 143 unsigned char *stackbuf, int stackbuf_size) 144 { 145 struct compat_cmsghdr __user *ucmsg; 146 struct cmsghdr *kcmsg, *kcmsg_base; 147 compat_size_t ucmlen; 148 __kernel_size_t kcmlen, tmp; 149 int err = -EFAULT; 150 151 BUILD_BUG_ON(sizeof(struct compat_cmsghdr) != 152 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))); 153 154 kcmlen = 0; 155 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf; 156 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); 157 while (ucmsg != NULL) { 158 if (get_user(ucmlen, &ucmsg->cmsg_len)) 159 return -EFAULT; 160 161 /* Catch bogons. */ 162 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) 163 return -EINVAL; 164 165 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr)); 166 tmp = CMSG_ALIGN(tmp); 167 kcmlen += tmp; 168 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); 169 } 170 if (kcmlen == 0) 171 return -EINVAL; 172 173 /* The kcmlen holds the 64-bit version of the control length. 174 * It may not be modified as we do not stick it into the kmsg 175 * until we have successfully copied over all of the data 176 * from the user. 177 */ 178 if (kcmlen > stackbuf_size) 179 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL); 180 if (kcmsg == NULL) 181 return -ENOMEM; 182 183 /* Now copy them over neatly. */ 184 memset(kcmsg, 0, kcmlen); 185 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); 186 while (ucmsg != NULL) { 187 struct compat_cmsghdr cmsg; 188 if (copy_from_user(&cmsg, ucmsg, sizeof(cmsg))) 189 goto Efault; 190 if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg)) 191 goto Einval; 192 tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr)); 193 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp)) 194 goto Einval; 195 kcmsg->cmsg_len = tmp; 196 kcmsg->cmsg_level = cmsg.cmsg_level; 197 kcmsg->cmsg_type = cmsg.cmsg_type; 198 tmp = CMSG_ALIGN(tmp); 199 if (copy_from_user(CMSG_DATA(kcmsg), 200 CMSG_COMPAT_DATA(ucmsg), 201 (cmsg.cmsg_len - sizeof(*ucmsg)))) 202 goto Efault; 203 204 /* Advance. */ 205 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp); 206 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, cmsg.cmsg_len); 207 } 208 209 /* 210 * check the length of messages copied in is the same as the 211 * what we get from the first loop 212 */ 213 if ((char *)kcmsg - (char *)kcmsg_base != kcmlen) 214 goto Einval; 215 216 /* Ok, looks like we made it. Hook it up and return success. */ 217 kmsg->msg_control = kcmsg_base; 218 kmsg->msg_controllen = kcmlen; 219 return 0; 220 221 Einval: 222 err = -EINVAL; 223 Efault: 224 if (kcmsg_base != (struct cmsghdr *)stackbuf) 225 sock_kfree_s(sk, kcmsg_base, kcmlen); 226 return err; 227 } 228 229 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data) 230 { 231 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; 232 struct compat_cmsghdr cmhdr; 233 struct old_timeval32 ctv; 234 struct old_timespec32 cts[3]; 235 int cmlen; 236 237 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) { 238 kmsg->msg_flags |= MSG_CTRUNC; 239 return 0; /* XXX: return error? check spec. */ 240 } 241 242 if (!COMPAT_USE_64BIT_TIME) { 243 if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) { 244 struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data; 245 ctv.tv_sec = tv->tv_sec; 246 ctv.tv_usec = tv->tv_usec; 247 data = &ctv; 248 len = sizeof(ctv); 249 } 250 if (level == SOL_SOCKET && 251 (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) { 252 int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3; 253 int i; 254 struct __kernel_old_timespec *ts = data; 255 for (i = 0; i < count; i++) { 256 cts[i].tv_sec = ts[i].tv_sec; 257 cts[i].tv_nsec = ts[i].tv_nsec; 258 } 259 data = &cts; 260 len = sizeof(cts[0]) * count; 261 } 262 } 263 264 cmlen = CMSG_COMPAT_LEN(len); 265 if (kmsg->msg_controllen < cmlen) { 266 kmsg->msg_flags |= MSG_CTRUNC; 267 cmlen = kmsg->msg_controllen; 268 } 269 cmhdr.cmsg_level = level; 270 cmhdr.cmsg_type = type; 271 cmhdr.cmsg_len = cmlen; 272 273 if (copy_to_user(cm, &cmhdr, sizeof cmhdr)) 274 return -EFAULT; 275 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr))) 276 return -EFAULT; 277 cmlen = CMSG_COMPAT_SPACE(len); 278 if (kmsg->msg_controllen < cmlen) 279 cmlen = kmsg->msg_controllen; 280 kmsg->msg_control += cmlen; 281 kmsg->msg_controllen -= cmlen; 282 return 0; 283 } 284 285 static int scm_max_fds_compat(struct msghdr *msg) 286 { 287 if (msg->msg_controllen <= sizeof(struct compat_cmsghdr)) 288 return 0; 289 return (msg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int); 290 } 291 292 void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm) 293 { 294 struct compat_cmsghdr __user *cm = 295 (struct compat_cmsghdr __user *)msg->msg_control; 296 unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0; 297 int fdmax = min_t(int, scm_max_fds_compat(msg), scm->fp->count); 298 int __user *cmsg_data = CMSG_COMPAT_DATA(cm); 299 int err = 0, i; 300 301 for (i = 0; i < fdmax; i++) { 302 err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags); 303 if (err < 0) 304 break; 305 } 306 307 if (i > 0) { 308 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int)); 309 310 err = put_user(SOL_SOCKET, &cm->cmsg_level); 311 if (!err) 312 err = put_user(SCM_RIGHTS, &cm->cmsg_type); 313 if (!err) 314 err = put_user(cmlen, &cm->cmsg_len); 315 if (!err) { 316 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int)); 317 if (msg->msg_controllen < cmlen) 318 cmlen = msg->msg_controllen; 319 msg->msg_control += cmlen; 320 msg->msg_controllen -= cmlen; 321 } 322 } 323 324 if (i < scm->fp->count || (scm->fp->count && fdmax <= 0)) 325 msg->msg_flags |= MSG_CTRUNC; 326 327 /* 328 * All of the files that fit in the message have had their usage counts 329 * incremented, so we just free the list. 330 */ 331 __scm_destroy(scm); 332 } 333 334 /* Argument list sizes for compat_sys_socketcall */ 335 #define AL(x) ((x) * sizeof(u32)) 336 static unsigned char nas[21] = { 337 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3), 338 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6), 339 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3), 340 AL(4), AL(5), AL(4) 341 }; 342 #undef AL 343 344 static inline long __compat_sys_sendmsg(int fd, 345 struct compat_msghdr __user *msg, 346 unsigned int flags) 347 { 348 return __sys_sendmsg(fd, (struct user_msghdr __user *)msg, 349 flags | MSG_CMSG_COMPAT, false); 350 } 351 352 COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg, 353 unsigned int, flags) 354 { 355 return __compat_sys_sendmsg(fd, msg, flags); 356 } 357 358 static inline long __compat_sys_sendmmsg(int fd, 359 struct compat_mmsghdr __user *mmsg, 360 unsigned int vlen, unsigned int flags) 361 { 362 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, 363 flags | MSG_CMSG_COMPAT, false); 364 } 365 366 COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg, 367 unsigned int, vlen, unsigned int, flags) 368 { 369 return __compat_sys_sendmmsg(fd, mmsg, vlen, flags); 370 } 371 372 static inline long __compat_sys_recvmsg(int fd, 373 struct compat_msghdr __user *msg, 374 unsigned int flags) 375 { 376 return __sys_recvmsg(fd, (struct user_msghdr __user *)msg, 377 flags | MSG_CMSG_COMPAT, false); 378 } 379 380 COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg, 381 unsigned int, flags) 382 { 383 return __compat_sys_recvmsg(fd, msg, flags); 384 } 385 386 static inline long __compat_sys_recvfrom(int fd, void __user *buf, 387 compat_size_t len, unsigned int flags, 388 struct sockaddr __user *addr, 389 int __user *addrlen) 390 { 391 return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, 392 addrlen); 393 } 394 395 COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags) 396 { 397 return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL); 398 } 399 400 COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len, 401 unsigned int, flags, struct sockaddr __user *, addr, 402 int __user *, addrlen) 403 { 404 return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen); 405 } 406 407 COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg, 408 unsigned int, vlen, unsigned int, flags, 409 struct __kernel_timespec __user *, timeout) 410 { 411 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, 412 flags | MSG_CMSG_COMPAT, timeout, NULL); 413 } 414 415 #ifdef CONFIG_COMPAT_32BIT_TIME 416 COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg, 417 unsigned int, vlen, unsigned int, flags, 418 struct old_timespec32 __user *, timeout) 419 { 420 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, 421 flags | MSG_CMSG_COMPAT, NULL, timeout); 422 } 423 #endif 424 425 COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args) 426 { 427 u32 a[AUDITSC_ARGS]; 428 unsigned int len; 429 u32 a0, a1; 430 int ret; 431 432 if (call < SYS_SOCKET || call > SYS_SENDMMSG) 433 return -EINVAL; 434 len = nas[call]; 435 if (len > sizeof(a)) 436 return -EINVAL; 437 438 if (copy_from_user(a, args, len)) 439 return -EFAULT; 440 441 ret = audit_socketcall_compat(len / sizeof(a[0]), a); 442 if (ret) 443 return ret; 444 445 a0 = a[0]; 446 a1 = a[1]; 447 448 switch (call) { 449 case SYS_SOCKET: 450 ret = __sys_socket(a0, a1, a[2]); 451 break; 452 case SYS_BIND: 453 ret = __sys_bind(a0, compat_ptr(a1), a[2]); 454 break; 455 case SYS_CONNECT: 456 ret = __sys_connect(a0, compat_ptr(a1), a[2]); 457 break; 458 case SYS_LISTEN: 459 ret = __sys_listen(a0, a1); 460 break; 461 case SYS_ACCEPT: 462 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0); 463 break; 464 case SYS_GETSOCKNAME: 465 ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2])); 466 break; 467 case SYS_GETPEERNAME: 468 ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2])); 469 break; 470 case SYS_SOCKETPAIR: 471 ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3])); 472 break; 473 case SYS_SEND: 474 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0); 475 break; 476 case SYS_SENDTO: 477 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], 478 compat_ptr(a[4]), a[5]); 479 break; 480 case SYS_RECV: 481 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], 482 NULL, NULL); 483 break; 484 case SYS_RECVFROM: 485 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], 486 compat_ptr(a[4]), 487 compat_ptr(a[5])); 488 break; 489 case SYS_SHUTDOWN: 490 ret = __sys_shutdown(a0, a1); 491 break; 492 case SYS_SETSOCKOPT: 493 ret = __sys_setsockopt(a0, a1, a[2], compat_ptr(a[3]), a[4]); 494 break; 495 case SYS_GETSOCKOPT: 496 ret = __sys_getsockopt(a0, a1, a[2], compat_ptr(a[3]), 497 compat_ptr(a[4])); 498 break; 499 case SYS_SENDMSG: 500 ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]); 501 break; 502 case SYS_SENDMMSG: 503 ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]); 504 break; 505 case SYS_RECVMSG: 506 ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]); 507 break; 508 case SYS_RECVMMSG: 509 ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2], 510 a[3] | MSG_CMSG_COMPAT, NULL, 511 compat_ptr(a[4])); 512 break; 513 case SYS_ACCEPT4: 514 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]); 515 break; 516 default: 517 ret = -EINVAL; 518 break; 519 } 520 return ret; 521 } 522