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*da6c28aaSamw * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/param.h> 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/ucred.h> 317c478bd9Sstevel@tonic-gate #include <sys/file.h> 327c478bd9Sstevel@tonic-gate #include <sys/errno.h> 337c478bd9Sstevel@tonic-gate #include <sys/systm.h> 347c478bd9Sstevel@tonic-gate #include <sys/stream.h> 357c478bd9Sstevel@tonic-gate #include <sys/strsun.h> 367c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 377c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 387c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 407c478bd9Sstevel@tonic-gate #include <sys/socket.h> 417c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 427c478bd9Sstevel@tonic-gate #include <c2/audit.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * Getpeerucred system call implementation. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate static int 487c478bd9Sstevel@tonic-gate getpeerucred(int fd, void *buf) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate file_t *fp; 517c478bd9Sstevel@tonic-gate struct ucred_s *uc; 527c478bd9Sstevel@tonic-gate vnode_t *vp; 537c478bd9Sstevel@tonic-gate k_peercred_t kpc; 547c478bd9Sstevel@tonic-gate int err; 557c478bd9Sstevel@tonic-gate int32_t rval; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate kpc.pc_cr = NULL; 587c478bd9Sstevel@tonic-gate kpc.pc_cpid = -1; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate if ((fp = getf(fd)) == NULL) 617c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate vp = fp->f_vnode; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate switch (vp->v_type) { 667c478bd9Sstevel@tonic-gate case VFIFO: 677c478bd9Sstevel@tonic-gate case VSOCK: 687c478bd9Sstevel@tonic-gate err = VOP_IOCTL(vp, _I_GETPEERCRED, (intptr_t)&kpc, 69*da6c28aaSamw FKIOCTL, CRED(), &rval, NULL); 707c478bd9Sstevel@tonic-gate break; 717c478bd9Sstevel@tonic-gate case VCHR: { 727c478bd9Sstevel@tonic-gate struct strioctl strioc; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate if (vp->v_stream == NULL) { 757c478bd9Sstevel@tonic-gate err = ENOTSUP; 767c478bd9Sstevel@tonic-gate break; 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate strioc.ic_cmd = _I_GETPEERCRED; 797c478bd9Sstevel@tonic-gate strioc.ic_timout = INFTIM; 807c478bd9Sstevel@tonic-gate strioc.ic_len = (int)sizeof (k_peercred_t); 817c478bd9Sstevel@tonic-gate strioc.ic_dp = (char *)&kpc; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate err = strdoioctl(vp->v_stream, &strioc, FNATIVE|FKIOCTL, 847c478bd9Sstevel@tonic-gate STR_NOSIG|K_TO_K, CRED(), &rval); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Map all unexpected error codes to ENOTSUP. 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate switch (err) { 907c478bd9Sstevel@tonic-gate case 0: 917c478bd9Sstevel@tonic-gate case ENOTSUP: 927c478bd9Sstevel@tonic-gate case ENOTCONN: 937c478bd9Sstevel@tonic-gate case ENOMEM: 947c478bd9Sstevel@tonic-gate break; 957c478bd9Sstevel@tonic-gate default: 967c478bd9Sstevel@tonic-gate err = ENOTSUP; 977c478bd9Sstevel@tonic-gate break; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate break; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate default: 1027c478bd9Sstevel@tonic-gate err = ENOTSUP; 1037c478bd9Sstevel@tonic-gate break; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate releasef(fd); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * If someone gave us a credential, err will be 0. 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate if (kpc.pc_cr != NULL) { 1117c478bd9Sstevel@tonic-gate ASSERT(err == 0); 1127c478bd9Sstevel@tonic-gate 11345916cd2Sjpk uc = cred2ucred(kpc.pc_cr, kpc.pc_cpid, NULL, CRED()); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate crfree(kpc.pc_cr); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate err = copyout(uc, buf, uc->uc_size); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate kmem_free(uc, uc->uc_size); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate if (err != 0) 1227c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate return (0); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate return (set_errno(err)); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate static int 1307c478bd9Sstevel@tonic-gate ucred_get(pid_t pid, void *ubuf) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate proc_t *p; 1337c478bd9Sstevel@tonic-gate cred_t *pcr; 1347c478bd9Sstevel@tonic-gate int err; 1357c478bd9Sstevel@tonic-gate struct ucred_s *uc; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate if (pid == P_MYID || pid == curproc->p_pid) { 1387c478bd9Sstevel@tonic-gate pcr = CRED(); 1397c478bd9Sstevel@tonic-gate crhold(pcr); 1407c478bd9Sstevel@tonic-gate pid = curproc->p_pid; 1417c478bd9Sstevel@tonic-gate } else { 1427c478bd9Sstevel@tonic-gate cred_t *updcred = NULL; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate if (pid < 0) 1457c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (audit_active) 1487c478bd9Sstevel@tonic-gate updcred = cralloc(); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 1517c478bd9Sstevel@tonic-gate p = prfind(pid); 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate if (p == NULL) { 1547c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 1557c478bd9Sstevel@tonic-gate if (updcred != NULL) 1567c478bd9Sstevel@tonic-gate crfree(updcred); 1577c478bd9Sstevel@tonic-gate return (set_errno(ESRCH)); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* 1617c478bd9Sstevel@tonic-gate * Assure that audit data in cred is up-to-date. 1627c478bd9Sstevel@tonic-gate * updcred will be used or freed. 1637c478bd9Sstevel@tonic-gate */ 1647c478bd9Sstevel@tonic-gate if (audit_active) 1657c478bd9Sstevel@tonic-gate audit_update_context(p, updcred); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate err = priv_proc_cred_perm(CRED(), p, &pcr, VREAD); 1687c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate if (err != 0) 1717c478bd9Sstevel@tonic-gate return (set_errno(err)); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 17445916cd2Sjpk uc = cred2ucred(pcr, pid, NULL, CRED()); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate crfree(pcr); 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate err = copyout(uc, ubuf, uc->uc_size); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate kmem_free(uc, uc->uc_size); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (err) 1837c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate return (0); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate int 1897c478bd9Sstevel@tonic-gate ucredsys(int code, int obj, void *buf) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate switch (code) { 1927c478bd9Sstevel@tonic-gate case UCREDSYS_UCREDGET: 1937c478bd9Sstevel@tonic-gate return (ucred_get((pid_t)obj, buf)); 1947c478bd9Sstevel@tonic-gate case UCREDSYS_GETPEERUCRED: 1957c478bd9Sstevel@tonic-gate return (getpeerucred(obj, buf)); 1967c478bd9Sstevel@tonic-gate default: 1977c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2027c478bd9Sstevel@tonic-gate int 2037c478bd9Sstevel@tonic-gate ucredsys32(int arg1, int arg2, caddr32_t arg3) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate return (ucredsys(arg1, arg2, (void *)(uintptr_t)arg3)); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate #endif 208