1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma init(init)
28 
29 #include <s10_brand.h>
30 #include <stdlib.h>
31 #include <sys/syscall.h>
32 
33 /*
34  * This is a library that is LD_PRELOADed into native processes.
35  * Its primary function is to perform one brand operation, B_S10_NATIVE,
36  * which checks that this is actually a native process.  If it is, then
37  * the operation changes the executable name so that it is no longer
38  * ld.sol.1.  Instead it changes it to be the name of the real native
39  * executable that we're runnning.  This allows things like pgrep to work
40  * as expected.  Note that this brand operation only changes the process
41  * name wrt the kernel.  From the process' perspective, the first
42  * argument and AT_SUN_EXECNAME are still ld.so.1.
43  *
44  * The library also unsets the LD_LIBRARY_PATH_* and LD_PRELOAD_*
45  * environment variables created by the brand's native wrapper scripts
46  * (e.g., s10_isaexec_wrapper) in order to ensure that execve(2) and its
47  * ilk, which brand the calling process, do not cause ld.so.1 to link native
48  * libraries to the resulting process.  The native wrapper scripts make
49  * LD_LIBRARY_PATH_* point to library directories (e.g., /usr/lib) prefixed
50  * with "/.SUNWnative" in order to make native processes link with native
51  * libraries.  However, if a native process running within a branded zone
52  * executes exec(2), then the new process becomes branded.  Therefore, if this
53  * library were to not unset the LD_LIBRARY_PATH_* environment variables, then
54  * if a native process were to invoke an exec(2) function, then the resulting
55  * process would be branded and linked with native libraries.
56  * LD_PRELOAD_*, which the native wrapper scripts set to "s10_npreload.so.1"
57  * (the name of this library), must be cleared as well because
58  * s10_npreload.so.1 is only preloaded into native processes and can only be
59  * accessed via the /.SUNWnative library paths.
60  *
61  * NOTE: This trick won't work if another library that invokes an exec(2)
62  * function in its initialization function is initialized before this library.
63  * Such a problem won't happen if the brand only replaces binaries shipped with
64  * Solaris (e.g., ifconfig(1M)) with their native counterparts because most (if
65  * not all) Solaris system libraries don't exec(2) within their initialization
66  * functions.
67  */
68 void
69 init(void)
70 {
71 	sysret_t rval;
72 
73 	(void) __systemcall(&rval, SYS_brand, B_S10_NATIVE);
74 
75 	/*
76 	 * We can safely use unsetenv(3C) to clear LD_LIBRARY_PATH_* and
77 	 * LD_PRELOAD_* because ld.so.1 caches their values before this
78 	 * library is initialized.
79 	 */
80 	(void) unsetenv("LD_LIBRARY_PATH_32");
81 	(void) unsetenv("LD_LIBRARY_PATH_64");
82 	(void) unsetenv("LD_PRELOAD_32");
83 	(void) unsetenv("LD_PRELOAD_64");
84 }
85