Skip to content

Commit

Permalink
Switch to android::base::ReadFully
Browse files Browse the repository at this point in the history
The if (read(...size) != size) pattern is unreliable, switch
to the android base ReadFully which wraps read in a loop.

Change-Id: I2324e4c45da3c9b53b18df6eb09ce69a6604b5d1
  • Loading branch information
jredestig authored and enh-google committed Oct 13, 2015
1 parent 73bf853 commit 67b3cad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions fs_mgr/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/include \
external/openssl/include

LOCAL_MODULE:= libfs_mgr
LOCAL_STATIC_LIBRARIES := liblogwrap libmincrypt libext4_utils_static libsquashfs_utils
LOCAL_STATIC_LIBRARIES := liblogwrap libmincrypt libext4_utils_static libsquashfs_utils libbase
LOCAL_C_INCLUDES += system/extras/ext4_utils system/extras/squashfs_utils \
bootable/recovery
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
Expand All @@ -39,7 +39,7 @@ LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)/sbin
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)

LOCAL_STATIC_LIBRARIES := libfs_mgr liblogwrap libcutils liblog libc libmincrypt libext4_utils_static libsquashfs_utils
LOCAL_STATIC_LIBRARIES := libfs_mgr liblogwrap libcutils liblog libc libmincrypt libext4_utils_static libsquashfs_utils libbase
LOCAL_STATIC_LIBRARIES += libsparse_static libz libselinux
LOCAL_CXX_STL := libc++_static

Expand Down
21 changes: 9 additions & 12 deletions fs_mgr/fs_mgr_verity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <libgen.h>
#include <time.h>

#include <base/file.h>
#include <private/android_filesystem_config.h>
#include <cutils/properties.h>
#include <logwrap/logwrap.h>
Expand Down Expand Up @@ -200,7 +201,7 @@ static int ext4_get_target_device_size(char *blk_device, uint64_t *device_size)
return -1;
}

if (TEMP_FAILURE_RETRY(read(data_device, &sb, sizeof(sb))) != sizeof(sb)) {
if (!android::base::ReadFully(data_device, &sb, sizeof(sb))) {
ERROR("Error reading superblock");
close(data_device);
return -1;
Expand Down Expand Up @@ -256,10 +257,8 @@ static int read_verity_metadata(uint64_t device_size, char *block_device, char *
ERROR("Could not seek to start of verity metadata block.\n");
goto out;
}

// check the magic number
if (TEMP_FAILURE_RETRY(read(device, &magic_number, sizeof(magic_number))) !=
sizeof(magic_number)) {
if (!android::base::ReadFully(device, &magic_number, sizeof(magic_number))) {
ERROR("Couldn't read magic number!\n");
goto out;
}
Expand All @@ -278,8 +277,8 @@ static int read_verity_metadata(uint64_t device_size, char *block_device, char *
}

// check the protocol version
if (TEMP_FAILURE_RETRY(read(device, &protocol_version,
sizeof(protocol_version))) != sizeof(protocol_version)) {
if (!android::base::ReadFully(device, &protocol_version,
sizeof(protocol_version))) {
ERROR("Couldn't read verity metadata protocol version!\n");
goto out;
}
Expand All @@ -294,7 +293,7 @@ static int read_verity_metadata(uint64_t device_size, char *block_device, char *
ERROR("Couldn't allocate memory for signature!\n");
goto out;
}
if (TEMP_FAILURE_RETRY(read(device, *signature, RSANUMBYTES)) != RSANUMBYTES) {
if (!android::base::ReadFully(device, *signature, RSANUMBYTES)) {
ERROR("Couldn't read signature from verity metadata!\n");
goto out;
}
Expand All @@ -305,8 +304,7 @@ static int read_verity_metadata(uint64_t device_size, char *block_device, char *
}

// get the size of the table
if (TEMP_FAILURE_RETRY(read(device, &table_length, sizeof(table_length))) !=
sizeof(table_length)) {
if (!android::base::ReadFully(device, &table_length, sizeof(table_length))) {
ERROR("Couldn't get the size of the verity table from metadata!\n");
goto out;
}
Expand All @@ -317,8 +315,7 @@ static int read_verity_metadata(uint64_t device_size, char *block_device, char *
ERROR("Couldn't allocate memory for verity table!\n");
goto out;
}
if (TEMP_FAILURE_RETRY(read(device, *table, table_length)) !=
(ssize_t)table_length) {
if (!android::base::ReadFully(device, *table, table_length)) {
ERROR("Couldn't read the verity table from metadata!\n");
goto out;
}
Expand Down Expand Up @@ -483,7 +480,7 @@ static int check_verity_restart(const char *fname)
goto out;
}

if (TEMP_FAILURE_RETRY(read(fd, buffer, size)) != size) {
if (!android::base::ReadFully(fd, buffer, size)) {
ERROR("Failed to read %zd bytes from %s (%s)\n", size, fname,
strerror(errno));
goto out;
Expand Down

0 comments on commit 67b3cad

Please sign in to comment.