fuse_sideload: Change the minimal block size to 4096.
run_fuse_sideload() is passing the block size as the max_read option, so it will only handle a request that involves at most two blocks at a time. However, the minimal allowed value was set to 1024 prior to this CL, which is inconsistent with the kernel code (fs/fuse/inode.c) that sets it to the greater of 4096 and the passed-in max_read option. This would fail the calls with a block size / max_read less than 4096 due to the wrongly computed block indices. Note that we didn't observe real issue in practice, because we have been using 64 KiB block sizes for both of adb and sdcard sideload calls. The issue only shows up in my local CL (to come later) that uses 1024 block size in run_fuse_sideload() tests. Test: recovery_component_test Test: adb sideload with the new recovery image on angler Change-Id: Id9f0cfea13d0d193dcb7cd41a1553a23739545f2
This commit is contained in:
parent
ba365180d3
commit
ed13819a0d
4 changed files with 166 additions and 146 deletions
|
@ -25,7 +25,9 @@ LOCAL_SRC_FILES := fuse_sideload.cpp
|
||||||
LOCAL_CFLAGS := -Wall -Werror
|
LOCAL_CFLAGS := -Wall -Werror
|
||||||
LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
|
LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
|
||||||
LOCAL_MODULE := libfusesideload
|
LOCAL_MODULE := libfusesideload
|
||||||
LOCAL_STATIC_LIBRARIES := libcrypto
|
LOCAL_STATIC_LIBRARIES := \
|
||||||
|
libcrypto \
|
||||||
|
libbase
|
||||||
include $(BUILD_STATIC_LIBRARY)
|
include $(BUILD_STATIC_LIBRARY)
|
||||||
|
|
||||||
# libmounts (static library)
|
# libmounts (static library)
|
||||||
|
|
|
@ -61,6 +61,9 @@
|
||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <android-base/stringprintf.h>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
#include "fuse_sideload.h"
|
#include "fuse_sideload.h"
|
||||||
|
@ -364,164 +367,163 @@ static int handle_read(void* data, struct fuse_data* fd, const struct fuse_in_he
|
||||||
return NO_STATUS;
|
return NO_STATUS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int run_fuse_sideload(struct provider_vtab* vtab, void* cookie,
|
int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_size,
|
||||||
uint64_t file_size, uint32_t block_size)
|
uint32_t block_size) {
|
||||||
{
|
// If something's already mounted on our mountpoint, try to remove it. (Mostly in case of a
|
||||||
int result;
|
// previous abnormal exit.)
|
||||||
|
umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_FORCE);
|
||||||
|
|
||||||
// If something's already mounted on our mountpoint, try to remove
|
// fs/fuse/inode.c in kernel code uses the greater of 4096 and the passed-in max_read.
|
||||||
// it. (Mostly in case of a previous abnormal exit.)
|
if (block_size < 4096) {
|
||||||
umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_FORCE);
|
fprintf(stderr, "block size (%u) is too small\n", block_size);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (block_size > (1 << 22)) { // 4 MiB
|
||||||
|
fprintf(stderr, "block size (%u) is too large\n", block_size);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (block_size < 1024) {
|
struct fuse_data fd = {};
|
||||||
fprintf(stderr, "block size (%u) is too small\n", block_size);
|
fd.vtab = vtab;
|
||||||
return -1;
|
fd.cookie = cookie;
|
||||||
}
|
fd.file_size = file_size;
|
||||||
if (block_size > (1<<22)) { // 4 MiB
|
fd.block_size = block_size;
|
||||||
fprintf(stderr, "block size (%u) is too large\n", block_size);
|
fd.file_blocks = (file_size == 0) ? 0 : (((file_size - 1) / block_size) + 1);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fuse_data fd;
|
int result;
|
||||||
memset(&fd, 0, sizeof(fd));
|
if (fd.file_blocks > (1 << 18)) {
|
||||||
fd.vtab = vtab;
|
fprintf(stderr, "file has too many blocks (%u)\n", fd.file_blocks);
|
||||||
fd.cookie = cookie;
|
result = -1;
|
||||||
fd.file_size = file_size;
|
goto done;
|
||||||
fd.block_size = block_size;
|
}
|
||||||
fd.file_blocks = (file_size == 0) ? 0 : (((file_size-1) / block_size) + 1);
|
|
||||||
|
|
||||||
if (fd.file_blocks > (1<<18)) {
|
fd.hashes = (uint8_t*)calloc(fd.file_blocks, SHA256_DIGEST_LENGTH);
|
||||||
fprintf(stderr, "file has too many blocks (%u)\n", fd.file_blocks);
|
if (fd.hashes == NULL) {
|
||||||
result = -1;
|
fprintf(stderr, "failed to allocate %d bites for hashes\n",
|
||||||
goto done;
|
fd.file_blocks * SHA256_DIGEST_LENGTH);
|
||||||
}
|
result = -1;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
fd.hashes = (uint8_t*)calloc(fd.file_blocks, SHA256_DIGEST_LENGTH);
|
fd.uid = getuid();
|
||||||
if (fd.hashes == NULL) {
|
fd.gid = getgid();
|
||||||
fprintf(stderr, "failed to allocate %d bites for hashes\n",
|
|
||||||
fd.file_blocks * SHA256_DIGEST_LENGTH);
|
|
||||||
result = -1;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
fd.uid = getuid();
|
fd.curr_block = -1;
|
||||||
fd.gid = getgid();
|
fd.block_data = (uint8_t*)malloc(block_size);
|
||||||
|
if (fd.block_data == NULL) {
|
||||||
|
fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size);
|
||||||
|
result = -1;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
fd.extra_block = (uint8_t*)malloc(block_size);
|
||||||
|
if (fd.extra_block == NULL) {
|
||||||
|
fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size);
|
||||||
|
result = -1;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
fd.curr_block = -1;
|
fd.ffd = open("/dev/fuse", O_RDWR);
|
||||||
fd.block_data = (uint8_t*)malloc(block_size);
|
if (fd.ffd < 0) {
|
||||||
if (fd.block_data == NULL) {
|
perror("open /dev/fuse");
|
||||||
fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size);
|
result = -1;
|
||||||
result = -1;
|
goto done;
|
||||||
goto done;
|
}
|
||||||
}
|
|
||||||
fd.extra_block = (uint8_t*)malloc(block_size);
|
|
||||||
if (fd.extra_block == NULL) {
|
|
||||||
fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size);
|
|
||||||
result = -1;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
fd.ffd = open("/dev/fuse", O_RDWR);
|
{
|
||||||
if (fd.ffd < 0) {
|
std::string opts = android::base::StringPrintf(
|
||||||
perror("open /dev/fuse");
|
"fd=%d,user_id=%d,group_id=%d,max_read=%u,allow_other,rootmode=040000", fd.ffd, fd.uid,
|
||||||
result = -1;
|
fd.gid, block_size);
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
char opts[256];
|
result = mount("/dev/fuse", FUSE_SIDELOAD_HOST_MOUNTPOINT, "fuse",
|
||||||
snprintf(opts, sizeof(opts),
|
MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC, opts.c_str());
|
||||||
("fd=%d,user_id=%d,group_id=%d,max_read=%u,"
|
|
||||||
"allow_other,rootmode=040000"),
|
|
||||||
fd.ffd, fd.uid, fd.gid, block_size);
|
|
||||||
|
|
||||||
result = mount("/dev/fuse", FUSE_SIDELOAD_HOST_MOUNTPOINT,
|
|
||||||
"fuse", MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC, opts);
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
perror("mount");
|
perror("mount");
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX*8];
|
}
|
||||||
for (;;) {
|
|
||||||
ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
|
|
||||||
if (len == -1) {
|
|
||||||
perror("read request");
|
|
||||||
if (errno == ENODEV) {
|
|
||||||
result = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((size_t)len < sizeof(struct fuse_in_header)) {
|
uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX * 8];
|
||||||
fprintf(stderr, "request too short: len=%zu\n", (size_t)len);
|
for (;;) {
|
||||||
continue;
|
ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
|
||||||
}
|
if (len == -1) {
|
||||||
|
perror("read request");
|
||||||
struct fuse_in_header* hdr = (struct fuse_in_header*) request_buffer;
|
if (errno == ENODEV) {
|
||||||
void* data = request_buffer + sizeof(struct fuse_in_header);
|
result = -1;
|
||||||
|
break;
|
||||||
result = -ENOSYS;
|
}
|
||||||
|
continue;
|
||||||
switch (hdr->opcode) {
|
|
||||||
case FUSE_INIT:
|
|
||||||
result = handle_init(data, &fd, hdr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FUSE_LOOKUP:
|
|
||||||
result = handle_lookup(data, &fd, hdr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FUSE_GETATTR:
|
|
||||||
result = handle_getattr(data, &fd, hdr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FUSE_OPEN:
|
|
||||||
result = handle_open(data, &fd, hdr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FUSE_READ:
|
|
||||||
result = handle_read(data, &fd, hdr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FUSE_FLUSH:
|
|
||||||
result = handle_flush(data, &fd, hdr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FUSE_RELEASE:
|
|
||||||
result = handle_release(data, &fd, hdr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
fprintf(stderr, "unknown fuse request opcode %d\n", hdr->opcode);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result == NO_STATUS_EXIT) {
|
|
||||||
result = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result != NO_STATUS) {
|
|
||||||
struct fuse_out_header outhdr;
|
|
||||||
outhdr.len = sizeof(outhdr);
|
|
||||||
outhdr.error = result;
|
|
||||||
outhdr.unique = hdr->unique;
|
|
||||||
TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
if (static_cast<size_t>(len) < sizeof(struct fuse_in_header)) {
|
||||||
fd.vtab->close(fd.cookie);
|
fprintf(stderr, "request too short: len=%zd\n", len);
|
||||||
|
continue;
|
||||||
result = umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_DETACH);
|
|
||||||
if (result < 0) {
|
|
||||||
printf("fuse_sideload umount failed: %s\n", strerror(errno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd.ffd) close(fd.ffd);
|
struct fuse_in_header* hdr = reinterpret_cast<struct fuse_in_header*>(request_buffer);
|
||||||
free(fd.hashes);
|
void* data = request_buffer + sizeof(struct fuse_in_header);
|
||||||
free(fd.block_data);
|
|
||||||
free(fd.extra_block);
|
|
||||||
|
|
||||||
return result;
|
result = -ENOSYS;
|
||||||
|
|
||||||
|
switch (hdr->opcode) {
|
||||||
|
case FUSE_INIT:
|
||||||
|
result = handle_init(data, &fd, hdr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FUSE_LOOKUP:
|
||||||
|
result = handle_lookup(data, &fd, hdr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FUSE_GETATTR:
|
||||||
|
result = handle_getattr(data, &fd, hdr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FUSE_OPEN:
|
||||||
|
result = handle_open(data, &fd, hdr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FUSE_READ:
|
||||||
|
result = handle_read(data, &fd, hdr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FUSE_FLUSH:
|
||||||
|
result = handle_flush(data, &fd, hdr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FUSE_RELEASE:
|
||||||
|
result = handle_release(data, &fd, hdr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "unknown fuse request opcode %d\n", hdr->opcode);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NO_STATUS_EXIT) {
|
||||||
|
result = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result != NO_STATUS) {
|
||||||
|
struct fuse_out_header outhdr;
|
||||||
|
outhdr.len = sizeof(outhdr);
|
||||||
|
outhdr.error = result;
|
||||||
|
outhdr.unique = hdr->unique;
|
||||||
|
TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
fd.vtab->close(fd.cookie);
|
||||||
|
|
||||||
|
result = umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_DETACH);
|
||||||
|
if (result < 0) {
|
||||||
|
printf("fuse_sideload umount failed: %s\n", strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fd.ffd) close(fd.ffd);
|
||||||
|
free(fd.hashes);
|
||||||
|
free(fd.block_data);
|
||||||
|
free(fd.extra_block);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,6 +126,7 @@ LOCAL_STATIC_LIBRARIES := \
|
||||||
libimgpatch \
|
libimgpatch \
|
||||||
libbsdiff \
|
libbsdiff \
|
||||||
libbspatch \
|
libbspatch \
|
||||||
|
libfusesideload \
|
||||||
libotafault \
|
libotafault \
|
||||||
librecovery \
|
librecovery \
|
||||||
libupdater \
|
libupdater \
|
||||||
|
|
|
@ -13,9 +13,24 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
TEST(SideloadTest, fusedevice) {
|
#include "fuse_sideload.h"
|
||||||
ASSERT_NE(-1, access("/dev/fuse", R_OK | W_OK));
|
|
||||||
|
TEST(SideloadTest, fuse_device) {
|
||||||
|
ASSERT_EQ(0, access("/dev/fuse", R_OK | W_OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
|
||||||
|
provider_vtab vtab;
|
||||||
|
vtab.close = [](void*) {};
|
||||||
|
|
||||||
|
ASSERT_EQ(-1, run_fuse_sideload(&vtab, nullptr, 4096, 4095));
|
||||||
|
ASSERT_EQ(-1, run_fuse_sideload(&vtab, nullptr, 4096, (1 << 22) + 1));
|
||||||
|
|
||||||
|
// Too many blocks.
|
||||||
|
ASSERT_EQ(-1, run_fuse_sideload(&vtab, nullptr, ((1 << 18) + 1) * 4096, 4096));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue