/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2009, Sun Microsystems, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8* - Redistributions of source code must retain the above copyright notice,9* this list of conditions and the following disclaimer.10* - Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13* - Neither the name of Sun Microsystems, Inc. nor the names of its14* contributors may be used to endorse or promote products derived15* from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*/2930#include <sys/cdefs.h>31/*32* svc_auth_unix.c33* Handles UNIX flavor authentication parameters on the service side of rpc.34* There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.35* _svcauth_unix does full blown unix style uid,gid+gids auth,36* _svcauth_short uses a shorthand auth to index into a cache of longhand auths.37* Note: the shorthand has been gutted for efficiency.38*39* Copyright (C) 1984, Sun Microsystems, Inc.40*/4142#include <sys/param.h>43#include <sys/ucred.h>4445#include <rpc/rpc.h>4647#include <rpc/rpc_com.h>4849/*50* Unix longhand authenticator51*/52enum auth_stat53_svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)54{55enum auth_stat stat;56XDR xdrs;57int32_t *buf;58struct xucred *xcr;59uint32_t auth_len, time;6061xcr = rqst->rq_clntcred;62auth_len = (u_int)msg->rm_call.cb_cred.oa_length;63xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,64XDR_DECODE);65buf = XDR_INLINE(&xdrs, auth_len);66if (buf != NULL) {67/* 'time', 'str_len', UID, GID and 'supp_ngroups'. */68const uint32_t min_len = 5 * BYTES_PER_XDR_UNIT;69uint32_t str_len, supp_ngroups;7071if (auth_len < min_len)72goto badcred;73time = IXDR_GET_UINT32(buf);74str_len = IXDR_GET_UINT32(buf);75if (str_len > AUTH_SYS_MAX_HOSTNAME)76goto badcred;77str_len = RNDUP(str_len);78/*79* Recheck message length now that we know the value of80* 'str_len' (and that it won't cause an overflow in additions81* below) to protect access to the credentials part.82*/83if (auth_len < min_len + str_len)84goto badcred;85buf += str_len / sizeof (int32_t);86xcr->cr_uid = IXDR_GET_UINT32(buf);87xcr->cr_gid = IXDR_GET_UINT32(buf);88supp_ngroups = IXDR_GET_UINT32(buf);89/*90* See the herald comment before a similar test at the end of91* xdr_authunix_parms() for why we strictly respect RFC 5531 and92* why we may have to drop the last supplementary group when93* there are AUTH_SYS_MAX_GROUPS of them.94*/95if (supp_ngroups > AUTH_SYS_MAX_GROUPS)96goto badcred;97/*98* Final message length check, as we now know how much we will99* read in total.100*/101if (auth_len < min_len + str_len +102supp_ngroups * BYTES_PER_XDR_UNIT)103goto badcred;104105/*106* Note that 'xcr' is a 'struct xucred', which still has the107* historical layout where the effective GID is in cr_groups[0]108* and is accounted in 'cr_ngroups'.109*/110for (uint32_t i = 0; i < supp_ngroups; ++i) {111if (i < XU_NGROUPS - 1)112xcr->cr_sgroups[i] = IXDR_GET_INT32(buf);113else114buf++;115}116xcr->cr_ngroups = MIN(supp_ngroups + 1, XU_NGROUPS);117} else if (!xdr_authunix_parms(&xdrs, &time, xcr))118goto badcred;119120rqst->rq_verf = _null_auth;121stat = AUTH_OK;122done:123XDR_DESTROY(&xdrs);124125return (stat);126127badcred:128stat = AUTH_BADCRED;129goto done;130}131132133/*134* Shorthand unix authenticator135* Looks up longhand in a cache.136*/137/*ARGSUSED*/138enum auth_stat139_svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)140{141return (AUTH_REJECTEDCRED);142}143144145