applypatch: fix memory leaks reported by static analysis.

Bug: 26906416
Change-Id: I163df5a8f3abda3ba5d4ed81dfc8567054eceb27
This commit is contained in:
Yabin Cui 2016-02-03 17:08:52 -08:00
parent 6e71c90fad
commit d483c20a7e
7 changed files with 202 additions and 303 deletions

View file

@ -55,7 +55,7 @@ LOCAL_CLANG := true
LOCAL_SRC_FILES := main.cpp LOCAL_SRC_FILES := main.cpp
LOCAL_MODULE := applypatch LOCAL_MODULE := applypatch
LOCAL_C_INCLUDES += bootable/recovery LOCAL_C_INCLUDES += bootable/recovery
LOCAL_STATIC_LIBRARIES += libapplypatch libbase libmtdutils libcrypto_static libbz LOCAL_STATIC_LIBRARIES += libapplypatch libbase libmtdutils libcrypto_static libbz libedify
LOCAL_SHARED_LIBRARIES += libz libcutils libc LOCAL_SHARED_LIBRARIES += libz libcutils libc
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)

View file

@ -25,6 +25,9 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <memory>
#include <string>
#include <android-base/strings.h> #include <android-base/strings.h>
#include "openssl/sha.h" #include "openssl/sha.h"
@ -67,25 +70,29 @@ int LoadFileContents(const char* filename, FileContents* file) {
} }
file->size = file->st.st_size; file->size = file->st.st_size;
file->data = reinterpret_cast<unsigned char*>(malloc(file->size)); file->data = nullptr;
std::unique_ptr<unsigned char, decltype(&free)> data(
static_cast<unsigned char*>(malloc(file->size)), free);
if (data == nullptr) {
printf("failed to allocate memory: %s\n", strerror(errno));
return -1;
}
FILE* f = fopen(filename, "rb"); FILE* f = fopen(filename, "rb");
if (f == NULL) { if (f == NULL) {
printf("failed to open \"%s\": %s\n", filename, strerror(errno)); printf("failed to open \"%s\": %s\n", filename, strerror(errno));
free(file->data);
file->data = NULL;
return -1; return -1;
} }
size_t bytes_read = fread(file->data, 1, file->size, f); size_t bytes_read = fread(data.get(), 1, file->size, f);
if (bytes_read != static_cast<size_t>(file->size)) { if (bytes_read != static_cast<size_t>(file->size)) {
printf("short read of \"%s\" (%zu bytes of %zd)\n", filename, bytes_read, file->size); printf("short read of \"%s\" (%zu bytes of %zd)\n", filename, bytes_read, file->size);
free(file->data); fclose(f);
file->data = NULL;
return -1; return -1;
} }
fclose(f); fclose(f);
file->data = data.release();
SHA1(file->data, file->size, file->sha1); SHA1(file->data, file->size, file->sha1);
return 0; return 0;
} }
@ -185,7 +192,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
uint8_t parsed_sha[SHA_DIGEST_LENGTH]; uint8_t parsed_sha[SHA_DIGEST_LENGTH];
// Allocate enough memory to hold the largest size. // Allocate enough memory to hold the largest size.
file->data = reinterpret_cast<unsigned char*>(malloc(size[index[pairs-1]])); file->data = static_cast<unsigned char*>(malloc(size[index[pairs-1]]));
char* p = (char*)file->data; char* p = (char*)file->data;
file->size = 0; // # bytes read so far file->size = 0; // # bytes read so far
bool found = false; bool found = false;
@ -313,7 +320,7 @@ int SaveFileContents(const char* filename, const FileContents* file) {
// "MTD:<partition>[:...]" or "EMMC:<partition_device>[:...]". The target name // "MTD:<partition>[:...]" or "EMMC:<partition_device>[:...]". The target name
// might contain multiple colons, but WriteToPartition() only uses the first // might contain multiple colons, but WriteToPartition() only uses the first
// two and ignores the rest. Return 0 on success. // two and ignores the rest. Return 0 on success.
int WriteToPartition(unsigned char* data, size_t len, const char* target) { int WriteToPartition(const unsigned char* data, size_t len, const char* target) {
std::string copy(target); std::string copy(target);
std::vector<std::string> pieces = android::base::Split(copy, ":"); std::vector<std::string> pieces = android::base::Split(copy, ":");
@ -352,7 +359,7 @@ int WriteToPartition(unsigned char* data, size_t len, const char* target) {
return -1; return -1;
} }
size_t written = mtd_write_data(ctx, reinterpret_cast<char*>(data), len); size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len);
if (written != len) { if (written != len) {
printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition); printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition);
mtd_write_close(ctx); mtd_write_close(ctx);
@ -578,7 +585,7 @@ int ShowLicenses() {
} }
ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) { ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
int fd = *reinterpret_cast<int *>(token); int fd = *static_cast<int*>(token);
ssize_t done = 0; ssize_t done = 0;
ssize_t wrote; ssize_t wrote;
while (done < len) { while (done < len) {
@ -592,19 +599,9 @@ ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
return done; return done;
} }
typedef struct {
unsigned char* buffer;
ssize_t size;
ssize_t pos;
} MemorySinkInfo;
ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) { ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
MemorySinkInfo* msi = reinterpret_cast<MemorySinkInfo*>(token); std::string* s = static_cast<std::string*>(token);
if (msi->size - msi->pos < len) { s->append(reinterpret_cast<const char*>(data), len);
return -1;
}
memcpy(msi->buffer + msi->pos, data, len);
msi->pos += len;
return len; return len;
} }
@ -815,31 +812,52 @@ static int GenerateTarget(FileContents* source_file,
const Value* bonus_data) { const Value* bonus_data) {
int retry = 1; int retry = 1;
SHA_CTX ctx; SHA_CTX ctx;
int output; std::string memory_sink_str;
MemorySinkInfo msi;
FileContents* source_to_use; FileContents* source_to_use;
char* outname;
int made_copy = 0; int made_copy = 0;
bool target_is_partition = (strncmp(target_filename, "MTD:", 4) == 0 ||
strncmp(target_filename, "EMMC:", 5) == 0);
const std::string tmp_target_filename = std::string(target_filename) + ".patch";
// assume that target_filename (eg "/system/app/Foo.apk") is located // assume that target_filename (eg "/system/app/Foo.apk") is located
// on the same filesystem as its top-level directory ("/system"). // on the same filesystem as its top-level directory ("/system").
// We need something that exists for calling statfs(). // We need something that exists for calling statfs().
char target_fs[strlen(target_filename)+1]; std::string target_fs = target_filename;
char* slash = strchr(target_filename+1, '/'); auto slash_pos = target_fs.find('/', 1);
if (slash != NULL) { if (slash_pos != std::string::npos) {
int count = slash - target_filename; target_fs.resize(slash_pos);
strncpy(target_fs, target_filename, count); }
target_fs[count] = '\0';
const Value* patch;
if (source_patch_value != NULL) {
source_to_use = source_file;
patch = source_patch_value;
} else { } else {
strcpy(target_fs, target_filename); source_to_use = copy_file;
patch = copy_patch_value;
}
if (patch->type != VAL_BLOB) {
printf("patch is not a blob\n");
return 1;
}
char* header = patch->data;
ssize_t header_bytes_read = patch->size;
bool use_bsdiff = false;
if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
use_bsdiff = true;
} else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
use_bsdiff = false;
} else {
printf("Unknown patch file format\n");
return 1;
} }
do { do {
// Is there enough room in the target filesystem to hold the patched // Is there enough room in the target filesystem to hold the patched
// file? // file?
if (strncmp(target_filename, "MTD:", 4) == 0 || if (target_is_partition) {
strncmp(target_filename, "EMMC:", 5) == 0) {
// If the target is a partition, we're actually going to // If the target is a partition, we're actually going to
// write the output to /tmp and then copy it to the // write the output to /tmp and then copy it to the
// partition. statfs() always returns 0 blocks free for // partition. statfs() always returns 0 blocks free for
@ -861,7 +879,7 @@ static int GenerateTarget(FileContents* source_file,
} else { } else {
int enough_space = 0; int enough_space = 0;
if (retry > 0) { if (retry > 0) {
size_t free_space = FreeSpaceForFile(target_fs); size_t free_space = FreeSpaceForFile(target_fs.c_str());
enough_space = enough_space =
(free_space > (256 << 10)) && // 256k (two-block) minimum (free_space > (256 << 10)) && // 256k (two-block) minimum
(free_space > (target_size * 3 / 2)); // 50% margin of error (free_space > (target_size * 3 / 2)); // 50% margin of error
@ -901,84 +919,53 @@ static int GenerateTarget(FileContents* source_file,
made_copy = 1; made_copy = 1;
unlink(source_filename); unlink(source_filename);
size_t free_space = FreeSpaceForFile(target_fs); size_t free_space = FreeSpaceForFile(target_fs.c_str());
printf("(now %zu bytes free for target) ", free_space); printf("(now %zu bytes free for target) ", free_space);
} }
} }
const Value* patch;
if (source_patch_value != NULL) {
source_to_use = source_file;
patch = source_patch_value;
} else {
source_to_use = copy_file;
patch = copy_patch_value;
}
if (patch->type != VAL_BLOB) {
printf("patch is not a blob\n");
return 1;
}
SinkFn sink = NULL; SinkFn sink = NULL;
void* token = NULL; void* token = NULL;
output = -1; int output_fd = -1;
outname = NULL; if (target_is_partition) {
if (strncmp(target_filename, "MTD:", 4) == 0 ||
strncmp(target_filename, "EMMC:", 5) == 0) {
// We store the decoded output in memory. // We store the decoded output in memory.
msi.buffer = reinterpret_cast<unsigned char*>(malloc(target_size));
if (msi.buffer == NULL) {
printf("failed to alloc %zu bytes for output\n", target_size);
return 1;
}
msi.pos = 0;
msi.size = target_size;
sink = MemorySink; sink = MemorySink;
token = &msi; token = &memory_sink_str;
} else { } else {
// We write the decoded output to "<tgt-file>.patch". // We write the decoded output to "<tgt-file>.patch".
outname = reinterpret_cast<char*>(malloc(strlen(target_filename) + 10)); output_fd = open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
strcpy(outname, target_filename); S_IRUSR | S_IWUSR);
strcat(outname, ".patch"); if (output_fd < 0) {
printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(),
output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR); strerror(errno));
if (output < 0) {
printf("failed to open output file %s: %s\n",
outname, strerror(errno));
return 1; return 1;
} }
sink = FileSink; sink = FileSink;
token = &output; token = &output_fd;
} }
char* header = patch->data;
ssize_t header_bytes_read = patch->size;
SHA1_Init(&ctx); SHA1_Init(&ctx);
int result; int result;
if (use_bsdiff) {
if (header_bytes_read >= 8 &&
memcmp(header, "BSDIFF40", 8) == 0) {
result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size, result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
patch, 0, sink, token, &ctx); patch, 0, sink, token, &ctx);
} else if (header_bytes_read >= 8 && } else {
memcmp(header, "IMGDIFF2", 8) == 0) {
result = ApplyImagePatch(source_to_use->data, source_to_use->size, result = ApplyImagePatch(source_to_use->data, source_to_use->size,
patch, sink, token, &ctx, bonus_data); patch, sink, token, &ctx, bonus_data);
} else {
printf("Unknown patch file format\n");
return 1;
} }
if (output >= 0) { if (!target_is_partition) {
if (fsync(output) != 0) { if (fsync(output_fd) != 0) {
printf("failed to fsync file \"%s\" (%s)\n", outname, strerror(errno)); printf("failed to fsync file \"%s\" (%s)\n", tmp_target_filename.c_str(),
strerror(errno));
result = 1; result = 1;
} }
if (close(output) != 0) { if (close(output_fd) != 0) {
printf("failed to close file \"%s\" (%s)\n", outname, strerror(errno)); printf("failed to close file \"%s\" (%s)\n", tmp_target_filename.c_str(),
strerror(errno));
result = 1; result = 1;
} }
} }
@ -990,8 +977,8 @@ static int GenerateTarget(FileContents* source_file,
} else { } else {
printf("applying patch failed; retrying\n"); printf("applying patch failed; retrying\n");
} }
if (outname != NULL) { if (!target_is_partition) {
unlink(outname); unlink(tmp_target_filename.c_str());
} }
} else { } else {
// succeeded; no need to retry // succeeded; no need to retry
@ -1008,27 +995,27 @@ static int GenerateTarget(FileContents* source_file,
printf("now %s\n", short_sha1(target_sha1).c_str()); printf("now %s\n", short_sha1(target_sha1).c_str());
} }
if (output < 0) { if (target_is_partition) {
// Copy the temp file to the partition. // Copy the temp file to the partition.
if (WriteToPartition(msi.buffer, msi.pos, target_filename) != 0) { if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
memory_sink_str.size(), target_filename) != 0) {
printf("write of patched data to %s failed\n", target_filename); printf("write of patched data to %s failed\n", target_filename);
return 1; return 1;
} }
free(msi.buffer);
} else { } else {
// Give the .patch file the same owner, group, and mode of the // Give the .patch file the same owner, group, and mode of the
// original source file. // original source file.
if (chmod(outname, source_to_use->st.st_mode) != 0) { if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) {
printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno)); printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
return 1; return 1;
} }
if (chown(outname, source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) { if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) {
printf("chown of \"%s\" failed: %s\n", outname, strerror(errno)); printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
return 1; return 1;
} }
// Finally, rename the .patch file to replace the target file. // Finally, rename the .patch file to replace the target file.
if (rename(outname, target_filename) != 0) { if (rename(tmp_target_filename.c_str(), target_filename) != 0) {
printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno)); printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno));
return 1; return 1;
} }

View file

@ -18,6 +18,9 @@
#define _APPLYPATCH_H #define _APPLYPATCH_H
#include <sys/stat.h> #include <sys/stat.h>
#include <vector>
#include "openssl/sha.h" #include "openssl/sha.h"
#include "edify/expr.h" #include "edify/expr.h"
@ -68,22 +71,22 @@ void FreeFileContents(FileContents* file);
int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str, int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
int num_patches); int num_patches);
// bsdiff.c // bsdiff.cpp
void ShowBSDiffLicense(); void ShowBSDiffLicense();
int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size, int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
const Value* patch, ssize_t patch_offset, const Value* patch, ssize_t patch_offset,
SinkFn sink, void* token, SHA_CTX* ctx); SinkFn sink, void* token, SHA_CTX* ctx);
int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size, int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
const Value* patch, ssize_t patch_offset, const Value* patch, ssize_t patch_offset,
unsigned char** new_data, ssize_t* new_size); std::vector<unsigned char>* new_data);
// imgpatch.c // imgpatch.cpp
int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
const Value* patch, const Value* patch,
SinkFn sink, void* token, SHA_CTX* ctx, SinkFn sink, void* token, SHA_CTX* ctx,
const Value* bonus_data); const Value* bonus_data);
// freecache.c // freecache.cpp
int MakeFreeSpaceOnCache(size_t bytes_needed); int MakeFreeSpaceOnCache(size_t bytes_needed);
#endif #endif

View file

@ -24,7 +24,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <malloc.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
@ -103,26 +102,22 @@ int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
const Value* patch, ssize_t patch_offset, const Value* patch, ssize_t patch_offset,
SinkFn sink, void* token, SHA_CTX* ctx) { SinkFn sink, void* token, SHA_CTX* ctx) {
unsigned char* new_data; std::vector<unsigned char> new_data;
ssize_t new_size; if (ApplyBSDiffPatchMem(old_data, old_size, patch, patch_offset, &new_data) != 0) {
if (ApplyBSDiffPatchMem(old_data, old_size, patch, patch_offset,
&new_data, &new_size) != 0) {
return -1; return -1;
} }
if (sink(new_data, new_size, token) < new_size) { if (sink(new_data.data(), new_data.size(), token) < static_cast<ssize_t>(new_data.size())) {
printf("short write of output: %d (%s)\n", errno, strerror(errno)); printf("short write of output: %d (%s)\n", errno, strerror(errno));
return 1; return 1;
} }
if (ctx) SHA1_Update(ctx, new_data, new_size); if (ctx) SHA1_Update(ctx, new_data.data(), new_data.size());
free(new_data);
return 0; return 0;
} }
int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size, int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
const Value* patch, ssize_t patch_offset, const Value* patch, ssize_t patch_offset,
unsigned char** new_data, ssize_t* new_size) { std::vector<unsigned char>* new_data) {
// Patch data format: // Patch data format:
// 0 8 "BSDIFF40" // 0 8 "BSDIFF40"
// 8 8 X // 8 8 X
@ -141,12 +136,12 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
return 1; return 1;
} }
ssize_t ctrl_len, data_len; ssize_t ctrl_len, data_len, new_size;
ctrl_len = offtin(header+8); ctrl_len = offtin(header+8);
data_len = offtin(header+16); data_len = offtin(header+16);
*new_size = offtin(header+24); new_size = offtin(header+24);
if (ctrl_len < 0 || data_len < 0 || *new_size < 0) { if (ctrl_len < 0 || data_len < 0 || new_size < 0) {
printf("corrupt patch file header (data lengths)\n"); printf("corrupt patch file header (data lengths)\n");
return 1; return 1;
} }
@ -183,18 +178,14 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
printf("failed to bzinit extra stream (%d)\n", bzerr); printf("failed to bzinit extra stream (%d)\n", bzerr);
} }
*new_data = reinterpret_cast<unsigned char*>(malloc(*new_size)); new_data->resize(new_size);
if (*new_data == NULL) {
printf("failed to allocate %zd bytes of memory for output file\n", *new_size);
return 1;
}
off_t oldpos = 0, newpos = 0; off_t oldpos = 0, newpos = 0;
off_t ctrl[3]; off_t ctrl[3];
off_t len_read; off_t len_read;
int i; int i;
unsigned char buf[24]; unsigned char buf[24];
while (newpos < *new_size) { while (newpos < new_size) {
// Read control data // Read control data
if (FillBuffer(buf, 24, &cstream) != 0) { if (FillBuffer(buf, 24, &cstream) != 0) {
printf("error while reading control stream\n"); printf("error while reading control stream\n");
@ -210,13 +201,13 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
} }
// Sanity check // Sanity check
if (newpos + ctrl[0] > *new_size) { if (newpos + ctrl[0] > new_size) {
printf("corrupt patch (new file overrun)\n"); printf("corrupt patch (new file overrun)\n");
return 1; return 1;
} }
// Read diff string // Read diff string
if (FillBuffer(*new_data + newpos, ctrl[0], &dstream) != 0) { if (FillBuffer(new_data->data() + newpos, ctrl[0], &dstream) != 0) {
printf("error while reading diff stream\n"); printf("error while reading diff stream\n");
return 1; return 1;
} }
@ -233,13 +224,13 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
oldpos += ctrl[0]; oldpos += ctrl[0];
// Sanity check // Sanity check
if (newpos + ctrl[1] > *new_size) { if (newpos + ctrl[1] > new_size) {
printf("corrupt patch (new file overrun)\n"); printf("corrupt patch (new file overrun)\n");
return 1; return 1;
} }
// Read extra string // Read extra string
if (FillBuffer(*new_data + newpos, ctrl[1], &estream) != 0) { if (FillBuffer(new_data->data() + newpos, ctrl[1], &estream) != 0) {
printf("error while reading extra stream\n"); printf("error while reading extra stream\n");
return 1; return 1;
} }

View file

@ -25,119 +25,90 @@
#include <dirent.h> #include <dirent.h>
#include <ctype.h> #include <ctype.h>
#include <memory>
#include <set>
#include <string>
#include <android-base/parseint.h>
#include <android-base/stringprintf.h>
#include "applypatch.h" #include "applypatch.h"
static int EliminateOpenFiles(char** files, int file_count) { static int EliminateOpenFiles(std::set<std::string>* files) {
DIR* d; std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir);
struct dirent* de; if (!d) {
d = opendir("/proc");
if (d == NULL) {
printf("error opening /proc: %s\n", strerror(errno)); printf("error opening /proc: %s\n", strerror(errno));
return -1; return -1;
} }
while ((de = readdir(d)) != 0) { struct dirent* de;
int i; while ((de = readdir(d.get())) != 0) {
for (i = 0; de->d_name[i] != '\0' && isdigit(de->d_name[i]); ++i); unsigned int pid;
if (de->d_name[i]) continue; if (!android::base::ParseUint(de->d_name, &pid)) {
continue;
}
std::string path = android::base::StringPrintf("/proc/%s/fd/", de->d_name);
// de->d_name[i] is numeric
char path[FILENAME_MAX];
strcpy(path, "/proc/");
strcat(path, de->d_name);
strcat(path, "/fd/");
DIR* fdd;
struct dirent* fdde; struct dirent* fdde;
fdd = opendir(path); std::unique_ptr<DIR, decltype(&closedir)> fdd(opendir(path.c_str()), closedir);
if (fdd == NULL) { if (!fdd) {
printf("error opening %s: %s\n", path, strerror(errno)); printf("error opening %s: %s\n", path.c_str(), strerror(errno));
continue; continue;
} }
while ((fdde = readdir(fdd)) != 0) { while ((fdde = readdir(fdd.get())) != 0) {
char fd_path[FILENAME_MAX]; std::string fd_path = path + fdde->d_name;
char link[FILENAME_MAX]; char link[FILENAME_MAX];
strcpy(fd_path, path);
strcat(fd_path, fdde->d_name);
int count; int count = readlink(fd_path.c_str(), link, sizeof(link)-1);
count = readlink(fd_path, link, sizeof(link)-1);
if (count >= 0) { if (count >= 0) {
link[count] = '\0'; link[count] = '\0';
// This is inefficient, but it should only matter if there are
// lots of files in /cache, and lots of them are open (neither
// of which should be true, especially in recovery).
if (strncmp(link, "/cache/", 7) == 0) { if (strncmp(link, "/cache/", 7) == 0) {
int j; if (files->erase(link) > 0) {
for (j = 0; j < file_count; ++j) { printf("%s is open by %s\n", link, de->d_name);
if (files[j] && strcmp(files[j], link) == 0) {
printf("%s is open by %s\n", link, de->d_name);
free(files[j]);
files[j] = NULL;
}
} }
} }
} }
} }
closedir(fdd);
} }
closedir(d);
return 0; return 0;
} }
int FindExpendableFiles(char*** names, int* entries) { static std::set<std::string> FindExpendableFiles() {
DIR* d; std::set<std::string> files;
struct dirent* de;
int size = 32;
*entries = 0;
*names = reinterpret_cast<char**>(malloc(size * sizeof(char*)));
char path[FILENAME_MAX];
// We're allowed to delete unopened regular files in any of these // We're allowed to delete unopened regular files in any of these
// directories. // directories.
const char* dirs[2] = {"/cache", "/cache/recovery/otatest"}; const char* dirs[2] = {"/cache", "/cache/recovery/otatest"};
for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) {
d = opendir(dirs[i]); std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirs[i]), closedir);
if (d == NULL) { if (!d) {
printf("error opening %s: %s\n", dirs[i], strerror(errno)); printf("error opening %s: %s\n", dirs[i], strerror(errno));
continue; continue;
} }
// Look for regular files in the directory (not in any subdirectories). // Look for regular files in the directory (not in any subdirectories).
while ((de = readdir(d)) != 0) { struct dirent* de;
strcpy(path, dirs[i]); while ((de = readdir(d.get())) != 0) {
strcat(path, "/"); std::string path = std::string(dirs[i]) + "/" + de->d_name;
strcat(path, de->d_name);
// We can't delete CACHE_TEMP_SOURCE; if it's there we might have // We can't delete CACHE_TEMP_SOURCE; if it's there we might have
// restarted during installation and could be depending on it to // restarted during installation and could be depending on it to
// be there. // be there.
if (strcmp(path, CACHE_TEMP_SOURCE) == 0) continue; if (path == CACHE_TEMP_SOURCE) {
continue;
}
struct stat st; struct stat st;
if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) { if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
if (*entries >= size) { files.insert(path);
size *= 2;
*names = reinterpret_cast<char**>(realloc(*names, size * sizeof(char*)));
}
(*names)[(*entries)++] = strdup(path);
} }
} }
closedir(d);
} }
printf("%d regular files in deletable directories\n", *entries); printf("%zu regular files in deletable directories\n", files.size());
if (EliminateOpenFiles(&files) < 0) {
if (EliminateOpenFiles(*names, *entries) < 0) { return std::set<std::string>();
return -1;
} }
return files;
return 0;
} }
int MakeFreeSpaceOnCache(size_t bytes_needed) { int MakeFreeSpaceOnCache(size_t bytes_needed) {
@ -147,15 +118,8 @@ int MakeFreeSpaceOnCache(size_t bytes_needed) {
if (free_now >= bytes_needed) { if (free_now >= bytes_needed) {
return 0; return 0;
} }
std::set<std::string> files = FindExpendableFiles();
char** names; if (files.empty()) {
int entries;
if (FindExpendableFiles(&names, &entries) < 0) {
return -1;
}
if (entries == 0) {
// nothing we can delete to free up space! // nothing we can delete to free up space!
printf("no files can be deleted to free space on /cache\n"); printf("no files can be deleted to free space on /cache\n");
return -1; return -1;
@ -167,20 +131,13 @@ int MakeFreeSpaceOnCache(size_t bytes_needed) {
// //
// Instead, we'll be dumb. // Instead, we'll be dumb.
int i; for (const auto& file : files) {
for (i = 0; i < entries && free_now < bytes_needed; ++i) { unlink(file.c_str());
if (names[i]) { free_now = FreeSpaceForFile("/cache");
unlink(names[i]); printf("deleted %s; now %zu bytes free\n", file.c_str(), free_now);
free_now = FreeSpaceForFile("/cache"); if (free_now < bytes_needed) {
printf("deleted %s; now %zu bytes free\n", names[i], free_now); break;
free(names[i]);
} }
} }
for (; i < entries; ++i) {
free(names[i]);
}
free(names);
return (free_now >= bytes_needed) ? 0 : -1; return (free_now >= bytes_needed) ? 0 : -1;
} }

View file

@ -21,10 +21,11 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <malloc.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <vector>
#include "zlib.h" #include "zlib.h"
#include "openssl/sha.h" #include "openssl/sha.h"
#include "applypatch.h" #include "applypatch.h"
@ -129,7 +130,6 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
size_t src_len = Read8(deflate_header+8); size_t src_len = Read8(deflate_header+8);
size_t patch_offset = Read8(deflate_header+16); size_t patch_offset = Read8(deflate_header+16);
size_t expanded_len = Read8(deflate_header+24); size_t expanded_len = Read8(deflate_header+24);
size_t target_len = Read8(deflate_header+32);
int level = Read4(deflate_header+40); int level = Read4(deflate_header+40);
int method = Read4(deflate_header+44); int method = Read4(deflate_header+44);
int windowBits = Read4(deflate_header+48); int windowBits = Read4(deflate_header+48);
@ -150,13 +150,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// must be appended from the bonus_data value. // must be appended from the bonus_data value.
size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0; size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0;
unsigned char* expanded_source = reinterpret_cast<unsigned char*>(malloc(expanded_len)); std::vector<unsigned char> expanded_source(expanded_len);
if (expanded_source == NULL) {
printf("failed to allocate %zu bytes for expanded_source\n",
expanded_len);
return -1;
}
z_stream strm; z_stream strm;
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
@ -164,7 +158,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
strm.avail_in = src_len; strm.avail_in = src_len;
strm.next_in = (unsigned char*)(old_data + src_start); strm.next_in = (unsigned char*)(old_data + src_start);
strm.avail_out = expanded_len; strm.avail_out = expanded_len;
strm.next_out = expanded_source; strm.next_out = expanded_source.data();
int ret; int ret;
ret = inflateInit2(&strm, -15); ret = inflateInit2(&strm, -15);
@ -189,18 +183,16 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
inflateEnd(&strm); inflateEnd(&strm);
if (bonus_size) { if (bonus_size) {
memcpy(expanded_source + (expanded_len - bonus_size), memcpy(expanded_source.data() + (expanded_len - bonus_size),
bonus_data->data, bonus_size); bonus_data->data, bonus_size);
} }
// Next, apply the bsdiff patch (in memory) to the uncompressed // Next, apply the bsdiff patch (in memory) to the uncompressed
// data. // data.
unsigned char* uncompressed_target_data; std::vector<unsigned char> uncompressed_target_data;
ssize_t uncompressed_target_size; if (ApplyBSDiffPatchMem(expanded_source.data(), expanded_len,
if (ApplyBSDiffPatchMem(expanded_source, expanded_len,
patch, patch_offset, patch, patch_offset,
&uncompressed_target_data, &uncompressed_target_data) != 0) {
&uncompressed_target_size) != 0) {
return -1; return -1;
} }
@ -208,40 +200,36 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// we're done with the expanded_source data buffer, so we'll // we're done with the expanded_source data buffer, so we'll
// reuse that memory to receive the output of deflate. // reuse that memory to receive the output of deflate.
unsigned char* temp_data = expanded_source; if (expanded_source.size() < 32768U) {
ssize_t temp_size = expanded_len; expanded_source.resize(32768U);
if (temp_size < 32768) {
// ... unless the buffer is too small, in which case we'll
// allocate a fresh one.
free(temp_data);
temp_data = reinterpret_cast<unsigned char*>(malloc(32768));
temp_size = 32768;
} }
std::vector<unsigned char>& temp_data = expanded_source;
// now the deflate stream // now the deflate stream
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
strm.opaque = Z_NULL; strm.opaque = Z_NULL;
strm.avail_in = uncompressed_target_size; strm.avail_in = uncompressed_target_data.size();
strm.next_in = uncompressed_target_data; strm.next_in = uncompressed_target_data.data();
ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy); ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
if (ret != Z_OK) {
printf("failed to init uncompressed data deflation: %d\n", ret);
return -1;
}
do { do {
strm.avail_out = temp_size; strm.avail_out = temp_data.size();
strm.next_out = temp_data; strm.next_out = temp_data.data();
ret = deflate(&strm, Z_FINISH); ret = deflate(&strm, Z_FINISH);
ssize_t have = temp_size - strm.avail_out; ssize_t have = temp_data.size() - strm.avail_out;
if (sink(temp_data, have, token) != have) { if (sink(temp_data.data(), have, token) != have) {
printf("failed to write %ld compressed bytes to output\n", printf("failed to write %ld compressed bytes to output\n",
(long)have); (long)have);
return -1; return -1;
} }
if (ctx) SHA1_Update(ctx, temp_data, have); if (ctx) SHA1_Update(ctx, temp_data.data(), have);
} while (ret != Z_STREAM_END); } while (ret != Z_STREAM_END);
deflateEnd(&strm); deflateEnd(&strm);
free(temp_data);
free(uncompressed_target_data);
} else { } else {
printf("patch chunk %d is unknown type %d\n", i, type); printf("patch chunk %d is unknown type %d\n", i, type);
return -1; return -1;

View file

@ -19,6 +19,9 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <memory>
#include <vector>
#include "applypatch.h" #include "applypatch.h"
#include "edify/expr.h" #include "edify/expr.h"
#include "openssl/sha.h" #include "openssl/sha.h"
@ -47,16 +50,11 @@ static int SpaceMode(int argc, char** argv) {
// "<sha1>:<filename>" into the new parallel arrays *sha1s and // "<sha1>:<filename>" into the new parallel arrays *sha1s and
// *patches (loading file contents into the patches). Returns true on // *patches (loading file contents into the patches). Returns true on
// success. // success.
static bool ParsePatchArgs(int argc, char** argv, char*** sha1s, static bool ParsePatchArgs(int argc, char** argv, std::vector<char*>* sha1s,
Value*** patches, int* num_patches) { std::vector<std::unique_ptr<Value, decltype(&FreeValue)>>* patches) {
*num_patches = argc;
*sha1s = reinterpret_cast<char**>(malloc(*num_patches * sizeof(char*)));
*patches = reinterpret_cast<Value**>(malloc(*num_patches * sizeof(Value*)));
memset(*patches, 0, *num_patches * sizeof(Value*));
uint8_t digest[SHA_DIGEST_LENGTH]; uint8_t digest[SHA_DIGEST_LENGTH];
for (int i = 0; i < *num_patches; ++i) { for (int i = 0; i < argc; ++i) {
char* colon = strchr(argv[i], ':'); char* colon = strchr(argv[i], ':');
if (colon != NULL) { if (colon != NULL) {
*colon = '\0'; *colon = '\0';
@ -68,34 +66,22 @@ static bool ParsePatchArgs(int argc, char** argv, char*** sha1s,
return false; return false;
} }
(*sha1s)[i] = argv[i]; sha1s->push_back(argv[i]);
if (colon == NULL) { if (colon == NULL) {
(*patches)[i] = NULL; patches->emplace_back(nullptr, FreeValue);
} else { } else {
FileContents fc; FileContents fc;
if (LoadFileContents(colon, &fc) != 0) { if (LoadFileContents(colon, &fc) != 0) {
goto abort; return false;
} }
(*patches)[i] = reinterpret_cast<Value*>(malloc(sizeof(Value))); std::unique_ptr<Value, decltype(&FreeValue)> value(new Value, FreeValue);
(*patches)[i]->type = VAL_BLOB; value->type = VAL_BLOB;
(*patches)[i]->size = fc.size; value->size = fc.size;
(*patches)[i]->data = reinterpret_cast<char*>(fc.data); value->data = reinterpret_cast<char*>(fc.data);
patches->push_back(std::move(value));
} }
} }
return true; return true;
abort:
for (int i = 0; i < *num_patches; ++i) {
Value* p = (*patches)[i];
if (p != NULL) {
free(p->data);
free(p);
}
}
free(*sha1s);
free(*patches);
return false;
} }
static int FlashMode(const char* src_filename, const char* tgt_filename, static int FlashMode(const char* src_filename, const char* tgt_filename,
@ -104,17 +90,17 @@ static int FlashMode(const char* src_filename, const char* tgt_filename,
} }
static int PatchMode(int argc, char** argv) { static int PatchMode(int argc, char** argv) {
Value* bonus = NULL; std::unique_ptr<Value, decltype(&FreeValue)> bonus(nullptr, FreeValue);
if (argc >= 3 && strcmp(argv[1], "-b") == 0) { if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
FileContents fc; FileContents fc;
if (LoadFileContents(argv[2], &fc) != 0) { if (LoadFileContents(argv[2], &fc) != 0) {
printf("failed to load bonus file %s\n", argv[2]); printf("failed to load bonus file %s\n", argv[2]);
return 1; return 1;
} }
bonus = reinterpret_cast<Value*>(malloc(sizeof(Value))); bonus.reset(new Value);
bonus->type = VAL_BLOB; bonus->type = VAL_BLOB;
bonus->size = fc.size; bonus->size = fc.size;
bonus->data = (char*)fc.data; bonus->data = reinterpret_cast<char*>(fc.data);
argc -= 2; argc -= 2;
argv += 2; argv += 2;
} }
@ -140,33 +126,20 @@ static int PatchMode(int argc, char** argv) {
} }
char** sha1s; std::vector<char*> sha1s;
Value** patches; std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
int num_patches; if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches)) {
if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches)) {
printf("failed to parse patch args\n"); printf("failed to parse patch args\n");
return 1; return 1;
} }
int result = applypatch(argv[1], argv[2], argv[3], target_size, std::vector<Value*> patch_ptrs;
num_patches, sha1s, patches, bonus); for (const auto& p : patches) {
patch_ptrs.push_back(p.get());
int i;
for (i = 0; i < num_patches; ++i) {
Value* p = patches[i];
if (p != NULL) {
free(p->data);
free(p);
}
} }
if (bonus) { return applypatch(argv[1], argv[2], argv[3], target_size,
free(bonus->data); patch_ptrs.size(), sha1s.data(),
free(bonus); patch_ptrs.data(), bonus.get());
}
free(sha1s);
free(patches);
return result;
} }
// This program applies binary patches to files in a way that is safe // This program applies binary patches to files in a way that is safe