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
|
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "bloom.h"
#define PAM_BLOOM_FILTER_DIR "/etc/security/pam_bloom/filters.d"
#define MAX_PASSWORD_LENGTH 4096
static bool
has_bloom_extension(const char *name)
{
size_t len = strlen(name);
return len > 6 && strcmp(name + len - 6, ".bloom") == 0;
}
static int
validate_filter_fd(int fd, const char *filename)
{
struct stat st;
if (fstat(fd, &st) < 0) {
perror(filename);
return -1;
}
if (!S_ISREG(st.st_mode)) {
fprintf(stderr, "%s: not a regular file\n", filename);
return -1;
}
if (st.st_uid != 0) {
fprintf(stderr, "%s: not owned by root\n", filename);
return -1;
}
if (st.st_mode & (S_IWGRP | S_IWOTH)) {
fprintf(stderr, "%s: writable by group or others\n", filename);
return -1;
}
return 0;
}
static int
validate_filter_directory(int dirfd, const char *directory)
{
struct stat st;
if (fstat(dirfd, &st) < 0) {
perror(directory);
return -1;
}
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s: not a directory\n", directory);
return -1;
}
if (st.st_uid != 0) {
fprintf(stderr, "%s: not owned by root\n", directory);
return -1;
}
if (st.st_mode & (S_IWGRP | S_IWOTH)) {
fprintf(stderr, "%s: writable by group or others\n", directory);
return -1;
}
return 0;
}
/*
* Returns:
* 1 password matched a filter
* 0 password did not match
* -1 error
*/
static int
check_filters(const char *directory, const char *password, size_t password_len)
{
int dirfd = open(directory, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
if (dirfd < 0) {
perror(directory);
return -1;
}
if (validate_filter_directory(dirfd, directory) < 0) {
close(dirfd);
return -1;
}
DIR *dir = fdopendir(dirfd);
if (dir == NULL) {
perror(directory);
close(dirfd);
return -1;
}
bool saw_filter = false;
int result = 0;
for (;;) {
errno = 0;
struct dirent *entry = readdir(dir);
if (entry == NULL) {
if (errno != 0) {
perror("readdir");
result = -1;
}
break;
}
if (!has_bloom_extension(entry->d_name)) {
continue;
}
saw_filter = true;
int fd = openat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
if (fd < 0) {
perror(entry->d_name);
result = -1;
break;
}
if (validate_filter_fd(fd, entry->d_name) < 0) {
close(fd);
result = -1;
break;
}
bloomfilter_mapped bf = {0};
bloom_error_t err = bloom_map_fd(&bf, fd);
if (err != BF_SUCCESS) {
fprintf(stderr, "%s: %s\n", entry->d_name, bloom_strerror(err));
close(fd);
result = -1;
break;
}
bool found = bloom_mapped_lookup(&bf, password, password_len);
if (found) {
fprintf(stderr, "password matches filter: %s\n",
(const char *)bf.header->name);
}
bloom_unmap(&bf);
close(fd);
if (found) {
result = 1;
break;
}
}
if (result == 0 && !saw_filter) {
fprintf(stderr, "warning: no Bloom filters found in %s\n", directory);
}
if (closedir(dir) != 0 && result == 0) {
perror("closedir");
result = -1;
}
return result;
}
int
main(void)
{
char password[MAX_PASSWORD_LENGTH + 2];
if (fgets(password, sizeof(password), stdin) == NULL) {
if (ferror(stdin)) {
perror("stdin");
} else {
fprintf(stderr, "no password provided\n");
}
return 2;
}
size_t len = strlen(password);
if (len > 0 && password[len - 1] == '\n') {
password[--len] = '\0';
}
if (len > 0 && password[len - 1] == '\r') {
password[--len] = '\0';
}
/*
* If fgets() filled the buffer without seeing a newline, reject the
* input rather than silently checking a truncated password.
*/
if (len == MAX_PASSWORD_LENGTH + 1) {
fprintf(stderr, "password exceeds maximum length\n");
return 2;
}
int result = check_filters(PAM_BLOOM_FILTER_DIR, password, len);
/* zero the plaintext password in memory. */
explicit_bzero(password, sizeof(password));
if (result < 0) return 2;
if (result > 0) return 1;
return 0;
}
|