Bug: 25861755 Test: Boot device, check directory exists as it should. Change-Id: I413631452e8e0bdd869887091f8b077bd5f9297egugelfrei
parent
3b71fc5100
commit
1a9652613a
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Tool to create a directory with the right SELinux context applied, or
|
||||
* apply the context if it's absent. Also fixes mode, uid, gid.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <android-base/logging.h>
|
||||
|
||||
#include <cutils/fs.h>
|
||||
#include <selinux/android.h>
|
||||
|
||||
void usage(const char* progname) {
|
||||
fprintf(stderr, "Usage: %s --mode MODE --uid UID --gid GID -- <path>\n", progname);
|
||||
}
|
||||
|
||||
bool small_int(const std::string& s) {
|
||||
return !s.empty() && s.size() < 7 && s.find_first_not_of("0123456789") == std::string::npos;
|
||||
}
|
||||
|
||||
int main(int argc, const char* const argv[]) {
|
||||
setenv("ANDROID_LOG_TAGS", "*:v", 1);
|
||||
android::base::InitLogging(const_cast<char**>(argv));
|
||||
std::vector<std::string> args(argv + 1, argv + argc);
|
||||
// Enforce exact format of arguments. You can always loosen but you can never tighten :)
|
||||
if (args.size() != 8 || args[0] != "--mode" || !small_int(args[1]) || args[2] != "--uid" ||
|
||||
!small_int(args[3]) || args[4] != "--gid" || !small_int(args[5]) || args[6] != "--") {
|
||||
usage(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
mode_t mode = (mode_t)stoi(args[1], nullptr, 8);
|
||||
uid_t uid = (uid_t)stoi(args[3]);
|
||||
gid_t gid = (gid_t)stoi(args[5]);
|
||||
const char* path = args[7].c_str();
|
||||
|
||||
struct selabel_handle* sehandle = selinux_android_file_context_handle();
|
||||
char* secontext = nullptr;
|
||||
if (sehandle) {
|
||||
if (selabel_lookup(sehandle, &secontext, path, S_IFDIR) == 0) {
|
||||
setfscreatecon(secontext);
|
||||
}
|
||||
}
|
||||
|
||||
if (fs_prepare_dir(path, mode, uid, gid) != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (secontext) {
|
||||
char* oldsecontext = nullptr;
|
||||
if (lgetfilecon(path, &oldsecontext) < 0) {
|
||||
PLOG(ERROR) << "Unable to read secontext for: " << path;
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(secontext, oldsecontext) != 0) {
|
||||
LOG(INFO) << "Relabelling from " << oldsecontext << " to " << secontext << ": " << path;
|
||||
if (lsetfilecon(path, secontext) != 0) {
|
||||
PLOG(ERROR) << "Relabelling failed for: " << path;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
#!/system/bin/sh
|
||||
#
|
||||
# Copyright (C) 2017 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.
|
||||
|
||||
set -e
|
||||
|
||||
case "$3" in
|
||||
*[!0-9]* | '')
|
||||
echo "Invalid user id"
|
||||
exit -1
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ x"$4" != x ] ; then
|
||||
echo "Volume must be root volume"
|
||||
exit -1;
|
||||
fi
|
||||
|
||||
case "$1" in
|
||||
misc_de|misc_ce)
|
||||
computed_path="/data/$1/$3"
|
||||
if [ x"$computed_path" != x"$2" ] ; then
|
||||
echo "Parameter path didn't match computed path: " $computed_path
|
||||
exit -1;
|
||||
fi
|
||||
/system/bin/prepare_dir --mode 700 --uid 0 --gid 0 -- "$computed_path"/vold
|
||||
;;
|
||||
*)
|
||||
echo "Unknown type: $1"
|
||||
exit -1;
|
||||
;;
|
||||
esac
|
Loading…
Reference in new issue