|
41 | 41 | */
|
42 | 42 |
|
43 | 43 | #include <sys/types.h>
|
| 44 | +#include <sys/capsicum.h> |
44 | 45 | #include <sys/mman.h>
|
45 | 46 | #include <sys/stat.h>
|
46 | 47 |
|
| 48 | +#include <capsicum_helpers.h> |
47 | 49 | #include <err.h>
|
48 | 50 | #include <errno.h>
|
49 | 51 | #include <fcntl.h>
|
|
89 | 91 | main(int argc, char *argv[])
|
90 | 92 | {
|
91 | 93 | struct stat sb;
|
92 |
| - int ch, fd, match; |
| 94 | + int ch, match; |
| 95 | + size_t nfiles; |
93 | 96 | wchar_t termchar;
|
| 97 | + cap_rights_t rights; |
| 98 | + int *fds; |
94 | 99 | unsigned char *back, *front;
|
95 | 100 | unsigned const char *file;
|
96 | 101 | wchar_t *key;
|
@@ -132,22 +137,38 @@ main(int argc, char *argv[])
|
132 | 137 |
|
133 | 138 | match = 1;
|
134 | 139 |
|
135 |
| - do { |
136 |
| - if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb)) |
| 140 | + cap_rights_init(&rights, CAP_MMAP_R, CAP_READ, CAP_FSTAT); |
| 141 | + nfiles = argc > 1 ? argc - 1 : argc; |
| 142 | + if ((fds = malloc(nfiles * sizeof(int))) == NULL) |
| 143 | + err(2, NULL); |
| 144 | + for (size_t idx = 0; idx < nfiles; file = argv[idx++]) { |
| 145 | + if ((fds[idx] = open(file, O_RDONLY, 0)) < 0) |
| 146 | + continue; |
| 147 | + if (caph_rights_limit(fds[idx], &rights) != 0) |
| 148 | + err(2, "unable to limit rights for %s", file); |
| 149 | + } |
| 150 | + |
| 151 | + caph_cache_catpages(); |
| 152 | + if (caph_enter() != 0) |
| 153 | + err(EXIT_FAILURE, "failed to enter capability mode"); |
| 154 | + |
| 155 | + for (size_t idx = 0; idx < nfiles; file = argv[idx++]) { |
| 156 | + if (fstat(fds[idx], &sb)) |
137 | 157 | err(2, "%s", file);
|
138 | 158 | if ((uintmax_t)sb.st_size > (uintmax_t)SIZE_T_MAX)
|
139 | 159 | errx(2, "%s: %s", file, strerror(EFBIG));
|
140 | 160 | if (sb.st_size == 0) {
|
141 |
| - close(fd); |
| 161 | + close(fds[idx]); |
142 | 162 | continue;
|
143 | 163 | }
|
144 |
| - if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) |
| 164 | + if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fds[idx], (off_t)0)) == MAP_FAILED) |
145 | 165 | err(2, "%s", file);
|
146 | 166 | back = front + sb.st_size;
|
147 | 167 | match *= (look(key, front, back));
|
148 |
| - close(fd); |
149 |
| - } while (argc-- > 2 && (file = *argv++)); |
| 168 | + close(fds[idx]); |
| 169 | + } |
150 | 170 |
|
| 171 | + free(fds); |
151 | 172 | exit(match);
|
152 | 173 | }
|
153 | 174 |
|
|
0 commit comments