platform_system_core/init/util_test.cpp
Eric Biggers 6cb5a36f4c init: fix mkdir to reliably detect top-level /data directories
To determine the default encryption action, the mkdir command checks
whether the given path is a top-level directory of /data.  However, it
assumed a path without any duplicate slashes or trailing slash(es).

While everyone *should* be providing paths without unnecessary slashes,
it is not guaranteed, as paths with unnecessary slashes still work
correctly for all other parts of the mkdir command, including the
SELinux label lookup and the actual directory creation.  In particular,
the /data/fonts directory is being created using 'mkdir /data/fonts/'.

The effect is that the mkdir command thinks that /data/fonts/ is *not* a
top-level directory of /data, so it defaults to no encryption action.
Fortunately, the full command happens to use "encryption=Require", so we
dodged a bullet there, though the warning "Inferred action different
from explicit one" is still triggered.

There are a few approaches we could take here, including even just
fixing the /data/fonts/ command specifically, but I think the best
solution is to have mkdir clean its path at the very beginning.  This
retains the Linux path semantics that people expect, while avoiding
surprises in path processing afterwards.  This CL implements that.

Note, this CL intentionally changes the behavior of, and thus would
break, any existing cases where mkdir is used to create a top-level
/data directory using a path with unnecessary slashes and without using
an explicit encryption action.  There are no known cases where this
already occurs, however.  No cases exist in platform code, and vendor
init scripts shouldn't be creating top-level /data directories anyway.

Test: atest CtsInitTestCases
Test: Booted and verified that a trailing slash is no longer present in
      the log message "Verified that /data/fonts/ has the encryption
      policy ...".  Also verified that the message "Inferred action
      different ..." is no longer present just above it.
Bug: 232554803
Change-Id: Ie55c3ac1a2b1cf50632d54a1e565cb98c17b2a6a
2022-05-13 19:29:00 +00:00

187 lines
6.4 KiB
C++

/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "util.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <gtest/gtest.h>
using namespace std::literals::string_literals;
namespace android {
namespace init {
TEST(util, ReadFile_ENOENT) {
errno = 0;
auto file_contents = ReadFile("/proc/does-not-exist");
EXPECT_EQ(ENOENT, errno);
ASSERT_FALSE(file_contents.ok());
EXPECT_EQ("open() failed: No such file or directory", file_contents.error().message());
}
TEST(util, ReadFileGroupWriteable) {
std::string s("hello");
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
EXPECT_RESULT_OK(WriteFile(tf.path, s));
EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
auto file_contents = ReadFile(tf.path);
ASSERT_FALSE(file_contents.ok()) << strerror(errno);
EXPECT_EQ("Skipping insecure file", file_contents.error().message());
}
TEST(util, ReadFileWorldWiteable) {
std::string s("hello");
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
EXPECT_RESULT_OK(WriteFile(tf.path, s));
EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
auto file_contents = ReadFile(tf.path);
ASSERT_FALSE(file_contents.ok());
EXPECT_EQ("Skipping insecure file", file_contents.error().message());
}
TEST(util, ReadFileSymbolicLink) {
errno = 0;
// lrwxr-xr-x 1 root shell 6 2009-01-01 09:00 /system/bin/ps -> toybox
auto file_contents = ReadFile("/system/bin/ps");
EXPECT_EQ(ELOOP, errno);
ASSERT_FALSE(file_contents.ok());
EXPECT_EQ("open() failed: Too many symbolic links encountered",
file_contents.error().message());
}
TEST(util, ReadFileSuccess) {
auto file_contents = ReadFile("/proc/version");
ASSERT_TRUE(file_contents.ok());
EXPECT_GT(file_contents->length(), 6U);
EXPECT_EQ('\n', file_contents->at(file_contents->length() - 1));
(*file_contents)[5] = 0;
EXPECT_STREQ("Linux", file_contents->c_str());
}
TEST(util, WriteFileBinary) {
std::string contents("abcd");
contents.push_back('\0');
contents.push_back('\0');
contents.append("dcba");
ASSERT_EQ(10u, contents.size());
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
EXPECT_RESULT_OK(WriteFile(tf.path, contents));
auto read_back_contents = ReadFile(tf.path);
ASSERT_RESULT_OK(read_back_contents);
EXPECT_EQ(contents, *read_back_contents);
EXPECT_EQ(10u, read_back_contents->size());
}
TEST(util, WriteFileNotExist) {
std::string s("hello");
TemporaryDir test_dir;
std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
EXPECT_RESULT_OK(WriteFile(path, s));
auto file_contents = ReadFile(path);
ASSERT_RESULT_OK(file_contents);
EXPECT_EQ(s, *file_contents);
struct stat sb;
int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
EXPECT_NE(-1, fd);
EXPECT_EQ(0, fstat(fd, &sb));
EXPECT_EQ(0, close(fd));
EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
EXPECT_EQ(0, unlink(path.c_str()));
}
TEST(util, WriteFileExist) {
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
EXPECT_RESULT_OK(WriteFile(tf.path, "1hello1"));
auto file_contents = ReadFile(tf.path);
ASSERT_RESULT_OK(file_contents);
EXPECT_EQ("1hello1", *file_contents);
EXPECT_RESULT_OK(WriteFile(tf.path, "2ll2"));
file_contents = ReadFile(tf.path);
ASSERT_RESULT_OK(file_contents);
EXPECT_EQ("2ll2", *file_contents);
}
TEST(util, DecodeUid) {
auto decoded_uid = DecodeUid("root");
EXPECT_TRUE(decoded_uid.ok());
EXPECT_EQ(0U, *decoded_uid);
decoded_uid = DecodeUid("toot");
EXPECT_FALSE(decoded_uid.ok());
EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error().message());
decoded_uid = DecodeUid("123");
EXPECT_RESULT_OK(decoded_uid);
EXPECT_EQ(123U, *decoded_uid);
}
TEST(util, is_dir) {
TemporaryDir test_dir;
EXPECT_TRUE(is_dir(test_dir.path));
TemporaryFile tf;
EXPECT_FALSE(is_dir(tf.path));
}
TEST(util, mkdir_recursive) {
TemporaryDir test_dir;
std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
EXPECT_TRUE(mkdir_recursive(path, 0755));
std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
EXPECT_TRUE(is_dir(path1.c_str()));
std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
EXPECT_TRUE(is_dir(path1.c_str()));
std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
EXPECT_TRUE(is_dir(path1.c_str()));
}
TEST(util, mkdir_recursive_extra_slashes) {
TemporaryDir test_dir;
std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
EXPECT_TRUE(mkdir_recursive(path, 0755));
std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
EXPECT_TRUE(is_dir(path1.c_str()));
std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
EXPECT_TRUE(is_dir(path1.c_str()));
std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
EXPECT_TRUE(is_dir(path1.c_str()));
}
TEST(util, CleanDirPath) {
EXPECT_EQ("", CleanDirPath(""));
EXPECT_EQ("/", CleanDirPath("/"));
EXPECT_EQ("/", CleanDirPath("//"));
EXPECT_EQ("/foo", CleanDirPath("/foo"));
EXPECT_EQ("/foo", CleanDirPath("//foo"));
EXPECT_EQ("/foo", CleanDirPath("/foo/"));
EXPECT_EQ("/foo/bar", CleanDirPath("/foo/bar"));
EXPECT_EQ("/foo/bar", CleanDirPath("/foo/bar/"));
EXPECT_EQ("/foo/bar", CleanDirPath("/foo/bar////"));
EXPECT_EQ("/foo/bar", CleanDirPath("//foo//bar"));
}
} // namespace init
} // namespace android