summaryrefslogtreecommitdiffstats
path: root/applypatch
diff options
context:
space:
mode:
Diffstat (limited to 'applypatch')
-rw-r--r--applypatch/Android.mk4
-rw-r--r--applypatch/applypatch.c97
-rw-r--r--applypatch/applypatch.h6
-rw-r--r--applypatch/bspatch.c9
-rw-r--r--applypatch/imgpatch.c11
-rw-r--r--applypatch/main.c4
6 files changed, 71 insertions, 60 deletions
diff --git a/applypatch/Android.mk b/applypatch/Android.mk
index ef57f243c..4984093dd 100644
--- a/applypatch/Android.mk
+++ b/applypatch/Android.mk
@@ -28,7 +28,7 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.c
LOCAL_MODULE := applypatch
LOCAL_C_INCLUDES += bootable/recovery
-LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz libminelf
+LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz
LOCAL_SHARED_LIBRARIES += libz libcutils libstdc++ libc
include $(BUILD_EXECUTABLE)
@@ -40,7 +40,7 @@ LOCAL_MODULE := applypatch_static
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_TAGS := eng
LOCAL_C_INCLUDES += bootable/recovery
-LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz libminelf
+LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz
LOCAL_STATIC_LIBRARIES += libz libcutils libstdc++ libc
include $(BUILD_EXECUTABLE)
diff --git a/applypatch/applypatch.c b/applypatch/applypatch.c
index cb9bc2349..2c86e0984 100644
--- a/applypatch/applypatch.c
+++ b/applypatch/applypatch.c
@@ -24,6 +24,7 @@
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
+#include <stdbool.h>
#include "mincrypt/sha.h"
#include "applypatch.h"
@@ -31,7 +32,7 @@
#include "edify/expr.h"
static int LoadPartitionContents(const char* filename, FileContents* file);
-static ssize_t FileSink(unsigned char* data, ssize_t len, void* token);
+static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
static int GenerateTarget(FileContents* source_file,
const Value* source_patch_value,
FileContents* copy_file,
@@ -44,14 +45,11 @@ static int GenerateTarget(FileContents* source_file,
static int mtd_partitions_scanned = 0;
-// Read a file into memory; optionally (retouch_flag == RETOUCH_DO_MASK) mask
-// the retouched entries back to their original value (such that SHA-1 checks
-// don't fail due to randomization); store the file contents and associated
+// Read a file into memory; store the file contents and associated
// metadata in *file.
//
// Return 0 on success.
-int LoadFileContents(const char* filename, FileContents* file,
- int retouch_flag) {
+int LoadFileContents(const char* filename, FileContents* file) {
file->data = NULL;
// A special 'filename' beginning with "MTD:" or "EMMC:" means to
@@ -87,20 +85,6 @@ int LoadFileContents(const char* filename, FileContents* file,
}
fclose(f);
- // apply_patch[_check] functions are blind to randomization. Randomization
- // is taken care of in [Undo]RetouchBinariesFn. If there is a mismatch
- // within a file, this means the file is assumed "corrupt" for simplicity.
- if (retouch_flag) {
- int32_t desired_offset = 0;
- if (retouch_mask_data(file->data, file->size,
- &desired_offset, NULL) != RETOUCH_DATA_MATCHED) {
- printf("error trying to mask retouch entries\n");
- free(file->data);
- file->data = NULL;
- return -1;
- }
- }
-
SHA_hash(file->data, file->size, file->sha1);
return 0;
}
@@ -247,7 +231,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
break;
}
if (next != read) {
- printf("short read (%d bytes of %d) for partition \"%s\"\n",
+ printf("short read (%zu bytes of %zu) for partition \"%s\"\n",
read, next, partition);
free(file->data);
file->data = NULL;
@@ -274,7 +258,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) {
// we have a match. stop reading the partition; we'll return
// the data we've read so far.
- printf("partition read matched size %d sha %s\n",
+ printf("partition read matched size %zu sha %s\n",
size[index[i]], sha1sum[index[i]]);
break;
}
@@ -325,7 +309,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) {
// Save the contents of the given FileContents object under the given
// filename. Return 0 on success.
int SaveFileContents(const char* filename, const FileContents* file) {
- int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
+ int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
if (fd < 0) {
printf("failed to open \"%s\" for write: %s\n",
filename, strerror(errno));
@@ -340,8 +324,14 @@ int SaveFileContents(const char* filename, const FileContents* file) {
close(fd);
return -1;
}
- fsync(fd);
- close(fd);
+ if (fsync(fd) != 0) {
+ printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
+ return -1;
+ }
+ if (close(fd) != 0) {
+ printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
+ return -1;
+ }
if (chmod(filename, file->st.st_mode) != 0) {
printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
@@ -402,7 +392,7 @@ int WriteToPartition(unsigned char* data, size_t len,
size_t written = mtd_write_data(ctx, (char*)data, len);
if (written != len) {
- printf("only wrote %d of %d bytes to MTD %s\n",
+ printf("only wrote %zu of %zu bytes to MTD %s\n",
written, len, partition);
mtd_write_close(ctx);
return -1;
@@ -424,7 +414,7 @@ int WriteToPartition(unsigned char* data, size_t len,
{
size_t start = 0;
int success = 0;
- int fd = open(partition, O_RDWR);
+ int fd = open(partition, O_RDWR | O_SYNC);
if (fd < 0) {
printf("failed to open %s: %s\n", partition, strerror(errno));
return -1;
@@ -449,7 +439,22 @@ int WriteToPartition(unsigned char* data, size_t len,
}
start += written;
}
- fsync(fd);
+ if (fsync(fd) != 0) {
+ printf("failed to sync to %s (%s)\n",
+ partition, strerror(errno));
+ return -1;
+ }
+ if (close(fd) != 0) {
+ printf("failed to close %s (%s)\n",
+ partition, strerror(errno));
+ return -1;
+ }
+ fd = open(partition, O_RDONLY);
+ if (fd < 0) {
+ printf("failed to reopen %s for verify (%s)\n",
+ partition, strerror(errno));
+ return -1;
+ }
// drop caches so our subsequent verification read
// won't just be reading the cache.
@@ -476,20 +481,20 @@ int WriteToPartition(unsigned char* data, size_t len,
if (errno == EINTR) {
read_count = 0;
} else {
- printf("verify read error %s at %d: %s\n",
+ printf("verify read error %s at %zu: %s\n",
partition, p, strerror(errno));
return -1;
}
}
if ((size_t)read_count < to_read) {
- printf("short verify read %s at %d: %d %d %s\n",
+ printf("short verify read %s at %zu: %zd %zu %s\n",
partition, p, read_count, to_read, strerror(errno));
}
so_far += read_count;
}
if (memcmp(buffer, data+p, to_read)) {
- printf("verification failed starting at %d\n", p);
+ printf("verification failed starting at %zu\n", p);
start = p;
break;
}
@@ -579,7 +584,7 @@ int applypatch_check(const char* filename,
// LoadFileContents is successful. (Useful for reading
// partitions, where the filename encodes the sha1s; no need to
// check them twice.)
- if (LoadFileContents(filename, &file, RETOUCH_DO_MASK) != 0 ||
+ if (LoadFileContents(filename, &file) != 0 ||
(num_patches > 0 &&
FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) {
printf("file \"%s\" doesn't have any of expected "
@@ -594,7 +599,7 @@ int applypatch_check(const char* filename,
// exists and matches the sha1 we're looking for, the check still
// passes.
- if (LoadFileContents(CACHE_TEMP_SOURCE, &file, RETOUCH_DO_MASK) != 0) {
+ if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
printf("failed to load cache file\n");
return 1;
}
@@ -615,7 +620,7 @@ int ShowLicenses() {
return 0;
}
-ssize_t FileSink(unsigned char* data, ssize_t len, void* token) {
+ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
int fd = *(int *)token;
ssize_t done = 0;
ssize_t wrote;
@@ -636,7 +641,7 @@ typedef struct {
ssize_t pos;
} MemorySinkInfo;
-ssize_t MemorySink(unsigned char* data, ssize_t len, void* token) {
+ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
MemorySinkInfo* msi = (MemorySinkInfo*)token;
if (msi->size - msi->pos < len) {
return -1;
@@ -730,8 +735,7 @@ int applypatch(const char* source_filename,
const Value* copy_patch_value = NULL;
// We try to load the target file into the source_file object.
- if (LoadFileContents(target_filename, &source_file,
- RETOUCH_DO_MASK) == 0) {
+ if (LoadFileContents(target_filename, &source_file) == 0) {
if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
// The early-exit case: the patch was already applied, this file
// has the desired hash, nothing for us to do.
@@ -750,8 +754,7 @@ int applypatch(const char* source_filename,
// target file, or we did but it's different from the source file.
free(source_file.data);
source_file.data = NULL;
- LoadFileContents(source_filename, &source_file,
- RETOUCH_DO_MASK);
+ LoadFileContents(source_filename, &source_file);
}
if (source_file.data != NULL) {
@@ -767,8 +770,7 @@ int applypatch(const char* source_filename,
source_file.data = NULL;
printf("source file is bad; trying copy\n");
- if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file,
- RETOUCH_DO_MASK) < 0) {
+ if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
// fail.
printf("failed to read copy file\n");
return 1;
@@ -938,7 +940,8 @@ static int GenerateTarget(FileContents* source_file,
strcpy(outname, target_filename);
strcat(outname, ".patch");
- output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
+ output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
+ S_IRUSR | S_IWUSR);
if (output < 0) {
printf("failed to open output file %s: %s\n",
outname, strerror(errno));
@@ -969,8 +972,14 @@ static int GenerateTarget(FileContents* source_file,
}
if (output >= 0) {
- fsync(output);
- close(output);
+ if (fsync(output) != 0) {
+ printf("failed to fsync file \"%s\" (%s)\n", outname, strerror(errno));
+ result = 1;
+ }
+ if (close(output) != 0) {
+ printf("failed to close file \"%s\" (%s)\n", outname, strerror(errno));
+ result = 1;
+ }
}
if (result != 0) {
diff --git a/applypatch/applypatch.h b/applypatch/applypatch.h
index f1f13a100..edec84812 100644
--- a/applypatch/applypatch.h
+++ b/applypatch/applypatch.h
@@ -19,7 +19,6 @@
#include <sys/stat.h>
#include "mincrypt/sha.h"
-#include "minelf/Retouch.h"
#include "edify/expr.h"
typedef struct _Patch {
@@ -41,7 +40,7 @@ typedef struct _FileContents {
// and use it as the source instead.
#define CACHE_TEMP_SOURCE "/cache/saved.file"
-typedef ssize_t (*SinkFn)(unsigned char*, ssize_t, void*);
+typedef ssize_t (*SinkFn)(const unsigned char*, ssize_t, void*);
// applypatch.c
int ShowLicenses();
@@ -61,8 +60,7 @@ int applypatch_check(const char* filename,
int num_patches,
char** const patch_sha1_str);
-int LoadFileContents(const char* filename, FileContents* file,
- int retouch_flag);
+int LoadFileContents(const char* filename, FileContents* file);
int SaveFileContents(const char* filename, const FileContents* file);
void FreeFileContents(FileContents* file);
int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
diff --git a/applypatch/bspatch.c b/applypatch/bspatch.c
index 2e80f81d0..b34ec2a88 100644
--- a/applypatch/bspatch.c
+++ b/applypatch/bspatch.c
@@ -112,9 +112,7 @@ int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
printf("short write of output: %d (%s)\n", errno, strerror(errno));
return 1;
}
- if (ctx) {
- SHA_update(ctx, new_data, new_size);
- }
+ if (ctx) SHA_update(ctx, new_data, new_size);
free(new_data);
return 0;
@@ -205,6 +203,11 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
ctrl[1] = offtin(buf+8);
ctrl[2] = offtin(buf+16);
+ if (ctrl[0] < 0 || ctrl[1] < 0) {
+ printf("corrupt patch (negative byte counts)\n");
+ return 1;
+ }
+
// Sanity check
if (newpos + ctrl[0] > *new_size) {
printf("corrupt patch (new file overrun)\n");
diff --git a/applypatch/imgpatch.c b/applypatch/imgpatch.c
index 3a1df3872..33c448762 100644
--- a/applypatch/imgpatch.c
+++ b/applypatch/imgpatch.c
@@ -18,6 +18,7 @@
// format.
#include <stdio.h>
+#include <sys/cdefs.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
@@ -35,7 +36,7 @@
* file, and update the SHA context with the output data as well.
* Return 0 on success.
*/
-int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
+int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size __unused,
const Value* patch,
SinkFn sink, void* token, SHA_CTX* ctx,
const Value* bonus_data) {
@@ -94,7 +95,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
printf("failed to read chunk %d raw data\n", i);
return -1;
}
- SHA_update(ctx, patch->data + pos, data_len);
+ if (ctx) SHA_update(ctx, patch->data + pos, data_len);
if (sink((unsigned char*)patch->data + pos,
data_len, token) != data_len) {
printf("failed to write chunk %d raw data\n", i);
@@ -132,7 +133,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
unsigned char* expanded_source = malloc(expanded_len);
if (expanded_source == NULL) {
- printf("failed to allocate %d bytes for expanded_source\n",
+ printf("failed to allocate %zu bytes for expanded_source\n",
expanded_len);
return -1;
}
@@ -163,7 +164,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// We should have filled the output buffer exactly, except
// for the bonus_size.
if (strm.avail_out != bonus_size) {
- printf("source inflation short by %d bytes\n", strm.avail_out-bonus_size);
+ printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size);
return -1;
}
inflateEnd(&strm);
@@ -216,7 +217,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
(long)have);
return -1;
}
- SHA_update(ctx, temp_data, have);
+ if (ctx) SHA_update(ctx, temp_data, have);
} while (ret != Z_STREAM_END);
deflateEnd(&strm);
diff --git a/applypatch/main.c b/applypatch/main.c
index f61db5d9e..8e9fe80ef 100644
--- a/applypatch/main.c
+++ b/applypatch/main.c
@@ -74,7 +74,7 @@ static int ParsePatchArgs(int argc, char** argv,
(*patches)[i] = NULL;
} else {
FileContents fc;
- if (LoadFileContents(colon, &fc, RETOUCH_DONT_MASK) != 0) {
+ if (LoadFileContents(colon, &fc) != 0) {
goto abort;
}
(*patches)[i] = malloc(sizeof(Value));
@@ -103,7 +103,7 @@ int PatchMode(int argc, char** argv) {
Value* bonus = NULL;
if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
FileContents fc;
- if (LoadFileContents(argv[2], &fc, RETOUCH_DONT_MASK) != 0) {
+ if (LoadFileContents(argv[2], &fc) != 0) {
printf("failed to load bonus file %s\n", argv[2]);
return 1;
}