guard against hard links to non-ordinary-files when reading tcb shadow
[musl] / src / passwd / getspnam_r.c
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include <sys/stat.h>
4 #include "pwf.h"
5
6 /* This implementation support Openwall-style TCB passwords in place of
7  * traditional shadow, if the appropriate directories and files exist.
8  * Thus, it is careful to avoid following symlinks or blocking on fifos
9  * which a malicious user might create in place of his or her TCB shadow
10  * file. It also avoids any allocation to prevent memory-exhaustion
11  * attacks via huge TCB shadow files. */
12
13 int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct spwd **res)
14 {
15         char path[20+NAME_MAX];
16         FILE *f = 0;
17         int rv = 0;
18         int fd;
19         size_t k, l = strlen(name);
20         char *s;
21         int skip = 0;
22
23         *res = 0;
24
25         /* Disallow potentially-malicious user names */
26         if (*name=='.' || strchr(name, '/') || !l)
27                 return EINVAL;
28
29         /* Buffer size must at least be able to hold name, plus some.. */
30         if (size < l+100) return ERANGE;
31
32         /* Protect against truncation */
33         if (snprintf(path, sizeof path, "/etc/tcb/%s/shadow", name) >= sizeof path)
34                 return EINVAL;
35
36         fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK);
37         if (fd >= 0) {
38                 struct stat st = { 0 };
39                 errno = EINVAL;
40                 if (fstat(fd, &st) || !S_ISREG(st.st_mode) || !(f = fdopen(fd, "rb"))) {
41                         close(fd);
42                         return errno;
43                 }
44         } else {
45                 f = fopen("/etc/shadow", "rb");
46                 if (!f) return errno;
47         }
48
49         while (fgets(buf, size, f) && (k=strlen(buf))>0) {
50                 if (skip || strncmp(name, buf, l)) {
51                         skip = buf[k-1] != '\n';
52                         continue;
53                 }
54                 if (buf[k-1] != '\n') {
55                         rv = ERANGE;
56                         break;
57                 }
58                 buf[k-1] = 0;
59
60                 s = buf;
61                 sp->sp_namp = s;
62                 if (!(s = strchr(s, ':'))) continue;
63
64                 *s++ = 0; sp->sp_pwdp = s;
65                 if (!(s = strchr(s, ':'))) continue;
66
67                 *s++ = 0; sp->sp_lstchg = atol(s);
68                 if (!(s = strchr(s, ':'))) continue;
69
70                 *s++ = 0; sp->sp_min = atol(s);
71                 if (!(s = strchr(s, ':'))) continue;
72
73                 *s++ = 0; sp->sp_max = atol(s);
74                 if (!(s = strchr(s, ':'))) continue;
75
76                 *s++ = 0; sp->sp_warn = atol(s);
77                 if (!(s = strchr(s, ':'))) continue;
78
79                 *s++ = 0; sp->sp_inact = atol(s);
80                 if (!(s = strchr(s, ':'))) continue;
81
82                 *s++ = 0; sp->sp_expire = atol(s);
83                 if (!(s = strchr(s, ':'))) continue;
84
85                 *s++ = 0; sp->sp_flag = atol(s);
86                 *res = sp;
87                 break;
88         }
89         fclose(f);
90         return rv;
91 }