1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <unistd.h> 30*0Sstevel@tonic-gate #include <strings.h> 31*0Sstevel@tonic-gate #include <limits.h> 32*0Sstevel@tonic-gate #include <dlfcn.h> 33*0Sstevel@tonic-gate #include "_conv.h" 34*0Sstevel@tonic-gate #include "lddstub_msg.h" 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate static char orgstub[PATH_MAX]; 37*0Sstevel@tonic-gate static char orgstub64[PATH_MAX]; 38*0Sstevel@tonic-gate static int orgflag; 39*0Sstevel@tonic-gate static int orgflag64; 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #ifdef NEED_OWN_STRLCAT 42*0Sstevel@tonic-gate /* 43*0Sstevel@tonic-gate * Appends src to the dstsize buffer at dst. The append will never 44*0Sstevel@tonic-gate * overflow the destination buffer and the buffer will always be null 45*0Sstevel@tonic-gate * terminated. Never reference beyond &bst[dstsize-1] when computing 46*0Sstevel@tonic-gate * the length of the pre-existing string. 47*0Sstevel@tonic-gate */ 48*0Sstevel@tonic-gate size_t 49*0Sstevel@tonic-gate strlcat(char *dst, const char *src, size_t dstsize) 50*0Sstevel@tonic-gate { 51*0Sstevel@tonic-gate char *df = dst; 52*0Sstevel@tonic-gate size_t left = dstsize; 53*0Sstevel@tonic-gate size_t l1; 54*0Sstevel@tonic-gate size_t l2 = strlen(src); 55*0Sstevel@tonic-gate size_t copied; 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate while (left-- != 0 && *df != '\0') 58*0Sstevel@tonic-gate df++; 59*0Sstevel@tonic-gate l1 = df - dst; 60*0Sstevel@tonic-gate if (dstsize == l1) 61*0Sstevel@tonic-gate return (l1 + l2); 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2; 64*0Sstevel@tonic-gate memcpy(dst + l1, src, copied); 65*0Sstevel@tonic-gate dst[l1+copied] = '\0'; 66*0Sstevel@tonic-gate return (l1 + l2); 67*0Sstevel@tonic-gate } 68*0Sstevel@tonic-gate #endif 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate static int 71*0Sstevel@tonic-gate originlddstub(char *buffer, const char *orgfile) 72*0Sstevel@tonic-gate { 73*0Sstevel@tonic-gate int len; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate if (dlinfo(RTLD_SELF, RTLD_DI_ORIGIN, (void *)buffer) == -1) 76*0Sstevel@tonic-gate return (-1); 77*0Sstevel@tonic-gate if (strlcat(buffer, orgfile, PATH_MAX) >= PATH_MAX) 78*0Sstevel@tonic-gate return (-1); 79*0Sstevel@tonic-gate if ((len = resolvepath(buffer, buffer, (PATH_MAX - 1))) == -1) 80*0Sstevel@tonic-gate return (-1); 81*0Sstevel@tonic-gate buffer[len] = '\0'; 82*0Sstevel@tonic-gate if (access(orgstub, X_OK) == -1) 83*0Sstevel@tonic-gate return (-1); 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate return (0); 86*0Sstevel@tonic-gate } 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate /* 89*0Sstevel@tonic-gate * Determine what lddstub to run. 90*0Sstevel@tonic-gate */ 91*0Sstevel@tonic-gate const char * 92*0Sstevel@tonic-gate conv_lddstub(int class) 93*0Sstevel@tonic-gate { 94*0Sstevel@tonic-gate const char *stub; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate /* 97*0Sstevel@tonic-gate * Establish defaults. 98*0Sstevel@tonic-gate */ 99*0Sstevel@tonic-gate if (class == ELFCLASS32) 100*0Sstevel@tonic-gate stub = MSG_ORIG(MSG_PTH_LDDSTUB); 101*0Sstevel@tonic-gate else 102*0Sstevel@tonic-gate stub = MSG_ORIG(MSG_PTH_LDDSTUB_64); 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate /* 105*0Sstevel@tonic-gate * Provided we're not secure, determine lddstub's location from our 106*0Sstevel@tonic-gate * own origin. 107*0Sstevel@tonic-gate */ 108*0Sstevel@tonic-gate if (geteuid()) { 109*0Sstevel@tonic-gate if ((class == ELFCLASS32) && (orgflag != -1)) { 110*0Sstevel@tonic-gate if (orgflag == 0) { 111*0Sstevel@tonic-gate if ((orgflag = originlddstub(orgstub, 112*0Sstevel@tonic-gate MSG_ORIG(MSG_ORG_LDDSTUB))) == -1) 113*0Sstevel@tonic-gate return (stub); 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate stub = (const char *)orgstub; 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate if ((class == ELFCLASS64) && (orgflag64 != -1)) { 118*0Sstevel@tonic-gate if (orgflag64 == 0) { 119*0Sstevel@tonic-gate if ((orgflag64 = originlddstub(orgstub64, 120*0Sstevel@tonic-gate MSG_ORIG(MSG_ORG_LDDSTUB_64))) == -1) 121*0Sstevel@tonic-gate return (stub); 122*0Sstevel@tonic-gate } 123*0Sstevel@tonic-gate stub = (const char *)orgstub64; 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate return (stub); 127*0Sstevel@tonic-gate } 128