17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*005d3febSMarek Pospisil * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/param.h> 277c478bd9Sstevel@tonic-gate #include <sys/types.h> 287c478bd9Sstevel@tonic-gate #include <sys/ucred.h> 297c478bd9Sstevel@tonic-gate #include <sys/file.h> 307c478bd9Sstevel@tonic-gate #include <sys/errno.h> 317c478bd9Sstevel@tonic-gate #include <sys/systm.h> 327c478bd9Sstevel@tonic-gate #include <sys/stream.h> 337c478bd9Sstevel@tonic-gate #include <sys/strsun.h> 347c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 357c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 367c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 387c478bd9Sstevel@tonic-gate #include <sys/socket.h> 397c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 407c478bd9Sstevel@tonic-gate #include <c2/audit.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * Getpeerucred system call implementation. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate static int 467c478bd9Sstevel@tonic-gate getpeerucred(int fd, void *buf) 477c478bd9Sstevel@tonic-gate { 487c478bd9Sstevel@tonic-gate file_t *fp; 497c478bd9Sstevel@tonic-gate struct ucred_s *uc; 507c478bd9Sstevel@tonic-gate vnode_t *vp; 517c478bd9Sstevel@tonic-gate k_peercred_t kpc; 527c478bd9Sstevel@tonic-gate int err; 537c478bd9Sstevel@tonic-gate int32_t rval; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate kpc.pc_cr = NULL; 567c478bd9Sstevel@tonic-gate kpc.pc_cpid = -1; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate if ((fp = getf(fd)) == NULL) 597c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate vp = fp->f_vnode; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate switch (vp->v_type) { 647c478bd9Sstevel@tonic-gate case VFIFO: 657c478bd9Sstevel@tonic-gate case VSOCK: 667c478bd9Sstevel@tonic-gate err = VOP_IOCTL(vp, _I_GETPEERCRED, (intptr_t)&kpc, 67da6c28aaSamw FKIOCTL, CRED(), &rval, NULL); 687c478bd9Sstevel@tonic-gate break; 697c478bd9Sstevel@tonic-gate case VCHR: { 707c478bd9Sstevel@tonic-gate struct strioctl strioc; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate if (vp->v_stream == NULL) { 737c478bd9Sstevel@tonic-gate err = ENOTSUP; 747c478bd9Sstevel@tonic-gate break; 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate strioc.ic_cmd = _I_GETPEERCRED; 777c478bd9Sstevel@tonic-gate strioc.ic_timout = INFTIM; 787c478bd9Sstevel@tonic-gate strioc.ic_len = (int)sizeof (k_peercred_t); 797c478bd9Sstevel@tonic-gate strioc.ic_dp = (char *)&kpc; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate err = strdoioctl(vp->v_stream, &strioc, FNATIVE|FKIOCTL, 827c478bd9Sstevel@tonic-gate STR_NOSIG|K_TO_K, CRED(), &rval); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* 857c478bd9Sstevel@tonic-gate * Map all unexpected error codes to ENOTSUP. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate switch (err) { 887c478bd9Sstevel@tonic-gate case 0: 897c478bd9Sstevel@tonic-gate case ENOTSUP: 907c478bd9Sstevel@tonic-gate case ENOTCONN: 917c478bd9Sstevel@tonic-gate case ENOMEM: 927c478bd9Sstevel@tonic-gate break; 937c478bd9Sstevel@tonic-gate default: 947c478bd9Sstevel@tonic-gate err = ENOTSUP; 957c478bd9Sstevel@tonic-gate break; 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate break; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate default: 1007c478bd9Sstevel@tonic-gate err = ENOTSUP; 1017c478bd9Sstevel@tonic-gate break; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate releasef(fd); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate /* 1067c478bd9Sstevel@tonic-gate * If someone gave us a credential, err will be 0. 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate if (kpc.pc_cr != NULL) { 1097c478bd9Sstevel@tonic-gate ASSERT(err == 0); 1107c478bd9Sstevel@tonic-gate 11145916cd2Sjpk uc = cred2ucred(kpc.pc_cr, kpc.pc_cpid, NULL, CRED()); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate crfree(kpc.pc_cr); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate err = copyout(uc, buf, uc->uc_size); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate kmem_free(uc, uc->uc_size); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate if (err != 0) 1207c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate return (0); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate return (set_errno(err)); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate static int 1287c478bd9Sstevel@tonic-gate ucred_get(pid_t pid, void *ubuf) 1297c478bd9Sstevel@tonic-gate { 1307c478bd9Sstevel@tonic-gate proc_t *p; 1317c478bd9Sstevel@tonic-gate cred_t *pcr; 1327c478bd9Sstevel@tonic-gate int err; 1337c478bd9Sstevel@tonic-gate struct ucred_s *uc; 134*005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING(); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate if (pid == P_MYID || pid == curproc->p_pid) { 1377c478bd9Sstevel@tonic-gate pcr = CRED(); 1387c478bd9Sstevel@tonic-gate crhold(pcr); 1397c478bd9Sstevel@tonic-gate pid = curproc->p_pid; 1407c478bd9Sstevel@tonic-gate } else { 1417c478bd9Sstevel@tonic-gate cred_t *updcred = NULL; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate if (pid < 0) 1447c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 1457c478bd9Sstevel@tonic-gate 146*005d3febSMarek Pospisil if (auditing) 1477c478bd9Sstevel@tonic-gate updcred = cralloc(); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 1507c478bd9Sstevel@tonic-gate p = prfind(pid); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate if (p == NULL) { 1537c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 1547c478bd9Sstevel@tonic-gate if (updcred != NULL) 1557c478bd9Sstevel@tonic-gate crfree(updcred); 1567c478bd9Sstevel@tonic-gate return (set_errno(ESRCH)); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate * Assure that audit data in cred is up-to-date. 1617c478bd9Sstevel@tonic-gate * updcred will be used or freed. 1627c478bd9Sstevel@tonic-gate */ 163*005d3febSMarek Pospisil if (auditing) 1647c478bd9Sstevel@tonic-gate audit_update_context(p, updcred); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate err = priv_proc_cred_perm(CRED(), p, &pcr, VREAD); 1677c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate if (err != 0) 1707c478bd9Sstevel@tonic-gate return (set_errno(err)); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 17345916cd2Sjpk uc = cred2ucred(pcr, pid, NULL, CRED()); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate crfree(pcr); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate err = copyout(uc, ubuf, uc->uc_size); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate kmem_free(uc, uc->uc_size); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate if (err) 1827c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate return (0); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate int 1887c478bd9Sstevel@tonic-gate ucredsys(int code, int obj, void *buf) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate switch (code) { 1917c478bd9Sstevel@tonic-gate case UCREDSYS_UCREDGET: 1927c478bd9Sstevel@tonic-gate return (ucred_get((pid_t)obj, buf)); 1937c478bd9Sstevel@tonic-gate case UCREDSYS_GETPEERUCRED: 1947c478bd9Sstevel@tonic-gate return (getpeerucred(obj, buf)); 1957c478bd9Sstevel@tonic-gate default: 1967c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2017c478bd9Sstevel@tonic-gate int 2027c478bd9Sstevel@tonic-gate ucredsys32(int arg1, int arg2, caddr32_t arg3) 2037c478bd9Sstevel@tonic-gate { 2047c478bd9Sstevel@tonic-gate return (ucredsys(arg1, arg2, (void *)(uintptr_t)arg3)); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate #endif 207