diff options
author | Tom Cherry <tomcherry@google.com> | 2018-02-02 16:10:07 -0800 |
---|---|---|
committer | Tom Cherry <tomcherry@google.com> | 2018-02-08 12:38:57 -0800 |
commit | 6034ef85d86675d063d6b1920e85e4c471b95904 (patch) | |
tree | 717a7dfe7c2778a81919f04cccd1cbaf43f9297d /libc | |
parent | 4cd3550d50cb14efe3d86ab9732ce75def5b85bd (diff) | |
download | android_bionic-6034ef85d86675d063d6b1920e85e4c471b95904.tar.gz android_bionic-6034ef85d86675d063d6b1920e85e4c471b95904.tar.bz2 android_bionic-6034ef85d86675d063d6b1920e85e4c471b95904.zip |
Add ability to read /etc/passwd and /etc/group
Add the capability to read /etc/passwd and /etc/group for getpw* and
getgr* functions.
Bug: 27999086
Test: pwd, grp, grp_pwd_file unit tests
Test: Read in custom users/groups from /etc/{passwd,group}
Change-Id: Idc1f054af8a7ca34743a90493495f0ccc775a0d8
Diffstat (limited to 'libc')
-rw-r--r-- | libc/Android.bp | 1 | ||||
-rw-r--r-- | libc/bionic/grp_pwd.cpp | 30 | ||||
-rw-r--r-- | libc/bionic/grp_pwd_file.cpp | 326 | ||||
-rw-r--r-- | libc/bionic/grp_pwd_file.h | 97 | ||||
-rw-r--r-- | libc/private/bionic_lock.h | 15 |
5 files changed, 467 insertions, 2 deletions
diff --git a/libc/Android.bp b/libc/Android.bp index 2ea851466..89af3cd48 100644 --- a/libc/Android.bp +++ b/libc/Android.bp @@ -1276,6 +1276,7 @@ cc_library_static { "bionic/getpriority.cpp", "bionic/gettid.cpp", "bionic/grp_pwd.cpp", + "bionic/grp_pwd_file.cpp", "bionic/iconv.cpp", "bionic/icu_wrappers.cpp", "bionic/ifaddrs.cpp", diff --git a/libc/bionic/grp_pwd.cpp b/libc/bionic/grp_pwd.cpp index 43a503208..952058fef 100644 --- a/libc/bionic/grp_pwd.cpp +++ b/libc/bionic/grp_pwd.cpp @@ -44,6 +44,10 @@ // Generated android_ids array #include "generated_android_ids.h" +#include "grp_pwd_file.h" + +static PasswdFile vendor_passwd("/vendor/etc/passwd"); +static GroupFile vendor_group("/vendor/etc/group"); // POSIX seems to envisage an implementation where the <pwd.h> functions are // implemented by brute-force searching with getpwent(3), and the <grp.h> @@ -422,7 +426,11 @@ static id_t oem_id_from_name(const char* name) { static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) { if (!is_oem_id(uid)) { - return NULL; + return nullptr; + } + + if (vendor_passwd.FindById(uid, state)) { + return &state->passwd_; } snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid); @@ -440,7 +448,11 @@ static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) { static group* oem_id_to_group(gid_t gid, group_state_t* state) { if (!is_oem_id(gid)) { - return NULL; + return nullptr; + } + + if (vendor_group.FindById(gid, state)) { + return &state->group_; } snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), @@ -530,6 +542,13 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. if (pw != NULL) { return pw; } + + if (vendor_passwd.FindByName(login, state)) { + if (is_oem_id(state->passwd_.pw_uid)) { + return &state->passwd_; + } + } + // Handle OEM range. pw = oem_id_to_passwd(oem_id_from_name(login), state); if (pw != NULL) { @@ -640,6 +659,13 @@ static group* getgrnam_internal(const char* name, group_state_t* state) { if (grp != NULL) { return grp; } + + if (vendor_group.FindByName(name, state)) { + if (is_oem_id(state->group_.gr_gid)) { + return &state->group_; + } + } + // Handle OEM range. grp = oem_id_to_group(oem_id_from_name(name), state); if (grp != NULL) { diff --git a/libc/bionic/grp_pwd_file.cpp b/libc/bionic/grp_pwd_file.cpp new file mode 100644 index 000000000..911daeadb --- /dev/null +++ b/libc/bionic/grp_pwd_file.cpp @@ -0,0 +1,326 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "grp_pwd_file.h" + +#include <fcntl.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <sys/stat.h> + +#include "private/ErrnoRestorer.h" + +// This file mmap's /*/etc/passwd and /*/etc/group in order to return their contents without any +// allocations. Note that these files and the strings contained within them are explicitly not +// null-terminated. ':'s are used to deliminate fields and '\n's are used to deliminate lines. +// There is a check that the file ends with '\n', such that terminating loops at '\n' ensures that +// memory will be not ready before the mmap region. + +namespace { + +void CopyFieldToString(char* dest, const char* source, size_t max) { + while (*source != ':' && *source != '\n' && max > 1) { + *dest++ = *source++; + --max; + } + *dest = '\0'; +} + +bool FieldToUid(const char* field, uid_t* uid) { + if (field == nullptr) { + return false; + } + + char* end = nullptr; + errno = 0; + uid_t result = strtoul(field, &end, 0); + if (errno != 0 || field == end || *end != ':') { + return false; + } + *uid = result; + return true; +} + +// Returns a pointer to one past the end of line. +const char* ParseLine(const char* begin, const char* end, const char** fields, size_t num_fields) { + size_t fields_written = 0; + const char* position = begin; + fields[fields_written++] = position; + + while (position < end && fields_written < num_fields) { + if (*position == '\n') { + return position + 1; + } + if (*position == ':') { + fields[fields_written++] = position + 1; + } + position++; + } + + while (position < end && *position != '\n') { + position++; + } + + return position + 1; +} + +struct PasswdLine { + const char* name() const { + return fields[0]; + } + // Password is not supported. + const char* uid() const { + return fields[2]; + } + const char* gid() const { + return fields[3]; + } + // User Info is not supported + const char* dir() const { + return fields[5]; + } + const char* shell() const { + return fields[6]; + } + + bool ToPasswdState(passwd_state_t* passwd_state) { + if (name() == nullptr || dir() == nullptr || shell() == nullptr) { + return false; + } + + uid_t uid; + if (!FieldToUid(this->uid(), &uid)) { + return false; + } + + gid_t gid; + if (!FieldToUid(this->gid(), &gid)) { + return false; + } + + passwd_state->passwd_.pw_uid = uid; + passwd_state->passwd_.pw_gid = gid; + + CopyFieldToString(passwd_state->name_buffer_, name(), sizeof(passwd_state->name_buffer_)); + passwd_state->passwd_.pw_name = passwd_state->name_buffer_; + + passwd_state->passwd_.pw_passwd = nullptr; + +#ifdef __LP64__ + passwd_state->passwd_.pw_gecos = nullptr; +#endif + + CopyFieldToString(passwd_state->dir_buffer_, dir(), sizeof(passwd_state->dir_buffer_)); + passwd_state->passwd_.pw_dir = passwd_state->dir_buffer_; + + CopyFieldToString(passwd_state->sh_buffer_, shell(), sizeof(passwd_state->sh_buffer_)); + passwd_state->passwd_.pw_shell = passwd_state->sh_buffer_; + + return true; + } + + static constexpr size_t kNumFields = 7; + const char* fields[kNumFields] = {}; +}; + +struct GroupLine { + const char* name() const { + return fields[0]; + } + // Password is not supported. + const char* gid() const { + return fields[2]; + } + // User list is not supported (returns simply name) + + bool ToGroupState(group_state_t* group_state) { + if (name() == nullptr || gid() == nullptr) { + return false; + } + + gid_t gid; + if (!FieldToUid(this->gid(), &gid)) { + return false; + } + + group_state->group_.gr_gid = gid; + + CopyFieldToString(group_state->group_name_buffer_, name(), + sizeof(group_state->group_name_buffer_)); + group_state->group_.gr_name = group_state->group_name_buffer_; + + group_state->group_.gr_passwd = nullptr; + + group_state->group_.gr_mem = group_state->group_members_; + group_state->group_.gr_mem[0] = group_state->group_.gr_name; + group_state->group_.gr_mem[1] = nullptr; + + return true; + } + + static constexpr size_t kNumFields = 4; + const char* fields[kNumFields] = {}; +}; + +} // namespace + +MmapFile::MmapFile(const char* filename) : filename_(filename) { + lock_.init(false); +} + +MmapFile::~MmapFile() { + if (status_ == FileStatus::Initialized) { + size_t size = end_ - start_ + 1; + munmap(const_cast<char*>(start_), size); + } +} + +bool MmapFile::GetFile(const char** start, const char** end) { + LockGuard guard(lock_); + if (status_ == FileStatus::Initialized) { + *start = start_; + *end = end_; + return true; + } + if (status_ == FileStatus::Error) { + return false; + } + + if (!DoMmap()) { + status_ = FileStatus::Error; + return false; + } + + status_ = FileStatus::Initialized; + *start = start_; + *end = end_; + return true; +} + +bool MmapFile::DoMmap() { + int fd = open(filename_, O_CLOEXEC | O_NOFOLLOW | O_RDONLY); + + struct stat fd_stat; + if (fstat(fd, &fd_stat) == -1) { + close(fd); + return false; + } + + auto mmap_size = fd_stat.st_size; + + const void* map_result = mmap(nullptr, mmap_size, PROT_READ, MAP_SHARED, fd, 0); + close(fd); + + if (map_result == MAP_FAILED) { + return false; + } + + start_ = static_cast<const char*>(map_result); + end_ = start_ + mmap_size - 1; + + return *end_ == '\n'; +} + +template <typename Line, typename Predicate> +bool MmapFile::Find(Line* line, Predicate predicate) { + const char* start; + const char* end; + if (!GetFile(&start, &end)) { + return false; + } + + const char* line_beginning = start; + + while (line_beginning < end) { + line_beginning = ParseLine(line_beginning, end, line->fields, line->kNumFields); + if (predicate(line)) return true; + } + + return false; +} + +template <typename Line> +bool MmapFile::FindById(uid_t uid, Line* line) { + return Find(line, [uid](const auto& line) { + uid_t line_id; + if (!FieldToUid(line->fields[2], &line_id)) { + return false; + } + + return line_id == uid; + }); +} + +template <typename Line> +bool MmapFile::FindByName(const char* name, Line* line) { + return Find(line, [name](const auto& line) { + const char* line_name = line->fields[0]; + if (line_name == nullptr) { + return false; + } + + const char* match_name = name; + while (*line_name != '\n' && *line_name != ':' && *match_name != '\0') { + if (*line_name++ != *match_name++) { + return false; + } + } + + return *line_name == ':' && *match_name == '\0'; + }); +} + +PasswdFile::PasswdFile(const char* filename) : mmap_file_(filename) { +} + +bool PasswdFile::FindById(uid_t id, passwd_state_t* passwd_state) { + ErrnoRestorer errno_restorer; + PasswdLine passwd_line; + return mmap_file_.FindById(id, &passwd_line) && passwd_line.ToPasswdState(passwd_state); +} + +bool PasswdFile::FindByName(const char* name, passwd_state_t* passwd_state) { + ErrnoRestorer errno_restorer; + PasswdLine passwd_line; + return mmap_file_.FindByName(name, &passwd_line) && passwd_line.ToPasswdState(passwd_state); +} + +GroupFile::GroupFile(const char* filename) : mmap_file_(filename) { +} + +bool GroupFile::FindById(gid_t id, group_state_t* group_state) { + ErrnoRestorer errno_restorer; + GroupLine group_line; + return mmap_file_.FindById(id, &group_line) && group_line.ToGroupState(group_state); +} + +bool GroupFile::FindByName(const char* name, group_state_t* group_state) { + ErrnoRestorer errno_restorer; + GroupLine group_line; + return mmap_file_.FindByName(name, &group_line) && group_line.ToGroupState(group_state); +} diff --git a/libc/bionic/grp_pwd_file.h b/libc/bionic/grp_pwd_file.h new file mode 100644 index 000000000..ae8cd982e --- /dev/null +++ b/libc/bionic/grp_pwd_file.h @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef GRP_PWD_FILE_H +#define GRP_PWD_FILE_H + +#include <grp.h> +#include <pwd.h> + +#include "private/bionic_lock.h" +#include "private/bionic_macros.h" +#include "private/grp_pwd.h" + +class MmapFile { + public: + MmapFile(const char* filename); + ~MmapFile(); + + template <typename Line> + bool FindById(uid_t uid, Line* line); + template <typename Line> + bool FindByName(const char* name, Line* line); + + DISALLOW_COPY_AND_ASSIGN(MmapFile); + + private: + enum class FileStatus { + Uninitialized, + Initialized, + Error, + }; + + bool GetFile(const char** start, const char** end); + bool DoMmap(); + + template <typename Line, typename Predicate> + bool Find(Line* line, Predicate predicate); + + FileStatus status_ = FileStatus::Uninitialized; + Lock lock_; + const char* filename_ = nullptr; + const char* start_ = nullptr; + const char* end_ = nullptr; +}; + +class PasswdFile { + public: + PasswdFile(const char* filename); + + bool FindById(uid_t id, passwd_state_t* passwd_state); + bool FindByName(const char* name, passwd_state_t* passwd_state); + + DISALLOW_COPY_AND_ASSIGN(PasswdFile); + + private: + MmapFile mmap_file_; +}; + +class GroupFile { + public: + GroupFile(const char* filename); + + bool FindById(gid_t id, group_state_t* group_state); + bool FindByName(const char* name, group_state_t* group_state); + + DISALLOW_COPY_AND_ASSIGN(GroupFile); + + private: + MmapFile mmap_file_; +}; + +#endif diff --git a/libc/private/bionic_lock.h b/libc/private/bionic_lock.h index b38924716..54168d3ad 100644 --- a/libc/private/bionic_lock.h +++ b/libc/private/bionic_lock.h @@ -76,4 +76,19 @@ class Lock { } }; +class LockGuard { + public: + LockGuard(Lock& lock) : lock_(lock) { + lock_.lock(); + } + ~LockGuard() { + lock_.unlock(); + } + + DISALLOW_COPY_AND_ASSIGN(LockGuard); + + private: + Lock& lock_; +}; + #endif // _BIONIC_LOCK_H |