1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* scm.c - Socket level control messages processing. 3 * 4 * Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 5 * Alignment and value checking mods by Craig Metz 6 */ 7 8 #include <linux/module.h> 9 #include <linux/signal.h> 10 #include <linux/capability.h> 11 #include <linux/errno.h> 12 #include <linux/sched.h> 13 #include <linux/sched/user.h> 14 #include <linux/mm.h> 15 #include <linux/kernel.h> 16 #include <linux/stat.h> 17 #include <linux/socket.h> 18 #include <linux/file.h> 19 #include <linux/fcntl.h> 20 #include <linux/net.h> 21 #include <linux/interrupt.h> 22 #include <linux/netdevice.h> 23 #include <linux/security.h> 24 #include <linux/pid_namespace.h> 25 #include <linux/pid.h> 26 #include <uapi/linux/pidfd.h> 27 #include <linux/pidfs.h> 28 #include <linux/nsproxy.h> 29 #include <linux/slab.h> 30 #include <linux/errqueue.h> 31 #include <linux/io_uring.h> 32 33 #include <linux/uaccess.h> 34 35 #include <net/protocol.h> 36 #include <linux/skbuff.h> 37 #include <net/sock.h> 38 #include <net/compat.h> 39 #include <net/scm.h> 40 #include <net/cls_cgroup.h> 41 #include <net/af_unix.h> 42 43 44 /* 45 * Only allow a user to send credentials, that they could set with 46 * setu(g)id. 47 */ 48 49 static __inline__ int scm_check_creds(struct ucred *creds) 50 { 51 const struct cred *cred = current_cred(); 52 kuid_t uid = make_kuid(cred->user_ns, creds->uid); 53 kgid_t gid = make_kgid(cred->user_ns, creds->gid); 54 55 if (!uid_valid(uid) || !gid_valid(gid)) 56 return -EINVAL; 57 58 if ((creds->pid == task_tgid_vnr(current) || 59 ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) && 60 ((uid_eq(uid, cred->uid) || uid_eq(uid, cred->euid) || 61 uid_eq(uid, cred->suid)) || ns_capable(cred->user_ns, CAP_SETUID)) && 62 ((gid_eq(gid, cred->gid) || gid_eq(gid, cred->egid) || 63 gid_eq(gid, cred->sgid)) || ns_capable(cred->user_ns, CAP_SETGID))) { 64 return 0; 65 } 66 return -EPERM; 67 } 68 69 static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp) 70 { 71 int *fdp = (int*)CMSG_DATA(cmsg); 72 struct scm_fp_list *fpl = *fplp; 73 struct file **fpp; 74 int i, num; 75 76 num = (cmsg->cmsg_len - sizeof(struct cmsghdr))/sizeof(int); 77 78 if (num <= 0) 79 return 0; 80 81 if (num > SCM_MAX_FD) 82 return -EINVAL; 83 84 if (!fpl) 85 { 86 fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL_ACCOUNT); 87 if (!fpl) 88 return -ENOMEM; 89 *fplp = fpl; 90 fpl->count = 0; 91 fpl->count_unix = 0; 92 fpl->max = SCM_MAX_FD; 93 fpl->user = NULL; 94 #if IS_ENABLED(CONFIG_UNIX) 95 fpl->inflight = false; 96 fpl->dead = false; 97 fpl->edges = NULL; 98 INIT_LIST_HEAD(&fpl->vertices); 99 #endif 100 } 101 fpp = &fpl->fp[fpl->count]; 102 103 if (fpl->count + num > fpl->max) 104 return -EINVAL; 105 106 /* 107 * Verify the descriptors and increment the usage count. 108 */ 109 110 for (i=0; i< num; i++) 111 { 112 int fd = fdp[i]; 113 struct file *file; 114 115 if (fd < 0 || !(file = fget_raw(fd))) 116 return -EBADF; 117 /* don't allow io_uring files */ 118 if (io_is_uring_fops(file)) { 119 fput(file); 120 return -EINVAL; 121 } 122 if (unix_get_socket(file)) 123 fpl->count_unix++; 124 125 *fpp++ = file; 126 fpl->count++; 127 } 128 129 if (!fpl->user) 130 fpl->user = get_uid(current_user()); 131 132 return num; 133 } 134 135 void __scm_destroy(struct scm_cookie *scm) 136 { 137 struct scm_fp_list *fpl = scm->fp; 138 int i; 139 140 if (fpl) { 141 scm->fp = NULL; 142 for (i=fpl->count-1; i>=0; i--) 143 fput(fpl->fp[i]); 144 free_uid(fpl->user); 145 kfree(fpl); 146 } 147 } 148 EXPORT_SYMBOL(__scm_destroy); 149 150 static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid) 151 { 152 int err; 153 154 /* drop all previous references */ 155 scm_destroy_cred(scm); 156 157 err = pidfs_register_pid(pid); 158 if (unlikely(err)) 159 return err; 160 161 scm->pid = pid; 162 scm->creds.pid = pid_vnr(pid); 163 return 0; 164 } 165 166 int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p) 167 { 168 const struct proto_ops *ops = READ_ONCE(sock->ops); 169 struct cmsghdr *cmsg; 170 int err; 171 172 for_each_cmsghdr(cmsg, msg) { 173 err = -EINVAL; 174 175 /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */ 176 /* The first check was omitted in <= 2.2.5. The reasoning was 177 that parser checks cmsg_len in any case, so that 178 additional check would be work duplication. 179 But if cmsg_level is not SOL_SOCKET, we do not check 180 for too short ancillary data object at all! Oops. 181 OK, let's add it... 182 */ 183 if (!CMSG_OK(msg, cmsg)) 184 goto error; 185 186 if (cmsg->cmsg_level != SOL_SOCKET) 187 continue; 188 189 switch (cmsg->cmsg_type) 190 { 191 case SCM_RIGHTS: 192 if (!ops || ops->family != PF_UNIX) 193 goto error; 194 err=scm_fp_copy(cmsg, &p->fp); 195 if (err<0) 196 goto error; 197 break; 198 case SCM_CREDENTIALS: 199 { 200 struct ucred creds; 201 kuid_t uid; 202 kgid_t gid; 203 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred))) 204 goto error; 205 memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred)); 206 err = scm_check_creds(&creds); 207 if (err) 208 goto error; 209 210 if (!p->pid || pid_vnr(p->pid) != creds.pid) { 211 struct pid *pid; 212 err = -ESRCH; 213 pid = find_get_pid(creds.pid); 214 if (!pid) 215 goto error; 216 217 /* pass a struct pid reference from 218 * find_get_pid() to scm_replace_pid(). 219 */ 220 err = scm_replace_pid(p, pid); 221 if (err) { 222 put_pid(pid); 223 goto error; 224 } 225 } 226 227 err = -EINVAL; 228 uid = make_kuid(current_user_ns(), creds.uid); 229 gid = make_kgid(current_user_ns(), creds.gid); 230 if (!uid_valid(uid) || !gid_valid(gid)) 231 goto error; 232 233 p->creds.uid = uid; 234 p->creds.gid = gid; 235 break; 236 } 237 default: 238 goto error; 239 } 240 } 241 242 if (p->fp && !p->fp->count) 243 { 244 kfree(p->fp); 245 p->fp = NULL; 246 } 247 return 0; 248 249 error: 250 scm_destroy(p); 251 return err; 252 } 253 EXPORT_SYMBOL(__scm_send); 254 255 int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data) 256 { 257 int cmlen = CMSG_LEN(len); 258 259 if (msg->msg_flags & MSG_CMSG_COMPAT) 260 return put_cmsg_compat(msg, level, type, len, data); 261 262 if (!msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) { 263 msg->msg_flags |= MSG_CTRUNC; 264 return 0; /* XXX: return error? check spec. */ 265 } 266 if (msg->msg_controllen < cmlen) { 267 msg->msg_flags |= MSG_CTRUNC; 268 cmlen = msg->msg_controllen; 269 } 270 271 if (msg->msg_control_is_user) { 272 struct cmsghdr __user *cm = msg->msg_control_user; 273 274 check_object_size(data, cmlen - sizeof(*cm), true); 275 276 if (!user_write_access_begin(cm, cmlen)) 277 goto efault; 278 279 unsafe_put_user(cmlen, &cm->cmsg_len, efault_end); 280 unsafe_put_user(level, &cm->cmsg_level, efault_end); 281 unsafe_put_user(type, &cm->cmsg_type, efault_end); 282 unsafe_copy_to_user(CMSG_USER_DATA(cm), data, 283 cmlen - sizeof(*cm), efault_end); 284 user_write_access_end(); 285 } else { 286 struct cmsghdr *cm = msg->msg_control; 287 288 cm->cmsg_level = level; 289 cm->cmsg_type = type; 290 cm->cmsg_len = cmlen; 291 memcpy(CMSG_DATA(cm), data, cmlen - sizeof(*cm)); 292 } 293 294 cmlen = min(CMSG_SPACE(len), msg->msg_controllen); 295 if (msg->msg_control_is_user) 296 msg->msg_control_user += cmlen; 297 else 298 msg->msg_control += cmlen; 299 msg->msg_controllen -= cmlen; 300 return 0; 301 302 efault_end: 303 user_write_access_end(); 304 efault: 305 return -EFAULT; 306 } 307 EXPORT_SYMBOL(put_cmsg); 308 309 int put_cmsg_notrunc(struct msghdr *msg, int level, int type, int len, 310 void *data) 311 { 312 /* Don't produce truncated CMSGs */ 313 if (!msg->msg_control || msg->msg_controllen < CMSG_LEN(len)) 314 return -ETOOSMALL; 315 316 return put_cmsg(msg, level, type, len, data); 317 } 318 319 void put_cmsg_scm_timestamping64(struct msghdr *msg, struct scm_timestamping_internal *tss_internal) 320 { 321 struct scm_timestamping64 tss; 322 int i; 323 324 for (i = 0; i < ARRAY_SIZE(tss.ts); i++) { 325 tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec; 326 tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec; 327 } 328 329 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_NEW, sizeof(tss), &tss); 330 } 331 EXPORT_SYMBOL(put_cmsg_scm_timestamping64); 332 333 void put_cmsg_scm_timestamping(struct msghdr *msg, struct scm_timestamping_internal *tss_internal) 334 { 335 struct scm_timestamping tss; 336 int i; 337 338 for (i = 0; i < ARRAY_SIZE(tss.ts); i++) { 339 tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec; 340 tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec; 341 } 342 343 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_OLD, sizeof(tss), &tss); 344 } 345 EXPORT_SYMBOL(put_cmsg_scm_timestamping); 346 347 static int scm_max_fds(struct msghdr *msg) 348 { 349 if (msg->msg_controllen <= sizeof(struct cmsghdr)) 350 return 0; 351 return (msg->msg_controllen - sizeof(struct cmsghdr)) / sizeof(int); 352 } 353 354 void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm) 355 { 356 struct cmsghdr __user *cm = 357 (__force struct cmsghdr __user *)msg->msg_control_user; 358 unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0; 359 int fdmax = min_t(int, scm_max_fds(msg), scm->fp->count); 360 int __user *cmsg_data = CMSG_USER_DATA(cm); 361 int err = 0, i; 362 363 /* no use for FD passing from kernel space callers */ 364 if (WARN_ON_ONCE(!msg->msg_control_is_user)) 365 return; 366 367 if (msg->msg_flags & MSG_CMSG_COMPAT) { 368 scm_detach_fds_compat(msg, scm); 369 return; 370 } 371 372 for (i = 0; i < fdmax; i++) { 373 err = scm_recv_one_fd(scm->fp->fp[i], cmsg_data + i, o_flags); 374 if (err < 0) 375 break; 376 } 377 378 if (i > 0) { 379 int cmlen = CMSG_LEN(i * sizeof(int)); 380 381 err = put_user(SOL_SOCKET, &cm->cmsg_level); 382 if (!err) 383 err = put_user(SCM_RIGHTS, &cm->cmsg_type); 384 if (!err) 385 err = put_user(cmlen, &cm->cmsg_len); 386 if (!err) { 387 cmlen = CMSG_SPACE(i * sizeof(int)); 388 if (msg->msg_controllen < cmlen) 389 cmlen = msg->msg_controllen; 390 msg->msg_control_user += cmlen; 391 msg->msg_controllen -= cmlen; 392 } 393 } 394 395 if (i < scm->fp->count || (scm->fp->count && fdmax <= 0)) 396 msg->msg_flags |= MSG_CTRUNC; 397 398 /* 399 * All of the files that fit in the message have had their usage counts 400 * incremented, so we just free the list. 401 */ 402 __scm_destroy(scm); 403 } 404 EXPORT_SYMBOL(scm_detach_fds); 405 406 struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl) 407 { 408 struct scm_fp_list *new_fpl; 409 int i; 410 411 if (!fpl) 412 return NULL; 413 414 new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]), 415 GFP_KERNEL_ACCOUNT); 416 if (new_fpl) { 417 for (i = 0; i < fpl->count; i++) 418 get_file(fpl->fp[i]); 419 420 new_fpl->max = new_fpl->count; 421 new_fpl->user = get_uid(fpl->user); 422 #if IS_ENABLED(CONFIG_UNIX) 423 new_fpl->inflight = false; 424 new_fpl->edges = NULL; 425 INIT_LIST_HEAD(&new_fpl->vertices); 426 #endif 427 } 428 return new_fpl; 429 } 430 EXPORT_SYMBOL(scm_fp_dup); 431 432 #ifdef CONFIG_SECURITY_NETWORK 433 static void scm_passec(struct sock *sk, struct msghdr *msg, struct scm_cookie *scm) 434 { 435 struct lsm_context ctx; 436 int err; 437 438 if (sk->sk_scm_security) { 439 err = security_secid_to_secctx(scm->secid, &ctx); 440 441 if (err >= 0) { 442 put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, ctx.len, 443 ctx.context); 444 445 security_release_secctx(&ctx); 446 } 447 } 448 } 449 450 static bool scm_has_secdata(struct sock *sk) 451 { 452 return sk->sk_scm_security; 453 } 454 #else 455 static void scm_passec(struct sock *sk, struct msghdr *msg, struct scm_cookie *scm) 456 { 457 } 458 459 static bool scm_has_secdata(struct sock *sk) 460 { 461 return false; 462 } 463 #endif 464 465 static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm) 466 { 467 struct file *pidfd_file = NULL; 468 int len, pidfd; 469 470 /* put_cmsg() doesn't return an error if CMSG is truncated, 471 * that's why we need to opencode these checks here. 472 */ 473 if (msg->msg_flags & MSG_CMSG_COMPAT) 474 len = sizeof(struct compat_cmsghdr) + sizeof(int); 475 else 476 len = sizeof(struct cmsghdr) + sizeof(int); 477 478 if (msg->msg_controllen < len) { 479 msg->msg_flags |= MSG_CTRUNC; 480 return; 481 } 482 483 if (!scm->pid) 484 return; 485 486 pidfd = pidfd_prepare(scm->pid, PIDFD_STALE, &pidfd_file); 487 488 if (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), &pidfd)) { 489 if (pidfd_file) { 490 put_unused_fd(pidfd); 491 fput(pidfd_file); 492 } 493 494 return; 495 } 496 497 if (pidfd_file) 498 fd_install(pidfd, pidfd_file); 499 } 500 501 static bool __scm_recv_common(struct sock *sk, struct msghdr *msg, 502 struct scm_cookie *scm, int flags) 503 { 504 if (!msg->msg_control) { 505 if (sk->sk_scm_credentials || sk->sk_scm_pidfd || 506 scm->fp || scm_has_secdata(sk)) 507 msg->msg_flags |= MSG_CTRUNC; 508 509 scm_destroy(scm); 510 return false; 511 } 512 513 if (sk->sk_scm_credentials) { 514 struct user_namespace *current_ns = current_user_ns(); 515 struct ucred ucreds = { 516 .pid = scm->creds.pid, 517 .uid = from_kuid_munged(current_ns, scm->creds.uid), 518 .gid = from_kgid_munged(current_ns, scm->creds.gid), 519 }; 520 521 put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(ucreds), &ucreds); 522 } 523 524 scm_passec(sk, msg, scm); 525 526 if (scm->fp) 527 scm_detach_fds(msg, scm); 528 529 return true; 530 } 531 532 void scm_recv(struct socket *sock, struct msghdr *msg, 533 struct scm_cookie *scm, int flags) 534 { 535 if (!__scm_recv_common(sock->sk, msg, scm, flags)) 536 return; 537 538 scm_destroy_cred(scm); 539 } 540 EXPORT_SYMBOL(scm_recv); 541 542 void scm_recv_unix(struct socket *sock, struct msghdr *msg, 543 struct scm_cookie *scm, int flags) 544 { 545 if (!__scm_recv_common(sock->sk, msg, scm, flags)) 546 return; 547 548 if (sock->sk->sk_scm_pidfd) 549 scm_pidfd_recv(msg, scm); 550 551 scm_destroy_cred(scm); 552 } 553