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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "lint.h"
#include "mtlib.h"
#include <string.h>
#include <syslog.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <thread.h>
#include <synch.h>
#include <ctype.h>
#include <errno.h>
#include "libc.h"
#include "nlspath_checks.h"
extern const char **_environ;
/*
* We want to prevent the use of NLSPATH by setugid applications but
* not completely. CDE depends on this very much.
* Yes, this is ugly.
*/
struct trusted_systemdirs {
const char *dir;
size_t dirlen;
};
#define _USRLIB "/usr/lib/"
#define _USRDT "/usr/dt/"
#define _USROW "/usr/openwin/"
static const struct trusted_systemdirs prefix[] = {
{ _USRLIB, sizeof (_USRLIB) - 1 },
{ _USRDT, sizeof (_USRDT) - 1 },
{ _USROW, sizeof (_USROW) - 1 },
{ NULL, 0 }
};
static int8_t nlspath_safe;
/*
* Routine to check the safety of a messages file.
* When the program specifies a pathname and doesn't
* use NLSPATH, it should specify the "safe" flag as 1.
* Most checks will be disabled then.
* fstat64 is done here and the stat structure is returned
* to prevent duplication of system calls.
*
* The trust return value contains an indication of
* trustworthiness (i.e., does check_format need to be called or
* not)
*/
int
nls_safe_open(const char *path, struct stat64 *statbuf, int *trust, int safe)
{
int fd;
int trust_path;
int systemdir = 0;
int abs_path = 0;
int trust_owner = 0;
int trust_group = 0;
const struct trusted_systemdirs *p;
/*
* If SAFE_F has been specified or NLSPATH is safe (or not set),
* set trust_path and trust the file as an initial value.
*/
trust_path = *trust = safe || nlspath_safe;
fd = open(path, O_RDONLY);
if (fd < 0)
return (-1);
if (fstat64(fd, statbuf) == -1) {
(void) close(fd);
return (-1);
}
/*
* Trust only files owned by root or bin (uid 2), except
* when specified as full path or when NLSPATH is known to
* be safe.
* Don't trust files writable by other or writable
* by non-bin, non-root system group.
* Don't trust these files even if the path is correct.
* Since we don't support changing uids/gids on our files,
* we hardcode them here for now.
*/
/*
* if the path is absolute and does not contain "/../",
* set abs_path.
*/
if (*path == '/' && strstr(path, "/../") == NULL) {
abs_path = 1;
/*
* if the path belongs to the trusted system directory,
* set systemdir.
*/
for (p = prefix; p->dir; p++) {
if (strncmp(p->dir, path, p->dirlen) == 0) {
systemdir = 1;
break;
}
}
}
/*
* If the owner is root or bin, set trust_owner.
*/
if (statbuf->st_uid == 0 || statbuf->st_uid == 2) {
trust_owner = 1;
}
/*
* If the file is neither other-writable nor group-writable by
* non-bin and non-root system group, set trust_group.
*/
if ((statbuf->st_mode & (S_IWOTH)) == 0 &&
((statbuf->st_mode & (S_IWGRP)) == 0 ||
(statbuf->st_gid < 4 && statbuf->st_gid != 1))) {
trust_group = 1;
}
/*
* Even if UNSAFE_F has been specified and unsafe-NLSPATH
* has been set, trust the file as long as it belongs to
* the trusted system directory.
*/
if (!*trust && systemdir) {
*trust = 1;
}
/*
* If:
* file is not a full pathname,
* or
* neither trust_owner nor trust_path is set,
* or
* trust_group is not set,
* untrust it.
*/
if (*trust &&
(!abs_path || (!trust_owner && !trust_path) || !trust_group)) {
*trust = 0;
}
/*
* If set[ug]id process, open for the untrusted file should fail.
* Otherwise, the message extracted from the untrusted file
* will have to be checked by check_format().
*/
if (issetugid()) {
if (!*trust) {
/*
* Open should fail
*/
(void) close(fd);
return (-1);
}
/*
* if the path does not belong to the trusted system directory
* or if the owner is neither root nor bin, untrust it.
*/
if (!systemdir || !trust_owner) {
*trust = 0;
}
}
return (fd);
}
/*
* Extract a format into a normalized format string.
* Returns the number of arguments converted, -1 on error.
* The string norm should contain 2N bytes; an upperbound is the
* length of the format string.
* The canonical format consists of two chars: one is the conversion
* character (s, c, d, x, etc), the second one is the option flag.
* L, ll, l, w as defined below.
* A special conversion character, '*', indicates that the argument
* is used as a precision specifier.
*/
#define OPT_L 0x01
#define OPT_l 0x02
#define OPT_ll 0x04
#define OPT_w 0x08
#define OPT_h 0x10
#define OPT_hh 0x20
#define OPT_j 0x40
/* Number of bytes per canonical format entry */
#define FORMAT_SIZE 2
/*
* Check and store the argument; allow each argument to be used only as
* one type even though printf allows multiple uses. The specification only
* allows one use, but we don't want to break existing functional code,
* even if it's buggy.
*/
#define STORE(buf, size, arg, val) if (arg * FORMAT_SIZE + 1 >= size ||\
(strict ? \
(buf[arg*FORMAT_SIZE] != '\0' && \
buf[arg*FORMAT_SIZE] != val) \
: \
(buf[arg*FORMAT_SIZE] == 'n'))) \
return (-1); \
else {\
if (arg >= maxarg) \
maxarg = arg + 1; \
narg++; \
buf[arg*FORMAT_SIZE] = val; \
}
/*
* This function extracts sprintf format into a canonical
* sprintf form. It's not as easy as just removing everything
* that isn't a format specifier, because of "%n$" specifiers.
* Ideally, this should be compatible with printf and not
* fail on bad formats.
* However, that makes writing a proper check_format that
* doesn't cause crashes a lot harder.
*/
static int
extract_format(const char *fmt, char *norm, size_t sz, int strict)
{
int narg = 0;
int t, arg, argp;
int dotseen;
char flag;
char conv;
int lastarg = -1;
int prevarg;
int maxarg = 0; /* Highest index seen + 1 */
int lflag;
(void) memset(norm, '\0', sz);
#ifdef DEBUG
printf("Format \"%s\" canonical form: ", fmt);
#endif
for (; *fmt; fmt++) {
if (*fmt == '%') {
if (*++fmt == '%')
continue;
if (*fmt == '\0')
break;
prevarg = lastarg;
arg = ++lastarg;
t = 0;
while (*fmt && isdigit(*fmt))
t = t * 10 + *fmt++ - '0';
if (*fmt == '$') {
lastarg = arg = t - 1;
fmt++;
}
if (*fmt == '\0')
goto end;
dotseen = 0;
flag = 0;
lflag = 0;
again:
/* Skip flags */
while (*fmt) {
switch (*fmt) {
case '\'':
case '+':
case '-':
case ' ':
case '#':
case '0':
fmt++;
continue;
}
break;
}
while (*fmt && isdigit(*fmt))
fmt++;
if (*fmt == '*') {
if (isdigit(fmt[1])) {
fmt++;
t = 0;
while (*fmt && isdigit(*fmt))
t = t * 10 + *fmt++ - '0';
if (*fmt == '$') {
argp = t - 1;
STORE(norm, sz, argp, '*');
}
/*
* If digits follow a '*', it is
* not loaded as an argument, the
* digits are used instead.
*/
} else {
/*
* Weird as it may seem, if we
* use an numbered argument, we
* get the next one if we have
* an unnumbered '*'
*/
if (fmt[1] == '$')
fmt++;
else {
argp = arg;
prevarg = arg;
lastarg = ++arg;
STORE(norm, sz, argp, '*');
}
}
fmt++;
}
/* Fail on two or more dots if we do strict checking */
if (*fmt == '.' || *fmt == '*') {
if (dotseen && strict)
return (-1);
dotseen = 1;
fmt++;
goto again;
}
if (*fmt == '\0')
goto end;
while (*fmt) {
switch (*fmt) {
case 'l':
if (!(flag & OPT_ll)) {
if (lflag) {
flag &= ~OPT_l;
flag |= OPT_ll;
} else {
flag |= OPT_l;
}
}
lflag++;
break;
case 'L':
flag |= OPT_L;
break;
case 'w':
flag |= OPT_w;
break;
case 'h':
if (flag & (OPT_h|OPT_hh))
flag |= OPT_hh;
else
flag |= OPT_h;
break;
case 'j':
flag |= OPT_j;
break;
case 'z':
case 't':
if (!(flag & OPT_ll)) {
flag |= OPT_l;
}
break;
case '\'':
case '+':
case '-':
case ' ':
case '#':
case '.':
case '*':
goto again;
default:
if (isdigit(*fmt))
goto again;
else
goto done;
}
fmt++;
}
done:
if (*fmt == '\0')
goto end;
switch (*fmt) {
case 'C':
flag |= OPT_l;
/* FALLTHROUGH */
case 'd':
case 'i':
case 'o':
case 'u':
case 'c':
case 'x':
case 'X':
conv = 'I';
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'a':
case 'A':
case 'g':
case 'G':
conv = 'D';
break;
case 'S':
flag |= OPT_l;
/* FALLTHROUGH */
case 's':
conv = 's';
break;
case 'p':
case 'n':
conv = *fmt;
break;
default:
lastarg = prevarg;
continue;
}
STORE(norm, sz, arg, conv);
norm[arg*FORMAT_SIZE + 1] = flag;
}
}
#ifdef DEBUG
for (t = 0; t < maxarg * FORMAT_SIZE; t += FORMAT_SIZE) {
printf("%c(%d)", norm[t], norm[t+1]);
}
putchar('\n');
#endif
end:
if (strict)
for (arg = 0; arg < maxarg; arg++)
if (norm[arg*FORMAT_SIZE] == '\0')
return (-1);
return (maxarg);
}
char *
check_format(const char *org, const char *new, int strict)
{
char *ofmt, *nfmt, *torg;
size_t osz, nsz;
int olen, nlen;
if (!org) {
/*
* Default message is NULL.
* dtmail uses NULL for default message.
*/
torg = "(NULL)";
} else {
torg = (char *)org;
}
/* Short cut */
if (org == new || strcmp(torg, new) == 0 ||
strchr(new, '%') == NULL)
return ((char *)new);
osz = strlen(torg) * FORMAT_SIZE;
ofmt = malloc(osz);
if (ofmt == NULL)
return ((char *)org);
olen = extract_format(torg, ofmt, osz, 0);
if (olen == -1)
syslog(LOG_AUTH|LOG_INFO,
"invalid format in gettext argument: \"%s\"", torg);
nsz = strlen(new) * FORMAT_SIZE;
nfmt = malloc(nsz);
if (nfmt == NULL) {
free(ofmt);
return ((char *)org);
}
nlen = extract_format(new, nfmt, nsz, strict);
if (nlen == -1) {
free(ofmt);
free(nfmt);
syslog(LOG_AUTH|LOG_NOTICE,
"invalid format in message file \"%.100s\" -> \"%s\"",
torg, new);
errno = EBADMSG;
return ((char *)org);
}
if (strict && (olen != nlen || olen == -1)) {
free(ofmt);
free(nfmt);
syslog(LOG_AUTH|LOG_NOTICE,
"incompatible format in message file: \"%.100s\" != \"%s\"",
torg, new);
errno = EBADMSG;
return ((char *)org);
}
if (strict && memcmp(ofmt, nfmt, nlen * FORMAT_SIZE) == 0) {
free(ofmt);
free(nfmt);
return ((char *)new);
} else {
if (!strict) {
char *n;
nlen *= FORMAT_SIZE;
for (n = nfmt;
(n = memchr(n, 'n', nfmt + nlen - n)) != NULL;
n++) {
int off = (n - nfmt);
if (off >= olen * FORMAT_SIZE ||
ofmt[off] != 'n' ||
ofmt[off+1] != nfmt[off+1]) {
free(ofmt);
free(nfmt);
syslog(LOG_AUTH|LOG_NOTICE,
"dangerous format in message file: "
"\"%.100s\" -> \"%s\"", torg, new);
errno = EBADMSG;
return ((char *)org);
}
}
free(ofmt);
free(nfmt);
return ((char *)new);
}
free(ofmt);
free(nfmt);
syslog(LOG_AUTH|LOG_NOTICE,
"incompatible format in message file \"%.100s\" != \"%s\"",
torg, new);
errno = EBADMSG;
return ((char *)org);
}
}
/*
* s1 is either name, or name=value
* s2 is name=value
* if names match, return value of s2, else NULL
* used for environment searching: see getenv
*/
const char *
nvmatch(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == '=')
return (s2);
if (*s1 == '\0' && *(s2-1) == '=')
return (s2);
return (NULL);
}
/*
* Handle NLSPATH environment variables in the environment.
* This routine is hooked into getenv/putenv at first call.
*
* The intention is to ignore NLSPATH in set-uid applications,
* and determine whether the NLSPATH in an application was set
* by the applications or derived from the user's environment.
*/
void
clean_env(void)
{
const char **p;
if (_environ == NULL) {
/* can happen when processing a SunOS 4.x AOUT file */
nlspath_safe = 1;
return;
}
/* Find the first NLSPATH occurrence */
for (p = _environ; *p; p++)
if (**p == 'N' && nvmatch("NLSPATH", *p) != NULL)
break;
if (!*p) /* None found, we're safe */
nlspath_safe = 1;
else if (issetugid()) { /* Found and set-uid, clean */
int off = 1;
for (p++; (p[-off] = p[0]) != NULL; p++)
if (**p == 'N' && nvmatch("NLSPATH", *p) != NULL)
off++;
nlspath_safe = 1;
}
}
|