From c49d7069039241ad02a8a710e9054d253abee3ad Mon Sep 17 00:00:00 2001 From: bigbiff bigbiff Date: Fri, 11 Oct 2013 20:28:00 -0400 Subject: fix getting and setting contexts in selinux recreate lost+found with selinux contexts Change-Id: I19e8696d47319dfb641520956c8a27c2a796a0c3 --- libtar/append.c | 15 ++++----------- libtar/extract.c | 3 ++- partition.cpp | 9 +++++++++ partitionmanager.cpp | 1 - twrp.cpp | 10 +++++++++- twrpTar.cpp | 7 ++++++- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/libtar/append.c b/libtar/append.c index 3a8bfc690..dcd864984 100644 --- a/libtar/append.c +++ b/libtar/append.c @@ -92,29 +92,22 @@ tar_append_file(TAR *t, char *realname, char *savename) #ifdef HAVE_SELINUX /* get selinux context */ - if(t->options & TAR_STORE_SELINUX) - { - if(t->th_buf.selinux_context != NULL) - { + if(t->options & TAR_STORE_SELINUX) { + if(t->th_buf.selinux_context != NULL) { free(t->th_buf.selinux_context); t->th_buf.selinux_context = NULL; } security_context_t selinux_context = NULL; - if(getfilecon(realname, &selinux_context) >= 0) - { + if (lgetfilecon(realname, &selinux_context) >= 0) { t->th_buf.selinux_context = strdup(selinux_context); + printf("setting selinux context: %s\n", selinux_context); freecon(selinux_context); } else - { -#ifdef DEBUG perror("Failed to get selinux context"); -#endif - } } #endif - /* check if it's a hardlink */ #ifdef DEBUG puts(" tar_append_file(): checking inode cache for hardlink..."); diff --git a/libtar/extract.c b/libtar/extract.c index d19ba859d..8081aa2b9 100644 --- a/libtar/extract.c +++ b/libtar/extract.c @@ -161,8 +161,9 @@ tar_extract_file(TAR *t, char *realname, char *prefix) #ifdef DEBUG printf(" Restoring SELinux context %s to file %s\n", t->th_buf.selinux_context, realname); #endif - if(setfilecon(realname, t->th_buf.selinux_context) < 0) + if (lsetfilecon(realname, t->th_buf.selinux_context) < 0) { fprintf(stderr, "Failed to restore SELinux context %s!\n", strerror(errno)); + } } #endif diff --git a/partition.cpp b/partition.cpp index c1c99f6f2..10cfb9f27 100644 --- a/partition.cpp +++ b/partition.cpp @@ -53,6 +53,9 @@ extern "C" { #include "make_ext4fs.h" #endif } +#ifdef HAVE_SELINUX +#include "selinux/selinux.h" +#endif using namespace std; @@ -1272,6 +1275,12 @@ bool TWPartition::Wipe_EXT4() { LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str()); return false; } else { + #ifdef HAVE_SELINUX + string sedir = Mount_Point + "/lost+found"; + PartitionManager.Mount_By_Path(sedir.c_str(), true); + rmdir(sedir.c_str()); + mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP); + #endif return true; } #else diff --git a/partitionmanager.cpp b/partitionmanager.cpp index ca93a2ab1..445e4b563 100644 --- a/partitionmanager.cpp +++ b/partitionmanager.cpp @@ -901,7 +901,6 @@ int TWPartitionManager::Run_Restore(string Restore_Name) { end_pos = Restore_List.find(";", start_pos); } } - TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details"); Update_System_Details(); UnMount_Main_Partitions(); diff --git a/twrp.cpp b/twrp.cpp index 765cad342..56ffedb90 100644 --- a/twrp.cpp +++ b/twrp.cpp @@ -104,10 +104,18 @@ int main(int argc, char **argv) { gui_loadResources(); #ifdef HAVE_SELINUX + if (TWFunc::Path_Exists("/prebuilt_file_contexts")) { + if (TWFunc::Path_Exists("/file_contexts")) { + printf("Renaming regular /file_contexts -> /file_contexts.bak\n"); + rename("/file_contexts", "/file_contexts.bak"); + } + printf("Moving /prebuilt_file_contexts -> /file_contexts\n"); + rename("/prebuilt_file_contexts", "/file_contexts"); + } struct selinux_opt selinux_options[] = { { SELABEL_OPT_PATH, "/file_contexts" } }; - selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); + selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); if (!selinux_handle) printf("No file contexts for SELinux\n"); else diff --git a/twrpTar.cpp b/twrpTar.cpp index 76c23c868..eba482ee7 100644 --- a/twrpTar.cpp +++ b/twrpTar.cpp @@ -34,6 +34,7 @@ extern "C" { #include #include #include +#include #include #include "twrpTar.hpp" #include "twcommon.h" @@ -532,7 +533,7 @@ int twrpTar::Generate_Multiple_Archives(string Path) { continue; // Skip /data/media if (de->d_type == DT_BLK || de->d_type == DT_CHR) continue; - if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, "lost+foud") != 0) + if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) { unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false); if (Archive_Current_Size + folder_size > MAX_ARCHIVE_SIZE) { @@ -723,6 +724,10 @@ int twrpTar::tarDirs(bool include_root) { if (strcmp(de->d_name, ".") != 0) { subfolder += de->d_name; } else { + std::string parentDir = basename(subfolder.c_str()); + LOGINFO("parentDir: %s\n", parentDir.c_str()); + if (!parentDir.compare("lost+found")) + continue; LOGINFO("tarDirs addFile '%s' including root: %i\n", subfolder.c_str(), include_root); if (addFile(subfolder, include_root) != 0) return -1; -- cgit v1.2.3