Commit b5fab76b authored by Ethan Yonker's avatar Ethan Yonker Committed by Dees Troy

Replace fix permissions with fix contexts for emulated storage

Fix permissions rarely fixed anything on more recent versions of
Android and usually made things worse. Instead we will replace it
with a more dumbed down option that should fix contexts on
/data/media with a few improvements to ensure that contexts get
fixed for multiple users and on adopted storage.

Change-Id: If5523781936a0b04196e2ad871cae767ebae2583
parent ebc4cfae
......@@ -42,7 +42,7 @@ TARGET_RECOVERY_GUI := true
LOCAL_SRC_FILES := \
twrp.cpp \
fixPermissions.cpp \
fixContexts.cpp \
twrpTar.cpp \
twrpDU.cpp \
twrpDigest.cpp \
......@@ -342,7 +342,6 @@ LOCAL_ADDITIONAL_DEPENDENCIES := \
dump_image \
erase_image \
flash_image \
fix_permissions.sh \
mke2fs.conf \
pigz \
teamwin \
......
/*
Copyright 2012-2016 bigbiff/Dees_Troy TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <cctype>
#include "fixContexts.hpp"
#include "twrp-functions.hpp"
#include "twcommon.h"
#ifdef HAVE_SELINUX
#include "selinux/selinux.h"
#include "selinux/label.h"
#include "selinux/android.h"
#include "selinux/label.h"
#endif
using namespace std;
#ifdef HAVE_SELINUX
struct selabel_handle *sehandle;
struct selinux_opt selinux_options[] = {
{ SELABEL_OPT_PATH, "/file_contexts" }
};
int fixContexts::restorecon(string entry, struct stat *sb) {
char *oldcontext, *newcontext;
if (lgetfilecon(entry.c_str(), &oldcontext) < 0) {
LOGINFO("Couldn't get selinux context for %s\n", entry.c_str());
return -1;
}
if (selabel_lookup(sehandle, &newcontext, entry.c_str(), sb->st_mode) < 0) {
LOGINFO("Couldn't lookup selinux context for %s\n", entry.c_str());
return -1;
}
if (strcmp(oldcontext, newcontext) != 0) {
LOGINFO("Relabeling %s from %s to %s\n", entry.c_str(), oldcontext, newcontext);
if (lsetfilecon(entry.c_str(), newcontext) < 0) {
LOGINFO("Couldn't label %s with %s: %s\n", entry.c_str(), newcontext, strerror(errno));
}
}
freecon(oldcontext);
freecon(newcontext);
return 0;
}
int fixContexts::fixContextsRecursively(string name, int level) {
DIR *d;
struct dirent *de;
struct stat sb;
string path;
if (!(d = opendir(name.c_str())))
return -1;
if (!(de = readdir(d)))
return -1;
do {
if (de->d_type == DT_DIR) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
path = name + "/" + de->d_name;
restorecon(path, &sb);
fixContextsRecursively(path, level + 1);
}
else {
path = name + "/" + de->d_name;
restorecon(path, &sb);
}
} while ((de = readdir(d)));
closedir(d);
return 0;
}
int fixContexts::fixDataMediaContexts(string Mount_Point) {
DIR *d;
struct dirent *de;
struct stat sb;
LOGINFO("Fixing media contexts on '%s'\n", Mount_Point.c_str());
sehandle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
if (!sehandle) {
LOGINFO("Unable to open /file_contexts\n");
return 0;
}
if (TWFunc::Path_Exists(Mount_Point + "/media/0")) {
string dir = Mount_Point + "/media";
if (!(d = opendir(dir.c_str()))) {
LOGINFO("opendir failed (%s)\n", strerror(errno));
return -1;
}
if (!(de = readdir(d))) {
LOGINFO("readdir failed (%s)\n", strerror(errno));
closedir(d);
return -1;
}
do {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0 || de->d_type != DT_DIR)
continue;
size_t len = strlen(de->d_name);
bool is_numeric = true;
char* folder_name = de->d_name;
for (size_t i = 0; i < len; i++) {
if (!isdigit(*folder_name)) {
is_numeric = false;
break;
}
folder_name++;
}
if (is_numeric) {
dir = Mount_Point + "/media/";
dir += de->d_name;
restorecon(dir, &sb);
fixContextsRecursively(dir, 0);
}
} while ((de = readdir(d)));
closedir(d);
} else if (TWFunc::Path_Exists(Mount_Point + "/media")) {
restorecon(Mount_Point + "/media", &sb);
fixContextsRecursively(Mount_Point + "/media", 0);
} else {
LOGINFO("fixDataMediaContexts: %s/media does not exist!\n", Mount_Point.c_str());
return 0;
}
selabel_close(sehandle);
return 0;
}
#else
int fixContexts::restorecon(string entry __unused, struct stat *sb __unused) {
return -1;
}
int fixContexts::fixContextsRecursively(string name __unused, int level __unused) {
return -1;
}
int fixContexts::fixDataMediaContexts(string Mount_Point __unused) {
return -1;
}
#endif
/*
Copyright 2012-2016 bigbiff/Dees_Troy TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __FIXCONTEXTS_HPP
#define __FIXCONTEXTS_HPP
#include <string>
using namespace std;
class fixContexts {
public:
static int fixDataMediaContexts(string Mount_Point);
private:
static int restorecon(string entry, struct stat *sb);
static int fixContextsRecursively(string path, int level);
};
#endif
This diff is collapsed.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include "gui/rapidxml.hpp"
#include "twrp-functions.hpp"
using namespace std;
class fixPermissions {
public:
fixPermissions();
~fixPermissions();
int fixPerms(bool enable_debug, bool remove_data_for_missing_apps);
int fixContexts();
int fixDataInternalContexts(void);
private:
int pchown(string fn, int puid, int pgid);
int pchmod(string fn, mode_t mode);
vector <string> listAllDirectories(string path);
vector <string> listAllFiles(string path);
void deletePackages();
int getPackages(const string& packageFile);
int fixApps();
int fixAllFiles(string directory, int uid, int gid, mode_t file_perms);
int fixDir(const string& dir, int diruid, int dirgid, mode_t dirmode, int fileuid, int filegid, mode_t filemode);
int fixDataData(string dataDir);
int restorecon(string entry, struct stat *sb);
int fixDataDataContexts(void);
int fixContextsRecursively(string path, int level);
struct package {
string pkgName;
string codePath;
string appDir;
string dDir;
int gid;
int uid;
package *next;
};
bool debug;
bool remove_data;
package* head;
};
......@@ -209,6 +209,7 @@ GUIAction::GUIAction(xml_node<>* node)
ADD_ACTION(wipe);
ADD_ACTION(refreshsizes);
ADD_ACTION(nandroid);
ADD_ACTION(fixcontexts);
ADD_ACTION(fixpermissions);
ADD_ACTION(dd);
ADD_ACTION(partitionsd);
......@@ -1219,16 +1220,16 @@ int GUIAction::cancelbackup(std::string arg __unused) {
return 0;
}
int GUIAction::fixpermissions(std::string arg __unused)
int GUIAction::fixcontexts(std::string arg __unused)
{
int op_status = 0;
operation_start("Fix Permissions");
LOGINFO("fix permissions started!\n");
operation_start("Fix Contexts");
LOGINFO("fix contexts started!\n");
if (simulate) {
simulate_progress_bar();
} else {
op_status = PartitionManager.Fix_Permissions();
op_status = PartitionManager.Fix_Contexts();
if (op_status != 0)
op_status = 1; // failure
}
......@@ -1236,6 +1237,11 @@ int GUIAction::fixpermissions(std::string arg __unused)
return 0;
}
int GUIAction::fixpermissions(std::string arg)
{
return fixcontexts(arg);
}
int GUIAction::dd(std::string arg)
{
operation_start("imaging");
......
......@@ -337,6 +337,7 @@ protected:
int wipe(std::string arg);
int refreshsizes(std::string arg);
int nandroid(std::string arg);
int fixcontexts(std::string arg);
int fixpermissions(std::string arg);
int dd(std::string arg);
int partitionsd(std::string arg);
......
......@@ -3094,9 +3094,10 @@
</button>
<button style="main_button">
<condition var1="tw_has_data_media" var2="1"/>
<placement x="%center_x%" y="%row2_y%"/>
<text>{@fix_perm_btn=Fix Permissions}</text>
<action function="page">fixperms</action>
<text>{@fix_context_btn=Fix Contexts}</text>
<action function="page">fixcontexts</action>
</button>
<button style="main_button">
......@@ -4467,7 +4468,7 @@
</action>
</page>
<page name="fixperms">
<page name="fixcontexts">
<template name="page"/>
<text style="text_l">
......@@ -4477,37 +4478,31 @@
<text style="text_m">
<placement x="%col1_x_header%" y="%row4_header_y%"/>
<text>{@fix_perms_hdr=Fix Permissions}</text>
<text>{@fix_contexts_hdr=Fix Contexts}</text>
</text>
<text style="text_m_accent">
<placement x="%center_x%" y="%row2_y%" placement="5"/>
<text>{@fix_perms_note=Note: Fixing permissions is rarely needed.}</text>
<text>{@fix_contexts_note1=Note: Fixing contexts is rarely needed.}</text>
</text>
<checkbox>
<placement x="%col2_x_left%" y="%row4_y%"/>
<text>{@fix_perms_selinux_chk=Also fix SELinux Contexts}</text>
<data variable="tw_fixperms_restorecon"/>
</checkbox>
<text style="text_m_fail">
<placement x="%center_x%" y="%row6_y%" placement="5"/>
<text>{@fix_perms_sel_note1=Fixing SELinux Contexts may cause}</text>
<text>{@fix_contexts_note2=Fixing SELinux Contexts may cause}</text>
</text>
<text style="text_m_fail">
<placement x="%center_x%" y="%row7_y%" placement="5"/>
<text>{@fix_perms_sel_note2=your device to not boot properly.}</text>
<text>{@fix_contexts_note3=your device to not boot properly.}</text>
</text>
<slider style="slider_centered">
<text>{@swipe_to_fix_perms=Swipe to Fix Permissions}</text>
<text>{@swipe_to_fix_contexts=Swipe to Fix Contexts}</text>
<actions>
<action function="set">tw_back=advanced</action>
<action function="set">tw_action=fixpermissions</action>
<action function="set">tw_action_text1={@fixing_perms=Fixing Permissions...}</action>
<action function="set">tw_complete_text1={@fix_perms_complete=Fix Permissions Complete}</action>
<action function="set">tw_action=fixcontexts</action>
<action function="set">tw_action_text1={@fixing_contexts=Fixing Contexts...}</action>
<action function="set">tw_complete_text1={@fix_contexts_complete=Fix Contexts Complete}</action>
<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_page</action>
......
......@@ -365,8 +365,7 @@
<string name="copy_log_confirm">Copy Log to SD Card?</string>
<string name="copying_log">Copying Log to SD Card...</string>
<string name="copy_log_complete">Log Copy Complete</string>
<string name="fix_perm_btn">Fix Permissions</string>
<string name="fix_perm_s_btn">Fix Perms</string>
<string name="fix_context_btn">Fix Contexts</string>
<string name="part_sd_btn">Partition SD Card</string>
<string name="part_sd_s_btn">SD Card</string>
<string name="file_manager_btn">File Manager</string>
......@@ -453,15 +452,14 @@
<string name="sideload_confirm">ADB Sideload</string>
<string name="sideload_usage">Usage: adb sideload filename.zip</string>
<string name="sideload_complete">ADB Sideload Complete</string>
<string name="fix_perms_hdr">Fix Permissions</string>
<string name="fix_perms_note">Note: Fixing permissions is rarely needed.</string>
<string name="fix_perms_selinux_chk">Also fix SELinux Contexts</string>
<string name="fix_perms_sel_note1">Fixing SELinux Contexts may cause</string>
<string name="fix_perms_sel_note2">your device to not boot properly.</string>
<string name="swipe_to_fix_perms">Swipe to Fix Permissions</string>
<string name="swipe_fix_perms"> Fix Perms</string>
<string name="fixing_perms">Fixing Permissions...</string>
<string name="fix_perms_complete">Fix Permissions Complete</string>
<string name="fix_contexts_hdr">Fix Contexts</string>
<string name="fix_contexts_note1">Note: Fixing contexts is rarely needed.</string>
<string name="fix_contexts_note2">Fixing SELinux Contexts may cause</string>
<string name="fix_contexts_note3">your device to not boot properly.</string>
<string name="swipe_to_fix_contexts">Swipe to Fix Contexts</string>
<string name="swipe_fix_contexts"> Fix Contexts</string>
<string name="fixing_contexts">Fixing Contexts...</string>
<string name="fix_contexts_complete">Fix Contexts Complete</string>
<string name="reboot_hdr">Reboot</string>
<string name="su_hdr">SuperSU Check</string>
<string name="su_note1">Your device does not appear to be rooted.</string>
......
......@@ -3168,9 +3168,10 @@
</button>
<button style="main_button">
<condition var1="tw_has_data_media" var2="1"/>
<placement x="%center_x%" y="%row2a_y%"/>
<text>{@fix_perm_btn=Fix Permissions}</text>
<action function="page">fixperms</action>
<text>{@fix_context_btn=Fix Contexts}</text>
<action function="page">fixcontexts</action>
</button>
<button style="main_button">
......@@ -4363,7 +4364,7 @@
</action>
</page>
<page name="fixperms">
<page name="fixcontexts">
<template name="page"/>
<text style="text_l">
......@@ -4373,37 +4374,31 @@
<text style="text_m">
<placement x="%col1_x_header%" y="%row4_header_y%"/>
<text>{@fix_perms_hdr=Fix Permissions}</text>
<text>{@fix_contexts_hdr=Fix Contexts}</text>
</text>
<text style="text_m_accent">
<placement x="%center_x%" y="%row2_y%" placement="5"/>
<text>{@fix_perms_note=Note: Fixing permissions is rarely needed.}</text>
<text>{@fix_contexts_note1=Note: Fixing contexts is rarely needed.}</text>
</text>
<checkbox>
<placement x="%indent%" y="%row4_y%"/>
<text>{@fix_perms_selinux_chk=Also fix SELinux Contexts}</text>
<data variable="tw_fixperms_restorecon"/>
</checkbox>
<text style="text_m_fail">
<placement x="%center_x%" y="%row6_y%" placement="5"/>
<text>{@fix_perms_sel_note1=Fixing SELinux Contexts may cause}</text>
<text>{@fix_contexts_note2=Fixing SELinux Contexts may cause}</text>
</text>
<text style="text_m_fail">
<placement x="%center_x%" y="%row7_y%" placement="5"/>
<text>{@fix_perms_sel_note2=your device to not boot properly.}</text>
<text>{@fix_contexts_note3=your device to not boot properly.}</text>
</text>
<slider>
<text>{@swipe_to_fix_perms=Swipe to Fix Permissions}</text>
<text>{@swipe_to_fix_contexts=Swipe to Fix Contexts}</text>
<actions>
<action function="set">tw_back=advanced</action>
<action function="set">tw_action=fixpermissions</action>
<action function="set">tw_action_text1={@fixing_perms=Fixing Permissions...}</action>
<action function="set">tw_complete_text1={@fix_perms_complete=Fix Permissions Complete}</action>
<action function="set">tw_action=fixcontexts</action>
<action function="set">tw_action_text1={@fixing_contexts=Fixing Contexts...}</action>
<action function="set">tw_complete_text1={@fix_contexts_complete=Fix Contexts Complete}</action>
<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_page</action>
......
......@@ -3887,9 +3887,10 @@
</button>
<button style="main_button">
<condition var1="tw_has_data_media" var2="1"/>
<placement x="%col1_x_right%" y="%row1_y%"/>
<text>{@fix_perm_s_btn=Fix Perms}</text>
<action function="page">fixperms</action>
<text>{@fix_context_btn=Fix Contexts}</text>
<action function="page">fixcontexts</action>
</button>
<button style="main_button">
......@@ -5158,44 +5159,38 @@
</action>
</page>
<page name="fixperms">
<page name="fixcontexts">
<template name="page"/>
<template name="statusbar"/>
<text style="text_m">
<placement x="%col1_x_left%" y="%row1_header_y%"/>
<text>{@advanced_hdr=Advanced} &gt; {@fix_perms_hdr=Fix Permissions}</text>
<text>{@advanced_hdr=Advanced} &gt; {@fix_contexts_hdr=Fix Contexts}</text>
</text>
<text style="text_m_accent">
<placement x="%center_x%" y="%row2_y%" placement="5"/>
<text>{@fix_perms_note=Note: Fixing permissions is rarely needed.}</text>
<text>{@fix_contexts_note1=Note: Fixing contexts is rarely needed.}</text>
</text>
<checkbox>
<placement x="%indent%" y="%row4_y%"/>
<text>{@fix_perms_selinux_chk=Also fix SELinux Contexts}</text>
<data variable="tw_fixperms_restorecon"/>
</checkbox>
<text style="text_m_fail">
<placement x="%center_x%" y="%row6a_y%" placement="5"/>
<text>{@fix_perms_sel_note1=Fixing SELinux Contexts may cause}</text>
<text>{@fix_contexts_note2=Fixing SELinux Contexts may cause}</text>
</text>
<text style="text_m_fail">
<placement x="%center_x%" y="%row7a_y%" placement="5"/>
<text>{@fix_perms_sel_note2=your device to not boot properly.}</text>
<text>{@fix_contexts_note3=your device to not boot properly.}</text>
</text>
<slider>
<text>{@swipe_fix_perms= Fix Perms}</text>
<text>{@swipe_fix_contexts= Fix Contexts}</text>
<actions>
<action function="set">tw_back=advanced</action>
<action function="set">tw_action=fixpermissions</action>
<action function="set">tw_action_text1={@fixing_perms=Fixing Permissions...}</action>
<action function="set">tw_complete_text1={@fix_perms_complete=Fix Permissions Complete}</action>
<action function="set">tw_action=fixcontexts</action>
<action function="set">tw_action_text1={@fixing_contexts=Fixing Contexts...}</action>
<action function="set">tw_complete_text1={@fix_contexts_complete=Fix Contexts Complete}</action>
<action function="set">tw_slider_text={@swipe_confirm= Confirm}</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_page</action>
......
......@@ -381,8 +381,8 @@ int OpenRecoveryScript::run_script_file(void) {
}
property_set("ctl.start", "adbd");
gui_msg("done=Done.");
} else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0) {
ret_val = PartitionManager.Fix_Permissions();
} else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0 || strcmp(command, "fixcontexts") == 0) {
ret_val = PartitionManager.Fix_Contexts();
if (ret_val != 0)
ret_val = 1; // failure
} else if (strcmp(command, "decrypt") == 0) {
......
......@@ -41,7 +41,6 @@
#include "twrpDigest.hpp"
#include "twrpTar.hpp"
#include "twrpDU.hpp"
#include "fixPermissions.hpp"
#include "infomanager.hpp"
#include "set_metadata.h"
#include "gui/gui.hpp"
......
......@@ -37,7 +37,7 @@
#include "partitions.hpp"
#include "data.hpp"
#include "twrp-functions.hpp"
#include "fixPermissions.hpp"
#include "fixContexts.hpp"
#include "twrpDigest.hpp"
#include "twrpDU.hpp"
#include "set_metadata.h"
......@@ -1490,25 +1490,24 @@ int TWPartitionManager::Decrypt_Device(string Password) {
return 1;
}
int TWPartitionManager::Fix_Permissions(void) {
int result = 0;
if (!Mount_By_Path("/data", true))
return false;
if (!Mount_By_Path("/system", true))
return false;
Mount_By_Path("/sd-ext", false);
fixPermissions perms;
result = perms.fixPerms(true, false);
int TWPartitionManager::Fix_Contexts(void) {
#ifdef HAVE_SELINUX
if (result == 0 && DataManager::GetIntValue("tw_fixperms_restorecon") == 1)
result = perms.fixContexts();
#endif
std::vector<TWPartition*>::iterator iter;
for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
if ((*iter)->Has_Data_Media) {
if ((*iter)->Mount(true)) {
if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0)
return -1;
}
}
}
UnMount_Main_Partitions();
gui_msg("done=Done.");
return result;
return 0;
#else
LOGERR("Cannot fix contexts, no selinux support present.\n");
return -1;
#endif
}
TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) {
......
......@@ -228,7 +228,7 @@ public:
TWPartition *Get_Default_Storage_Partition(); // Returns a pointer to a default storage partition
int Cancel_Backup(); // Signals partition backup to cancel
void Clean_Backup_Folder(string Backup_Folder); // Clean Backup Folder on Error
int Fix_Permissions();
int Fix_Contexts();
void Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List);
int Fstab_Processed(); // Indicates if the fstab has been processed or not
void Output_Storage_Fstab(); // Creates a /cache/recovery/storage.fstab file with a list of all potential storage locations for app use
......
......@@ -234,15 +234,6 @@ LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)
#fix_permissions
include $(CLEAR_VARS)
LOCAL_MODULE := fix_permissions.sh
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)
#mke2fs.conf
include $(CLEAR_VARS)
LOCAL_MODULE := mke2fs.conf
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment