blob: e9b9fe35bca5ef7b52bc6160c1b62b12127a35f0 [file] [log] [blame]
Tom Cherry6034ef82018-02-02 16:10:07 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "grp_pwd_file.h"
30
31#include <fcntl.h>
32#include <stdlib.h>
33#include <sys/mman.h>
34#include <sys/stat.h>
35
36#include "private/ErrnoRestorer.h"
37
38// This file mmap's /*/etc/passwd and /*/etc/group in order to return their contents without any
39// allocations. Note that these files and the strings contained within them are explicitly not
40// null-terminated. ':'s are used to deliminate fields and '\n's are used to deliminate lines.
41// There is a check that the file ends with '\n', such that terminating loops at '\n' ensures that
Tom Cherrye267f1a2018-05-10 13:34:54 -070042// memory will be not read beyond the mmap region.
Tom Cherry6034ef82018-02-02 16:10:07 -080043
44namespace {
45
46void CopyFieldToString(char* dest, const char* source, size_t max) {
47 while (*source != ':' && *source != '\n' && max > 1) {
48 *dest++ = *source++;
49 --max;
50 }
51 *dest = '\0';
52}
53
54bool FieldToUid(const char* field, uid_t* uid) {
55 if (field == nullptr) {
56 return false;
57 }
58
59 char* end = nullptr;
60 errno = 0;
61 uid_t result = strtoul(field, &end, 0);
62 if (errno != 0 || field == end || *end != ':') {
63 return false;
64 }
65 *uid = result;
66 return true;
67}
68
69// Returns a pointer to one past the end of line.
70const char* ParseLine(const char* begin, const char* end, const char** fields, size_t num_fields) {
71 size_t fields_written = 0;
72 const char* position = begin;
73 fields[fields_written++] = position;
74
75 while (position < end && fields_written < num_fields) {
76 if (*position == '\n') {
77 return position + 1;
78 }
79 if (*position == ':') {
80 fields[fields_written++] = position + 1;
81 }
82 position++;
83 }
84
85 while (position < end && *position != '\n') {
86 position++;
87 }
88
89 return position + 1;
90}
91
92struct PasswdLine {
93 const char* name() const {
94 return fields[0];
95 }
96 // Password is not supported.
97 const char* uid() const {
98 return fields[2];
99 }
100 const char* gid() const {
101 return fields[3];
102 }
103 // User Info is not supported
104 const char* dir() const {
105 return fields[5];
106 }
107 const char* shell() const {
108 return fields[6];
109 }
110
111 bool ToPasswdState(passwd_state_t* passwd_state) {
112 if (name() == nullptr || dir() == nullptr || shell() == nullptr) {
113 return false;
114 }
115
116 uid_t uid;
117 if (!FieldToUid(this->uid(), &uid)) {
118 return false;
119 }
120
121 gid_t gid;
122 if (!FieldToUid(this->gid(), &gid)) {
123 return false;
124 }
125
126 passwd_state->passwd_.pw_uid = uid;
127 passwd_state->passwd_.pw_gid = gid;
128
129 CopyFieldToString(passwd_state->name_buffer_, name(), sizeof(passwd_state->name_buffer_));
130 passwd_state->passwd_.pw_name = passwd_state->name_buffer_;
131
132 passwd_state->passwd_.pw_passwd = nullptr;
133
134#ifdef __LP64__
135 passwd_state->passwd_.pw_gecos = nullptr;
136#endif
137
138 CopyFieldToString(passwd_state->dir_buffer_, dir(), sizeof(passwd_state->dir_buffer_));
139 passwd_state->passwd_.pw_dir = passwd_state->dir_buffer_;
140
141 CopyFieldToString(passwd_state->sh_buffer_, shell(), sizeof(passwd_state->sh_buffer_));
142 passwd_state->passwd_.pw_shell = passwd_state->sh_buffer_;
143
144 return true;
145 }
146
147 static constexpr size_t kNumFields = 7;
148 const char* fields[kNumFields] = {};
149};
150
151struct GroupLine {
152 const char* name() const {
153 return fields[0];
154 }
155 // Password is not supported.
156 const char* gid() const {
157 return fields[2];
158 }
159 // User list is not supported (returns simply name)
160
161 bool ToGroupState(group_state_t* group_state) {
162 if (name() == nullptr || gid() == nullptr) {
163 return false;
164 }
165
166 gid_t gid;
167 if (!FieldToUid(this->gid(), &gid)) {
168 return false;
169 }
170
171 group_state->group_.gr_gid = gid;
172
173 CopyFieldToString(group_state->group_name_buffer_, name(),
174 sizeof(group_state->group_name_buffer_));
175 group_state->group_.gr_name = group_state->group_name_buffer_;
176
177 group_state->group_.gr_passwd = nullptr;
178
179 group_state->group_.gr_mem = group_state->group_members_;
180 group_state->group_.gr_mem[0] = group_state->group_.gr_name;
181 group_state->group_.gr_mem[1] = nullptr;
182
183 return true;
184 }
185
186 static constexpr size_t kNumFields = 4;
187 const char* fields[kNumFields] = {};
188};
189
190} // namespace
191
192MmapFile::MmapFile(const char* filename) : filename_(filename) {
193 lock_.init(false);
194}
195
Tom Cherry5fe73262018-02-20 15:40:59 -0800196void MmapFile::Unmap() {
Tom Cherry6034ef82018-02-02 16:10:07 -0800197 if (status_ == FileStatus::Initialized) {
198 size_t size = end_ - start_ + 1;
199 munmap(const_cast<char*>(start_), size);
Tom Cherry5fe73262018-02-20 15:40:59 -0800200 status_ = FileStatus::Uninitialized;
201 start_ = nullptr;
202 end_ = nullptr;
Tom Cherry6034ef82018-02-02 16:10:07 -0800203 }
204}
205
206bool MmapFile::GetFile(const char** start, const char** end) {
207 LockGuard guard(lock_);
208 if (status_ == FileStatus::Initialized) {
209 *start = start_;
210 *end = end_;
211 return true;
212 }
213 if (status_ == FileStatus::Error) {
214 return false;
215 }
216
217 if (!DoMmap()) {
218 status_ = FileStatus::Error;
219 return false;
220 }
221
222 status_ = FileStatus::Initialized;
223 *start = start_;
224 *end = end_;
225 return true;
226}
227
228bool MmapFile::DoMmap() {
229 int fd = open(filename_, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
230
231 struct stat fd_stat;
232 if (fstat(fd, &fd_stat) == -1) {
233 close(fd);
234 return false;
235 }
236
237 auto mmap_size = fd_stat.st_size;
238
Tom Cherrycb4d4212018-02-20 15:50:04 -0800239 void* map_result = mmap(nullptr, mmap_size, PROT_READ, MAP_SHARED, fd, 0);
Tom Cherry6034ef82018-02-02 16:10:07 -0800240 close(fd);
241
242 if (map_result == MAP_FAILED) {
243 return false;
244 }
245
246 start_ = static_cast<const char*>(map_result);
247 end_ = start_ + mmap_size - 1;
248
Tom Cherrycb4d4212018-02-20 15:50:04 -0800249 if (*end_ != '\n') {
250 munmap(map_result, mmap_size);
251 return false;
252 }
253
254 return true;
Tom Cherry6034ef82018-02-02 16:10:07 -0800255}
256
257template <typename Line, typename Predicate>
258bool MmapFile::Find(Line* line, Predicate predicate) {
259 const char* start;
260 const char* end;
261 if (!GetFile(&start, &end)) {
262 return false;
263 }
264
265 const char* line_beginning = start;
266
267 while (line_beginning < end) {
268 line_beginning = ParseLine(line_beginning, end, line->fields, line->kNumFields);
269 if (predicate(line)) return true;
270 }
271
272 return false;
273}
274
275template <typename Line>
276bool MmapFile::FindById(uid_t uid, Line* line) {
277 return Find(line, [uid](const auto& line) {
278 uid_t line_id;
279 if (!FieldToUid(line->fields[2], &line_id)) {
280 return false;
281 }
282
283 return line_id == uid;
284 });
285}
286
287template <typename Line>
288bool MmapFile::FindByName(const char* name, Line* line) {
289 return Find(line, [name](const auto& line) {
290 const char* line_name = line->fields[0];
291 if (line_name == nullptr) {
292 return false;
293 }
294
295 const char* match_name = name;
296 while (*line_name != '\n' && *line_name != ':' && *match_name != '\0') {
297 if (*line_name++ != *match_name++) {
298 return false;
299 }
300 }
301
302 return *line_name == ':' && *match_name == '\0';
303 });
304}
305
306PasswdFile::PasswdFile(const char* filename) : mmap_file_(filename) {
307}
308
309bool PasswdFile::FindById(uid_t id, passwd_state_t* passwd_state) {
310 ErrnoRestorer errno_restorer;
311 PasswdLine passwd_line;
312 return mmap_file_.FindById(id, &passwd_line) && passwd_line.ToPasswdState(passwd_state);
313}
314
315bool PasswdFile::FindByName(const char* name, passwd_state_t* passwd_state) {
316 ErrnoRestorer errno_restorer;
317 PasswdLine passwd_line;
318 return mmap_file_.FindByName(name, &passwd_line) && passwd_line.ToPasswdState(passwd_state);
319}
320
321GroupFile::GroupFile(const char* filename) : mmap_file_(filename) {
322}
323
324bool GroupFile::FindById(gid_t id, group_state_t* group_state) {
325 ErrnoRestorer errno_restorer;
326 GroupLine group_line;
327 return mmap_file_.FindById(id, &group_line) && group_line.ToGroupState(group_state);
328}
329
330bool GroupFile::FindByName(const char* name, group_state_t* group_state) {
331 ErrnoRestorer errno_restorer;
332 GroupLine group_line;
333 return mmap_file_.FindByName(name, &group_line) && group_line.ToGroupState(group_state);
334}