1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <wait.h>
#include <limits.h>
#include <gelf.h>
#include "machdep.h"
#include "sgs.h"
#include "conv.h"
#include "_crle.h"
#include "msg.h"
/*
* Establish an association between a filter and filtee. Both the filter and
* filtee already exist in the internal hash table, since auditing registers
* objects (la_objopen()) before it registers filters (la_objfilter()).
*/
static int
filter(Crle_desc *crle, const char *filter, const char *str, const char *filtee)
{
Hash_ent *fltrent, *flteent;
Flt_desc *flt;
Aliste idx;
/*
* Locate the filter. Mark the underlying object as the filter to
* reflect that no matter how it is referenced, it's a filter.
*/
if ((fltrent = get_hash(crle->c_strtbl, (Addr)filter, 0,
HASH_FND_ENT)) == NULL)
return (1);
if ((fltrent = get_hash(crle->c_strtbl, (Addr)fltrent->e_obj->o_path, 0,
HASH_FND_ENT)) == NULL)
return (1);
fltrent->e_obj->o_flags |= RTC_OBJ_FILTER;
/*
* Locate the filtee. Mark the referencing object as the filtee, as
* this is the object referenced by the filter.
*/
if ((flteent = get_hash(crle->c_strtbl, (Addr)filtee, 0,
HASH_FND_ENT)) == NULL)
return (1);
flteent->e_flags |= RTC_OBJ_FILTEE;
/*
* Traverse the filter list using the filters real name. If ld.so.1
* inspects the resulting configuration file for filters, it's the
* objects real name that will be used (PATHNAME()).
*/
for (APLIST_TRAVERSE(crle->c_flt, idx, flt)) {
/*
* Determine whether this filter and filtee string pair already
* exist.
*/
if ((strcmp(flt->f_fent->e_obj->o_path,
fltrent->e_obj->o_path) != 0) &&
(strcmp(flt->f_str, str) != 0))
continue;
/*
* Add this filtee additional association.
*/
if (aplist_append(&(flt->f_filtee), flteent,
AL_CNT_CRLE) == NULL)
return (1);
crle->c_fltenum++;
return (0);
}
/*
* This is a new filter descriptor. Add this new filtee association.
*/
if (((flt = malloc(sizeof (Flt_desc))) == NULL) ||
((flt->f_strsz = strlen(str) + 1) == 0) ||
((flt->f_str = malloc(flt->f_strsz)) == NULL)) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
crle->c_name, strerror(err));
return (1);
}
if ((aplist_append(&(crle->c_flt), flt, AL_CNT_CRLE) == NULL) ||
(aplist_append(&(flt->f_filtee), flteent, AL_CNT_CRLE) == NULL))
return (1);
flt->f_fent = fltrent;
(void) memcpy((void *)flt->f_str, (void *)str, flt->f_strsz);
crle->c_strsize += flt->f_strsz;
crle->c_fltrnum += 1;
crle->c_fltenum += 2; /* Account for null filtee desc. */
return (0);
}
/*
* Establish the dependencies of an ELF object and add them to the internal
* configuration information. This information is gathered by using libcrle.so.1
* as an audit library - this is akin to using ldd(1) only simpler.
*/
int
depend(Crle_desc *crle, const char *name, Half flags, GElf_Ehdr *ehdr)
{
const char *exename;
const char *preload;
int fildes[2], pid;
/*
* If we're dealing with a dynamic executable we'll execute it,
* otherwise we'll preload the shared object with one of the lddstub's.
*/
if (ehdr->e_type == ET_EXEC) {
exename = name;
preload = NULL;
} else {
exename = conv_lddstub(M_CLASS);
preload = name;
}
/*
* Set up a pipe through which the audit library will write the
* dependencies.
*/
if (pipe(fildes) == -1) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_PIPE),
crle->c_name, strerror(err));
return (1);
}
/*
* Fork ourselves to run our executable and collect its dependencies.
*/
if ((pid = fork()) == -1) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK),
crle->c_name, strerror(err));
return (1);
}
if (pid) {
/*
* Parent. Read each dependency from the audit library. The read
* side of the pipe is attached to stdio to make obtaining the
* individual dependencies easier.
*/
int error = 0, status;
FILE *fd;
char buffer[PATH_MAX];
(void) close(fildes[1]);
if ((fd = fdopen(fildes[0], MSG_ORIG(MSG_STR_READ))) != NULL) {
char *str;
while (fgets(buffer, PATH_MAX, fd) != NULL) {
/*
* Make sure we recognize the message, remove
* the newline (which allowed fgets() use) and
* register the name;
*/
if (strncmp(MSG_ORIG(MSG_AUD_PRF), buffer,
MSG_AUD_PRF_SIZE))
continue;
str = strrchr(buffer, '\n');
*str = '\0';
str = buffer + MSG_AUD_PRF_SIZE;
if (strncmp(MSG_ORIG(MSG_AUD_DEPEND),
str, MSG_AUD_DEPEND_SIZE) == 0) {
/*
* Process any dependencies.
*/
str += MSG_AUD_DEPEND_SIZE;
if ((error = inspect(crle, str,
(flags & ~RTC_OBJ_GROUP))) != 0)
break;
} else if (strncmp(MSG_ORIG(MSG_AUD_FILTER),
str, MSG_AUD_FILTER_SIZE) == 0) {
char *_flt, *_str;
/*
* Process any filters.
*/
_flt = str += MSG_AUD_FILTER_SIZE;
_str = strchr(str, ':');
*_str++ = '\0'; str = _str++;
str = strrchr(str, ')');
*str++ = '\0'; str++;
if ((error = filter(crle, _flt, _str,
str)) != 0)
break;
}
}
} else
error = errno;
while (wait(&status) != pid)
;
if (status) {
if (WIFSIGNALED(status)) {
(void) fprintf(stderr,
MSG_INTL(MSG_SYS_EXEC), crle->c_name,
exename, (WSIGMASK & status),
((status & WCOREFLG) ?
MSG_INTL(MSG_SYS_CORE) :
MSG_ORIG(MSG_STR_EMPTY)));
}
error = status;
}
(void) fclose(fd);
return (error);
} else {
char efds[MSG_ENV_AUD_FD_SIZE + 10];
char epld[PATH_MAX];
char eldf[PATH_MAX];
(void) close(fildes[0]);
/*
* Child. Set up environment variables to enable and identify
* auditing. Initialize CRLE_FD and LD_FLAGS strings.
*/
(void) snprintf(efds, (MSG_ENV_AUD_FD_SIZE + 10),
MSG_ORIG(MSG_ENV_AUD_FD), fildes[1]);
(void) snprintf(eldf, PATH_MAX, MSG_ORIG(MSG_ENV_LD_FLAGS));
/*
* If asked to dump a group of dependencies make sure any
* lazily-loaded objects get processed - (append loadavail to
* LD_FLAGS=confgen).
*/
if (flags & RTC_OBJ_GROUP)
(void) strcat(eldf, MSG_ORIG(MSG_LDFLG_LOADAVAIL));
/*
* Put LD_PRELOAD= in the environment if necessary.
*/
if (preload) {
(void) snprintf(epld, PATH_MAX,
MSG_ORIG(MSG_ENV_LD_PRELOAD), preload);
}
/*
* Put strings in the environment for exec().
* NOTE, use of automatic variables for construction of the
* environment variables is legitimate here, as they are local
* to the child process and are established solely for exec().
*/
if ((putenv(efds) != 0) || (putenv(crle->c_audit) != 0) ||
(putenv(eldf) != 0) || (preload && (putenv(epld) != 0))) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_PUTENV),
crle->c_name, strerror(err));
return (1);
}
if (execlp(exename, exename, 0) == -1) {
_exit(errno);
/* NOTREACHED */
}
}
return (0);
}
|